1 //===--- RuntimeDyldChecker.cpp - RuntimeDyld tester framework --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCDisassembler.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/Support/StringRefMemoryObject.h"
16 #include "llvm/Support/Path.h"
17 #include "RuntimeDyldCheckerImpl.h"
18 #include "RuntimeDyldImpl.h"
22 #define DEBUG_TYPE "rtdyld"
28 // Helper class that implements the language evaluated by RuntimeDyldChecker.
29 class RuntimeDyldCheckerExprEval {
31 RuntimeDyldCheckerExprEval(const RuntimeDyldCheckerImpl &Checker,
32 raw_ostream &ErrStream)
35 bool evaluate(StringRef Expr) const {
36 // Expect equality expression of the form 'LHS = RHS'.
38 size_t EQIdx = Expr.find('=');
40 ParseContext OutsideLoad(false);
43 StringRef LHSExpr = Expr.substr(0, EQIdx).rtrim();
44 StringRef RemainingExpr;
46 std::tie(LHSResult, RemainingExpr) =
47 evalComplexExpr(evalSimpleExpr(LHSExpr, OutsideLoad), OutsideLoad);
48 if (LHSResult.hasError())
49 return handleError(Expr, LHSResult);
50 if (RemainingExpr != "")
51 return handleError(Expr, unexpectedToken(RemainingExpr, LHSExpr, ""));
54 StringRef RHSExpr = Expr.substr(EQIdx + 1).ltrim();
56 std::tie(RHSResult, RemainingExpr) =
57 evalComplexExpr(evalSimpleExpr(RHSExpr, OutsideLoad), OutsideLoad);
58 if (RHSResult.hasError())
59 return handleError(Expr, RHSResult);
60 if (RemainingExpr != "")
61 return handleError(Expr, unexpectedToken(RemainingExpr, RHSExpr, ""));
63 if (LHSResult.getValue() != RHSResult.getValue()) {
64 Checker.ErrStream << "Expression '" << Expr << "' is false: "
65 << format("0x%" PRIx64, LHSResult.getValue())
66 << " != " << format("0x%" PRIx64, RHSResult.getValue())
74 // RuntimeDyldCheckerExprEval requires some context when parsing exprs. In
75 // particular, it needs to know whether a symbol is being evaluated in the
76 // context of a load, in which case we want the linker's local address for
77 // the symbol, or outside of a load, in which case we want the symbol's
78 // address in the remote target.
82 ParseContext(bool IsInsideLoad) : IsInsideLoad(IsInsideLoad) {}
85 const RuntimeDyldCheckerImpl &Checker;
87 enum class BinOpToken : unsigned {
99 EvalResult() : Value(0), ErrorMsg("") {}
100 EvalResult(uint64_t Value) : Value(Value), ErrorMsg("") {}
101 EvalResult(std::string ErrorMsg) : Value(0), ErrorMsg(ErrorMsg) {}
102 uint64_t getValue() const { return Value; }
103 bool hasError() const { return ErrorMsg != ""; }
104 const std::string &getErrorMsg() const { return ErrorMsg; }
108 std::string ErrorMsg;
111 StringRef getTokenForError(StringRef Expr) const {
115 StringRef Token, Remaining;
116 if (isalpha(Expr[0]))
117 std::tie(Token, Remaining) = parseSymbol(Expr);
118 else if (isdigit(Expr[0]))
119 std::tie(Token, Remaining) = parseNumberString(Expr);
122 if (Expr.startswith("<<") || Expr.startswith(">>"))
124 Token = Expr.substr(0, TokLen);
129 EvalResult unexpectedToken(StringRef TokenStart, StringRef SubExpr,
130 StringRef ErrText) const {
131 std::string ErrorMsg("Encountered unexpected token '");
132 ErrorMsg += getTokenForError(TokenStart);
134 ErrorMsg += "' while parsing subexpression '";
142 return EvalResult(std::move(ErrorMsg));
145 bool handleError(StringRef Expr, const EvalResult &R) const {
146 assert(R.hasError() && "Not an error result.");
147 Checker.ErrStream << "Error evaluating expression '" << Expr
148 << "': " << R.getErrorMsg() << "\n";
152 std::pair<BinOpToken, StringRef> parseBinOpToken(StringRef Expr) const {
154 return std::make_pair(BinOpToken::Invalid, "");
156 // Handle the two 2-character tokens.
157 if (Expr.startswith("<<"))
158 return std::make_pair(BinOpToken::ShiftLeft, Expr.substr(2).ltrim());
159 if (Expr.startswith(">>"))
160 return std::make_pair(BinOpToken::ShiftRight, Expr.substr(2).ltrim());
162 // Handle one-character tokens.
166 return std::make_pair(BinOpToken::Invalid, Expr);
168 Op = BinOpToken::Add;
171 Op = BinOpToken::Sub;
174 Op = BinOpToken::BitwiseAnd;
177 Op = BinOpToken::BitwiseOr;
181 return std::make_pair(Op, Expr.substr(1).ltrim());
184 EvalResult computeBinOpResult(BinOpToken Op, const EvalResult &LHSResult,
185 const EvalResult &RHSResult) const {
188 llvm_unreachable("Tried to evaluate unrecognized operation.");
189 case BinOpToken::Add:
190 return EvalResult(LHSResult.getValue() + RHSResult.getValue());
191 case BinOpToken::Sub:
192 return EvalResult(LHSResult.getValue() - RHSResult.getValue());
193 case BinOpToken::BitwiseAnd:
194 return EvalResult(LHSResult.getValue() & RHSResult.getValue());
195 case BinOpToken::BitwiseOr:
196 return EvalResult(LHSResult.getValue() | RHSResult.getValue());
197 case BinOpToken::ShiftLeft:
198 return EvalResult(LHSResult.getValue() << RHSResult.getValue());
199 case BinOpToken::ShiftRight:
200 return EvalResult(LHSResult.getValue() >> RHSResult.getValue());
204 // Parse a symbol and return a (string, string) pair representing the symbol
205 // name and expression remaining to be parsed.
206 std::pair<StringRef, StringRef> parseSymbol(StringRef Expr) const {
207 size_t FirstNonSymbol = Expr.find_first_not_of("0123456789"
208 "abcdefghijklmnopqrstuvwxyz"
209 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
211 return std::make_pair(Expr.substr(0, FirstNonSymbol),
212 Expr.substr(FirstNonSymbol).ltrim());
215 // Evaluate a call to decode_operand. Decode the instruction operand at the
216 // given symbol and get the value of the requested operand.
217 // Returns an error if the instruction cannot be decoded, or the requested
218 // operand is not an immediate.
219 // On success, retuns a pair containing the value of the operand, plus
220 // the expression remaining to be evaluated.
221 std::pair<EvalResult, StringRef> evalDecodeOperand(StringRef Expr) const {
222 if (!Expr.startswith("("))
223 return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), "");
224 StringRef RemainingExpr = Expr.substr(1).ltrim();
226 std::tie(Symbol, RemainingExpr) = parseSymbol(RemainingExpr);
228 if (!Checker.isSymbolValid(Symbol))
229 return std::make_pair(
230 EvalResult(("Cannot decode unknown symbol '" + Symbol + "'").str()),
233 if (!RemainingExpr.startswith(","))
234 return std::make_pair(
235 unexpectedToken(RemainingExpr, RemainingExpr, "expected ','"), "");
236 RemainingExpr = RemainingExpr.substr(1).ltrim();
238 EvalResult OpIdxExpr;
239 std::tie(OpIdxExpr, RemainingExpr) = evalNumberExpr(RemainingExpr);
240 if (OpIdxExpr.hasError())
241 return std::make_pair(OpIdxExpr, "");
243 if (!RemainingExpr.startswith(")"))
244 return std::make_pair(
245 unexpectedToken(RemainingExpr, RemainingExpr, "expected ')'"), "");
246 RemainingExpr = RemainingExpr.substr(1).ltrim();
250 if (!decodeInst(Symbol, Inst, Size))
251 return std::make_pair(
252 EvalResult(("Couldn't decode instruction at '" + Symbol + "'").str()),
255 unsigned OpIdx = OpIdxExpr.getValue();
256 if (OpIdx >= Inst.getNumOperands()) {
258 raw_string_ostream ErrMsgStream(ErrMsg);
259 ErrMsgStream << "Invalid operand index '" << format("%i", OpIdx)
260 << "' for instruction '" << Symbol
261 << "'. Instruction has only "
262 << format("%i", Inst.getNumOperands())
263 << " operands.\nInstruction is:\n ";
264 Inst.dump_pretty(ErrMsgStream,
265 Checker.Disassembler->getContext().getAsmInfo(),
266 Checker.InstPrinter);
267 return std::make_pair(EvalResult(ErrMsgStream.str()), "");
270 const MCOperand &Op = Inst.getOperand(OpIdx);
273 raw_string_ostream ErrMsgStream(ErrMsg);
274 ErrMsgStream << "Operand '" << format("%i", OpIdx) << "' of instruction '"
275 << Symbol << "' is not an immediate.\nInstruction is:\n ";
276 Inst.dump_pretty(ErrMsgStream,
277 Checker.Disassembler->getContext().getAsmInfo(),
278 Checker.InstPrinter);
280 return std::make_pair(EvalResult(ErrMsgStream.str()), "");
283 return std::make_pair(EvalResult(Op.getImm()), RemainingExpr);
286 // Evaluate a call to next_pc.
287 // Decode the instruction at the given symbol and return the following program
289 // Returns an error if the instruction cannot be decoded.
290 // On success, returns a pair containing the next PC, plus of the
291 // expression remaining to be evaluated.
292 std::pair<EvalResult, StringRef> evalNextPC(StringRef Expr,
293 ParseContext PCtx) const {
294 if (!Expr.startswith("("))
295 return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), "");
296 StringRef RemainingExpr = Expr.substr(1).ltrim();
298 std::tie(Symbol, RemainingExpr) = parseSymbol(RemainingExpr);
300 if (!Checker.isSymbolValid(Symbol))
301 return std::make_pair(
302 EvalResult(("Cannot decode unknown symbol '" + Symbol + "'").str()),
305 if (!RemainingExpr.startswith(")"))
306 return std::make_pair(
307 unexpectedToken(RemainingExpr, RemainingExpr, "expected ')'"), "");
308 RemainingExpr = RemainingExpr.substr(1).ltrim();
312 if (!decodeInst(Symbol, Inst, InstSize))
313 return std::make_pair(
314 EvalResult(("Couldn't decode instruction at '" + Symbol + "'").str()),
317 uint64_t SymbolAddr = PCtx.IsInsideLoad
318 ? Checker.getSymbolLinkerAddr(Symbol)
319 : Checker.getSymbolRemoteAddr(Symbol);
320 uint64_t NextPC = SymbolAddr + InstSize;
322 return std::make_pair(EvalResult(NextPC), RemainingExpr);
325 // Evaluate a call to stub_addr.
326 // Look up and return the address of the stub for the given
327 // (<file name>, <section name>, <symbol name>) tuple.
328 // On success, returns a pair containing the stub address, plus the expression
329 // remaining to be evaluated.
330 std::pair<EvalResult, StringRef> evalStubAddr(StringRef Expr,
331 ParseContext PCtx) const {
332 if (!Expr.startswith("("))
333 return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), "");
334 StringRef RemainingExpr = Expr.substr(1).ltrim();
336 // Handle file-name specially, as it may contain characters that aren't
337 // legal for symbols.
339 size_t ComaIdx = RemainingExpr.find(',');
340 FileName = RemainingExpr.substr(0, ComaIdx).rtrim();
341 RemainingExpr = RemainingExpr.substr(ComaIdx).ltrim();
343 if (!RemainingExpr.startswith(","))
344 return std::make_pair(
345 unexpectedToken(RemainingExpr, Expr, "expected ','"), "");
346 RemainingExpr = RemainingExpr.substr(1).ltrim();
348 StringRef SectionName;
349 std::tie(SectionName, RemainingExpr) = parseSymbol(RemainingExpr);
351 if (!RemainingExpr.startswith(","))
352 return std::make_pair(
353 unexpectedToken(RemainingExpr, Expr, "expected ','"), "");
354 RemainingExpr = RemainingExpr.substr(1).ltrim();
357 std::tie(Symbol, RemainingExpr) = parseSymbol(RemainingExpr);
359 if (!RemainingExpr.startswith(")"))
360 return std::make_pair(
361 unexpectedToken(RemainingExpr, Expr, "expected ')'"), "");
362 RemainingExpr = RemainingExpr.substr(1).ltrim();
365 std::string ErrorMsg = "";
366 std::tie(StubAddr, ErrorMsg) = Checker.getStubAddrFor(
367 FileName, SectionName, Symbol, PCtx.IsInsideLoad);
370 return std::make_pair(EvalResult(ErrorMsg), "");
372 return std::make_pair(EvalResult(StubAddr), RemainingExpr);
375 std::pair<EvalResult, StringRef> evalSectionAddr(StringRef Expr,
376 ParseContext PCtx) const {
377 if (!Expr.startswith("("))
378 return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), "");
379 StringRef RemainingExpr = Expr.substr(1).ltrim();
381 // Handle file-name specially, as it may contain characters that aren't
382 // legal for symbols.
384 size_t ComaIdx = RemainingExpr.find(',');
385 FileName = RemainingExpr.substr(0, ComaIdx).rtrim();
386 RemainingExpr = RemainingExpr.substr(ComaIdx).ltrim();
388 if (!RemainingExpr.startswith(","))
389 return std::make_pair(
390 unexpectedToken(RemainingExpr, Expr, "expected ','"), "");
391 RemainingExpr = RemainingExpr.substr(1).ltrim();
393 StringRef SectionName;
394 std::tie(SectionName, RemainingExpr) = parseSymbol(RemainingExpr);
396 if (!RemainingExpr.startswith(")"))
397 return std::make_pair(
398 unexpectedToken(RemainingExpr, Expr, "expected ')'"), "");
399 RemainingExpr = RemainingExpr.substr(1).ltrim();
402 std::string ErrorMsg = "";
403 std::tie(StubAddr, ErrorMsg) = Checker.getSectionAddr(
404 FileName, SectionName, PCtx.IsInsideLoad);
407 return std::make_pair(EvalResult(ErrorMsg), "");
409 return std::make_pair(EvalResult(StubAddr), RemainingExpr);
412 // Evaluate an identiefer expr, which may be a symbol, or a call to
413 // one of the builtin functions: get_insn_opcode or get_insn_length.
414 // Return the result, plus the expression remaining to be parsed.
415 std::pair<EvalResult, StringRef> evalIdentifierExpr(StringRef Expr,
416 ParseContext PCtx) const {
418 StringRef RemainingExpr;
419 std::tie(Symbol, RemainingExpr) = parseSymbol(Expr);
421 // Check for builtin function calls.
422 if (Symbol == "decode_operand")
423 return evalDecodeOperand(RemainingExpr);
424 else if (Symbol == "next_pc")
425 return evalNextPC(RemainingExpr, PCtx);
426 else if (Symbol == "stub_addr")
427 return evalStubAddr(RemainingExpr, PCtx);
428 else if (Symbol == "section_addr")
429 return evalSectionAddr(RemainingExpr, PCtx);
431 if (!Checker.isSymbolValid(Symbol)) {
432 std::string ErrMsg("No known address for symbol '");
435 if (Symbol.startswith("L"))
436 ErrMsg += " (this appears to be an assembler local label - "
437 " perhaps drop the 'L'?)";
439 return std::make_pair(EvalResult(ErrMsg), "");
442 // The value for the symbol depends on the context we're evaluating in:
443 // Inside a load this is the address in the linker's memory, outside a
444 // load it's the address in the target processes memory.
445 uint64_t Value = PCtx.IsInsideLoad ? Checker.getSymbolLinkerAddr(Symbol)
446 : Checker.getSymbolRemoteAddr(Symbol);
448 // Looks like a plain symbol reference.
449 return std::make_pair(EvalResult(Value), RemainingExpr);
452 // Parse a number (hexadecimal or decimal) and return a (string, string)
453 // pair representing the number and the expression remaining to be parsed.
454 std::pair<StringRef, StringRef> parseNumberString(StringRef Expr) const {
455 size_t FirstNonDigit = StringRef::npos;
456 if (Expr.startswith("0x")) {
457 FirstNonDigit = Expr.find_first_not_of("0123456789abcdefABCDEF", 2);
458 if (FirstNonDigit == StringRef::npos)
459 FirstNonDigit = Expr.size();
461 FirstNonDigit = Expr.find_first_not_of("0123456789");
462 if (FirstNonDigit == StringRef::npos)
463 FirstNonDigit = Expr.size();
465 return std::make_pair(Expr.substr(0, FirstNonDigit),
466 Expr.substr(FirstNonDigit));
469 // Evaluate a constant numeric expression (hexidecimal or decimal) and
470 // return a pair containing the result, and the expression remaining to be
472 std::pair<EvalResult, StringRef> evalNumberExpr(StringRef Expr) const {
474 StringRef RemainingExpr;
475 std::tie(ValueStr, RemainingExpr) = parseNumberString(Expr);
477 if (ValueStr.empty() || !isdigit(ValueStr[0]))
478 return std::make_pair(
479 unexpectedToken(RemainingExpr, RemainingExpr, "expected number"), "");
481 ValueStr.getAsInteger(0, Value);
482 return std::make_pair(EvalResult(Value), RemainingExpr);
485 // Evaluate an expression of the form "(<expr>)" and return a pair
486 // containing the result of evaluating <expr>, plus the expression
487 // remaining to be parsed.
488 std::pair<EvalResult, StringRef> evalParensExpr(StringRef Expr,
489 ParseContext PCtx) const {
490 assert(Expr.startswith("(") && "Not a parenthesized expression");
491 EvalResult SubExprResult;
492 StringRef RemainingExpr;
493 std::tie(SubExprResult, RemainingExpr) =
494 evalComplexExpr(evalSimpleExpr(Expr.substr(1).ltrim(), PCtx), PCtx);
495 if (SubExprResult.hasError())
496 return std::make_pair(SubExprResult, "");
497 if (!RemainingExpr.startswith(")"))
498 return std::make_pair(
499 unexpectedToken(RemainingExpr, Expr, "expected ')'"), "");
500 RemainingExpr = RemainingExpr.substr(1).ltrim();
501 return std::make_pair(SubExprResult, RemainingExpr);
504 // Evaluate an expression in one of the following forms:
506 // Return a pair containing the result, plus the expression remaining to be
508 std::pair<EvalResult, StringRef> evalLoadExpr(StringRef Expr) const {
509 assert(Expr.startswith("*") && "Not a load expression");
510 StringRef RemainingExpr = Expr.substr(1).ltrim();
513 if (!RemainingExpr.startswith("{"))
514 return std::make_pair(EvalResult("Expected '{' following '*'."), "");
515 RemainingExpr = RemainingExpr.substr(1).ltrim();
516 EvalResult ReadSizeExpr;
517 std::tie(ReadSizeExpr, RemainingExpr) = evalNumberExpr(RemainingExpr);
518 if (ReadSizeExpr.hasError())
519 return std::make_pair(ReadSizeExpr, RemainingExpr);
520 uint64_t ReadSize = ReadSizeExpr.getValue();
521 if (ReadSize < 1 || ReadSize > 8)
522 return std::make_pair(EvalResult("Invalid size for dereference."), "");
523 if (!RemainingExpr.startswith("}"))
524 return std::make_pair(EvalResult("Missing '}' for dereference."), "");
525 RemainingExpr = RemainingExpr.substr(1).ltrim();
527 // Evaluate the expression representing the load address.
528 ParseContext LoadCtx(true);
529 EvalResult LoadAddrExprResult;
530 std::tie(LoadAddrExprResult, RemainingExpr) =
531 evalComplexExpr(evalSimpleExpr(RemainingExpr, LoadCtx), LoadCtx);
533 if (LoadAddrExprResult.hasError())
534 return std::make_pair(LoadAddrExprResult, "");
536 uint64_t LoadAddr = LoadAddrExprResult.getValue();
538 return std::make_pair(
539 EvalResult(Checker.readMemoryAtAddr(LoadAddr, ReadSize)),
543 // Evaluate a "simple" expression. This is any expression that _isn't_ an
544 // un-parenthesized binary expression.
546 // "Simple" expressions can be optionally bit-sliced. See evalSlicedExpr.
548 // Returns a pair containing the result of the evaluation, plus the
549 // expression remaining to be parsed.
550 std::pair<EvalResult, StringRef> evalSimpleExpr(StringRef Expr,
551 ParseContext PCtx) const {
552 EvalResult SubExprResult;
553 StringRef RemainingExpr;
556 return std::make_pair(EvalResult("Unexpected end of expression"), "");
559 std::tie(SubExprResult, RemainingExpr) = evalParensExpr(Expr, PCtx);
560 else if (Expr[0] == '*')
561 std::tie(SubExprResult, RemainingExpr) = evalLoadExpr(Expr);
562 else if (isalpha(Expr[0]) || Expr[0] == '_')
563 std::tie(SubExprResult, RemainingExpr) = evalIdentifierExpr(Expr, PCtx);
564 else if (isdigit(Expr[0]))
565 std::tie(SubExprResult, RemainingExpr) = evalNumberExpr(Expr);
567 return std::make_pair(
568 unexpectedToken(Expr, Expr,
569 "expected '(', '*', identifier, or number"), "");
571 if (SubExprResult.hasError())
572 return std::make_pair(SubExprResult, RemainingExpr);
574 // Evaluate bit-slice if present.
575 if (RemainingExpr.startswith("["))
576 std::tie(SubExprResult, RemainingExpr) =
577 evalSliceExpr(std::make_pair(SubExprResult, RemainingExpr));
579 return std::make_pair(SubExprResult, RemainingExpr);
582 // Evaluate a bit-slice of an expression.
583 // A bit-slice has the form "<expr>[high:low]". The result of evaluating a
584 // slice is the bits between high and low (inclusive) in the original
585 // expression, right shifted so that the "low" bit is in position 0 in the
587 // Returns a pair containing the result of the slice operation, plus the
588 // expression remaining to be parsed.
589 std::pair<EvalResult, StringRef>
590 evalSliceExpr(std::pair<EvalResult, StringRef> Ctx) const {
591 EvalResult SubExprResult;
592 StringRef RemainingExpr;
593 std::tie(SubExprResult, RemainingExpr) = Ctx;
595 assert(RemainingExpr.startswith("[") && "Not a slice expr.");
596 RemainingExpr = RemainingExpr.substr(1).ltrim();
598 EvalResult HighBitExpr;
599 std::tie(HighBitExpr, RemainingExpr) = evalNumberExpr(RemainingExpr);
601 if (HighBitExpr.hasError())
602 return std::make_pair(HighBitExpr, RemainingExpr);
604 if (!RemainingExpr.startswith(":"))
605 return std::make_pair(
606 unexpectedToken(RemainingExpr, RemainingExpr, "expected ':'"), "");
607 RemainingExpr = RemainingExpr.substr(1).ltrim();
609 EvalResult LowBitExpr;
610 std::tie(LowBitExpr, RemainingExpr) = evalNumberExpr(RemainingExpr);
612 if (LowBitExpr.hasError())
613 return std::make_pair(LowBitExpr, RemainingExpr);
615 if (!RemainingExpr.startswith("]"))
616 return std::make_pair(
617 unexpectedToken(RemainingExpr, RemainingExpr, "expected ']'"), "");
618 RemainingExpr = RemainingExpr.substr(1).ltrim();
620 unsigned HighBit = HighBitExpr.getValue();
621 unsigned LowBit = LowBitExpr.getValue();
622 uint64_t Mask = ((uint64_t)1 << (HighBit - LowBit + 1)) - 1;
623 uint64_t SlicedValue = (SubExprResult.getValue() >> LowBit) & Mask;
624 return std::make_pair(EvalResult(SlicedValue), RemainingExpr);
627 // Evaluate a "complex" expression.
628 // Takes an already evaluated subexpression and checks for the presence of a
629 // binary operator, computing the result of the binary operation if one is
630 // found. Used to make arithmetic expressions left-associative.
631 // Returns a pair containing the ultimate result of evaluating the
632 // expression, plus the expression remaining to be evaluated.
633 std::pair<EvalResult, StringRef>
634 evalComplexExpr(std::pair<EvalResult, StringRef> LHSAndRemaining,
635 ParseContext PCtx) const {
636 EvalResult LHSResult;
637 StringRef RemainingExpr;
638 std::tie(LHSResult, RemainingExpr) = LHSAndRemaining;
640 // If there was an error, or there's nothing left to evaluate, return the
642 if (LHSResult.hasError() || RemainingExpr == "")
643 return std::make_pair(LHSResult, RemainingExpr);
645 // Otherwise check if this is a binary expressioan.
647 std::tie(BinOp, RemainingExpr) = parseBinOpToken(RemainingExpr);
649 // If this isn't a recognized expression just return.
650 if (BinOp == BinOpToken::Invalid)
651 return std::make_pair(LHSResult, RemainingExpr);
653 // This is a recognized bin-op. Evaluate the RHS, then evaluate the binop.
654 EvalResult RHSResult;
655 std::tie(RHSResult, RemainingExpr) = evalSimpleExpr(RemainingExpr, PCtx);
657 // If there was an error evaluating the RHS, return it.
658 if (RHSResult.hasError())
659 return std::make_pair(RHSResult, RemainingExpr);
661 // This is a binary expression - evaluate and try to continue as a
663 EvalResult ThisResult(computeBinOpResult(BinOp, LHSResult, RHSResult));
665 return evalComplexExpr(std::make_pair(ThisResult, RemainingExpr), PCtx);
668 bool decodeInst(StringRef Symbol, MCInst &Inst, uint64_t &Size) const {
669 MCDisassembler *Dis = Checker.Disassembler;
670 StringRef SectionMem = Checker.getSubsectionStartingAt(Symbol);
671 StringRefMemoryObject SectionBytes(SectionMem, 0);
673 MCDisassembler::DecodeStatus S =
674 Dis->getInstruction(Inst, Size, SectionBytes, 0, nulls(), nulls());
676 return (S == MCDisassembler::Success);
681 RuntimeDyldCheckerImpl::RuntimeDyldCheckerImpl(RuntimeDyld &RTDyld,
682 MCDisassembler *Disassembler,
683 MCInstPrinter *InstPrinter,
684 raw_ostream &ErrStream)
685 : RTDyld(RTDyld), Disassembler(Disassembler), InstPrinter(InstPrinter),
686 ErrStream(ErrStream) {
687 RTDyld.Checker = this;
690 bool RuntimeDyldCheckerImpl::check(StringRef CheckExpr) const {
691 CheckExpr = CheckExpr.trim();
692 DEBUG(dbgs() << "RuntimeDyldChecker: Checking '" << CheckExpr << "'...\n");
693 RuntimeDyldCheckerExprEval P(*this, ErrStream);
694 bool Result = P.evaluate(CheckExpr);
696 DEBUG(dbgs() << "RuntimeDyldChecker: '" << CheckExpr << "' "
697 << (Result ? "passed" : "FAILED") << ".\n");
701 bool RuntimeDyldCheckerImpl::checkAllRulesInBuffer(StringRef RulePrefix,
702 MemoryBuffer *MemBuf) const {
703 bool DidAllTestsPass = true;
704 unsigned NumRules = 0;
706 const char *LineStart = MemBuf->getBufferStart();
709 while (LineStart != MemBuf->getBufferEnd() && std::isspace(*LineStart))
712 while (LineStart != MemBuf->getBufferEnd() && *LineStart != '\0') {
713 const char *LineEnd = LineStart;
714 while (LineEnd != MemBuf->getBufferEnd() && *LineEnd != '\r' &&
718 StringRef Line(LineStart, LineEnd - LineStart);
719 if (Line.startswith(RulePrefix)) {
720 DidAllTestsPass &= check(Line.substr(RulePrefix.size()));
726 while (LineStart != MemBuf->getBufferEnd() && std::isspace(*LineStart))
729 return DidAllTestsPass && (NumRules != 0);
732 bool RuntimeDyldCheckerImpl::isSymbolValid(StringRef Symbol) const {
733 return getRTDyld().getSymbolAddress(Symbol) != nullptr;
736 uint64_t RuntimeDyldCheckerImpl::getSymbolLinkerAddr(StringRef Symbol) const {
737 return static_cast<uint64_t>(
738 reinterpret_cast<uintptr_t>(getRTDyld().getSymbolAddress(Symbol)));
741 uint64_t RuntimeDyldCheckerImpl::getSymbolRemoteAddr(StringRef Symbol) const {
742 return getRTDyld().getAnySymbolRemoteAddress(Symbol);
745 uint64_t RuntimeDyldCheckerImpl::readMemoryAtAddr(uint64_t SrcAddr,
746 unsigned Size) const {
747 uintptr_t PtrSizedAddr = static_cast<uintptr_t>(SrcAddr);
748 assert(PtrSizedAddr == SrcAddr && "Linker memory pointer out-of-range.");
749 uint8_t *Src = reinterpret_cast<uint8_t*>(PtrSizedAddr);
750 return getRTDyld().readBytesUnaligned(Src, Size);
754 std::pair<const RuntimeDyldCheckerImpl::SectionAddressInfo*, std::string>
755 RuntimeDyldCheckerImpl::findSectionAddrInfo(StringRef FileName,
756 StringRef SectionName) const {
758 auto SectionMapItr = Stubs.find(FileName);
759 if (SectionMapItr == Stubs.end()) {
760 std::string ErrorMsg = "File '";
761 ErrorMsg += FileName;
762 ErrorMsg += "' not found. ";
764 ErrorMsg += "No stubs registered.";
766 ErrorMsg += "Available files are:";
767 for (const auto& StubEntry : Stubs) {
769 ErrorMsg += StubEntry.first;
774 return std::make_pair(nullptr, ErrorMsg);
777 auto SectionInfoItr = SectionMapItr->second.find(SectionName);
778 if (SectionInfoItr == SectionMapItr->second.end())
779 return std::make_pair(nullptr,
780 ("Section '" + SectionName + "' not found in file '" +
781 FileName + "'\n").str());
783 return std::make_pair(&SectionInfoItr->second, std::string(""));
786 std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getSectionAddr(
787 StringRef FileName, StringRef SectionName, bool IsInsideLoad) const {
789 const SectionAddressInfo *SectionInfo = nullptr;
791 std::string ErrorMsg;
792 std::tie(SectionInfo, ErrorMsg) =
793 findSectionAddrInfo(FileName, SectionName);
795 return std::make_pair(0, ErrorMsg);
798 unsigned SectionID = SectionInfo->SectionID;
802 static_cast<uint64_t>(
803 reinterpret_cast<uintptr_t>(getRTDyld().Sections[SectionID].Address));
805 Addr = getRTDyld().Sections[SectionID].LoadAddress;
807 return std::make_pair(Addr, std::string(""));
810 std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getStubAddrFor(
811 StringRef FileName, StringRef SectionName, StringRef SymbolName,
812 bool IsInsideLoad) const {
814 const SectionAddressInfo *SectionInfo = nullptr;
816 std::string ErrorMsg;
817 std::tie(SectionInfo, ErrorMsg) =
818 findSectionAddrInfo(FileName, SectionName);
820 return std::make_pair(0, ErrorMsg);
823 unsigned SectionID = SectionInfo->SectionID;
824 const StubOffsetsMap &SymbolStubs = SectionInfo->StubOffsets;
825 auto StubOffsetItr = SymbolStubs.find(SymbolName);
826 if (StubOffsetItr == SymbolStubs.end())
827 return std::make_pair(0,
828 ("Symbol '" + SymbolName + "' not found.\n").str());
830 uint64_t StubOffset = StubOffsetItr->second;
834 uintptr_t SectionBase =
835 reinterpret_cast<uintptr_t>(getRTDyld().Sections[SectionID].Address);
836 Addr = static_cast<uint64_t>(SectionBase) + StubOffset;
838 uint64_t SectionBase = getRTDyld().Sections[SectionID].LoadAddress;
839 Addr = SectionBase + StubOffset;
842 return std::make_pair(Addr, std::string(""));
846 RuntimeDyldCheckerImpl::getSubsectionStartingAt(StringRef Name) const {
847 RuntimeDyldImpl::SymbolTableMap::const_iterator pos =
848 getRTDyld().GlobalSymbolTable.find(Name);
849 if (pos == getRTDyld().GlobalSymbolTable.end())
851 RuntimeDyldImpl::SymbolLoc Loc = pos->second;
852 uint8_t *SectionAddr = getRTDyld().getSectionAddress(Loc.first);
853 return StringRef(reinterpret_cast<const char *>(SectionAddr) + Loc.second,
854 getRTDyld().Sections[Loc.first].Size - Loc.second);
857 void RuntimeDyldCheckerImpl::registerSection(
858 StringRef FilePath, unsigned SectionID) {
859 StringRef FileName = sys::path::filename(FilePath);
860 const SectionEntry &Section = getRTDyld().Sections[SectionID];
861 StringRef SectionName = Section.Name;
863 Stubs[FileName][SectionName].SectionID = SectionID;
866 void RuntimeDyldCheckerImpl::registerStubMap(
867 StringRef FilePath, unsigned SectionID,
868 const RuntimeDyldImpl::StubMap &RTDyldStubs) {
869 StringRef FileName = sys::path::filename(FilePath);
870 const SectionEntry &Section = getRTDyld().Sections[SectionID];
871 StringRef SectionName = Section.Name;
873 Stubs[FileName][SectionName].SectionID = SectionID;
875 for (auto &StubMapEntry : RTDyldStubs) {
876 std::string SymbolName = "";
878 if (StubMapEntry.first.SymbolName)
879 SymbolName = StubMapEntry.first.SymbolName;
881 // If this is a (Section, Offset) pair, do a reverse lookup in the
882 // global symbol table to find the name.
883 for (auto &GSTEntry : getRTDyld().GlobalSymbolTable) {
884 if (GSTEntry.second.first == StubMapEntry.first.SectionID &&
885 GSTEntry.second.second ==
886 static_cast<uint64_t>(StubMapEntry.first.Addend)) {
887 SymbolName = GSTEntry.first();
893 if (SymbolName != "")
894 Stubs[FileName][SectionName].StubOffsets[SymbolName] =
899 RuntimeDyldChecker::RuntimeDyldChecker(RuntimeDyld &RTDyld,
900 MCDisassembler *Disassembler,
901 MCInstPrinter *InstPrinter,
902 raw_ostream &ErrStream)
903 : Impl(make_unique<RuntimeDyldCheckerImpl>(RTDyld, Disassembler,
904 InstPrinter, ErrStream)) {}
906 RuntimeDyldChecker::~RuntimeDyldChecker() {}
908 bool RuntimeDyldChecker::check(StringRef CheckExpr) const {
909 return Impl->check(CheckExpr);
912 bool RuntimeDyldChecker::checkAllRulesInBuffer(StringRef RulePrefix,
913 MemoryBuffer *MemBuf) const {
914 return Impl->checkAllRulesInBuffer(RulePrefix, MemBuf);