1 //===- unittests/Interpreter/InterpreterExceptionTest.cpp -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Unit tests for Clang's Interpreter library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Interpreter/Interpreter.h" 14 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclGroup.h" 18 #include "clang/Basic/TargetInfo.h" 19 #include "clang/Basic/Version.h" 20 #include "clang/Config/config.h" 21 #include "clang/Frontend/CompilerInstance.h" 22 #include "clang/Frontend/TextDiagnosticPrinter.h" 23 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ExecutionEngine/Orc/LLJIT.h" 26 #include "llvm/Support/ManagedStatic.h" 27 #include "llvm/Support/TargetSelect.h" 28 #include "llvm-c/Error.h" 29 30 #include "gmock/gmock.h" 31 #include "gtest/gtest.h" 32 33 using namespace clang; 34 35 namespace { 36 using Args = std::vector<const char *>; 37 static std::unique_ptr<Interpreter> 38 createInterpreter(const Args &ExtraArgs = {}, 39 DiagnosticConsumer *Client = nullptr) { 40 Args ClangArgs = {"-Xclang", "-emit-llvm-only"}; 41 ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); 42 auto CI = cantFail(clang::IncrementalCompilerBuilder::create(ClangArgs)); 43 if (Client) 44 CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false); 45 return cantFail(clang::Interpreter::create(std::move(CI))); 46 } 47 48 TEST(InterpreterTest, CatchException) { 49 llvm::InitializeNativeTarget(); 50 llvm::InitializeNativeTargetAsmPrinter(); 51 52 { 53 auto J = llvm::orc::LLJITBuilder().create(); 54 if (!J) { 55 // The platform does not support JITs. 56 // Using llvm::consumeError will require typeinfo for ErrorInfoBase, we 57 // can avoid that by going via the C interface. 58 LLVMConsumeError(llvm::wrap(J.takeError())); 59 return; 60 } 61 } 62 63 #define Stringify(s) Stringifyx(s) 64 #define Stringifyx(s) #s 65 66 // We define a custom exception to avoid #include-ing the <exception> header 67 // which would require this test to know about the libstdc++ location. 68 // its own header file. 69 #define CUSTOM_EXCEPTION \ 70 struct custom_exception { \ 71 custom_exception(const char *Msg) : Message(Msg) {} \ 72 const char *Message; \ 73 }; 74 75 CUSTOM_EXCEPTION; 76 77 std::string ExceptionCode = Stringify(CUSTOM_EXCEPTION); 78 ExceptionCode += 79 R"( 80 extern "C" int printf(const char*, ...); 81 static void ThrowerAnError(const char* Name) { 82 throw custom_exception(Name); 83 } 84 85 extern "C" int throw_exception() { 86 try { 87 ThrowerAnError("To be caught in JIT"); 88 } catch (const custom_exception& E) { 89 printf("Caught: '%s'\n", E.Message); 90 } catch (...) { 91 printf("Unknown exception\n"); 92 } 93 ThrowerAnError("To be caught in binary"); 94 return 0; 95 } 96 )"; 97 std::unique_ptr<Interpreter> Interp = createInterpreter(); 98 // FIXME: Re-enable the excluded target triples. 99 const clang::CompilerInstance *CI = Interp->getCompilerInstance(); 100 const llvm::Triple &Triple = CI->getASTContext().getTargetInfo().getTriple(); 101 102 // AIX is unsupported. 103 if (Triple.isOSAIX()) 104 return; 105 106 // FIXME: ARM fails due to `Not implemented relocation type!` 107 if (Triple.isARM()) 108 return; 109 110 // FIXME: libunwind on darwin is broken, see PR49692. 111 if (Triple.isOSDarwin() && (Triple.getArch() == llvm::Triple::aarch64 || 112 Triple.getArch() == llvm::Triple::aarch64_32)) 113 return; 114 115 // Check if platform does not support exceptions. 116 { 117 // Force the creation of an incremental executor to call getSymbolAddress. 118 llvm::cantFail(Interp->ParseAndExecute("")); 119 auto Sym = Interp->getSymbolAddress("__cxa_throw"); 120 if (!Sym) { 121 LLVMConsumeError(llvm::wrap(Sym.takeError())); 122 return; 123 } 124 } 125 126 llvm::cantFail(Interp->ParseAndExecute(ExceptionCode)); 127 testing::internal::CaptureStdout(); 128 auto ThrowException = 129 (int (*)())llvm::cantFail(Interp->getSymbolAddress("throw_exception")); 130 EXPECT_ANY_THROW(ThrowException()); 131 std::string CapturedStdOut = testing::internal::GetCapturedStdout(); 132 EXPECT_EQ(CapturedStdOut, "Caught: 'To be caught in JIT'\n"); 133 134 llvm::llvm_shutdown(); 135 } 136 137 } // end anonymous namespace 138