堆模拟栈实现协程切换后栈内存异常修改问题求助
堆模拟栈实现协程切换时栈内存意外修改问题分析
问题背景
通过C++程序,用堆内存模拟栈空间,配合汇编指令操作寄存器完成协程切换,但切换到新栈后调用函数时,出现栈内存被意外修改的情况,已影响程序正常运行。目前不确定修改操作来自GCC还是操作系统,已知写入内容包含程序运行目录。
函数调用前GDB调试信息
(gdb) info frame Stack level 0, frame at 0x420520: rip = 0x7ffff7eaf3b6 in clsn::Coroutine::CoroutineFunc (/data/workspace/DeftRPC/src/coroutine/Coroutine.cpp:16); saved rip = 0x0 called by frame at 0x420528 source language c++. Arglist at 0x420510, args: arg=0x41ffd0 Locals at 0x420510, Previous frame's sp is 0x420520 Saved registers: rbp at 0x420510, rip at 0x420518
此时RIP(0x420518)附近内存状态:
函数调用后GDB调试信息
(gdb) info frame Stack level 0, frame at 0x4204f0: rip = 0x7ffff7eaf436 in clsn::Coroutine::operator() (/data/workspace/DeftRPC/src/coroutine/Coroutine.cpp:32); saved rip = 0x7ffff7eaf3c2 called by frame at 0x420520 source language c++. Arglist at 0x4204e0, args: this=0x41ffd0 Locals at 0x4204e0, Previous frame's sp is 0x4204f0 Saved registers: rbp at 0x4204e0, rip at 0x4204e8
RIP向低地址移动属于正常栈操作行为,当前RIP(0x4204e8)附近内存状态:
截至以上步骤,程序行为均符合预期。
异常内存修改情况
当前RIP低地址区域的栈内存出现非预期修改,内存变化状态如下:
内容的提问来源于stack exchange,提问作者qingfu liu
相关产品推荐
相关产品推荐

