Search code examples
c++antlr4

ANTLR4 C++ segmentation fault in SingletonPredictionContext::equals


I'm having a segmentation fault in a very simple (probably stuppid) ANTLR4 grammar using the C++ runtime.

The grammar in question:

grammar MemMap;

memmap: region+ EOF;
region: (WORD | '_')+  WORD*;

fragment LOWERCASE: [a-z];
fragment UPPERCASE: [A-Z];

WORD: [A-Za-z]+;

The C++ code:

int main() {
    ANTLRInputStream input("test_test     , 0x0 , 0x1 , 4K      , TEST , TEST , :R:W:X:8:16:32:64:");
    MemMapLexer lexer(&input);

    CommonTokenStream tokens(&lexer);

    MemMapParser parser(&tokens);

    auto* p = parser.memmap();  // <-- This call produces a SEGFAULT
}

The stack trace is the following:

antlr4::atn::PredictionContext::getContextType PredictionContext.h:170
antlr4::atn::SingletonPredictionContext::equals SingletonPredictionContext.cpp:60
antlr4::atn::operator== PredictionContext.h:206
antlr4::atn::PredictionContextCache::PredictionContextComparer::operator() PredictionContextCache.cpp:55
std::__detail::_Equal_helper::_S_equals hashtable_policy.h:1460
std::__detail::_Hashtable_base::_M_equals hashtable_policy.h:1844
std::_Hashtable::_M_find_before_node hashtable.h:1562
std::_Hashtable::_M_find_node hashtable.h:649
std::_Hashtable::find hashtable.h:1452
std::unordered_set::find unordered_set.h:654
antlr4::atn::PredictionContextCache::get PredictionContextCache.cpp:40
getCachedContextImpl PredictionContext.cpp:53
antlr4::atn::PredictionContext::getCachedContext PredictionContext.cpp:520
antlr4::atn::ATNSimulator::getCachedContext ATNSimulator.cpp:32
antlr4::atn::ATNConfigSet::optimizeConfigs ATNConfigSet.cpp:138
antlr4::atn::ParserATNSimulator::addDFAState ParserATNSimulator.cpp:1335
antlr4::atn::ParserATNSimulator::addDFAEdge ParserATNSimulator.cpp:1287
antlr4::atn::ParserATNSimulator::computeTargetState ParserATNSimulator.cpp:324
antlr4::atn::ParserATNSimulator::execATN ParserATNSimulator.cpp:188
antlr4::atn::ParserATNSimulator::adaptivePredict ParserATNSimulator.cpp:163
antlr_memmap::MemMapParser::region() 0x000000000048b2f2
antlr_memmap::MemMapParser::memmap() 0x000000000048acdc

It might be that I have incorrectly build my grammar, but I wouldn't expect a SEGFAULT inside the generated code by ANTLR4, maybe an exception or some kind of error.


Solution

  • Issue was the use of different stdlibs when compiling ANTLR runtime and the C++ progam. Compiling with the same stdlib, solved the issue.