Just an update on this issue.
Finally, I resolved this issue with the following solution:
For the source code (portion):
"
struct annotated {
size_t foo;
char array[] __attribute__((counted_by (foo)));
};
p2->array[8] = 0;
“
C FE will generate the following: (*.005t.original)
*(.ACCESS_WITH_SIZE (p2->array, &p2->foo, 1, 8, -1) + 8) = 0;
i.e, the RETURN type of the call to .ACCESS_WITH_SIZE should be a pointer type
to char, char *
(Previously, the RETURN type of the call is char [])"
This resolved the issue nicely.
Let me know if you see any obvious issue with this solution.
thanks.
Qing
> On Nov 30, 2023, at 11:07 AM, Qing Zhao <[email protected]> wrote:
>
> Hi,
>
> 1. For the following source code (portion):
>
> struct annotated {
> size_t foo;
> char b;
> char array[] __attribute__((counted_by (foo)));
> };
>
> static void noinline bar ()
> {
> struct annotated *p2 = alloc_buf (10);
> p2->array[8] = 0;
> return;
> }
>
> 2. I modified C FE to generate the following code for the routine “bar”:
>
> ;; Function bar (null)
> ;; enabled by -tree-original
> {
> struct annotated * p2 = alloc_buf (10);
>
> struct annotated * p2 = alloc_buf (10);
> .ACCESS_WITH_SIZE ((char *) &p2->array, &p2->foo, 1, 8, -1)[8] = 0;
> return;
> }
>
> The gimpliflication asserted at:/home/opc/Install/latest-d/bin/gcc -O2
> -fdump-tree-all ttt_1.c
> ttt_1.c: In function ‘bar’:
> ttt_1.c:29:5: internal compiler error: in create_tmp_var, at
> gimple-expr.cc:488
> 29 | p2->array[8] = 0;
> | ~~^~~~~~~
>
> 3. The reason for this assertion failure is: (in gcc/gimplify.cc)
>
> 16686 case CALL_EXPR:
> 16687 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
> 16688
> 16689 /* C99 code may assign to an array in a structure returned
> 16690 from a function, and this has undefined behavior only on
> 16691 execution, so create a temporary if an lvalue is
> 16692 required. */
> 16693 if (fallback == fb_lvalue)
> 16694 {
> 16695 *expr_p = get_initialized_tmp_var (*expr_p, pre_p,
> post_p, false);
> 16696 mark_addressable (*expr_p);
> 16697 ret = GS_OK;
> 16698 }
> 16699 break;
>
> At Line 16695, when gimplifier tried to create a temporary value for the
> .ACCESS_WITH_SIZE function as:
> tmp = .ACCESS_WITH_SIZE ((char *) &p2->array, &p2->foo, 1, 8, -1);
>
> It asserted since the TYPE of the function .ACCESS_WITH_SIZE is an
> INCOMPLETE_TYPE (it’s the TYPE of p2->array, which is an incomplete type).
>
> 4. I am stuck on how to resolve this issue properly:
> The first question is:
>
> Where should we generate
> tmp = .ACCESS_WITH_SIZE ((char *) &p2->array, &p2->foo, 1, 8, -1)
>
> In C FE or in middle-end gimplification?
>
> Thanks a lot for your help.
>
> Qing
>