2013-10-02 17:12:36 +00:00
|
|
|
//===----- RemoteTargetExternal.h - LLVM out-of-process JIT execution -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Definition of the RemoteTargetExternal class which executes JITed code in a
|
|
|
|
// separate process from where it was built.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLI_REMOTETARGETEXTERNAL_H
|
|
|
|
#define LLI_REMOTETARGETEXTERNAL_H
|
|
|
|
|
2014-01-23 22:19:45 +00:00
|
|
|
#include "RPCChannel.h"
|
2014-01-13 08:04:33 +00:00
|
|
|
#include "RemoteTarget.h"
|
|
|
|
#include "RemoteTargetMessage.h"
|
2013-10-02 17:12:36 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-01-13 08:04:33 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2013-10-02 17:12:36 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class RemoteTargetExternal : public RemoteTarget {
|
2014-01-23 22:19:45 +00:00
|
|
|
RPCChannel RPC;
|
|
|
|
|
|
|
|
bool WriteBytes(const void *Data, size_t Size) {
|
2014-01-24 17:18:52 +00:00
|
|
|
return RPC.WriteBytes(Data, Size);
|
2014-01-23 22:19:45 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 17:18:52 +00:00
|
|
|
bool ReadBytes(void *Data, size_t Size) { return RPC.ReadBytes(Data, Size); }
|
2014-01-23 22:19:45 +00:00
|
|
|
|
2013-10-02 17:12:36 +00:00
|
|
|
public:
|
|
|
|
/// Allocate space in the remote target address space.
|
|
|
|
///
|
|
|
|
/// @param Size Amount of space, in bytes, to allocate.
|
|
|
|
/// @param Alignment Required minimum alignment for allocated space.
|
|
|
|
/// @param[out] Address Remote address of the allocated memory.
|
|
|
|
///
|
2014-01-14 22:43:43 +00:00
|
|
|
/// @returns True on success. On failure, ErrorMsg is updated with
|
2013-10-02 17:12:36 +00:00
|
|
|
/// descriptive text of the encountered error.
|
2014-03-08 08:27:28 +00:00
|
|
|
bool allocateSpace(size_t Size, unsigned Alignment,
|
|
|
|
uint64_t &Address) override;
|
2013-10-02 17:12:36 +00:00
|
|
|
|
|
|
|
/// Load data into the target address space.
|
|
|
|
///
|
|
|
|
/// @param Address Destination address in the target process.
|
|
|
|
/// @param Data Source address in the host process.
|
|
|
|
/// @param Size Number of bytes to copy.
|
|
|
|
///
|
2014-01-14 22:43:43 +00:00
|
|
|
/// @returns True on success. On failure, ErrorMsg is updated with
|
2013-10-02 17:12:36 +00:00
|
|
|
/// descriptive text of the encountered error.
|
2014-03-08 08:27:28 +00:00
|
|
|
bool loadData(uint64_t Address, const void *Data, size_t Size) override;
|
2013-10-02 17:12:36 +00:00
|
|
|
|
|
|
|
/// Load code into the target address space and prepare it for execution.
|
|
|
|
///
|
|
|
|
/// @param Address Destination address in the target process.
|
|
|
|
/// @param Data Source address in the host process.
|
|
|
|
/// @param Size Number of bytes to copy.
|
|
|
|
///
|
2014-01-14 22:43:43 +00:00
|
|
|
/// @returns True on success. On failure, ErrorMsg is updated with
|
2013-10-02 17:12:36 +00:00
|
|
|
/// descriptive text of the encountered error.
|
2014-03-08 08:27:28 +00:00
|
|
|
bool loadCode(uint64_t Address, const void *Data, size_t Size) override;
|
2013-10-02 17:12:36 +00:00
|
|
|
|
|
|
|
/// Execute code in the target process. The called function is required
|
|
|
|
/// to be of signature int "(*)(void)".
|
|
|
|
///
|
|
|
|
/// @param Address Address of the loaded function in the target
|
|
|
|
/// process.
|
|
|
|
/// @param[out] RetVal The integer return value of the called function.
|
|
|
|
///
|
2014-01-14 22:43:43 +00:00
|
|
|
/// @returns True on success. On failure, ErrorMsg is updated with
|
2013-10-02 17:12:36 +00:00
|
|
|
/// descriptive text of the encountered error.
|
2014-03-08 08:27:28 +00:00
|
|
|
bool executeCode(uint64_t Address, int &RetVal) override;
|
2013-10-02 17:12:36 +00:00
|
|
|
|
2014-01-24 17:20:08 +00:00
|
|
|
/// Minimum alignment for memory permissions. Used to separate code and
|
2013-10-02 17:12:36 +00:00
|
|
|
/// data regions to make sure data doesn't get marked as code or vice
|
|
|
|
/// versa.
|
|
|
|
///
|
|
|
|
/// @returns Page alignment return value. Default of 4k.
|
2014-03-08 08:27:28 +00:00
|
|
|
unsigned getPageAlignment() override { return 4096; }
|
2013-10-02 17:12:36 +00:00
|
|
|
|
2014-03-08 08:27:28 +00:00
|
|
|
bool create() override {
|
2014-01-23 22:19:45 +00:00
|
|
|
RPC.ChildName = ChildName;
|
|
|
|
if (!RPC.createServer())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We must get Ack from the client (blocking read)
|
|
|
|
if (!Receive(LLI_ChildActive)) {
|
|
|
|
ErrorMsg += ", (RPCChannel::create) - Stopping process!";
|
|
|
|
stop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-02 17:12:36 +00:00
|
|
|
|
|
|
|
/// Terminate the remote process.
|
2014-03-08 08:27:28 +00:00
|
|
|
void stop() override;
|
2013-10-02 17:12:36 +00:00
|
|
|
|
|
|
|
RemoteTargetExternal(std::string &Name) : RemoteTarget(), ChildName(Name) {}
|
2014-01-23 22:19:45 +00:00
|
|
|
virtual ~RemoteTargetExternal() {}
|
2013-10-02 17:12:36 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string ChildName;
|
|
|
|
|
2014-01-14 22:43:43 +00:00
|
|
|
bool SendAllocateSpace(uint32_t Alignment, uint32_t Size);
|
|
|
|
bool SendLoadSection(uint64_t Addr,
|
2013-10-02 17:12:36 +00:00
|
|
|
const void *Data,
|
|
|
|
uint32_t Size,
|
|
|
|
bool IsCode);
|
2014-01-14 22:43:43 +00:00
|
|
|
bool SendExecute(uint64_t Addr);
|
|
|
|
bool SendTerminate();
|
|
|
|
|
|
|
|
// High-level wrappers for receiving data
|
|
|
|
bool Receive(LLIMessageType Msg);
|
|
|
|
bool Receive(LLIMessageType Msg, int32_t &Data);
|
|
|
|
bool Receive(LLIMessageType Msg, uint64_t &Data);
|
|
|
|
|
|
|
|
// Lower level target-independent read/write to deal with errors
|
|
|
|
bool ReceiveHeader(LLIMessageType Msg);
|
|
|
|
bool ReceivePayload();
|
|
|
|
bool SendHeader(LLIMessageType Msg);
|
|
|
|
bool SendPayload();
|
|
|
|
|
|
|
|
// Functions to append/retrieve data from the payload
|
|
|
|
SmallVector<const void *, 2> SendData;
|
|
|
|
SmallVector<void *, 1> ReceiveData; // Future proof
|
|
|
|
SmallVector<int, 2> Sizes;
|
|
|
|
void AppendWrite(const void *Data, uint32_t Size);
|
|
|
|
void AppendRead(void *Data, uint32_t Size);
|
2013-10-02 17:12:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLI_REMOTETARGETEXTERNAL_H
|