Skip to content

Commit 3c12644

Browse files
committed
Swift: add a guard around hashing to aboid use-after-destructor
1 parent d6d8480 commit 3c12644

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

swift/extractor/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int main(int argc, char** argv) {
5252
configuration.sourceArchiveDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_SOURCE_ARCHIVE_DIR", ".");
5353
configuration.scratchDir = getenv_or("CODEQL_EXTRACTOR_SWIFT_SCRATCH_DIR", ".");
5454

55-
codeql::initInterception(configuration.getTempArtifactDir());
55+
codeql::initRemapping(configuration.getTempArtifactDir());
5656

5757
configuration.frontendOptions.reserve(argc - 1);
5858
for (int i = 1; i < argc; i++) {
@@ -73,7 +73,7 @@ int main(int argc, char** argv) {
7373
Observer observer(configuration);
7474
int frontend_rc = swift::performFrontend(args, "swift-extractor", (void*)main, &observer);
7575

76-
codeql::remapArtifacts(remapping);
76+
codeql::finalizeRemapping(remapping);
7777

7878
return frontend_rc;
7979
}

swift/extractor/remapping/SwiftOpenInterception.Linux.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace codeql {
44
// TBD
5-
void remapArtifacts(const std::unordered_map<std::string, std::string>& mapping) {}
6-
void initInterception(const std::string& dir) {}
5+
void initRemapping(const std::string& dir) {}
6+
void finalizeRemapping(const std::unordered_map<std::string, std::string>& mapping) {}
77

88
} // namespace codeql

swift/extractor/remapping/SwiftOpenInterception.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace codeql {
77

8-
void initInterception(const std::string& dir);
9-
void remapArtifacts(const std::unordered_map<std::string, std::string>& mapping);
8+
void initRemapping(const std::string& dir);
9+
void finalizeRemapping(const std::unordered_map<std::string, std::string>& mapping);
1010

1111
} // namespace codeql

swift/extractor/remapping/SwiftOpenInterception.macOS.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace codeql {
1010

1111
static std::string scratchDir;
12+
static bool interceptionEnabled = false;
1213

1314
static int (*original_open)(const char*, int, ...) = nullptr;
1415

@@ -37,7 +38,7 @@ static int codeql_open(const char* path, int oflag, ...) {
3738

3839
std::string newPath(path);
3940

40-
if (llvm::sys::fs::exists(newPath)) {
41+
if (interceptionEnabled && llvm::sys::fs::exists(newPath)) {
4142
// TODO: check file magic instead
4243
if (llvm::StringRef(newPath).endswith(".swiftmodule")) {
4344
auto hash = fileHash(newPath);
@@ -51,7 +52,7 @@ static int codeql_open(const char* path, int oflag, ...) {
5152
return original_open(newPath.c_str(), oflag, mode);
5253
}
5354

54-
void remapArtifacts(const std::unordered_map<std::string, std::string>& mapping) {
55+
void finalizeRemapping(const std::unordered_map<std::string, std::string>& mapping) {
5556
for (auto& [original, patched] : mapping) {
5657
// TODO: Check file magic instead
5758
if (!llvm::StringRef(original).endswith(".swiftmodule")) {
@@ -66,14 +67,16 @@ void remapArtifacts(const std::unordered_map<std::string, std::string>& mapping)
6667
}
6768
}
6869
}
70+
interceptionEnabled = false;
6971
}
7072

71-
void initInterception(const std::string& dir) {
73+
void initRemapping(const std::string& dir) {
7274
scratchDir = dir;
7375

7476
struct rebinding binding[] = {
7577
{"open", reinterpret_cast<void*>(codeql_open), reinterpret_cast<void**>(&original_open)}};
7678
rebind_symbols(binding, 1);
79+
interceptionEnabled = true;
7780
}
7881

7982
} // namespace codeql

0 commit comments

Comments
 (0)