how to decompile bytecode into human readable format

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

how to decompile bytecode into human readable format

Post by jickster » Mon Oct 02, 2017 8:54 pm

I used the mpy-cross utility to turn a .py into a bytecode .mpy file.

Is it possible to decompile the .mpy into human readable format?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how to decompile bytecode into human readable format

Post by pythoncoder » Tue Oct 03, 2017 6:16 am

I don't think anyone has written a tool to do this. I imagine it would be a straightforward task if there were any documentation for the bytecode. Alas (as far as I know) there is not. It's also worth noting that the bytecode is something of a moving target in that Damien occasionally adds opcodes. Hence it's wise to recompile mpy-cross when you download MicroPython sourcecode.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: how to decompile bytecode into human readable format

Post by Roberthh » Tue Oct 03, 2017 6:39 am

@jickster What is your intention?
If you are asking whether bytecode is something like a protected format: It is not! The intention is efficiency: split the compile and execute phase and use a machine with a lot of resources (your PC) for compile.
If you are simply interested how the bytecode looks like? There may be information available by Damien or at least represented by the source code.
If you have bytecode from somewhere and want to look into it: just go ahead and do it. You may end up with your own tool and a lot of understanding of the bytecode.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how to decompile bytecode into human readable format

Post by pythoncoder » Tue Oct 03, 2017 7:00 am

@Roberthh You're describing a process of reverse engineering here - writing short MicroPython scripts, examining the bytecode and deducing its purpose. While possible and instructive it would be hard (understatement?) to guarantee a complete set of bytecodes.

A decompiler would be a useful tool, notably when trying to optimise code. If there's much interest perhaps you or I might consider writing one but I think we'd need information from Damien. To some extent its feasibility would depend on the degree to which existing bytecodes are set in stone or whether their functionality is subject to change.
Peter Hinch
Index to my micropython libraries.

mwm
Posts: 36
Joined: Wed Aug 09, 2017 3:52 pm

Re: how to decompile bytecode into human readable format

Post by mwm » Tue Oct 03, 2017 9:03 am


User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: how to decompile bytecode into human readable format

Post by dhylands » Tue Oct 03, 2017 3:35 pm

The extract_byte_code.py was intended to extract native code and then I ran a disassembler on the binary blob to compare to the inline assembler.

There is actually a byte-code annotator in the source code. You can turn it on and get the byte code dumped in a human readable form while its compiling.

It looks like there are some functions for displaying bytecodes in human readable form in the py/showbc.c file.

Pytri
Posts: 2
Joined: Fri Nov 13, 2020 7:50 pm

Re: how to decompile bytecode into human readable format

Post by Pytri » Sat Nov 14, 2020 7:06 am

jickster wrote:
Mon Oct 02, 2017 8:54 pm
I used the mpy-cross utility to turn a .py into a bytecode .mpy file.

Is it possible to decompile the .mpy into human readable format?
Give a try to Python Decompiler Online. It works for .pyc files.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how to decompile bytecode into human readable format

Post by jimmo » Sat Nov 14, 2020 9:15 am

Pytri wrote:
Sat Nov 14, 2020 7:06 am
Give a try to Python Decompiler Online. It works for .pyc files.
MicroPython does not use pyc files, and the bytecode is completely different to CPython.

MasterOfGizmo
Posts: 50
Joined: Sun Nov 29, 2020 8:17 pm

Re: how to decompile bytecode into human readable format

Post by MasterOfGizmo » Sun Nov 29, 2020 8:44 pm

Any news on this?

The Lego Spike hub runs micropython and the source code doesn't seem to be public. Its default UI comes in form of a mpy and can easily be downloaded from the device. It would be nice to be able to decompile and modify this.

jcase
Posts: 3
Joined: Sun Dec 20, 2020 5:50 pm

Re: how to decompile bytecode into human readable format

Post by jcase » Sun Dec 20, 2020 5:53 pm

While today is my first day playing with micropython, I'm pretty versed in writing disassemblers and decompilers from scratch. I'll give it a shot at writing one. Can anyone share a source for some precompiled bins that don't have source available? Obviously I'll start with my own examples, but would like some unknowns for validation.

Post Reply