RuntimeDyldChecker.h revision 274955
1139776Simp//===---- RuntimeDyldChecker.h - RuntimeDyld tester framework -----*- C++ -*-=//
21541Srgrimes//
31541Srgrimes//                     The LLVM Compiler Infrastructure
41541Srgrimes//
51541Srgrimes// This file is distributed under the University of Illinois Open Source
61541Srgrimes// License. See LICENSE.TXT for details.
71541Srgrimes//
81541Srgrimes//===----------------------------------------------------------------------===//
91541Srgrimes
101541Srgrimes#ifndef LLVM_RUNTIMEDYLDCHECKER_H
111541Srgrimes#define LLVM_RUNTIMEDYLDCHECKER_H
121541Srgrimes
131541Srgrimes#include "RuntimeDyld.h"
141541Srgrimes#include "llvm/Support/Debug.h"
151541Srgrimes#include "llvm/Support/raw_ostream.h"
161541Srgrimes#include <map>
171541Srgrimes
181541Srgrimesnamespace llvm {
191541Srgrimes
201541Srgrimesclass MCDisassembler;
211541Srgrimesclass MCInstPrinter;
221541Srgrimes
231541Srgrimes/// \brief RuntimeDyld invariant checker for verifying that RuntimeDyld has
241541Srgrimes///        correctly applied relocations.
251541Srgrimes///
261541Srgrimes/// The RuntimeDyldChecker class evaluates expressions against an attached
271541Srgrimes/// RuntimeDyld instance to verify that relocations have been applied
281541Srgrimes/// correctly.
291541Srgrimes///
301541Srgrimes/// The expression language supports basic pointer arithmetic and bit-masking,
311541Srgrimes/// and has limited disassembler integration for accessing instruction
321541Srgrimes/// operands and the next PC (program counter) address for each instruction.
331541Srgrimes///
3450477Speter/// The language syntax is:
351541Srgrimes///
361541Srgrimes/// check = expr '=' expr
3755206Speter///
38179288Slulf/// expr = binary_expr
39179288Slulf///      | sliceable_expr
401541Srgrimes///
411541Srgrimes/// sliceable_expr = '*{' number '}' load_addr_expr [slice]
42179288Slulf///                | '(' expr ')' [slice]
431541Srgrimes///                | ident_expr [slice]
441541Srgrimes///                | number [slice]
4560406Schris///
4660406Schris/// slice = '[' high-bit-index ':' low-bit-index ']'
471541Srgrimes///
481541Srgrimes/// load_addr_expr = symbol
491541Srgrimes///                | '(' symbol '+' number ')'
5060406Schris///                | '(' symbol '-' number ')'
511541Srgrimes///
521541Srgrimes/// ident_expr = 'decode_operand' '(' symbol ',' operand-index ')'
531541Srgrimes///            | 'next_pc'        '(' symbol ')'
5460938Sjake///            | symbol
551541Srgrimes///
561541Srgrimes/// binary_expr = expr '+' expr
571541Srgrimes///             | expr '-' expr
581541Srgrimes///             | expr '&' expr
591541Srgrimes///             | expr '|' expr
601541Srgrimes///             | expr '<<' expr
61179288Slulf///             | expr '>>' expr
621541Srgrimes///
631541Srgrimesclass RuntimeDyldChecker {
641541Srgrimes  friend class RuntimeDyldCheckerExprEval;
65132023Salfredpublic:
66179288Slulf  RuntimeDyldChecker(RuntimeDyld &RTDyld,
67179288Slulf                     MCDisassembler *Disassembler,
68191990Sattilio                     MCInstPrinter *InstPrinter,
6955206Speter                     llvm::raw_ostream &ErrStream)
70    : RTDyld(*RTDyld.Dyld), Disassembler(Disassembler),
71      InstPrinter(InstPrinter), ErrStream(ErrStream) {}
72
73  /// \brief Check a single expression against the attached RuntimeDyld
74  ///        instance.
75  bool check(StringRef CheckExpr) const;
76
77  /// \brief Scan the given memory buffer for lines beginning with the string
78  ///        in RulePrefix. The remainder of the line is passed to the check
79  ///        method to be evaluated as an expression.
80  bool checkAllRulesInBuffer(StringRef RulePrefix, MemoryBuffer *MemBuf) const;
81
82private:
83
84  bool isSymbolValid(StringRef Symbol) const;
85  uint64_t getSymbolAddress(StringRef Symbol) const;
86  uint64_t readMemoryAtSymbol(StringRef Symbol, int64_t Offset,
87                              unsigned Size) const;
88  StringRef getSubsectionStartingAt(StringRef Name) const;
89
90  RuntimeDyldImpl &RTDyld;
91  MCDisassembler *Disassembler;
92  MCInstPrinter *InstPrinter;
93  llvm::raw_ostream &ErrStream;
94};
95
96} // end namespace llvm
97
98#endif // LLVM_RUNTIMEDYLDCHECKER_H
99