lks9 / src-tracer

Other
0 stars 0 forks source link

Missing default in switch causes ambiguous trace #58

Closed lks9 closed 4 months ago

lks9 commented 4 months ago

Example

switch (i) {
  case 0:
    a = 1;
}
if ( b ) {
  // do nothing 
} else if ( q ) {
  a = 0;
}

Instrumented

_SWITCH_START(sw0, 1) switch (i) {
  case 0: _CASE(0, sw0, 1)
    a = 1;
}
if ( b ) { _IF
  // do nothing 
} else { _ELSE if ( q ) { _IF
  a = 0;
} else { _ELSE } }

We don't know whether NT means that we go into case 0 of switch and into the if-branch, or that we skip the switch (nothing is traced there), go into else then if (the else-if-branch).

Solution is to add a default branch when instrumenting:

_SWITCH_START(sw0, 1) switch (i) {
  case 0: _CASE(0, sw0, 1)
    a = 1;
break; default: _CASE(1, sw0, 1) }
if ( b ) { _IF
  // do nothing 
} else { _ELSE if ( q ) { _IF
  a = 0;
} else { _ELSE } }

Then the trace from above would be either NT or TNT.