Repository navigation
Description
Hello,
As described below, lld-link.exe adds no unwind information for the tailMergeX64 code when using /delayload, even though this code includes a call instruction. As stated in Microsoft documentation:
Table-based exception handling requires a table entry for all functions that allocate stack space or call another function (for example, nonleaf functions). [...] Failure to do so will result in unreliable exception handling and debugging of processes.
This missing unwind information currently has the following real-world impact in the Firefox web browser for Windows x64, when an exception is raised with a return address pointing to tailMergeX64 stored on the stack:
- this can prevent the crash reporter from being reached, because Microsoft's internal code in
ntdllthat unwinds structured exception handlers may stop attailMergeX64, resulting in no crash dump being created (see bug 1801322 comment 1); - when we somehow succeed to get a crash dump, our crash analyzer generates garbage once it reaches
tailMergeX64, resulting in hard-to-triage crashes (see bug 1740094).
Now I will provide more details with a simple example that reproduces the issue. When using the latest LLVM Windows x64 public release:
>"C:\Program Files\LLVM\bin\clang.exe" --version
clang version 15.0.6
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
>"C:\Program Files\LLVM\bin\lld-link.exe" --version
LLD 15.0.6To compile the following program into an object file:
#include <windows.h>
int main(void) {
MessageBoxW(NULL, L"Hello", L"Hello", MB_OK);
return 0;
}Then link it with lld-link.exe using /delayload:
> "C:\Program Files\LLVM\bin\clang.exe" -c main.cpp
> "C:\Program Files\LLVM\bin\lld-link.exe" main.o /defaultlib:libcmt.lib /defaultlib:user32.lib /defaultlib:delayimp.lib /delayload:user32.dllThe tailMergeX64 code is added at the end of the .text section to handle delay-loading, but there is not associated unwind information for this function in the .pdata (RUNTIME_FUNCTION) and .xdata (UNWIND_INFO) sections.
This can be verified with a script that I provide here:
> python show_missing_unwind_info.py main.exe
Loading 'main.exe', please wait...
Section .text: unwind information covers 94.00% of 51712 bytes.
Looking for call instructions in the parts not covered by unwind information...
0x14000d8af: e8903dffff call 0x140001644
0x14000d8b4: 660f6f0424 movdqa xmm0, xmmword ptr [rsp]
0x14000d8b9: 660f6f4c2410 movdqa xmm1, xmmword ptr [rsp + 0x10]
0x14000d8bf: 660f6f542420 movdqa xmm2, xmmword ptr [rsp + 0x20]
Found 1 call instructions without unwind information in section .text.
Note that some of those may be false positives.Or with a debug session, here in WinDbg:
# This is the entry in the import address table for MessageBoxW
0:000> dq main+179f0 L1
00007ff6`afde79f0 00007ff6`afddd878
# This is the delay-load jumper for MessageBoxW
0:000> u main+d878 L0x18
main+0xd878:
00007ff6`afddd878 488d0571a10000 lea rax,[main+0x179f0 (00007ff6`afde79f0)]
00007ff6`afddd87f e900000000 jmp main+0xd884 (00007ff6`afddd884)
# This is the tailMergeX64 function for delay-loaded user32.dll
00007ff6`afddd884 51 push rcx
00007ff6`afddd885 52 push rdx
00007ff6`afddd886 4150 push r8
00007ff6`afddd888 4151 push r9
00007ff6`afddd88a 4883ec48 sub rsp,48h
00007ff6`afddd88e 660f7f0424 movdqa xmmword ptr [rsp],xmm0
00007ff6`afddd893 660f7f4c2410 movdqa xmmword ptr [rsp+10h],xmm1
00007ff6`afddd899 660f7f542420 movdqa xmmword ptr [rsp+20h],xmm2
00007ff6`afddd89f 660f7f5c2430 movdqa xmmword ptr [rsp+30h],xmm3
00007ff6`afddd8a5 488bd0 mov rdx,rax
00007ff6`afddd8a8 488d0d817c0000 lea rcx,[main+0x15530 (00007ff6`afde5530)]
# This calls __delayLoadHelper2
00007ff6`afddd8af e8903dffff call main+0x1644 (00007ff6`afdd1644)
00007ff6`afddd8b4 660f6f0424 movdqa xmm0,xmmword ptr [rsp]
00007ff6`afddd8b9 660f6f4c2410 movdqa xmm1,xmmword ptr [rsp+10h]
00007ff6`afddd8bf 660f6f542420 movdqa xmm2,xmmword ptr [rsp+20h]
00007ff6`afddd8c5 660f6f5c2430 movdqa xmm3,xmmword ptr [rsp+30h]
00007ff6`afddd8cb 4883c448 add rsp,48h
00007ff6`afddd8cf 4159 pop r9
00007ff6`afddd8d1 4158 pop r8
00007ff6`afddd8d3 5a pop rdx
00007ff6`afddd8d4 59 pop rcx
00007ff6`afddd8d5 ffe0 jmp rax
# Displaying full contents of the .pdata section (which is sorted)
0:000> dt ntdll!_IMAGE_RUNTIME_FUNCTION_ENTRY[0x128] main+0x19000
[...]
+0x000 BeginAddress : 0xd81b
+0x004 EndAddress : 0xd834
+0x008 UnwindInfoAddress : 0x15ff8
+0x008 UnwindData : 0x15ff8
+0x000 BeginAddress : 0xd834
+0x004 EndAddress : 0xd84c
+0x008 UnwindInfoAddress : 0x15ff8
+0x008 UnwindData : 0x15ff8
+0x000 BeginAddress : 0xd84c
+0x004 EndAddress : 0xd878
+0x008 UnwindInfoAddress : 0x15ff8
+0x008 UnwindData : 0x15ff8
# There should be an entry here for the tailMergeX64 function (BeginAddress: 0xd884),
# but the unwind information ends here
+0x000 BeginAddress : 0
+0x004 EndAddress : 0
+0x008 UnwindInfoAddress : 0
+0x008 UnwindData : 0The expected result is the one obtained when linking with MSVC's link.exe instead:
> link.exe .\main.o libcmt.lib user32.lib delayimp.lib /DELAYLOAD:user32.dll
Microsoft (R) Incremental Linker Version 14.33.31630.0
Copyright (C) Microsoft Corporation. All rights reserved.
Or using clang-cl.exe to do compile and link at the same time, although I'm not sure what this does under the hood:
"C:\Program Files\LLVM\bin\clang-cl.exe" main.cpp user32.lib delayimp.lib /link/DELAYLOAD:user32.dllIn both cases, unwind information is present for the tailMergeX64 code; note that this code also differs slightly from the one added by lld-link.exe.
This can be seen with the script:
> python show_missing_unwind_info.py main.exe
Loading 'main.exe', please wait...
Section .text: unwind information covers 94.23% of 51712 bytes.
Looking for call instructions in the parts not covered by unwind information...
Found no call instructions without unwind information in section .text.Or, in a debug session:
# This is the entry in the import address table for MessageBoxW
0:000> dq main+0x18a10
00007ff7`2fcd8a10 00007ff7`2fcc1030
# This is the delay-load jumper for MessageBoxW
0:000> u main+0x1030 L0x1C
main+0x1030:
00007ff7`2fcc1030 488d05d9790100 lea rax,[main+0x18a10 (00007ff7`2fcd8a10)]
00007ff7`2fcc1037 e900000000 jmp main+0x103c (00007ff7`2fcc103c)
# This is the tailMergeX64 function for delay-loaded user32.dll
00007ff7`2fcc103c 48894c2408 mov qword ptr [rsp+8],rcx
00007ff7`2fcc1041 4889542410 mov qword ptr [rsp+10h],rdx
00007ff7`2fcc1046 4c89442418 mov qword ptr [rsp+18h],r8
00007ff7`2fcc104b 4c894c2420 mov qword ptr [rsp+20h],r9
00007ff7`2fcc1050 4883ec68 sub rsp,68h
00007ff7`2fcc1054 660f7f442420 movdqa xmmword ptr [rsp+20h],xmm0
00007ff7`2fcc105a 660f7f4c2430 movdqa xmmword ptr [rsp+30h],xmm1
00007ff7`2fcc1060 660f7f542440 movdqa xmmword ptr [rsp+40h],xmm2
00007ff7`2fcc1066 660f7f5c2450 movdqa xmmword ptr [rsp+50h],xmm3
00007ff7`2fcc106c 488bd0 mov rdx,rax
00007ff7`2fcc106f 488d0df2570100 lea rcx,[main+0x16868 (00007ff7`2fcd6868)]
# This calls __delayLoadHelper2
00007ff7`2fcc1076 e8e1030000 call main+0x145c (00007ff7`2fcc145c)
00007ff7`2fcc107b 660f6f442420 movdqa xmm0,xmmword ptr [rsp+20h]
00007ff7`2fcc1081 660f6f4c2430 movdqa xmm1,xmmword ptr [rsp+30h]
00007ff7`2fcc1087 660f6f542440 movdqa xmm2,xmmword ptr [rsp+40h]
00007ff7`2fcc108d 660f6f5c2450 movdqa xmm3,xmmword ptr [rsp+50h]
00007ff7`2fcc1093 488b4c2470 mov rcx,qword ptr [rsp+70h]
00007ff7`2fcc1098 488b542478 mov rdx,qword ptr [rsp+78h]
00007ff7`2fcc109d 4c8b842480000000 mov r8,qword ptr [rsp+80h]
00007ff7`2fcc10a5 4c8b8c2488000000 mov r9,qword ptr [rsp+88h]
00007ff7`2fcc10ad 4883c468 add rsp,68h
00007ff7`2fcc10b1 eb00 jmp main+0x10b3 (00007ff7`2fcc10b3)
00007ff7`2fcc10b3 ffe0 jmp rax
00007ff7`2fcc10b5 cc int 3
00007ff7`2fcc10b6 cc int 3
00007ff7`2fcc10b7 cc int 3
# Displaying contents of the .pdata section (which is sorted)
0:000> dt ntdll!_IMAGE_RUNTIME_FUNCTION_ENTRY[4] main+0x1a000
[4] +0x000 BeginAddress : 0x1000
+0x004 EndAddress : 0x102a
+0x008 UnwindInfoAddress : 0x15cd0
+0x008 UnwindData : 0x15cd0
-> +0x000 BeginAddress : 0x103c
+0x004 EndAddress : 0x10b3
+0x008 UnwindInfoAddress : 0x15cd8
+0x008 UnwindData : 0x15cd8 <-
+0x000 BeginAddress : 0x10b8
+0x004 EndAddress : 0x1162
+0x008 UnwindInfoAddress : 0x15d18
+0x008 UnwindData : 0x15d18
+0x000 BeginAddress : 0x1164
+0x004 EndAddress : 0x1202
+0x008 UnwindInfoAddress : 0x15ce0
+0x008 UnwindData : 0x15ce0
[...]
# This is the unwind information for the tailMergeX64 function,
# stored in the .xdata section
0:000> db main+0x15cd8 L8
00007ff7`2fcd5cd8 01 18 01 00 18 c2 00 00
Activity
yjugl commentedon Jan 13, 2023
The following unwind information can be used for the current version of
tailMergeX64:It was originally generated for the following MASM procedure:
The bytes generated for the MASM procedure were the same as the ones hardcoded for
tailMergeX64inlld/COFF/DLL.cpp(except for the placeholder zeros).serge-sans-paille commentedon Jan 13, 2023
Tentative patch for amd64: https://sergesanspaille.fedorapeople.org/0001-lld-COFF-Provide-unwinding-information-for-Chunk-inj.patch
yjugl commentedon Jan 13, 2023
The patch looks good to me!
First, the script is happy:
Then, I created (with Visual Studio) a DLL that triggers an exception at load:
Then compiled a simple program that delay-loads the DLL by calling into the function:
When launching the program in a debugger, the stack is correctly fully accessible when the exception is triggered:
Whereas linking with the public version of
lld-link.exeyields a partial stack, because unwinding stops attailMergeX64:llvmbot commentedon Jan 13, 2023
@llvm/issue-subscribers-lld-coff
yjugl commentedon Jan 16, 2023
This will be fixed by: https://reviews.llvm.org/D141691
Thank you @serge-sans-paille!
[lld][COFF] Provide unwinding information for Chunk injected by /dela…
[lld][COFF] Provide unwinding information for Chunk injected by /dela…