RemoteTarget.h revision 263508
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
25namespace llvm {
26
27class RemoteTarget {
28  std::string ErrorMsg;
29  bool IsRunning;
30
31  SmallVector<sys::MemoryBlock, 16> Allocations;
32
33public:
34  StringRef getErrorMsg() const { return ErrorMsg; }
35
36  /// Allocate space in the remote target address space.
37  ///
38  /// @param      Size      Amount of space, in bytes, to allocate.
39  /// @param      Alignment Required minimum alignment for allocated space.
40  /// @param[out] Address   Remote address of the allocated memory.
41  ///
42  /// @returns False on success. On failure, ErrorMsg is updated with
43  ///          descriptive text of the encountered error.
44  virtual bool allocateSpace(size_t Size,
45                             unsigned Alignment,
46                             uint64_t &Address);
47
48  /// Load data into the target address space.
49  ///
50  /// @param      Address   Destination address in the target process.
51  /// @param      Data      Source address in the host process.
52  /// @param      Size      Number of bytes to copy.
53  ///
54  /// @returns False on success. On failure, ErrorMsg is updated with
55  ///          descriptive text of the encountered error.
56  virtual bool loadData(uint64_t Address,
57                        const void *Data,
58                        size_t Size);
59
60  /// Load code into the target address space and prepare it for execution.
61  ///
62  /// @param      Address   Destination address in the target process.
63  /// @param      Data      Source address in the host process.
64  /// @param      Size      Number of bytes to copy.
65  ///
66  /// @returns False on success. On failure, ErrorMsg is updated with
67  ///          descriptive text of the encountered error.
68  virtual bool loadCode(uint64_t Address,
69                        const void *Data,
70                        size_t Size);
71
72  /// Execute code in the target process. The called function is required
73  /// to be of signature int "(*)(void)".
74  ///
75  /// @param      Address   Address of the loaded function in the target
76  ///                       process.
77  /// @param[out] RetVal    The integer return value of the called function.
78  ///
79  /// @returns False on success. On failure, ErrorMsg is updated with
80  ///          descriptive text of the encountered error.
81  virtual bool executeCode(uint64_t Address,
82                           int &RetVal);
83
84  /// Minimum alignment for memory permissions. Used to seperate code and
85  /// data regions to make sure data doesn't get marked as code or vice
86  /// versa.
87  ///
88  /// @returns Page alignment return value. Default of 4k.
89  virtual unsigned getPageAlignment() { return 4096; }
90
91  /// Start the remote process.
92  virtual void create();
93
94  /// Terminate the remote process.
95  virtual void stop();
96
97  RemoteTarget() : ErrorMsg(""), IsRunning(false) {}
98  virtual ~RemoteTarget() { if (IsRunning) stop(); }
99
100  // Create an instance of the system-specific remote target class.
101  static RemoteTarget *createRemoteTarget();
102  static RemoteTarget *createExternalRemoteTarget(std::string &ChildName);
103  static bool hostSupportsExternalRemoteTarget();
104private:
105  // Main processing function for the remote target process. Command messages
106  // are received on file descriptor CmdFD and responses come back on OutFD.
107  static void doRemoteTargeting(int CmdFD, int OutFD);
108};
109
110} // end namespace llvm
111
112#endif
113