From 790a4f263613437ba87ce323053eb42f7161f422 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 17 May 2025 21:04:37 -0500 Subject: [PATCH] Make tests pass with clang sanitizers. Fix some issue with clang sanitizers, name -fsanitize=thread and -fsanitize=undefined. The threading issue arose from the implementation of ev/deadlock when allowing for interpreter intrerrupts, as this is implemented by racing a timeout thread with a worker thread. The undefined behavior issue arose in some very old code in corelib.c that will actually work as expected for most compilers, but was both undefined and unecessary as we have a correct implemenation in util.c. --- src/core/corelib.c | 13 +++---------- src/core/ev.c | 16 ++++++---------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/core/corelib.c b/src/core/corelib.c index 52d445f3..9c03f51c 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -653,22 +653,15 @@ JANET_CORE_FN(janet_core_check_int, "(int? x)", "Check if x can be exactly represented as a 32 bit signed two's complement integer.") { janet_fixarity(argc, 1); - if (!janet_checktype(argv[0], JANET_NUMBER)) goto ret_false; - double num = janet_unwrap_number(argv[0]); - return janet_wrap_boolean(num == (double)((int32_t)num)); -ret_false: - return janet_wrap_false(); + return janet_wrap_boolean(janet_checkint(argv[0])); } JANET_CORE_FN(janet_core_check_nat, "(nat? x)", "Check if x can be exactly represented as a non-negative 32 bit signed two's complement integer.") { janet_fixarity(argc, 1); - if (!janet_checktype(argv[0], JANET_NUMBER)) goto ret_false; - double num = janet_unwrap_number(argv[0]); - return janet_wrap_boolean(num >= 0 && (num == (double)((int32_t)num))); -ret_false: - return janet_wrap_false(); + if (!janet_checkint(argv[0])) return janet_wrap_false(); + return janet_wrap_boolean(janet_unwrap_integer(argv[0]) >= 0); } JANET_CORE_FN(janet_core_is_bytes, diff --git a/src/core/ev.c b/src/core/ev.c index d0fbca56..78fe96dd 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -671,11 +671,9 @@ static DWORD WINAPI janet_timeout_body(LPVOID ptr) { JanetThreadedTimeout tto = *(JanetThreadedTimeout *)ptr; janet_free(ptr); SleepEx((DWORD)(tto.sec * 1000), TRUE); - if (janet_fiber_can_resume(tto.fiber)) { - janet_interpreter_interrupt(tto.vm); - JanetEVGenericMessage msg = {0}; - janet_ev_post_event(tto.vm, janet_timeout_cb, msg); - } + janet_interpreter_interrupt(tto.vm); + JanetEVGenericMessage msg = {0}; + janet_ev_post_event(tto.vm, janet_timeout_cb, msg); return 0; } #else @@ -696,11 +694,9 @@ static void *janet_timeout_body(void *ptr) { ? (long)((tto.sec - ((uint32_t)tto.sec)) * 1000000000) : 0; nanosleep(&ts, &ts); - if (janet_fiber_can_resume(tto.fiber)) { - janet_interpreter_interrupt(tto.vm); - JanetEVGenericMessage msg = {0}; - janet_ev_post_event(tto.vm, janet_timeout_cb, msg); - } + janet_interpreter_interrupt(tto.vm); + JanetEVGenericMessage msg = {0}; + janet_ev_post_event(tto.vm, janet_timeout_cb, msg); return NULL; } #endif