sangeeta chowdhary via llvm-dev
2018-Jul-17 04:00 UTC
[llvm-dev] how to convert value to size_t
Hi,
I am instrumenting load and store and passing memory address to the instrumented
function like below -
Type* void_ptr_ty = PointerType::getUnqual(Type::getInt8Ty(M.getContext()));
RecordMem = M.getOrInsertFunction("RecordMem", VoidTy, size_ty,
void_ptr_ty);
bool IsWrite = isa<StoreInst>(*I);
Value *Addr = IsWrite
? cast<StoreInst>(I)->getPointerOperand()
: cast<LoadInst>(I)->getPointerOperand();
BitCastInst* bitcast = new BitCastInst(Addr,
PointerType::getUnqual(Type::getInt8Ty(M.getContext())),
"", I);
ArgsV.push_back(bitcast);
IRB.CreateCall(RecordMem, ArgsV);
and in function RecordMem, I am type casting void Addr to size_t -
extern "C" void RecordMem(THREADID threadid, void * addr) {
size_t addr_addr = (size_t)addr;
…
}
I don’t want to type cast to size_t in RecordMem. I want to pass it as size_t
from llvm pass.
My RecordMem interface now is -
extern "C" void RecordMem(THREADID threadid, size_t addr) {}
How can I achieve this? How can I pass the address as size_t?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20180716/08577f8f/attachment.html>
Tim Northover via llvm-dev
2018-Jul-17 08:51 UTC
[llvm-dev] how to convert value to size_t
Hi Sangeeta, On Tue, 17 Jul 2018 at 05:00, sangeeta chowdhary via llvm-dev <llvm-dev at lists.llvm.org> wrote:> How can I achieve this? How can I pass the address as size_t?You have to know what size_t maps to for your platform and then use a PtrToInt instruction to cast it to that instead of i8*. LLVM doesn't store this information anywhere, but the answer is usually the same as the pointer size (which you could obtain from the Module's DataLayout). Though to my mind passing a "void *" is more descriptive anyway, or maybe a uintptr_t at a stretch. Cheers. Tim.