nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

SIGSEGV hangs instead of raising NilAccessDefect with `import segfaults` and gc:arc #16415

Open qb-0 opened 3 years ago

qb-0 commented 3 years ago

SIGSEGV is not catchable with arc and import segfaults.

Example

import segfaults

try:
  echo cast[ptr int](nil)[]
except NilAccessDefect:
  echo "SegFault!"

Current Output

Traceback (most recent call last)
Tests\segfaulttest.nim(4) segfaulttest
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

SegFault!
$ nim -v
Nim Compiler Version 1.4.2 [Windows: amd64]
Compiled at 2020-11-30
timotheecour commented 3 years ago

bad example because in devel this now gives static, not RT error (regardless of gc): Error: nil dereference is not allowed

however there is indeed a bug

when true:
  import segfaults
  var a: ptr int
  try:
    echo a[]
  except NilAccessDefect:
    echo "SegFault!"

default gc: prints SegFault! gc:arc: hangs

And yes, segfaults is useful and needs to be supported, it's useful in cases where untrusted code should not cause a program to abort if it can (eg: repls, dll plugins)

Araq commented 3 years ago

And yes, segfaults is useful and needs to be supported, it's useful in cases where untrusted code should not cause a program to abort if it can (eg: repls, dll plugins)

Feel free to submit a patch then.

johnnovak commented 3 years ago

And yes, segfaults is useful and needs to be supported, it's useful in cases where untrusted code should not cause a program to abort if it can (eg: repls, dll plugins)

Or for applications shipped to users, where you want to capture errors in a crash log with full stack traces, that the user can the submit in a bug report.

timotheecour commented 3 years ago

note that you'll get a stacktrace without segfaults if you compile with --stacktrace

johnnovak commented 3 years ago

note that you'll get a stacktrace without segfaults if you compile with --stacktrace

Yeah I know, but it just gets dumped to stderr, or in case of a Windows app, it gets shown in a popup dialog box. What I want is to write the stacktrace into a log file, so I need a mechanism to hook into this thing and change the behaviour of just dumping the stacktrace to stderr/displaying it in an alert box.

timotheecour commented 3 years ago

can you try unhandledExceptionHook and onUnhandledException?

(we should still fix the bug though)

ringabout commented 2 years ago

It is related to goto exception and the example works with exceptions:setjmp.

bung87 commented 1 year ago

change segfaults.nim

var SEGV_ACCERR {.importc, header: "<signal.h>".}: cint
...
if y.si_code == SEGV_MAPERR or y.si_code == SEGV_ACCERR:

compile with -d:noSignalHandler -d:nimPanics

you get Error: unhandled exception: Could not access value because it is nil. [NilAccessDefect]

lib/system/excpt.nim also set a signal handler, so noSignalHandler flag is required. Edit: turns out only -d:nimPanics required, it become unhandled exception.