Home | History | Annotate | Download | only in lli
      1 //===- RemoteTarget.h - LLVM Remote process JIT execution ----------------===//
      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 // Definition of the RemoteTarget class which executes JITed code in a
     11 // separate address range from where it was built.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef REMOTEPROCESS_H
     16 #define REMOTEPROCESS_H
     17 
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/ADT/StringRef.h"
     20 #include "llvm/Support/DataTypes.h"
     21 #include "llvm/Support/Memory.h"
     22 #include <stdlib.h>
     23 #include <string>
     24 
     25 namespace llvm {
     26 
     27 class RemoteTarget {
     28   bool IsRunning;
     29 
     30   typedef SmallVector<sys::MemoryBlock, 16> AllocMapType;
     31   AllocMapType Allocations;
     32 
     33 protected:
     34   std::string ErrorMsg;
     35 
     36 public:
     37   StringRef getErrorMsg() const { return ErrorMsg; }
     38 
     39   /// Allocate space in the remote target address space.
     40   ///
     41   /// @param      Size      Amount of space, in bytes, to allocate.
     42   /// @param      Alignment Required minimum alignment for allocated space.
     43   /// @param[out] Address   Remote address of the allocated memory.
     44   ///
     45   /// @returns True on success. On failure, ErrorMsg is updated with
     46   ///          descriptive text of the encountered error.
     47   virtual bool allocateSpace(size_t Size,
     48                              unsigned Alignment,
     49                              uint64_t &Address);
     50 
     51   bool isAllocatedMemory(uint64_t Address, uint32_t Size) {
     52     uint64_t AddressEnd = Address + Size;
     53     for (AllocMapType::const_iterator I = Allocations.begin(),
     54                                       E = Allocations.end();
     55          I != E; ++I) {
     56       if (Address >= (uint64_t)I->base() &&
     57           AddressEnd <= (uint64_t)I->base() + I->size())
     58         return true;
     59     }
     60     return false;
     61   }
     62 
     63   /// Load data into the target address space.
     64   ///
     65   /// @param      Address   Destination address in the target process.
     66   /// @param      Data      Source address in the host process.
     67   /// @param      Size      Number of bytes to copy.
     68   ///
     69   /// @returns True on success. On failure, ErrorMsg is updated with
     70   ///          descriptive text of the encountered error.
     71   virtual bool loadData(uint64_t Address,
     72                         const void *Data,
     73                         size_t Size);
     74 
     75   /// Load code into the target address space and prepare it for execution.
     76   ///
     77   /// @param      Address   Destination address in the target process.
     78   /// @param      Data      Source address in the host process.
     79   /// @param      Size      Number of bytes to copy.
     80   ///
     81   /// @returns True on success. On failure, ErrorMsg is updated with
     82   ///          descriptive text of the encountered error.
     83   virtual bool loadCode(uint64_t Address,
     84                         const void *Data,
     85                         size_t Size);
     86 
     87   /// Execute code in the target process. The called function is required
     88   /// to be of signature int "(*)(void)".
     89   ///
     90   /// @param      Address   Address of the loaded function in the target
     91   ///                       process.
     92   /// @param[out] RetVal    The integer return value of the called function.
     93   ///
     94   /// @returns True on success. On failure, ErrorMsg is updated with
     95   ///          descriptive text of the encountered error.
     96   virtual bool executeCode(uint64_t Address,
     97                            int &RetVal);
     98 
     99   /// Minimum alignment for memory permissions. Used to separate code and
    100   /// data regions to make sure data doesn't get marked as code or vice
    101   /// versa.
    102   ///
    103   /// @returns Page alignment return value. Default of 4k.
    104   virtual unsigned getPageAlignment() { return 4096; }
    105 
    106   /// Start the remote process.
    107   virtual bool create();
    108 
    109   /// Terminate the remote process.
    110   virtual void stop();
    111 
    112   RemoteTarget() : IsRunning(false), ErrorMsg("") {}
    113   virtual ~RemoteTarget() { if (IsRunning) stop(); }
    114 private:
    115   // Main processing function for the remote target process. Command messages
    116   // are received on file descriptor CmdFD and responses come back on OutFD.
    117   static void doRemoteTargeting(int CmdFD, int OutFD);
    118 };
    119 
    120 } // end namespace llvm
    121 
    122 #endif
    123