分叉进程的零星堆栈指针分段错误
Sporadic stack pointer segmentation fault of forked process
我正在尝试调试一个进程,该进程偶尔会在访问堆栈指针时出现分段错误。
用于在 main() { 的第一行之前进行段错误的进程现在,段断层移到了另一个地方。
如果它没有崩溃,那么进程将无限期地继续正确运行。
我猜这与流程的实例化方式有关:
pid_t COsAbstraction::Start(const uint32_t coreId, const char* argv[])
{
pid_t pid;
sigset_t mask;
sigset_t save;
/*
* Block SIGCHLD
*/
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &mask, &save);
pid = fork();
if(pid < 0)
{
_exit(false);
}
if (pid != 0)
{
// I am the parent
pthread_sigmask(SIG_UNBLOCK, &save, NULL);
return pid;;
}
// I am the child
// Replace myself with the desired process
execvp(argv[ 0 ], const_cast<char**>(argv))
_exit(0);
}
崩溃代码(零星):
(gdb) disassemble
Dump of assembler code for function main:
0x0804c663 <main+0>: lea 0x4(%esp),%ecx
0x0804c667 <main+4>: and $0xfffffff0,%esp
0x0804c66a <main+7>: pushl -0x4(%ecx)
0x0804c66d <main+10>: push %ebp
0x0804c66e <main+11>: mov %esp,%ebp
0x0804c670 <main+13>: push %edi
0x0804c671 <main+14>: push %esi
0x0804c672 <main+15>: push %ebx
0x0804c673 <main+16>: push %ecx
0x0804c674 <main+17>: sub $0x525a8,%esp
0x0804c67a <main+23>: call 0x804a22e <__i686.get_pc_thunk.bx>
0x0804c67f <main+28>: add $0x6125,%ebx
0x0804c685 <main+34>: mov (%ecx),%eax
0x0804c687 <main+36>: mov %eax,-0x52578(%ebp)
0x0804c68d <main+42>: mov 0x4(%ecx),%ecx
0x0804c690 <main+45>: mov %ecx,-0x5257c(%ebp)
0x0804c696 <main+51>: movl $0x0,-0x418(%ebp)
0x0804c6a0 <main+61>: movl $0x400,0x8(%esp)
0x0804c6a8 <main+69>: movl $0x0,0x4(%esp)
0x0804c6b0 <main+77>: lea -0x410(%ebp),%eax
0x0804c6b6 <main+83>: mov %eax,(%esp)
0x0804c6b9 <main+86>: call 0x8049c4c <memset@plt>
(gdb) info registers
eax 0xf746d564 -146352796
ecx 0xf746d4e0 -146352928
edx 0xf746d500 -146352896
ebx 0x444a7ff4 1145733108
esp 0xf741af10 0xf741af10
ebp 0xf746d4c8 0xf746d4c8
esi 0x8050b30 134548272
edi 0x8049d90 134520208
eip 0x804c67a 0x804c67a <main+23>
eflags 0x210282 [ SF IF RF ID ]
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
此代码在 24 核 RedHat Linux 2.6 设备上运行。
更新:
事实证明,崩溃与调用过程无关!简单的"hello world"在从外壳中崩溃数百次时也会崩溃:
#include <stdio.h>
int main(int argc, char* argv[])
{
char a[100*1024];
a[0] = '1';
printf("Hello There!n");
return 0;
}
我应该提交一个新问题还是修改这个问题?
C 程序
崩溃,因为您正在退出程序堆栈。
关键是像这样的变量
char a[100*1024];
在堆栈上分配,它的大小是有限的,当您指向数组的开头时,如果进程,您将退出堆栈。
举个例子,我只是修改了代码,因为我使用的是 amd64,所以我有
#include <stdio.h>
int main(int argc, char* argv[])
{
char a[12*1024*1024];
a[0] = '1';
printf("Hello There!n");
return 0;
}
所以我正在尝试在堆栈中放置一个 12 MB 的变量。如您所见,堆栈的默认大小为 8MB,当我尝试执行它时,我得到这个:
ottavio@magritte:/tmp$ ulimit -s
8192
ottavio@magritte:/tmp$ ./x
Segmentation fault
但是如果我增加堆栈大小,它可以工作
ottavio@magritte:/tmp$ ulimit -s 16384
ottavio@magritte:/tmp$ ./x
Hello There!
ottavio@magritte:/tmp$
相关文章:
- 数组的指针从不分段故障
- 分段错误 - 读取初始化指针的数组
- 为什么在将 void 指针转换为整数指针时出现分段错误
- C++为什么我的指针选择排序中存在分段错误?
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 尝试将对象插入空指针数组时出现分段错误
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 在C++中,当指向删除和指向不同对象时,分段故障指针
- C++ 取消引用指向矢量的空指针时的分段错误
- 取消引用指针并立即为其分配变量,导致分段错误
- 如何避免指针超出范围(多态性)的C++分段错误
- 在指针迭代中使用for循环时出现分段错误
- 分配指针时出现分段错误
- C++:在push_back时迭代指针向量时出现分段错误
- 不断得到分段错误和free()无效指针
- 共享内存指针分段错误
- 分叉进程的零星堆栈指针分段错误
- 从线程原因解引用返回的指针;分段故障:11
- c++方法返回指针:分段错误
- C++中的指针;分段错误错误: