diff options
author | Tim Neumann <timnn@google.com> | 2024-04-02 19:59:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-02 10:59:29 -0700 |
commit | f792f14b01605453c7c0c17f3b4564335c0d9d14 (patch) | |
tree | ae3db8d7ca006a1432b16959ce8ab0e6db43db4c /llvm/lib/Target | |
parent | 9c9f94063ce1bd819ba4482f537d1e580dd31eef (diff) | |
download | llvm-f792f14b01605453c7c0c17f3b4564335c0d9d14.zip llvm-f792f14b01605453c7c0c17f3b4564335c0d9d14.tar.gz llvm-f792f14b01605453c7c0c17f3b4564335c0d9d14.tar.bz2 |
[WebAssembly] Allocate MCSymbolWasm data on MCContext (#85866)
Fixes #85578, a use-after-free caused by some `MCSymbolWasm` data being
freed too early.
Previously, `WebAssemblyAsmParser` owned the data that is moved to
`MCContext` by this PR, which caused problems when handling module ASM,
because the ASM parser was destroyed after parsing the module ASM, but
the symbols persisted.
The added test passes locally with an LLVM build with AddressSanitizer
enabled.
Implementation notes:
* I've called the added method
<code>allocate<b><i>Generic</i></b>String</code> and added the second
paragraph of its documentation to maybe guide people a bit on when to
use this method (based on my (limited) understanding of the `MCContext`
class). We could also just call it `allocateString` and remove that
second paragraph.
* The added `createWasmSignature` method does not support taking the
return and parameter types as arguments: Specifying them afterwards is
barely any longer and prevents them from being accidentally specified in
the wrong order.
* This removes a _"TODO: Do the uniquing of Signatures here instead of
ObjectFileWriter?"_ since the field it's attached to is also removed.
Let me know if you think that TODO should be preserved somewhere.
Diffstat (limited to 'llvm/lib/Target')
6 files changed, 39 insertions, 74 deletions
diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp index 020c0d6..8e20631 100644 --- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -196,10 +196,6 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser { MCAsmParser &Parser; MCAsmLexer &Lexer; - // Much like WebAssemblyAsmPrinter in the backend, we have to own these. - std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures; - std::vector<std::unique_ptr<std::string>> Names; - // Order of labels, directives and instructions in a .s file have no // syntactical enforcement. This class is a callback from the actual parser, // and yet we have to be feeding data to the streamer in a very particular @@ -287,16 +283,6 @@ public: return Parser.Error(Loc.isValid() ? Loc : Lexer.getTok().getLoc(), Msg); } - void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) { - Signatures.push_back(std::move(Sig)); - } - - StringRef storeName(StringRef Name) { - std::unique_ptr<std::string> N = std::make_unique<std::string>(Name); - Names.push_back(std::move(N)); - return *Names.back(); - } - std::pair<StringRef, StringRef> nestingString(NestingType NT) { switch (NT) { case Function: @@ -640,21 +626,20 @@ public: // represent as a signature, such that we can re-build this signature, // attach it to an anonymous symbol, which is what WasmObjectWriter // expects to be able to recreate the actual unique-ified type indices. + auto &Ctx = getContext(); auto Loc = Parser.getTok(); - auto Signature = std::make_unique<wasm::WasmSignature>(); - if (parseSignature(Signature.get())) + auto Signature = Ctx.createWasmSignature(); + if (parseSignature(Signature)) return true; // Got signature as block type, don't need more - TC.setLastSig(*Signature.get()); + TC.setLastSig(*Signature); if (ExpectBlockType) - NestingStack.back().Sig = *Signature.get(); + NestingStack.back().Sig = *Signature; ExpectBlockType = false; - auto &Ctx = getContext(); // The "true" here will cause this to be a nameless symbol. MCSymbol *Sym = Ctx.createTempSymbol("typeindex", true); auto *WasmSym = cast<MCSymbolWasm>(Sym); - WasmSym->setSignature(Signature.get()); - addSignature(std::move(Signature)); + WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); const MCExpr *Expr = MCSymbolRefExpr::create( WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx); @@ -887,12 +872,11 @@ public: CurrentState = FunctionStart; LastFunctionLabel = WasmSym; } - auto Signature = std::make_unique<wasm::WasmSignature>(); - if (parseSignature(Signature.get())) + auto Signature = Ctx.createWasmSignature(); + if (parseSignature(Signature)) return ParseStatus::Failure; TC.funcDecl(*Signature); - WasmSym->setSignature(Signature.get()); - addSignature(std::move(Signature)); + WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); TOut.emitFunctionType(WasmSym); // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. @@ -909,7 +893,7 @@ public: if (ExportName.empty()) return ParseStatus::Failure; auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); - WasmSym->setExportName(storeName(ExportName)); + WasmSym->setExportName(Ctx.allocateString(ExportName)); TOut.emitExportName(WasmSym, ExportName); return expect(AsmToken::EndOfStatement, "EOL"); } @@ -924,7 +908,7 @@ public: if (ImportModule.empty()) return ParseStatus::Failure; auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); - WasmSym->setImportModule(storeName(ImportModule)); + WasmSym->setImportModule(Ctx.allocateString(ImportModule)); TOut.emitImportModule(WasmSym, ImportModule); return expect(AsmToken::EndOfStatement, "EOL"); } @@ -939,7 +923,7 @@ public: if (ImportName.empty()) return ParseStatus::Failure; auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); - WasmSym->setImportName(storeName(ImportName)); + WasmSym->setImportName(Ctx.allocateString(ImportName)); TOut.emitImportName(WasmSym, ImportName); return expect(AsmToken::EndOfStatement, "EOL"); } @@ -949,11 +933,10 @@ public: if (SymName.empty()) return ParseStatus::Failure; auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); - auto Signature = std::make_unique<wasm::WasmSignature>(); + auto Signature = Ctx.createWasmSignature(); if (parseRegTypeList(Signature->Params)) return ParseStatus::Failure; - WasmSym->setSignature(Signature.get()); - addSignature(std::move(Signature)); + WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG); TOut.emitTagType(WasmSym); // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp index 03897b5..3524abb 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -266,10 +266,10 @@ MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) { WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); WebAssembly::getLibcallSignature(Subtarget, Name, Returns, Params); } - auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns), - std::move(Params)); - WasmSym->setSignature(Signature.get()); - addSignature(std::move(Signature)); + auto Signature = OutContext.createWasmSignature(); + Signature->Returns = std::move(Returns); + Signature->Params = std::move(Params); + WasmSym->setSignature(Signature); return WasmSym; } @@ -338,11 +338,11 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) { // and thus also contain a signature, but we need to get the signature // anyway here in case it is an invoke that has not yet been created. We // will discard it later if it turns out not to be necessary. - auto Signature = signatureFromMVTs(Results, Params); + auto Signature = signatureFromMVTs(OutContext, Results, Params); bool InvokeDetected = false; auto *Sym = getMCSymbolForFunction( &F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj, - Signature.get(), InvokeDetected); + Signature, InvokeDetected); // Multiple functions can be mapped to the same invoke symbol. For // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32' @@ -353,11 +353,7 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) { Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); if (!Sym->getSignature()) { - Sym->setSignature(Signature.get()); - addSignature(std::move(Signature)); - } else { - // This symbol has already been created and had a signature. Discard it. - Signature.reset(); + Sym->setSignature(Signature); } getTargetStreamer()->emitFunctionType(Sym); @@ -365,7 +361,7 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) { if (F.hasFnAttribute("wasm-import-module")) { StringRef Name = F.getFnAttribute("wasm-import-module").getValueAsString(); - Sym->setImportModule(storeName(Name)); + Sym->setImportModule(OutContext.allocateString(Name)); getTargetStreamer()->emitImportModule(Sym, Name); } if (F.hasFnAttribute("wasm-import-name")) { @@ -375,14 +371,14 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) { InvokeDetected ? Sym->getName() : F.getFnAttribute("wasm-import-name").getValueAsString(); - Sym->setImportName(storeName(Name)); + Sym->setImportName(OutContext.allocateString(Name)); getTargetStreamer()->emitImportName(Sym, Name); } if (F.hasFnAttribute("wasm-export-name")) { auto *Sym = cast<MCSymbolWasm>(getSymbol(&F)); StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString(); - Sym->setExportName(storeName(Name)); + Sym->setExportName(OutContext.allocateString(Name)); getTargetStreamer()->emitExportName(Sym, Name); } } @@ -618,10 +614,9 @@ void WebAssemblyAsmPrinter::emitFunctionBodyStart() { SmallVector<MVT, 4> ParamVTs; computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs); - auto Signature = signatureFromMVTs(ResultVTs, ParamVTs); + auto Signature = signatureFromMVTs(OutContext, ResultVTs, ParamVTs); auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym); - WasmSym->setSignature(Signature.get()); - addSignature(std::move(Signature)); + WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); getTargetStreamer()->emitFunctionType(WasmSym); diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h index c30e015..6a544ab 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h @@ -22,17 +22,8 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter { const WebAssemblySubtarget *Subtarget; const MachineRegisterInfo *MRI; WebAssemblyFunctionInfo *MFI; - // TODO: Do the uniquing of Signatures here instead of ObjectFileWriter? - std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures; - std::vector<std::unique_ptr<std::string>> Names; bool signaturesEmitted = false; - StringRef storeName(StringRef Name) { - std::unique_ptr<std::string> N = std::make_unique<std::string>(Name); - Names.push_back(std::move(N)); - return *Names.back(); - } - public: explicit WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) @@ -44,9 +35,6 @@ public: } const WebAssemblySubtarget &getSubtarget() const { return *Subtarget; } - void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) { - Signatures.push_back(std::move(Sig)); - } //===------------------------------------------------------------------===// // MachineFunctionPass Implementation. diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp index f6e24f7..431dc7f 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -73,14 +73,13 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { SmallVector<MVT, 4> ParamMVTs; const auto *const F = dyn_cast<Function>(Global); computeSignatureVTs(FuncTy, F, CurrentFunc, TM, ParamMVTs, ResultMVTs); - auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs); + auto Signature = signatureFromMVTs(Ctx, ResultMVTs, ParamMVTs); bool InvokeDetected = false; auto *WasmSym = Printer.getMCSymbolForFunction( F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj, - Signature.get(), InvokeDetected); - WasmSym->setSignature(Signature.get()); - Printer.addSignature(std::move(Signature)); + Signature, InvokeDetected); + WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); return WasmSym; } @@ -142,12 +141,12 @@ MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO, MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand( SmallVectorImpl<wasm::ValType> &&Returns, SmallVectorImpl<wasm::ValType> &&Params) const { - auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns), - std::move(Params)); + auto Signature = Ctx.createWasmSignature(); + Signature->Returns = std::move(Returns); + Signature->Params = std::move(Params); MCSymbol *Sym = Printer.createTempSymbol("typeindex"); auto *WasmSym = cast<MCSymbolWasm>(Sym); - WasmSym->setSignature(Signature.get()); - Printer.addSignature(std::move(Signature)); + WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); const MCExpr *Expr = MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx); diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp index d17394e..6f4e7d8 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp @@ -111,10 +111,10 @@ void llvm::valTypesFromMVTs(ArrayRef<MVT> In, Out.push_back(WebAssembly::toValType(Ty)); } -std::unique_ptr<wasm::WasmSignature> -llvm::signatureFromMVTs(const SmallVectorImpl<MVT> &Results, +wasm::WasmSignature * +llvm::signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl<MVT> &Results, const SmallVectorImpl<MVT> &Params) { - auto Sig = std::make_unique<wasm::WasmSignature>(); + auto Sig = Ctx.createWasmSignature(); valTypesFromMVTs(Results, Sig->Returns); valTypesFromMVTs(Params, Sig->Params); return Sig; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h b/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h index 3705918..6c9824b 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h @@ -171,9 +171,9 @@ void computeSignatureVTs(const FunctionType *Ty, const Function *TargetFunc, void valTypesFromMVTs(ArrayRef<MVT> In, SmallVectorImpl<wasm::ValType> &Out); -std::unique_ptr<wasm::WasmSignature> -signatureFromMVTs(const SmallVectorImpl<MVT> &Results, - const SmallVectorImpl<MVT> &Params); +wasm::WasmSignature *signatureFromMVTs(MCContext &Ctx, + const SmallVectorImpl<MVT> &Results, + const SmallVectorImpl<MVT> &Params); namespace yaml { |