Skip to content

Commit

Permalink
fix #40 and fix #41
Browse files Browse the repository at this point in the history
  • Loading branch information
ahueck committed Dec 29, 2020
1 parent 146fbaf commit 8d1f8ff
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
22 changes: 13 additions & 9 deletions lib/passes/instrumentation/TypeARTFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#include "TypeARTFunctions.h"

#include "support/Logger.h"

#include "llvm/IR/Argument.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
Expand Down Expand Up @@ -49,13 +49,17 @@ llvm::Function* TAFunctionDeclarator::make_function(IFunc id, llvm::StringRef ba
// f->setLinkage(GlobalValue::ExternalWeakLinkage);
};
const auto do_make = [&](auto& name, auto f_type) {
auto fc = m.getOrInsertFunction(name, f_type);
#if LLVM_VERSION >= 10
auto f = dyn_cast<Function>(fc.getCallee());
#else
auto f = dyn_cast<Function>(fc);
#endif
setFunctionLinkageExternal(f);
const bool has_f = m.getFunction(name) != nullptr;
auto fc = m.getOrInsertFunction(name, f_type);

Function* f{nullptr};
if (has_f) {
LOG_WARNING("Function " << name << " is already declared in the module.")
f = dyn_cast<Function>(fc.getCallee()->stripPointerCasts());
} else {
f = dyn_cast<Function>(fc.getCallee());
setFunctionLinkageExternal(f);
}
addOptimizerAttributes(f);
return f;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/CallbackInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void __typeart_alloc_global(const void* addr, int typeId, size_t count);
void __typeart_free(const void* addr);

void __typeart_alloc_stack(const void* addr, int typeId, size_t count);
void __typeart_leave_scope(size_t alloca_count);
void __typeart_leave_scope(int alloca_count);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ void TypeArtRT::onFreeHeap(const void* addr, const void* retAddr) {
}
}

void TypeArtRT::onLeaveScope(size_t alloca_count, const void* retAddr) {
void TypeArtRT::onLeaveScope(int alloca_count, const void* retAddr) {
if (unlikely(alloca_count > stackVars.size())) {
LOG_ERROR("Stack is smaller than requested de-allocation count. alloca_count: " << alloca_count
<< ". size: " << stackVars.size());
Expand Down Expand Up @@ -580,7 +580,7 @@ void __typeart_free(const void* addr) {
RUNTIME_GUARD_END;
}

void __typeart_leave_scope(size_t alloca_count) {
void __typeart_leave_scope(int alloca_count) {
RUNTIME_GUARD_BEGIN;
const void* retAddr = __builtin_return_address(0);
typeart::TypeArtRT::get().onLeaveScope(alloca_count, retAddr);
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class TypeArtRT final {

void onFreeHeap(const void* addr, const void* retAddr);

void onLeaveScope(size_t alloca_count, const void* retAddr);
void onLeaveScope(int alloca_count, const void* retAddr);

private:
TypeArtRT();
Expand Down
10 changes: 10 additions & 0 deletions test/pass/misc/03_make_mismatch_callback.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %c-to-llvm %s | %apply-typeart -typeart-alloca -S 2>&1 | FileCheck %s
// XFAIL: *

#include <stddef.h>
void __typeart_leave_scope(size_t alloca_count);

int main(void) {
__typeart_leave_scope(0);
return 0;
}
13 changes: 13 additions & 0 deletions test/pass/misc/04_make_callback_match.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %c-to-llvm %s | %apply-typeart -typeart-alloca -S 2>&1 | FileCheck %s

void __typeart_leave_scope(int alloca_count);

int main(void) {
__typeart_leave_scope(0);
return 0;
}

// CHECK: TypeArtPass [Heap & Stack]
// CHECK-NEXT: Malloc : 0
// CHECK-NEXT: Free : 0
// CHECK-NEXT: Alloca : 1
18 changes: 18 additions & 0 deletions test/pass/misc/05_make_all_callbacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %c-to-llvm %s | %apply-typeart -typeart-alloca -S 2>&1 | FileCheck %s

#include "../../../lib/runtime/CallbackInterface.h"

int main(void) {
int count = 0;
int type_id = 10;
size_t extent = 0;
void* addr = NULL;
__typeart_alloc(addr, type_id, extent);
__typeart_alloc_global(addr, type_id, extent);
__typeart_alloc_stack(addr, type_id, extent);
__typeart_free(addr);
__typeart_leave_scope(count);
return 0;
}

// CHECK: TypeArtPass [Heap & Stack]

0 comments on commit 8d1f8ff

Please sign in to comment.