llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][CodeGen] `alloc_size(1, 1)` asserts in addAllocSizeAttrFromRawRepr; `alloc_size(N, N)` emits IR that opt rejects

Open skaeads12 opened this issue 2 months ago • 1 comments

https://godbolt.org/z/sqhGsb1Tn (assertions trunk -O2 -c; also release trunk -emit-llvm and GCC 16.2)

void *b(int) __attribute__((alloc_size(1, 1)));
void *c(void) { return b(4); }
clang: /root/llvm-project/llvm/lib/IR/Attributes.cpp:2262: llvm::AttrBuilder& llvm::AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t): Assertion `RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"' failed.
 #12 clang::CodeGen::CodeGenModule::ConstructAttributeList(...)
 #13 clang::CodeGen::CodeGenModule::SetLLVMFunctionAttributes(...)
 #14 clang::CodeGen::CodeGenModule::SetFunctionAttributes(...)
 #15 clang::CodeGen::CodeGenModule::GetOrCreateLLVMFunction(...)

Sema accepts alloc_size(N, N) (handleAllocSizeAttr never compares the two indices), and GCC gives it the obvious meaning, size = arg * arg: after char *p = b(4);, GCC 16.2 folds __builtin_dynamic_object_size(p, 0) to 16. CodeGenModule::ConstructAttributeList then calls AttrBuilder::addAllocSizeAttr(0, 0). packAllocSizeArgs encodes that as 0 << 32 | 0 = 0, which is the raw value addAllocSizeAttrFromRawRepr reserves for "no allocsize", so the assertion fires. A release build emits allocsize(0,0) plus dereferenceable_or_null(16) on the call.

This is not specific to (1, 1). LLParser rejects identical allocsize indices (error: 'allocsize' indices can't refer to the same parameter), but LangRef does not state that restriction and the Verifier does not check it. So alloc_size(2, 2) does not assert and passes -fverify-intermediate-code, yet its -emit-llvm output (allocsize(1,1)) cannot be read back by opt (https://godbolt.org/z/hM3rrq1dv). Clang should either diagnose alloc_size(N, N) in Sema or not emit allocsize for it. The Verifier could also reject identical indices, as LLParser does.

Found by a fuzzer (generated C program), reduced by hand. Command: clang --target=x86_64-unknown-linux-gnu -O2 -c repro.c (same at -O0) Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29): assertion above. Release trunk f4085066128a (2026-09-29): compiles, emits allocsize(0,0). History: not a regression. Assertion builds 4.0.0, 10.0.0, 14.0.0, 19.1.0, 22.1.0 and 23.1.0 all hit the same assertion; 3.9.0 does not know the attribute (unknown attribute 'alloc_size' ignored), so this has been there since alloc_size support was added. Release builds (4.0.0, 23.1.0, trunk) compile it. Classification: crash on valid code. GCC 16.2 accepts it with -Wall -Wextra -pedantic and no diagnostics, and so does Clang's Sema. Related: none found (searched "Invalid allocsize arguments", alloc_size/allocsize issues). #137870 is a different alloc_size crash (variadic function, fixed).

skaeads12 avatar Sep 29 '26 16:09 skaeads12

@llvm/issue-subscribers-clang-codegen

Author: skaeads12

https://godbolt.org/z/sqhGsb1Tn (assertions trunk `-O2 -c`; also release trunk `-emit-llvm` and GCC 16.2)
void *b(int) __attribute__((alloc_size(1, 1)));
void *c(void) { return b(4); }
clang: /root/llvm-project/llvm/lib/IR/Attributes.cpp:2262: llvm::AttrBuilder&amp; llvm::AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t): Assertion `RawArgs &amp;&amp; "Invalid allocsize arguments -- given allocsize(0, 0)"' failed.
 #<!-- -->12 clang::CodeGen::CodeGenModule::ConstructAttributeList(...)
 #<!-- -->13 clang::CodeGen::CodeGenModule::SetLLVMFunctionAttributes(...)
 #<!-- -->14 clang::CodeGen::CodeGenModule::SetFunctionAttributes(...)
 #<!-- -->15 clang::CodeGen::CodeGenModule::GetOrCreateLLVMFunction(...)

Sema accepts alloc_size(N, N) (handleAllocSizeAttr never compares the two indices), and GCC gives it the obvious meaning, size = arg * arg: after char *p = b(4);, GCC 16.2 folds __builtin_dynamic_object_size(p, 0) to 16. CodeGenModule::ConstructAttributeList then calls AttrBuilder::addAllocSizeAttr(0, 0). packAllocSizeArgs encodes that as 0 &lt;&lt; 32 | 0 = 0, which is the raw value addAllocSizeAttrFromRawRepr reserves for "no allocsize", so the assertion fires. A release build emits allocsize(0,0) plus dereferenceable_or_null(16) on the call.

This is not specific to (1, 1). LLParser rejects identical allocsize indices (error: 'allocsize' indices can't refer to the same parameter), but LangRef does not state that restriction and the Verifier does not check it. So alloc_size(2, 2) does not assert and passes -fverify-intermediate-code, yet its -emit-llvm output (allocsize(1,1)) cannot be read back by opt (https://godbolt.org/z/hM3rrq1dv). Clang should either diagnose alloc_size(N, N) in Sema or not emit allocsize for it. The Verifier could also reject identical indices, as LLParser does.

Found by a fuzzer (generated C program), reduced by hand. Command: clang --target=x86_64-unknown-linux-gnu -O2 -c repro.c (same at -O0) Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29): assertion above. Release trunk f4085066128a (2026-09-29): compiles, emits allocsize(0,0). History: not a regression. Assertion builds 4.0.0, 10.0.0, 14.0.0, 19.1.0, 22.1.0 and 23.1.0 all hit the same assertion; 3.9.0 does not know the attribute (unknown attribute 'alloc_size' ignored), so this has been there since alloc_size support was added. Release builds (4.0.0, 23.1.0, trunk) compile it. Classification: crash on valid code. GCC 16.2 accepts it with -Wall -Wextra -pedantic and no diagnostics, and so does Clang's Sema. Related: none found (searched "Invalid allocsize arguments", alloc_size/allocsize issues). #137870 is a different alloc_size crash (variadic function, fixed).