cling/lib/MetaProcessor/MetaProcessor.cpp

539 lines
18 KiB
C++
Raw Normal View History

//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Axel Naumann <axel@cern.ch>
2014-01-07 14:08:37 +04:00
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
#include "cling/MetaProcessor/MetaProcessor.h"
#include "cling/MetaProcessor/InputValidator.h"
#include "cling/MetaProcessor/MetaParser.h"
#include "cling/MetaProcessor/MetaSema.h"
#include "cling/MetaProcessor/Display.h"
#include "cling/Interpreter/Interpreter.h"
2014-02-27 01:37:16 +04:00
#include "cling/Interpreter/Value.h"
#include "cling/Utils/Output.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/BinaryFormat/Magic.h"
2013-09-19 19:59:58 +04:00
#include "llvm/Support/Path.h"
#include <fcntl.h>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <sstream>
#include <stdio.h>
2014-02-03 17:02:56 +04:00
#ifndef WIN32
#include <unistd.h>
#else
#include <io.h>
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#endif
using namespace clang;
namespace cling {
class MetaProcessor::RedirectOutput {
static int dupOnce(int Fd, int& Bak) {
// Flush now or can drop the buffer when dup2 is called with Fd later.
// This seems only neccessary when piping stdout or stderr, but do it
// for ttys to avoid over complicated code for minimal benefit.
::fflush(Fd==STDOUT_FILENO ? stdout : stderr);
if (Bak == kInvalidFD)
Bak = ::dup(Fd);
return Bak;
}
struct Redirect {
int FD;
MetaProcessor::RedirectionScope Scope;
bool Close;
Redirect(std::string file, bool append, RedirectionScope S, int* Baks) :
FD(-1), Scope(S), Close(false) {
if (S & kSTDSTRM) {
// Remove the flag from Scope, we don't need it anymore
Scope = RedirectionScope(Scope & ~kSTDSTRM);
if (file == "&1")
FD = dupOnce(STDOUT_FILENO, Baks[0]);
else if (file == "&2")
FD = dupOnce(STDERR_FILENO, Baks[1]);
// Close = false; Parent manages lifetime
if (FD != -1)
return;
llvm_unreachable("kSTDSTRM passed for unknown stream");
}
const int Perm = 0644;
#ifdef _WIN32
const int Mode = _O_CREAT | _O_WRONLY | (append ? _O_APPEND : _O_TRUNC);
FD = ::_open(file.c_str(), Mode, Perm);
#else
const int Mode = O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC);
FD = ::open(file.c_str(), Mode, Perm);
#endif
if (FD == -1) {
::perror("Redirect::open");
return;
}
Close = true;
if (append)
::lseek(FD, 0, SEEK_END);
}
~Redirect() {
if (Close)
::close(FD);
}
};
typedef std::vector<std::unique_ptr<Redirect>> RedirectStack;
enum { kNumRedirects = 2, kInvalidFD = -1 };
RedirectStack m_Stack;
int m_Bak[kNumRedirects];
int m_CurStdOut;
#ifdef _WIN32
// After a redirection from stdout into stderr then undirecting stdout, the
// console will loose line-buffering. To get arround this we test if stdout
// is a tty during construction, and if so mark the case when stdout has
// returned from a redirection into stderr, then handle it ~RedirectOutput.
// We need two bits for 3 possible states.
unsigned m_TTY : 2;
#else
const bool m_TTY;
#endif
// Exception safe push routine
int push(Redirect* R) {
std::unique_ptr<Redirect> Re(R);
const int FD = R->FD;
m_Stack.emplace_back(Re.get());
Re.release();
return FD;
}
// Call ::dup2 and report errMsg on failure
bool dup2(int oldfd, int newfd, const char* errMsg) {
if (::dup2(oldfd, newfd) == kInvalidFD) {
::perror(errMsg);
return false;
}
return true;
}
// Restore stdstream from backup and close the backup
void close(int &oldfd, int newfd) {
assert((newfd == STDOUT_FILENO || newfd == STDERR_FILENO) && "Not std FD");
assert(oldfd == m_Bak[newfd == STDERR_FILENO] && "Not backup FD");
if (oldfd != kInvalidFD) {
dup2(oldfd, newfd, "RedirectOutput::close");
::close(oldfd);
oldfd = kInvalidFD;
}
}
int restore(int FD, FILE *F, MetaProcessor::RedirectionScope Flag,
int &bakFD) {
// If no backup, we have never redirected the file, so nothing to restore
if (bakFD != kInvalidFD) {
// Find the last redirect for the scope, and restore redirection to it
for (RedirectStack::const_reverse_iterator it = m_Stack.rbegin(),
e = m_Stack.rend();
it != e; ++it) {
const Redirect *R = (*it).get();
if (R->Scope & Flag) {
dup2(R->FD, FD, "RedirectOutput::restore");
return R->FD;
}
}
// No redirection for this scope, restore to backup
fflush(F);
close(bakFD, FD);
}
return kInvalidFD;
}
public:
RedirectOutput() : m_CurStdOut(kInvalidFD),
m_TTY(::isatty(STDOUT_FILENO) ? 1 : 0) {
for (unsigned i = 0; i < kNumRedirects; ++i)
m_Bak[i] = kInvalidFD;
}
~RedirectOutput() {
close(m_Bak[0], STDOUT_FILENO);
close(m_Bak[1], STDERR_FILENO);
while (!m_Stack.empty())
m_Stack.pop_back();
#ifdef _WIN32
// State 2, was tty to begin with, then redirected to stderr and back.
if (m_TTY == 2)
::freopen("CON", "w", stdout);
#else
// If redirection took place without writing anything to the terminal
// beforehand (--nologo) then the dup2 relinking stdout will have caused
// it to be re-opened without line buffering.
if (m_TTY)
::setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
#endif
}
void redirect(llvm::StringRef file, bool apnd,
MetaProcessor::RedirectionScope scope) {
if (file.empty()) {
// Unredirection, remove last redirection state(s) for given scope(s)
if (m_Stack.empty()) {
cling::errs() << "No redirections left to remove\n";
return;
}
MetaProcessor::RedirectionScope lScope = scope;
SmallVector<RedirectStack::iterator, 2> Remove;
for (auto it = m_Stack.rbegin(), e = m_Stack.rend(); it != e; ++it) {
Redirect *R = (*it).get();
const unsigned Match = R->Scope & lScope;
if (Match) {
#ifdef _WIN32
// stdout back from stderr, fix up our console output on destruction
if (m_TTY && R->FD == m_Bak[1] && scope & kSTDOUT)
m_TTY = 2;
#endif
// Clear the flag so restore below will ignore R for scope
R->Scope = MetaProcessor::RedirectionScope(R->Scope & ~Match);
// If no scope left, then R should be removed
if (!R->Scope) {
// standard [24.4.1/1] says &*(reverse_iterator(i)) == &*(i - 1)
Remove.push_back(std::next(it).base());
}
// Clear match to reduce lScope (kSTDBOTH -> kSTDOUT or kSTDERR)
lScope = MetaProcessor::RedirectionScope(lScope & ~Match);
// If nothing to match anymore, then we're done
if (!lScope)
break;
}
}
// std::vector::erase invalidates iterators at or after the point of
// the erase, so if we reverse iterate on Remove everything is fine
for (auto it = Remove.rbegin(), e = Remove.rend(); it != e; ++it)
m_Stack.erase(*it);
} else {
// Add new redirection state
if (push(new Redirect(file.str(), apnd, scope, m_Bak)) != kInvalidFD) {
// Save a backup for the scope(s), if not already done
if (scope & MetaProcessor::kSTDOUT)
dupOnce(STDOUT_FILENO, m_Bak[0]);
if (scope & MetaProcessor::kSTDERR)
dupOnce(STDERR_FILENO, m_Bak[1]);
} else
return; // Failure
}
if (scope & MetaProcessor::kSTDOUT)
m_CurStdOut =
restore(STDOUT_FILENO, stdout, MetaProcessor::kSTDOUT, m_Bak[0]);
if (scope & MetaProcessor::kSTDERR)
restore(STDERR_FILENO, stderr, MetaProcessor::kSTDERR, m_Bak[1]);
}
void resetStdOut(bool toBackup = false) {
// When not outputing to a TTY there is no need to unredirect as
// TerminalDisplay handles writing to the console FD already.
if (!m_TTY)
return;
if (toBackup) {
if (m_Bak[0] != kInvalidFD) {
fflush(stdout);
dup2(m_Bak[0], STDOUT_FILENO, "RedirectOutput::resetStdOut");
}
} else if (m_CurStdOut != kInvalidFD)
dup2(m_CurStdOut, STDOUT_FILENO, "RedirectOutput::resetStdOut");
}
bool empty() const {
return m_Stack.empty();
2014-01-14 21:25:43 +04:00
}
};
MetaProcessor::MaybeRedirectOutputRAII::MaybeRedirectOutputRAII(
MetaProcessor &P) :
m_MetaProcessor(P) {
if (m_MetaProcessor.m_RedirectOutput)
m_MetaProcessor.m_RedirectOutput->resetStdOut(true);
2014-01-14 21:25:43 +04:00
}
MetaProcessor::MaybeRedirectOutputRAII::~MaybeRedirectOutputRAII() {
if (m_MetaProcessor.m_RedirectOutput)
m_MetaProcessor.m_RedirectOutput->resetStdOut();
}
2014-01-14 21:25:43 +04:00
2014-08-04 06:05:42 +04:00
MetaProcessor::MetaProcessor(Interpreter& interp, raw_ostream& outs)
: m_Interp(interp), m_Outs(&outs) {
m_InputValidator.reset(new InputValidator());
Lifetime of MetaParser is that of its input: Before, MetaParser might have pointed to a StringRef whose storage was gone, see asan failure in roottest/cling/other/runfileClose.C below. This was caused by recursive uses of MetaParser; see stack trace below: the inner recursion returned, but as the same MetaParser object was used by both frames, the objects cursor now pointed to freed memory. Instead, create a MetaParser (and MetaLexer) object per input. That way, their lifetime corresponds to the lifetime of their input. ================================================================= ==529104==ERROR: AddressSanitizer: stack-use-after-return on address 0x7ffff3afd82a at pc 0x7fffea18df6d bp 0x7fffffff8170 sp 0x7fffffff8168 READ of size 1 at 0x7ffff3afd82a thread T0 [Detaching after fork from child process 529183] #0 0x7fffea18df6c in cling::MetaLexer::Lex(cling::Token&) src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 #1 0x7fffea190d7c in cling::MetaParser::lookAhead(unsigned int) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:89:15 #2 0x7fffea190bd5 in cling::MetaParser::consumeToken() src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:49:5 #3 0x7fffea191d4d in cling::MetaParser::isLCommand(cling::MetaSema::ActionResult&) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:147:9 #4 0x7fffea1914dd in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:123:12 #5 0x7fffea191216 in cling::MetaParser::isMetaCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:101:33 #6 0x7fffea14e5aa in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:317:24 #7 0x7fffe99b67b7 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) src/core/metacling/src/TCling.cxx:2431:29 #8 0x7fffe99bde30 in TCling::Load(char const*, bool) src/core/metacling/src/TCling.cxx:3454:10 #9 0x7ffff7865f11 in TSystem::Load(char const*, char const*, bool) src/core/base/src/TSystem.cxx:1941:27 #10 0x7ffff7b8a0e3 in TUnixSystem::Load(char const*, char const*, bool) src/core/unix/src/TUnixSystem.cxx:2789:20 #11 0x7fffd78dd08b (<unknown module>) #12 0x7fffe9f8a5d9 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const src/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp:376:3 #13 0x7fffe9d73dc2 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1141:20 #14 0x7fffe9d6e317 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1391:29 #15 0x7fffe9d6c1fe in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:819:9 #16 0x7fffea151826 in cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:507:22 #17 0x7fffe99b585b in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2570:39 #18 0x7fffe99bbfee in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:3496:17 #19 0x7ffff77203d3 in TApplication::ExecuteFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1608:30 #20 0x7ffff771ebdf in TApplication::ProcessFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1480:11 #21 0x7ffff771e385 in TApplication::ProcessLine(char const*, bool, int*) src/core/base/src/TApplication.cxx:1453:14 #22 0x7ffff7f8157a in TRint::ProcessLineNr(char const*, char const*, int*) src/core/rint/src/TRint.cxx:766:11 #23 0x7ffff7f802f0 in TRint::Run(bool) src/core/rint/src/TRint.cxx:424:22 #24 0x4ff96d in main src/main/src/rmain.cxx:30:12 #25 0x7ffff6e040b2 in __libc_start_main /build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16 #26 0x41f35d in _start (asan/bin/root.exe+0x41f35d) Address 0x7ffff3afd82a is located in stack of thread T0 at offset 42 in frame #0 0x7fffe99b3d8f in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2456 This frame has 21 object(s): [32, 56) 'sLine' (line 2462) <== Memory access at offset 42 is inside this variable [96, 104) 'R__guard2471' (line 2471) [128, 136) 'R__guard2488' (line 2488) [160, 176) 'interpreterFlagsRAII' (line 2491) [192, 240) 'result' (line 2511) [272, 276) 'compRes' (line 2512) [288, 312) 'mod_line' (line 2517) [352, 376) 'aclicMode' (line 2518) [416, 440) 'arguments' (line 2519) [480, 504) 'io' (line 2520) [544, 568) 'fname' (line 2521) [608, 632) 'ref.tmp' (line 2547) [672, 696) 'ref.tmp145' (line 2547) [736, 768) 'code' (line 2555) [800, 832) 'codeline' (line 2556) [864, 1384) 'in' (line 2559) [1520, 1552) 'ref.tmp176' (line 2562) [1584, 1600) 'agg.tmp' [1616, 1624) 'ref.tmp198' (line 2568) [1648, 1664) 'agg.tmp207' [1680, 1696) 'autoParseRaii' (line 2588) HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-return src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 in cling::MetaLexer::Lex(cling::Token&) Shadow bytes around the buggy address: 0x10007e757ab0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ac0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ad0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ae0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757af0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 =>0x10007e757b00: f5 f5 f5 f5 f5[f5]f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b10: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b20: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b30: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b40: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b50: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==529104==ABORTING at src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:49 at src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:41 compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0, disableValuePrinting=false) at src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:314 input_line=0x7ffff3afd829 "#define XYZ 21", compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0) at src/core/metacling/src/TCling.cxx:2431 error=0x7fffd78cb0f4 <x>) at src/core/metacling/src/TCling.cxx:2591 sync=false, err=0x7fffd78cb0f4 <x>) at src/core/base/src/TApplication.cxx:1472 line=0x7fffd78c9000 "#define XYZ 21", error=0x7fffd78cb0f4 <x>) at src/core/base/src/TROOT.cxx:2328 from asan/roottest/cling/other/fileClose_C.so filename=0x6070000f0fd0 "asan/roottest/cling/other/fileClose_C.so", flag=257) at /home/axel/build/llvm/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:6270 at src/interpreter/cling/lib/Utils/PlatformPosix.cpp:118 permanent=false, resolved=true) at src/interpreter/cling/lib/Interpreter/DynamicLibraryManager.cpp:184 at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1444 T=0x0) at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1560 at src/interpreter/cling/lib/MetaProcessor/MetaSema.cpp:57 actionResult=@0x7ffff39532b0: cling::MetaSema::AR_Success)
2021-04-29 16:20:43 +03:00
m_MetaSema.reset(new MetaSema(interp, *this));
}
MetaProcessor::~MetaProcessor() {
}
int MetaProcessor::process(llvm::StringRef input_line,
2013-06-10 17:14:36 +04:00
Interpreter::CompilationResult& compRes,
2017-05-02 12:19:14 +03:00
Value* result,
bool disableValuePrinting /* = false */) {
2013-06-10 17:14:36 +04:00
if (result)
2014-02-27 01:37:16 +04:00
*result = Value();
compRes = Interpreter::kSuccess;
int expectedIndent = m_InputValidator->getExpectedIndent();
2014-08-04 06:05:42 +04:00
if (expectedIndent)
compRes = Interpreter::kMoreInputExpected;
if (input_line.empty() ||
(input_line.size() == 1 && input_line.front() == '\n')) {
// just a blank line, nothing to do.
return expectedIndent;
}
// Check for and handle meta commands.
Lifetime of MetaParser is that of its input: Before, MetaParser might have pointed to a StringRef whose storage was gone, see asan failure in roottest/cling/other/runfileClose.C below. This was caused by recursive uses of MetaParser; see stack trace below: the inner recursion returned, but as the same MetaParser object was used by both frames, the objects cursor now pointed to freed memory. Instead, create a MetaParser (and MetaLexer) object per input. That way, their lifetime corresponds to the lifetime of their input. ================================================================= ==529104==ERROR: AddressSanitizer: stack-use-after-return on address 0x7ffff3afd82a at pc 0x7fffea18df6d bp 0x7fffffff8170 sp 0x7fffffff8168 READ of size 1 at 0x7ffff3afd82a thread T0 [Detaching after fork from child process 529183] #0 0x7fffea18df6c in cling::MetaLexer::Lex(cling::Token&) src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 #1 0x7fffea190d7c in cling::MetaParser::lookAhead(unsigned int) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:89:15 #2 0x7fffea190bd5 in cling::MetaParser::consumeToken() src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:49:5 #3 0x7fffea191d4d in cling::MetaParser::isLCommand(cling::MetaSema::ActionResult&) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:147:9 #4 0x7fffea1914dd in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:123:12 #5 0x7fffea191216 in cling::MetaParser::isMetaCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:101:33 #6 0x7fffea14e5aa in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:317:24 #7 0x7fffe99b67b7 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) src/core/metacling/src/TCling.cxx:2431:29 #8 0x7fffe99bde30 in TCling::Load(char const*, bool) src/core/metacling/src/TCling.cxx:3454:10 #9 0x7ffff7865f11 in TSystem::Load(char const*, char const*, bool) src/core/base/src/TSystem.cxx:1941:27 #10 0x7ffff7b8a0e3 in TUnixSystem::Load(char const*, char const*, bool) src/core/unix/src/TUnixSystem.cxx:2789:20 #11 0x7fffd78dd08b (<unknown module>) #12 0x7fffe9f8a5d9 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const src/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp:376:3 #13 0x7fffe9d73dc2 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1141:20 #14 0x7fffe9d6e317 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1391:29 #15 0x7fffe9d6c1fe in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:819:9 #16 0x7fffea151826 in cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:507:22 #17 0x7fffe99b585b in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2570:39 #18 0x7fffe99bbfee in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:3496:17 #19 0x7ffff77203d3 in TApplication::ExecuteFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1608:30 #20 0x7ffff771ebdf in TApplication::ProcessFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1480:11 #21 0x7ffff771e385 in TApplication::ProcessLine(char const*, bool, int*) src/core/base/src/TApplication.cxx:1453:14 #22 0x7ffff7f8157a in TRint::ProcessLineNr(char const*, char const*, int*) src/core/rint/src/TRint.cxx:766:11 #23 0x7ffff7f802f0 in TRint::Run(bool) src/core/rint/src/TRint.cxx:424:22 #24 0x4ff96d in main src/main/src/rmain.cxx:30:12 #25 0x7ffff6e040b2 in __libc_start_main /build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16 #26 0x41f35d in _start (asan/bin/root.exe+0x41f35d) Address 0x7ffff3afd82a is located in stack of thread T0 at offset 42 in frame #0 0x7fffe99b3d8f in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2456 This frame has 21 object(s): [32, 56) 'sLine' (line 2462) <== Memory access at offset 42 is inside this variable [96, 104) 'R__guard2471' (line 2471) [128, 136) 'R__guard2488' (line 2488) [160, 176) 'interpreterFlagsRAII' (line 2491) [192, 240) 'result' (line 2511) [272, 276) 'compRes' (line 2512) [288, 312) 'mod_line' (line 2517) [352, 376) 'aclicMode' (line 2518) [416, 440) 'arguments' (line 2519) [480, 504) 'io' (line 2520) [544, 568) 'fname' (line 2521) [608, 632) 'ref.tmp' (line 2547) [672, 696) 'ref.tmp145' (line 2547) [736, 768) 'code' (line 2555) [800, 832) 'codeline' (line 2556) [864, 1384) 'in' (line 2559) [1520, 1552) 'ref.tmp176' (line 2562) [1584, 1600) 'agg.tmp' [1616, 1624) 'ref.tmp198' (line 2568) [1648, 1664) 'agg.tmp207' [1680, 1696) 'autoParseRaii' (line 2588) HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-return src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 in cling::MetaLexer::Lex(cling::Token&) Shadow bytes around the buggy address: 0x10007e757ab0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ac0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ad0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ae0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757af0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 =>0x10007e757b00: f5 f5 f5 f5 f5[f5]f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b10: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b20: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b30: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b40: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b50: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==529104==ABORTING at src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:49 at src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:41 compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0, disableValuePrinting=false) at src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:314 input_line=0x7ffff3afd829 "#define XYZ 21", compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0) at src/core/metacling/src/TCling.cxx:2431 error=0x7fffd78cb0f4 <x>) at src/core/metacling/src/TCling.cxx:2591 sync=false, err=0x7fffd78cb0f4 <x>) at src/core/base/src/TApplication.cxx:1472 line=0x7fffd78c9000 "#define XYZ 21", error=0x7fffd78cb0f4 <x>) at src/core/base/src/TROOT.cxx:2328 from asan/roottest/cling/other/fileClose_C.so filename=0x6070000f0fd0 "asan/roottest/cling/other/fileClose_C.so", flag=257) at /home/axel/build/llvm/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:6270 at src/interpreter/cling/lib/Utils/PlatformPosix.cpp:118 permanent=false, resolved=true) at src/interpreter/cling/lib/Interpreter/DynamicLibraryManager.cpp:184 at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1444 T=0x0) at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1560 at src/interpreter/cling/lib/MetaProcessor/MetaSema.cpp:57 actionResult=@0x7ffff39532b0: cling::MetaSema::AR_Success)
2021-04-29 16:20:43 +03:00
MetaParser parser(*m_MetaSema, input_line);
MetaSema::ActionResult actionResult = MetaSema::AR_Success;
if (!m_InputValidator->inBlockComment() &&
Lifetime of MetaParser is that of its input: Before, MetaParser might have pointed to a StringRef whose storage was gone, see asan failure in roottest/cling/other/runfileClose.C below. This was caused by recursive uses of MetaParser; see stack trace below: the inner recursion returned, but as the same MetaParser object was used by both frames, the objects cursor now pointed to freed memory. Instead, create a MetaParser (and MetaLexer) object per input. That way, their lifetime corresponds to the lifetime of their input. ================================================================= ==529104==ERROR: AddressSanitizer: stack-use-after-return on address 0x7ffff3afd82a at pc 0x7fffea18df6d bp 0x7fffffff8170 sp 0x7fffffff8168 READ of size 1 at 0x7ffff3afd82a thread T0 [Detaching after fork from child process 529183] #0 0x7fffea18df6c in cling::MetaLexer::Lex(cling::Token&) src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 #1 0x7fffea190d7c in cling::MetaParser::lookAhead(unsigned int) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:89:15 #2 0x7fffea190bd5 in cling::MetaParser::consumeToken() src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:49:5 #3 0x7fffea191d4d in cling::MetaParser::isLCommand(cling::MetaSema::ActionResult&) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:147:9 #4 0x7fffea1914dd in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:123:12 #5 0x7fffea191216 in cling::MetaParser::isMetaCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:101:33 #6 0x7fffea14e5aa in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:317:24 #7 0x7fffe99b67b7 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) src/core/metacling/src/TCling.cxx:2431:29 #8 0x7fffe99bde30 in TCling::Load(char const*, bool) src/core/metacling/src/TCling.cxx:3454:10 #9 0x7ffff7865f11 in TSystem::Load(char const*, char const*, bool) src/core/base/src/TSystem.cxx:1941:27 #10 0x7ffff7b8a0e3 in TUnixSystem::Load(char const*, char const*, bool) src/core/unix/src/TUnixSystem.cxx:2789:20 #11 0x7fffd78dd08b (<unknown module>) #12 0x7fffe9f8a5d9 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const src/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp:376:3 #13 0x7fffe9d73dc2 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1141:20 #14 0x7fffe9d6e317 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1391:29 #15 0x7fffe9d6c1fe in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:819:9 #16 0x7fffea151826 in cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:507:22 #17 0x7fffe99b585b in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2570:39 #18 0x7fffe99bbfee in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:3496:17 #19 0x7ffff77203d3 in TApplication::ExecuteFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1608:30 #20 0x7ffff771ebdf in TApplication::ProcessFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1480:11 #21 0x7ffff771e385 in TApplication::ProcessLine(char const*, bool, int*) src/core/base/src/TApplication.cxx:1453:14 #22 0x7ffff7f8157a in TRint::ProcessLineNr(char const*, char const*, int*) src/core/rint/src/TRint.cxx:766:11 #23 0x7ffff7f802f0 in TRint::Run(bool) src/core/rint/src/TRint.cxx:424:22 #24 0x4ff96d in main src/main/src/rmain.cxx:30:12 #25 0x7ffff6e040b2 in __libc_start_main /build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16 #26 0x41f35d in _start (asan/bin/root.exe+0x41f35d) Address 0x7ffff3afd82a is located in stack of thread T0 at offset 42 in frame #0 0x7fffe99b3d8f in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2456 This frame has 21 object(s): [32, 56) 'sLine' (line 2462) <== Memory access at offset 42 is inside this variable [96, 104) 'R__guard2471' (line 2471) [128, 136) 'R__guard2488' (line 2488) [160, 176) 'interpreterFlagsRAII' (line 2491) [192, 240) 'result' (line 2511) [272, 276) 'compRes' (line 2512) [288, 312) 'mod_line' (line 2517) [352, 376) 'aclicMode' (line 2518) [416, 440) 'arguments' (line 2519) [480, 504) 'io' (line 2520) [544, 568) 'fname' (line 2521) [608, 632) 'ref.tmp' (line 2547) [672, 696) 'ref.tmp145' (line 2547) [736, 768) 'code' (line 2555) [800, 832) 'codeline' (line 2556) [864, 1384) 'in' (line 2559) [1520, 1552) 'ref.tmp176' (line 2562) [1584, 1600) 'agg.tmp' [1616, 1624) 'ref.tmp198' (line 2568) [1648, 1664) 'agg.tmp207' [1680, 1696) 'autoParseRaii' (line 2588) HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-return src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 in cling::MetaLexer::Lex(cling::Token&) Shadow bytes around the buggy address: 0x10007e757ab0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ac0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ad0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ae0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757af0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 =>0x10007e757b00: f5 f5 f5 f5 f5[f5]f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b10: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b20: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b30: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b40: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b50: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==529104==ABORTING at src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:49 at src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:41 compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0, disableValuePrinting=false) at src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:314 input_line=0x7ffff3afd829 "#define XYZ 21", compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0) at src/core/metacling/src/TCling.cxx:2431 error=0x7fffd78cb0f4 <x>) at src/core/metacling/src/TCling.cxx:2591 sync=false, err=0x7fffd78cb0f4 <x>) at src/core/base/src/TApplication.cxx:1472 line=0x7fffd78c9000 "#define XYZ 21", error=0x7fffd78cb0f4 <x>) at src/core/base/src/TROOT.cxx:2328 from asan/roottest/cling/other/fileClose_C.so filename=0x6070000f0fd0 "asan/roottest/cling/other/fileClose_C.so", flag=257) at /home/axel/build/llvm/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:6270 at src/interpreter/cling/lib/Utils/PlatformPosix.cpp:118 permanent=false, resolved=true) at src/interpreter/cling/lib/Interpreter/DynamicLibraryManager.cpp:184 at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1444 T=0x0) at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1560 at src/interpreter/cling/lib/MetaProcessor/MetaSema.cpp:57 actionResult=@0x7ffff39532b0: cling::MetaSema::AR_Success)
2021-04-29 16:20:43 +03:00
parser.isMetaCommand(actionResult, result)) {
Lifetime of MetaParser is that of its input: Before, MetaParser might have pointed to a StringRef whose storage was gone, see asan failure in roottest/cling/other/runfileClose.C below. This was caused by recursive uses of MetaParser; see stack trace below: the inner recursion returned, but as the same MetaParser object was used by both frames, the objects cursor now pointed to freed memory. Instead, create a MetaParser (and MetaLexer) object per input. That way, their lifetime corresponds to the lifetime of their input. ================================================================= ==529104==ERROR: AddressSanitizer: stack-use-after-return on address 0x7ffff3afd82a at pc 0x7fffea18df6d bp 0x7fffffff8170 sp 0x7fffffff8168 READ of size 1 at 0x7ffff3afd82a thread T0 [Detaching after fork from child process 529183] #0 0x7fffea18df6c in cling::MetaLexer::Lex(cling::Token&) src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 #1 0x7fffea190d7c in cling::MetaParser::lookAhead(unsigned int) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:89:15 #2 0x7fffea190bd5 in cling::MetaParser::consumeToken() src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:49:5 #3 0x7fffea191d4d in cling::MetaParser::isLCommand(cling::MetaSema::ActionResult&) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:147:9 #4 0x7fffea1914dd in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:123:12 #5 0x7fffea191216 in cling::MetaParser::isMetaCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:101:33 #6 0x7fffea14e5aa in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:317:24 #7 0x7fffe99b67b7 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) src/core/metacling/src/TCling.cxx:2431:29 #8 0x7fffe99bde30 in TCling::Load(char const*, bool) src/core/metacling/src/TCling.cxx:3454:10 #9 0x7ffff7865f11 in TSystem::Load(char const*, char const*, bool) src/core/base/src/TSystem.cxx:1941:27 #10 0x7ffff7b8a0e3 in TUnixSystem::Load(char const*, char const*, bool) src/core/unix/src/TUnixSystem.cxx:2789:20 #11 0x7fffd78dd08b (<unknown module>) #12 0x7fffe9f8a5d9 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const src/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp:376:3 #13 0x7fffe9d73dc2 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1141:20 #14 0x7fffe9d6e317 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1391:29 #15 0x7fffe9d6c1fe in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:819:9 #16 0x7fffea151826 in cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:507:22 #17 0x7fffe99b585b in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2570:39 #18 0x7fffe99bbfee in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:3496:17 #19 0x7ffff77203d3 in TApplication::ExecuteFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1608:30 #20 0x7ffff771ebdf in TApplication::ProcessFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1480:11 #21 0x7ffff771e385 in TApplication::ProcessLine(char const*, bool, int*) src/core/base/src/TApplication.cxx:1453:14 #22 0x7ffff7f8157a in TRint::ProcessLineNr(char const*, char const*, int*) src/core/rint/src/TRint.cxx:766:11 #23 0x7ffff7f802f0 in TRint::Run(bool) src/core/rint/src/TRint.cxx:424:22 #24 0x4ff96d in main src/main/src/rmain.cxx:30:12 #25 0x7ffff6e040b2 in __libc_start_main /build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16 #26 0x41f35d in _start (asan/bin/root.exe+0x41f35d) Address 0x7ffff3afd82a is located in stack of thread T0 at offset 42 in frame #0 0x7fffe99b3d8f in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2456 This frame has 21 object(s): [32, 56) 'sLine' (line 2462) <== Memory access at offset 42 is inside this variable [96, 104) 'R__guard2471' (line 2471) [128, 136) 'R__guard2488' (line 2488) [160, 176) 'interpreterFlagsRAII' (line 2491) [192, 240) 'result' (line 2511) [272, 276) 'compRes' (line 2512) [288, 312) 'mod_line' (line 2517) [352, 376) 'aclicMode' (line 2518) [416, 440) 'arguments' (line 2519) [480, 504) 'io' (line 2520) [544, 568) 'fname' (line 2521) [608, 632) 'ref.tmp' (line 2547) [672, 696) 'ref.tmp145' (line 2547) [736, 768) 'code' (line 2555) [800, 832) 'codeline' (line 2556) [864, 1384) 'in' (line 2559) [1520, 1552) 'ref.tmp176' (line 2562) [1584, 1600) 'agg.tmp' [1616, 1624) 'ref.tmp198' (line 2568) [1648, 1664) 'agg.tmp207' [1680, 1696) 'autoParseRaii' (line 2588) HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-return src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 in cling::MetaLexer::Lex(cling::Token&) Shadow bytes around the buggy address: 0x10007e757ab0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ac0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ad0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ae0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757af0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 =>0x10007e757b00: f5 f5 f5 f5 f5[f5]f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b10: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b20: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b30: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b40: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b50: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==529104==ABORTING at src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:49 at src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:41 compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0, disableValuePrinting=false) at src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:314 input_line=0x7ffff3afd829 "#define XYZ 21", compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0) at src/core/metacling/src/TCling.cxx:2431 error=0x7fffd78cb0f4 <x>) at src/core/metacling/src/TCling.cxx:2591 sync=false, err=0x7fffd78cb0f4 <x>) at src/core/base/src/TApplication.cxx:1472 line=0x7fffd78c9000 "#define XYZ 21", error=0x7fffd78cb0f4 <x>) at src/core/base/src/TROOT.cxx:2328 from asan/roottest/cling/other/fileClose_C.so filename=0x6070000f0fd0 "asan/roottest/cling/other/fileClose_C.so", flag=257) at /home/axel/build/llvm/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:6270 at src/interpreter/cling/lib/Utils/PlatformPosix.cpp:118 permanent=false, resolved=true) at src/interpreter/cling/lib/Interpreter/DynamicLibraryManager.cpp:184 at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1444 T=0x0) at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1560 at src/interpreter/cling/lib/MetaProcessor/MetaSema.cpp:57 actionResult=@0x7ffff39532b0: cling::MetaSema::AR_Success)
2021-04-29 16:20:43 +03:00
if (parser.isQuitRequested())
return -1;
if (actionResult != MetaSema::AR_Success)
compRes = Interpreter::kFailure;
// ExpectedIndent might have changed after meta command.
return m_InputValidator->getExpectedIndent();
}
// Check if the current statement is now complete. If not, return to
// prompt for more.
if (m_InputValidator->validate(input_line) == InputValidator::kIncomplete) {
compRes = Interpreter::kMoreInputExpected;
return m_InputValidator->getExpectedIndent();
}
// We have a complete statement, compile and execute it.
std::string input;
m_InputValidator->reset(&input);
// if (m_Options.RawInput)
// compResLocal = m_Interp.declare(input);
// else
2017-05-02 12:19:14 +03:00
compRes = m_Interp.process(input, result, /*Transaction*/ nullptr,
disableValuePrinting);
return 0;
}
void MetaProcessor::cancelContinuation() const {
m_InputValidator->reset();
}
bool MetaProcessor::awaitingMoreInput() const {
return m_InputValidator->getLastResult() ==
InputValidator::ValidationResult::kIncomplete;
}
int MetaProcessor::getExpectedIndent() const {
return m_InputValidator->getExpectedIndent();
}
static Interpreter::CompilationResult reportIOErr(llvm::StringRef File,
const char* What) {
cling::errs() << "Error in cling::MetaProcessor: "
"cannot " << What << " input: '" << File << "'\n";
return Interpreter::kFailure;
}
Interpreter::CompilationResult
MetaProcessor::readInputFromFile(llvm::StringRef filename,
2014-05-19 18:34:21 +04:00
Value* result,
size_t posOpenCurly,
bool lineByLine) {
// FIXME: This will fail for Unicode BOMs (and seems really weird)
{
// check that it's not binary:
std::ifstream in(filename.str().c_str(), std::ios::in | std::ios::binary);
if (in.fail())
return reportIOErr(filename, "open");
char magic[1024] = {0};
in.read(magic, sizeof(magic));
size_t readMagic = in.gcount();
// Binary files < 300 bytes are rare, and below newlines etc make the
// heuristic unreliable.
if (!in.fail() && readMagic >= 300) {
llvm::StringRef magicStr(magic,in.gcount());
llvm::file_magic fileType
= llvm::identify_magic(magicStr);
if (fileType != llvm::file_magic::unknown)
return reportIOErr(filename, "read from binary");
unsigned printable = 0;
for (size_t i = 0; i < readMagic; ++i)
if (isprint(magic[i]))
++printable;
if (10 * printable < 5 * readMagic) {
// 50% printable for ASCII files should be a safe guess.
return reportIOErr(filename, "won't read from likely binary");
}
}
}
// Windows requires std::ifstream::binary to properly handle
// CRLF and LF line endings
std::ifstream in(filename.str().c_str(), std::ifstream::binary);
if (in.fail())
return reportIOErr(filename, "open");
in.seekg(0, std::ios::end);
if (in.fail())
return reportIOErr(filename, "seek");
size_t size = in.tellg();
if (in.fail())
return reportIOErr(filename, "tell");
in.seekg(0);
if (in.fail())
return reportIOErr(filename, "rewind");
std::string content(size, ' ');
2014-08-04 06:05:42 +04:00
in.read(&content[0], size);
if (in.fail())
return reportIOErr(filename, "read");
static const char whitespace[] = " \t\r\n";
if (content.length() > 2 && content[0] == '#' && content[1] == '!') {
// Convert shebang line to comment. That's nice because it doesn't
// change the content size, leaving posOpenCurly untouched.
content[0] = '/';
content[1] = '/';
}
if (posOpenCurly != (size_t)-1 && !content.empty()) {
assert(content[posOpenCurly] == '{'
&& "No curly at claimed position of opening curly!");
// hide the curly brace:
content[posOpenCurly] = ' ';
// and the matching closing '}'
size_t posCloseCurly = content.find_last_not_of(whitespace);
if (posCloseCurly != std::string::npos) {
if (content[posCloseCurly] == ';' && content[posCloseCurly-1] == '}') {
content[posCloseCurly--] = ' '; // replace ';' and enter next if
}
if (content[posCloseCurly] == '}') {
content[posCloseCurly] = ' '; // replace '}'
} else {
std::string::size_type posBlockClose = content.find_last_of('}');
if (posBlockClose != std::string::npos) {
content[posBlockClose] = ' '; // replace '}'
}
std::string::size_type posComment
= content.find_first_not_of(whitespace, posBlockClose);
if (posComment != std::string::npos
&& content[posComment] == '/' && content[posComment+1] == '/') {
// More text (comments) are okay after the last '}', but
// we can not easily find it to remove it (so we need to upgrade
// this code to better handle the case with comments or
// preprocessor code before and after the leading { and
// trailing })
while (posComment <= posCloseCurly) {
content[posComment++] = ' '; // replace '}' and comment
}
} else {
content[posCloseCurly] = '{';
// By putting the '{' back, we keep the code as consistent as
// the user wrote it ... but we should still warn that we not
// goint to treat this file an unamed macro.
cling::errs()
<< "Warning in cling::MetaProcessor: can not find the closing '}', "
<< llvm::sys::path::filename(filename)
<< " is not handled as an unamed script!\n";
} // did not find "//"
} // remove comments after the trailing '}'
} // find '}'
} // ignore outermost block
m_CurrentlyExecutingFile = filename;
bool topmost = !m_TopExecutingFile.data();
if (topmost)
m_TopExecutingFile = m_CurrentlyExecutingFile;
std::string path(filename.str());
#ifdef _WIN32
std::size_t p = 0;
while ((p = path.find('\\', p)) != std::string::npos) {
path.insert(p, "\\");
p += 2;
}
#endif
content.insert(0, "#line 2 \"" + path + "\" \n");
// We don't want to value print the results of a unnamed macro.
if (content.back() != ';')
content.append(";");
Interpreter::InputFlagsRAII RAII(m_Interp, Interpreter::kInputFromFile |
(lineByLine ? Interpreter::kIFFLineByLine : 0));
Interpreter::CompilationResult ret = Interpreter::kSuccess;
if (lineByLine) {
int rslt = 0;
std::string line;
std::stringstream ss(content);
while (std::getline(ss, line, '\n')) {
rslt = process(line, ret, result);
if (ret == Interpreter::kFailure)
break;
}
if (rslt) {
cling::errs() << "Error in cling::MetaProcessor: file "
<< llvm::sys::path::filename(filename)
<< " is incomplete (missing parenthesis or similar)!\n";
}
} else
ret = m_Interp.process(content, result);
m_CurrentlyExecutingFile = llvm::StringRef();
if (topmost)
m_TopExecutingFile = llvm::StringRef();
return ret;
}
void MetaProcessor::setStdStream(llvm::StringRef file, RedirectionScope scope,
bool append) {
assert((scope & kSTDOUT || scope & kSTDERR) && "Invalid RedirectionScope");
if (!m_RedirectOutput)
m_RedirectOutput.reset(new RedirectOutput);
m_RedirectOutput->redirect(file, append, scope);
if (m_RedirectOutput->empty())
m_RedirectOutput.reset();
}
2015-01-21 18:51:20 +03:00
void MetaProcessor::registerUnloadPoint(const Transaction* T,
llvm::StringRef filename) {
Lifetime of MetaParser is that of its input: Before, MetaParser might have pointed to a StringRef whose storage was gone, see asan failure in roottest/cling/other/runfileClose.C below. This was caused by recursive uses of MetaParser; see stack trace below: the inner recursion returned, but as the same MetaParser object was used by both frames, the objects cursor now pointed to freed memory. Instead, create a MetaParser (and MetaLexer) object per input. That way, their lifetime corresponds to the lifetime of their input. ================================================================= ==529104==ERROR: AddressSanitizer: stack-use-after-return on address 0x7ffff3afd82a at pc 0x7fffea18df6d bp 0x7fffffff8170 sp 0x7fffffff8168 READ of size 1 at 0x7ffff3afd82a thread T0 [Detaching after fork from child process 529183] #0 0x7fffea18df6c in cling::MetaLexer::Lex(cling::Token&) src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 #1 0x7fffea190d7c in cling::MetaParser::lookAhead(unsigned int) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:89:15 #2 0x7fffea190bd5 in cling::MetaParser::consumeToken() src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:49:5 #3 0x7fffea191d4d in cling::MetaParser::isLCommand(cling::MetaSema::ActionResult&) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:147:9 #4 0x7fffea1914dd in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:123:12 #5 0x7fffea191216 in cling::MetaParser::isMetaCommand(cling::MetaSema::ActionResult&, cling::Value*) src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:101:33 #6 0x7fffea14e5aa in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:317:24 #7 0x7fffe99b67b7 in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) src/core/metacling/src/TCling.cxx:2431:29 #8 0x7fffe99bde30 in TCling::Load(char const*, bool) src/core/metacling/src/TCling.cxx:3454:10 #9 0x7ffff7865f11 in TSystem::Load(char const*, char const*, bool) src/core/base/src/TSystem.cxx:1941:27 #10 0x7ffff7b8a0e3 in TUnixSystem::Load(char const*, char const*, bool) src/core/unix/src/TUnixSystem.cxx:2789:20 #11 0x7fffd78dd08b (<unknown module>) #12 0x7fffe9f8a5d9 in cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const src/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp:376:3 #13 0x7fffe9d73dc2 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1141:20 #14 0x7fffe9d6e317 in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1391:29 #15 0x7fffe9d6c1fe in cling::Interpreter::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) src/interpreter/cling/lib/Interpreter/Interpreter.cpp:819:9 #16 0x7fffea151826 in cling::MetaProcessor::readInputFromFile(llvm::StringRef, cling::Value*, unsigned long, bool) src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:507:22 #17 0x7fffe99b585b in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2570:39 #18 0x7fffe99bbfee in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:3496:17 #19 0x7ffff77203d3 in TApplication::ExecuteFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1608:30 #20 0x7ffff771ebdf in TApplication::ProcessFile(char const*, int*, bool) src/core/base/src/TApplication.cxx:1480:11 #21 0x7ffff771e385 in TApplication::ProcessLine(char const*, bool, int*) src/core/base/src/TApplication.cxx:1453:14 #22 0x7ffff7f8157a in TRint::ProcessLineNr(char const*, char const*, int*) src/core/rint/src/TRint.cxx:766:11 #23 0x7ffff7f802f0 in TRint::Run(bool) src/core/rint/src/TRint.cxx:424:22 #24 0x4ff96d in main src/main/src/rmain.cxx:30:12 #25 0x7ffff6e040b2 in __libc_start_main /build/glibc-YbNSs7/glibc-2.31/csu/../csu/libc-start.c:308:16 #26 0x41f35d in _start (asan/bin/root.exe+0x41f35d) Address 0x7ffff3afd82a is located in stack of thread T0 at offset 42 in frame #0 0x7fffe99b3d8f in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) src/core/metacling/src/TCling.cxx:2456 This frame has 21 object(s): [32, 56) 'sLine' (line 2462) <== Memory access at offset 42 is inside this variable [96, 104) 'R__guard2471' (line 2471) [128, 136) 'R__guard2488' (line 2488) [160, 176) 'interpreterFlagsRAII' (line 2491) [192, 240) 'result' (line 2511) [272, 276) 'compRes' (line 2512) [288, 312) 'mod_line' (line 2517) [352, 376) 'aclicMode' (line 2518) [416, 440) 'arguments' (line 2519) [480, 504) 'io' (line 2520) [544, 568) 'fname' (line 2521) [608, 632) 'ref.tmp' (line 2547) [672, 696) 'ref.tmp145' (line 2547) [736, 768) 'code' (line 2555) [800, 832) 'codeline' (line 2556) [864, 1384) 'in' (line 2559) [1520, 1552) 'ref.tmp176' (line 2562) [1584, 1600) 'agg.tmp' [1616, 1624) 'ref.tmp198' (line 2568) [1648, 1664) 'agg.tmp207' [1680, 1696) 'autoParseRaii' (line 2588) HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-return src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:58:11 in cling::MetaLexer::Lex(cling::Token&) Shadow bytes around the buggy address: 0x10007e757ab0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ac0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ad0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757ae0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757af0: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 =>0x10007e757b00: f5 f5 f5 f5 f5[f5]f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b10: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b20: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b30: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b40: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 0x10007e757b50: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==529104==ABORTING at src/interpreter/cling/lib/MetaProcessor/MetaLexer.cpp:49 at src/interpreter/cling/lib/MetaProcessor/MetaParser.cpp:41 compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0, disableValuePrinting=false) at src/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp:314 input_line=0x7ffff3afd829 "#define XYZ 21", compRes=@0x7ffff3afd910: cling::Interpreter::kSuccess, result=0x7ffff3afd8c0) at src/core/metacling/src/TCling.cxx:2431 error=0x7fffd78cb0f4 <x>) at src/core/metacling/src/TCling.cxx:2591 sync=false, err=0x7fffd78cb0f4 <x>) at src/core/base/src/TApplication.cxx:1472 line=0x7fffd78c9000 "#define XYZ 21", error=0x7fffd78cb0f4 <x>) at src/core/base/src/TROOT.cxx:2328 from asan/roottest/cling/other/fileClose_C.so filename=0x6070000f0fd0 "asan/roottest/cling/other/fileClose_C.so", flag=257) at /home/axel/build/llvm/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:6270 at src/interpreter/cling/lib/Utils/PlatformPosix.cpp:118 permanent=false, resolved=true) at src/interpreter/cling/lib/Interpreter/DynamicLibraryManager.cpp:184 at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1444 T=0x0) at src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1560 at src/interpreter/cling/lib/MetaProcessor/MetaSema.cpp:57 actionResult=@0x7ffff39532b0: cling::MetaSema::AR_Success)
2021-04-29 16:20:43 +03:00
m_MetaSema->registerUnloadPoint(T, filename);
2015-01-21 18:51:20 +03:00
}
} // end namespace cling