Skip to content

Commit

Permalink
Windows: enable nimRawSetjmp by default [backport] (#19891)
Browse files Browse the repository at this point in the history
* Windows: enable nimRawSetjmp by default

See #19197. The default setjmp can randomly segfault on windows

* Attempt to disable the flag for bootstraping

* Disable styleCheck for c_setjmp

(cherry picked from commit 251bdc1)
  • Loading branch information
Menduist authored and narimiran committed Jun 20, 2022
1 parent 7b35ce9 commit 843c479
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ cppDefine "NAN_INFINITY"
cppDefine "INF"
cppDefine "NAN"

when defined(windows) and not defined(booting):
# Avoid some rare stack corruption while using exceptions with a SEH-enabled
# toolchain: https://github.com/nim-lang/Nim/pull/19197
switch("define", "nimRawSetjmp")
7 changes: 7 additions & 0 deletions lib/system/ansi_c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ elif defined(nimBuiltinSetjmp):
proc c_builtin_setjmp(jmpb: ptr pointer): cint {.
importc: "__builtin_setjmp", nodecl.}
c_builtin_setjmp(unsafeAddr jmpb[0])

elif defined(nimRawSetjmp) and not defined(nimStdSetjmp):
when defined(windows):
# No `_longjmp()` on Windows.
Expand All @@ -124,10 +125,16 @@ elif defined(nimRawSetjmp) and not defined(nimStdSetjmp):
# prone to stack corruption during unwinding, so we disable that by setting
# it to NULL.
# More details: https://github.com/status-im/nimbus-eth2/issues/3121
when defined(nimHasStyleChecks):
{.push styleChecks: off.}

proc c_setjmp*(jmpb: C_JmpBuf): cint =
proc c_setjmp_win(jmpb: C_JmpBuf, ctx: pointer): cint {.
header: "<setjmp.h>", importc: "_setjmp".}
c_setjmp_win(jmpb, nil)

when defined(nimHasStyleChecks):
{.pop.}
else:
proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
header: "<setjmp.h>", importc: "_longjmp".}
Expand Down

0 comments on commit 843c479

Please sign in to comment.