This is an archive of the discontinued LLVM Phabricator instance.

[lldb][AArch64] Add MakeTaggedRanges to MemoryTagManager
ClosedPublic

Authored by DavidSpickett on Oct 29 2021, 8:38 AM.

Details

Summary

This is to be used when you want to know what subranges
of a larger range have memory tagging. Like MakeTaggedRange
but memory without tags is skipped and you get a list of ranges back.

Will be used later by DumpDataExtractor to show memory tags.

MakeTaggedRanges assumes that the memory regions it is
given are sorted in ascending order and do not overlap.
For the current use case where you get regions from
GetMemoryRegions and are on some Linux like OS, this is
reasonable to assume.

I've used asserts to check those conditions. In future
any API binding will check them up front to prevent a crash.

Diff Detail

Event Timeline

DavidSpickett created this revision.Oct 29 2021, 8:38 AM
DavidSpickett requested review of this revision.Oct 29 2021, 8:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 29 2021, 8:38 AM
omjavaid added inline comments.Nov 8 2021, 3:13 AM
lldb/source/Plugins/Process/Utility/MemoryTagManagerAArch64MTE.cpp
71

I think this function name should be camel-case although there are examples of similar functions not following the camel case. I am bit unsure do you a reference for/against?

149

so what would be the behavior if regions do overlap. Should we return an error rather than keep going?

161

RemoveNonAddressBits hard-coded but symbols may resolve to higher order bits containing PACs. For now I only came across code pointers with PACs. But if you suspect code regions can be inputs to this function then may be make accommodating changes probably in separate patch.

168

this loop looks like a candidate for using range based for instead of declaring iterate. We can always break if range in invalid.

for (auto region : memory_regions) may be ?

  • Range based for loop over memory regions
  • Reduce number of ->GetRange calls
  • Check for overlapping regions up front and error if founda
  • Added test that the range invesion check removes ignores tags
  • Added the same test to existing MakeTaggedRange tests
DavidSpickett marked 2 inline comments as done.Nov 26 2021, 4:04 AM
DavidSpickett added inline comments.
lldb/source/Plugins/Process/Utility/MemoryTagManagerAArch64MTE.cpp
149

I've addressed this by checking up front for overlaps in the list. We might see this if:

  • there is invalid data from the remote
  • the remote is using a memory protection unit that allows overlaps (Arm M class allows this)

Afaik no hosted OS would let you have two mappings that use the same range of virtual memory.

There's no good way to reconcile the overlap so an error is what I've gone with. I'll handle this error in the later patch to DumpDataExtractor.

It is a shame to walk the regions up front but as mentioned in the comment I found some corner cases that lead to some ugly code that would have its own drawbacks.

Given that the eventual goal here is "memory read <...> --show-tags" which is opt in, and we expect a fairly small amount of regions, I think the compromise is worth it.

(if M class ever got memory tagging we can make this smarter overall)

161

The end goal here is "memory read <...> --show-tags" and that command will have to use the ABI plugin in any case so the addresses this gets will be PAC/TBI/MTE free already.

So for now this should be addressed by https://reviews.llvm.org/D103626. (once I convince myself which address should be show in the output but that's not for this issue)

I agree that there is a bit of a conflict here with the ABI plugin removing everything vs the MemoryTagManager removing only the tag. However I think that it will work fine in this case.

DavidSpickett added inline comments.Nov 26 2021, 4:06 AM
lldb/source/Plugins/Process/Utility/MemoryTagManagerAArch64MTE.cpp
161

Speaking of conflict, MemoryTagManagerAArch64MTE::RemoveNonAddressBits should probably be renamed to RemoveTagBits but I'll do that as another change.

DavidSpickett marked 2 inline comments as done.Jan 12 2022, 3:00 AM

ping!

lldb/source/Plugins/Process/Utility/MemoryTagManagerAArch64MTE.cpp
161

On second thought this will be better done in a general effort to remove the layering weirdness we currently have. Where we've got a tag manager that is removing the tags and the rest of the top byte. Then the ABI removing all of that and more.

Ideally the tag manager should only know about tags but some places assume it can also remove non-address bits in general. So this is more than just a rename and will take a bit more time.

Rebase, update the name of RemoveTagBits.

omjavaid added inline comments.Jan 23 2022, 6:43 PM
lldb/source/Plugins/Process/Utility/MemoryTagManagerAArch64MTE.cpp
178

From implementation of Process::GetMemoryRegions It seems our regions vector will always be sorted and non-overlapping? Is it no so?

Process::GetMemoryRegions starts from addr zero and keeps fetching regions based on end_addr of previous range. So in all cases regions will be sorted.

Since output of GetMemoryRegions will be sorted and not overlap,
switch the checks to asserts and don't sort the input regions.

Updated the testing to reflect that and added some comment
diagrams showing what regions are used for the later tests.

DavidSpickett edited the summary of this revision. (Show Details)Jan 25 2022, 6:56 AM
DavidSpickett marked an inline comment as done.
omjavaid accepted this revision.Jan 25 2022, 5:23 PM

LGTM Thanks! for your patience

This revision is now accepted and ready to land.Jan 25 2022, 5:23 PM

MakeTaggedRanges now takes const MemoryRegionInfos& like MakeTaggedRange does.

This revision was landed with ongoing or failed builds.Jan 26 2022, 3:30 AM
This revision was automatically updated to reflect the committed changes.