From f792f14b01605453c7c0c17f3b4564335c0d9d14 Mon Sep 17 00:00:00 2001 From: Tim Neumann Date: Tue, 2 Apr 2024 19:59:29 +0200 Subject: [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 allocateGenericString 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. --- llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.cpp') 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 In, Out.push_back(WebAssembly::toValType(Ty)); } -std::unique_ptr -llvm::signatureFromMVTs(const SmallVectorImpl &Results, +wasm::WasmSignature * +llvm::signatureFromMVTs(MCContext &Ctx, const SmallVectorImpl &Results, const SmallVectorImpl &Params) { - auto Sig = std::make_unique(); + auto Sig = Ctx.createWasmSignature(); valTypesFromMVTs(Results, Sig->Returns); valTypesFromMVTs(Params, Sig->Params); return Sig; -- cgit v1.1