1

I have a function called analyze() which is like following:

def analyze():
    for stmt in irsb.statements:
        if isinstance(stmt, pyvex.IRStmt.WrTmp):
           wrtmp(stmt)
        if isinstance(stmt, pyvex.IRStmt.Store):
           address = stmt.addr
           address1 = '{}'.format(address)[1:]
           print address1
           data = stmt.data
           data1 = '{}'.format(data)[1:]
           tmp3 = store64(address1, int64(data1))
        if isinstance(stmt, pyvex.IRStmt.Put):
           expr = stmt.expressions[0]
           putoffset = stmt.offset
           data =  stmt.data
           data4 = '{}'.format(data)[1:]
           if (str(data).startswith("0x")):
             #const_1 = ir.Constant(int64, data4)
             tmp = put64(putoffset, ZERO_TAG)
           else:
             put64(putoffset, int64(data4))
             if isinstance(stmt.data, pyvex.IRExpr.Const):
                reg_name = irsb.arch.translate_register_name(stmt.offset, stmt.data.result_size(stmt.data.tag))
                print reg_name
        stmt.pp()

This code function gets following input and try to analyze it:

CODE = b"\xc1\xe0\x05"  
irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())

When this input is in the same file in my code (lets call the whole as analyze.py) it works and python analyze.py will make me an output. However, I want to make a seperate file(call array.py), call analyze there and also put the inputs inside it and run python array.py to get the same result. I did the following for array.py:

from analyze import analyze

CODE = b"\xc1\xe0\x05" 
irsb = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())
analyze()

However, when I run the array.py, it stops me with error;

NameError: name 'CODE' is not defined

how can I resolve this problem? What is the solution?

2
  • Your variable scoping is not correct. You should be passing in isrb as a parameter to your function. Commented Sep 21, 2017 at 16:13
  • @sarah, can you provide your directory structure? Commented Sep 21, 2017 at 16:13

1 Answer 1

2

A simple change in your function, add parameters:

def analyze(irsb):   # irsb here called parameter
  ...
  # The rest is the same

And then pass arguments when calling it:

from analyze import analyze

CODE = b"\xc1\xe0\x05" 
irsb_as_arg = pyvex.block.IRSB(CODE, 0x80482f0, archinfo.ArchAMD64())
analyze(irsb_as_arg)   # irsb_as_arg is an argument

I have just changed here irsb to irsb_as_arg to take attention, but it can be the same name

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it works. What if I want to import functions of analyze.py when analyze.py is in another directory?
Please refer to python absolute and relative import, that depends if you treated the whole as a module or direct script stackoverflow.com/questions/46327361/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.