Refactor lli-child-target to remove duplicated code

Eliminate the copies LLVM's System mmap and cache invalidation code. These were
slowly drifting away from the original version, and moreover the copied code
was a dead end in terms of portability.

We now statically link to Support but in practice with stripping this adds next
to no weight to the resultant binary.

Also avoid installing lli-child-target to the user's $PATH. It's not meant to
be run directly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199881 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alp Toker
2014-01-23 11:04:42 +00:00
parent 5e95e642e9
commit 972fb8ca77
6 changed files with 32 additions and 174 deletions
+12 -41
View File
@@ -1,4 +1,6 @@
#include "llvm/Config/config.h"
#include "llvm/Support/Memory.h"
#include "../RemoteTarget.h"
#include "../RemoteTargetMessage.h"
#include <assert.h>
#include <map>
@@ -14,13 +16,13 @@ public:
void initialize();
LLIMessageType waitForIncomingMessage();
void handleMessage(LLIMessageType messageType);
RemoteTarget *RT;
private:
// Incoming message handlers
void handleAllocateSpace();
void handleLoadSection(bool IsCode);
void handleExecute();
void handleTerminate();
// Outgoing message handlers
void sendChildActive();
@@ -32,15 +34,6 @@ private:
void initializeConnection();
int WriteBytes(const void *Data, size_t Size);
int ReadBytes(void *Data, size_t Size);
uint64_t allocate(uint32_t Alignment, uint32_t Size);
void makeSectionExecutable(uint64_t Addr, uint32_t Size);
void InvalidateInstructionCache(const void *Addr, size_t Len);
void releaseMemory(uint64_t Addr, uint32_t Size);
bool isAllocatedMemory(uint64_t Address, uint32_t Size);
// Store a map of allocated buffers to sizes.
typedef std::map<uint64_t, uint32_t> AllocMapType;
AllocMapType m_AllocatedBufferMap;
// Communication handles (OS-specific)
void *ConnectionData;
@@ -48,6 +41,7 @@ private:
int main() {
LLIChildTarget ThisChild;
ThisChild.RT = new RemoteTarget();
ThisChild.initialize();
LLIMessageType MsgType;
do {
@@ -55,6 +49,7 @@ int main() {
ThisChild.handleMessage(MsgType);
} while (MsgType != LLI_Terminate &&
MsgType != LLI_Error);
delete ThisChild.RT;
return 0;
}
@@ -86,7 +81,7 @@ void LLIChildTarget::handleMessage(LLIMessageType messageType) {
handleExecute();
break;
case LLI_Terminate:
handleTerminate();
RT->stop();
break;
default:
// FIXME: Handle error!
@@ -112,7 +107,8 @@ void LLIChildTarget::handleAllocateSpace() {
assert(rc == 4);
// Allocate the memory.
uint64_t Addr = allocate(Alignment, AllocSize);
uint64_t Addr;
RT->allocateSpace(AllocSize, Alignment, Addr);
// Send AllocationResult message.
sendAllocationResult(Addr);
@@ -131,7 +127,7 @@ void LLIChildTarget::handleLoadSection(bool IsCode) {
assert(rc == 8);
size_t BufferSize = DataSize - 8;
if (!isAllocatedMemory(Addr, BufferSize))
if (!RT->isAllocatedMemory(Addr, BufferSize))
return sendLoadStatus(LLI_Status_NotAllocated);
// Read section data into previously allocated buffer
@@ -141,7 +137,7 @@ void LLIChildTarget::handleLoadSection(bool IsCode) {
// If IsCode, mark memory executable
if (IsCode)
makeSectionExecutable(Addr, BufferSize);
sys::Memory::InvalidateInstructionCache((void *)Addr, BufferSize);
// Send MarkLoadComplete message.
sendLoadStatus(LLI_Status_Success);
@@ -161,38 +157,13 @@ void LLIChildTarget::handleExecute() {
assert(rc == 8);
// Call function
int Result;
int (*fn)(void) = (int(*)(void))Addr;
Result = fn();
int32_t Result = -1;
RT->executeCode(Addr, Result);
// Send ExecutionResult message.
sendExecutionComplete(Result);
}
void LLIChildTarget::handleTerminate() {
// Release all allocated memory
AllocMapType::iterator Begin = m_AllocatedBufferMap.begin();
AllocMapType::iterator End = m_AllocatedBufferMap.end();
for (AllocMapType::iterator It = Begin; It != End; ++It) {
releaseMemory(It->first, It->second);
}
m_AllocatedBufferMap.clear();
}
bool LLIChildTarget::isAllocatedMemory(uint64_t Address, uint32_t Size) {
uint64_t End = Address+Size;
AllocMapType::iterator ItBegin = m_AllocatedBufferMap.begin();
AllocMapType::iterator ItEnd = m_AllocatedBufferMap.end();
for (AllocMapType::iterator It = ItBegin; It != ItEnd; ++It) {
uint64_t A = It->first;
uint64_t E = A + It->second;
// Starts and finishes inside allocated region
if (Address >= A && End <= E)
return true;
}
return false;
}
// Outgoing message handlers
void LLIChildTarget::sendChildActive() {
// Write the message type.