This is an archive of the discontinued LLVM Phabricator instance.

[hwasan] Stack instrumentation.
ClosedPublic

Authored by eugenis on Dec 27 2017, 3:34 PM.

Details

Summary

Very basic stack instrumentation using tagged pointers.
Tag for N'th alloca in a function is built as XOR of:

  • base tag for the function, which is just some bits of SP (poor man's random)
  • small constant which is a function of N.

Allocas are aligned to 16 bytes. On every ReturnInst allocas are
re-tagged to catch use-after-return.

This implementation has a bunch of issues that will be taken care of
later:

  1. lifetime intrinsics referring to tagged pointers are not recognized in SDAG. This effectively disables stack coloring.
  2. Generated code is quite inefficient. There is one extra instruction at each memory access that adds the base tag to the untagged alloca address. It would be better to keep tagged SP in a callee-saved register and address allocas as an offset of that XOR retag, but that needs better coordination between hwasan instrumentation pass and prologue/epilogue insertion.
  3. Lifetime instrinsics are ignored and use-after-scope is not implemented. This would be harder to do than in ASan, because we need to use a differently tagged pointer depending on which lifetime.start / lifetime.end the current instruction is dominated / post-dominated.

Diff Detail

Repository
rL LLVM

Event Timeline

eugenis created this revision.Dec 27 2017, 3:34 PM
alekseyshl accepted this revision.Jan 3 2018, 8:25 PM
This revision is now accepted and ready to land.Jan 3 2018, 8:25 PM
kcc accepted this revision.Jan 11 2018, 12:28 PM

LGTM with two nits, feel free to address them separately.

compiler-rt/test/hwasan/TestCases/stack-oob.cc
1 ↗(On Diff #128245)

I'd add more sizes here.

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
355 ↗(On Diff #128245)

Will this work if

  • the size is large and memset doesn't get inlined
  • hwasan has the msan interceptor

?

355 ↗(On Diff #128245)

I mean,

  • hwasan has the memset interceptor
eugenis added inline comments.Jan 11 2018, 2:27 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
355 ↗(On Diff #128245)

That's why I'm changing the memset interceptor to skips checks if the address is in the shadow range.

eugenis updated this revision to Diff 129532.Jan 11 2018, 2:38 PM

extended the test

kcc added inline comments.Jan 11 2018, 2:42 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
355 ↗(On Diff #128245)

of course! I didn't connect the dots.
Then just leave a comment about this here, and remove the commented code above.

eugenis updated this revision to Diff 129534.Jan 11 2018, 2:46 PM

added a comment

This revision was automatically updated to reflect the committed changes.