This is an archive of the discontinued LLVM Phabricator instance.

[asan] Add exception handler to map memory on demand on Win64.
ClosedPublic

Authored by wang0109 on Jul 1 2016, 3:42 PM.

Details

Summary

[asan] Add exception handler to map memory on demand on Win64.
Memory will be committed on demand when exception happens while accessing
shadow memeory region.

Diff Detail

Event Timeline

wang0109 updated this revision to Diff 62554.Jul 1 2016, 3:42 PM
wang0109 retitled this revision from to [asan] Add exception handler to map memory on demand on Win64..
wang0109 updated this object.
wang0109 added reviewers: etienneb, chrisha.
etienneb edited reviewers, added: rnk; removed: chrisha, etienneb.Jul 1 2016, 3:45 PM
etienneb added subscribers: etienneb, chrisha.
etienneb added inline comments.Jul 1 2016, 3:51 PM
lib/asan/asan_win.cc
227

Does this can be done into: InitializeExceptionHandlerOnWindows64

244

no { } here

265

'auto' here won't comply with the common coding style in llvm.
The type is not on the rhs, so use char* instead.

271

ditto 'auto'

280

rewrite the following list has a single line.

if (AddVectoredExceptionHandler(TRUE, &ShadowExceptionHandler) != nullptr)

or

if (!AddVectoredExceptionHandler(TRUE, &ShadowExceptionHandler))
wang0109 updated this revision to Diff 62593.Jul 1 2016, 7:14 PM
  • update diff: format and minor styling
etienneb added inline comments.Jul 4 2016, 8:17 AM
lib/asan/asan_win.cc
257

As I get it, for the shadow memory this should never happens???
The whole memory space used by the shadow must be already reserved.

etienneb added inline comments.Jul 4 2016, 8:46 AM
lib/asan/asan_internal.h
65

I'm not a fan of these #ifdef

Should we use something similar than: void InitializePlatformInterceptors();
but for the shadow.

wang0109 added inline comments.Jul 4 2016, 8:48 AM
lib/asan/asan_internal.h
65

Sounds reasonable. Could be called InitializePlatformExceptionHandlers() ?

wang0109 updated this revision to Diff 62741.Jul 5 2016, 6:48 AM
  • update diff: remove several ifdefs, rename to InitializePlatformExceptionHandlers
rnk added inline comments.Jul 6 2016, 9:20 AM
lib/asan/asan_win.cc
223–231

Use GetPageSizeCached() and GetMmapGranularity() instead.

249–250

Use RoundDownTo instead of the bitmath

257

Yeah, what's the story here? We're going with up-front reservation first, and that succeeds in Win8.1+, right?

260–261

RoundDownTo

wang0109 updated this revision to Diff 62961.Jul 6 2016, 2:08 PM
  • update diff: cleanup code regarding RoundDownTo, GetPageSize
chrisha added inline comments.Jul 6 2016, 2:13 PM
lib/asan/asan_win.cc
257

This is cut and paste from a small test harness I wrote. Because of the page table overhead on Win7, I was tinkering with not even reserving the shadow at all, and had added this support. I think it simpler if we just say only Win8+ is recommended for x64, and document the page table overhead for Win7. This is better than the potential correctness problem that can result if somebody else gets memory where we think we have the shadow.

wang0109 added inline comments.Jul 6 2016, 2:14 PM
lib/asan/asan_win.cc
257

Yes. This piece of code was ported from a test program (credits to chrisha). I thought for a while and it should be not useful in the case of windows 10, apart from being defensive code. For window 7, I am not quite sure what's the behavior. One thing that I would like to test is, VirtualAlloc() seems to have alignment requirement of allocation granularity for any flag involving MEM_RESERVE, if I understand correctly. For MEM_COMMIT only flag, address only have to be aligned with page size.

wang0109 updated this revision to Diff 63446.Jul 10 2016, 4:43 PM
  • update diff: remove unneeded code, avoid commit limit error 1455

drive-by

lib/asan/asan_win.cc
262

Change this for a CHECK(...)

lib/sanitizer_common/sanitizer_win.cc
171 ↗(On Diff #63446)

IIRC, this function is used at other place. (MmapFixedNoReserve).
We should probably do a variant of that fonction, showing the "on-demand" behavior in the name.

178 ↗(On Diff #63446)

nit: execption -> exeception

wang0109 updated this revision to Diff 63447.Jul 10 2016, 4:57 PM
  • update diff: fix typo, use CHECK() to replace __debugbreak
wang0109 added inline comments.Jul 10 2016, 5:12 PM
lib/sanitizer_common/sanitizer_win.cc
171 ↗(On Diff #63447)

I see, this function also used in dfsan, esan etc. The name is already confusing.. It says "NoReserve" yet we are passing MEM_RESERVE flag..

rnk accepted this revision.Jul 11 2016, 12:53 PM
rnk edited edge metadata.

lgtm

lib/sanitizer_common/sanitizer_win.cc
171 ↗(On Diff #63447)

The name reflects the flags that you would pass to mmap on unix.

178 ↗(On Diff #63447)

still need to fix this

This revision is now accepted and ready to land.Jul 11 2016, 12:53 PM
wang0109 updated this revision to Diff 63557.Jul 11 2016, 1:00 PM
wang0109 edited edge metadata.
  • update diff: fix typo

This CL is not compiling.
Please fix:

../llvm/llvm/projects/compiler-rt/lib/asan/asan_win.cc:232:  Lines should be <= 80 characters long  [whitespace/line_length] [2]
../llvm/llvm/projects/compiler-rt/lib/asan/asan_win.cc:249:  Lines should be <= 80 characters long  [whitespace/line_length] [2]

and this

projects/compiler-rt/lib/asan/CMakeFiles/RTAsan_dynamic.x86_64.dir/asan_rtl.cc.o: In function `AsanInitInternal':
/usr/local/google/home/etienneb/llvm/llvm/projects/compiler-rt/lib/asan/asan_rtl.cc:423: undefined reference to `__asan::InitializePlatformExceptionHandlers()'
collect2: error: ld returned 1 exit status
wang0109 updated this revision to Diff 63570.Jul 11 2016, 2:05 PM
  • update diff: fix compile for linux/mac
  • update diff: coding style
This revision was automatically updated to reflect the committed changes.