This is an archive of the discontinued LLVM Phabricator instance.

tsan: support pie binaries on newer kernels
ClosedPublic

Authored by dvyukov on Sep 20 2016, 9:07 AM.

Details

Reviewers
kcc
Summary

4.1+ Linux kernels map pie binaries at 0x55:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=d1fd836dcf00d2028c700c7e44d2c23404062c90
Currently tsan does not support app memory at 0x55 (https://github.com/google/sanitizers/issues/503).
Older kernels also map pie binaries at 0x55 when ASLR is disables (most notably under gdb).

This change extends tsan mapping for linux/x86_64 to cover 0x554-0x568 app range and fixes both 4.1+ kernels and gdb.

This required to slightly shrink low and high app ranges and move heap. The mapping become even more non-linear, since now we xor lower bits. Now even a continuous app range maps to split, intermixed shadow ranges. This breaks ShadowToMemImpl as it assumes linear mapping at least within a continuous app range (however it turned out to be already broken at least on arm64/42-bit vma as uncovered by r281970). So also change ShadowToMemImpl to hopefully a more robust implementation that does not assume a linear mapping.

Diff Detail

Event Timeline

dvyukov updated this revision to Diff 71945.Sep 20 2016, 9:07 AM
dvyukov retitled this revision from to tsan: support pie binaries on newer kernels.
dvyukov updated this object.
dvyukov added a reviewer: kcc.
dvyukov added subscribers: kubamracek, rengolin.
dvyukov updated this revision to Diff 71947.Sep 20 2016, 9:08 AM

+Jakub,

If/when it lands, it would be useful to cherry-pick to gcc as it fixes a very real problem.

Well want to merge all the libsanitizer changes in the next month or two at most, but sure, this can be also desirable for release branch backporting eventually.

kcc accepted this revision.Sep 20 2016, 1:29 PM
kcc edited edge metadata.

LGTM for this change, as unavoidable. (assuming you've tested it an various kernels)
But this is very sad.
Maybe you can file a bug against kernel to request some kind of feature that will protect us from changing the shadow in future?
E.g. if we can put something in the binary to tell the kernel to not touch (or to pre-allocate) certain ranges?

This revision is now accepted and ready to land.Sep 20 2016, 1:29 PM
In D24769#547979, @kcc wrote:

Maybe you can file a bug against kernel to request some kind of feature that will protect us from changing the shadow in future?
E.g. if we can put something in the binary to tell the kernel to not touch (or to pre-allocate) certain ranges?

It seems that it will require extending ELF format, linkers, compiler drivers, dynamic loaders and kernel. Looks like a huge amount of work. Kernel bug tracker is for bugs, for features you just implement them and mail patches.

Submitted as 282152. Monitoring bots.