https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120947
Bug ID: 120947 Summary: __builtin_object_size should understand allocations pointed by pointers within a struct Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: siddhesh at gcc dot gnu.org Target Milestone: --- Considering this program: typedef __SIZE_TYPE__ size_t; extern void pin_pointer(void *); extern int some_value(void); struct magic_ { char unused[9]; // at least 9 }; struct magic_map { struct magic_ *magic; }; static int coalesce_entries(struct magic_ **ma) { size_t slen; slen = sizeof (**ma); *ma = __builtin_malloc (slen); for (unsigned i = 0; i < 1; i++) { char b[1024] = {}; struct magic_ *ptr = *ma; (void) __builtin___memcpy_chk (ptr, b, sizeof (*ptr), __builtin_dynamic_object_size (ptr, 0)); } return 0; } struct magic_map * apprentice_load(void) { char buf[128]; // did not shrink, but needs to be more than 100 struct magic_map *map2; map2 = __builtin_malloc (sizeof (*map2)); pin_pointer(&buf); coalesce_entries(&map2->magic); pin_pointer(map2); } $ gcc/cc1 -quiet -fdump-tree-objsz-details -O1 ../countedby.c -o /dev/null The objsz pass is unable to see the allocation assigned to *ma since the gimple_assign analysis is unable to determine if there's a usable estimate for object size and as a result, it bails out. This should be supportable to some extent by walking vdefs of *ma to see if we can find a definite allocation, like in this case.