This is an archive of the discontinued LLVM Phabricator instance.

[HWASan] Add basic stack tagging support for LAM.
ClosedPublic

Authored by morehouse on May 20 2021, 11:43 PM.

Details

Summary

Adds the basic instrumentation needed for stack tagging.

Currently does not support stack short granules or TLS stack histories,
since a different code path is followed for the callback instrumentation
we use.

We may simply wait to support these two features until we switch to
a custom calling convention.

Diff Detail

Event Timeline

xiangzhangllvm requested review of this revision.May 20 2021, 11:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 20 2021, 11:43 PM
morehouse added inline comments.May 24 2021, 1:03 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

Can we avoid creating ClUntagPointer for now? I am able to test locally with QEMU, and I'm also setting up a buildbot to ensure new patches don't break the LAM functionality.

192

Can we wire ClUsePageAlias up to fsanitize-hwaddress-experimental-aliasing in this patch? Then we can make ClUsePageAlias default to false and can remove this TODO.

193–194
268

Since getTargetTagShift and getTargetTagMask are used only during initializeModule, and since they are both tiny functions, let's remove them and inline the logic in initializeModule.

270

Since this is no longer constexpr, let's remove the k prefix.

499–504

For now we want to keep InstrumentWithCalls == true for x86 in general, even in LAM mode (no aliases). Please update this line.

557–559

We also want to avoid instrumenting globals for now, so please update this line to apply to x86 in general.

715

Please keep this check here for now. I want to avoid reintroducing untagging to x86.

981

The function name is confusing. Can we name it something like applyTagMask?

1016

This mask of the base tag seems unnecessary since we also mask the final tag.

1047

It seems like overkill to move the logic into this function. Let's leave it as part of tagPointer.

1054

Let's avoid implementing the kernel case for LAM, since we don't have a good way to test yet.

1528

Let's avoid doing this for now. I'd like to focus just on stack support in this patch. Future patches can address global support.

llvm/test/Instrumentation/HWAddressSanitizer/X86/stack.ll
5 ↗(On Diff #346931)

Instead of making a new test, can we reuse the alloca tests for aarch64 and update the expected instrumentation?

Hello @morehouse, thanks very much for your careful review, I'll fix all your comments, just for "ClUntagPointer", I feel it very useful in my develop.

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

Yes, of course, we can. But here ClUntagPointer is disabled in default (false), no affect our current tests.
I and back this option, because I find It is very helpful for me in developing HWASAN without hardware or simulator supported. I can run most simple test on my local machine.

1016

make sense, it seem will handle at retagTargetTag fucntion.

morehouse added inline comments.May 25 2021, 8:31 AM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

It may be inconvenient for now, but as part of setting up a buildbot I'm also automating the process of setting up the custom QEMU and kernel required to test LAM. If all goes well, it should land later this week or next week. Then you can use that instead of the ClUntagPointer option.

For now, could you keep the ClUntagPointer patch locally for your testing purposes, and remove it from the patch we actually submit?

NO problem, Thanks!

xiangzhangllvm marked 11 inline comments as done.
xiangzhangllvm marked 3 inline comments as done.Jun 1 2021, 6:57 PM

Sorry for late update, a little busy in these 2 month : )

Thanks for the update. I'll take a closer look tomorrow and try testing locally. You can also use the new buildbot script to test in QEMU (see inline comment below).

@eugenis @vitalybuka If you could take a look as well that would be helpful. I'm not too familiar with the stack retagging instrumentation.

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

Thanks. FYI, the buildbot is now live: https://lab.llvm.org/buildbot/#/builders/169

You can test locally using the buildbot script here. You can comment out the Scudo stuff to avoid testing that.

You'll need a QEMU image located at /b/qemu_image. You can build one using this script.

193–194

This comment wasn't addressed.

llvm/test/Instrumentation/HWAddressSanitizer/X86/stack.ll
5 ↗(On Diff #346931)

I see this test is gone now, but I don't see any alloca tests copied into the X86 directory. Maybe you forgot to add them in the patch?

xiangzhangllvm added inline comments.Jun 2 2021, 5:45 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

Thanks for your work! let me do a try.

193–194

Oh, yes, I should use "hwasan-experimental-use-page-aliases", thank you very much!

llvm/test/Instrumentation/HWAddressSanitizer/X86/stack.ll
5 ↗(On Diff #346931)

Did you mean the llvm/test/Instrumentation/HWAddressSanitizer/alloca.ll ?
It already at the parent directory of this test (llvm/test/Instrumentation/HWAddressSanitizer/X86)
So, the check-all can test it.

morehouse added inline comments.Jun 2 2021, 5:49 PM
llvm/test/Instrumentation/HWAddressSanitizer/X86/stack.ll
5 ↗(On Diff #346931)

I meant that one and the other tests with alloca in the name. Those are testing instrumentation for aarch64. I want to also check the instrumentation for them on x86, since we have differences between the architectures.

xiangzhangllvm added inline comments.Jun 3 2021, 12:44 AM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

How can I run it online for this patch ? (it seems jobs in https://lab.llvm.org/buildbot/#/builders/169 are on line )

(For locally test, I can't download the QEMU in my company's server, for network permission and without root authority)

morehouse added inline comments.Jun 3 2021, 12:32 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

Can't run it online unless its been submitted.

Sounds like your setup isn't well suited for working on LAM support. I was planning to work on stack and globals support anyway, so perhaps I will commandeer these patches and work on them myself.

eugenis added inline comments.Jun 3 2021, 2:15 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
967

retagMaskX86

You could also make this non-static and get rid of the second argument. Or apply the mask in retagMask.

1016

It seems more efficient to mask StackTag, and then make sure that all retag constants are within the mask bits. This way you'll only need to apply the mask once per function.

1037

Why not simply replace 0xFF here with TagMaskByte?

xiangzhangllvm added inline comments.Jun 3 2021, 8:42 PM
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
190

OK, no problem, feel free to use this patch, Let co-push the HWASAN work.
I'll continue to refine it (finish you and eugenis's comments).

1037

Because "xor" will let Tag > TagMask again : )

morehouse commandeered this revision.Jun 7 2021, 3:58 PM
morehouse edited reviewers, added: xiangzhangllvm; removed: morehouse.

I've addressed most comments locally; commandeering to prevent wasting Xiang's time addressing the feedback.

Will upload with tests tomorrow.

morehouse retitled this revision from [HWASAN] Update pointer tag for X86_64 to [HWASan] Add basic stack tagging support for LAM..Jun 9 2021, 12:29 PM
morehouse edited the summary of this revision. (Show Details)
morehouse updated this revision to Diff 350975.Jun 9 2021, 12:30 PM
morehouse marked 3 inline comments as done.
  • Rename flag as experimental.
  • Refactor and simplify code.
  • Apply mask to base tag only.
  • Enable some compiler-rt stack tests on x86.
  • Add IR tests.
Herald added a project: Restricted Project. · View Herald TranscriptJun 9 2021, 12:30 PM
Herald added subscribers: Restricted Project, cfe-commits. · View Herald Transcript
morehouse updated this revision to Diff 350976.Jun 9 2021, 12:38 PM
  • Privatize new member variables.
vitalybuka accepted this revision.Jun 9 2021, 1:17 PM
vitalybuka added inline comments.
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
504

As is it's not technically correct

518

maybe move up and combine with other IsX86 checks about

960

const

This revision is now accepted and ready to land.Jun 9 2021, 1:17 PM
morehouse updated this revision to Diff 350989.Jun 9 2021, 1:26 PM
morehouse marked 2 inline comments as done.
  • - Address nits.
morehouse updated this revision to Diff 351459.Jun 11 2021, 8:18 AM
  • Fix clang test failure on Windows.
This revision was landed with ongoing or failed builds.Jun 11 2021, 8:22 AM
This revision was automatically updated to reflect the committed changes.