[AddressSanitizer] Fix for wrong argument values appearing in backtraces
authorVedant Kumar <vsk@apple.com>
Tue, 31 Mar 2020 22:27:06 +0000 (15:27 -0700)
committerVedant Kumar <vsk@apple.com>
Mon, 6 Apr 2020 22:59:25 +0000 (15:59 -0700)
commit5f185a89991e85eed10c630028fc9d2569514491
treea78d8dd75c140cc40e12339eaa1b64154395f419
parent7545be074d478297ac60efaee3cf919d436d6f32
[AddressSanitizer] Fix for wrong argument values appearing in backtraces

Summary:
In some cases, ASan may insert instrumentation before function arguments
have been stored into their allocas. This causes two issues:

1) The argument value must be spilled until it can be stored into the
   reserved alloca, wasting a stack slot.

2) Until the store occurs in a later basic block, the debug location
   will point to the wrong frame offset, and backtraces will show an
   uninitialized value.

The proposed solution is to move instructions which initialize allocas
for arguments up into the entry block, before the position where ASan
starts inserting its instrumentation.

For the motivating test case, before the patch we see:

```
 | 0033: movq %rdi, 0x68(%rbx)  |   | DW_TAG_formal_parameter     |
 | ...                          |   |   DW_AT_name ("a")          |
 | 00d1: movq 0x68(%rbx), %rsi  |   |   DW_AT_location (RBX+0x90) |
 | 00d5: movq %rsi, 0x90(%rbx)  |   |       ^ not correct ...     |
```

and after the patch we see:

```
 | 002f: movq %rdi, 0x70(%rbx)  |   | DW_TAG_formal_parameter     |
 |                              |   |   DW_AT_name ("a")          |
 |                              |   |   DW_AT_location (RBX+0x70) |
```

rdar://61122691

Reviewers: aprantl, eugenis

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D77182
llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
llvm/test/Instrumentation/AddressSanitizer/hoist-argument-init-insts.ll [new file with mode: 0644]