使用 ctypes 模块和 GDB 会话更改内存中的 Python 整数
Changing Python integer in memory using ctypes module and GDB session
我的问题是基于 this reddit post。那里的示例显示了如何使用 ctypes
模块中的 cast
函数更改内存中的整数:
>>> import ctypes
>>> ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[3] = 100
>>> 29
100
我对这里的底层内部结构很感兴趣,我已经在 GDB 会话中通过在 CPython
:
中的 cast
函数上设置断点来检查了这一点
(gdb) break cast
Function "cast" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (cast) pending.
(gdb) run test.py
Starting program: /root/.pyenv/versions/3.8.0-debug/bin/python test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
0x7ffff00e7b40
Breakpoint 1, cast (ptr=0x9e6e40 <small_ints+1088>, src=10382912, ctype=<_ctypes.PyCPointerType at remote 0xa812a0>) at /root/.pyenv/sources/3.8.0-debug/Python-3.8.0/Modules/_ctypes/_ctypes.c:5540
5540 if (0 == cast_check_pointertype(ctype))
(gdb) p *(PyLongObject *) ptr
= {
ob_base = {
ob_base = {
ob_refcnt = 12,
ob_type = 0x9b8060 <PyLong_Type>
},
ob_size = 1
},
ob_digit = {100}
}
(gdb) p *((long *) ptr + 3)
= 100
(gdb) p ((long *) ptr + 3)
= (long *) 0x9e6e58 <small_ints+1112>
(gdb) p *((char *) ptr + 3 * 8)
= 100 'd'
(gdb) p ((char *) ptr + 3 * 8)
= 0x9e6e58 <small_ints+1112> "d"
(gdb) set *((long *) ptr + 3) = 29
(gdb) p *((long *) ptr + 3)
= 29
(gdb) p *((char *) ptr + 3 * 8)
= 29 '5'
我想知道是否可以在 GDB 会话中使用 Python 获取内存地址,因为我无法访问返回的地址:
(gdb) python print("{:#x}".format(ctypes.addressof(ctypes.c_int(29))))
0x7f1053c947f0
(gdb) python print("{:#x}".format(id(29)))
0x22699d8
(gdb) p *0x7f1053c947f0
Cannot access memory at address 0x7f1053c947f0
(gdb) p *0x22699d8
Cannot access memory at address 0x22699d8
索引也与 Python REPL 不同,我猜这与字节顺序有关?
(gdb) python print(ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[3])
9
(gdb) python print (ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[2])
29
问题:
- 为什么 GDB 会话中 Python 的内存地址不可访问,值不在进程内存范围内 (
info proc mappings
)?
- 为什么索引与 Python REPL 不同?
- (奖金问题)我希望
CPython
cast
函数中的 src
参数保存对象的地址,但它似乎是 ptr
在 memcpy 之后 result->b_ptr
指向与 &ptr
不同的值?这是实际的演员表吗?
- 您的 Python 进程不是真正的 python 进程,相反,GDB 是 运行 一个 Python REPL。把它想象成 GDB 内部的另一个线程。当然,这是一个简化,你应该看到the docs
- 我无法重现此行为:
(gdb) python
>import ctypes
>print(ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[3])
>end
29
我想不出会发生这种行为的任何原因(最不重要的是字节顺序,这在整个系统中都是相同的*)
src
参数似乎用作原始类型,而不是原始对象。有关参考,请参阅 ctypes.h and ctypes/__init__.py(_SimpleCData 只是 CDataObject,带有一些帮助程序,如索引和 repr)。是的,在这种情况下,memcpy 是实际转换的对象,但如果您在两种数据类型之间进行转换,则需要事先进行额外的工作。
* 除了在 ARM 上,您可以在其中使用指令更改字节顺序
我的问题是基于 this reddit post。那里的示例显示了如何使用 ctypes
模块中的 cast
函数更改内存中的整数:
>>> import ctypes
>>> ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[3] = 100
>>> 29
100
我对这里的底层内部结构很感兴趣,我已经在 GDB 会话中通过在 CPython
:
cast
函数上设置断点来检查了这一点
(gdb) break cast
Function "cast" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (cast) pending.
(gdb) run test.py
Starting program: /root/.pyenv/versions/3.8.0-debug/bin/python test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
0x7ffff00e7b40
Breakpoint 1, cast (ptr=0x9e6e40 <small_ints+1088>, src=10382912, ctype=<_ctypes.PyCPointerType at remote 0xa812a0>) at /root/.pyenv/sources/3.8.0-debug/Python-3.8.0/Modules/_ctypes/_ctypes.c:5540
5540 if (0 == cast_check_pointertype(ctype))
(gdb) p *(PyLongObject *) ptr
= {
ob_base = {
ob_base = {
ob_refcnt = 12,
ob_type = 0x9b8060 <PyLong_Type>
},
ob_size = 1
},
ob_digit = {100}
}
(gdb) p *((long *) ptr + 3)
= 100
(gdb) p ((long *) ptr + 3)
= (long *) 0x9e6e58 <small_ints+1112>
(gdb) p *((char *) ptr + 3 * 8)
= 100 'd'
(gdb) p ((char *) ptr + 3 * 8)
= 0x9e6e58 <small_ints+1112> "d"
(gdb) set *((long *) ptr + 3) = 29
(gdb) p *((long *) ptr + 3)
= 29
(gdb) p *((char *) ptr + 3 * 8)
= 29 '5'
我想知道是否可以在 GDB 会话中使用 Python 获取内存地址,因为我无法访问返回的地址:
(gdb) python print("{:#x}".format(ctypes.addressof(ctypes.c_int(29))))
0x7f1053c947f0
(gdb) python print("{:#x}".format(id(29)))
0x22699d8
(gdb) p *0x7f1053c947f0
Cannot access memory at address 0x7f1053c947f0
(gdb) p *0x22699d8
Cannot access memory at address 0x22699d8
索引也与 Python REPL 不同,我猜这与字节顺序有关?
(gdb) python print(ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[3])
9
(gdb) python print (ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[2])
29
问题:
- 为什么 GDB 会话中 Python 的内存地址不可访问,值不在进程内存范围内 (
info proc mappings
)? - 为什么索引与 Python REPL 不同?
- (奖金问题)我希望
CPython
cast
函数中的src
参数保存对象的地址,但它似乎是ptr
在 memcpy 之后result->b_ptr
指向与&ptr
不同的值?这是实际的演员表吗?
- 您的 Python 进程不是真正的 python 进程,相反,GDB 是 运行 一个 Python REPL。把它想象成 GDB 内部的另一个线程。当然,这是一个简化,你应该看到the docs
- 我无法重现此行为:
我想不出会发生这种行为的任何原因(最不重要的是字节顺序,这在整个系统中都是相同的*)(gdb) python >import ctypes >print(ctypes.cast(id(29), ctypes.POINTER(ctypes.c_long))[3]) >end 29
src
参数似乎用作原始类型,而不是原始对象。有关参考,请参阅 ctypes.h and ctypes/__init__.py(_SimpleCData 只是 CDataObject,带有一些帮助程序,如索引和 repr)。是的,在这种情况下,memcpy 是实际转换的对象,但如果您在两种数据类型之间进行转换,则需要事先进行额外的工作。
* 除了在 ARM 上,您可以在其中使用指令更改字节顺序