Skip to content

Commit

Permalink
Fix ASan for zero-sized variables passed as function argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanEngelen committed Jan 12, 2025
1 parent 268ad13 commit 254519a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,12 @@ void DtoVarDeclaration(VarDeclaration *vd) {

Type *type = isSpecialRefVar(vd) ? pointerTo(vd->type) : vd->type;

// We also allocate a variable for zero-sized variables, because they are technically not `null` when loaded.
// The x86_64 ABI "loads" zero-sized function arguments, and without an allocation ASan will report an error (Github #4816).
llvm::Value *allocainst;
bool isRealAlloca = false;
LLType *lltype = DtoType(type); // void for noreturn
if (lltype->isVoidTy() || gDataLayout->getTypeSizeInBits(lltype) == 0) {
if (lltype->isVoidTy()) {
allocainst = getNullPtr();
} else if (type != vd->type) {
allocainst = DtoAlloca(type, vd->toChars());
Expand Down
20 changes: 20 additions & 0 deletions tests/sanitizers/asan_gh4816.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Test extern(C) structs (zero sized structs) and zero-sized arrays. Github #4816

// REQUIRES: ASan

// RUN: %ldc -g -fsanitize=address %s -of=%t%exe
// RUN: %t%exe

auto foo(void[0] bar) { }

extern(C) struct S {}
auto foo(S s) { }

void main()
{
void[0] bar;
foo(bar);

S s;
foo(s);
}

0 comments on commit 254519a

Please sign in to comment.