morganstanley / hobbes

A language and an embedded JIT compiler
http://hobbes.readthedocs.io/
Apache License 2.0
1.16k stars 105 forks source link

SIGINT handler throws uncaught exception and might have UB #404

Open mo-xiaoming opened 3 years ago

mo-xiaoming commented 3 years ago

Setup

OS: macOS catalina LLVM: brew installed llvm: stable 12.0.0 Compiler: Apple clang version 12.0.0 (clang-1200.0.31.1) readline: brew installed readline: stable 8.1

hobbes was built with LLVM_DIR=/usr/local/Cellar/llvm/12.0.0_1/lib/cmake

Uncaught exception

$ ./hi
hi : an interactive shell for hobbes
      type ':h' for help on commands

> 3 + 5
8
> ^C libc++abi: terminating with uncaught exception of type hi::interruption_error: std::exception
Abort trap: 6

UB

Might be unrelated to the above problem, but the SIGINT handler might have some issues

[[noreturn]] void interruptEval(int) {
  siglongjmp(continueInterrupt, 42);
}

Although siglongjmp family can be used in C signal handling(reference), it is not among the list of C++ signal-safe functions(cppreference), which make it UB

siglongjmp jumps the execution to

void installInterruptHandler() {
  if (sigsetjmp(continueInterrupt,1) == 0) {
    signal(SIGINT, &interruptEval);
  } else if (!terminating) {
    throw interruption_error();
  } else {
    exit(-1);
  }
}

which throws interruption_error, and throwing in a signal handler is an UB as well

smunix commented 3 years ago

Agreed, throwing in a signal handler is an UB indeed. Do you mind pushing a pull request for review?