rocky/python-xasm

pyc-xasm KeyError: '0'

p0mel0 opened this issue · 1 comments

In the python interactive command line, I try to output bytecode via dis.dis(my_function_bytecode)

>>> sys.version
'2.6.2 (r262:71600, Dec 16 2020, 13:15:02) \n[GCC 4.8.5 20150623 (Red Hat 4.8.5-1)]'
>>> import aaa.bbb.ccc as ttt
>>> import dis
>>> dis.dis(ttt.handler.__code__.co_code)
          0 LOAD_GLOBAL         0 (0)
          3 LOAD_CONST          1 (1)
          6 LOAD_CONST          2 (2)
          9 CALL_FUNCTION       2
         12 STORE_FAST          1 (1)
         15 LOAD_GLOBAL         0 (0)

Copy the output to the test.pyasm file and add the 'Python bytecode 2.6 (62131)' header

# Python bytecode 2.6 (62131)
          0 LOAD_GLOBAL         0 (0)
          3 LOAD_CONST          1 (1)
          6 LOAD_CONST          2 (2)
          9 CALL_FUNCTION       2
         12 STORE_FAST          1 (1)
         15 LOAD_GLOBAL         0 (0)
         18 LOAD_CONST          1 (1)
         21 LOAD_CONST          2 (2)
         24 CALL_FUNCTION       2
         27 STORE_FAST          2 (2)
         30 LOAD_GLOBAL         0 (0)
         33 LOAD_CONST          1 (1)
         36 LOAD_CONST          2 (2)
         39 CALL_FUNCTION       2
        ......
       1908 BINARY_MODULO  
       1909 LOAD_CONST         26 (26)
       1912 LOAD_ATTR          81 (81)
       1915 LOAD_FAST          26 (26)
       1918 LOAD_CONST         52 (52)
       1921 LOAD_CONST          4 (4)
       1924 SLICE+3        
       1925 CALL_FUNCTION       1
       1928 CALL_FUNCTION       2
       1931 POP_TOP        

I tried to convert 'test.pyasm' to pyc by using pyc-xasm, but the following error appears. How should I fix it

➜  python-xasm git:(master) ✗ ./pyc-xasm ibapapachemod_1.pyasm
Traceback (most recent call last):
  File "./pyc-xasm", line 33, in <module>
    sys.exit(load_entry_point('xasm', 'console_scripts', 'pyc-xasm')())
  File "/opt/homebrew/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homebrew/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/opt/homebrew/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homebrew/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/Users/poom/Downloads/python-xasm/xasm/xasm_cli.py", line 28, in main
    asm = asm_file(asm_path)
  File "/Users/poom/Downloads/python-xasm/xasm/assemble.py", line 326, in asm_file
    opname, operand = get_opname_operand(asm.opc, fields[1:])
  File "/Users/poom/Downloads/python-xasm/xasm/assemble.py", line 39, in get_opname_operand
    if opc.opmap[opname] < opc.HAVE_ARGUMENT:
KeyError: 'SLICE+3'
➜  python-xasm git:(master) ✗
rocky commented

A guess is that xasm is not understanding this is Python 2.6 bytecode as is using Python 3.x opcodes instead. (SLICE+3 is not a valid opcode in Python 3.x).

There are basically 3 things that make this kind of issue not something that is ready for reporting here, whether or not this is a bug versus a improper use or user-related problem.

  1. The assembly file must be as short as possible. Your bytecode seems to have size about 2K bytes. (Aside from the elision, this does not end like a normal assembly file should). Narrow the assembly code down into the smallest possible assembly code that exhibits the problem.
  2. The full input assembly file must be provided. When you report a bug here, you lose privacy of your code. But that's shouldn't be a problem since you can change names of variables and values of constants as you like. And as in step 1. Most of the code should get removed as well, except for a small portion.