Skip to content

Commit c34351c

Browse files
committed
Revert "[lld] check cache before real_path in loadDylib (#140791)"
This is causing use-after-frees due to references getting invalidating after the loadedDylibs map grows, see comments on the PR. This reverts commit 475a8a4.
1 parent c3057de commit c34351c

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

lld/MachO/DriverUtils.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,31 +227,19 @@ static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
227227

228228
DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
229229
bool isBundleLoader, bool explicitlyLinked) {
230-
CachedHashStringRef path(mbref.getBufferIdentifier());
230+
// Frameworks can be found from different symlink paths, so resolve
231+
// symlinks before looking up in the dylib cache.
232+
SmallString<128> realPath;
233+
std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
234+
CachedHashStringRef path(!err ? uniqueSaver().save(StringRef(realPath))
235+
: mbref.getBufferIdentifier());
231236
DylibFile *&file = loadedDylibs[path];
232237
if (file) {
233238
if (explicitlyLinked)
234239
file->setExplicitlyLinked();
235240
return file;
236241
}
237242

238-
// Frameworks can be found from different symlink paths, so resolve
239-
// symlinks and look up in the dylib cache.
240-
DylibFile *&realfile = file;
241-
SmallString<128> realPath;
242-
std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
243-
if (!err) {
244-
CachedHashStringRef resolvedPath(uniqueSaver().save(StringRef(realPath)));
245-
realfile = loadedDylibs[resolvedPath];
246-
if (realfile) {
247-
if (explicitlyLinked)
248-
realfile->setExplicitlyLinked();
249-
250-
file = realfile;
251-
return realfile;
252-
}
253-
}
254-
255243
DylibFile *newFile;
256244
file_magic magic = identify_magic(mbref.getBuffer());
257245
if (magic == file_magic::tapi_file) {
@@ -263,7 +251,6 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
263251
}
264252
file =
265253
make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
266-
realfile = file;
267254

268255
// parseReexports() can recursively call loadDylib(). That's fine since
269256
// we wrote the DylibFile we just loaded to the loadDylib cache via the
@@ -279,7 +266,6 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
279266
magic == file_magic::macho_executable ||
280267
magic == file_magic::macho_bundle);
281268
file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
282-
realfile = file;
283269

284270
// parseLoadCommands() can also recursively call loadDylib(). See comment
285271
// in previous block for why this means we must copy `file` here.

0 commit comments

Comments
 (0)