Skip to content

Commit 5415dbf

Browse files
authored
Make examples/fib.py work on Linux too directly (#3)
1 parent b7e3d07 commit 5415dbf

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ Python bindings for [@yrp](https://github.com/yrp604/)'s [BochsCPU](https://gith
1414
* You can also generate a `.whl`:
1515

1616
```
17-
mkdir wheel
18-
python -m pip wheel . -w .\wheel\
17+
python -m pip wheel .
1918
```
2019

2120
## Install
@@ -29,14 +28,14 @@ python -m pip install .
2928
Or without cloning
3029

3130
```
32-
python -m pip install git+https://github.com/hugsy/bochscpu-python.git#egg=bochscpu
31+
python -m pip install git+https://github.com/hugsy/bochscpu-python.git#egg=bochscpu-python
3332
```
3433

3534

3635
### Via PyPI
3736

3837
```
39-
python -m pip install bochscpu
38+
python -m pip install bochscpu-python
4039
```
4140

4241

@@ -51,4 +50,6 @@ python -m pip install wheel/bochscpu-$version-$os-$arch.whl
5150

5251
## Usage
5352

54-
Just import the `bochscpu` module
53+
Just import the `bochscpu` module and let the fun begin!
54+
55+
Enjoy 🍻

examples/fib.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import ctypes
12
import dataclasses
3+
import platform
24
import struct
5+
import time
6+
37
import capstone
48
import keystone
59

6-
from typing import Any
7-
8-
import ctypes
9-
import time
1010
import bochscpu
1111

1212
DEBUG = True
@@ -27,7 +27,32 @@ def dbg(x: str):
2727
print(f"[Py] {x}")
2828

2929

30+
def mmap(sz: int = PAGE_SIZE, perm: str = "rw"):
31+
PROT_READ = 0x1
32+
PROT_WRITE = 0x2
33+
PROT_EXEC = 0x4
34+
MAP_PRIVATE = 0x2
35+
MAP_ANONYMOUS = 0x20
36+
libc = ctypes.CDLL("libc.so.6")
37+
mmap = libc.mmap
38+
mmap.restype = ctypes.c_void_p
39+
mmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_long]
40+
flags = 0
41+
match perm:
42+
case "ro":
43+
flags = PROT_READ
44+
case "rw":
45+
flags = PROT_READ | PROT_WRITE
46+
case "rwx":
47+
flags = PROT_READ | PROT_WRITE | PROT_EXECUTE
48+
case _:
49+
raise ValueError
50+
return mmap(-1, sz, flags, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
51+
52+
3053
def VirtualAlloc(sz: int = PAGE_SIZE, perm: str = "rw"):
54+
if platform.system() == "Linux":
55+
return mmap(sz, perm)
3156
MEM_COMMIT = 0x1000
3257
MEM_RESERVE = 0x2000
3358
PAGE_NOACCESS = 0x01
@@ -293,7 +318,7 @@ def emulate(code: bytes):
293318
push rbx
294319
push rax
295320
296-
cmp rcx, 0xffffff
321+
cmp rcx, 0xfffff
297322
jne loop
298323
299324
nop

0 commit comments

Comments
 (0)