Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions interpreter/cling/lib/Interpreter/DynamicLibraryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/Path.h"

#include <fstream>
#include <system_error>
#include <sys/stat.h>

Expand Down Expand Up @@ -459,12 +460,26 @@ namespace cling {
return false;
}

file_magic Magic;
const std::error_code Error = identify_magic(libFullPath, Magic);
if (exists)
*exists = !Error;
// Do not use the identify_magic overload taking a path: It will open the
// file and then mmap its contents, possibly causing bus errors when another
// process truncates the file while we are trying to read it. Instead just
// read the first 1024 bytes, which should be enough for identify_magic to
// do its work.
// TODO: Fix the code upstream and consider going back to calling the
// convenience function after a future LLVM upgrade.
std::ifstream in(libFullPath.str(), std::ios::binary);
char header[1024] = {0};
in.read(header, sizeof(header));
if (in.fail()) {
if (exists)
*exists = false;
return false;
}

StringRef headerStr(header, in.gcount());
file_magic Magic = identify_magic(headerStr);

bool result = !Error &&
bool result =
#ifdef __APPLE__
(Magic == file_magic::macho_fixed_virtual_memory_shared_lib
|| Magic == file_magic::macho_dynamically_linked_shared_lib
Expand Down