https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109557
Bug ID: 109557 Summary: __builtin_dynamic_object_size() does not work for simple testing case Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: qinzhao at gcc dot gnu.org Target Milestone: --- during my work for PR108896, I found that for the following small testing case: [opc@qinzhao-ol8u3-x86 108896]$ cat test.c #include <stdlib.h> #include <assert.h> 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, 0) == sizeof (struct P)); return; } int main() { store(7, 7); assert (__builtin_dynamic_object_size (p, 0) == sizeof (struct P)); free (p); } with gcc13, compiled with -O, the above first assertion succeed, but the second one failed. when checking the tree-object-size.cc, I found: 1377 static void 1378 expr_object_size (struct object_size_info *osi, tree ptr, tree value) 1379 { 1380 int object_size_type = osi->object_size_type; 1381 unsigned int varno = SSA_NAME_VERSION (ptr); 1382 tree bytes, wholesize; 1383 1384 gcc_assert (!object_sizes_unknown_p (object_size_type, varno)); 1385 gcc_assert (osi->pass == 0); 1386 1387 if (TREE_CODE (value) == WITH_SIZE_EXPR) 1388 value = TREE_OPERAND (value, 0); 1389 1390 /* Pointer variables should have been handled by merge_object_sizes. */ 1391 gcc_assert (TREE_CODE (value) != SSA_NAME 1392 || !POINTER_TYPE_P (TREE_TYPE (value))); 1393 1394 if (TREE_CODE (value) == ADDR_EXPR) 1395 addr_object_size (osi, value, object_size_type, &bytes, &wholesize); 1396 else 1397 bytes = wholesize = size_unknown (object_size_type); 1398 1399 object_sizes_set (osi, varno, bytes, wholesize); 1400 } in the above, for the 2nd __builtin_dynamic_object_size, the above line 1397 is called, therefore size_unknown was returned for it. 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)?