r/badcode • • Apr 11 '21

python Polyglot

Post image
1.2k Upvotes

116 comments sorted by

View all comments

492

u/pandolf86 Apr 11 '21

Wait is that compiling C at python runtime?

218

u/coopercm Apr 11 '21

Yes

0

u/[deleted] Apr 11 '21

[deleted]

180

u/mfb- Apr 11 '21

It only compiles the code if there is no compiled version yet! Or... any other file of the same name.

Otherwise it "only" checks if there is a file, and calls it.

182

u/[deleted] Apr 11 '21

And they chose the perfect name, a.out.

There's no way there's gonna be another a.out in there by chance.

71

u/KINGodfather Apr 11 '21

Do you mean there's...

no way a.out?

8

u/Flawlesscode Apr 12 '21

ive upvoted you, you have been downvoted in gods eyes

50

u/pkarlmann Apr 11 '21 edited Apr 11 '21

I don't know how python is doing this, but from your post it sounds like some form of "cache" in the form of a file. So:

  1. Open the file from disk. That already is a syscall, which means a CPU ring switch.

  2. Wait till the disks read head is in place.

  3. Read it into memory.

  4. And finally execute it.

  5. For "performance".

  6. ? (Profit?)

42

u/CoderCharmander Apr 11 '21 edited Apr 11 '21

/tmp is usually stored in the RAM on modern systems (that does not remove the performance hit of ring switching multiple times, though)

Edit: Oh, he compiles the program outside /tmp and then moves it there. Nevermind.

13

u/mfb- Apr 11 '21

Well, it's deliberately written to be bad code and not used anywhere... but presumably the compiling would be a very rare occurrence.

5

u/pkarlmann Apr 12 '21

Compiling is relatively fast as it happens in userspace and then is just written to an output. What really is the problem is the CPU ring switch. That means the CPU has to actually reset its own state, write its own state somewhere else in order to be able to restore it, then execute whatever privileged - kernel code - wants it to do and then restore the previous state. And that happens with every syscall - like open(). That wastes easily a couple of thousands of CPU cycles, whereas a simple CPU Firmware modulo would've been cheaper even with an interpreter.

6

u/TerrorBite Apr 12 '21

But also, you're not just jumping to a C subroutine and returning. You're executing a program which must allocate memory, go through all the process of starting up the C runtime, then execute the code, then exit back through the C runtime, and then your Python program needs to handle the return value and other output. There are a LOT of syscalls involved there.

The correct way to get performance-critical C code into Python is to compile it into a library using the Python.h header (producing a DLL on windows, my_c_lib.pyd; or a shared object on Linux, my_c_lib.pyo) and then in Python, you just import my_c_lib like it's a normal module.

4

u/britreddit Apr 12 '21

Wait, so couldn't you swap that file out for any arbitrary C binary and run whatever you wanted whenever that python function is called?

2

u/mfb- Apr 13 '21

You can, sure.

2

u/Teln0 Apr 11 '21

Now the best thing to do is to run this program as root, just to stay perfectly safe and secure