Page MenuHomePhabricator

[asan] Intercept all Heap* related imports from ucrtbase.dll
ClosedPublic

Authored by rnk on Mar 23 2016, 1:53 PM.

Details

Summary

ucrtbase.dll appears to be built with some kind of cross-module
inlining, because there are calls to imported Heap* routines sprinkled
throughout the code. This inlining defeats our attempts to hotpatch
malloc, _malloc_base, and related functions. Failing to intercept an
allocation or deallocation results in a crash when the program attempts
to deallocate or reallocate memory with the wrong allocator.

This change patches the IAT of ucrtbase.dll to replace the addresses of
the imported Heap* functions with implementations provided by ASan. We
don't globally intercept the win32 Heap* functions because they are
typically used by system DLLs that run before ASan initializes.
Eventually, we may want to intercept them, but for now I think this is
the minimal change that will keep ASan stable.

Diff Detail

Repository
rL LLVM

Event Timeline

rnk updated this revision to Diff 51467.Mar 23 2016, 1:53 PM
rnk retitled this revision from to [asan] Intercept all Heap* related imports from ucrtbase.dll.
rnk updated this object.
rnk added a reviewer: samsonov.
rnk added a subscriber: llvm-commits.
samsonov added inline comments.
lib/asan/asan_malloc_win.cc
131 ↗(On Diff #51467)

I'd rather make asan_malloc_usable_size take a const void *

166 ↗(On Diff #51467)

Any reason for not calling asan_malloc, asan_free etc. from these functions directly? E.g. we'll have the allocation stack trace which only contains the top frame from ASan runtime (in this case, HeapAlloc function).

234 ↗(On Diff #51467)

Will this work on older versions of MSVC we're still supporting (if any)?

rnk updated this revision to Diff 51586.Mar 24 2016, 12:55 PM
rnk marked an inline comment as done.
  • implement suggestions
lib/asan/asan_malloc_win.cc
166 ↗(On Diff #51467)

That's a good idea.

234 ↗(On Diff #51467)

The override will fail because ucrtbase.dll won't be present in the process, but ASan will continue to work fine. I've tested with VS 2013 and things still work.

samsonov accepted this revision.Mar 24 2016, 1:20 PM
samsonov edited edge metadata.

LGTM

This revision is now accepted and ready to land.Mar 24 2016, 1:20 PM
This revision was automatically updated to reflect the committed changes.