--- tags: comp-decomp title: Slicing Instructions corpus --- # Overview This experiment analyzes the information gained from slicing code objects to see what instructions are discovered. ## Slicing assumptions All Code Object slices have the following properties. - Python version: 3.8 - All slices don't start with: - `STORE_FAST` - `JUMP_ABSOLUTE` - Update jumps if the target of the jump is within the slice region - Update Jump instructions to last instruction if greater than last offset of slice. - Append `RETURN_VALUE` instruction to preserve nature of the code object :::info Our slice will also contain the original code object .i.e. decompilation of the code object. ::: ## Code examples: All code examples are motivated by changing control flows from [here](https://hackmd.io/Pnoy0j14Qy2172yNDlYF2A?both) ### Example 1: Original code: ```python while a or b: continue ``` Decompilation: ```python while not a: if b: continue ``` Instructions from slicing: ```python { 'return', 'while not a:', ' if b:', ' continue', ' pass', 'if b:' } ``` ### Example 2: Original code: ```python while a or b or c and d: continue z=z ``` Decompilation: ```python while not a: if not b: if c: if d: continue z = z ``` Instructions from slicing: ```python { ' pass', ' pass', 'if not b:', 'if d:', 'while not a:', ' if c:', ' continue', 'if c:', ' if d:', ' if c:', ' if d:', 'z = z', 'return', ' if d:', ' pass', ' if not b:' } ``` :::warning Might use PyFET for more diverse inputs. ::: ### Example 3: Original code: ```python for item in token_generator: tmp = a and b if tmp: if c or d or e : z = z else: z = z ``` Decompilation: ```python for item in token_generator: tmp = a and b if not tmp or c or d or e: # This is incorrect z = z else: z = z ``` Instructions from slicing: ```python { ' z = z', ' else:', 'if not c:', 'tmp = b', 'tmp = a and b', ' if not tmp or c or d or e:', ' if e:', ' if c or d or e:', 'if not d:', ' pass', 'if tmp:', ' if not d:', ' pass', ' pass', ' pass', 'z = z', ' if not d:', ' if not c:', ' if not d:', ' if e:', 'if e:', 'for item in token_generator:', ' tmp = a and b', ' if e:', ' if tmp:', ' if not c:', ' pass', ' if e:', ' z = z', 'return' } ``` ### Example 4: Original code: ```pyhton def nextUid(): try: pass except z: raise 'errr' return x ``` Decompiled code: ```python def nextUid(): try: pass except z: raise 'errr' else: return x ``` Instructions from slicing: ```python { " raise 'errr'", 'try:', 'return x', "raise 'errr'", ' return x', 'except z:', ' pass', 'else:' } ```