From 02f70cda8a9fb038705e03b6e65625119bdef4e7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 13 Feb 2018 10:54:46 -0500 Subject: [PATCH] zig_llvm.cpp uses new(std::nothrow) This fixes a mismatched malloc/delete because we were allocating with malloc and then llvm was freeing with delete. --- src/zig_llvm.cpp | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 3e92752d9f..97c07ab820 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -43,31 +43,8 @@ #include -#if defined(_MSC_VER) -#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict) -#else -#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__)) -#endif - using namespace llvm; -template -ATTRIBUTE_RETURNS_NOALIAS static inline T * create(Args... args) { - T * ptr = reinterpret_cast(malloc(sizeof(T))); - if (ptr == nullptr) - return nullptr; - new (ptr) T(args...); - return ptr; -} - -template -static inline void destroy(T * ptr) { - if (ptr != nullptr) { - ptr[0].~T(); - } - free(ptr); -} - void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R) { initializeLoopStrengthReducePass(*unwrap(R)); } @@ -116,7 +93,11 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM Module* module = unwrap(module_ref); - PassManagerBuilder *PMBuilder = create(); + PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder(); + if (PMBuilder == nullptr) { + *error_message = strdup("memory allocation failure"); + return true; + } PMBuilder->OptLevel = target_machine->getOptLevel(); PMBuilder->SizeLevel = 0; @@ -150,7 +131,8 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM // Set up the per-function pass manager. legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module); - FPM.add(create(tlii)); + auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii); + FPM.add(tliwp); FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis())); if (assertions_on) { FPM.add(createVerifierPass()); @@ -446,10 +428,9 @@ unsigned ZigLLVMTag_DW_union_type(void) { } ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) { - DIBuilder *di_builder = reinterpret_cast(malloc(sizeof(DIBuilder))); + DIBuilder *di_builder = new(std::nothrow) DIBuilder(*unwrap(module), allow_unresolved); if (di_builder == nullptr) return nullptr; - new (di_builder) DIBuilder(*unwrap(module), allow_unresolved); return reinterpret_cast(di_builder); }