Skip to content

Fix for using macho in Pry/IRB#431

Merged
woodruffw merged 5 commits intoHomebrew:masterfrom
rickmark:rickmark/pry_perf
Jan 18, 2022
Merged

Fix for using macho in Pry/IRB#431
woodruffw merged 5 commits intoHomebrew:masterfrom
rickmark:rickmark/pry_perf

Conversation

@rickmark
Copy link
Contributor

Using 'macho' in Pry/IRB was slow due to the editor printing the entire contents of @raw_data, this makes the inspect element print only the length of the view.

Using 'macho' in Pry/IRB was slow due to the editor printing the entire contents of @raw_data, this makes the inspect element print only the length of the view.
@woodruffw
Copy link
Member

LGTM! Mind fixing the lint?

Offenses:

lib/macho/view.rb:34:28: C: [Correctable] Style/RedundantSelf: Redundant self detected.
      "#<#{self.class}:0x#{self.__id__.to_s(16)} @endianness=#{@endianness.inspect}, @offset=#{@offset.inspect}, length=#{@raw_data.length}>"
                           ^^^^

11 files inspected, 1 offense detected, 1 offense auto-correctable
Error: Process completed with exit code 1.

@rickmark
Copy link
Contributor Author

Doing so now - also adding a MachOFile reference to MachOView so that you can navigate backwards from a command to the file (some commands make use of other commands in a file such as MH_FILESET_ENTRY to the LC_LOAD at the same address)

@woodruffw
Copy link
Member

also adding a MachOFile reference to MachOView so that you can navigate backwards from a command to the file (some commands make use of other commands in a file such as MH_FILESET_ENTRY to the LC_LOAD at the same address)

Cool, that should be fine as long as we don't break any APIs that allow for dynamic construction/creation of LCs 🙂

@rickmark
Copy link
Contributor Author

Looking to exercise new code paths now

@rickmark
Copy link
Contributor Author

also adding a MachOFile reference to MachOView so that you can navigate backwards from a command to the file (some commands make use of other commands in a file such as MH_FILESET_ENTRY to the LC_LOAD at the same address)

Cool, that should be fine as long as we don't break any APIs that allow for dynamic construction/creation of LCs 🙂

Simple solution was to attach the MachOFile to the MachOView so navigation would be command -> view -> macho_file -> command which could be shortened by delegation but hey

First example of load commands that are able to navigate from one to the other (LC_FILESET_ENTRY is paired to a LC_SEGMENT64 that occurs at the same fileoff).  This should provide required test coverage of MachOView to MachOFile navigation
@rickmark
Copy link
Contributor Author

Ok I'm a bad person... This PR expanded scope to cover the use case.

The E2E capability is load BootKernelCollection in pry (fast due to the @raw_data fix)

Iterate over FH_FILESET_ENTRY and map each to .segment and extract

@rickmark
Copy link
Contributor Author

Added some tests to make CI happy

@woodruffw
Copy link
Member

LGTM structurally, although I'm a little bit confused by what's going on with FilesetEntryCommand#segment here -- it looks like you're using it to find a corresponding segment load command for the embedded Mach-O, but I was under the impression that fileoff could be within a segment and not just at the start of one. Is that a misunderstanding on my part?

@rickmark
Copy link
Contributor Author

LGTM structurally, although I'm a little bit confused by what's going on with FilesetEntryCommand#segment here -- it looks like you're using it to find a corresponding segment load command for the embedded Mach-O, but I was under the impression that fileoff could be within a segment and not just at the start of one. Is that a misunderstanding on my part?

Let me do some more testing - it seems to me that the .kc file has __REGION{digit} that have a file offset - the FILESET_INFO has a matching fileoff and annotates

I'm thinking of creating a Git LFS repo for text mach-os (large ones like kc files and prelink kernels that can be brought into testing if you want the other larger types.

@woodruffw
Copy link
Member

Makes sense! I didn't realize each had a __REGION. If that's the observed behavior (knowing Apple, I'm sure it isn't documented), then I'm good with this approach.

@rickmark
Copy link
Contributor Author

rickmark commented Jan 14, 2022 via email

@woodruffw
Copy link
Member

We have a unified CI configuration set up for Homebrew, so I think we're good without commit hooks for the time being.

@rickmark
Copy link
Contributor Author

rickmark commented Jan 14, 2022 via email

@woodruffw
Copy link
Member

Oh, I misunderstood. Sure, as long as they're opt-in, I would accept a PR enabling them for rubocop.

Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! We should give some though to perhaps refactoring MachOView entirely so that it doesn't effectively have two copies/references of the raw Mach-O data in it, now that we store both macho_file and raw_data. In particular, raw_data is probably replaceable with an offset and length for macho_file.raw_data. But that can wait for another PR.

@woodruffw woodruffw merged commit 9fa5c47 into Homebrew:master Jan 18, 2022
@rickmark rickmark deleted the rickmark/pry_perf branch January 18, 2022 00:41
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants