Skip to content

Commit c2cbfa4

Browse files
authored
Merge pull request #1676 from tsujamin/msvc-runtime-stack-check-ffi-crash
Disable MSVC runtime stack checks in janet_ffi_win64
2 parents b9bc89a + a856893 commit c2cbfa4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/core/ffi.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,15 @@ typedef double (win64_variant_f_ffif)(double, double, uint64_t, double);
13441344
typedef double (win64_variant_f_fffi)(double, double, double, uint64_t);
13451345
typedef double (win64_variant_f_ffff)(double, double, double, double);
13461346

1347+
/* MSVC stack frame runtime error checking (/RTCs) prepends alloca() allocations with an _RTC_ALLOCA_NODE
1348+
* header; misalligning stack-based FFI arguments and causing the memmove() (by stack_shift) to corrupt
1349+
* the _RTC_ALLOCA_NODE header.
1350+
*
1351+
* We turn off the RTC-instrumented alloca() and adding of _RTC_CheckStackVars to function prologue just
1352+
* for janet_ffi_win64() */
1353+
#ifdef __MSVC_RUNTIME_CHECKS
1354+
#pragma runtime_checks( "s", off )
1355+
#endif
13471356
static Janet janet_ffi_win64(JanetFFISignature *signature, void *function_pointer, const Janet *argv) {
13481357
union {
13491358
uint64_t integer;
@@ -1493,6 +1502,10 @@ static Janet janet_ffi_win64(JanetFFISignature *signature, void *function_pointe
14931502

14941503
return janet_ffi_read_one(ret_mem, signature->ret.type, JANET_FFI_MAX_RECUR);
14951504
}
1505+
#ifdef __MSVC_RUNTIME_CHECKS
1506+
// Restore stack frame runtime error checking (/RTCs) if it was enabled.
1507+
#pragma runtime_checks ( "s", restore )
1508+
#endif
14961509

14971510
#endif
14981511

0 commit comments

Comments
 (0)