mirror of
https://github.com/github/codeql.git
synced 2026-05-01 11:45:14 +02:00
Swift: replace assertions and prints in the file library
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "swift/extractor/infra/file/PathHash.h"
|
||||
#include "swift/extractor/infra/file/Path.h"
|
||||
#include "swift/extractor/infra/log/SwiftAssert.h"
|
||||
|
||||
#ifdef __APPLE__
|
||||
// path is hardcoded as otherwise redirection could break when setting DYLD_FALLBACK_LIBRARY_PATH
|
||||
@@ -27,14 +28,22 @@
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
namespace {
|
||||
codeql::Logger& logger() {
|
||||
static codeql::Logger ret{"open_interception"};
|
||||
return ret;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace original {
|
||||
|
||||
void* openLibC() {
|
||||
if (auto ret = dlopen(SHARED_LIBC, RTLD_LAZY)) {
|
||||
return ret;
|
||||
}
|
||||
std::cerr << "Unable to dlopen " SHARED_LIBC "!\n";
|
||||
std::abort();
|
||||
LOG_CRITICAL("Unable to dlopen " SHARED_LIBC "!");
|
||||
abort();
|
||||
}
|
||||
|
||||
void* libc() {
|
||||
@@ -71,8 +80,12 @@ bool mayBeRedirected(const char* path, int flags = O_RDONLY) {
|
||||
std::optional<std::string> hashFile(const fs::path& path) {
|
||||
auto fd = original::open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
auto ec = std::make_error_code(static_cast<std::errc>(errno));
|
||||
std::cerr << "unable to open " << path << " for reading (" << ec.message() << ")\n";
|
||||
if (errno == ENOENT) {
|
||||
LOG_DEBUG("ignoring non-existing module {}", path);
|
||||
} else {
|
||||
LOG_ERROR("unable to open {} for hashing ({})", path,
|
||||
std::make_error_code(static_cast<std::errc>(errno)));
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
auto hasher = picosha2::hash256_one_by_one();
|
||||
@@ -102,7 +115,7 @@ class FileInterceptor {
|
||||
|
||||
int open(const char* path, int flags, mode_t mode = 0) const {
|
||||
fs::path fsPath{path};
|
||||
assert((flags & O_ACCMODE) == O_RDONLY);
|
||||
CODEQL_ASSERT((flags & O_ACCMODE) == O_RDONLY, "We should only be intercepting file reads");
|
||||
// try to use the hash map first
|
||||
errno = 0;
|
||||
if (auto hashed = hashPath(path)) {
|
||||
@@ -114,15 +127,15 @@ class FileInterceptor {
|
||||
}
|
||||
|
||||
fs::path redirect(const fs::path& target) const {
|
||||
assert(mayBeRedirected(target.c_str()));
|
||||
CODEQL_ASSERT(mayBeRedirected(target.c_str()), "Trying to redirect {} which is unsupported",
|
||||
target);
|
||||
auto redirected = redirectedPath(target);
|
||||
fs::create_directories(redirected.parent_path());
|
||||
if (auto hashed = hashPath(target)) {
|
||||
std::error_code ec;
|
||||
fs::create_symlink(*hashed, redirected, ec);
|
||||
if (ec) {
|
||||
std::cerr << "Cannot remap file " << *hashed << " -> " << redirected << ": " << ec.message()
|
||||
<< "\n";
|
||||
if (ec && ec.value() != ENOENT) {
|
||||
LOG_WARNING("Cannot remap file {} -> {} ({})", *hashed, redirected, ec);
|
||||
}
|
||||
return *hashed;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user