Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a C++ pointer with LLVM JIT'd code

I want to have most of my program be an ordinarily compiled C++ program. Said program uses a chunk of contiguous memory for a stack. The top of the stack is maintained by an ordinary pointer.

I want to share that pointer with code generated via 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;                 // I want this pointer to be shared with JIT'd code

llvm::Function *const func = llvm::cast<llvm::Function>(
    module->getOrInsertFunction( "func", llvm::Type::getVoidTy( ctx ), (llvm::Type*)0 )
);
llvm::BasicBlock *const block = llvm::BasicBlock::Create( ctx, "entry", func );

pointerInc( &top, block );        // Increment the pointer in JIT'd code

llvm::ReturnInst::Create( ctx, block );
llvm::verifyFunction( *func, &llvm::outs() );
llvm::verifyModule( *module, &llvm::outs() );
module->dump();

llvm::EngineBuilder eb( std::move( uptr_module ) );
llvm::ExecutionEngine *const exec = eb.create();
assert( exec );

void *const func_ptr = exec->getPointerToFunction( func );
assert( func_ptr );
typedef void (*PFv_v)();
(*(PFv_v)func_ptr)();             // Call JIT'd function

where pointerInc() will insert JIT'd code into the current BasicBlock to increment top. The pointerInc() code is:

// 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 doesn't work. It's the body of (the larger) pointerInc() that's wrong. The code is actually derived from LLVM C++ API code generated by llc on an ordinary C++ program that increments a pointer.

When run, 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:

  1. Is this correct? Can I even do what I want, i.e., share a raw C++ pointer with JIT'd code?
  2. Why is it dumping core?

Even if I made the JIT'd function be empty, the code still core dumps at the line that calls the function. The LLVM JIT set-up code looks like all the examples I've seen, so I don't see what's wrong with that either.

A little help?


Update

If I change the deprecated line:

void *const func_ptr = exec->getPointerToFunction( func );

to the new line:

uint64_t const func_ptr = exec->getFunctionAddress( "func" );

then func_ptr is null.

like image 870
Paul J. Lucas Avatar asked Oct 31 '22 23:10

Paul J. Lucas


1 Answers

After playing around a lot more with lcc (and using better C++ code to feed into it), I have it working:

llvm::Value* pointerToPointer( void *ptr, llvm::BasicBlock *block ) {
    using namespace llvm;
    LLVMContext &ctx = block->getContext();
    ConstantInt *const ptrAsInt =
        ConstantInt::get( IntegerType::get( ctx, 64 ), (uint64_t)ptr );
    PointerType *const Int8Ptr_type = Type::getInt8PtrTy( ctx );
    PointerType *const Int8PtrPtr_type = PointerType::getUnqual( Int8Ptr_type );
    return new IntToPtrInst( ptrAsInt, Int8PtrPtr_type, "pptr", block );
}

void pointerInc( llvm::Value *pptr, llvm::ConstantInt *sizeof_T,
                llvm::BasicBlock *block ) {
    using namespace llvm;
    LLVMContext &ctx = block->getContext();

    LoadInst *const ptr = new LoadInst( pptr, "ptr", block );
    ptr->setAlignment( sizeof(void*) );

    GetElementPtrInst *const inc =
        GetElementPtrInst::Create( ptr, sizeof_T, "inc", block );

    StoreInst *const store = new StoreInst( inc, pptr, block );
    store->setAlignment(sizeof(void*));
}

template<typename T>
inline void pointerInc( T **pptr, llvm::BasicBlock *block ) {
    using namespace llvm;
    LLVMContext &ctx = block->getContext();
    ConstantInt *const sizeof_T = ConstantInt::get(
        IntegerType::get( ctx, 64 ), (uint64_t)sizeof( T )
    );
    pointerInc( pointerToPointer( pptr, block ), sizeof_T, block );
}

However, the program runs successfully only when the JIT'd function is called via:

vector<GenericValue> noargs;
exec->runFunction( func, noargs );

Using either getFunctionAddress() or getPointerToFunction() dumps core. I still don't have an answer to that.

like image 161
Paul J. Lucas Avatar answered Nov 15 '22 03:11

Paul J. Lucas