================
@@ -0,0 +1,134 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=alignment,null \
+// RUN:   -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-UBSAN
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CHECK-NO-UBSAN
+
+// Test that EmitAggregateCopy emits null and alignment checks when sanitizers
+// are enabled for aggregate copy operations with pointers.
+
+struct alignas(16) AlignedStruct {
+  int a;
+  int b;
+  int c;
+  int d;
+};
+
+struct NormalStruct {
+  int x;
+  int y;
+  int z;
+};
+
+// Stack-to-stack copies are optimized away (compiler knows they're valid)
+// CHECK-UBSAN-LABEL: define {{.*}}void @_Z19test_aligned_structv()
+// CHECK-NO-UBSAN-LABEL: define {{.*}}void @_Z19test_aligned_structv()
+void test_aligned_struct() {
+  AlignedStruct src = {1, 2, 3, 4};
+  AlignedStruct dest;
+
+  // CHECK-UBSAN: call void @llvm.memcpy
+  // CHECK-NO-UBSAN: call void @llvm.memcpy
+
+  dest = src;
+}
+
+// CHECK-UBSAN-LABEL: define {{.*}}void @_Z18test_normal_structv()
+// CHECK-NO-UBSAN-LABEL: define {{.*}}void @_Z18test_normal_structv()
+void test_normal_struct() {
----------------
vasu-the-sharma wrote:

You're right. I've revised test_normal_struct() to be more meaningful by adding 
a pointer-based variant (test_normal_struct_ptrs) that actually exercises the 
runtime checks. The original stack-to-stack copy doesn't add much value since 
it's optimized away. The new pointer-based test demonstrates that null checks 
are still performed even for normally-aligned structs.

https://github.com/llvm/llvm-project/pull/164548
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to