llvm.org GIT mirror llvm / 89329e9
[asan] Preserve flags in asm instrumentation. Patch by Yuri Gorshenin. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208296 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 6 years ago
4 changed file(s) with 68 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
176176 MCStreamer &Out) override;
177177 };
178178
179 void X86AddressSanitizer64::InstrumentMemOperandImpl(
180 X86Operand *Op, unsigned AccessSize, bool IsWrite, MCContext &Ctx,
181 MCStreamer &Out) {
179 void X86AddressSanitizer64::InstrumentMemOperandImpl(X86Operand *Op,
180 unsigned AccessSize,
181 bool IsWrite,
182 MCContext &Ctx,
183 MCStreamer &Out) {
182184 // FIXME: emit .cfi directives for correct stack unwinding.
183 // Set %rsp below current red zone (128 bytes wide)
184 EmitInstruction(Out, MCInstBuilder(X86::SUB64ri32).addReg(X86::RSP)
185 .addReg(X86::RSP).addImm(128));
185
186 // Set %rsp below current red zone (128 bytes wide) using LEA instruction to
187 // preserve flags.
188 {
189 MCInst Inst;
190 Inst.setOpcode(X86::LEA64r);
191 Inst.addOperand(MCOperand::CreateReg(X86::RSP));
192
193 const MCExpr *Disp = MCConstantExpr::Create(-128, Ctx);
194 X86Operand *Op =
195 X86Operand::CreateMem(0, Disp, X86::RSP, 0, 1, SMLoc(), SMLoc());
196 Op->addMemOperands(Inst, 5);
197 EmitInstruction(Out, Inst);
198 }
186199 EmitInstruction(Out, MCInstBuilder(X86::PUSH64r).addReg(X86::RDI));
187200 {
188201 MCInst Inst;
199212 EmitInstruction(Out, MCInstBuilder(X86::CALL64pcrel32).addExpr(FuncExpr));
200213 }
201214 EmitInstruction(Out, MCInstBuilder(X86::POP64r).addReg(X86::RDI));
202 EmitInstruction(Out, MCInstBuilder(X86::ADD64ri32).addReg(X86::RSP)
203 .addReg(X86::RSP).addImm(128));
215
216 // Restore old %rsp value.
217 {
218 MCInst Inst;
219 Inst.setOpcode(X86::LEA64r);
220 Inst.addOperand(MCOperand::CreateReg(X86::RSP));
221
222 const MCExpr *Disp = MCConstantExpr::Create(128, Ctx);
223 X86Operand *Op =
224 X86Operand::CreateMem(0, Disp, X86::RSP, 0, 1, SMLoc(), SMLoc());
225 Op->addMemOperands(Inst, 5);
226 EmitInstruction(Out, Inst);
227 }
204228 }
205229
206230 } // End anonymous namespace
33 target triple = "x86_64-unknown-linux-gnu"
44
55 ; CHECK-LABEL: mov1b
6 ; CHECK: subq $128, %rsp
6 ; CHECK: leaq -128(%rsp), %rsp
77 ; CHECK-NEXT: pushq %rdi
88 ; CHECK-NEXT: leaq {{.*}}, %rdi
99 ; CHECK-NEXT: callq __sanitizer_sanitize_load1@PLT
1010 ; CHECK-NEXT: popq %rdi
11 ; CHECK-NEXT: addq $128, %rsp
11 ; CHECK-NEXT: leaq 128(%rsp), %rsp
1212
13 ; CHECK: subq $128, %rsp
13 ; CHECK: leaq -128(%rsp), %rsp
1414 ; CHECK-NEXT: pushq %rdi
1515 ; CHECK-NEXT: leaq {{.*}}, %rdi
1616 ; CHECK-NEXT: callq __sanitizer_sanitize_store1@PLT
1717 ; CHECK-NEXT: popq %rdi
18 ; CHECK-NEXT: addq $128, %rsp
18 ; CHECK-NEXT: leaq 128(%rsp), %rsp
1919
2020 ; CHECK: movb {{.*}}, {{.*}}
2121 define void @mov1b(i8* %dst, i8* %src) #0 {
2525 }
2626
2727 ; CHECK-LABEL: mov2b
28 ; CHECK: subq $128, %rsp
28 ; CHECK: leaq -128(%rsp), %rsp
2929 ; CHECK-NEXT: pushq %rdi
3030 ; CHECK-NEXT: leaq {{.*}}, %rdi
3131 ; CHECK-NEXT: callq __sanitizer_sanitize_load2@PLT
3232 ; CHECK-NEXT: popq %rdi
33 ; CHECK-NEXT: addq $128, %rsp
33 ; CHECK-NEXT: leaq 128(%rsp), %rsp
3434
35 ; CHECK: subq $128, %rsp
35 ; CHECK: leaq -128(%rsp), %rsp
3636 ; CHECK-NEXT: pushq %rdi
3737 ; CHECK-NEXT: leaq {{.*}}, %rdi
3838 ; CHECK-NEXT: callq __sanitizer_sanitize_store2@PLT
3939 ; CHECK-NEXT: popq %rdi
40 ; CHECK-NEXT: addq $128, %rsp
40 ; CHECK-NEXT: leaq 128(%rsp), %rsp
4141
4242 ; CHECK: movw {{.*}}, {{.*}}
4343 define void @mov2b(i16* %dst, i16* %src) #0 {
4747 }
4848
4949 ; CHECK-LABEL: mov4b
50 ; CHECK: subq $128, %rsp
50 ; CHECK: leaq -128(%rsp), %rsp
5151 ; CHECK-NEXT: pushq %rdi
5252 ; CHECK-NEXT: leaq {{.*}}, %rdi
5353 ; CHECK-NEXT: callq __sanitizer_sanitize_load4@PLT
5454 ; CHECK-NEXT: popq %rdi
55 ; CHECK-NEXT: addq $128, %rsp
55 ; CHECK-NEXT: leaq 128(%rsp), %rsp
5656
57 ; CHECK: subq $128, %rsp
57 ; CHECK: leaq -128(%rsp), %rsp
5858 ; CHECK-NEXT: pushq %rdi
5959 ; CHECK-NEXT: leaq {{.*}}, %rdi
6060 ; CHECK-NEXT: callq __sanitizer_sanitize_store4@PLT
6161 ; CHECK-NEXT: popq %rdi
62 ; CHECK-NEXT: addq $128, %rsp
62 ; CHECK-NEXT: leaq 128(%rsp), %rsp
6363
6464 ; CHECK: movl {{.*}}, {{.*}}
6565 define void @mov4b(i32* %dst, i32* %src) #0 {
6969 }
7070
7171 ; CHECK-LABEL: mov8b
72 ; CHECK: subq $128, %rsp
72 ; CHECK: leaq -128(%rsp), %rsp
7373 ; CHECK-NEXT: pushq %rdi
7474 ; CHECK-NEXT: leaq {{.*}}, %rdi
7575 ; CHECK-NEXT: callq __sanitizer_sanitize_load8@PLT
7676 ; CHECK-NEXT: popq %rdi
77 ; CHECK-NEXT: addq $128, %rsp
77 ; CHECK-NEXT: leaq 128(%rsp), %rsp
7878
79 ; CHECK: subq $128, %rsp
79 ; CHECK: leaq -128(%rsp), %rsp
8080 ; CHECK-NEXT: pushq %rdi
8181 ; CHECK-NEXT: leaq {{.*}}, %rdi
8282 ; CHECK-NEXT: callq __sanitizer_sanitize_store8@PLT
8383 ; CHECK-NEXT: popq %rdi
84 ; CHECK-NEXT: addq $128, %rsp
84 ; CHECK-NEXT: leaq 128(%rsp), %rsp
8585
8686 ; CHECK: movq {{.*}}, {{.*}}
8787 define void @mov8b(i64* %dst, i64* %src) #0 {
9191 }
9292
9393 ; CHECK-LABEL: mov16b
94 ; CHECK: subq $128, %rsp
94 ; CHECK: leaq -128(%rsp), %rsp
9595 ; CHECK-NEXT: pushq %rdi
9696 ; CHECK-NEXT: leaq {{.*}}, %rdi
9797 ; CHECK-NEXT: callq __sanitizer_sanitize_load16@PLT
9898 ; CHECK-NEXT: popq %rdi
99 ; CHECK-NEXT: addq $128, %rsp
99 ; CHECK-NEXT: leaq 128(%rsp), %rsp
100100
101 ; CHECK: subq $128, %rsp
101 ; CHECK: leaq -128(%rsp), %rsp
102102 ; CHECK-NEXT: pushq %rdi
103103 ; CHECK-NEXT: leaq {{.*}}, %rdi
104104 ; CHECK-NEXT: callq __sanitizer_sanitize_store16@PLT
105105 ; CHECK-NEXT: popq %rdi
106 ; CHECK-NEXT: addq $128, %rsp
106 ; CHECK-NEXT: leaq 128(%rsp), %rsp
107107
108108 ; CHECK: movaps {{.*}}, {{.*}}
109109 define void @mov16b(<2 x i64>* %dst, <2 x i64>* %src) #0 {
55 .type mov1b,@function
66 # CHECK-LABEL: mov1b:
77 #
8 # CHECK: subq $128, %rsp
8 # CHECK: leaq -128(%rsp), %rsp
99 # CHECK-NEXT: pushq %rdi
1010 # CHECK-NEXT: leaq (%rsi), %rdi
1111 # CHECK-NEXT: callq __sanitizer_sanitize_load1@PLT
1212 # CHECK-NEXT: popq %rdi
13 # CHECK-NEXT: addq $128, %rsp
13 # CHECK-NEXT: leaq 128(%rsp), %rsp
1414 #
1515 # CHECK-NEXT: movb (%rsi), %al
1616 #
17 # CHECK-NEXT: subq $128, %rsp
17 # CHECK-NEXT: leaq -128(%rsp), %rsp
1818 # CHECK-NEXT: pushq %rdi
1919 # CHECK-NEXT: leaq (%rdi), %rdi
2020 # CHECK-NEXT: callq __sanitizer_sanitize_store1@PLT
2121 # CHECK-NEXT: popq %rdi
22 # CHECK-NEXT: addq $128, %rsp
22 # CHECK-NEXT: leaq 128(%rsp), %rsp
2323 #
2424 # CHECK-NEXT: movb %al, (%rdi)
2525 mov1b: # @mov1b
4040 .type mov16b,@function
4141 # CHECK-LABEL: mov16b:
4242 #
43 # CHECK: subq $128, %rsp
43 # CHECK: leaq -128(%rsp), %rsp
4444 # CHECK-NEXT: pushq %rdi
4545 # CHECK-NEXT: leaq (%rsi), %rdi
4646 # CHECK-NEXT: callq __sanitizer_sanitize_load16@PLT
4747 # CHECK-NEXT: popq %rdi
48 # CHECK-NEXT: addq $128, %rsp
48 # CHECK-NEXT: leaq 128(%rsp), %rsp
4949 #
5050 # CHECK-NEXT: movaps (%rsi), %xmm0
5151 #
52 # CHECK-NEXT: subq $128, %rsp
52 # CHECK-NEXT: leaq -128(%rsp), %rsp
5353 # CHECK-NEXT: pushq %rdi
5454 # CHECK-NEXT: leaq (%rdi), %rdi
5555 # CHECK-NEXT: callq __sanitizer_sanitize_store16@PLT
5656 # CHECK-NEXT: popq %rdi
57 # CHECK-NEXT: addq $128, %rsp
57 # CHECK-NEXT: leaq 128(%rsp), %rsp
5858 #
5959 # CHECK-NEXT: movaps %xmm0, (%rdi)
6060 mov16b: # @mov16b
55 .type swap,@function
66 # CHECK-LABEL: swap:
77 #
8 # CHECK: subq $128, %rsp
8 # CHECK: leaq -128(%rsp), %rsp
99 # CHECK-NEXT: pushq %rdi
1010 # CHECK-NEXT: leaq (%rcx), %rdi
1111 # CHECK-NEXT: callq __sanitizer_sanitize_load8@PLT
1212 # CHECK-NEXT: popq %rdi
13 # CHECK-NEXT: addq $128, %rsp
13 # CHECK-NEXT: leaq 128(%rsp), %rsp
1414 #
1515 # CHECK-NEXT: movq (%rcx), %rax
1616 #
17 # CHECK-NEXT: subq $128, %rsp
17 # CHECK-NEXT: leaq -128(%rsp), %rsp
1818 # CHECK-NEXT: pushq %rdi
1919 # CHECK-NEXT: leaq (%rdx), %rdi
2020 # CHECK-NEXT: callq __sanitizer_sanitize_load8@PLT
2121 # CHECK-NEXT: popq %rdi
22 # CHECK-NEXT: addq $128, %rsp
22 # CHECK-NEXT: leaq 128(%rsp), %rsp
2323 #
2424 # CHECK-NEXT: movq (%rdx), %rbx
2525 #
26 # CHECK: subq $128, %rsp
26 # CHECK: leaq -128(%rsp), %rsp
2727 # CHECK-NEXT: pushq %rdi
2828 # CHECK-NEXT: leaq (%rcx), %rdi
2929 # CHECK-NEXT: callq __sanitizer_sanitize_store8@PLT
3030 # CHECK-NEXT: popq %rdi
31 # CHECK-NEXT: addq $128, %rsp
31 # CHECK-NEXT: leaq 128(%rsp), %rsp
3232 #
3333 # CHECK-NEXT: movq %rbx, (%rcx)
3434 #
35 # CHECK-NEXT: subq $128, %rsp
35 # CHECK-NEXT: leaq -128(%rsp), %rsp
3636 # CHECK-NEXT: pushq %rdi
3737 # CHECK-NEXT: leaq (%rdx), %rdi
3838 # CHECK-NEXT: callq __sanitizer_sanitize_store8@PLT
3939 # CHECK-NEXT: popq %rdi
40 # CHECK-NEXT: addq $128, %rsp
40 # CHECK-NEXT: leaq 128(%rsp), %rsp
4141 #
4242 # CHECK-NEXT: movq %rax, (%rdx)
4343 swap: # @swap