1; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca \
2; RUN:       -asan-use-after-return=runtime -S | FileCheck %s \
3; RUN:       --check-prefixes=CHECK,CHECK-RUNTIME
4; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca -asan-mapping-scale=5 \
5; RUN:       -asan-use-after-return=runtime -S | FileCheck %s \
6; RUN:       --check-prefixes=CHECK,CHECK-RUNTIME
7; RUN: opt < %s -passes='asan-pipeline'  -asan-stack-dynamic-alloca \
8; RUN:       -asan-use-after-return=always -S | FileCheck %s \
9; RUN:       --check-prefixes=CHECK,CHECK-ALWAYS \
10; RUN:       --implicit-check-not=__asan_option_detect_stack_use_after_return
11; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca \
12; RUN:       -asan-use-after-return=always -S | FileCheck %s \
13; RUN:       --check-prefixes=CHECK,CHECK-ALWAYS \
14; RUN:       --implicit-check-not=__asan_option_detect_stack_use_after_return
15target 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-S128"
16target triple = "x86_64-unknown-linux-gnu"
17
18define void @Func1() sanitize_address {
19entry:
20; CHECK-LABEL: Func1
21
22; CHECK: entry:
23; CHECK-RUNTIME: load i32, i32* @__asan_option_detect_stack_use_after_return
24; COM: CHECK-NORUNTIME-NOT: load i32, i32* @__asan_option_detect_stack_use_after_return
25
26; CHECK-RUNTIME: [[UAR_ENABLED_BB:^[0-9]+]]:
27; CHECK-RUNTIME: [[FAKE_STACK_RT:%[0-9]+]] = call i64 @__asan_stack_malloc_
28; CHECK-ALWAYS: [[FAKE_STACK_RT:%[0-9]+]] = call i64 @__asan_stack_malloc_always_
29
30; CHECK-RUNTIME: [[FAKE_STACK_BB:^[0-9]+]]:
31; CHECK-RUNTIME: [[FAKE_STACK:%[0-9]+]] = phi i64 [ 0, %entry ], [ [[FAKE_STACK_RT]], %[[UAR_ENABLED_BB]] ]
32; CHECK-RUNTIME: icmp eq i64 [[FAKE_STACK]], 0
33; CHECK-ALWAYS: icmp eq i64 [[FAKE_STACK_RT]], 0
34
35; CHECK: [[NO_FAKE_STACK_BB:^[0-9]+]]:
36; CHECK: %MyAlloca = alloca i8, i64
37; CHECK: [[ALLOCA:%[0-9]+]] = ptrtoint i8* %MyAlloca
38
39; CHECK-RUNTIME: phi i64 [ [[FAKE_STACK]], %[[FAKE_STACK_BB]] ], [ [[ALLOCA]], %[[NO_FAKE_STACK_BB]] ]
40; CHECK-ALWAYS: phi i64 [ [[FAKE_STACK_RT]], %entry ], [ [[ALLOCA]], %[[NO_FAKE_STACK_BB]] ]
41
42; CHECK: ret void
43
44  %XXX = alloca [20 x i8], align 1
45  %arr.ptr = bitcast [20 x i8]* %XXX to i8*
46  store volatile i8 0, i8* %arr.ptr
47  ret void
48}
49
50; Test that dynamic alloca is not used for functions with inline assembly.
51define void @Func2() sanitize_address {
52entry:
53; CHECK-LABEL: Func2
54; CHECK: alloca [96 x i8]
55; CHECK: ret void
56
57  %XXX = alloca [20 x i8], align 1
58  %arr.ptr = bitcast [20 x i8]* %XXX to i8*
59  store volatile i8 0, i8* %arr.ptr
60  call void asm sideeffect "mov %%rbx, %%rcx", "~{dirflag},~{fpsr},~{flags}"() nounwind
61  ret void
62}
63
64; Test that dynamic alloca is not used when setjmp is present.
65%struct.__jmp_buf_tag = type { [8 x i64], i32, %struct.__sigset_t }
66%struct.__sigset_t = type { [16 x i64] }
67@_ZL3buf = internal global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16
68
69define void @Func3() uwtable sanitize_address {
70; CHECK-LABEL: define void @Func3
71; CHECK-NOT: __asan_option_detect_stack_use_after_return
72; CHECK-NOT: __asan_stack_malloc
73; CHECK: call void @__asan_handle_no_return
74; CHECK: call void @longjmp
75; CHECK: ret void
76entry:
77  %a = alloca i32, align 4
78  %call = call i32 @_setjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @_ZL3buf, i32 0, i32 0)) nounwind returns_twice
79  %cmp = icmp eq i32 0, %call
80  br i1 %cmp, label %if.then, label %if.end
81
82if.then:                                          ; preds = %entry
83  call void @longjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @_ZL3buf, i32 0, i32 0), i32 1) noreturn nounwind
84  unreachable
85
86if.end:                                           ; preds = %entry
87  call void @_Z10escape_ptrPi(i32* %a)
88  ret void
89}
90
91declare i32 @_setjmp(%struct.__jmp_buf_tag*) nounwind returns_twice
92declare void @longjmp(%struct.__jmp_buf_tag*, i32) noreturn nounwind
93declare void @_Z10escape_ptrPi(i32*)
94