Consider the following code:
import ctypes
IPC_PRIVATE, MAP_SIZE, IPC_CREAT, IPC_EXCL = 0, 65536, 512, 1024
shmget = ctypes.cdll.LoadLibrary("libc.so.6").shmget
shmat = ctypes.cdll.LoadLibrary("libc.so.6").shmat
shm_id = shmget(IPC_PRIVATE, MAP_SIZE, IPC_CREAT | IPC_EXCL | 0600)
trace_bits = shmat(shm_id, 0, 0)
s = ctypes.string_at(ctypes.c_void_p(trace_bits), 1)
print(s[0])
When I try to run it, it gives me a "segmentation fault" after a successful run of shmat. What am I doing wrong?
By default, all ctypes functions which are wrapped like this have restype == c_int. So you need to set it correctly before you call it. The same is for argtypes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With