I want most of my program to be a usually compiled C ++ program. The specified program uses a piece of contiguous memory for the stack. The top of the stack is supported by a regular pointer.
I want to share this pointer with code generated through LLVM JIT. For example, given:
llvm::InitializeNativeTarget(); llvm::LLVMContext ctx; std::unique_ptr<llvm::Module> uptr_module = llvm::make_unique<llvm::Module>( "lt", ctx ); llvm::Module *const module = uptr_module.get(); int *const stack = new int[100]; int *top = stack;
where pointerInc() will insert the JIT'd code into the current BasicBlock to increase top . Code pointerInc() :
// Convert a raw C++ pointer into an LLVM Constant*. template<typename T> inline llvm::Value* ptrToValue( T **pptr, llvm::LLVMContext &ctx ) { return return llvm::ConstantInt::get( llvm::Type::getInt64Ty( ctx ), (uint64_t)pptr ); } void pointerInc( llvm::Constant *pptrAsInt64, llvm::ConstantInt *sizeof_T, llvm::BasicBlock *block ) { llvm::LLVMContext &ctx = block->getContext(); llvm::Constant *const intToPtr8 = llvm::ConstantExpr::getIntToPtr( pptrAsInt64, llvm::PointerType::getUnqual( llvm::Type::getInt8Ty( ctx ) ) ); llvm::GetElementPtrInst *const inc = llvm::GetElementPtrInst::Create( intToPtr8, sizeof_T, "inc", block ); llvm::CastInst *const cast = llvm::CastInst::CreatePointerCast( inc, llvm::Type::getInt64Ty( ctx ), "cast", block ); llvm::Constant *const intToPtr64 = llvm::ConstantExpr::getIntToPtr( pptrAsInt64, llvm::PointerType::getUnqual( llvm::Type::getInt64Ty( ctx ) ) ); llvm::StoreInst *const store = new llvm::StoreInst( cast, intToPtr64, false, block ); store->setAlignment( 8 ); } template<typename T> inline void pointerInc( T **pptr, llvm::BasicBlock *block ) { llvm::LLVMContext &ctx = block->getContext(); llvm::ConstantInt *const sizeof_T = llvm::ConstantInt::get( llvm::Type::getInt64Ty( ctx ), sizeof( T ) ); pointerInc( ptrToValue( pptr, ctx ), sizeof_T, block ); }
Unfortunately this does not work. This is the wrong body of (larger) pointerInc() . The code is actually derived from the LLVM C ++ API code generated by llc on a regular C ++ program that increments a pointer.
At startup, the program prints:
&p = 140734551679784 -------------------- ; ModuleID = 'lt' define void @func() { entry: %inc = getelementptr i8* inttoptr (i64 140734551679784 to i8*), i64 4 %cast = ptrtoint i8* %inc to i64 store i64 %cast, i64* inttoptr (i64 140734551679784 to i64*), align 8 ret void } Segmentation fault: 11 (core dumped)
There are two questions:
- It is right? Can I do what I want, that is, share a raw C ++ pointer with JIT'd code?
- Why does this reset the core?
Even if I made the JIT'd function empty, the code still remains in the line that calls the function. The LLVM JIT setup code looks like all the examples I saw, so I donโt see what is wrong with this.
Help a little?
Update
If I change the deprecated line:
void *const func_ptr = exec->getPointerToFunction( func );
to a new line:
uint64_t const func_ptr = exec->getFunctionAddress( "func" );
then func_ptr is null.