qilingframework / qiling

A True Instrumentable Binary Emulation Framework
https://qiling.io
GNU General Public License v2.0
5.14k stars 744 forks source link

How do you implement blocking system calls with gevent? #1461

Closed iMoD1998 closed 6 months ago

iMoD1998 commented 8 months ago

I've noticed this library uses gevent for essentially context switching on blocking threads but its not clear how you can go about implementing a blocking system call that needs to return a value. Only examples of gevent being used is for yielding and sleeping but im currently having an issue where the emulation gets deadlocked on a newselect system call.

I have tried to use gevent's monkey patching but it seems to cause issues maybe with unicorn that causes crashes on some random pointer deref.

Also if a single thread tries to use gevent on a select call that waits infinitely it will not hand control to another thread and complain about it would loop forever?

I have managed to do a ghetto fix by making the infinitely waiting select a finite time and then using the same code as sleep to pause that thread for some time to hand control back over to the main thread but its not ideal.

Just looking for a bit of info or example how i could implement this.

wtdcode commented 8 months ago

Generally speaking, you can simply do blocking things in sched_cb safely.

See: https://github.com/qilingframework/qiling/blob/9a78d186c97d6ff42d7df31155dda2cd9e1a7fe3/qiling/os/linux/thread.py#L202

iMoD1998 commented 8 months ago

Generally speaking, you can simply do blocking things in sched_cb safely.

See:

https://github.com/qilingframework/qiling/blob/9a78d186c97d6ff42d7df31155dda2cd9e1a7fe3/qiling/os/linux/thread.py#L202

How does this work with return values?