feat: add infinite loop control args
This commit is contained in:
+18
-16
@@ -14,10 +14,7 @@
|
||||
#include <vm/fzl_vm_object.h>
|
||||
|
||||
char last_err[256];
|
||||
size_t last_jmp_ip = 0;
|
||||
|
||||
// NOTE: add `--max-iter` option for limit
|
||||
size_t max_iter_limit = 100000;
|
||||
size_t last_jmp_ip = 0;
|
||||
size_t total_jmp_on_same = 0;
|
||||
|
||||
static char *expect_hdr = "FZL\0\1";
|
||||
@@ -279,7 +276,7 @@ void print_err_data(fzl_vm_t *vm)
|
||||
printf("\t> [%zd] %lu\n", i, vm->cstack[i]);
|
||||
}
|
||||
|
||||
int fzl_vm_run(fzl_vm_t *vm)
|
||||
int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter)
|
||||
{
|
||||
if (!vm)
|
||||
{
|
||||
@@ -673,19 +670,24 @@ int fzl_vm_run(fzl_vm_t *vm)
|
||||
"runtime error - expected JMP addr");
|
||||
return -1;
|
||||
}
|
||||
uint64_t jmp_ip = vm->ops[++vm->ip];
|
||||
if (jmp_ip == last_jmp_ip)
|
||||
total_jmp_on_same++;
|
||||
else
|
||||
total_jmp_on_same = 0;
|
||||
|
||||
if (total_jmp_on_same > max_iter_limit)
|
||||
uint64_t jmp_ip = vm->ops[++vm->ip];
|
||||
if (!allow_infinite)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"runtime error - exceeded max loop jumps (infinite loop?)\n"
|
||||
"If you thinks there's mistake, change jumps limit with "
|
||||
"--max-iter=N");
|
||||
return -1;
|
||||
if (jmp_ip == last_jmp_ip)
|
||||
total_jmp_on_same++;
|
||||
else
|
||||
total_jmp_on_same = 0;
|
||||
|
||||
if (total_jmp_on_same > max_iter)
|
||||
{
|
||||
snprintf(
|
||||
last_err, sizeof(last_err),
|
||||
"runtime error - exceeded max loop jumps (infinite loop?)\n"
|
||||
"If you thinks there's mistake, change jumps limit with "
|
||||
"--max-iter=N");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
vm->ip = jmp_ip - 1;
|
||||
|
||||
Reference in New Issue
Block a user