【问题标题】:Is there any way to avoid terminating the program when stack smashing occurs?发生堆栈粉碎时,有什么方法可以避免终止程序?
【发布时间】:2017-03-25 19:28:43
【问题描述】:

我在 C++ 中使用 pthread 编写了一个具有 3 个线程的程序。当其中一个线程发生缓冲区溢出时,整个程序终止,其他线程无法运行,并显示此消息:*** stack smashing detected ***: ./a.out terminated
我想堆栈粉碎只会杀死其中发生 BOF 的线程,而其他线程保持活动状态。所以,我尝试忽略信号,但这并没有解决我的问题。
这是我的程序:

#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <signal.h>
#include <string.h>

using namespace std;

int a = 0;

void sig_func(int sig)
{
}

void *func (void *arg)
{
  int c = a++;
  cout << "start thread " << c << endl;
  if (c == 1)
  {
    char stuff[8];
    strcpy(stuff, "123456789");
  }
  cout << "end thread " << c << endl;
}

int main ()
{
  pthread_t tid1, tid2, tid3;
  for(int i = 1; i <=31 ; i++)  //this line ignores all signals from 1 to 31.
     signal(i,sig_func);
  pthread_create (&tid1, 0, func, 0);
  sleep (1);
  pthread_create (&tid2, 0, func, 0);
  sleep (1);
  pthread_create (&tid3, 0, func, 0);
  sleep (1);
  return 0;
}

当我用g++ a.cpp -lpthread编译它时,输出是这样的:

start thread 0
end thread 0
start thread 1
end thread 1
*** stack smashing detected ***: ./a.out terminated
Aborted (core dumped)

有没有什么办法可以把堆栈粉碎只导致杀死其中发生BOF的线程,而程序不终止?
请注意,我不想使用 -fno-stack-protector 选项编译我的程序以避免受到金丝雀的保护。

【问题讨论】:

  • 我很好奇你为什么故意要发送 smashes its stack 的代码(溢出它的缓冲区)并调用 UB。
  • 当你导致未定义的行为时,无法控制会发生什么。

标签: c++ multithreading pthreads stack-smash


【解决方案1】:

有什么方法可以避免在堆栈粉碎时终止程序?

没有

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多