Cosa può causare un errore di segmentazione quando si entra semplicemente in una funzione?SIGSEGV quando si immette una funzione
La funzione è entrata assomiglia:
21: void eesu3(Matrix & iQ)
22: {
dove Matrix
è un struct
. Quando si esegue con GDB il backtrace produce:
(gdb) backtrace
#0 eesu3 (iQ=...) at /home/.../eesu3.cc:22
#1 ...
GDB non dice che cosa è iQ
. I ...
sono letteralmente lì. Cosa potrebbe causare questo?
GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
programma costruito con -O3 -g
Il chiamante va come:
Matrix q;
// do some stuff with q
eesu3(q);
Niente di speciale qui
Riesegui il programma con valgrind:
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes <prgname>
uscita:
==2240== Warning: client switching stacks? SP change: 0x7fef7ef68 --> 0x7fe5e3000
==2240== to suppress, use: --max-stackframe=10076008 or greater
==2240== Invalid write of size 8
==2240== at 0x14C765B: eesu3(Matrix &) (eesu3.cc:22)
...
==2240== Address 0x7fe5e3fd8 is on thread 1's stack
==2240==
==2240== Can't extend stack to 0x7fe5e2420 during signal delivery for thread 1:
==2240== no stack segment
==2240==
==2240== Process terminating with default action of signal 11 (SIGSEGV)
==2240== Access not within mapped region at address 0x7FE5E2420
==2240== at 0x14C765B: eesu3(Matrix&) (eesu3.cc:22)
==2240== If you believe this happened as a result of a stack
==2240== overflow in your program's main thread (unlikely but
==2240== possible), you can try to increase the size of the
==2240== main thread stack using the --main-stacksize= flag.
==2240== The main thread stack size used in this run was 8388608.
Sembra proprio uno stack danneggiato.
Dump of assembler code for function eesu3(Matrix &):
0x00000000014c7640 <+0>: push %rbp
0x00000000014c7641 <+1>: mov %rsp,%rbp
0x00000000014c7644 <+4>: push %r15
0x00000000014c7646 <+6>: push %r14
0x00000000014c7648 <+8>: push %r13
0x00000000014c764a <+10>: push %r12
0x00000000014c764c <+12>: push %rbx
0x00000000014c764d <+13>: and $0xfffffffffffff000,%rsp
0x00000000014c7654 <+20>: sub $0x99b000,%rsp
=> 0x00000000014c765b <+27>: mov %rdi,0xfd8(%rsp)
Va bene, per essere chiari: i dati di Matrix sono disponibili. Praticamente contiene un puntatore ai dati. La struttura è piccola, 32 byte. (Appena controllato)
Ora, ho ricostruito il programma con diverse opzioni di ottimizzazione:
-O0
: l'errore non mostra.
-O1
: l'errore non viene visualizzato.
-O3
: l'errore non viene visualizzato.
--update
-O3 -fno-inline -fno-inline-functions
: l'errore non mostra.
Questo lo spiega. Troppi inline nella funzione hanno portato a un eccessivo utilizzo dello stack.
Il problema era dovuto a un overflow dello stack
Per controllare le variabili, ecc. Non ottimizzare. Compilare con '-O0 -g' – RageD
Corruzione dello stack? – Benj
Ok, ricostruirà con '-O0 -g'. Ci vuole un po 'di tempo – ritter