This is an archive of the discontinued LLVM Phabricator instance.

[ASan] Added intermediate functions between assembly and __asan_report.* to avoid link errors.
ClosedPublic

Authored by kstoimenov on Feb 2 2022, 9:58 AM.

Details

Summary

Instead of calling asan_report.* directly from assembly code they have been replaced with corresponding asan_report.*_asm function, which call asan_report.*. All asan_report.* are now undefined weak symbols, which allows DSOs to link when z defs is used.

Diff Detail

Event Timeline

kstoimenov requested review of this revision.Feb 2 2022, 9:58 AM
kstoimenov created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 2 2022, 9:58 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
kstoimenov edited the summary of this revision. (Show Details)Feb 2 2022, 10:45 AM
kstoimenov added reviewers: vitalybuka, MaskRay.
This revision is now accepted and ready to land.Feb 2 2022, 11:54 AM
MaskRay added a comment.EditedFeb 2 2022, 12:49 PM

Other than __asan_report.*_asm intermediate functions the @PLT is removed from the assembly code.

The @PLT modifier does not matter for x86-64. @PLT for jmp/call is ignored anyway since the integrated assembler switched from R_X86_64_PC32 to R_X86_64_PLT32.

Added intermediate functions between assembly and __asan_report.* to avoid link errors.

Do you mind more information to clarify how the link error (which diagnostic?) is avoided?

MaskRay added inline comments.Feb 2 2022, 2:30 PM
compiler-rt/lib/asan/asan_rtl_static.cpp
20

WEAK_DEF may be misleading because the declared symbol (__asan_report_load1) is not a definition.

Consider just write extern "C" SANITIZER_WEAK_ATTRIBUTE

  • https://reviews.llvm.org/D107850 outlined some instrumented code sequences to __asan_check_* functions
  • -fsanitize-address-outline-instrumentation -mllvm -asan-optimize-callbacks=1 teaches the asan instrumentation to call __asan_check_*
  • https://reviews.llvm.org/D114558 implemented these __asan_check_* functions in libclang_rt.asan-static.a. They were later changed to hidden visibility to avoid PLT
  • A __asan_check_* function may call __asan_report_* in libclang_rt.asan.a

When linking a shared object, libclang_rt.asan-static.a may bring in some resolved undefined symbols, causing a --no-undefined (-z defs) link to fail.

This patch changes the symbol table entries

% egrep 'load8|load_add_8_RAX' asan_static.txt
    15: 000000000000011a     0 NOTYPE  LOCAL  DEFAULT    2 .fail_load_add_8_RAX
   227: 0000000000000108     0 FUNC    GLOBAL HIDDEN     2 __asan_check_load_add_8_RAX
   228: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __asan_report_load8

to

% egrep 'load8|load_add_8_RAX' asan_static.new.txt
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    9 .text.__asan_report_load8_asm
    25: 0000000000000000     5 FUNC    GLOBAL HIDDEN     9 __asan_report_load8_asm
    26: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __asan_report_load8
    15: 000000000000011a     0 NOTYPE  LOCAL  DEFAULT    2 .fail_load_add_8_RAX
   227: 0000000000000108     0 FUNC    GLOBAL HIDDEN     2 __asan_check_load_add_8_RAX
   228: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __asan_report_load8_asm

Since the call to __asan_report_load8 is now weak, the linker will not error for --no-undefined (-z defs).
So, the patch LGTM, but it needs some clarification.

MaskRay added a comment.EditedFeb 2 2022, 2:36 PM

Added intermediate functions between assembly and __asan_report.* to avoid link errors.

The main difference is not intermediate functions, but weak references to functions defined in asan.a (e.g. __asan_report_load8) to fix --no-undefined linker errors.

kstoimenov updated this revision to Diff 405474.Feb 2 2022, 3:40 PM
kstoimenov edited the summary of this revision. (Show Details)

Fixed function def.

kstoimenov marked an inline comment as done.Feb 2 2022, 3:42 PM
MaskRay accepted this revision.Feb 2 2022, 3:49 PM
kstoimenov edited the summary of this revision. (Show Details)Feb 2 2022, 4:01 PM
kstoimenov edited the summary of this revision. (Show Details)