This is an archive of the discontinued LLVM Phabricator instance.

[ASan] Introduce a way set different ways of emitting module destructors.
ClosedPublic

Authored by delcypher on Feb 11 2021, 7:32 PM.

Details

Summary

Previously there was no way to control how module destructors were emitted
by ModuleAddressSanitizerPass. However, we want language frontends (e.g. Clang)
to be able to decide how to emit these destructors (if at all).

This patch introduces the AsanDtorKind enum that represents the different ways
destructors can be emitted. There are currently only two valid ways to emit destructors.

  • Global - Use llvm.global_dtors. This was the previous behavior and is the default.
  • None - Do not emit module destructors.

The ModuleAddressSanitizerPass and the various wrappers around it have been updated
to take the AsanDtorKind as an argument.

The -asan-destructor-kind= command line argument has been introduced to make this
easy to test from opt. If this argument is specified it overrides the value passed
to the ModuleAddressSanitizerPass constructor.

Note that AsanDtorKind is not bool because we will introduce a new way to
emit destructors in a subsequent patch.

Note that AsanDtorKind is given its own header file because if it is declared
in Transforms/Instrumentation/AddressSanitizer.h it leads to compile error
(Module is ambiguous) when trying to use it in
clang/Basic/CodeGenOptions.def.

rdar://71609176

Diff Detail

Event Timeline

delcypher created this revision.Feb 11 2021, 7:32 PM
delcypher requested review of this revision.Feb 11 2021, 7:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 11 2021, 7:32 PM
vitalybuka requested changes to this revision.Feb 17 2021, 2:09 PM
vitalybuka added inline comments.
llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h
11

please fix clang-tidy

13

unused include?

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
2175

Please fix all clang-tidy

2265

Can you check if some pre-existing tests fail if constructor accidentally initialized it to AsanDtorKind::None)

This revision now requires changes to proceed.Feb 17 2021, 2:09 PM
delcypher added inline comments.Feb 18 2021, 4:21 PM
llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h
13

Oops. I thought I had removed that one. I'll fix this.

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
2175

I didn't change the variable name for the IRBuilder so this problem was in the code before my change. Should fixing this be in a separate change seeing as the code clean up isn't really related to change I'm making?

2265

Sure. I'll test this. I would expect test/asan/TestCases/Posix/dlclose-test.cpp and the new llvm/test/Instrumentation/AddressSanitizer/no_global_dtors.ll to fail at a minimum but I will confirm my assumptions and report back.

delcypher updated this revision to Diff 325596.Feb 22 2021, 3:21 PM
  • Fix clang-tidy warnings
  • Remove unused header include
delcypher marked 3 inline comments as done.Feb 22 2021, 3:22 PM
delcypher marked an inline comment as done.Feb 22 2021, 10:03 PM

@vitalybuka Is this good to go?

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
2265

@vitalybuka If I change ClOverrideDestructorKind to default to AsanDtorKind::None I get the following test failures.

$ ninja check-asan
...
Failed Tests (1):
  AddressSanitizer-x86_64-darwin :: TestCases/Posix/dlclose-test.cpp

$ ninja check-llvm
...
Failed Tests (3):
  LLVM :: Instrumentation/AddressSanitizer/global_metadata_darwin.ll
  LLVM :: Instrumentation/AddressSanitizer/instrument_global.ll
  LLVM :: Instrumentation/AddressSanitizer/no_global_dtors.ll
vitalybuka accepted this revision.Feb 23 2021, 4:48 PM

LGTM

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
2175

Oh, I didn't noticed that this was preexisting.

llvm/test/Instrumentation/AddressSanitizer/no_global_dtors.ll
2

RUN: without line break is more readable
and clang format does not enforce 80char limit in tests

2–23

80char is not enforced in tests
and < can be avoided

This revision is now accepted and ready to land.Feb 23 2021, 4:48 PM
This revision was landed with ongoing or failed builds.Feb 23 2021, 8:38 PM
This revision was automatically updated to reflect the committed changes.