This is an archive of the discontinued LLVM Phabricator instance.

[msan] Fix PR32842
ClosedPublic

Authored by glider on May 5 2017, 9:52 AM.

Details

Reviewers
eugenis
kcc
Summary

It turned out that MSan was incorrectly calculating the shadow for int comparisons: it was done by truncating the result of (Shadow1 OR Shadow2) to i1, effectively rendering all bits except LSB useless.
This approach doesn't work e.g. in the case where the values being compared are even (i.e. have the LSB of the shadow equal to zero).
Instead, if CreateShadowCast() has to cast a bigger int to i1, we replace the truncation with an ICMP to 0.

This patch doesn't affect the code generated for SPEC 2006 binaries, i.e. there's no performance impact.

For the test case reported in PR32842 MSan with the patch generates a slightly more efficient code:

orq     %rcx, %rax
jne     .LBB0_6

, instead of:

orl     %ecx, %eax
testb   $1, %al
jne     .LBB0_6

Diff Detail

Event Timeline

glider created this revision.May 5 2017, 9:52 AM
glider edited the summary of this revision. (Show Details)May 8 2017, 4:13 AM
glider edited the summary of this revision. (Show Details)May 8 2017, 5:07 AM
eugenis edited edge metadata.May 8 2017, 12:55 PM

Please add an IR test, and, optionally, a runtime test in compiler-rt.

lib/Transforms/Instrumentation/MemorySanitizer.cpp
1581

Inner braces are unnecessary.

glider updated this revision to Diff 98282.May 9 2017, 7:58 AM

Fixed the comment.
The test is in https://reviews.llvm.org/D33002

glider marked an inline comment as done.May 9 2017, 7:58 AM

Still missing an IR test, which should look something like test/Instrumentation/MemorySanitizer/msan_basic.ll - pass a single-instruction function through opt -msan and see what comes out.

Also, if you have a program that fails to find a bug, add it as a test under projects/compiler-rt/test/msan/.

glider updated this revision to Diff 98453.May 10 2017, 7:32 AM

Added an IR test suggested by Evgenii.

Thanks for the suggestion, works for me!
I've also added a compiler-rt regtest, see https://reviews.llvm.org/D33043

eugenis accepted this revision.May 10 2017, 11:48 AM

LGTM with a note

test/Instrumentation/MemorySanitizer/pr32842.ll
14

%1 and %0 could be unreliable.
Just test the entire instruction sequence, something like this:
%[[A]] = load.*msan_param_tls
%[[B]] = load.*
msan_param_tls
%[[C]] = or i32 %[[A], %[[B]]
%[[D]] = icmp ne i32%[[C]], 0
store i1 %[[D]], .*__msan_retval_tls

This revision is now accepted and ready to land.May 10 2017, 11:48 AM
glider updated this revision to Diff 98605.May 11 2017, 3:40 AM

Fixed the test.

Committed in r302787.

eugenis closed this revision.Jun 2 2017, 1:49 PM