This is an archive of the discontinued LLVM Phabricator instance.

[HWASAN][LSAN] Fix false positive memory leak reports on X86_64
ClosedPublic

Authored by kstoimenov on Jul 14 2023, 1:54 PM.

Details

Summary

Before this patch when running HWASAN on x86_64 with memory tagging support we got a bunch of false memory leak reports. The reason for that is that the heuristic used to detect if an 8 bytes could be a user pointer was not valid when memory tagging is used as the top byte could contain non-zero information.

Diff Detail

Event Timeline

kstoimenov created this revision.Jul 14 2023, 1:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2023, 1:54 PM
Herald added a subscriber: Enna1. · View Herald Transcript
kstoimenov requested review of this revision.Jul 14 2023, 1:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2023, 1:54 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka added inline comments.
compiler-rt/lib/lsan/lsan_common.cpp
279

we should query this from kernel, like in CanUseTaggingAbi

vitalybuka added inline comments.Jul 14 2023, 2:28 PM
compiler-rt/lib/lsan/lsan_common.cpp
271–273

I am confused.
This one masking out 55:48, not the top byte?

279

Maybe it's fine keep it simple and handle the worst case https://lwn.net/Articles/902094/

vitalybuka added inline comments.Jul 17 2023, 11:48 AM
compiler-rt/lib/lsan/lsan_common.cpp
271–273

Never mind, the point to distinguish user space, so we check top bits AFTER top byte.

279

I measured "return true" and on average it's 1.5% regressions.

So it's measureable, current 17 bit vs 8 bit, I can't measure a difference, but still think it's safe to keep as much as possible bits, 11 should work for us.

Please drop TODO that we will need to refactore this code for LAM48, or 5 level page tables.

Added a test.

Updated comment.

Removed include.

vitalybuka added inline comments.Jul 17 2023, 3:28 PM
compiler-rt/lib/lsan/lsan_common.cpp
279

not done?

compiler-rt/test/lsan/TestCases/user_pointer.cpp
10–18

Maybe something more stronger

Addressed comments.

kstoimenov marked 2 inline comments as done.Jul 17 2023, 5:23 PM
kstoimenov added inline comments.
compiler-rt/test/lsan/TestCases/user_pointer.cpp
10–18

This code actually generates memory leaks because the allocated memory is not referenced by any pointer. I had a version where there are 2 arrays: one with the pointer and the other one with & ' p & (255ULL << 48)', but I am not sure how with would help. Probably the best is just to leave with one

vitalybuka accepted this revision.Jul 18 2023, 11:03 AM
vitalybuka added inline comments.
compiler-rt/test/lsan/TestCases/user_pointer.cpp
15

I see. So if we set masked bits, it will still fail other LSAN checks

so the test as is is not very useful, but not easy way change that.

This revision is now accepted and ready to land.Jul 18 2023, 11:03 AM
kstoimenov retitled this revision from [HWASAN][LSAN] Fix false positive memeory leak reports on X86 to [HWASAN][LSAN] Fix false positive memory leak reports on X86.Jul 18 2023, 11:24 AM
kstoimenov retitled this revision from [HWASAN][LSAN] Fix false positive memory leak reports on X86 to [HWASAN][LSAN] Fix false positive memory leak reports on X86_64.Jul 18 2023, 11:28 AM
kstoimenov edited the summary of this revision. (Show Details)
kstoimenov added inline comments.Jul 18 2023, 11:30 AM
compiler-rt/test/lsan/TestCases/user_pointer.cpp
15

I will catch false positives. This test failed with HWASAN on x86 without my fix. Granted a bunch of other tests would have also failed, but it is good to have an explicit one.

kstoimenov edited the summary of this revision. (Show Details)Jul 18 2023, 2:16 PM