Skip to content

Commit cf022ca

Browse files
authored
Fix ASan for zero-sized variables passed as function argument. (#4821)
Fixes #4816
1 parent f02744e commit cf022ca

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

gen/llvmhelpers.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,12 @@ void DtoVarDeclaration(VarDeclaration *vd) {
895895

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

898+
// We also allocate a variable for zero-sized variables, because they are technically not `null` when loaded.
899+
// The x86_64 ABI "loads" zero-sized function arguments, and without an allocation ASan will report an error (Github #4816).
898900
llvm::Value *allocainst;
899901
bool isRealAlloca = false;
900902
LLType *lltype = DtoType(type); // void for noreturn
901-
if (lltype->isVoidTy() || gDataLayout->getTypeSizeInBits(lltype) == 0) {
903+
if (lltype->isVoidTy()) {
902904
allocainst = getNullPtr();
903905
} else if (type != vd->type) {
904906
allocainst = DtoAlloca(type, vd->toChars());

tests/sanitizers/asan_gh4816.d

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test extern(C) structs (zero sized structs) and zero-sized arrays. Github #4816
2+
3+
// REQUIRES: ASan
4+
5+
// RUN: %ldc -g -fsanitize=address %s -of=%t%exe
6+
// RUN: %t%exe
7+
8+
auto foo(void[0] bar) { }
9+
10+
extern(C) struct S {}
11+
auto foo(S s) { }
12+
13+
void main()
14+
{
15+
void[0] bar;
16+
foo(bar);
17+
18+
S s;
19+
foo(s);
20+
}

0 commit comments

Comments
 (0)