Bug 111030 - tree-object-size: incorrect sub-object size for VLA
Summary: tree-object-size: incorrect sub-object size for VLA
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2023-08-15 14:05 UTC by qinzhao
Modified: 2023-10-25 13:29 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description qinzhao 2023-08-15 14:05:32 UTC
current __builtin_dynamic_object_size cannot handle VLA correctly for the sub-object size, please see the following testing case:

#include <stdio.h>
#include <stddef.h>

#define expect(p, _v) do { \
    size_t v = _v; \
    if (p == v) \
        __builtin_printf ("ok:  %s == %zd\n", #p, p); \
    else \
        {  \
          __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \
        } \
} while (0);

#define noinline __attribute__((__noinline__))

static void noinline bar (int index)
{
  struct annotated {
    long foo;
    char b;
    char array[index];
    long c;
  } q, *p;

  p = &q;

  expect (__builtin_dynamic_object_size(p->array, 0), 
	  sizeof (struct annotated) - offsetof (struct annotated, array[0]));
  expect (__builtin_dynamic_object_size(p->array, 1), 
	  offsetof (struct annotated, array[index]) - offsetof (struct annotated, array[0]));
  return;
}

int main ()
{
  bar (10);
  return 0;
}

when compiled with the latest gcc and run:
/home/opc/Install/latest-d/bin/gcc -O t.c
ok:  __builtin_dynamic_object_size(p->array, 0) == 23
WAT: __builtin_dynamic_object_size(p->array, 1) == 23 (expected 10)
Comment 1 Sam James 2023-08-16 07:40:14 UTC
For completeness, the ML discussion was https://inbox.sourceware.org/gcc-patches/34DCF245-5E66-4FC3-B817-E3C205EB5484@oracle.com.