Skip to content

Commit 5bd179e

Browse files
committed
[docs] Move LSANFailureSymbolication information to DebuggingTheCompiler.md instead of having its own document.
This information is generally centralized in DebuggingTheCompiler.md to make it easier to find.
1 parent 8e2f0e2 commit 5bd179e

File tree

2 files changed

+70
-68
lines changed

2 files changed

+70
-68
lines changed

docs/DebuggingTheCompiler.md

+70
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ benefit of all Swift developers.
3737
- [Using git-bisect in the presence of branch forwarding/feature branches](#using-git-bisect-in-the-presence-of-branch-forwardingfeature-branches)
3838
- [Reducing SIL test cases using bug_reducer](#reducing-sil-test-cases-using-bug_reducer)
3939
- [Disabling PCH Verification](#disabling-pch-verification)
40+
- [Diagnosing LSAN Failures in the Compiler](#diagnosing-lsan-failures-in-the-compiler)
4041
- [Debugging the Compiler Build](#debugging-the-compiler-build)
4142
- [Build Dry Run](#build-dry-run)
4243
- [Debugging the Compiler Driver](#debugging-the-compiler-driver-build)
@@ -846,6 +847,75 @@ checking by passing in to swift:
846847
NOTE: If there are actual differences in between the on disk PCH format and the
847848
format expected by the compiler crashes and undefined behavior may result.
848849

850+
## Diagnosing LSAN Failures in the Compiler
851+
852+
### Create Ubuntu Container
853+
854+
1. Use an x86 machine. The following instructions currently don’t work on arm64. It might be easy to adjust them or not, I have not tried
855+
2. Clone (or pull) swift-docker: https://github.com/swiftlang/swift-docker
856+
3. Build the Ubuntu 22.04 container: `cd swift-ci/master/ubuntu/22.04; docker build .`
857+
4. `docker run -it --cpus <CPUs> --memory <Memory> -v ~/<path to your local sources>:/src-on-host:cached --name lsan-reproducer --cap-add=SYS_PTRACE --security-opt seccomp=unconfined <hash that docker build outputs> bash`
858+
- The `-cap-add` and `-security-opt` arguments are needed to run LLDB inside the Docker container
859+
5. Copy the sources to inside the Docker container: `cp -r /src-on-host/* ~`
860+
- We need to to this because the build needs a case-sensitive file system and your host machine probably has a case-insensitive file system
861+
862+
Build inside the Container
863+
864+
1. `utils/build-script --preset buildbot_incremental_linux,lsan,tools=RDA,stdlib=DA,test=no`
865+
2. This should reproduce the LSAN failure
866+
3. Now, disassemble the failing CMake invocation to a swiftc invocation. I needed to set one environment variable and could the copy the swiftc invocation (but this might change as the build changes)
867+
868+
```
869+
export LD_LIBRARY_PATH=/opt/swift/5.8.1/usr/lib/swift/linux
870+
/home/build-user/build/buildbot_incremental_lsan/swift-linux-x86_64/./bin/swiftc <many arguments>
871+
```
872+
873+
### Symbolicating the LSAN report
874+
875+
For reasons that are not clear to me, LSAN does not symbolicate the report. To get the functions at the reported offsets, perform the following steps (there might be easier steps, please update this document if you know any).
876+
877+
1. Run the swiftc invocation that fails and copy the leak report to somewhere. The leak report should look like the following.
878+
```
879+
==3863==ERROR: LeakSanitizer: detected memory leaks
880+
881+
Direct leak of 120 byte(s) in 3 object(s) allocated from:
882+
#0 0x55b91c0b59b8 (/home/build-user/build/buildbot_incremental_lsan/swift-linux-x86_64/bin/swift-frontend+0x14d09b8)
883+
#1 0x55b91d51281c (/home/build-user/build/buildbot_incremental_lsan/swift-linux-x86_64/bin/swift-frontend+0x292d81c)
884+
#2 0x55b91c1b8700 (/home/build-user/build/buildbot_incremental_lsan/swift-linux-x86_64/bin/swift-frontend+0x15d3700)
885+
886+
SUMMARY: LeakSanitizer: 120 byte(s) leaked in 3 allocation(s).
887+
```
888+
2. `lldb -- <your swiftc invocation above>`
889+
3. Start running swiftc inside lldb by executing `r`
890+
4. Find the loaded offset of swift-frontend by running `image list`
891+
For example, this might output
892+
```
893+
[ 0] 0AEA10C1 0x0000555555554000 /home/build-user/build/buildbot_incremental_lsan/swift-linux-x86_64/bin/swift-frontend
894+
[ 1] D52BB67A-BBBB-E429-6E87-FC16144CA7CE-55276DD6 0x00007ffff7ffb000 [vdso] (0x00007ffff7ffb000)
895+
[ 2] 9EA8014C-F020-21A2-9E57-AA3E0512E9BB-6E30541D 0x00007ffff7dd3000 /lib/x86_64-linux-gnu/ld-2.27.so
896+
```
897+
The loaded offset is `0x0000555555554000`
898+
5. For the frame that you want to symbolicate,, add the offset you computed above to the stack frame in the LSAN report, eg. to symbolicate frame 1 `0x555555554000 + 0x292d81c = 0x555557E8181C`
899+
6. Look up the address using `image lookup -a <address you computed>`. This should output something like
900+
901+
```
902+
(lldb) image lookup -a 0x555557E8181C
903+
Address: swiftc[0x000000000292d81c] (swiftc.PT_LOAD[0]..text + 22056284)
904+
Summary: swiftc`registerFunctionTest(BridgedStringRef, void*) + 28 at SILBridging.cpp:148:3
905+
```
906+
907+
7. Hoorray, you know which function is leaking.
908+
909+
### Making Local Changes Inside the Container
910+
911+
For example, to install vim in the container run
912+
913+
```
914+
docker exec -u 0:0 -it lsan-reproducer bash
915+
$ apt update
916+
$ apt install vim
917+
```
918+
849919
# Debugging the Compiler Build
850920

851921
## Build Dry Run

docs/LSANFailureSymbolication.md

-68
This file was deleted.

0 commit comments

Comments
 (0)