This is an archive of the discontinued LLVM Phabricator instance.

Fix an off-by-one error with armv7 mach-o corefile register contexts (LC_THREADs)
ClosedPublic

Authored by jasonmolenda on Apr 25 2023, 6:14 PM.

Details

Summary

Slava was doing some fixups in ObjectFileMachO last August and one of the changes had a small off-by-one error, https://reviews.llvm.org/D131554 . A bunch of folks (including myself) looked at this specific bit of code and didn't see the issue, and there's no testsuite coverage for it.

This bit of code in RegisterContextDarwin_arm_Mach::SetRegisterDataFrom_LC_THREAD is reading the data for an ARM_THREAD_STATE register context, which has a fixed size for all practical purposes here. (the whole scheme of having ARM_THREAD_STATE + ARM_THREAD_STATE_COUNT is to allow for new registers to be added in the future and code to be able to detect which version it is based on size -- but I don't expect us to add additional register to the armv7 register context as this point).

Slava's change added a bounds check to ensure it was non-zero and within the max size of the ARM_THREAD_STATE state size. This bounds check didn't account for the cpsr register after the set of general purpose registers, so the register context wasn't being loaded from corefiles. We can simplify this by checking for equality for the total number of words for ARM_THREAD_STATE.

I also added a new test case that creates an empty armv7 and arm64 mach-o corefile with two register contexts, loads them into lldb and confirms that it could retrieve registers from both of the register contexts in both. I only have this test set to build & run on Darwin systems because I use Mach-O constants in the utility I wrote to create the nearly-empty corefiles. Too easy to miss a tiny problem like this when there's no test coverage.

Diff Detail

Event Timeline

jasonmolenda created this revision.Apr 25 2023, 6:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 25 2023, 6:14 PM
jasonmolenda requested review of this revision.Apr 25 2023, 6:14 PM
fixathon accepted this revision.Apr 25 2023, 6:58 PM

Great catch. Thank you for finding and fixing this!

This revision is now accepted and ready to land.Apr 25 2023, 6:58 PM