llvm / llvm/llvm-project

Clang does not detect misaligned store

Open
#199,785 4 comments 0 reactions 0 assignees View on GitHub
c++ compiler-rt:ubsan undefined behaviour
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/z6r5vqe4s

```c++
struct A {
int a;
int b;
double c;
};

struct B {
char c;
A a[10];
} __attribute__((packed));

alignas(32) B b[2];
A* p = &b[1].a[5];

A v;

int main() {
*p = {
.a = 3,
.b = 5,
.c = 7,
};
}
```

It still produces an aligned store in IR.

```llvm
define dso_local noundef i32 @main() #0 !func_sanitize !39 {
entry:
%ref.tmp = alloca %struct.A, align 8
%a = getelementptr inbounds nuw %struct.A, ptr %ref.tmp, i32 0, i32 0
store i32 3, ptr %a, align 8
%b = getelementptr inbounds nuw %struct.A, ptr %ref.tmp, i32 0, i32 1
store i32 5, ptr %b, align 4
%c = getelementptr inbounds nuw %struct.A, ptr %ref.tmp, i32 0, i32 2
store double 7.000000e+00, ptr %c, align 8
%0 = load ptr, ptr @p, align 8
call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %ref.tmp, i64 16, i1 false)
ret i32 0
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.