Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsupported opcode: SWAP #417

Closed
mhdl1991 opened this issue Dec 14, 2023 · 17 comments
Closed

Unsupported opcode: SWAP #417

mhdl1991 opened this issue Dec 14, 2023 · 17 comments

Comments

@mhdl1991
Copy link

Hi, I tried using this tool to help a friend with a .pyc file that was written in Python 3.11.4 and I got an error that about the SWAP opcode being unsupported.

I found a suggestion here to try and comment out a line in ASTree.cpp to make the decompiler not stop the moment it sees an unsupported opcode, but that gives me a "bad cast" error instead.

What can I do?

@greenozon
Copy link
Contributor

attach your sample for the beginning :)

@mhdl1991
Copy link
Author

attach your sample for the beginning :)

unfortunately for the sake of my friend's privacy, I don't think I can do so, sucks.

@greenozon
Copy link
Contributor

well, then the only option is to sit and wait...
or try to do PR and fix! :)

@hello-abson
Copy link

Source Generated with Decompyle++

File: test.pyc (Python 3.11)

def a():
Unsupported opcode: SWAP
pass

WARNING: Decompyle incomplete

SWAP demo

@hello-abson
Copy link

hello-abson commented Dec 31, 2023

test.zip

def a():
for dir in "":
path = dir
return path
pass
dis.dis(a)

26 0 RESUME 0

27 2 LOAD_CONST 1 ('')
4 GET_ITER
6 FOR_ITER 7 (to 22)
8 STORE_FAST 0 (dir)

28 10 LOAD_FAST 0 (dir)
12 STORE_FAST 1 (path)

29 14 LOAD_FAST 1 (path)
16 SWAP 2
18 POP_TOP
20 RETURN_VALUE

30 >> 22 LOAD_CONST 0 (None)
24 RETURN_VALUE

SWAP demo

@hello-abson
Copy link

I solved it myself and modified the source code to support the directive

@greenozon
Copy link
Contributor

great news!
how about creating PR for it? :)

@hello-abson
Copy link

Let swap press the stack,
stack.push(nullptr);

I've fixed about 10 py3.11 commands myself so far.
Is currently solving the try except instruction, card for a long time, do not understand how to repair

try:
pass
except:
raise
try:
pass
except:
print("123")

@greenozon
Copy link
Contributor

Hmm
but why push NULL to the TOS (top of stack)?

the standard python doc says something else about how exactly SWAP opcode behaves:

image

@hello-abson
Copy link

void swap(int index)
{
    if (m_ptr > -1)
    {
        PycRef<ASTNode> node = top();
        if (node) {
			m_stack[m_ptr] = m_stack[index - 1];
			m_stack[index - 1] = node;
        }
    }
}

I tried to write according to the document, and found that it did not show the expected results, so I gave up, and directly pressed into the empty stack to parse normally.

@greenozon
Copy link
Contributor

OK, your impl looks valid
but why do you push extra val (NULL) to the stack? it definitely violates the opcode mechanics...

one could agree with pushing NULL when the opcode is COPY (similar to SWAP)...
any ideas?

@hello-abson
Copy link

I'm still working on the parsing of try,
help me

try:
pass
except:
raise
try:
pass
except:
print("123")

@greenozon
Copy link
Contributor

Python 3.11 uses totally different scheme known as "zero-cost" exception handling.
Before 3.11, exceptions were handled by a runtime opcodes present in bytecode...

I guess you have to start parsing/using the exception table present in pyc structure
but it's a bit complicated as you might have nested exceptions/etc

@greenozon
Copy link
Contributor

I guess it is good idea to read some theory - it is inside Python sources in teh following path:
Python-3.11.6.tar\Python-3.11.6\Objects\exception_handling_notes.txt

@hello-abson
Copy link

#409

I saw in other posts that you have solved the problem, can you tell me how to solve it? Share the code

def a():
try:
print("123")
finally:
print("finally")

@greenozon
Copy link
Contributor

Please help me to solve this PR - #411
collaboration to the rescue!

@zrax
Copy link
Owner

zrax commented Feb 21, 2024

Duplicate #452

@zrax zrax closed this as completed Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants