[PATCH] D155879: Summary: [asan][win][msvc] override new and delete and seperate TUs

Charlie Barto via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 3 11:44:04 PDT 2023


barcharcraz created this revision.
barcharcraz added a reviewer: vitalybuka.
Herald added a subscriber: Enna1.
Herald added a project: All.
barcharcraz updated this revision to Diff 543705.
barcharcraz added a comment.
barcharcraz updated this revision to Diff 544106.
barcharcraz updated this revision to Diff 546668.
barcharcraz edited the summary of this revision.
barcharcraz published this revision for review.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

- only do the implib injections on windows, fixes build on linux


barcharcraz added a comment.

- Use conditionals at configure time to add implib object library


barcharcraz added a comment.

- add new and delete asan implementations to interface.inc
- Add dependency between overall asan target and the modified implib


Currently asan simply exports each overridden new/delete function from the DLL, this works fine normally, but fails if the user is overriding some, but not all, of these functions. In this case the non-overridden functions still come from the asan DLL, but they can't correctly call the user provided override (for example sized op delete should fall back to scalar op delete, if a scalar op delete is provided). Things were also broken in the static build because all the asan overrides were exported from the same TU, and so if you overrode one but not all of them then you'd get ODR violations. This PR should fix both of these cases, but the static case isn't really tested (and indeed one such test does fail) because linking asan statically basically doesn't work on windows right now with LLVM's version of asan. In fact, while we did fix this in our fork, it was a huge mess and we've now made the dynamic version work in all situations (/MD, /MT, /MDd, /MTd, etc) instead.

The following is the description from the internal PR that implemented most of this feature.

> Previously, operator new/delete were provided as DLL exports when linking dynamically and wholearchived when linked statically. Both scenarios were broken. When linking statically, the user could not define their own op new/delete, because they were already brought into the link by ASAN. When dynamically linking, if the user provided some but not all of the overloads, new and delete would be partially hooked. For example, if the user defined scalar op delete, but the program then called sized op delete, the sized op delete would still be the version provided by ASAN instead of falling back to the user-defined scalar op delete, like the standard requires.
>
> The change <internal PR number>: ASAN operator new/delete fallbacks in the ASAN libraries fixes this moving all operator new/delete definitions to be statically linked. However, this still won't work if /InferAsanLibs still whole-archives everything since then all the op new/deletes would always be provided by ASAN, which is why these changes are necessary.
>
> With these changes, we will no longer wholearchive all of ASAN and will leave the c++ parts (the op new/delete definitions) to be included as a default library. However, it is also necessary to ensure that the asan library with op new/delete will be searched before the corresponding CRT library with the same op new/delete definitions. To accomplish this, we make sure to add the asan library to the beginning of the default lib list, or move it explicitly to the front if it's already in the list. If the C runtime library is explicitly provided, we make sure to warn the user if the current linker line will result in operator new/delete not being provided by ASAN.

Note that the rearrangement of defaultlibs is not in this diff.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155879

Files:
  compiler-rt/lib/asan/CMakeLists.txt
  compiler-rt/lib/asan/asan_interface.inc
  compiler-rt/lib/asan/asan_stack.h
  compiler-rt/lib/asan/asan_win_delete_array_align_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_array_align_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_array_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_array_size_align_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_array_size_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_array_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_scalar_align_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_scalar_align_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_scalar_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_scalar_size_align_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_scalar_size_thunk.cpp
  compiler-rt/lib/asan/asan_win_delete_scalar_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_array_align_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_array_align_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_array_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_array_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_delete.cpp
  compiler-rt/lib/asan/asan_win_new_delete_thunk_common.h
  compiler-rt/lib/asan/asan_win_new_scalar_align_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_scalar_align_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_scalar_nothrow_thunk.cpp
  compiler-rt/lib/asan/asan_win_new_scalar_thunk.cpp
  compiler-rt/lib/asan/asan_win_thunk_common.h
  compiler-rt/test/asan/TestCases/Windows/new_delete_mfc_already_defined.cpp
  compiler-rt/test/asan/TestCases/Windows/new_delete_mfc_already_defined_dbg.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_array_new_with_dtor_left_oob.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_delete_replacement_array.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_delete_replacement_scalar.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_new_delete_replacement_all.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_new_delete_replacement_common.h
  compiler-rt/test/asan/TestCases/Windows/operator_new_delete_replacement_macros.h
  compiler-rt/test/asan/TestCases/Windows/operator_new_replacement_array.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_new_replacement_scalar.cpp
  compiler-rt/test/asan/TestCases/Windows/operator_new_uaf.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155879.546668.patch
Type: text/x-patch
Size: 92454 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230803/92be35c9/attachment.bin>


More information about the llvm-commits mailing list