Skip to content

Commit a1cec3e

Browse files
committed
Swift: replace assertions and prints in the file library
1 parent f965495 commit a1cec3e

File tree

7 files changed

+70
-45
lines changed

7 files changed

+70
-45
lines changed

swift/extractor/infra/file/BUILD.bazel

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ load("//swift:rules.bzl", "swift_cc_library")
22

33
swift_cc_library(
44
name = "file",
5-
srcs = glob(["*.cpp"]),
6-
hdrs = glob(["*.h"]) + [":path_hash_workaround"],
5+
srcs = glob(["*.cpp", "FsLogger.h"]),
6+
hdrs = glob(["*.h"], exclude=["FsLogger.h"]) + [":path_hash_workaround"],
77
visibility = ["//swift:__subpackages__"],
8+
deps = ["//swift/extractor/infra/log"],
89
)
910

1011
genrule(

swift/extractor/infra/file/FsLogger.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "swift/extractor/infra/log/SwiftLogging.h"
2+
3+
namespace codeql {
4+
namespace fs_logger {
5+
inline Logger& logger() {
6+
static Logger ret{"fs"};
7+
return ret;
8+
}
9+
} // namespace fs_logger
10+
} // namespace codeql

swift/extractor/infra/file/Path.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#include "swift/extractor/infra/file/Path.h"
2+
23
#include <iostream>
34
#include <unistd.h>
45

6+
#include "swift/extractor/infra/file/FsLogger.h"
7+
8+
using namespace std::string_view_literals;
9+
510
namespace codeql {
611

12+
using namespace fs_logger;
13+
714
static bool shouldCanonicalize() {
8-
auto preserve = getenv("CODEQL_PRESERVE_SYMLINKS");
9-
if (preserve && std::string(preserve) == "true") {
10-
return false;
11-
}
12-
preserve = getenv("SEMMLE_PRESERVE_SYMLINKS");
13-
if (preserve && std::string(preserve) == "true") {
14-
return false;
15+
for (auto var : {"CODEQL_PRESERVE_SYMLINKS", "SEMMLE_PRESERVE_SYMLINKS"}) {
16+
if (auto preserve = getenv(var); preserve && preserve == "true"sv) {
17+
return false;
18+
}
1519
}
1620
return true;
1721
}
@@ -26,8 +30,13 @@ std::filesystem::path resolvePath(const std::filesystem::path& path) {
2630
ret = std::filesystem::absolute(path, ec);
2731
}
2832
if (ec) {
29-
std::cerr << "Cannot get " << (canonicalize ? "canonical" : "absolute") << " path: " << path
30-
<< ": " << ec.message() << "\n";
33+
if (ec.value() == ENOENT) {
34+
// this is pretty normal, nothing to spam about
35+
LOG_DEBUG("resolving non-existing {}", path);
36+
} else {
37+
LOG_WARNING("cannot get {} path for {} ({})", canonicalize ? "canonical" : "absolute", path,
38+
ec);
39+
}
3140
return path;
3241
}
3342
return ret;
Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "swift/extractor/infra/file/TargetFile.h"
2+
#include "swift/extractor/infra/file/FsLogger.h"
3+
#include "swift/extractor/infra/log/SwiftLogging.h"
4+
#include "swift/extractor/infra/log/SwiftAssert.h"
25

3-
#include <iostream>
46
#include <cassert>
57
#include <cstdio>
68
#include <cerrno>
@@ -10,32 +12,24 @@
1012
namespace fs = std::filesystem;
1113

1214
namespace codeql {
13-
namespace {
14-
[[noreturn]] void error(const char* action, const fs::path& arg, std::error_code ec) {
15-
std::cerr << "Unable to " << action << ": " << arg << " (" << ec.message() << ")\n";
16-
std::abort();
17-
}
1815

19-
[[noreturn]] void error(const char* action, const fs::path& arg) {
20-
error(action, arg, {errno, std::system_category()});
21-
}
16+
using namespace fs_logger;
2217

23-
void check(const char* action, const fs::path& arg, std::error_code ec) {
24-
if (ec) {
25-
error(action, arg, ec);
26-
}
18+
namespace {
19+
std::error_code currentErrorCode() {
20+
return {errno, std::system_category()};
2721
}
2822

2923
void ensureParentDir(const fs::path& path) {
3024
auto parent = path.parent_path();
3125
std::error_code ec;
3226
fs::create_directories(parent, ec);
33-
check("create directory", parent, ec);
27+
CODEQL_ASSERT(!ec, "Unable to create directory {} ({})", parent, ec);
3428
}
3529

3630
fs::path initPath(const std::filesystem::path& target, const std::filesystem::path& dir) {
3731
fs::path ret{dir};
38-
assert(!target.empty() && "target must be a non-empty path");
32+
CODEQL_ASSERT(!target.empty());
3933
ret /= target.relative_path();
4034
ensureParentDir(ret);
4135
return ret;
@@ -53,13 +47,12 @@ bool TargetFile::init() {
5347
if (auto f = std::fopen(targetPath.c_str(), "wx")) {
5448
std::fclose(f);
5549
out.open(workingPath);
56-
checkOutput("open file for writing");
50+
checkOutput("open");
5751
return true;
5852
}
59-
if (errno != EEXIST) {
60-
error("open file for writing", targetPath);
61-
}
62-
// else we just lost the race
53+
CODEQL_ASSERT(errno == EEXIST, "Unable to open {} for writing ({})", targetPath,
54+
currentErrorCode());
55+
// else the file already exists and we just lost the race
6356
return false;
6457
}
6558

@@ -76,13 +69,11 @@ void TargetFile::commit() {
7669
out.close();
7770
std::error_code ec;
7871
fs::rename(workingPath, targetPath, ec);
79-
check("rename file", targetPath, ec);
72+
CODEQL_ASSERT(!ec, "Unable to rename {} -> {} ({})", workingPath, targetPath, ec);
8073
}
8174
}
8275

8376
void TargetFile::checkOutput(const char* action) {
84-
if (!out) {
85-
error(action, workingPath);
86-
}
77+
CODEQL_ASSERT(out, "Unable to {} {} ({})", action, workingPath, currentErrorCode());
8778
}
8879
} // namespace codeql

swift/extractor/infra/file/TargetFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TargetFile {
3232
TargetFile& operator<<(T&& value) {
3333
errno = 0;
3434
out << value;
35-
checkOutput("write to file");
35+
checkOutput("write to");
3636
return *this;
3737
}
3838

swift/extractor/infra/log/SwiftLogging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <binlog/TextOutputStream.hpp>
1010
#include <binlog/EventFilter.hpp>
1111
#include <binlog/adapt_stdfilesystem.hpp>
12+
#include <binlog/adapt_stderrorcode.hpp>
1213
#include <binlog/adapt_stdoptional.hpp>
1314
#include <binlog/adapt_stdvariant.hpp>
1415

swift/extractor/remapping/SwiftFileInterception.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "swift/extractor/infra/file/PathHash.h"
1616
#include "swift/extractor/infra/file/Path.h"
17+
#include "swift/extractor/infra/log/SwiftAssert.h"
1718

1819
#ifdef __APPLE__
1920
// path is hardcoded as otherwise redirection could break when setting DYLD_FALLBACK_LIBRARY_PATH
@@ -27,14 +28,22 @@
2728
namespace fs = std::filesystem;
2829

2930
namespace {
31+
32+
namespace {
33+
codeql::Logger& logger() {
34+
static codeql::Logger ret{"open_interception"};
35+
return ret;
36+
}
37+
} // namespace
38+
3039
namespace original {
3140

3241
void* openLibC() {
3342
if (auto ret = dlopen(SHARED_LIBC, RTLD_LAZY)) {
3443
return ret;
3544
}
36-
std::cerr << "Unable to dlopen " SHARED_LIBC "!\n";
37-
std::abort();
45+
LOG_CRITICAL("Unable to dlopen " SHARED_LIBC "!");
46+
abort();
3847
}
3948

4049
void* libc() {
@@ -71,8 +80,12 @@ bool mayBeRedirected(const char* path, int flags = O_RDONLY) {
7180
std::optional<std::string> hashFile(const fs::path& path) {
7281
auto fd = original::open(path.c_str(), O_RDONLY | O_CLOEXEC);
7382
if (fd < 0) {
74-
auto ec = std::make_error_code(static_cast<std::errc>(errno));
75-
std::cerr << "unable to open " << path << " for reading (" << ec.message() << ")\n";
83+
if (errno == ENOENT) {
84+
LOG_DEBUG("ignoring non-existing module {}", path);
85+
} else {
86+
LOG_ERROR("unable to open {} for hashing ({})", path,
87+
std::make_error_code(static_cast<std::errc>(errno)));
88+
}
7689
return std::nullopt;
7790
}
7891
auto hasher = picosha2::hash256_one_by_one();
@@ -102,7 +115,7 @@ class FileInterceptor {
102115

103116
int open(const char* path, int flags, mode_t mode = 0) const {
104117
fs::path fsPath{path};
105-
assert((flags & O_ACCMODE) == O_RDONLY);
118+
CODEQL_ASSERT((flags & O_ACCMODE) == O_RDONLY, "We should only be intercepting file reads");
106119
// try to use the hash map first
107120
errno = 0;
108121
if (auto hashed = hashPath(path)) {
@@ -114,15 +127,15 @@ class FileInterceptor {
114127
}
115128

116129
fs::path redirect(const fs::path& target) const {
117-
assert(mayBeRedirected(target.c_str()));
130+
CODEQL_ASSERT(mayBeRedirected(target.c_str()), "Trying to redirect {} which is unsupported",
131+
target);
118132
auto redirected = redirectedPath(target);
119133
fs::create_directories(redirected.parent_path());
120134
if (auto hashed = hashPath(target)) {
121135
std::error_code ec;
122136
fs::create_symlink(*hashed, redirected, ec);
123-
if (ec) {
124-
std::cerr << "Cannot remap file " << *hashed << " -> " << redirected << ": " << ec.message()
125-
<< "\n";
137+
if (ec && ec.value() != ENOENT) {
138+
LOG_WARNING("Cannot remap file {} -> {} ({})", *hashed, redirected, ec);
126139
}
127140
return *hashed;
128141
}

0 commit comments

Comments
 (0)