This is an archive of the discontinued LLVM Phabricator instance.

Don't instrument UBSan-generated code with ASan.
ClosedPublic

Authored by samsonov on Jul 16 2014, 12:43 PM.

Details

Reviewers
rsmith
Summary

This change adds "!ubsan" metadata to all the instructions
generated by Clang CodeGen to implement variety of UBSan checks. This
metadata can later be used by ASan instrumentation pass in LLVM backend
to avoid instrumenting UBSan-generated code. This should fix PR20085.

Diff Detail

Event Timeline

samsonov updated this revision to Diff 11521.Jul 16 2014, 12:43 PM
samsonov retitled this revision from to Don't instrument UBSan-generated code with ASan..
samsonov updated this object.
samsonov edited the test plan for this revision. (Show Details)
samsonov added a reviewer: rsmith.
samsonov added subscribers: kcc, byoungyoung, Unknown Object (MLST).
rsmith added inline comments.Jul 16 2014, 5:26 PM
tools/clang/lib/CodeGen/CGExprScalar.cpp
839

Several callers of this are only guarding their call to this function; how about sinking the SanitizerScope down into here (and teaching the scope to cope with multiple SanitizerScope objects being live at once)?

tools/clang/lib/CodeGen/CodeGenFunction.cpp
1660

It seems more future-safe to pick a more general name for this, describing its functionality not this particular use case. Maybe "nosanitize"?

samsonov added inline comments.Jul 16 2014, 5:47 PM
tools/clang/lib/CodeGen/CGExprScalar.cpp
839

On the contrary, there are no callers that have SanitizerScope only to guard the call to this function. Consider:

EmitBinOpCheck(Builder.CreateICmpULE(RHS, GetWidthMinusOneValue(Ops.LHS, RHS)), Ops);

we want to attach metadata to ICmpULE instruction created in this line. This is my main motivation for using asserts instead of re-entrant sanitizer scopes - if one adds a new UBSan check and writes the code like:

if (SanOpts->SanitizeFoo) {
  Bar = Builder.CreateBar();
  Baz = Builder.CreateBaz(Bar);
  EmitBinOpCheck(Baz, Info);
}

this will die with an assertion failure. If we allow multiple scopes, we won't notice missing metadata for bar and baz instructions.

tools/clang/lib/CodeGen/CodeGenFunction.cpp
1660

Sounds good.

rsmith added inline comments.Jul 16 2014, 6:04 PM
tools/clang/lib/CodeGen/CGExprScalar.cpp
839

Thanks, you're quite right :)

samsonov updated this revision to Diff 11551.Jul 16 2014, 6:25 PM

Rename !ubsan metadata to more generic and appropriate !nosanitize.

tools/clang/lib/CodeGen/CodeGenFunction.cpp
1660

Done.

rsmith accepted this revision.Jul 16 2014, 7:54 PM
rsmith edited edge metadata.

LGTM

This revision is now accepted and ready to land.Jul 16 2014, 7:54 PM
samsonov closed this revision.Jul 17 2014, 11:57 AM

Thanks! Submitted as r213291 and r213292.