-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[lld] check cache before real_path in loadDylib #140791
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,19 +229,31 @@ static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs; | |
|
|
||
| DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella, | ||
| bool isBundleLoader, bool explicitlyLinked) { | ||
| // Frameworks can be found from different symlink paths, so resolve | ||
| // symlinks before looking up in the dylib cache. | ||
| SmallString<128> realPath; | ||
| std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath); | ||
| CachedHashStringRef path(!err ? uniqueSaver().save(StringRef(realPath)) | ||
| : mbref.getBufferIdentifier()); | ||
| CachedHashStringRef path(mbref.getBufferIdentifier()); | ||
| DylibFile *&file = loadedDylibs[path]; | ||
| if (file) { | ||
| if (explicitlyLinked) | ||
| file->setExplicitlyLinked(); | ||
| return file; | ||
| } | ||
|
|
||
| // Frameworks can be found from different symlink paths, so resolve | ||
| // symlinks and look up in the dylib cache. | ||
| DylibFile *&realfile = file; | ||
| SmallString<128> realPath; | ||
| std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath); | ||
| if (!err) { | ||
| CachedHashStringRef resolvedPath(uniqueSaver().save(StringRef(realPath))); | ||
| realfile = loadedDylibs[resolvedPath]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a subtle use-after-invalidation here. Both See https://crbug.com/422206408 for a reproducer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the repro and revert, looks like this is going to need a better approach. |
||
| if (realfile) { | ||
| if (explicitlyLinked) | ||
| realfile->setExplicitlyLinked(); | ||
|
|
||
| file = realfile; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant since both As are all
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. At this point, in my head, The
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++ references are sneaky. Once they're bound, they cannot be re-bound. I.e., after
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not have added that line then. I found this about references to
I am not sure if |
||
| return realfile; | ||
| } | ||
| } | ||
|
|
||
| DylibFile *newFile; | ||
| file_magic magic = identify_magic(mbref.getBuffer()); | ||
| if (magic == file_magic::tapi_file) { | ||
|
|
@@ -253,6 +265,7 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella, | |
| } | ||
| file = | ||
| make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked); | ||
| realfile = file; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the common case we have Then what happens on this line? It does a load and a store to the same address? I'm sure it does the right thing it just looks funny to me. Also, if we set
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thats what I thought would happen, yes.
What would you suggest? Ultimately we have 2 cache pointers, that may or may not point to the same thing, and we need to update them.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we just use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would mean that we would never cache symlinks, only their resolved paths, which is the unhappy path. That would be a significant regression as a large number of load commands are symlinks (eg I also disagree with the idea of regressing the performance to reduce the amount of code by 2 lines. The only alternative I can see is to do have realfile default to a nullptr and have the two setters change to: Can't say I prefer it though.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would something like Edit: or add a single line after this |
||
|
|
||
| // parseReexports() can recursively call loadDylib(). That's fine since | ||
| // we wrote the DylibFile we just loaded to the loadDylib cache via the | ||
|
|
@@ -268,6 +281,7 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella, | |
| magic == file_magic::macho_executable || | ||
| magic == file_magic::macho_bundle); | ||
| file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked); | ||
| realfile = file; | ||
|
|
||
| // parseLoadCommands() can also recursively call loadDylib(). See comment | ||
| // in previous block for why this means we must copy `file` here. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.