Skip to content

Read machO sections section by section #37043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

augusto2112
Copy link
Contributor

@augusto2112 augusto2112 commented Apr 23, 2021

This changes how ReflectionContext reads machO reflection sections by reading them individually instead of as one big memory block spanning from the first to the last section (and including whatever else is in between). This change will enable an optimization on LLDB's side where, if we're reading read-only data, we read from the file-cache instead of the child process, which should speed up debugging when working with remote processes.

This changes how ReflectionContext reads machO reflection sections by reading them individually instead of as one big memory block spanning from the first to the last section (and including whatever else is in between). This change will enable an optimization on LLDB's side where, if we're reading read-only data, we read from the file-cache instead of the child process, which should speed up debugging when working with remote processes.
@augusto2112
Copy link
Contributor Author

@swift-ci Please test

@augusto2112
Copy link
Contributor Author

@swift-ci Please smoke test

@adrian-prantl
Copy link
Contributor

The only downside I can see in this patch is that we are making ~5x more individual calls to the MemoryReader (one per section instead of 1 per file) but that would seem to be the right trade-off for the majority of use-cases.

@mikeash
Copy link
Contributor

mikeash commented Apr 23, 2021

LGTM. I think the existing "read one big chunk" scheme was meant to cut down on calls to lldb's memory reader because it was faster to do it that way at that time. If it's now faster to make separate calls, let's do it!

Copy link
Contributor

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

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

Would be good to get @mikeash 's feedback too.

@@ -244,11 +215,13 @@ class ReflectionContext
if (strncmp(S->sectname, Name.data(), strlen(Name.data())) != 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not your code, but it's inefficient to call strlen(StringRef.data()), this should be Name.size() instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps you could fix that in a separate follow-up commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

Comment on lines +220 to +221
if (!LocalSectBuf)
return {nullptr, 0};
Copy link
Contributor

@kastiglione kastiglione Apr 23, 2021

Choose a reason for hiding this comment

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

Update: Don't read, it's all wrong.


I was curious about this, whether a problem with one section could cause issues that previous didn't exist. It doesn't look like it, but I do see something that looks like it could be a problem in the surrounding code.

This lambda is used to read the following sections:

__swift5_fieldmd
__swift5_assocty
__swift5_builtin
__swift5_capture
__swift5_typeref
__swift5_reflstr

If any of these sections are missing or can't be read, then readMachOSections returns early:

if (FieldMdSec.first == nullptr &&
    AssocTySec.first == nullptr &&
    BuiltinTySec.first == nullptr &&
    CaptureSec.first == nullptr &&
    TypeRefMdSec.first == nullptr &&
    ReflStrMdSec.first == nullptr)
  return false;

ReflectionInfo info = {
    {FieldMdSec.first, FieldMdSec.second},
    {AssocTySec.first, AssocTySec.second},
    {BuiltinTySec.first, BuiltinTySec.second},
    {CaptureSec.first, CaptureSec.second},
    {TypeRefMdSec.first, TypeRefMdSec.second},
    {ReflStrMdSec.first, ReflStrMdSec.second}};

this->addReflectionInfo(info);

Question

I looked at my small swift program, and it has only 3 of these sections, it does not have:

  • __swift5_assocty
  • __swift5_builtin
  • __swift5_reflstr

I am not especially familiar with these sections, but I can imagine there being some images that don't use associated types.

In practice, can these sections truly be absent, and, what does this break when one of these sections is missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

That logic skips the image only if all sections are missing. If any are present, then it gets added.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh facepalm haha

Copy link
Contributor

@kastiglione kastiglione Apr 23, 2021

Choose a reason for hiding this comment

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

can I delete these comments so nobody sees how poor my reading comprehension is?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, but we've all been there, no worries.

Copy link
Contributor

Choose a reason for hiding this comment

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

yup! I was kidding about deleting :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants