mirror of
https://github.com/github/codeql.git
synced 2026-04-27 17:55:19 +02:00
Swift: use std::filesystem and picoSHA2
This replaces usages of `llvm::fs` and string manipulation with `std::filesystem`, also replacing `std::string` with `std::filesystem::path` where it made sense. Moreover MD5 hashing used in macOS file remapping was replaced by SHA256 hashing using a small header-only SHA256 C++ library with an MIT license, https://github.com/okdshin/PicoSHA2. File contents hashing was relocated to the newly created `file` library for later planned reuse.
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
#include "swift/extractor/remapping/SwiftOpenInterception.h"
|
||||
|
||||
#include <fishhook.h>
|
||||
#include <llvm/Support/raw_ostream.h>
|
||||
#include <llvm/Support/FileSystem.h>
|
||||
#include <llvm/Support/Path.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <filesystem>
|
||||
|
||||
#include "swift/extractor/infra/file/FileHash.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace codeql {
|
||||
|
||||
static std::string scratchDir;
|
||||
static fs::path scratchDir;
|
||||
static bool interceptionEnabled = false;
|
||||
|
||||
static int (*original_open)(const char*, int, ...) = nullptr;
|
||||
|
||||
static std::string fileHash(const std::string& filename) {
|
||||
static std::string originalHashFile(const fs::path& filename) {
|
||||
int fd = original_open(filename.c_str(), O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return {};
|
||||
}
|
||||
auto maybeMD5 = llvm::sys::fs::md5_contents(fd);
|
||||
close(fd);
|
||||
if (!maybeMD5) {
|
||||
return {};
|
||||
}
|
||||
return maybeMD5->digest().str().str();
|
||||
|
||||
return hashFile(fd);
|
||||
}
|
||||
|
||||
static int codeql_open(const char* path, int oflag, ...) {
|
||||
@@ -36,14 +36,14 @@ static int codeql_open(const char* path, int oflag, ...) {
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
std::string newPath(path);
|
||||
fs::path newPath(path);
|
||||
|
||||
if (interceptionEnabled && llvm::sys::fs::exists(newPath)) {
|
||||
if (interceptionEnabled && fs::exists(newPath)) {
|
||||
// TODO: check file magic instead
|
||||
if (llvm::StringRef(newPath).endswith(".swiftmodule")) {
|
||||
auto hash = fileHash(newPath);
|
||||
auto hashed = scratchDir + "/" + hash;
|
||||
if (!hash.empty() && llvm::sys::fs::exists(hashed)) {
|
||||
if (newPath.extension() == ".swiftmodule") {
|
||||
auto hash = originalHashFile(newPath);
|
||||
auto hashed = scratchDir / hash;
|
||||
if (!hash.empty() && fs::exists(hashed)) {
|
||||
newPath = hashed;
|
||||
}
|
||||
}
|
||||
@@ -52,25 +52,28 @@ static int codeql_open(const char* path, int oflag, ...) {
|
||||
return original_open(newPath.c_str(), oflag, mode);
|
||||
}
|
||||
|
||||
void finalizeRemapping(const std::unordered_map<std::string, std::string>& mapping) {
|
||||
void finalizeRemapping(
|
||||
const std::unordered_map<std::filesystem::path, std::filesystem::path>& mapping) {
|
||||
for (auto& [original, patched] : mapping) {
|
||||
// TODO: Check file magic instead
|
||||
if (!llvm::StringRef(original).endswith(".swiftmodule")) {
|
||||
if (original.extension() != ".swiftmodule") {
|
||||
continue;
|
||||
}
|
||||
auto hash = fileHash(original);
|
||||
auto hashed = scratchDir + "/" + hash;
|
||||
if (!hash.empty() && llvm::sys::fs::exists(patched)) {
|
||||
if (std::error_code ec = llvm::sys::fs::create_link(/* from */ patched, /* to */ hashed)) {
|
||||
llvm::errs() << "Cannot remap file '" << patched << "' -> '" << hashed
|
||||
<< "': " << ec.message() << "\n";
|
||||
auto hash = originalHashFile(original);
|
||||
auto hashed = scratchDir / hash;
|
||||
if (!hash.empty() && fs::exists(hashed)) {
|
||||
std::error_code ec;
|
||||
fs::create_symlink(/* target */ patched, /* symlink */ hashed, ec);
|
||||
if (ec) {
|
||||
std::cerr << "Cannot remap file '" << patched << "' -> '" << hashed << "': " << ec.message()
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
interceptionEnabled = false;
|
||||
}
|
||||
|
||||
void initRemapping(const std::string& dir) {
|
||||
void initRemapping(const std::filesystem::path& dir) {
|
||||
scratchDir = dir;
|
||||
|
||||
struct rebinding binding[] = {
|
||||
|
||||
Reference in New Issue
Block a user