This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Fix Sanitizer Coverage for Windows.
AbandonedPublic

Authored by mpividori on Jan 6 2017, 7:18 PM.

Details

Summary

Hi,

In this diff, I fix Sanitizer Coverage for Windows, when using a shared run time for asan, which means, when compiling for MD.

I needed to update the code, since currently it doesn't work because it relies on weak symbols.
This works fine for Linux, since weak symbols work in both cases, when using a static asan rt or when considering a shared asan rt.
In Windows, we have something similar to weak symbols, using "pragma"(/alternatename...". This works for static libraries but this doesn't work for dlls.
For MD, asan is implemented in a separate dll, and the linker will fails because we can not set "__sanitizer_cov_trace_*" functions as optional in "clang-rt_asan_dynamic-arch.dll".

So, for example, for "sanitizer_cov_trace_pc_guard", we include the default implementation in asan dll.
When initialized, asan dll will try to find an implementation of "
sanitizer_cov_trace_pc_guard_impl" in the main executable, (I can't use the same name, so I decided to add a suffix "_impl" for user provided implementations). If it finds it, it will override its own "sanitizer_cov_trace_pc_guard" with that function, to use the user provided implementation.
So, users that don't want to use the default implementation, can change it, defining "
sanitizer_cov_trace_pc_guard_impl" with dllexport.

So, for example, for libFuzzer:

+ libFuzzer.lib defines "sanitizer_cov_trace_pc_guard_impl" (non default implementation), and exports it with dllexport.
+ asan.dll exports "
sanitizer_cov_trace_pc_guard" with default implementation.
+ All user's code, for example: "main.cc", "someUserLib.dll", etc, is instrumented with calls to "__sanitizer_cov_trace_pc_guard", which the linker resolves to point to the asan.dll's implementation.

When asan dll is initialized, it will look for an implementation of "sanitizer_cov_trace_pc_guard_impl" in the main executable, and it will find the implementation from libFuzzer.lib. Then, it will override "sanitizer_cov_trace_pc_guard" function pointer in asan.dll to point to "__sanitizer_cov_trace_pc_guard_impl" implementation.

Thanks,
Marcos

Diff Detail

Event Timeline

mpividori updated this revision to Diff 83486.Jan 6 2017, 7:18 PM
mpividori retitled this revision from to [compiler-rt] Fix Sanitizer Coverage for Windows..
mpividori updated this object.
mpividori added reviewers: kcc, aizatsky, zturner, rnk.
mpividori added a subscriber: llvm-commits.

This changes are based on: asan_win_dll_thunk.cc

mpividori abandoned this revision.Jan 10 2017, 10:58 AM

Hi, I took a different approach that will also work for MT. So I will abandon this diff, and I will soon submit the new one.
I apologize for this confusion.

kcc added inline comments.Jan 10 2017, 3:56 PM
lib/asan/asan_win_coverage.cc
1

why is this in asan and not in a common dir?

@kcc (I abandoned this diff) Because all the implementation of dll for Windows is inside asan.