RemoteTarget.h 4.2 KB

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