From cbb5499313638d4eb905226d5c107d6a5a18a577 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 27 Oct 2016 18:54:38 +0300 Subject: [PATCH] Verify bitcode before write, minor fixes. (#21) --- .../jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt | 8 ++++++++ .../org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt | 2 +- runtime/src/main/cpp/Memory.cpp | 3 ++- sum.c | 5 ----- sum_func.c | 3 --- 5 files changed, 11 insertions(+), 10 deletions(-) delete mode 100644 sum.c delete mode 100644 sum_func.c diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt index abc77d23af9..b768602a4b4 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt @@ -24,6 +24,14 @@ fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) { module.acceptVoid(RTTIGeneratorVisitor(context)) context.runtime.importRuntime(llvmModule) module.acceptVoid(CodeGeneratorVisitor(context)) + memScoped { + val errorRef = alloc(Int8Box.ref) + // TODO: use LLVMDisposeMessage() on errorRef, once possible in interop. + if (LLVMVerifyModule( + llvmModule, LLVMVerifierFailureAction.LLVMPrintMessageAction, errorRef) == 1) { + throw Error("Invalid module"); + } + } LLVMWriteBitcodeToFile(llvmModule, outFile) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt index dd69405acae..e747ec8115c 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt @@ -85,7 +85,7 @@ internal fun getLlvmFunctionType(function: FunctionDescriptor): LLVMOpaqueType? if (extraParam.size == 0) return LLVMFunctionType(returnType, null, 0, 0) memScoped { - val paramTypesPtr = mallocNativeArrayOf(LLVMOpaqueType, *extraParam.toTypedArray())[0] // TODO: dispose + val paramTypesPtr = allocNativeArrayOf(LLVMOpaqueType, *extraParam.toTypedArray())[0] // TODO: dispose return LLVMFunctionType(returnType, paramTypesPtr, extraParam.size, 0) } } diff --git a/runtime/src/main/cpp/Memory.cpp b/runtime/src/main/cpp/Memory.cpp index 90f00a4e30b..f10c33a85da 100644 --- a/runtime/src/main/cpp/Memory.cpp +++ b/runtime/src/main/cpp/Memory.cpp @@ -13,7 +13,8 @@ ArenaContainer::ArenaContainer(uint32_t size) { calloc(size + sizeof(ArenaContainerHeader), 1)); header_ = header; header->ref_count_ = CONTAINER_TAG_INCREMENT; - header->current_ = reinterpret_cast(header_) + sizeof(ArenaContainerHeader); + header->current_ = + reinterpret_cast(header_) + sizeof(ArenaContainerHeader); header->end_ = header->current_ + size; } diff --git a/sum.c b/sum.c deleted file mode 100644 index a7f7e84f208..00000000000 --- a/sum.c +++ /dev/null @@ -1,5 +0,0 @@ -int foo(int a, int b){ - int c; - c = a + b; - return c; -} diff --git a/sum_func.c b/sum_func.c deleted file mode 100644 index 2f7f6f96434..00000000000 --- a/sum_func.c +++ /dev/null @@ -1,3 +0,0 @@ -int foo() { return 1; } -int bar() {return 2;} -int sum() {return foo() + bar();}