[all-commits] [llvm/llvm-project] 69c2c6: [CodeGen] Fix -fsanitize=array-bounds for __sized_...

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


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

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

  Log Message:
  -----------
  [CodeGen] Fix -fsanitize=array-bounds for __sized_by / _or_null pointers

`EmitCountedByBoundsChecking()` 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 loaded bound is a byte count, but the
  element index was compared against it directly, so an access was only
  flagged once the index exceeded the byte count -- missing out-of-bounds
  accesses for a pointee larger than one byte. Scale the index to bytes
  ('index * sizeof(element)') before comparing. counted_by counts elements
  and is unchanged; a void (or otherwise zero-sized) pointee uses the GNU
  element size of 1, i.e. no scaling.

* __counted_by_or_null / __sized_by_or_null: a null pointer describes no
  accessible memory, but the null case was ignored, so an access through a
  null pointer with a non-zero count was not trapped. Treat the bound as 0
  when the pointer is null.

The index scaling is threaded through EmitBoundsCheckImpl via a new
IndexScale parameter; the runtime handler is still passed the unscaled
element index. Plain __counted_by and the general array-bounds path use a
scale of 1 and are unchanged.

Assisted-by: Claude Code

rdar://184139694



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