31 Commits

Author SHA1 Message Date
Mike Hommey
ce4618a9c4
[ASan][windows] Recognize movzx r11d, BYTE PTR [rdx] in interception_win (#111638)
The instruction is present in some library in the 24H2 update for
Windows 11:

==8508==interception_win: unhandled instruction at 0x7ff83e193a40: 44 0f
b6 1a 4c 8b d2 48

This could be generalized, but getting all the ModR/M byte combinations
right is tricky. Many other classes of instructions handled in this file
could use some generalization too.
2024-10-18 12:42:03 +00:00
Charlie Barto
3bd8f4e0a0 [sanitizer][asan][msvc] Teach GetInstructionSize about many instructions that appear in MSVC generated code. (#69490)
MSVC can sometimes generate instructions in function prologues that asan
previously didn't know the size of. This teaches asan those sizes. This isn't
super useful for using ASAN with non-msvc compilers, but it does stand alone.

From https://reviews.llvm.org/D151008
2024-09-19 15:44:08 +02:00
Hans Wennborg
04ccbe6e70 Fix typos in interception_win.cpp 2024-09-19 13:11:10 +02:00
Hans
3d2925b9de
[win/asan] AllocateMemoryForTrampoline within 2 GB of the module's base address (#108822)
Since we may copy code (see CopyInstructions) to the trampoline which
could reference data inside the original module, we really want the
trampoline to be within 2 GB of not just the original function, but
within anything that function may have rip-relative accesses to, i.e.
within 2 GB of that function's whole module.

This fixes interception failures like the following scenario:

1. Intercept `CreateProcess` in kernel32.dll, allocating a trampoline
region right after
2. Start intercepting `memcpy` in the main executable, which is loaded
at a lower address than kernel32.dll, but still within 2 GB of the
trampoline region so we keep using it.
3. Try to copy instructions from `memcpy` to the trampoline. Turns out
one instruction references data that is more than 2GB away from the
trampoline, so it can't be relocated.
4. The process exits due to a CHECK failure

(Full story at https://crbug.com/341936875#comment45 and following.)
2024-09-18 08:58:14 +02:00
Alex Richardson
46fe36a429 Revert "[compiler-rt] Remove duplicates of sanitizer_common functions"
This works for MinGW, but the MSVC linker apparently doens't pull in
those symbols. Reverting for now since I won't be able to reproduce it today.

https://lab.llvm.org/buildbot/#/builders/107/builds/2337

This reverts commit 9df92cbd1addb03c7169f05cf3b628f88c610224.
2024-08-29 17:29:01 -07:00
Alexander Richardson
9df92cbd1a
[compiler-rt] Remove duplicates of sanitizer_common functions
These functions in interception_win.cpp already exist in
sanitizer_common. Use those instead.

Reviewed By: mstorsjo

Pull Request: https://github.com/llvm/llvm-project/pull/106488
2024-08-29 16:00:44 -07:00
Charlie Barto
423832421b
[asan][windows] Weak function interception support in instruction size decoder. (#86570)
This makes it so we'll be able to decode the instructions used in the
weak function stubs from
https://github.com/llvm/llvm-project/pull/81677. This code doesn't
technically require those changes.

Co-authored-by: Amy Wishnousky <amyw@microsoft.com>
2024-03-28 09:52:25 -07:00
Zack Johnson
4b10d1fdd3
[compiler-rt] Fixing UB on Windows for trampoline allocations (#85639)
Fixing the type of the constant to avoid undefined behavior with respect
to overflow.
2024-03-19 15:16:18 +02:00
Charlie Barto
41b09bbe98
[ASAN][sanitizers][win] Allow windows-asan to be built with /MDd and intercept functions from the debug runtimes. (#77840)
It turns out this works _mostly_ fine, even when mixing debug versions
of asan with programs built with the release runtime. Using /MT (or
/MTd) with a dynamically linked asan has never really worked that well,
and I am planning on opening a PR that will completely remove the
static-asan configuration for windows and make programs linked with the
static CRT/runtime work with the DLL version of asan. This is better
than the current situation because the static linked version of asan
doesn't work well on windows if there are multiple DLLs in the process
using it.

The check for building asan with only /MD or /MT has been removed. It
was in AsanDoesNotSupportStaticLinkage, but was checking for debug CRTs,
not static linkage. The kind of static linkage this function is supposed
to check for (on linux for example) doesn't really exist on windows.

Note: There is one outstanding issue with this approach, if you mix a
/MDd DLLs and /MD dlls in the same process then the "real" function
called by asan interceptors will be the same for calls from both
contexts, potentially screwing up things like errno. This only happens
if you mix /MD and /MDd in the same process, because otherwise asan
won't find functions from both runtimes to intercept. We are working on
a fix for this, and it mainly hits with the CRT functions exported from
both ucrtbase and ntdll.


This change is being upstreamed from Microsoft's fork.
2024-01-16 13:19:03 -08:00
Zack Johnson
cbe27c45cd
[ASan][Windows] Interception fix for 'mov al, byte ptr []' sequences (#72531) 2023-12-07 10:17:58 -05:00
Farzon Lotfi
d79aee9f73
Changes to support running tests for Windows arm64 asan (#66973)
1. Differentiate SANITIZER_WINDOWS64 for x64 and arm64
2. turn off interception tests that expect x86 assembly

---------

Co-authored-by: Farzon Lotfi <farzon@farzon.com>
2023-11-27 12:28:44 -05:00
nicole mazzuca
160e8eb449
[ASan] Recognize lea r10, [rip + XX] (#68910)
This instruction is present in memcpy in the latest vcruntime

This PR has been opened for @AndrewDeanMS (a teammate inside Microsoft)
who made the PR to our internal branch.

Co-authored-by: Andrew Dean <Andrew.Dean@microsoft.com>
2023-10-13 08:47:23 -07:00
nicole mazzuca
bc34a83380
[ASan][Windows] Fix rip-relative instruction replacement (#68432)
The old code incorrectly checked what relative offsets were allowed.

The correct check is that the offset from the target to the instruction
pointer should be within $[-2^{31}, 2^{31})$; however, the check that
was originally written was that the offset was within $[0, 2^{31})$.
Negative offsets are certainly allowable (as long as they fit in 32
bits), and this change fixes that.
2023-10-09 14:53:07 -07:00
Alexandre Ganea
2385cf66e2 [compiler-rt] Fix "interception" file name 2023-10-02 12:29:29 -04:00
Farzon Lotfi
5a48a824aa [compiler-rt] Fix interception_win.cpp arm64 instruction lengths
Updates GetInstructionSize to account for arm64 instruction sizes.

ARM64 instruction are always 4 bytes long but GetInstructionSize in
interception_win.cpp assumes x86_64 which has mixed sizes.

Fix is for: https://github.com/llvm/llvm-project/issues/64319

Before the changeclang_rt.asan_dynamic-aarch64.dll would crash at:
OverrideFunction -> OverrideFunctionWithHotPatch -> GetInstructionSize:825

After the change:
dllthunkintercept -> dllthunkgetrealaddressordie -> InternalGetProcAddress
2023-09-12 22:43:51 -04:00
Gabor Horvath
8330116ebd Revert "ASan: Add additional wcs* interceptors on Windows" to unbreak build bot
This reverts commit c0c83668f8992ec00f5718ee330f82e2ce4ad5c8.
2023-08-21 15:45:51 -07:00
Nicole Mazzuca
c0c83668f8 ASan: Add additional wcs* interceptors on Windows
This adds wcs[n]cat, wcs[n]cmp, wcs[n]cpy, and wcschr functions to the
interception code on Windows; wcs[n]cat was already intercepted, but only on
POSIX.

Differential Revision: https://reviews.llvm.org/D157038
2023-08-21 14:17:42 -07:00
Alvin Wong
1a7a00bdc9 [compiler-rt][interception][win] Add error messages for some errors
Depends on D149002

Differential Revision: https://reviews.llvm.org/D149346
2023-05-04 22:41:27 +08:00
Alvin Wong
0716888d82 [compiler-rt][interception][asan][win] Improve error reporting
Add a callback from interception to allow asan on Windows to produce
better error messages. If an unrecoverable error occured when
intercepting functions, print a message before terminating.

Additionally, when encountering unknown instructions, a more helpful
message containing the address and the bytes of the unknown instruction
is now printed to help identify the issue and make it easier to propose
a fix.

Depends on D149549

Differential Revision: https://reviews.llvm.org/D149002
2023-05-04 22:41:26 +08:00
Alvin Wong
7b5571f3fc [compiler-rt][interception][win] Don't crash on unknown instructions
Do not treat unknown instructions as a fatal error. In most cases,
failure to intercept a function is reported by the caller, though
requires setting verbosity to 1 or higher to be visible.

Better error message reporting for asan will be added in a separate
patch.

Differential Revision: https://reviews.llvm.org/D149549
2023-05-04 22:41:26 +08:00
Alvin Wong
814a75dca7 [compiler-rt][asan][win] Intercept exceptions for i686 MinGW
The i686-w64-windows-gnu target does not use SEH (which MSVC uses),
but DWARF-2 exception handling or possibly sjlj depending on the
toolchain build options. On this target we have to actually intercept
functions in libc++ and libunwind which handles throwing exceptions.
This fixes the `TestCases/intercept-rethrow-exception.cpp` test.

The x86_64-w64-windows-gnu target already works because it uses SEH
which is handled by intercepting RaiseException, so this change does not
affect x86_64.

Depends on https://reviews.llvm.org/D148990

Differential Revision: https://reviews.llvm.org/D148991
2023-05-04 22:41:26 +08:00
Alvin Wong
ca40985ee9 [compiler-rt][interception][win] Add more assembly patterns
These assembly patterns are needed to intercept some libc++ and
libunwind functions built by Clang for i686-w64-windows-gnu target.

Differential Revision: https://reviews.llvm.org/D148990
2023-05-04 22:41:26 +08:00
Markus Böck
78c033b541 [sanitizers][windows] Correctly override functions with backward jmps
To reproduce: Download and run the latest Firefox ASAN build (https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.firefox.win64-asan-opt/artifacts/public/build/target.zip) on Windows 11 (version 10.0.22621 Build 22621); it will crash on launch. Note that this doesn't seem to crash on another Windows 11 VM I've tried, so I'm not sure how reproducible it is across machines, but it reproduces on my machine every time.

The problem seems to be that when overriding the memset function in OverrideFunctionWithRedirectJump(), the relative_offset is stored as a uptr. Per the Intel x64 instruction set reference (https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf - warning: large PDF), on page 646 the jmp instruction (specifically the near jump flavors that start with E9, which are the ones the OverrideFunctionWithRedirectJump() considers) treats the offset as a signed displacement. This causes an incorrect value to be stored for REAL(memset) which points to uninitialized memory, and a crash the next time that gets called.

The fix is to simply treat that offset as signed. I have also added a test case.

Fixes https://github.com/llvm/llvm-project/issues/58846

Differential Revision: https://reviews.llvm.org/D137788
2022-12-12 11:45:43 +01:00
Mariusz Borsa
8246b2e156 [Sanitizers][Darwin] Replace SANITIZER_MAC with SANITIZER_APPLE in source files
This is a follow up to [Sanitizers][Darwin] Rename Apple macro SANITIZER_MAC -> SANITIZER_APPLE (D125816)

Performed a global search/replace as in title against LLVM sources

Differential Revision: https://reviews.llvm.org/D126263
2022-05-24 12:59:27 -07:00
Martin Storsjö
aa45fc41c8 [sanitizers] Silence warnings about unused variables in i386 build. NFC. 2022-01-28 12:30:38 +02:00
Alexandre Ganea
7cd109b92c [asan] Additionnal prologue decoding for WinSDK 10.0.22000
Fixes interception of atoi() entry point.
2021-12-30 20:11:45 -05:00
Toshihito Kikuchi
22ea0cea59 [compiler-rt] [windows] Add more assembly patterns for interception
To intercept the functions in Win11's ntdll.dll, we need to use the trampoline
technique because there are bytes other than 0x90 or 0xcc in the gaps between
exported functions.  This patch adds more patterns that appear in ntdll's
functions.

Bug: https://bugs.llvm.org/show_bug.cgi?id=51721

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D109941
2021-09-21 15:51:58 -07:00
Kazuaki Ishizaki
a1e7e401d2 [compiler-rt] NFC: Fix trivial typo
Reviewed By: xgupta

Differential Revision: https://reviews.llvm.org/D77457
2021-09-04 14:12:58 +05:30
Martin Storsjö
979c38cc74 [compiler-rt] [windows] Add UNUSED attributes on variables/functions only used for 64 bit targets
This fixes warnings when building for 32 bit targets.

Differential Revision: https://reviews.llvm.org/D91852
2021-01-05 08:59:08 +02:00
Vitaly Buka
d2af368aee [compiler-rt] Remove some cpplint filters
llvm-svn: 371704
2019-09-12 02:20:36 +00:00
Nico Weber
ebbce04c14 compiler-rt: Rename .cc files in lib/interception to .cpp.
See https://reviews.llvm.org/D58620 for discussion, and for the commands
I ran. In addition I also ran

  for f in $(svn diff | diffstat | grep .cc | cut -f 2 -d ' '); do rg $f . ; done

and manually updated references to renamed files found by that.

llvm-svn: 367456
2019-07-31 18:01:55 +00:00