From 9aff4e63975ab09825e2a37fc333913ec4bb6dc7 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sun, 14 Jun 2020 17:00:02 +0200 Subject: [PATCH] callback.c: register the bytecode fragment used for callbacks So that the return address pushed on the bytecode interpreter stack is correctly recognized as a code pointer. --- runtime/callback.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/runtime/callback.c b/runtime/callback.c index 719363741a6d..aec4fe885d87 100644 --- a/runtime/callback.c +++ b/runtime/callback.c @@ -28,6 +28,7 @@ /* Bytecode callbacks */ +#include "caml/codefrag.h" #include "caml/interp.h" #include "caml/instruct.h" #include "caml/fix_code.h" @@ -37,25 +38,20 @@ CAMLexport int caml_callback_depth = 0; #ifndef LOCAL_CALLBACK_BYTECODE static opcode_t callback_code[] = { ACC, 0, APPLY, 0, POP, 1, STOP }; -#endif - -#ifdef THREADED_CODE +static int callback_code_inited = 0; -static int callback_code_threaded = 0; - -static void thread_callback(void) +static void init_callback_code(void) { + caml_register_code_fragment((char *) callback_code, + (char *) callback_code + sizeof(callback_code), + DIGEST_IGNORE, NULL); +#ifdef THREADED_CODE caml_thread_code(callback_code, sizeof(callback_code)); - callback_code_threaded = 1; +#endif + callback_code_inited = 1; } -#define Init_callback() if (!callback_code_threaded) thread_callback() - -#else - -#define Init_callback() - #endif CAMLexport value caml_callbackN_exn(value closure, int narg, value args[]) @@ -79,7 +75,7 @@ CAMLexport value caml_callbackN_exn(value closure, int narg, value args[]) Caml_state->extern_sp[narg + 1] = Val_unit; /* environment */ Caml_state->extern_sp[narg + 2] = Val_long(0); /* extra args */ Caml_state->extern_sp[narg + 3] = closure; - Init_callback(); + if (!callback_code_inited) init_callback_code(); callback_code[1] = narg + 3; callback_code[3] = narg; res = caml_interprete(callback_code, sizeof(callback_code)); @@ -96,6 +92,8 @@ CAMLexport value caml_callbackN_exn(value closure, int narg, value args[]) local_callback_code[4] = POP; local_callback_code[5] = 1; local_callback_code[6] = STOP; + /* Not registering the code fragment, as code fragment management + would need to be revised thoroughly for an hypothetical JIT */ #ifdef THREADED_CODE caml_thread_code(local_callback_code, sizeof(local_callback_code)); #endif /*THREADED_CODE*/