This is an archive of the discontinued LLVM Phabricator instance.

[HWASAN] Implemented LsanMetadata in HWASAN.
ClosedPublic

Authored by kstoimenov on Dec 6 2022, 2:28 PM.

Details

Summary

Please note that that this code is not used. It will be connected at some point when the rest of the code is ready.

Diff Detail

Event Timeline

kstoimenov created this revision.Dec 6 2022, 2:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 6 2022, 2:28 PM
Herald added a subscriber: Enna1. · View Herald Transcript
kstoimenov requested review of this revision.Dec 6 2022, 2:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 6 2022, 2:28 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka added inline comments.
compiler-rt/lib/hwasan/hwasan_allocator.h
34–36

we should pack this into 16 bytes:

private:
atomic_uint64_t alloc_context_id;
u32 requested_size_low;
u16 requested_size_high;
public:
u8 right_aligned;
u8 lsan_tag;
37–38

please make alloc_context_id private so only it can be accessed with getters?

38–52

Naming consistency would be nice, but it's better to rename get_requested_size and set_requested_size in followup patch

59–60

please add static_assert(sizeof(Metadata) == 16)

kstoimenov updated this revision to Diff 480689.Dec 6 2022, 4:42 PM
kstoimenov marked 2 inline comments as done.

Addressed comments.

kstoimenov marked an inline comment as done.
kstoimenov added inline comments.
compiler-rt/lib/hwasan/hwasan_allocator.h
38–52

ACK.

kstoimenov updated this revision to Diff 480696.Dec 6 2022, 4:51 PM

Fixed constuctor.

kstoimenov updated this revision to Diff 480697.Dec 6 2022, 4:54 PM

Removed ForEachChunk.

compiler-rt/lib/hwasan/hwasan_allocator.cpp
485

I am basing this code on the ASAN implentation and there it stores the asan::AsanChunk * in the metadata_ variable. I could modify this code to actually store hwasan::Metadata. What do you think?

vitalybuka added inline comments.Dec 6 2022, 7:06 PM
compiler-rt/lib/hwasan/hwasan_allocator.cpp
91

CheckInvalidFree tries to match tag, but for LSAN we need this info even without user pointer, so we need something else

alloc_context_id is optional info, we cannot use it for the check
and get_requested_size is probably too ok for now, but TODO is not move into own flag,

note, right_aligned is always false, so unused. I recommend to remove it in a separate patch and use this byte for is_allocated

485

I am basing this code on the ASAN implentation and there it stores the asan::AsanChunk * in the metadata_ variable. I could modify this code to actually store hwasan::Metadata. What do you think?

Please clarify, I don't understand the question

494

We need something like atomic_uint8_t chunk_state of asan
LSAN one is not atomic, and I believe it's wrong.

compiler-rt/lib/hwasan/hwasan_allocator.h
56

Maybe move after the class?

kstoimenov added inline comments.Dec 7 2022, 9:25 AM
compiler-rt/lib/hwasan/hwasan_allocator.cpp
485

What I meant is to have this code like so:

LsanMetadata::LsanMetadata(uptr chunk) {
  auto *c = chunk ? reinterpret_cast<__hwasan::HwasanChunkView *>(
                        chunk - __hwasan::kChunkHeaderSize)
                  : nullptr;
  metadata_ = c->metadata_;
}
vitalybuka added inline comments.Dec 7 2022, 12:27 PM
compiler-rt/lib/hwasan/hwasan_allocator.cpp
494

There is no HwasanChunkView in the memory,
HwasanChunkView is a temp helper

vitalybuka added inline comments.Dec 8 2022, 12:15 PM
compiler-rt/lib/hwasan/hwasan_allocator.h
40

right_aligned? something wrong with rebase

kstoimenov updated this revision to Diff 481477.Dec 8 2022, 5:03 PM

Added chunk_state and inline.

Renamed methods to match the reset of the code.

kstoimenov marked 2 inline comments as done.

Added header file.

After rebase + merge.

vitalybuka added inline comments.Dec 12 2022, 11:46 AM
compiler-rt/lib/hwasan/hwasan_allocator.cpp
79–80

this must be be the last store
and I don't like that SetAllocContext also changes chunk_state

if you list combine them I would call it
SetAllocated(stack, size)

Still make sure atomic_store(&chunk_state is the last action on allocation HwasanAllocate
E.g. I believe zoreinit and tagging must happen before atomic_store(&chunk_state so leak sanitizer did not try to interpret data which is not allocated yet.

231

I would expect here meta->SetState
just before RunMallocHooks

498

and below

kstoimenov marked 2 inline comments as done.

Addressed comments.

kstoimenov marked 2 inline comments as done.Dec 12 2022, 12:19 PM
kstoimenov added inline comments.
compiler-rt/lib/hwasan/hwasan_allocator.cpp
75–76

Question: do we even need thread id? Where is is used?

vitalybuka added inline comments.Dec 12 2022, 1:36 PM
compiler-rt/lib/hwasan/hwasan_allocator.cpp
75–76

asan uses thread id
lsan does not, at least yet

you you want to drop it, maybe just make "uptr requested_size"

86–89

Here we do an oppisit acquire stuff, e.g. like in AtomicallySetQuarantineFlagIfAllocated called from asan Deallocate
we need to make sure that other fields are relevant up to the point we flip chunk_state

280–282

alloc_context_id is better name here

301–302

// TODO: consider meta->SetUnallocated(free_context_id);

kstoimenov marked 3 inline comments as done.

Addressed more comments.

compiler-rt/lib/hwasan/hwasan_allocator.cpp
75–76

I will keep it as is.

280–282

Should we use the whole thing then? I mean stack + tid instead of just stack?

301–302

Just to confirm this would check if free_context_id is the same in SetUnallocated to make sure that we are deallocating the right chunk?

vitalybuka accepted this revision.Dec 12 2022, 2:46 PM
vitalybuka added inline comments.
compiler-rt/lib/hwasan/hwasan_allocator.cpp
93–94
280–282

Sure, you can name it alloc_stack_id, but please rename in a separate patch together with free_context_id

301–302

No, we may use free_context_id instead of alloc_context_id.
It can be used for reporting.

Now, for some reasons, t->heap_allocations() is used to keep free_context_id

This revision is now accepted and ready to land.Dec 12 2022, 2:46 PM
This revision was landed with ongoing or failed builds.Dec 13 2022, 9:05 AM
This revision was automatically updated to reflect the committed changes.
kstoimenov marked 5 inline comments as done.