Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

old superseded fix for #10343 D20200623T225224 #324

Draft
wants to merge 1 commit into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1374,11 +1374,13 @@ proc genMainProc(m: BModule) =
"}$N$N"

NimMainProc =
# "void wrapNimMain(void (*fun_ptr)());$N" &
"N_CDECL(void, NimMain)(void) {$N" &
"\tvoid (*volatile inner)(void);$N" &
"$4" &
"\tinner = NimMainInner;$N" &
"$2" &
# "\twrapNimMain(*inner);$N" &
"\t(*inner)();$N" &
"}$N$N"

Expand Down
2 changes: 0 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type

include "system/basic_types"


proc compileOption*(option: string): bool {.
magic: "CompileOption", noSideEffect.}
## Can be used to determine an `on|off` compile-time option. Example:
Expand Down Expand Up @@ -3026,7 +3025,6 @@ when defined(genode):
# Perform application initialization
# and return to thread entrypoint.


import system/widestrs
export widestrs

Expand Down
1 change: 1 addition & 0 deletions lib/system/ansi_c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,6 @@ proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonReloadable, inline.}
# we cannot throw an exception here!
discard c_fwrite(s, 1, cast[csize_t](s.len), f)
discard c_fflush(f)
# todo: same treatment as D20190117T013551 in case `c_fwrite` fails

{.pop.}
6 changes: 4 additions & 2 deletions lib/system/excpt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,12 @@ when defined(cpp) and appType != "lib" and not gotoBasedExceptions and
proc setTerminate(handler: proc() {.noconv.})
{.importc: "std::set_terminate", header: "<exception>".}


setTerminate proc() {.noconv.} =
# Remove ourself as a handler, reinstalling the default handler.
setTerminate(nil)


var msg = "Unknown error in unexpected exception handler"
try:
{.emit: "#if !defined(_MSC_VER) || (_MSC_VER >= 1923)".}
Expand All @@ -594,8 +596,8 @@ when defined(cpp) and appType != "lib" and not gotoBasedExceptions and
# stderr not available by default, use the LOG session
echo msg
else:
writeToStdErr msg & "\n"

proc writeToStdErrRobust(msg: string) {.importc.}
writeToStdErrRobust msg & "\n"
quit 1

when not defined(noSignalHandler) and not defined(useNimRtl):
Expand Down
62 changes: 62 additions & 0 deletions lib/system/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,65 @@ iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
## result.lines += 1
var res = TaintedString(newStringOfCap(80))
while f.readLine(res): yield res

when defined(cpp):
{.emit:"""
NIM_EXTERNC int fd_is_valid(int fd) {
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
""".}

# NIM_EXP
when false:
# proc wrapNimMain(fun: proc(){.cdecl.}) {.exportc.} =
proc wrapNimMain(fun: proc(){.cdecl.}) {.exportc, compilerproc.} =
# proc wrapNimMain(fun: proc(){.cdecl.}) {.compilerproc.} =
## used to catch un-caught exceptions instead of falling through
## `std::terminate`, for example allows to fix #10343
# Seems simpler than `std::set_terminate` alternatives.
fun()
when defined(cpp):
proc fd_is_valid(fd:cint): cint {.cdecl, importc.}
try:
fun()
except Exception as e:
let file = when defined(genode):
# stderr not available by default, use the LOG session
stdout
else: stderr

# using `c_feof(file)` doesn't work
let fd = c_fileno(file)
if fd_is_valid(fd) != 0.cint:
let ex = getCurrentException()
let trace = ex.getStackTrace()
let msg = trace & "Error: unhandled exception: " & ex.msg &
" [" & $ex.name & "]\n"
file.write msg
# stderr.write "uncaught exception:" & e.msg
else:
# note(D20190117T013551):
# write to a logfile ($pid.log) if `--logerrorDir:mydir` is passed
discard
quit(1)
else:
fun()

when not defined(nimscript):
{.emit:"""
NIM_EXTERNC bool fileDescriptorIsValid(int fd) {
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
""".}

proc fileDescriptorIsValid(fd:cint): bool {.cdecl, importc.}

proc writeToStdErrRobust(msg: string) {.exportc.} =
# using `c_feof(stderr)` doesn't give usable answer
let fd = c_fileno(stderr)
if fileDescriptorIsValid(fd):
stderr.write msg
else:
# note(D20190117T013551):
# write to a logfile ($pid.log) if `--logerrorDir:mydir` is passed
discard
12 changes: 8 additions & 4 deletions tests/exception/t9657.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
discard """
action: run
exitcode: 1
target: "c"
targets: "c cpp"
disabled: "openbsd"
disabled: "netbsd"
"""
# todo: remove `target: "c"` workaround once #10343 is properly fixed

close stdmsg
const m = "exception!"
discard writeBuffer(stdmsg, cstring(m), m.len)
writeLine stdmsg, "exception!"

when false:
# was there a 2nd issue/subtelty here?
const m = "exception!"
discard writeBuffer(stdmsg, cstring(m), m.len)