指针值可以通过将其传递给另一个函数来改变吗?
Can a pointer value change just by passing it to another function?
我正在使用 GDB 调试函数 A,在调用函数 B 之前,我打印了以下值:
p *(key->s->fds[3].handler)
结果是:(这些都是函数指针)
= {handle_read = 0x55555555ba49 <socks5_read>, handle_write = 0x55555555baa2 <socks5_write>, handle_block = 0x55555555bb0a <socks5_block>, handle_close = 0x55555555bb15 <socks5_close>}
然后,我键入 s
以使用 GDB 进入函数 B。
紧接着,我再次打印 p *(key->s->fds[3].handler)
,但它的值是:
= {handle_read = 0x7fffffffdb90, handle_write = 0x5555baa2, handle_block = 0x55555555bb0a <socks5_block>, handle_close = 0x55555555bb15 <socks5_close>}
我知道这个问题真的很开放,因为我应该展示我的一些代码。但我的问题是一般性的:我的结构的内容是否有可能仅仅通过传递一个指向函数的指针而改变?
如果有帮助,这是我在 GDB 中所做的完整跟踪:
(gdb) s
31 stm->current->on_arrival(stm->current->state, key);
(gdb) p *(key->s->fds[3].handler)
= {handle_read = 0x55555555ba49 <socks5_read>, handle_write = 0x55555555baa2 <socks5_write>, handle_block = 0x55555555bb0a <socks5_block>,
handle_close = 0x55555555bb15 <socks5_close>}
(gdb) s
hello_read_init (state=0, key=0x7fffffffdb90) at src/stm/stm_hello.c:20
20 hello_stm *hello_stm = &ATTACHMENT(key)->hello_state;
(gdb) p *(key->s->fds[3].handler)
= {handle_read = 0x7fffffffdb90, handle_write = 0x5555baa2, handle_block = 0x55555555bb0a <socks5_block>,
handle_close = 0x55555555bb15 <socks5_close>}
Is it possible that the contents of my struct change just by passing a pointer to a function?
我能想到 3 个可能的原因(可能性从大到小):
- 您的程序具有 stack use after scope 种类的未定义行为。
即如果key->s->fds[3].handler
指向一个已经返回的帧的栈,则该栈会被后续的调用指令覆盖。
- 你
step
还不够。
如果被调用的函数被优化,GDB 有可能在 hello_read_init
序言(尚未设置寄存器的区域)停止。
- 您有一个多线程程序和数据竞争(另一个线程覆盖
key->s->fds[3].handler->handle_read
而第一个线程使用它)。
使用 Address Sanitizer 应该会立即显示这是否是 UB 的实例(上述第一种可能性)。
我正在使用 GDB 调试函数 A,在调用函数 B 之前,我打印了以下值:
p *(key->s->fds[3].handler)
结果是:(这些都是函数指针)
= {handle_read = 0x55555555ba49 <socks5_read>, handle_write = 0x55555555baa2 <socks5_write>, handle_block = 0x55555555bb0a <socks5_block>, handle_close = 0x55555555bb15 <socks5_close>}
然后,我键入 s
以使用 GDB 进入函数 B。
紧接着,我再次打印 p *(key->s->fds[3].handler)
,但它的值是:
= {handle_read = 0x7fffffffdb90, handle_write = 0x5555baa2, handle_block = 0x55555555bb0a <socks5_block>, handle_close = 0x55555555bb15 <socks5_close>}
我知道这个问题真的很开放,因为我应该展示我的一些代码。但我的问题是一般性的:我的结构的内容是否有可能仅仅通过传递一个指向函数的指针而改变?
如果有帮助,这是我在 GDB 中所做的完整跟踪:
(gdb) s
31 stm->current->on_arrival(stm->current->state, key);
(gdb) p *(key->s->fds[3].handler)
= {handle_read = 0x55555555ba49 <socks5_read>, handle_write = 0x55555555baa2 <socks5_write>, handle_block = 0x55555555bb0a <socks5_block>,
handle_close = 0x55555555bb15 <socks5_close>}
(gdb) s
hello_read_init (state=0, key=0x7fffffffdb90) at src/stm/stm_hello.c:20
20 hello_stm *hello_stm = &ATTACHMENT(key)->hello_state;
(gdb) p *(key->s->fds[3].handler)
= {handle_read = 0x7fffffffdb90, handle_write = 0x5555baa2, handle_block = 0x55555555bb0a <socks5_block>,
handle_close = 0x55555555bb15 <socks5_close>}
Is it possible that the contents of my struct change just by passing a pointer to a function?
我能想到 3 个可能的原因(可能性从大到小):
- 您的程序具有 stack use after scope 种类的未定义行为。
即如果key->s->fds[3].handler
指向一个已经返回的帧的栈,则该栈会被后续的调用指令覆盖。 - 你
step
还不够。
如果被调用的函数被优化,GDB 有可能在hello_read_init
序言(尚未设置寄存器的区域)停止。 - 您有一个多线程程序和数据竞争(另一个线程覆盖
key->s->fds[3].handler->handle_read
而第一个线程使用它)。
使用 Address Sanitizer 应该会立即显示这是否是 UB 的实例(上述第一种可能性)。