authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-13 10:54:46-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-13 10:54:46-05:00
log02f70cda8a9fb038705e03b6e65625119bdef4e7
treef604b9c3d64b807298eaa36b42d38cc12194c076
parent2dcff95bd2fb8f377491ac48b0ecf961183abcd7

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.

1 files changed, 8 insertions(+), 27 deletions(-)

src/zig_llvm.cpp+8-27
...@@ -43,31 +43,8 @@...@@ -43,31 +43,8 @@
4343
44#include <stdlib.h>44#include <stdlib.h>
4545
46#if defined(_MSC_VER)
47#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
48#else
49#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
50#endif
51
52using namespace llvm;46using namespace llvm;
5347
54template<typename T, typename... Args>
55ATTRIBUTE_RETURNS_NOALIAS static inline T * create(Args... args) {
56 T * ptr = reinterpret_cast<T*>(malloc(sizeof(T)));
57 if (ptr == nullptr)
58 return nullptr;
59 new (ptr) T(args...);
60 return ptr;
61}
62
63template<typename T>
64static inline void destroy(T * ptr) {
65 if (ptr != nullptr) {
66 ptr[0].~T();
67 }
68 free(ptr);
69}
70
71void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R) {48void ZigLLVMInitializeLoopStrengthReducePass(LLVMPassRegistryRef R) {
72 initializeLoopStrengthReducePass(*unwrap(R));49 initializeLoopStrengthReducePass(*unwrap(R));
73}50}
...@@ -116,7 +93,11 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM...@@ -116,7 +93,11 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
11693
117 Module* module = unwrap(module_ref);94 Module* module = unwrap(module_ref);
11895
119 PassManagerBuilder *PMBuilder = create<PassManagerBuilder>();96 PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder();
97 if (PMBuilder == nullptr) {
98 *error_message = strdup("memory allocation failure");
99 return true;
100 }
120 PMBuilder->OptLevel = target_machine->getOptLevel();101 PMBuilder->OptLevel = target_machine->getOptLevel();
121 PMBuilder->SizeLevel = 0;102 PMBuilder->SizeLevel = 0;
122103
...@@ -150,7 +131,8 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM...@@ -150,7 +131,8 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
150131
151 // Set up the per-function pass manager.132 // Set up the per-function pass manager.
152 legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module);133 legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module);
153 FPM.add(create<TargetLibraryInfoWrapperPass>(tlii));134 auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii);
135 FPM.add(tliwp);
154 FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis()));136 FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis()));
155 if (assertions_on) {137 if (assertions_on) {
156 FPM.add(createVerifierPass());138 FPM.add(createVerifierPass());
...@@ -446,10 +428,9 @@ unsigned ZigLLVMTag_DW_union_type(void) {...@@ -446,10 +428,9 @@ unsigned ZigLLVMTag_DW_union_type(void) {
446}428}
447429
448ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {430ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {
449 DIBuilder *di_builder = reinterpret_cast<DIBuilder*>(malloc(sizeof(DIBuilder)));431 DIBuilder *di_builder = new(std::nothrow) DIBuilder(*unwrap(module), allow_unresolved);
450 if (di_builder == nullptr)432 if (di_builder == nullptr)
451 return nullptr;433 return nullptr;
452 new (di_builder) DIBuilder(*unwrap(module), allow_unresolved);
453 return reinterpret_cast<ZigLLVMDIBuilder *>(di_builder);434 return reinterpret_cast<ZigLLVMDIBuilder *>(di_builder);
454}435}
455436