#include "MCJITTestBase.h"
#include "llvm/Support/DynamicLibrary.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class MCJITTest : public testing::Test, public MCJITTestBase {
protected:
void SetUp() override { M.reset(createEmptyModule("<main>")); }
};
TEST_F(MCJITTest, global_variable) {
SKIP_UNSUPPORTED_PLATFORM;
int initialValue = 5;
GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
createJIT(std::move(M));
void *globalPtr = TheJIT->getPointerToGlobal(Global);
EXPECT_TRUE(nullptr != globalPtr)
<< "Unable to get pointer to global value from JIT";
EXPECT_EQ(initialValue, *(int32_t*)globalPtr)
<< "Unexpected initial value of global";
}
TEST_F(MCJITTest, add_function) {
SKIP_UNSUPPORTED_PLATFORM;
Function *F = insertAddFunction(M.get());
createJIT(std::move(M));
uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
EXPECT_TRUE(0 != addPtr)
<< "Unable to get pointer to function from JIT";
ASSERT_TRUE(addPtr != 0) << "Unable to get pointer to function .";
int (*AddPtr)(int, int) = (int(*)(int, int))addPtr ;
EXPECT_EQ(0, AddPtr(0, 0));
EXPECT_EQ(1, AddPtr(1, 0));
EXPECT_EQ(3, AddPtr(1, 2));
EXPECT_EQ(-5, AddPtr(-2, -3));
EXPECT_EQ(30, AddPtr(10, 20));
EXPECT_EQ(-30, AddPtr(-10, -20));
EXPECT_EQ(-40, AddPtr(-10, -30));
}
TEST_F(MCJITTest, run_main) {
SKIP_UNSUPPORTED_PLATFORM;
int rc = 6;
Function *Main = insertMainFunction(M.get(), 6);
createJIT(std::move(M));
uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
EXPECT_TRUE(0 != ptr)
<< "Unable to get pointer to main() from JIT";
int (*FuncPtr)() = (int(*)())ptr;
int returnCode = FuncPtr();
EXPECT_EQ(returnCode, rc);
}
TEST_F(MCJITTest, return_global) {
SKIP_UNSUPPORTED_PLATFORM;
int32_t initialNum = 7;
GlobalVariable *GV = insertGlobalInt32(M.get(), "myglob", initialNum);
Function *ReturnGlobal =
startFunction(M.get(), FunctionType::get(Builder.getInt32Ty(), {}, false),
"ReturnGlobal");
Value *ReadGlobal = Builder.CreateLoad(Builder.getInt32Ty(), GV);
endFunctionWithRet(ReturnGlobal, ReadGlobal);
createJIT(std::move(M));
uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
EXPECT_TRUE(0 != rgvPtr);
int32_t(*FuncPtr)() = (int32_t(*)())rgvPtr;
EXPECT_EQ(initialNum, FuncPtr())
<< "Invalid value for global returned from JITted function";
}
#if !defined(__arm__)
TEST_F(MCJITTest, multiple_functions) {
SKIP_UNSUPPORTED_PLATFORM;
unsigned int numLevels = 23;
int32_t innerRetVal= 5;
Function *Inner = startFunction(
M.get(), FunctionType::get(Builder.getInt32Ty(), {}, false), "Inner");
endFunctionWithRet(Inner, ConstantInt::get(Context, APInt(32, innerRetVal)));
Function *Outer;
for (unsigned int i = 0; i < numLevels; ++i) {
std::stringstream funcName;
funcName << "level_" << i;
Outer = startFunction(M.get(),
FunctionType::get(Builder.getInt32Ty(), {}, false),
funcName.str());
Value *innerResult = Builder.CreateCall(Inner, {});
endFunctionWithRet(Outer, innerResult);
Inner = Outer;
}
createJIT(std::move(M));
uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
EXPECT_TRUE(0 != ptr)
<< "Unable to get pointer to outer function from JIT";
int32_t(*FuncPtr)() = (int32_t(*)())ptr;
EXPECT_EQ(innerRetVal, FuncPtr())
<< "Incorrect result returned from function";
}
#endif
TEST_F(MCJITTest, multiple_decl_lookups) {
SKIP_UNSUPPORTED_PLATFORM;
Function *Foo = insertExternalReferenceToFunction(
M.get(), FunctionType::get(Builder.getVoidTy(), {}, false), "_exit");
createJIT(std::move(M));
void *A = TheJIT->getPointerToFunction(Foo);
void *B = TheJIT->getPointerToFunction(Foo);
EXPECT_TRUE(A != nullptr) << "Failed lookup - test not correctly configured.";
EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail.";
}
typedef void * (*FunctionHandlerPtr)(const std::string &str);
TEST_F(MCJITTest, lazy_function_creator_pointer) {
SKIP_UNSUPPORTED_PLATFORM;
Function *Foo = insertExternalReferenceToFunction(
M.get(), FunctionType::get(Builder.getInt32Ty(), {}, false),
"\1Foo");
startFunction(M.get(), FunctionType::get(Builder.getInt32Ty(), {}, false),
"Parent");
CallInst *Call = Builder.CreateCall(Foo, {});
Builder.CreateRet(Call);
createJIT(std::move(M));
static std::string UnresolvedExternal;
FunctionHandlerPtr UnresolvedHandler = [] (const std::string &str) {
void *symbol =
llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(str.c_str());
if (symbol) {
return symbol;
}
UnresolvedExternal = str;
return (void *)(uintptr_t)-1;
};
TheJIT->InstallLazyFunctionCreator(UnresolvedHandler);
TheJIT->finalizeObject();
EXPECT_EQ(UnresolvedExternal, "Foo");
}
TEST_F(MCJITTest, lazy_function_creator_lambda) {
SKIP_UNSUPPORTED_PLATFORM;
FunctionType *Int32VoidFnTy =
FunctionType::get(Builder.getInt32Ty(), {}, false);
Function *Foo1 =
insertExternalReferenceToFunction(M.get(), Int32VoidFnTy, "\1Foo1");
Function *Foo2 =
insertExternalReferenceToFunction(M.get(), Int32VoidFnTy, "\1Foo2");
startFunction(M.get(), Int32VoidFnTy, "Parent");
CallInst *Call1 = Builder.CreateCall(Foo1, {});
CallInst *Call2 = Builder.CreateCall(Foo2, {});
Value *Result = Builder.CreateAdd(Call1, Call2);
Builder.CreateRet(Result);
createJIT(std::move(M));
std::vector<std::string> UnresolvedExternals;
auto UnresolvedHandler = [&UnresolvedExternals] (const std::string &str) {
void *symbol =
llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(str.c_str());
if (symbol) {
return symbol;
}
UnresolvedExternals.push_back(str);
return (void *)(uintptr_t)-1;
};
TheJIT->InstallLazyFunctionCreator(UnresolvedHandler);
TheJIT->finalizeObject();
auto I = UnresolvedExternals.begin(), E = UnresolvedExternals.end();
EXPECT_EQ(UnresolvedExternals.size(), 2u);
EXPECT_FALSE(std::find(I, E, "Foo1") == E);
EXPECT_FALSE(std::find(I, E, "Foo2") == E);
}
}