On Thu, Dec 5, 2024 at 2:28 PM Bill Wendling <isanb...@gmail.com> wrote: > On Thu, Dec 5, 2024 at 1:09 PM Qing Zhao <qing.z...@oracle.com> wrote: > > > On Dec 3, 2024, at 10:29, Qing Zhao <qing.z...@oracle.com> wrote: > > >> On Dec 3, 2024, at 10:07, Martin Uecker <uec...@tugraz.at> wrote: > > >> The language extension does not exist yet, so there is no problem. > > > Yeah, I should mention this as “corresponding future language extension” > > > -:) > > >> > > >> But I hope we will get it and then specify it so that this works > > >> correctly without this footgun. > > >> > > >> Of course, if GCC gets the "counted_by" attribute wrong, there will > > >> be arguments later in WG14 why the feature is then different to it. > > > > > > I think that we need to resolve this issue first in the design of > > > “counted_by” for pointer fields. > > > I guess that we might need to come up with some additional limitations > > > for using the “counted_by” > > > attribute for pointer fields at the source code level in order to avoid > > > such potential error. But not > > > sure what exactly the additional limitation should be at this moment. > > > > > > Need some study here. > > > > Actually, I found out that this is really not a problem with the current > > design, for the following new testing case I added for my current > > implementation of the counted_by for pointer field: > > > > [ gcc.dg]$ cat pointer-counted-by-7.c > > /* Test the attribute counted_by for pointer field and its usage in > > * __builtin_dynamic_object_size. */ > > /* { dg-do run } */ > > /* { dg-options "-O2" } */ > > > > #include "builtin-object-size-common.h" > > > > struct annotated { > > int b; > > int *c __attribute__ ((counted_by (b))); > > }; > > > > struct annotated *__attribute__((__noinline__)) setup (int attr_count) > > { > > struct annotated *p_array_annotated > > = (struct annotated *) malloc (sizeof (struct annotated)); > > p_array_annotated->c = (int *) malloc (sizeof (int) * attr_count); > > p_array_annotated->b = attr_count; > > > > return p_array_annotated; > > } > > > > > > int main(int argc, char *argv[]) > > { > > struct annotated *x = setup (10); > > int *p = x->c; > > x = setup (20); > > EXPECT(__builtin_dynamic_object_size (p, 1), 10 * sizeof (int)); > > EXPECT(__builtin_dynamic_object_size (x->c, 1), 20 * sizeof (int)); > > DONE (); > > } > > > > This test case passed without any issue. > > > > Our previous introduction of the new internal function .ACCESS_WITH_SIZE > > already resolved this issue. > > > > So, I think that as long as the whole structure is set at the same time, > > should be fine. > > > > Let me know if you have more comment here. > > > Nice! I think this is going to be an issue with Clang's > implementation. I'll need to create our version of .ACCESS_WITH_SIZE. > It might end up simplifying some of the code. :-)
So my assumption that our GetElementPointerInst instruction would "just work" in this case was wrong. By assigning x->c to p it loses pretty much all information, e.g. the __counted_by attribute. It looks like we'll have to go with a similar intrinsic to .ACCESS_WITH_SIZE. -bw