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

[Bug]: CppInterop crashes when interpreting std::cerr/cout related code #422

Open
JohanMabille opened this issue Jan 8, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@JohanMabille
Copy link

JohanMabille commented Jan 8, 2025

Description of bug

The following code build with gcc 13.3 leads to a segfault:

#include <string>
#include <vector>

#include "clang/Interpreter/CppInterOp.h" // from CppInterOp package
#include "doctest/doctest.h"

namespace
{
    struct StreamRedirectRAII {
      std::string &err;
      StreamRedirectRAII(std::string &e) : err(e) {
        Cpp::BeginStdStreamCapture(Cpp::kStdErr);
        Cpp::BeginStdStreamCapture(Cpp::kStdOut);
      }
      ~StreamRedirectRAII() {
        std::string out = Cpp::EndStdStreamCapture();
        err = Cpp::EndStdStreamCapture();
        std::cout << out;
      }
    };

    using Args = std::vector<const char*>;
    void* createInterpreter(const Args &ExtraArgs = {}) {
      Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"};

      if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) {
        return s == "-resource-dir";}) == ExtraArgs.end()) {
        std::string resource_dir = Cpp::DetectResourceDir();
        if (resource_dir.empty())
          std::cerr << "Failed to detect the resource-dir\n";
        ClangArgs.push_back("-resource-dir");
        ClangArgs.push_back(resource_dir.c_str());
      }
      std::vector<std::string> CxxSystemIncludes;
      Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes);
      for (const std::string& CxxInclude : CxxSystemIncludes) {
        ClangArgs.push_back("-isystem");
        ClangArgs.push_back(CxxInclude.c_str());
      }
      ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
      // FIXME: We should process the kernel input options and conditionally pass
      // the gpu args here.
      return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/);
    }
}

TEST_SUITE("execute_request")
{
    TEST_CASE("simple")
    {
        createInterpreter();
        std::string err;
        bool compilation_result = false;
        try
        {
            StreamRedirectRAII R(err);
            std::string code = "#include <iostream>\nstd::cout << \"oops\" << std::endl;";
            compilation_result = Cpp::Process(code.c_str());
        }
        catch(...)
        {
        }
        REQUIRE(!compilation_result);
    }
}

Here is the stacktrace I got when running this with gdb:

Program received signal SIGABRT, Aborted.
0x00007fffec384cdc in ?? () from /lib64/libc.so.6
(gdb) bt
#0  0x00007fffec384cdc in ?? () from /lib64/libc.so.6
#1  0x00007fffec335ec2 in raise () from /lib64/libc.so.6
#2  0x00007fffec320472 in abort () from /lib64/libc.so.6
#3  0x00007fffedbbf331 in llvm::report_fatal_error(llvm::Twine const&, bool) [clone .cold] ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#4  0x00007ffff1788428 in llvm::report_fatal_error(char const*, bool) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#5  0x00007ffff2aad872 in llvm::AsmPrinter::emitInlineAsm(llvm::StringRef, llvm::MCSubtargetInfo const&, llvm::MCTargetOptions const&, llvm::MDNode const*, llvm::InlineAsm::AsmDialect) const ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#6  0x00007ffff2aa3545 in llvm::AsmPrinter::doInitialization(llvm::Module&) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#7  0x00007ffff146b978 in llvm::FPPassManager::doInitialization(llvm::Module&) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#8  0x00007ffff1477064 in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#9  0x00007ffff16f6517 in llvm::orc::SimpleCompiler::operator()(llvm::Module&) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#10 0x00007ffff1710ed3 in llvm::orc::IRCompileLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#11 0x00007ffff171199e in llvm::orc::IRTransformLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#12 0x00007ffff171199e in llvm::orc::IRTransformLayer::emit(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >, llvm::orc::ThreadSafeModule) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#13 0x00007ffff171b404 in llvm::orc::BasicIRLayerMaterializationUnit::materialize(std::unique_ptr<llvm::orc::MaterializationResponsibility, std::default_delete<llvm::orc::MaterializationResponsibility> >) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#14 0x00007ffff1655f36 in llvm::orc::MaterializationTask::run() ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#15 0x00007ffff1656584 in llvm::orc::ExecutionSession::dispatchOutstandingMUs() ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#16 0x00007ffff16605c0 in llvm::orc::ExecutionSession::OL_completeLookup(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >, std::shared_ptr<llvm::orc::AsynchronousSymbolQuery>, std::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib*, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#17 0x00007ffff1661bfe in llvm::orc::InProgressFullLookupState::complete(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#18 0x00007ffff164df0a in llvm::orc::ExecutionSession::OL_applyQueryPhase1(std::unique_ptr<llvm::orc::InProgressLookupState, std::default_delete<llvm::orc::InProgressLookupState> >, llvm::Error) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#19 0x00007ffff16569d6 in llvm::orc::ExecutionSession::lookup(llvm::orc::LookupKind, std::vector<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::allocator<std::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags> > > const&, llvm::orc::SymbolLookupSet, llvm::orc::SymbolState, llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPt--Type <RET> for more, q to quit, c to continue without paging--
r, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef> > >)>, std::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib*, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > const&)>) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#20 0x00007ffff1658048 in llvm::orc::Platform::lookupInitSymbols(llvm::orc::ExecutionSession&, llvm::DenseMap<llvm::orc::JITDylib*, llvm::orc::SymbolLookupSet, llvm::DenseMapInfo<llvm::orc::JITDylib*, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::orc::SymbolLookupSet> > const&) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#21 0x00007ffff168e299 in (anonymous namespace)::GenericLLVMIRPlatformSupport::initialize(llvm::orc::JITDylib&) () from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#22 0x00007fffedd62d88 in clang::IncrementalExecutor::runCtors() const ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#23 0x00007fffedd5b05d in clang::Interpreter::Execute(clang::PartialTranslationUnit&) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#24 0x00007fffedd1b227 in Cpp::Process(char const*) ()
   from /home/yoyo/mambaforge/envs/xeus-cpp/lib/libclangCppInterOp.so.19.1
#25 0x0000555555580b8f in DOCTEST_ANON_SUITE_2::DOCTEST_ANON_FUNC_3() ()
#26 0x0000555555579b80 in doctest::Context::run() ()
#27 0x000055555556502e in main ()

What operating system was you using when the bug occured?

No response

What is the architechture of the cpu on your system?

No response

What did you build CppInterOp against?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant