diff --git a/src/core/ffi.c b/src/core/ffi.c index 81483b6a..7e791ab8 100644 --- a/src/core/ffi.c +++ b/src/core/ffi.c @@ -1344,6 +1344,15 @@ typedef double (win64_variant_f_ffif)(double, double, uint64_t, double); typedef double (win64_variant_f_fffi)(double, double, double, uint64_t); typedef double (win64_variant_f_ffff)(double, double, double, double); +/* MSVC stack frame runtime error checking (/RTCs) prepends alloca() allocations with an _RTC_ALLOCA_NODE + * header; misalligning stack-based FFI arguments and causing the memmove() (by stack_shift) to corrupt + * the _RTC_ALLOCA_NODE header. + * + * We turn off the RTC-instrumented alloca() and adding of _RTC_CheckStackVars to function prologue just + * for janet_ffi_win64() */ +#ifdef __MSVC_RUNTIME_CHECKS +#pragma runtime_checks( "s", off ) +#endif static Janet janet_ffi_win64(JanetFFISignature *signature, void *function_pointer, const Janet *argv) { union { uint64_t integer; @@ -1493,6 +1502,10 @@ static Janet janet_ffi_win64(JanetFFISignature *signature, void *function_pointe return janet_ffi_read_one(ret_mem, signature->ret.type, JANET_FFI_MAX_RECUR); } +#ifdef __MSVC_RUNTIME_CHECKS +// Restore stack frame runtime error checking (/RTCs) if it was enabled. +#pragma runtime_checks ( "s", restore ) +#endif #endif