[all-commits] [llvm/llvm-project] 3dd51c: [CodeGen] Fix __builtin_dynamic_object_size for __...

Dan Liew via All-commits all-commits at lists.llvm.org
Wed Aug 5 11:39:55 PDT 2026


  Branch: refs/heads/users/delcypher/fix-or_null_sized_by-codegen-bugs-part-2
  Home:   https://github.com/llvm/llvm-project
  Commit: 3dd51c358dc7486f2d6dbd7e7387b3ea28627480
      https://github.com/llvm/llvm-project/commit/3dd51c358dc7486f2d6dbd7e7387b3ea28627480
  Author: Dan Liew <dliew at apple.com>
  Date:   2026-08-05 (Wed, 05 Aug 2026)

  Changed paths:
    M clang/lib/CodeGen/CGBuiltin.cpp
    M clang/test/CodeGen/attr-counted-by-for-pointers.c
    A clang/test/CodeGen/attr-counted-by-or-null-for-pointers.c
    M clang/test/CodeGen/attr-sized-by-for-pointers.c
    A clang/test/CodeGen/attr-sized-by-or-null-for-pointers.c

  Log Message:
  -----------
  [CodeGen] Fix __builtin_dynamic_object_size for __sized_by / _or_null pointers

`emitCountedByPointerSize()` assumed a CountAttributedType is always
a __counted_by pointer. That isn't true, there are four versions of the
attribute:

* `__counted_by`: Already handled correctly.
* `__counted_by_or_null`: Incorectly handled.
* `__sized_by`: Incorreclty handled.
* `__sized_by_or_null`: Incorreclty handled.

In particular:

* __sized_by / __sized_by_or_null: the attribute argument is a byte count,
  but the object size was computed as count * sizeof(*ptr), over-reporting by
  the element size for any pointee larger than one byte. Use the count
  directly for the byte-counting variants.

* __counted_by_or_null / __sized_by_or_null: a null pointer describes no
  accessible memory, but the null case was ignored. Emit
  'count = ptr ? count : 0' so __bdos reports 0 for a null pointer. The null
  check needs the pointer value; where the size query already has one emitted
  (the pass_dynamic_object_size path, or a non-volatile field) it reuses that
  value rather than re-loading the field, so a volatile pointer field is
  loaded once rather than twice (except in the '&ptr[idx]' form, where the
  pointer is re-emitted; see the FIXME). A direct __bdos(p->buf) on a volatile
  field never reaches this path: like sizeof, __bdos bails to the default
  rather than emit a side-effecting volatile load just to answer a size query,
  so the field is loaded zero times (see test_bdos_of_volatile_pointer).

* __sized_by on a pointer to an incomplete (forward-declared) type
  crashed: the annotated pointee's element size was computed
  unconditionally, and getTypeSizeInChars() asserts on an incomplete
  type. The element size is only needed for __counted_by (whose pointee
  Sema guarantees is complete at a use site) and for scaling an index
  (whose element type is complete because it is being subscripted), so
  only compute it there.

Assisted-by: Claude Code

rdar://184139638



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list