From 4d0de113a6372a03fe25b2fb196db165ba6a2bf7 Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Wed, 20 Mar 2024 09:44:36 +0000 Subject: [PATCH 1/3] [libclc] Fix up improper use of ExitOnError ExitOnError takes a llvm::Expected, but we were passing it the result of llvm::parseIR - std::unique_ptr - which, even when null, is not an error condition. Thus invalid IR input was silently being accepted until it would segfault on accessing the module. --- libclc/utils/libclc-remangler/LibclcRemangler.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libclc/utils/libclc-remangler/LibclcRemangler.cpp b/libclc/utils/libclc-remangler/LibclcRemangler.cpp index c9e92730c3e31..66f9493195c5f 100644 --- a/libclc/utils/libclc-remangler/LibclcRemangler.cpp +++ b/libclc/utils/libclc-remangler/LibclcRemangler.cpp @@ -777,12 +777,17 @@ class LibCLCRemangler : public ASTConsumer { void Initialize(ASTContext &C) override { ASTCtx = &C; - SMDiagnostic Err; std::unique_ptr const Buff = ExitOnErr( errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputIRFilename))); + + SMDiagnostic Err; std::unique_ptr const M = - ExitOnErr(Expected>( - parseIR(Buff.get()->getMemBufferRef(), Err, LLVMCtx))); + parseIR(Buff.get()->getMemBufferRef(), Err, LLVMCtx); + + if (!M) { + Err.print("libclc-remangler", errs()); + exit(1); + } handleModule(M.get()); } From 6a6fbbd2fe3470e97f6d78e7a6684658748857d6 Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Wed, 20 Mar 2024 09:48:45 +0000 Subject: [PATCH 2/3] [libclc] Update deprecated method use in remangler --- libclc/utils/libclc-remangler/LibclcRemangler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libclc/utils/libclc-remangler/LibclcRemangler.cpp b/libclc/utils/libclc-remangler/LibclcRemangler.cpp index 66f9493195c5f..a5f2a82a55365 100644 --- a/libclc/utils/libclc-remangler/LibclcRemangler.cpp +++ b/libclc/utils/libclc-remangler/LibclcRemangler.cpp @@ -845,7 +845,7 @@ class LibCLCRemangler : public ASTConsumer { } bool remangleFunction(Function &Func, llvm::Module *M) { - if (!Func.getName().startswith("_Z")) + if (!Func.getName().starts_with("_Z")) return true; std::string const MangledName = Func.getName().str(); From 968294db6d405aa000781b078295e35a185661e6 Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Wed, 20 Mar 2024 09:53:04 +0000 Subject: [PATCH 3/3] [libclc] Open remangler file system at PWD, not root I don't think there's any reason to open it at root. Opening it at the current working directory is more intuitive for developers using relative paths for input/output files. Previously "--input-ir foo.ll" would try and open "/foo.ll", which depending on the system is likely a permissions error, or a missing file, or even an unintended file. --- libclc/utils/libclc-remangler/LibclcRemangler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libclc/utils/libclc-remangler/LibclcRemangler.cpp b/libclc/utils/libclc-remangler/LibclcRemangler.cpp index a5f2a82a55365..cdc0540221d31 100644 --- a/libclc/utils/libclc-remangler/LibclcRemangler.cpp +++ b/libclc/utils/libclc-remangler/LibclcRemangler.cpp @@ -963,7 +963,7 @@ int main(int argc, const char **argv) { // Use a default Compilation DB instead of the build one, as it might contain // toolchain specific options, not compatible with clang. - FixedCompilationDatabase Compilations("/", std::vector()); + FixedCompilationDatabase Compilations(".", std::vector()); ClangTool Tool(Compilations, ExpectedParser->getSourcePathList()); LibCLCRemanglerActionFactory LRAF{};