Hi, Sid, Thanks a lot.
> On Jul 31, 2023, at 1:07 PM, Siddhesh Poyarekar <siddh...@gotplt.org> wrote: > > On 2023-07-31 13:03, Siddhesh Poyarekar wrote: >> On 2023-07-31 12:47, Qing Zhao wrote: >>> Hi, Sid and Jakub, >>> >>> I have a question in the following source portion of the routine >>> “addr_object_size” of gcc/tree-object-size.cc: >>> >>> 743 bytes = compute_object_offset (TREE_OPERAND (ptr, 0), var); >>> 744 if (bytes != error_mark_node) >>> 745 { >>> 746 bytes = size_for_offset (var_size, bytes); >>> 747 if (var != pt_var && pt_var_size && TREE_CODE (pt_var) == >>> MEM_REF) >>> 748 { >>> 749 tree bytes2 = compute_object_offset (TREE_OPERAND (ptr, >>> 0), >>> 750 pt_var); >>> 751 if (bytes2 != error_mark_node) >>> 752 { >>> 753 bytes2 = size_for_offset (pt_var_size, bytes2); >>> 754 bytes = size_binop (MIN_EXPR, bytes, bytes2); >>> 755 } >>> 756 } >>> 757 } >>> >>> At line 754, why we always use “MIN_EXPR” whenever it’s for OST_MINIMUM or >>> not? >>> Shall we use >>> >>> (object_size_type & OST_MINIMUM >>> ? MIN_EXPR : MAX_EXPR) >>> >> That MIN_EXPR is not for OST_MINIMUM. It is to cater for allocations like >> this: >> typedef struct >> { >> int a; >> } A; >> size_t f() >> { >> A *p = malloc (1); >> return __builtin_object_size (p, 0); > > Correction, that should be __builtin_object_size (&p->a, 0) Okay. I see. Then if the size info from the TYPE is smaller than the size info from the malloc, then based on the current code, we use the smaller one between these two, i.e, the size info from the TYPE. (Even for the OST_MAXIMUM). Is such behavior correct? This is for the new “counted_by” attribute and how to use it in __builtin_dynamic_object_size. for example: ======= struct annotated { size_t foo; int array[] __attribute__((counted_by (foo))); }; #define noinline __attribute__((__noinline__)) #define SIZE_BUMP 2 /* in the following function, malloc allocated more space than the value of counted_by attribute. Then what's the correct behavior we expect the __builtin_dynamic_object_size should have? */ static struct annotated * noinline alloc_buf (int index) { struct annotated *p; p = malloc(sizeof (*p) + (index + SIZE_BUMP) * sizeof (int)); p->foo = index; /*when checking the observed access p->array, we have info on both observered allocation and observed access, A. from observed allocation: (index + SIZE_BUMP) * sizeof (int) B. from observed access: p->foo * sizeof (int) in the above, p->foo = index. */ /* for MAXIMUM size, based on the current code, we will use the size info from the TYPE, i.e, the “counted_by” attribute, which is the smaller one. */ expect(__builtin_dynamic_object_size(p->array, 1), (p->foo) * sizeof(int)); return p; } Is the above the correct behavior? thanks. Qing > >> } >> where the returned size should be 1 and not sizeof (int). The mode doesn't >> really matter in this case. >> HTH. >> Sid