xref: /llvm-project/llvm/test/CodeGen/WebAssembly/stack-protector.ll (revision 41080b2fdd4b6c57d5a2926d6157b9847342b3a1)
1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2; RUN: llc -verify-machineinstrs -mtriple=wasm32-unknown-unknown < %s | FileCheck -check-prefix=WASM32 %s
3
4@"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00"		; <ptr> [#uses=1]
5
6define void @test(ptr %a) nounwind ssp {
7; WASM32-LABEL: test:
8; WASM32:         .functype test (i32) -> ()
9; WASM32-NEXT:    .local i32
10; WASM32-NEXT:  # %bb.0: # %entry
11; WASM32-NEXT:    global.get __stack_pointer
12; WASM32-NEXT:    i32.const 32
13; WASM32-NEXT:    i32.sub
14; WASM32-NEXT:    local.tee 1
15; WASM32-NEXT:    global.set __stack_pointer
16; WASM32-NEXT:    local.get 1
17; WASM32-NEXT:    local.get 0
18; WASM32-NEXT:    i32.store 16
19; WASM32-NEXT:    local.get 1
20; WASM32-NEXT:    i32.const 0
21; WASM32-NEXT:    i32.load __stack_chk_guard
22; WASM32-NEXT:    i32.store 28
23; WASM32-NEXT:    local.get 1
24; WASM32-NEXT:    i32.const 20
25; WASM32-NEXT:    i32.add
26; WASM32-NEXT:    local.get 0
27; WASM32-NEXT:    call strcpy
28; WASM32-NEXT:    drop
29; WASM32-NEXT:    local.get 1
30; WASM32-NEXT:    local.get 1
31; WASM32-NEXT:    i32.const 20
32; WASM32-NEXT:    i32.add
33; WASM32-NEXT:    i32.store 0
34; WASM32-NEXT:    i32.const LC
35; WASM32-NEXT:    local.get 1
36; WASM32-NEXT:    call printf
37; WASM32-NEXT:    drop
38; WASM32-NEXT:    block
39; WASM32-NEXT:    i32.const 0
40; WASM32-NEXT:    i32.load __stack_chk_guard
41; WASM32-NEXT:    local.get 1
42; WASM32-NEXT:    i32.load 28
43; WASM32-NEXT:    i32.eq
44; WASM32-NEXT:    br_if 0 # 0: down to label0
45; WASM32-NEXT:  # %bb.1: # %return
46; WASM32-NEXT:    call __stack_chk_fail
47; WASM32-NEXT:    unreachable
48; WASM32-NEXT:  .LBB0_2: # %return
49; WASM32-NEXT:    end_block # label0:
50; WASM32-NEXT:    local.get 1
51; WASM32-NEXT:    i32.const 32
52; WASM32-NEXT:    i32.add
53; WASM32-NEXT:    global.set __stack_pointer
54; WASM32-NEXT:    # fallthrough-return
55entry:
56	%a_addr = alloca ptr		; <ptr> [#uses=2]
57	%buf = alloca [8 x i8]		; <ptr> [#uses=2]
58  %"alloca point" = bitcast i32 0 to i32		; <i32> [#uses=0]
59	store ptr %a, ptr %a_addr
60	%0 = load ptr, ptr %a_addr, align 4		; <ptr> [#uses=1]
61	%1 = call ptr @strcpy(ptr %buf, ptr %0) nounwind		; <ptr> [#uses=0]
62	%2 = call i32 (ptr, ...) @printf(ptr @"\01LC", ptr %buf) nounwind		; <i32> [#uses=0]
63	br label %return
64
65return:		; preds = %entry
66	ret void
67}
68
69define i32 @test_return_i32(ptr %a) nounwind ssp {
70; WASM32-LABEL: test_return_i32:
71; WASM32:         .functype test_return_i32 (i32) -> (i32)
72; WASM32-NEXT:    .local i32
73; WASM32-NEXT:  # %bb.0: # %entry
74; WASM32-NEXT:    global.get __stack_pointer
75; WASM32-NEXT:    i32.const 32
76; WASM32-NEXT:    i32.sub
77; WASM32-NEXT:    local.tee 1
78; WASM32-NEXT:    global.set __stack_pointer
79; WASM32-NEXT:    local.get 1
80; WASM32-NEXT:    local.get 0
81; WASM32-NEXT:    i32.store 16
82; WASM32-NEXT:    local.get 1
83; WASM32-NEXT:    i32.const 0
84; WASM32-NEXT:    i32.load __stack_chk_guard
85; WASM32-NEXT:    i32.store 28
86; WASM32-NEXT:    local.get 1
87; WASM32-NEXT:    i32.const 20
88; WASM32-NEXT:    i32.add
89; WASM32-NEXT:    local.get 0
90; WASM32-NEXT:    call strcpy
91; WASM32-NEXT:    drop
92; WASM32-NEXT:    local.get 1
93; WASM32-NEXT:    local.get 1
94; WASM32-NEXT:    i32.const 20
95; WASM32-NEXT:    i32.add
96; WASM32-NEXT:    i32.store 0
97; WASM32-NEXT:    i32.const LC
98; WASM32-NEXT:    local.get 1
99; WASM32-NEXT:    call printf
100; WASM32-NEXT:    drop
101; WASM32-NEXT:    block
102; WASM32-NEXT:    i32.const 0
103; WASM32-NEXT:    i32.load __stack_chk_guard
104; WASM32-NEXT:    local.get 1
105; WASM32-NEXT:    i32.load 28
106; WASM32-NEXT:    i32.eq
107; WASM32-NEXT:    br_if 0 # 0: down to label1
108; WASM32-NEXT:  # %bb.1: # %return
109; WASM32-NEXT:    call __stack_chk_fail
110; WASM32-NEXT:    unreachable
111; WASM32-NEXT:  .LBB1_2: # %return
112; WASM32-NEXT:    end_block # label1:
113; WASM32-NEXT:    local.get 1
114; WASM32-NEXT:    i32.const 32
115; WASM32-NEXT:    i32.add
116; WASM32-NEXT:    global.set __stack_pointer
117; WASM32-NEXT:    i32.const 0
118; WASM32-NEXT:    # fallthrough-return
119entry:
120  %a_addr = alloca ptr    ; <ptr> [#uses=2]
121  %buf = alloca [8 x i8]    ; <ptr> [#uses=2]
122  %"alloca point" = bitcast i32 0 to i32    ; <i32> [#uses=0]
123  store ptr %a, ptr %a_addr
124  %0 = load ptr, ptr %a_addr, align 4    ; <ptr> [#uses=1]
125  %1 = call ptr @strcpy(ptr %buf, ptr %0) nounwind    ; <ptr> [#uses=0]
126  %2 = call i32 (ptr, ...) @printf(ptr @"\01LC", ptr %buf) nounwind    ; <i32> [#uses=0]
127  br label %return
128
129return:    ; preds = %entry
130  ret i32 0
131}
132
133declare ptr @strcpy(ptr, ptr) nounwind
134
135declare i32 @printf(ptr, ...) nounwind
136