Skip to content

Commit

Permalink
zig_llvm.cpp uses new(std::nothrow)
Browse files Browse the repository at this point in the history
This fixes a mismatched malloc/delete because
we were allocating with malloc and then llvm was
freeing with delete.
  • Loading branch information
andrewrk committed Feb 13, 2018
1 parent 2dcff95 commit 02f70cd
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/zig_llvm.cpp
Expand Up @@ -43,31 +43,8 @@

#include <stdlib.h>

#if defined(_MSC_VER)
#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
#else
#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
#endif

using namespace llvm;

template<typename T, typename... Args>
ATTRIBUTE_RETURNS_NOALIAS static inline T * create(Args... args) {
T * ptr = reinterpret_cast<T*>(malloc(sizeof(T)));
if (ptr == nullptr)
return nullptr;
new (ptr) T(args...);
return ptr;
}

template<typename T>
static inline void destroy(T * ptr) {
if (ptr != nullptr) {
ptr[0].~T();
}
free(ptr);
}

void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R) {
initializeLoopStrengthReducePass(*unwrap(R));
}
Expand Down Expand Up @@ -116,7 +93,11 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM

Module* module = unwrap(module_ref);

PassManagerBuilder *PMBuilder = create<PassManagerBuilder>();
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;

Expand Down Expand Up @@ -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<TargetLibraryInfoWrapperPass>(tlii));
auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii);
FPM.add(tliwp);
FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis()));
if (assertions_on) {
FPM.add(createVerifierPass());
Expand Down Expand Up @@ -446,10 +428,9 @@ unsigned ZigLLVMTag_DW_union_type(void) {
}

ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {
DIBuilder *di_builder = reinterpret_cast<DIBuilder*>(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<ZigLLVMDIBuilder *>(di_builder);
}

Expand Down

0 comments on commit 02f70cd

Please sign in to comment.