LCOV - code coverage report
Current view: top level - include/llvm/ExecutionEngine/Orc - ExecutionUtils.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 26 29 89.7 %
Date: 2018-02-21 17:27:13 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- ExecutionUtils.h - Utilities for executing code in Orc ---*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Contains utilities for executing code in Orc.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
      15             : #define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H
      16             : 
      17             : #include "llvm/ADT/StringMap.h"
      18             : #include "llvm/ADT/iterator_range.h"
      19             : #include "llvm/ExecutionEngine/JITSymbol.h"
      20             : #include "llvm/ExecutionEngine/Orc/Core.h"
      21             : #include "llvm/ExecutionEngine/Orc/OrcError.h"
      22             : #include "llvm/ExecutionEngine/RuntimeDyld.h"
      23             : #include <algorithm>
      24             : #include <cstdint>
      25             : #include <string>
      26             : #include <utility>
      27             : #include <vector>
      28             : 
      29             : namespace llvm {
      30             : 
      31             : class ConstantArray;
      32             : class GlobalVariable;
      33             : class Function;
      34             : class Module;
      35             : class Value;
      36             : 
      37             : namespace orc {
      38             : 
      39             : /// @brief This iterator provides a convenient way to iterate over the elements
      40             : ///        of an llvm.global_ctors/llvm.global_dtors instance.
      41             : ///
      42             : ///   The easiest way to get hold of instances of this class is to use the
      43             : /// getConstructors/getDestructors functions.
      44             : class CtorDtorIterator {
      45             : public:
      46             :   /// @brief Accessor for an element of the global_ctors/global_dtors array.
      47             :   ///
      48             :   ///   This class provides a read-only view of the element with any casts on
      49             :   /// the function stripped away.
      50             :   struct Element {
      51             :     Element(unsigned Priority, Function *Func, Value *Data)
      52           1 :       : Priority(Priority), Func(Func), Data(Data) {}
      53             : 
      54             :     unsigned Priority;
      55             :     Function *Func;
      56             :     Value *Data;
      57             :   };
      58             : 
      59             :   /// @brief Construct an iterator instance. If End is true then this iterator
      60             :   ///        acts as the end of the range, otherwise it is the beginning.
      61             :   CtorDtorIterator(const GlobalVariable *GV, bool End);
      62             : 
      63             :   /// @brief Test iterators for equality.
      64             :   bool operator==(const CtorDtorIterator &Other) const;
      65             : 
      66             :   /// @brief Test iterators for inequality.
      67             :   bool operator!=(const CtorDtorIterator &Other) const;
      68             : 
      69             :   /// @brief Pre-increment iterator.
      70             :   CtorDtorIterator& operator++();
      71             : 
      72             :   /// @brief Post-increment iterator.
      73             :   CtorDtorIterator operator++(int);
      74             : 
      75             :   /// @brief Dereference iterator. The resulting value provides a read-only view
      76             :   ///        of this element of the global_ctors/global_dtors list.
      77             :   Element operator*() const;
      78             : 
      79             : private:
      80             :   const ConstantArray *InitList;
      81             :   unsigned I;
      82             : };
      83             : 
      84             : /// @brief Create an iterator range over the entries of the llvm.global_ctors
      85             : ///        array.
      86             : iterator_range<CtorDtorIterator> getConstructors(const Module &M);
      87             : 
      88             : /// @brief Create an iterator range over the entries of the llvm.global_ctors
      89             : ///        array.
      90             : iterator_range<CtorDtorIterator> getDestructors(const Module &M);
      91             : 
      92             : /// @brief Convenience class for recording constructor/destructor names for
      93             : ///        later execution.
      94             : template <typename JITLayerT>
      95          25 : class CtorDtorRunner {
      96             : public:
      97             :   /// @brief Construct a CtorDtorRunner for the given range using the given
      98             :   ///        name mangling function.
      99          22 :   CtorDtorRunner(std::vector<std::string> CtorDtorNames, VModuleKey K)
     100          22 :       : CtorDtorNames(std::move(CtorDtorNames)), K(K) {}
     101             : 
     102             :   /// @brief Run the recorded constructors/destructors through the given JIT
     103             :   ///        layer.
     104          22 :   Error runViaLayer(JITLayerT &JITLayer) const {
     105             :     using CtorDtorTy = void (*)();
     106             : 
     107          22 :     for (const auto &CtorDtorName : CtorDtorNames) {
     108           2 :       dbgs() << "Searching for ctor/dtor: " << CtorDtorName << "...";
     109           2 :       if (auto CtorDtorSym = JITLayer.findSymbolIn(K, CtorDtorName, false)) {
     110           1 :         dbgs() << " found symbol...";
     111           2 :         if (auto AddrOrErr = CtorDtorSym.getAddress()) {
     112           2 :           dbgs() << " at addr " << format("0x%016x", *AddrOrErr) << "\n";
     113           1 :           CtorDtorTy CtorDtor =
     114             :             reinterpret_cast<CtorDtorTy>(static_cast<uintptr_t>(*AddrOrErr));
     115           1 :           CtorDtor();
     116             :         } else {
     117           0 :           dbgs() << " failed materialization!\n";
     118             :           return AddrOrErr.takeError();
     119             :         }
     120             :       } else {
     121           0 :         dbgs() << " failed to find symbol...";
     122           0 :         if (auto Err = CtorDtorSym.takeError())
     123             :           return Err;
     124             :         else
     125             :           return make_error<JITSymbolNotFound>(CtorDtorName);
     126             :       }
     127             :     }
     128             :     return Error::success();
     129             :   }
     130             : 
     131             : private:
     132             :   std::vector<std::string> CtorDtorNames;
     133             :   orc::VModuleKey K;
     134             : };
     135             : 
     136             : /// @brief Support class for static dtor execution. For hosted (in-process) JITs
     137             : ///        only!
     138             : ///
     139             : ///   If a __cxa_atexit function isn't found C++ programs that use static
     140             : /// destructors will fail to link. However, we don't want to use the host
     141             : /// process's __cxa_atexit, because it will schedule JIT'd destructors to run
     142             : /// after the JIT has been torn down, which is no good. This class makes it easy
     143             : /// to override __cxa_atexit (and the related __dso_handle).
     144             : ///
     145             : ///   To use, clients should manually call searchOverrides from their symbol
     146             : /// resolver. This should generally be done after attempting symbol resolution
     147             : /// inside the JIT, but before searching the host process's symbol table. When
     148             : /// the client determines that destructors should be run (generally at JIT
     149             : /// teardown or after a return from main), the runDestructors method should be
     150             : /// called.
     151          22 : class LocalCXXRuntimeOverrides {
     152             : public:
     153             :   /// Create a runtime-overrides class.
     154             :   template <typename MangleFtorT>
     155          22 :   LocalCXXRuntimeOverrides(const MangleFtorT &Mangle) {
     156          44 :     addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride));
     157          44 :     addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride));
     158          11 :   }
     159             : 
     160             :   /// Search overrided symbols.
     161          10 :   JITEvaluatedSymbol searchOverrides(const std::string &Name) {
     162          20 :     auto I = CXXRuntimeOverrides.find(Name);
     163          20 :     if (I != CXXRuntimeOverrides.end())
     164           2 :       return JITEvaluatedSymbol(I->second, JITSymbolFlags::Exported);
     165             :     return nullptr;
     166             :   }
     167             : 
     168             :   /// Run any destructors recorded by the overriden __cxa_atexit function
     169             :   /// (CXAAtExitOverride).
     170             :   void runDestructors();
     171             : 
     172             : private:
     173             :   template <typename PtrTy>
     174             :   JITTargetAddress toTargetAddress(PtrTy* P) {
     175          22 :     return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P));
     176             :   }
     177             : 
     178          22 :   void addOverride(const std::string &Name, JITTargetAddress Addr) {
     179          44 :     CXXRuntimeOverrides.insert(std::make_pair(Name, Addr));
     180          22 :   }
     181             : 
     182             :   StringMap<JITTargetAddress> CXXRuntimeOverrides;
     183             : 
     184             :   using DestructorPtr = void (*)(void *);
     185             :   using CXXDestructorDataPair = std::pair<DestructorPtr, void *>;
     186             :   using CXXDestructorDataPairList = std::vector<CXXDestructorDataPair>;
     187             :   CXXDestructorDataPairList DSOHandleOverride;
     188             :   static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg,
     189             :                                void *DSOHandle);
     190             : };
     191             : 
     192             : } // end namespace orc
     193             : 
     194             : } // end namespace llvm
     195             : 
     196             : #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H

Generated by: LCOV version 1.13