【发布时间】:2020-02-06 22:06:10
【问题描述】:
我的目标很简单。我想使用 mmap 分配一个内存页,然后将 hello world 字符串复制到该内存页中。
但是我在libc.memcpy(pageLoc, temp, ctypes.c_int(len(temp.value))) 行遇到了分段错误
由于我还是 ctypes 的新手,我怀疑这与我滥用指针有关。谁能告诉我为什么会这样,我做错了什么?
import ctypes
# initialize the ctype library
libc = ctypes.CDLL("libc.so.6")
pthread = ctypes.CDLL("libpthread.so.0")
# size of pages on system
PAGESIZE = ctypes.c_int(libc.getpagesize())
# allocate the page
pageLoc = ctypes.c_void_p(libc.mmap(0, PAGESIZE, 0x7, 0x22, 0, 0)) # defined in https://sites.uclouvain.be/SystInfo/usr/include/bits/mman.h.html
print "[*] Created a page in memory at address %s" % hex(pageLoc.value)
# copy hello world into the memory space
temp = ctypes.c_char_p("Hello world!\n\x00")
print hex(ctypes.c_void_p(
libc.memcpy(pageLoc, temp, ctypes.c_int(len(temp.value)))
))
# free the page
libc.munmap(pageLoc, PAGESIZE)
【问题讨论】:
标签: python linux python-2.7 ctypes