[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-07-17 Thread qinzhao at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

--- Comment #8 from qinzhao at gcc dot gnu.org ---
with the following slightly modified testing case, the same issue:
#include 
#include 
struct P {
  int k;
  int x[10]; 
} *p;

void store(int a, int b) 
{
  p = (struct P *)malloc (sizeof (struct P));
  p->k = a;
  p->x[b] = 0;
  assert (__builtin_dynamic_object_size (p, 1) == sizeof (struct P));
  return;
}

int main()
{
  store(7, 7);
  assert (__builtin_dynamic_object_size (p, 1) == sizeof (struct P));
  free (p);
}
[opc@qinzhao-ol8u3-x86 109557]$ sh t
/home/opc/Install/latest/bin/gcc -O -fsanitize=bounds -fsanitize=object-size
-fstrict-flex-arrays=3 -fdump-tree-all t.c
a.out: t.c:20: main: Assertion `__builtin_dynamic_object_size (p, 1) == sizeof
(struct P)' failed.
t: line 19: 629958 Aborted (core dumped) ./a.out

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread qinzhao at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

--- Comment #7 from qinzhao at gcc dot gnu.org ---
Okay, thanks for the comment. I see why this should not work.

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread siddhesh at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

Siddhesh Poyarekar  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #6 from Siddhesh Poyarekar  ---
(In reply to Martin Uecker from comment #5)
> With "in general" I meant in a different program.  From just knowing the
> type of the pointer you can not derive the object size.  This is how I
> understood the original question.

Ah ok, agreed.  Closing this as invalid then; I noticed I was missing the
inline keyword when I tried forcing inlining, so that was also a PEBKAC.

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread muecker at gwdg dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

--- Comment #5 from Martin Uecker  ---
(In reply to Siddhesh Poyarekar from comment #4)
> (In reply to Martin Uecker from comment #3)
> > I general the pointer could point to the first object of an array that has
> > more elements, or to an object of a different type.
> 
> How so?  p in comment 0 is just a NULL-initialized pointer.  It gets
> assigned to a malloc'd storage in store() (which the code in main() cannot
> see) but until then, it's a NULL pointer.

With "in general" I meant in a different program.  From just knowing the type
of the pointer you can not derive the object size.  This is how I understood
the original question.

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread siddhesh at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

--- Comment #4 from Siddhesh Poyarekar  ---
(In reply to Martin Uecker from comment #3)
> I general the pointer could point to the first object of an array that has
> more elements, or to an object of a different type.

How so?  p in comment 0 is just a NULL-initialized pointer.  It gets assigned
to a malloc'd storage in store() (which the code in main() cannot see) but
until then, it's a NULL pointer.

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread muecker at gwdg dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

Martin Uecker  changed:

   What|Removed |Added

 CC||muecker at gwdg dot de

--- Comment #3 from Martin Uecker  ---
I general the pointer could point to the first object of an array that has more
elements, or to an object of a different type.  The semantics of C are not
strong enough here, but it would be good to have some kind of annotation for
the pointer that would allow this.

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread siddhesh at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

--- Comment #2 from Siddhesh Poyarekar  ---
(In reply to qinzhao from comment #0)
> I am wondering for 
> p.3_1 = p;
> _2 = __builtin_object_size (p.3_1, 0);
> 
> why the size of p.3_1 cannot use the TYPE_SIZE of the pointee of p when its
> size can be determined (i.e, not a structure with a flexible array member,
> etc)?

To answer this specific question, it's because the compiler can't see in main()
if p is pointing to any actual storage.

[Bug middle-end/109557] __builtin_dynamic_object_size() does not work for simple testing case

2023-04-19 Thread siddhesh at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557

--- Comment #1 from Siddhesh Poyarekar  ---
The __bdos call itself cannot succeed in main() because it cannot see the
allocation in store().  One way it could succeed is if store() was inlined, but
for some reason it doesn't, even if you make the function static inline.

If I decorate store() with __attribute__((inline)) I get the warning:

foo.c:10:1: warning: ‘always_inline’ function might not be inlinable
[-Wattributes]

but it seems to proceed to inline the call because the assert in main() is no
longer hit.

So from the __bdos context I'm inclined to say NOTABUG.