Skip to content

Commit 085b04b

Browse files
authored
[asan] Pre-commit test with global constructor without any global (#104620)
In this test `@initializer()` can access globals outside of the module, but Asan does nothing to detect that.
1 parent 5ef2456 commit 085b04b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clangxx_asan %min_macos_deployment_target=10.11 -O0 %s %p/Helpers/initialization-bug-extra.cpp -o %t
2+
// RUN: %env_asan_opts=check_initialization_order=true:strict_init_order=true not %run %t 2>&1 | FileCheck %s
3+
4+
// Not implemented.
5+
// XFAIL: *
6+
7+
// Do not test with optimization -- the error may be optimized away.
8+
9+
// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
10+
// XFAIL: target={{.*windows-msvc.*}}
11+
12+
#include <stdio.h>
13+
14+
extern int y;
15+
16+
void __attribute__((constructor)) ctor() {
17+
printf("%d\n", y);
18+
// CHECK: AddressSanitizer: initialization-order-fiasco
19+
}
20+
21+
int main() {
22+
// ASan should have caused an exit before main runs.
23+
printf("PASS\n");
24+
// CHECK-NOT: PASS
25+
return 0;
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --global-value-regex ".*global_ctors.*" --version 5
2+
; RUN: opt < %s -passes=asan -S | FileCheck %s
3+
; RUN: opt < %s -passes=asan -S -asan-initialization-order=0 | FileCheck %s --check-prefixes=NOINIT
4+
5+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__late_ctor, ptr null }]
9+
10+
declare void @initializer() uwtable;
11+
12+
;.
13+
; CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__late_ctor, ptr null }, { i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr @asan.module_ctor }]
14+
;.
15+
; NOINIT: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__late_ctor, ptr null }, { i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr @asan.module_ctor }]
16+
;.
17+
define internal void @__late_ctor() sanitize_address section ".text.startup" {
18+
; CHECK-LABEL: define internal void @__late_ctor(
19+
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] section ".text.startup" {
20+
; CHECK-NEXT: [[ENTRY:.*:]]
21+
; CHECK-NEXT: call void @initializer()
22+
; CHECK-NEXT: ret void
23+
;
24+
; NOINIT-LABEL: define internal void @__late_ctor(
25+
; NOINIT-SAME: ) #[[ATTR1:[0-9]+]] section ".text.startup" {
26+
; NOINIT-NEXT: [[ENTRY:.*:]]
27+
; NOINIT-NEXT: call void @initializer()
28+
; NOINIT-NEXT: ret void
29+
;
30+
entry:
31+
32+
call void @initializer()
33+
ret void
34+
}

0 commit comments

Comments
 (0)