compiler-research / CppInterOp

A Clang-based C++ Interoperability Library
Other
38 stars 20 forks source link

Improve crash recovery for `Declare` #306

Closed Gnimuc closed 3 weeks ago

Gnimuc commented 3 weeks ago

I get the following segfault when calling Declare(code, false):

In file included from <<< inputs >>>:1:
In file included from input_line_1:1:
In file included from 
...
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/c++/v1/cwchar:114:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/c++/v1/wchar.h:126:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/wchar.h:89:10: fatal error: 'stdarg.h' file not found
   89 | #include <stdarg.h>
      |          ^~~~~~~~~~

[20751] signal 11 (2): Segmentation fault: 11
in expression starting at REPL[3]:1

The stack trace looks like this:

clang::Decl::getTranslationUnitDecl() at ...
clang::IncrementalParser::CleanUpPTU(clang::PartialTranslationUnit&) at ...
clang::IncrementalParser::ParseOrWrapTopLevelDecl() at ...
clang::IncrementalParser::Parse(llvm::StringRef) at ...
clang::Interpreter::Parse(llvm::StringRef) at ...
Cpp::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, clang::Value*, clang::PartialTranslationUnit**, bool) at ...
Cpp::Declare(char const*, bool) at ...

The reason is that I used the wrong resource-dir, so the interpreter can not find those headers shipped with Clang. But I think it's not good to just crash even if the setup is wrong.

Using Declare(code, true) doesn't alleviate the issue because diagnostics are suppressed as well.

I also tried to explicitly llvm::CrashRecoveryContext::Enable(), but it didn't work.

This seems to be a problem in the upstream.

https://github.com/llvm/llvm-project/blob/1bc8b3258e6d42f702fb11eb60d84d0e23935e3e/clang/lib/Interpreter/IncrementalParser.cpp#L255-L283

vgvassilev commented 3 weeks ago

Yes, error recovery is tricky and we need more minimal reproducers to fix things upstream. I do not think we can do anything in that regard in CppInterOp, unfortunately.

Gnimuc commented 3 weeks ago
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/c++/v1/wchar.h:126:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/include/wchar.h:89:10: fatal error: 'stdarg.h' file not found
   89 | #include <stdarg.h>
      |          ^~~~~~~~~~
Assertion failed: (Ptr && "dereferencing end() iterator"), function operator*, file /workspace/srcdir/llvm-project/clang/include/clang/AST/DeclBase.h, line 1315.

[75573] signal 6: Abort trap: 6

It turns out this is actually https://github.com/llvm/llvm-project/pull/85378.