Skip to content

Commit fd888d0

Browse files
[TOOLS] Don't crash if swift-demangle can't be called
1 parent a9a4934 commit fd888d0

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

tools/symbol_extractor/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ all: $(BINS)
55

66
%: %.cpp
77
echo "[G++] $@"
8-
g++ -std=c++20 -o $@ $< -lc++abi -lsqlite3
8+
g++ -std=c++20 -ggdb -o $@ $< -lc++abi -lsqlite3
99

1010
clean:
1111
echo "[CLEAN] $(BINS)"

tools/symbol_extractor/symbol_extractor.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ std::string read_string(FILE* fp) {
146146
return result;
147147
}
148148
std::string demangle_swift_symbol(const std::string& mangled) {
149+
// std::cout << "\t\tTrying to demangle: '" << mangled << "'" << std::endl;
149150
static int status = 0;
150151
if (status == 0) {
151152
waitpid(swift_demangler_pid, &status, WNOHANG);
@@ -156,20 +157,27 @@ std::string demangle_swift_symbol(const std::string& mangled) {
156157
}
157158
if (swift_demangler_stdin) {
158159
fclose(swift_demangler_stdin);
160+
swift_demangler_stdin = nullptr;
159161
}
160162
if (swift_demangler_stdout) {
161163
fclose(swift_demangler_stdout);
164+
swift_demangler_stdout = nullptr;
162165
}
163166
if (WIFEXITED(status)) {
164167
if (WEXITSTATUS(status) != 0) {
165168
std::cerr << "swift-demangle exited with code " << WEXITSTATUS(status) << std::endl;
169+
exit(2);
166170
}
167171
} else {
168172
std::cerr << "swift-demangle terminated abnormally" << std::endl;
173+
exit(2);
169174
}
170175
}
171176
}
172-
if (status != 0 || !swift_demangler_stdin || !swift_demangler_stdout) return mangled;
177+
if (status != 0 || !swift_demangler_stdin || !swift_demangler_stdout) {
178+
// std::cout << "\t\t\t--> Error while demangling..." << std::endl;
179+
return mangled;
180+
}
173181

174182
fputs(mangled.c_str(), swift_demangler_stdin);
175183
fputs("\n", swift_demangler_stdin);
@@ -184,7 +192,9 @@ std::string demangle_swift_symbol(const std::string& mangled) {
184192

185193
//read and return result
186194
std::string result = read_string(swift_demangler_stdout);
187-
return result.empty() ? mangled : result;
195+
std::string retval = result.empty() ? mangled : result;
196+
// std::cout << "\t\t\t--> Got: '" << result << "'" << std::endl;
197+
return retval;
188198
}
189199

190200
// Extract symbols from a Mach-O file
@@ -263,7 +273,7 @@ bool extract_symbols(const std::string& filepath, std::vector<SymbolEntry>& symb
263273
int status = 0;
264274
char* demangled = abi::__cxa_demangle(symNameRaw, nullptr, nullptr, &status);
265275
std::string symName = (status == 0 && demangled) ? demangled : symNameRaw;
266-
//don't try swift demangle, if c++ could already demangle it and only try to demangle stuff that'S marked as swift
276+
//don't try swift demangle, if c++ could already demangle it and only try to demangle stuff that's marked as swift
267277
if ((status != 0 || !demangled) && symName.starts_with("_$s"))
268278
{
269279
std::string newSymName = demangle_swift_symbol(symName);
@@ -514,19 +524,19 @@ int main(int argc, char* argv[]) {
514524
}
515525

516526
// Regex to parse directory names like: iPhone14,3 18.5 (22F76)
517-
std::regex dirRegex(R"(^(?:|.* )([0-9]+(?:\.[0-9]+)*) \(([^)]+)\)(?: (arm64e?))?$)");
527+
std::regex dirRegex(R"(^(?:|.* )([0-9]+(?:\.[0-9]+)*) \(([^)]+)\)(?: (arm64|arm64e|x86|x86_64))?$)");
518528

519529
if (swiftDemanglePath != "") {
520530
int master_fd = 0;
521-
swift_demangler_pid = forkpty(&master_fd, NULL, NULL, NULL);;
531+
swift_demangler_pid = forkpty(&master_fd, NULL, NULL, NULL);
522532
if (swift_demangler_pid != -1) {
523533
if (swift_demangler_pid == 0) {
524534
char* const envp[] = {
525535
(char*)"LD_LIBRARY_PATH=./",
526536
nullptr
527537
};
528538
execle(swiftDemanglePath.c_str(), "swift-demangle", (char*)nullptr, envp);
529-
perror("execl");
539+
perror("execle failed");
530540
exit(1);
531541
}
532542
else
@@ -538,7 +548,7 @@ int main(int argc, char* argv[]) {
538548
}
539549
}
540550
else
541-
perror("Error forking");
551+
perror("Error forking to swift-demangle");
542552
}
543553

544554
std::vector<std::tuple<std::string, std::string, std::string, std::vector<FileEntry>>> all_data;
@@ -547,37 +557,37 @@ int main(int argc, char* argv[]) {
547557
std::string name = entry->d_name;
548558
if (name == "." || name == ".." || name[0] == '.')
549559
continue;
550-
560+
551561
std::string fullPath = parentDir + "/" + name;
552-
562+
553563
struct stat st;
554564
if (stat(fullPath.c_str(), &st) != 0) {
555565
std::perror(("stat " + fullPath).c_str());
556566
continue;
557567
}
558-
568+
559569
if (!S_ISDIR(st.st_mode))
560570
continue;
561-
571+
562572
// Parse version and build
563573
std::string version, build, arch;
564574
if (!parse_dirname(name, version, build, arch, dirRegex)) {
565575
std::cerr << "Skipping directory (no version/build match): " << name << std::endl;
566576
continue;
567577
}
568-
578+
569579
std::cout << "Scanning directory: " << name << " (version: " << version << ", build: " << build << ", architecture: " << arch << ")" << std::endl;
570-
580+
571581
// Symbols subdirectory path
572582
std::string symbolsDir = fullPath + "/Symbols";
573-
583+
574584
// Check if Symbols directory exists
575585
struct stat stSymbols;
576586
if (stat(symbolsDir.c_str(), &stSymbols) != 0 || !S_ISDIR(stSymbols.st_mode)) {
577587
std::cerr << "Symbols directory missing or invalid: " << symbolsDir << std::endl;
578588
continue;
579589
}
580-
590+
581591
// Collect files with symbols
582592
std::vector<FileEntry> files;
583593
scan_dir_recursive(fullPath, "Symbols", files);
@@ -589,9 +599,11 @@ int main(int argc, char* argv[]) {
589599
if (swift_demangler_stdin) {
590600
fwrite("\n", 1, 1, swift_demangler_stdin); //unblock waiting child
591601
fclose(swift_demangler_stdin);
602+
swift_demangler_stdin = nullptr;
592603
}
593604
if (swift_demangler_stdout) {
594605
fclose(swift_demangler_stdout);
606+
swift_demangler_stdout = nullptr;
595607
}
596608
int status = 0;
597609
waitpid(swift_demangler_pid, &status, 0);

0 commit comments

Comments
 (0)