This is an archive of the discontinued LLVM Phabricator instance.

[asan] Allow reallocating uninstrumented pointers on Windows
DraftPublic

Authored by alvinhochun on Mar 27 2023, 11:49 AM.
This is a draft revision that has not yet been submitted for review.

Details

Summary

Asan have been ignoring deallocation of uninstrumented pointers since
https://reviews.llvm.org/D25946, but it did not do the same with
reallocation. Sometimes DLLs may reallocate memory allocated before asan
has been initialized, which can cause false asan reports, as stated in
https://github.com/llvm/llvm-project/issues/61685.

This change makes asan reallocation on Windows ignore pointers not
allocated by asan itself in the same way as deallocation does, for the
sake of consistency.

Diff Detail

Event Timeline

alvinhochun created this revision.Mar 27 2023, 11:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 27 2023, 11:49 AM
Herald added a subscriber: Enna1. · View Herald Transcript

WIP: Add test and make it work

A couple issues with this patch...

When building for i386, I get the following build errors:

/home/martin/code/llvm-mingw/llvm-project/compiler-rt/lib/asan/asan_malloc_win.cpp:67:1: error: typedef redefinition with different types ('size_t (*)(void *)' (aka 'unsigned int (*)(void *)') vs 'SIZE_T (*)(void *)' (aka 'unsigned long (*)(void *)'))
DEFINE_REAL(size_t, _msize, void *ptr)
^
/home/martin/code/llvm-mingw/llvm-project/compiler-rt/lib/asan/../interception/interception.h:198:24: note: expanded from macro 'DEFINE_REAL'
    typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
                       ^
/home/martin/code/llvm-mingw/llvm-project/compiler-rt/lib/asan/../interception/interception.h:163:23: note: expanded from macro 'FUNC_TYPE'
# define FUNC_TYPE(x) x##_type
                      ^
<scratch space>:103:1: note: expanded from here
_msize_type
^
/home/martin/code/llvm-mingw/llvm-project/compiler-rt/lib/asan/asan_interceptors.h:137:1: note: previous definition is here
DECLARE_REAL(SIZE_T, _msize, void *ptr)
^
/home/martin/code/llvm-mingw/llvm-project/compiler-rt/lib/asan/../interception/interception.h:166:24: note: expanded from macro 'DECLARE_REAL'
    typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
                       ^
/home/martin/code/llvm-mingw/llvm-project/compiler-rt/lib/asan/../interception/interception.h:163:23: note: expanded from macro 'FUNC_TYPE'
# define FUNC_TYPE(x) x##_type
                      ^
<scratch space>:45:1: note: expanded from here
_msize_type
^
1 error generated.

When running tests with MSVC, I get the following failure:

$ ":" "RUN: at line 1"
$ "C:/code/llvm-mingw/llvm-project/llvm/build-msvc/./bin/clang.exe" "-g" "-shared" "C:\code\llvm-mingw\llvm-project\compiler-rt\test\asan\TestCases\Windows\dll_heap_allocation_realloc.cpp" "-o" "C:\code\llvm-mingw\llvm-project\llvm\build-msvc\projects\compiler-rt\test\asan\X86_64WindowsDynamicConfig\TestCases\Windows\Output\dll_heap_allocation_realloc.cpp.tmp-1.dll" "-DHEAP_LIBRARY=1" "-Wl,--exclude-all-symbols" "-Wl,--out-implib,C:\code\llvm-mingw\llvm-project\llvm\build-msvc\projects\compiler-rt\test\asan\X86_64WindowsDynamicConfig\TestCases\Windows\Output\dll_heap_allocation_realloc.cpp.tmp-1.dll.a"
# command output:
LINK : warning LNK4044: unrecognized option '/-exclude-all-symbols'; ignored
LINK : warning LNK4044: unrecognized option '/-out-implib'; ignored
LINK : fatal error LNK1104: cannot open file 'C:\code\llvm-mingw\llvm-project\llvm\build-msvc\projects\compiler-rt\test\asan\X86_64WindowsDynamicConfig\TestCases\Windows\Output\dll_heap_allocation_realloc.cpp.tmp-1.dll.a'

# command stderr:
clang: error: linker command failed with exit code 1104 (use -v to see invocation)

error: command failed with exit status: 0x450

Fix non-Windows builds and try to "fix" tests for msvc