EricDallal
January 22nd, 2013, 12:52 AM
Hello,
I am trying to compile a program with the -O flag but the program crashes with a segfault whenever I compile with any of -O1, -O2, or -O3. I compiled the program with -Wall -ansi and -Wextra and obtain no warnings. The program works correctly without any optimization flags.
I have also tried adding the -g option and running the program in gdb. The program crashes in the following code segment:
int GetURDFS(FSM *fsm, ES *es, int K, EL el, IS &ur, int as) {
const int x1 = as % (fsm->n);
int n1 = as/(fsm->n) - 1;
int x2, n2;
int ind;
for (int i = 0, e = 1; i < fsm->ne; i++, e <<= 1) {
if ((e & (es->yes)) || (e & el)) continue; /* Observable events. */
if (((fsm->t)[x1]).find(e) == ((fsm->t)[x1]).end()) continue; /* Event e cannot occur from state x. */
x2 = ((fsm->t)[x1])[e]; /* Next state for a transition from state x on event e. */
if (n1 >= K) return 0;
if (e & (es->fault)) n2 = n1 + 1; /* Fault event. */
else n2 = (n1 == -1 ? -1 : n1 + 1); /* Must be unobservable non-faulty event. */
ind = (n2 + 1)*(fsm->n) + x2;
if (!(ur[ind/BITS] & (1 << (ind % BITS)))) {
ur[ind/BITS] += (1 << (ind % BITS));
if (!GetURDFS(fsm, es, K, el, ur, ind)) return 0; /* Diagnosability violation occured. */
}
}
return 1;
}
The error caught by gdb is:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401843 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::find (fsm=0x60a2e0, es=0x60a300, K=<value optimized out>, el=0, ur=..., as=4)
at /usr/include/c++/4.4/bits/stl_map.h:659
659 { return _M_t.find(__x); }
The back trace is:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000404667 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::find (this=0x609188,
__k=@0x7fffff5ff04c) at /usr/include/c++/4.4/bits/stl_tree.h:1421
1421 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
(gdb) bt
#0 0x0000000000404667 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::find (this=0x609188,
__k=@0x7fffff5ff04c) at /usr/include/c++/4.4/bits/stl_tree.h:1421
#1 0x0000000000401848 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::find (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0, ur=..., as=7)
at /usr/include/c++/4.4/bits/stl_map.h:659
#2 GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0, ur=..., as=7) at MPO.cpp:268
#3 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#4 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#5 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#6 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#7 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#8 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#9 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#10 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
---Type <return> to continue, or q <return> to quit---q
printing x1 from gdb gives me:
(gdb) print x1
$1 = 6328720
but printing as % (fsm->n) from gdb gives me:
(gdb) print as % (fsm->n)
$4 = 7
How is this possible?
I am trying to compile a program with the -O flag but the program crashes with a segfault whenever I compile with any of -O1, -O2, or -O3. I compiled the program with -Wall -ansi and -Wextra and obtain no warnings. The program works correctly without any optimization flags.
I have also tried adding the -g option and running the program in gdb. The program crashes in the following code segment:
int GetURDFS(FSM *fsm, ES *es, int K, EL el, IS &ur, int as) {
const int x1 = as % (fsm->n);
int n1 = as/(fsm->n) - 1;
int x2, n2;
int ind;
for (int i = 0, e = 1; i < fsm->ne; i++, e <<= 1) {
if ((e & (es->yes)) || (e & el)) continue; /* Observable events. */
if (((fsm->t)[x1]).find(e) == ((fsm->t)[x1]).end()) continue; /* Event e cannot occur from state x. */
x2 = ((fsm->t)[x1])[e]; /* Next state for a transition from state x on event e. */
if (n1 >= K) return 0;
if (e & (es->fault)) n2 = n1 + 1; /* Fault event. */
else n2 = (n1 == -1 ? -1 : n1 + 1); /* Must be unobservable non-faulty event. */
ind = (n2 + 1)*(fsm->n) + x2;
if (!(ur[ind/BITS] & (1 << (ind % BITS)))) {
ur[ind/BITS] += (1 << (ind % BITS));
if (!GetURDFS(fsm, es, K, el, ur, ind)) return 0; /* Diagnosability violation occured. */
}
}
return 1;
}
The error caught by gdb is:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401843 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::find (fsm=0x60a2e0, es=0x60a300, K=<value optimized out>, el=0, ur=..., as=4)
at /usr/include/c++/4.4/bits/stl_map.h:659
659 { return _M_t.find(__x); }
The back trace is:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000404667 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::find (this=0x609188,
__k=@0x7fffff5ff04c) at /usr/include/c++/4.4/bits/stl_tree.h:1421
1421 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
(gdb) bt
#0 0x0000000000404667 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::find (this=0x609188,
__k=@0x7fffff5ff04c) at /usr/include/c++/4.4/bits/stl_tree.h:1421
#1 0x0000000000401848 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::find (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0, ur=..., as=7)
at /usr/include/c++/4.4/bits/stl_map.h:659
#2 GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0, ur=..., as=7) at MPO.cpp:268
#3 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#4 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#5 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#6 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#7 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#8 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#9 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
#10 0x000000000040191c in GetURDFS (fsm=0x609010, es=0x60a300, K=<value optimized out>, el=0,
ur=<value optimized out>, as=<value optimized out>) at MPO.cpp:276
---Type <return> to continue, or q <return> to quit---q
printing x1 from gdb gives me:
(gdb) print x1
$1 = 6328720
but printing as % (fsm->n) from gdb gives me:
(gdb) print as % (fsm->n)
$4 = 7
How is this possible?