On Thu, Jul 24, 2025 at 8:03 AM Martin Uecker <ma.uec...@gmail.com> wrote:
> Am Donnerstag, dem 24.07.2025 um 14:08 +0000 schrieb Aaron Ballman:
> > On Wed, Jul 23, 2025 at 8:38 PM Martin Uecker <ma.uec...@gmail.com> wrote:
> > > > That said, John McCall pointed out some usage patterns Apple has with
> > > > their existing feature:
> > > >
> > > > * 655 simple references to variables or struct members: 
> > > > __counted_by(len)
> > > > * 73 dereferences of variables or struct members: __counted_by(*lenp)
> > > > * 80 integer literals: __counted_by(8)
> > > > * 60 macro references: __counted_by(NUM_EIGHT) [1]
> > > > * 9 simple sizeof expressions: __counted_by(sizeof(eight_bytes_t))
> > > > * 28 others my script couldn’t categorize:
> > > >   * 7 more complicated integer constant expressions:
> > > > __counted_by(num_bytes_for_bits(NUM_FIFTY_SEVEN)) [2]
> > > >   * 16 arithmetically-adjusted references to a single variable or
> > > > struct member: __counted_by(2 * len + 8)
> > > >   * 1 nested struct member: __counted_by(header.len)
> > > >   * 4 combinations of struct members: __counted_by(len + cnt) [3]
> > > >
> > > > Do the Linux kernel folks think this looks somewhat like what their
> > > > usage patterns will be as well? If so, I'd like to argue for my
> > > > personal stake in the ground: we don't need any new language features
> > > > to solve this problem, we can use the existing facilities to do so and
> > > > downscope the initial feature set until a better solution comes along
> > > > for forward references. Use two attributes: counted_by (whose argument
> > > > specifies an already in-scope identifier of what holds the count) and
> > > > counts (whose argument specifies an already in-scope identifier of
> > > > what it counts). e.g.,
> > > > ```
> > > > struct S {
> > > >   char *start_buffer;
> > > >   int start_len __counts(start_buffer);
> > > >   int end_len;
> > > >   char *end_buffer __counted_by(end_len);
> > > > };
> > > >
> > > > void func(char *buffer, int N __counts(buffer), int M, char *buffer
> > > > __counted_by(M));
> > > > ```
> > > > It's kind of gross to need two attributes to do the same notional
> > > > thing, but it does solve the vast majority of the usages seen in the
> > > > wild if you're willing to accept some awkwardness around things like:
> > > > ```
> > > > struct S {
> > > >   char *buffer;
> > > >   int *len __counts(buffer); // Note that len is a pointer
> > > > };
> > > > ```
> > > > because we'd need the semantics of `counts` to include dereferencing
> > > > to the `int` in order to be a valid count. We'd be sacrificing the
> > > > ability to handle the "others my script couldn't categorize", but
> > > > that's 28 out of the 905 total cases and maybe that's acceptable?
> > >
> > > So what do you think about the solution Qing mentioned:
> > >
> > > struct {
> > >   char *buf __counted_by_expr(int len; len + 7);
> > >   int len;
> > > };
> > >
> > > which would be very flexible and support all possible use cases
> > > and has no parsing or semantic interpretation issues.
> >
> > Personally, I'm not excited by it because one of the big sticking
> > points in the Clang community is shared header files with C++. Because
> > these attributes are used on structures and functions, the two most
> > common things you'll find in a shared header file, we *really* want
> > the feature to be workable in both languages to the greatest extent
> > possible. And once we care about C++, things get so much harder due to
> > the extra complexity it brings. So, for example, we'd have to figure
> > out how to handle things like:
> > ```
> > template <typename Ty>
> > struct S {
> >   char *buffer __counted_by_expr(Ty len; len + 7);
> >   int len; // Oooooops
> > };
> >
> > template <typename Ty, typename Uy>
> > struct T {
> >   char *buffer __counted_by_expr(Ty len; len + 7);
> >   Uy len; // Grrrrr
> > };
> > ```
>
> For my understanding: What is the problem here?  I would be an
> error if the declared type of len is inconsistent between the
> attribute and the type that cames later in the member. I guess
> a compiler could also warn already when it sees a template
> like this where it refers to different template arguments.

Also, C++ already has complex rules regarding scoping. I'm not sure if
forward declarations are needed or are even a benefit in these
examples, because they aren't valid in C and thus we only need to care
about how C++ handles them.

> But then, templates also certainly do not appear in shared headers,
> so I am not sure why Clang could not simmply offer both,
> a late-parsing version and also a C-compatible __counted_by_expr.
>
> I can understand if this is not your first choice,  but it seems
> to be a reasonable compromise to me.

By '__counted_by_expr', I assume you mean the one with forward declarations?

> > I think it's possible to handle these situations, but we'd have to sit
> > down and think through all the edge cases and whether we can handle
> > them all with some reasonable QoI. I think we'd ultimately run into
> > the same concerns here as we ran into with forward declared
> > parameters. I think the reason folks in Clang are more comfortable
> > with late parsing is because it means the user doesn't have to repeat
> > the type and the name which makes for less chances for the user to
> > screw things up and get into these weird situations. There can be
> > other weird situations with late parsing too, of course, but I think
> > the scope of those edge cases is a bit narrower.
>
> TBH, I am not terrible convinced about this argument.
>
> If I understood it correctly, the late parsing design seems to make
> no distinctions between which identifiers is used, the local or
> the global one and just prefers the local one if it exists, possibly
> giving a warning if there is also a global one.

Yes...kinda. The order of name lookup would essentially be: field
within a struct, any non-global scope, global scope. (As Kees pointed
out, there would be times when we need to support function calls and
counts in sub-structs, but those are handled by this convention.) The
only part of this ordering that *isn't* part of normal C identifier
resolution is the "field within a struct" part. The question about
whether or not this would cause "confusion" to C programmers isn't
completely settled, however Apple says that they have a lot of users
and have yet to run into anyone who was confused by it. While just
anecdotal evidence, it's a good indicator that people would use the
feature "correctly."

> My C++ examples shows that you can easily run into UB here in C++,
> especially since subtle differnt rules apply in different but very
> similar scenarious. How can this not be error prone?
>
> The forward declaration, the [.N] syntax, and also __self__ etc.
> would all make this explicit which identifiers is meant.

The dot-notation is a nice way to make explicit the programmer's
intent. There were several issues people brought up---one being that
it may be too "easy" to forget the dot ('.') and get a global rather
than a field. It's also a departure from how expressions are written
in C/C++.

> > The other downside is that we have more attributes that need to
> > support something similar, like the thread safety attributes (which I
> > believe is also an important use case for the Linux kernel folks?). We
> > could do this dance on a per-attribute basis, but if the solution
> > worked for all attributes *and* array extents at the same time, that
> > would be nice. Not certain it's a requirement though.
>
> True.  But if it is to work properly for arrays in C too, then the
> C constraints are also important IMHO, not just the C++ rules.

Aaron, I didn't notice information about array extents in the original
bounds safety RFC. Is Clang currently treating this as it does
whatever's inside the attribute or has it been implemented yet?

Just to be clear, my delayed parsing proposal wasn't meant to solve
this issue, because I don't know enough about the feature to make a
coherent proposal. However, I do like the dot-notation method if
delayed parsing isn't appropriate for it.

> > > The the thing is that WG14 had (weak) consensus for parameter
> > > forward declarations and  I think more consensus for [.N]
> > > syntax in structures already.  So I had hoped that we will be
> > > able to make progress on this.
> >
> > Question on the .N syntax: I thought I heard that this was something
> > GCC could handle, but that it still requires late parsing to ensure
> > type information for N is available and that was a problem. e.g.,
> >
> > void func(char *buffer __counted_by(.N * sizeof(.N)), int N);
> >
> > where we'd need to know both the name and the type. Am I wrong about
> > that being a challenge for GCC to support?
>
> I think it is generally a challenge to support.  One could certainly
> store away the tokens and parse them later (this is certainly doable),
> but it adds a lot of issues because you need to add a lot of constraints
> for things which should then not be allwoed.  And it is still not an
> acceptable solution for size arguments in C.
>
> .N would work here if you combine with a rule such as ".N" is always
> converted to "size_t".   Or you require an explicit cast if is different
> to "size_t" .

Does this mean that the example above would be treated essentially like:

  void func(char *buffer __counted_by((size_t).N * sizeof((size_t).N)), int N);

?

-bw

Reply via email to