root-project / cling

The cling C++ interpreter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Segfault when including a header that can't be found

zambony opened this issue · comments

  • Checked for duplicates

Describe the bug

With Cling built freshly from its sources, including a header file that does not exist or cannot be found causes Cling to segfault.
When doing the same thing using the prebuilt binaries, the error is correctly caught and the transaction is unloaded.

Expected behavior

Cling should unload the transaction which attempted to include the bad header, then let me continue on my merry way without crashing.

To Reproduce

  1. Build Cling from the all-in-one script or from cpt.py in a Release configuration.
  2. Create a test file, e.g., test.cxx:
#include <badheader>
#include <iostream>

void test()
{
    std::cout << "Hello, world!" << std::endl;
}
  1. Run cling.
  2. Enter .L path/to/test.cxx
  3. Observe that Cling segfaults, pointing to something related to unloading the anonymous namespace.
****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ .L test.cxx
In file included from input_line_3:1:
/app/release/bin/test.cxx:1:10: fatal error: 'badheader' file not found
#include <badheader>
         ^~~~~~~
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
./cling(+0x10d1dd4)[0x55ca7fd5bdd4]
./cling(+0x10cf5fe)[0x55ca7fd595fe]
/lib/x86_64-linux-gnu/libc.so.6(+0x43090)[0x7f9ae07f2090]
./cling(_ZN5clang11DeclContext10removeDeclEPNS_4DeclE+0x15e)[0x55ca81fc843e]
./cling(_ZN5cling12DeclUnloader14VisitNamedDeclEPN5clang9NamedDeclE+0x16e)[0x55ca7fbc7f2e]
./cling(_ZN5cling12DeclUnloader18VisitNamespaceDeclEPN5clang13NamespaceDeclE+0x117)[0x55ca7fbd56e7]
./cling(_ZN5cling12DeclUnloader16VisitDeclContextEPN5clang11DeclContextE+0x120)[0x55ca7fbd52a0]
./cling(_ZN5cling12DeclUnloader20VisitLinkageSpecDeclEPN5clang15LinkageSpecDeclE+0x43)[0x55ca7fbd5453]
./cling(_ZN5cling19TransactionUnloader18unloadDeclarationsEPNS_11TransactionERNS_12DeclUnloaderE+0x114)[0x55ca7fc7f194]
./cling(_ZN5cling19TransactionUnloader17RevertTransactionEPNS_11TransactionE+0x222)[0x55ca7fc7f432]
./cling(_ZN5cling11Interpreter6unloadERNS_11TransactionE+0x188)[0x55ca7fc3fc18]
./cling(_ZN5cling17IncrementalParser17commitTransactionERN4llvm14PointerIntPairIPNS_11TransactionELj2ENS0_12EParseResultENS1_21PointerLikeTypeTraitsIS4_EENS1_18PointerIntPairInfoIS4_Lj2ES7_EEEEb+0x370)[0x55ca7fc36e30]
./cling(_ZN5cling17IncrementalParser7CompileEN4llvm9StringRefERKNS_18CompilationOptionsE+0x6a)[0x55ca7fc3cdea]
./cling(_ZN5cling11Interpreter10loadHeaderERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPPNS_11TransactionE+0x1d6)[0x55ca7fc44856]
./cling(_ZN5cling8MetaSema13actOnLCommandEN4llvm9StringRefEPPNS_11TransactionE+0x1a5)[0x55ca7fca5e85]
./cling(_ZN5cling10MetaParser9isCommandERNS_8MetaSema12ActionResultEPNS_5ValueE+0x108)[0x55ca7fc9e548]
./cling(_ZN5cling13MetaProcessor7processEN4llvm9StringRefERNS_11Interpreter17CompilationResultEPNS_5ValueEb+0x1d8)[0x55ca7fc9f208]
./cling(_ZN5cling13UserInterface16runInteractivelyEb+0x26d)[0x55ca7fd8003d]
./cling(main+0x55d)[0x55ca7fa9b88d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x7f9ae07d3083]
./cling(_start+0x2e)[0x55ca7fb2b96e]
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./cling
Segmentation fault

Setup

Ubuntu 20.04 Docker image, GCC/G++ 9.4.0

FROM ubuntu:focal

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends sudo build-essential wget curl ninja-build python3 python3-pip
RUN apt-get install -y --no-install-recommends git cmake micro

WORKDIR /app

Build script. Removing my modifications doesn't make a difference, at least on my end.

#!/bin/bash
#
# axel@cern.ch, 2014-02-07

# which is not ideal, see http://stackoverflow.com/a/677212/1392758
python=`which python`
if type python2 > /dev/null 2>&1; then
    python=`which python2`
fi
if type python3 > /dev/null 2>&1; then
    python=`which python3`
fi

function update {
    cd src || exit 1
    echo '++ Updating llvm...'
    git pull origin cling-patches || exit 1
    cd tools/clang || exit 1
    echo '++ Updating clang...'
    git pull origin cling-patches || exit 1
    echo '++ Updating cling...'
    cd ../cling || exit 1
    git pull || exit 1
    echo '++ Update done.'
    cd ../../..
}

function clone {
    # clone what branch where
    where=$3
    if [ "$where" = "" ]; then
        where=$1
    fi
    echo '>> Cloning '$1'...'
    git clone http://root.cern.ch/git/${1}.git $where > /dev/null || exit 1
    ( cd $where && git checkout $2 )
}

function initial {
    if [ -d release ]; then
        echo '!! Directory release/ exists; refusing to build / install!'
        exit 1
    fi

    clone llvm cling-patches src
    cd src/tools || exit 1
    clone clang cling-patches
    clone cling master
    cd ../..
}

function configure {
    mkdir -p obj || exit 1
    INSTDIR=`pwd`/release
    cd obj || exit 1
    echo '>> Configuring...'
    cmake \
        -GNinja \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_CXX_FLAGS="-std=c++17" \
        -DCMAKE_CXX_STANDARD=17 \
        -DCLING_INCLUDE_TESTS=OFF \
        -DLLVM_BUILD_TOOLS=OFF \
        -DLLVM_BUILD_DOCS=OFF \
        -DLLVM_ENABLE_SPHINX=OFF \
        -DLLVM_ENABLE_DOXYGEN=OFF \
        -DLLVM_BUILD_LLVM_DYLIB=OFF \
        -DLLVM_TARGETS_TO_BUILD="host;NVPTX" \
        -DCMAKE_INSTALL_PREFIX=$INSTDIR \
        -DPYTHON_EXECUTABLE=$python \
        ../src > /dev/null || exit 1
    cd ..
}

function build {
    cd obj
    echo ':: Building...'
    ninja -j 12 || exit 1
    rm -rf ../release
    echo ':: Installing...'
    ninja -j 12 install || exit 1
    echo ':: SUCCESS.'
    cd ..
}

if [ -d src ]; then
    # update mode
    update
else
    initial
fi

if ! [ -e obj/Makefile ]; then
    configure
fi

build

echo 'Run ./release/bin/cling'

Additional context

Since prebuilt binaries work fine, and the binaries built for xeus-cling are okay, I assume it's something to do with my build environment, but I cannot find the reason why. This happened even when compiled on my host machine, not inside a Docker container, on two separate computers.

I have also tried using the specific patch branches cling-patches-rrelease_13 for the script, as cpt.py does, and it did not help.

The only other issue that my compiled version has that other packages do not is that, in some instances, it takes a few seconds for the very first interpreter statement to finish. After that, all actions are fast.

I can only think that it's because I'm using a newer GCC, or Cling linked against the wrong standard library.

It seems that unloading transactions in general fails for me. There are some occasions where it's okay, like if it's a single function call with a typo in the name.

@marvin-littlewood Cling seems to be in the middle of a large refactor, and their main branch is not stable. I had to build based off the tag for v0.9 for stable results.

It does not crash for me in current master, could you retry?

I just (2024-04-30:14:21 GMT) used the build-script and built the current version in the repository & ran the above test. There was no crash report - so all seems to be well again. Thank you.