What are the scenarios where a process gets a SIGABRT in C++? Does this signal always come from within the process or can this signal be sent from one process to another?
Is there a way to identify which process is sending this signal?
What are the scenarios where a process gets a SIGABRT in C++? Does this signal always come from within the process or can this signal be sent from one process to another?
Is there a way to identify which process is sending this signal?
abort()
sends the calling process the SIGABRT
signal, this is how abort()
basically works.
abort()
is usually called by library functions which detect an internal error or some seriously broken constraint. For example malloc()
will call abort()
if its internal structures are damaged by a heap overflow.
You can send any signal to any process using the kill(2)
interface:
kill -SIGABRT 30823
30823 was a dash
process I started, so I could easily find the process I wanted to kill.
$ /bin/dash
$ Aborted
The Aborted
output is apparently how dash
reports a SIGABRT.
It can be sent directly to any process using kill(2)
, or a process can send the signal to itself via assert(3)
, abort(3)
, or raise(3)
.
SIGABRT
is commonly used by libc and other libraries to abort the program in case of critical errors. For example, glibc sends an SIGABRT
in case of a detected double-free or other heap corruptions.
Also, most assert
implementations make use of SIGABRT
in case of a failed assert.
Furthermore, SIGABRT
can be sent from any other process like any other signal. Of course, the sending process needs to run as same user or root.
It usually happens when there is a problem with memory allocation.
It happened to me when my program was trying to allocate an array with negative size.
There's another simple cause in case of c++.
std::thread::~thread{
if((joinable ())
std::terminate ();
}
i.e. scope of thread ended but you forgot to call either
thread::join();
or
thread::detach();
The GNU libc will print out information to /dev/tty
regarding some fatal conditions before it calls abort()
(which then triggers SIGABRT
), but if you are running your program as a service or otherwise not in a real terminal window, these message can get lost, because there is no tty to display the messages.
See my post on redirecting libc to write to stderr instead of /dev/tty:
Catching libc error messages, redirecting from /dev/tty
A case when process get SIGABRT from itself: Hrvoje mentioned about a buried pure virtual being called from ctor generating an abort, i recreated an example for this. Here when d is to be constructed, it first calls its base class A ctor, and passes inside pointer to itself. the A ctor calls pure virtual method before table was filled with valid pointer, because d is not constructed yet.
#include<iostream>
using namespace std;
class A {
public:
A(A *pa){pa->f();}
virtual void f()=0;
};
class D : public A {
public:
D():A(this){}
virtual void f() {cout<<"D::f\n";}
};
int main(){
D d;
A *pa = &d;
pa->f();
return 0;
}
compile: g++ -o aa aa.cpp
ulimit -c unlimited
run: ./aa
pure virtual method called
terminate called without an active exception
Aborted (core dumped)
now lets quickly see the core file, and validate that SIGABRT was indeed called:
gdb aa core
see regs:
i r
rdx 0x6 6
rsi 0x69a 1690
rdi 0x69a 1690
rip 0x7feae3170c37
check code:
disas 0x7feae3170c37
mov $0xea,%eax = 234 <- this is the kill syscall, sends signal to process
syscall <-----
http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/
234 sys_tgkill pid_t tgid pid_t pid int sig = 6 = SIGABRT
:)
In my case, it was due to an input in an array at an index equal to the length of the array.
string x[5];
for(int i=1; i<=5; i++){
cin>>x[i];
}
x[5] is being accessed which is not present.