opened
dreiss
username_0
I just watched one of Robert's presentations, and he mentioned that to replay asynchronous signals at the right time, you count conditional branches retired and record the full set of registers, to be compared during replay.  This will fail if the code is running in a loop that has no conditional branches, and all register values are periodic.  The example below uses an infinite loop (unconditional branch) and an in-place increment instruction (doesn't copy the data value into any registers).  When replaying this program, the signal is generated on a very early iteration of the loop, and the output value is not the same as when the original program was run.


```c
#include <unistd.h>
#include <signal.h>

// Value that will be incremented in the main program
// and printed from the signal handler.
long value;

static void alarm_handler(int sig) {
  // Convert the value to a human-readable form.
  // The details are not important, but we must only use async-signal-safe
  // operations, and we must have a fixed number of conditional branches.
  long copy = value;
  char buf[64];
  int idx;
  for (idx = 0; idx < 16; idx++) {
    buf[idx] = 'a' + (copy & 0xf);
    copy >>= 4;
  }
  buf[idx++] = '\n';
  // Write out to the user and exit.
  ssize_t ret = write(STDOUT_FILENO, buf, idx);
  _exit(ret != idx);
}

int main(int argc, char* argv[]) {
  // Request an async signal after 1 second.
  signal(SIGALRM, alarm_handler);
  alarm(1);

  // Infinite loop.  No conditional branches.
  for (;;) {
    // This does not need to be an atomic operation,
    // but that was the easiest way to get gcc to emit code
    // that would add to value without changing register values.
    // Making value volatile resulted in a read-modify-write
    // (which leaves the value in a register),
    // and making this a normal increment resulted in
    // the increment being elided entirely.
    __sync_fetch_and_add(&value, 1);
  }

  return 0;
}
```
Divergence with infinite loops and async signals
issue
created
rocallahan
username_1
You're right, this is a known limitation of rr's approach. Fortunately this
doesn't seem to cause problems in practice.
comment
closed
rocallahan
username_1
issue
created
rocallahan
username_1
I'm going to close this because this is basically a design limitation of rr that doesn't seem to cause problems in practice.
comment