Add an optional watchdog timer (set vm.watchdog and build with -DENABLE_WATCHDOG)

This commit is contained in:
K. Lange 2021-01-06 10:40:23 +09:00
parent 3cc8e8ef79
commit 3c6b45709a
2 changed files with 16 additions and 0 deletions

15
vm.c
View File

@ -2363,6 +2363,7 @@ void krk_initVM(int flags) {
krk_initTable(&vm.globals);
krk_initTable(&vm.strings);
memset(vm.specialMethodNames,0,sizeof(vm.specialMethodNames));
vm.watchdog = 0;
/* To make lookup faster, store these so we can don't have to keep boxing
* and unboxing, copying/hashing etc. */
@ -2968,6 +2969,20 @@ static KrkValue run() {
}
#endif
#ifdef ENABLE_WATCHDOG
if (vm.watchdog - 1 == 0) {
fprintf(stderr, "Watchdog timer tripped.\n\n");
krk_dumpTraceback();
krk_resetStack();
fprintf(stderr, "\n\n");
vm.watchdog = 0;
exit(0);
return NONE_VAL();
} else if (vm.watchdog > 0) {
vm.watchdog--;
}
#endif
uint8_t opcode = READ_BYTE();
/* We split the instruction opcode table in half and use the top bit

1
vm.h
View File

@ -97,6 +97,7 @@ typedef struct {
KrkValue currentException;
int flags;
long watchdog;
} KrkVM;
#define KRK_ENABLE_TRACING (1 << 0)