This is an archive of the discontinued LLVM Phabricator instance.

[SanitizerCoverage] Make __start_/__stop_ symbols extern_weak
ClosedPublic

Authored by MaskRay on Mar 18 2021, 3:04 PM.

Details

Summary

On ELF, we place the metadata sections (__sancov_guards, __sancov_cntrs,
__sancov_bools, __sancov_pcs in section groups (either comdat any or
comdat noduplicates).

With --gc-sections, LLD since D96753 and GNU ld -z start-stop-gc may garbage
collect such sections. If all __sancov_bools are discarded, LLD will error
error: undefined hidden symbol: __start___sancov_cntrs (other sections are similar).

% cat a.c
void discarded() {}
% clang -fsanitize-coverage=func,trace-pc-guard -fpic -fvisibility=hidden a.c -shared -fuse-ld=lld -Wl,--gc-sections
...
ld.lld: error: undefined hidden symbol: __start___sancov_guards
>>> referenced by a.c
>>>               /tmp/a-456662.o:(sancov.module_ctor_trace_pc_guard)

Use the extern_weak linkage (lowered to undefined weak symbols) to avoid the
undefined error.

Diff Detail

Event Timeline

MaskRay created this revision.Mar 18 2021, 3:04 PM
MaskRay requested review of this revision.Mar 18 2021, 3:04 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMar 18 2021, 3:04 PM
Herald added subscribers: llvm-commits, Restricted Project. · View Herald Transcript
MaskRay updated this revision to Diff 331699.Mar 18 2021, 3:07 PM

drop accidental changes

MaskRay edited the summary of this revision. (Show Details)Mar 18 2021, 3:20 PM
This revision is now accepted and ready to land.Mar 18 2021, 4:36 PM
MaskRay edited the summary of this revision. (Show Details)Mar 18 2021, 4:43 PM

https://lab.llvm.org/buildbot/#/builders/127/builds/8015 fails like this

******************** TEST 'AddressSanitizer-x86_64-windows :: TestCases/Window
coverage-dll-stdio.cpp' FAILED ********************
Script:
--
: 'RUN: at line 4';   rm -rf C:\b\slave\sanitizer-windows\build\stage1\project
compiler-rt\test\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dl
stdio.cpp.tmp && mkdir C:\b\slave\sanitizer-windows\build\stage1\projects\comp
er-rt\test\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdi
cpp.tmp && cd C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\t
t\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp
: 'RUN: at line 5';      C:/b/slave/sanitizer-windows/build/stage1/./bin/clang
l.exe  -fsanitize=address -Wno-deprecated-declarations -WX -D_HAS_EXCEPTIONS=0
Zi     -fsanitize-coverage=func,trace-pc-guard -Od C:\b\slave\sanitizer-window
llvm-project\compiler-rt\test\asan\TestCases\Windows/dll_host.cpp -Fet.exe
: 'RUN: at line 6';      C:/b/slave/sanitizer-windows/build/stage1/./bin/clang
l.exe  -fsanitize=address -Wno-deprecated-declarations -WX -D_HAS_EXCEPTIONS=0
Zi     -fsanitize-coverage=func,trace-pc-guard -LD -Od C:\b\slave\sanitizer-wi
ows\llvm-project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cp
-Fet.dll
: 'RUN: at line 7';    ./t.exe t.dll 2>&1 | FileCheck C:\b\slave\sanitizer-win
ws\llvm-project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cpp
--
Exit Code: 1

Command Output (stdout):
--
$ ":" "RUN: at line 4"
$ "rm" "-rf" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\t
t\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp
$ "mkdir" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\test
san\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp"
$ "cd" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\test\as
\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp"
$ ":" "RUN: at line 5"
$ "C:/b/slave/sanitizer-windows/build/stage1/./bin/clang-cl.exe" "-fsanitize=a
ress" "-Wno-deprecated-declarations" "-WX" "-D_HAS_EXCEPTIONS=0" "-Zi" "-fsani
ze-coverage=func,trace-pc-guard" "-Od" "C:\b\slave\sanitizer-windows\llvm-proj
t\compiler-rt\test\asan\TestCases\Windows/dll_host.cpp" "-Fet.exe"
# command output:
   Creating library t.lib and object t.exp

$ ":" "RUN: at line 6"
$ "C:/b/slave/sanitizer-windows/build/stage1/./bin/clang-cl.exe" "-fsanitize=a
ress" "-Wno-deprecated-declarations" "-WX" "-D_HAS_EXCEPTIONS=0" "-Zi" "-fsani
ze-coverage=func,trace-pc-guard" "-LD" "-Od" "C:\b\slave\sanitizer-windows\llv
project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cpp" "-Fet.
l"
# command output:
   Creating library t.lib and object t.exp

$ ":" "RUN: at line 7"
$ "./t.exe" "t.dll"
note: command had no output on stdout or stderr
error: command failed with exit status: 1
$ "FileCheck" "C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan
estCases\Windows\coverage-dll-stdio.cpp"
# command stderr:
C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan\TestCases\Wind
s\coverage-dll-stdio.cpp:14:12: error: CHECK: expected string not found in inp

 // CHECK: hello world
           ^
<stdin>:1:1: note: scanning from here
Could not load DLL: t.dll (code: 1114)!
^

Input file: <stdin>
Check file: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan\Te
Cases\Windows\coverage-dll-stdio.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1: Could not load DLL: t.dll (code: 1114)!
check:14     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
          2: =================================================================
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          3: ==6276==ERROR: AddressSanitizer: access-violation on unknown addr
s 0x7ffb669b458c (pc 0x7ffb669b458c bp 0x000000000002 sp 0x002d80a8f458 T0)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          4: ==6276==The signal is caused by a UNKNOWN memory access.
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          5:  #0 0x7ffb669b458b (<unknown module>)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          6:  #1 0x7ffb84aed596 (C:\windows\SYSTEM32\ntdll.dll+0x18001d596)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          .
          .
          .
>>>>>>

error: command failed with exit status: 1

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
********************
Failed Tests (1):
  AddressSanitizer-x86_64-windows :: TestCases/Windows/coverage-dll-stdio.cpp

reverting change helps

https://lab.llvm.org/buildbot/#/builders/127/builds/8015 fails like this

******************** TEST 'AddressSanitizer-x86_64-windows :: TestCases/Window
coverage-dll-stdio.cpp' FAILED ********************
Script:
--
: 'RUN: at line 4';   rm -rf C:\b\slave\sanitizer-windows\build\stage1\project
compiler-rt\test\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dl
stdio.cpp.tmp && mkdir C:\b\slave\sanitizer-windows\build\stage1\projects\comp
er-rt\test\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdi
cpp.tmp && cd C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\t
t\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp
: 'RUN: at line 5';      C:/b/slave/sanitizer-windows/build/stage1/./bin/clang
l.exe  -fsanitize=address -Wno-deprecated-declarations -WX -D_HAS_EXCEPTIONS=0
Zi     -fsanitize-coverage=func,trace-pc-guard -Od C:\b\slave\sanitizer-window
llvm-project\compiler-rt\test\asan\TestCases\Windows/dll_host.cpp -Fet.exe
: 'RUN: at line 6';      C:/b/slave/sanitizer-windows/build/stage1/./bin/clang
l.exe  -fsanitize=address -Wno-deprecated-declarations -WX -D_HAS_EXCEPTIONS=0
Zi     -fsanitize-coverage=func,trace-pc-guard -LD -Od C:\b\slave\sanitizer-wi
ows\llvm-project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cp
-Fet.dll
: 'RUN: at line 7';    ./t.exe t.dll 2>&1 | FileCheck C:\b\slave\sanitizer-win
ws\llvm-project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cpp
--
Exit Code: 1

Command Output (stdout):
--
$ ":" "RUN: at line 4"
$ "rm" "-rf" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\t
t\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp
$ "mkdir" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\test
san\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp"
$ "cd" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\test\as
\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp"
$ ":" "RUN: at line 5"
$ "C:/b/slave/sanitizer-windows/build/stage1/./bin/clang-cl.exe" "-fsanitize=a
ress" "-Wno-deprecated-declarations" "-WX" "-D_HAS_EXCEPTIONS=0" "-Zi" "-fsani
ze-coverage=func,trace-pc-guard" "-Od" "C:\b\slave\sanitizer-windows\llvm-proj
t\compiler-rt\test\asan\TestCases\Windows/dll_host.cpp" "-Fet.exe"
# command output:
   Creating library t.lib and object t.exp

$ ":" "RUN: at line 6"
$ "C:/b/slave/sanitizer-windows/build/stage1/./bin/clang-cl.exe" "-fsanitize=a
ress" "-Wno-deprecated-declarations" "-WX" "-D_HAS_EXCEPTIONS=0" "-Zi" "-fsani
ze-coverage=func,trace-pc-guard" "-LD" "-Od" "C:\b\slave\sanitizer-windows\llv
project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cpp" "-Fet.
l"
# command output:
   Creating library t.lib and object t.exp

$ ":" "RUN: at line 7"
$ "./t.exe" "t.dll"
note: command had no output on stdout or stderr
error: command failed with exit status: 1
$ "FileCheck" "C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan
estCases\Windows\coverage-dll-stdio.cpp"
# command stderr:
C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan\TestCases\Wind
s\coverage-dll-stdio.cpp:14:12: error: CHECK: expected string not found in inp

 // CHECK: hello world
           ^
<stdin>:1:1: note: scanning from here
Could not load DLL: t.dll (code: 1114)!
^

Input file: <stdin>
Check file: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan\Te
Cases\Windows\coverage-dll-stdio.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1: Could not load DLL: t.dll (code: 1114)!
check:14     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
          2: =================================================================
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          3: ==6276==ERROR: AddressSanitizer: access-violation on unknown addr
s 0x7ffb669b458c (pc 0x7ffb669b458c bp 0x000000000002 sp 0x002d80a8f458 T0)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          4: ==6276==The signal is caused by a UNKNOWN memory access.
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          5:  #0 0x7ffb669b458b (<unknown module>)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          6:  #1 0x7ffb84aed596 (C:\windows\SYSTEM32\ntdll.dll+0x18001d596)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          .
          .
          .
>>>>>>

error: command failed with exit status: 1

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
********************
Failed Tests (1):
  AddressSanitizer-x86_64-windows :: TestCases/Windows/coverage-dll-stdio.cpp

reverting change helps

How about hacking Windows to not use extern_weak?

LGTM, I assume problem the patch is solving is not an issues on Windows?

https://lab.llvm.org/buildbot/#/builders/127/builds/8015 fails like this

******************** TEST 'AddressSanitizer-x86_64-windows :: TestCases/Window
coverage-dll-stdio.cpp' FAILED ********************
Script:
--
: 'RUN: at line 4';   rm -rf C:\b\slave\sanitizer-windows\build\stage1\project
compiler-rt\test\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dl
stdio.cpp.tmp && mkdir C:\b\slave\sanitizer-windows\build\stage1\projects\comp
er-rt\test\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdi
cpp.tmp && cd C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\t
t\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp
: 'RUN: at line 5';      C:/b/slave/sanitizer-windows/build/stage1/./bin/clang
l.exe  -fsanitize=address -Wno-deprecated-declarations -WX -D_HAS_EXCEPTIONS=0
Zi     -fsanitize-coverage=func,trace-pc-guard -Od C:\b\slave\sanitizer-window
llvm-project\compiler-rt\test\asan\TestCases\Windows/dll_host.cpp -Fet.exe
: 'RUN: at line 6';      C:/b/slave/sanitizer-windows/build/stage1/./bin/clang
l.exe  -fsanitize=address -Wno-deprecated-declarations -WX -D_HAS_EXCEPTIONS=0
Zi     -fsanitize-coverage=func,trace-pc-guard -LD -Od C:\b\slave\sanitizer-wi
ows\llvm-project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cp
-Fet.dll
: 'RUN: at line 7';    ./t.exe t.dll 2>&1 | FileCheck C:\b\slave\sanitizer-win
ws\llvm-project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cpp
--
Exit Code: 1

Command Output (stdout):
--
$ ":" "RUN: at line 4"
$ "rm" "-rf" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\t
t\asan\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp
$ "mkdir" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\test
san\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp"
$ "cd" "C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\test\as
\X86_64WindowsConfig\TestCases\Windows\Output\coverage-dll-stdio.cpp.tmp"
$ ":" "RUN: at line 5"
$ "C:/b/slave/sanitizer-windows/build/stage1/./bin/clang-cl.exe" "-fsanitize=a
ress" "-Wno-deprecated-declarations" "-WX" "-D_HAS_EXCEPTIONS=0" "-Zi" "-fsani
ze-coverage=func,trace-pc-guard" "-Od" "C:\b\slave\sanitizer-windows\llvm-proj
t\compiler-rt\test\asan\TestCases\Windows/dll_host.cpp" "-Fet.exe"
# command output:
   Creating library t.lib and object t.exp

$ ":" "RUN: at line 6"
$ "C:/b/slave/sanitizer-windows/build/stage1/./bin/clang-cl.exe" "-fsanitize=a
ress" "-Wno-deprecated-declarations" "-WX" "-D_HAS_EXCEPTIONS=0" "-Zi" "-fsani
ze-coverage=func,trace-pc-guard" "-LD" "-Od" "C:\b\slave\sanitizer-windows\llv
project\compiler-rt\test\asan\TestCases\Windows\coverage-dll-stdio.cpp" "-Fet.
l"
# command output:
   Creating library t.lib and object t.exp

$ ":" "RUN: at line 7"
$ "./t.exe" "t.dll"
note: command had no output on stdout or stderr
error: command failed with exit status: 1
$ "FileCheck" "C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan
estCases\Windows\coverage-dll-stdio.cpp"
# command stderr:
C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan\TestCases\Wind
s\coverage-dll-stdio.cpp:14:12: error: CHECK: expected string not found in inp

 // CHECK: hello world
           ^
<stdin>:1:1: note: scanning from here
Could not load DLL: t.dll (code: 1114)!
^

Input file: <stdin>
Check file: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\asan\Te
Cases\Windows\coverage-dll-stdio.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1: Could not load DLL: t.dll (code: 1114)!
check:14     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
          2: =================================================================
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          3: ==6276==ERROR: AddressSanitizer: access-violation on unknown addr
s 0x7ffb669b458c (pc 0x7ffb669b458c bp 0x000000000002 sp 0x002d80a8f458 T0)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          4: ==6276==The signal is caused by a UNKNOWN memory access.
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          5:  #0 0x7ffb669b458b (<unknown module>)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          6:  #1 0x7ffb84aed596 (C:\windows\SYSTEM32\ntdll.dll+0x18001d596)
check:14     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          .
          .
          .
>>>>>>

error: command failed with exit status: 1

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
********************
Failed Tests (1):
  AddressSanitizer-x86_64-windows :: TestCases/Windows/coverage-dll-stdio.cpp

reverting change helps

How about hacking Windows to not use extern_weak?

LGTM, I assume problem the patch is solving is not an issues on Windows?

It was a Linux issue.

MaskRay added a subscriber: rnk.Mar 22 2021, 11:07 PM

Fixed by 3c81822ec5d2375d243ef2d43468807aa96383ec. I used this comment:

+ Windows defines the start/stop symbols in compiler-rt so no need for
+
ExternalWeak.

(See lib/sanitizer_common/sanitizer_coverage_win_sections.cpp)

The comment could be improved if I knew why the test (https://lab.llvm.org/buildbot/#/builders/127/builds/8015) failed... @rnk