[llvm-dev] question about llvmlite

23 views
Skip to first unread message

Samaneh Berenjian via llvm-dev

unread,
Jun 21, 2017, 10:02:04 AM6/21/17
to llvm...@lists.llvm.org
​Hi all,

I am using llvmlite for pyvex and I want the output of my code (which is written based on llvmlite) to be like pyvex. In pyvex, (https://github.com/angr/pyvex). Considering pyvex, I tried to implement the following statements in pyvex to llvmlite:

for stmt in irsb.statements:
   if isinstance(stmt, pyvex.IRStmt.Store):
      print "ST%s(%s) = %s" % (self.endness[-2:].lower(), self.addr, self.data) (which gets the data in a register and store it in another register)

I translate it in llvmlite as follows:

from ctypes import CFUNCTYPE, c_int
import archinfo
import llvmlite.ir as ll
import llvmlite.binding as llvm
import pyvex

CODE = b"\x55\x48\x8b\x05\xb8\x13\x00\x00"
mehran = -100

llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()

module = ll.Module()
func_ty = ll.FunctionType(ll.VoidType(), [])
func = ll.Function(module, func_ty, name='read_instructions')
a = func.args
bb_entry = func.append_basic_block('entry')
irbuilder = ll.IRBuilder(bb_entry)
int_type = ll.IntType(64);
irsb = pyvex.block.IRSB(CODE, 0x400400, archinfo.ArchAMD64())


for stmt in irsb.statements:

    #if isinstance(pyvex.IRStmt.Store):
    with irbuilder.if_then(stmt, pyvex.IRStmt.Store):

       t = irbuilder.load_reg(ll.IntType(64), stmt.data)
       t1 = irbuilder.load_reg(ll.IntType(64), stmt.addr)
       tcall = irbuilder.call(func, [t])
       t1call = irbuilder.call(func, [t1])
       result  =  irbuilder.store_reg(ll.Constant(ll.IntType(64), t), ll.IntType(64), t1)
       irbuilder.ret( result )
       print( module )
       target = llvm.Target.from_default_triple()
       target_machine = target.create_target_machine()
       backing_mod = llvm.parse_assembly("")
       engine = llvm.create_mcjit_compiler(backing_mod, target_machine)
       mod = llvm.parse_assembly( str( module ) )
       mod.verify()
       engine.add_module(mod)
       engine.finalize_object()
       func_ptr = engine.get_function_address("read_instructions")
       c_fn_fib = CFUNCTYPE(c_int64, c_int64)(func_ptr)

           
Having the above code, when running it, I am stopped with error:
AttributeError: 'IMark' object has no attribute 'data'

Can anyone help me in this way?
Thank you
-- 
This email was Anti Virus checked by  Security Gateway.

Brian Cain via llvm-dev

unread,
Jun 21, 2017, 10:44:36 AM6/21/17
to Samaneh Berenjian, llvm...@lists.llvm.org

I think you want “stmt.addr” and “stmt.data” instead of “self.addr”, “self.data”.

 

You can check what attributes an object has with “dir(self)” and “dir(stmt)”.  The error indicates you’ve referenced an attribute of “self” that doesn’t exist.

 

-Brian

Samaneh Berenjian via llvm-dev

unread,
Jun 21, 2017, 11:45:35 AM6/21/17
to Brian Cain, llvm...@lists.llvm.org
Thank you for the reply. In the case of my code, it does not matter to use stmt or self. I even did your suggestion but right now it does not know the self. :(
Reply all
Reply to author
Forward
0 new messages