This is an archive of the discontinued LLVM Phabricator instance.

[DFSan] Handle mmap() calls before interceptors are installed.
ClosedPublic

Authored by morehouse on Aug 18 2020, 1:23 PM.

Details

Summary

InitializeInterceptors() calls dlsym(), which calls calloc(). Depending
on the allocator implementation, calloc() may invoke mmap(), which
results in a segfault since REAL(mmap) is still being resolved.

We fix this by doing a direct syscall if interceptors haven't been fully
resolved yet.

Diff Detail

Event Timeline

morehouse created this revision.Aug 18 2020, 1:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2020, 1:23 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
morehouse requested review of this revision.Aug 18 2020, 1:23 PM
vitalybuka added inline comments.Aug 18 2020, 7:04 PM
compiler-rt/lib/dfsan/dfsan_interceptors.cpp
23

Have you considered EnsureInterceptorsInitialized from compiler-rt/lib/cfi/cfi.cpp or compiler-rt/lib/safestack/safestack.cpp
I suspect it should not work as you can't do INTERCEPT_FUNCTION here

Anyway as is it's a data race, some synchronization is needed.

morehouse added inline comments.Aug 19 2020, 8:15 AM
compiler-rt/lib/dfsan/dfsan_interceptors.cpp
23

Initialization happens during preinit_array, when we're single-threaded. The issue this solves is that initialization calls dlysm calls calloc calls mmap. If we did lazy-initialization, we'd get infinite recursion.

vitalybuka accepted this revision.Aug 19 2020, 2:50 PM
vitalybuka added inline comments.
compiler-rt/lib/dfsan/dfsan_interceptors.cpp
23

Interesting, I never considered that. Maybe it's relevant for other EnsureInterceptorsInitialized and we don't need locking there.

28

Can you please fix this ?
clang-format: please reformat the code

This revision is now accepted and ready to land.Aug 19 2020, 2:50 PM
vitalybuka added inline comments.Aug 19 2020, 2:51 PM
compiler-rt/lib/dfsan/dfsan_interceptors.cpp
27

can you comment about threading here?

vitalybuka added inline comments.Aug 19 2020, 2:59 PM
compiler-rt/lib/dfsan/dfsan_interceptors.cpp
23

Also it's not true for !SANITIZER_CAN_USE_PREINIT_ARRAY. I don't see any attempts to init dfsan without SANITIZER_CAN_USE_PREINIT_ARRAY, so I guess it's fine.

morehouse updated this revision to Diff 286670.Aug 19 2020, 3:04 PM
  • Reformat and add comment on synchronization.
morehouse marked 5 inline comments as done.Aug 19 2020, 3:05 PM
This revision was landed with ongoing or failed builds.Aug 19 2020, 3:08 PM
This revision was automatically updated to reflect the committed changes.