Author: Jan Svoboda Date: 2026-03-02T11:59:17-08:00 New Revision: ed524ba0d4582a5017831394913093ae0a8ac15b
URL: https://github.com/llvm/llvm-project/commit/ed524ba0d4582a5017831394913093ae0a8ac15b DIFF: https://github.com/llvm/llvm-project/commit/ed524ba0d4582a5017831394913093ae0a8ac15b.diff LOG: [llvm] Avoid resolving `.incbin` during symbol collection (#172920) With IO sandboxing enabled, Clang requires all FS accesses happen through the one "true" VFS instance. That instance is currently not being propagated into the assembly parser, and doing so would be super involved. This triggers sandbox violations whenever an `.incbin` directive is encountered, since the parser needs the VFS to open the file. However, it seems that `asm()` directives are only parsed to get the symbols of an LLVM module, which cannot be affected by `.incbin` directives. This PR adds an option to the asm parser to avoid resolving `.incbin` directives when collecting module symbols, avoiding the sandbox violation. Added: clang/test/CodeGen/asm_incbin.c Modified: llvm/include/llvm/MC/MCParser/MCAsmParser.h llvm/lib/MC/MCParser/AsmParser.cpp llvm/lib/Object/ModuleSymbolTable.cpp Removed: ################################################################################ diff --git a/clang/test/CodeGen/asm_incbin.c b/clang/test/CodeGen/asm_incbin.c new file mode 100644 index 0000000000000..9ad447a1dfc98 --- /dev/null +++ b/clang/test/CodeGen/asm_incbin.c @@ -0,0 +1,8 @@ +// RUN: split-file %s %t +//--- foo.h +//--- tu.c +asm(".incbin \"foo.h\""); +// RUN: cd %t +// RUN: %clang -c -emit-llvm %t/tu.c -o %t/tu.ll +// RUN: llvm-dis %t/tu.ll -o - | FileCheck %s +// CHECK: module asm ".incbin \22foo.h\22" diff --git a/llvm/include/llvm/MC/MCParser/MCAsmParser.h b/llvm/include/llvm/MC/MCParser/MCAsmParser.h index 5d74b76592df9..da5ef08381aed 100644 --- a/llvm/include/llvm/MC/MCParser/MCAsmParser.h +++ b/llvm/include/llvm/MC/MCParser/MCAsmParser.h @@ -151,6 +151,11 @@ class LLVM_ABI MCAsmParser { bool ShowParsedOperands = false; + /// Flag tracking whether we're only interested in symbols, which allows us to + /// avoid some work (e.g. resolving .incbin directives). + // TODO: Adopt this in more places. + bool SymbolScanningMode = false; + public: MCAsmParser(const MCAsmParser &) = delete; MCAsmParser &operator=(const MCAsmParser &) = delete; @@ -176,6 +181,8 @@ class LLVM_ABI MCAsmParser { bool getShowParsedOperands() const { return ShowParsedOperands; } void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; } + void setSymbolScanningMode(bool Value) { SymbolScanningMode = Value; } + /// Run the parser on the input source buffer. virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0; diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 3452708bcec8a..dce0adaaf7805 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -855,6 +855,10 @@ bool AsmParser::enterIncludeFile(const std::string &Filename) { /// returns true on failure. bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, const MCExpr *Count, SMLoc Loc) { + // The .incbin file cannot introduce new symbols. + if (SymbolScanningMode) + return false; + std::string IncludedFile; unsigned NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); diff --git a/llvm/lib/Object/ModuleSymbolTable.cpp b/llvm/lib/Object/ModuleSymbolTable.cpp index 9442becdb7d33..ff0bc8b192de7 100644 --- a/llvm/lib/Object/ModuleSymbolTable.cpp +++ b/llvm/lib/Object/ModuleSymbolTable.cpp @@ -129,6 +129,8 @@ initializeRecordStreamer(const Module &M, // AsmPrinter::doInitialization()). Parser->setAssemblerDialect(InlineAsm::AD_ATT); + Parser->setSymbolScanningMode(true); + Parser->setTargetParser(*TAP); if (Parser->Run(false)) return; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
