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

Visual C++ exception message does not print, SIGABRT instead. #11846

Closed
treeform opened this issue Jul 29, 2019 · 8 comments
Closed

Visual C++ exception message does not print, SIGABRT instead. #11846

treeform opened this issue Jul 29, 2019 · 8 comments

Comments

@treeform
Copy link
Contributor

treeform commented Jul 29, 2019

Test code:

raise newException(IndexError, "hi there")

Correct (gcc C):

nim c -r .\bug.nim
***\bug.nim(1) bug
Error: unhandled exception: hi there [IndexError]

Correct (gcc C++):

nim cpp -r .\bug.nim
***\bug.nim(1) bug
Error: unhandled exception: hi there [IndexError]

Wrong (Visual C++)

nim cpp --cc:vcc -r .\bug.nim
***\bug.nim(1) bug
SIGABRT: Abnormal termination.

Nim version:

nim -v
Nim Compiler Version 0.20.99 [Windows: amd64]
Compiled at 2019-07-29
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release
@mratsim
Copy link
Collaborator

mratsim commented Jul 29, 2019

The whole C++ backend had this issue as raised in #6512, it was fixed by c9f1844 but it seems like the fix only worked for GCC/Clang.

@treeform
Copy link
Contributor Author

treeform commented Jul 29, 2019

This is what I did to make it work:

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

    var msg = "Unknown error in unexpected exception handler"
    # try:
    #   raise
    # except Exception:
    #   msg = currException.getStackTrace() & "Error: unhandled exception: " &
    #     currException.msg & " [" & $currException.name & "]"
    # except StdException as e:
    #   msg = "Error: unhandled cpp exception: " & $e.what()
    # except:
    #   msg = "Error: unhandled unknown cpp exception"

    if currException != nil:
      msg = currException.getStackTrace() & "Error: unhandled exception: " &
        currException.msg & " [" & $currException.name & "]"

    when defined(genode):
      # stderr not available by default, use the LOG session
      echo msg
    else:
      writeToStdErr msg & "\n"

    quit 1

At

setTerminate proc() {.noconv.} =

I think the issue is that the

try:
    raise

Just does not work with Visual C++ exceptions. But you can just look at the current exception if its there. I am not sure what this try-raise trying to do. I am confused by this.

I think this handler was made to also handle c++ native exceptions. But it was not tested on vc++.

@treeform
Copy link
Contributor Author

treeform commented Jul 29, 2019

Here is the issue what was being fixed by the regression: #10765 and PR 3cf0380

Hey @cooldome you worked on that PR could you take a look again maybe?

also my vc++ version:
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 for x64

@nc-x
Copy link
Contributor

nc-x commented Aug 17, 2019

Generating

try {
  std::rethrow_exception(std::current_exception());
}

instead of

try {throw;}

Works fine when tested on C++ code, but for the Nim code in this issue, msvc gives -

Error: unhandled cpp exception: bad exception

which is again wrong. (on gcc it works).

Minimal C++ code for anybody who wants to investigate
#include <exception>
#include <iostream>
using namespace std;

void x() {
  try {
    throw; // does not work in msvc
    // rethrow_exception(current_exception()); // works in msvc, but not for https://github.com/nim-lang/Nim/issues/11846
  } catch (...) {
    cout << "Called by terminate." << endl;
  }
  exit(-1);
}

int main() {
  try {
    set_terminate(x);
    throw 1;
  } catch (string) {
    cout << "ABC" << endl;
  }
  return 0;
}

treeform added a commit to treeform/Nim that referenced this issue Aug 31, 2019
treeform added a commit to treeform/Nim that referenced this issue Aug 31, 2019
treeform added a commit to treeform/Nim that referenced this issue Aug 31, 2019
@cooldome
Copy link
Member

cooldome commented Nov 7, 2019

fixed

@cooldome cooldome closed this as completed Nov 7, 2019
@treeform
Copy link
Contributor Author

@cooldome Still not fixed for me. I get the exception stack trace but not the message. :(

@treeform
Copy link
Contributor Author

This change here fixes it:

    {.emit"#if defined(_MSC_VER) && (_MSC_VER < 1923)".}
    #msg = "Error: unhandled unknown cpp exception"
    msg = currException.getStackTrace() & "Error: unhandled exception: " &
        currException.msg & " [" & $currException.name & "]"
    {.emit"#endif".}

@treeform
Copy link
Contributor Author

I have updated to the newer visual VC++ and that works as well. I guess we are laving older VC++ in broken state but that's probably fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants