Page MenuHomePhabricator

[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

Repository
rL LLVM

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 ↗(On Diff #62554)

Does this can be done into: InitializeExceptionHandlerOnWindows64

244 ↗(On Diff #62554)

no { } here

265 ↗(On Diff #62554)

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

271 ↗(On Diff #62554)

ditto 'auto'

280 ↗(On Diff #62554)

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 ↗(On Diff #62593)

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 ↗(On Diff #62593)

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 ↗(On Diff #62593)

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 ↗(On Diff #62741)

Use GetPageSizeCached() and GetMmapGranularity() instead.

249–250 ↗(On Diff #62741)

Use RoundDownTo instead of the bitmath

257 ↗(On Diff #62741)

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

260–261 ↗(On Diff #62741)

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 ↗(On Diff #62961)

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 ↗(On Diff #62961)

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 ↗(On Diff #63446)

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.