Files
UnrealEngineUWP/Engine/Source/Developer/Windows/LiveCodingServer/Private/External/LC_ExecutablePatcher.cpp
Ben Marsh e1fe0cc030 Integrating live coding feature (aka Live++) into UE4.
Allows fast iteration of C++ changes without restarting the application. To use, select the "Live Coding (Experimental)" mode from the drop down menu next to the editor's compile button, or type "LiveCoding" into the console for a monolithic build. Press Ctrl+Alt+F11 to find changes and compile.

Changes vs standalone Live++ version:

* UBT is used to execute builds. This allows standard UE4 adaptive unity mode, allows us to reuse object files when we do regular builds, supports using any build executor allowed by UBT (XGE, SNDBS, etc..).
* Adding new source files is supported.
* Custom visualizer for FNames is supported via a weakly linked symbol in a static library (Engine/Extras/NatvisHelpers).
* Settings are exposed in the editor's project settings dialog.
* Standalone application has been rewritten as a Slate app ("LiveCodingConsole"). There is an additional option to start the program as hidden, where it will not be visible until Ctrl+Alt+F11 is hit. Similarly, closing the window will hide it instead of closing the application.
* Does not require a standalone licensed version of Live++.

Known issues:

* Does not currently support class layout changes / object reinstancing

#rb none
#fyi Marc.Audy, Stefan.Boberg, Nick.Penwarden
#jira

[CL 5304722 by Ben Marsh in 4.22 branch]
2019-03-05 15:54:02 -05:00

81 lines
2.9 KiB
C++

// Copyright 2011-2019 Molecular Matters GmbH, all rights reserved.
#include "LC_ExecutablePatcher.h"
#include "LC_Executable.h"
#include "LC_PointerUtil.h"
#include "LC_Logging.h"
namespace
{
// the DLL entry point is a C-function with three 4-byte parameters, which means that
// we need to pop 12 bytes off the stack upon returning from the function, at least for x86 calling convention.
// this can be done with a "RET imm16" instruction, which is encoded as "C2 0C 00".
// additionally, the entry point has a BOOL return value, which means we must return
// a value in eax/rax, which can be done with a simple "MOV" instruction.
// in order to keep the injected code as small as possible, however, it is sufficient
// to move a value into the lowest 8-bit of AL only - the value only needs to be NOT zero.
#if LC_64_BIT
// the code to inject on x64 is:
// B0 01 mov al, 1
// C3 ret different calling convention than x86
static const uint8_t PATCH[ExecutablePatcher::INJECTED_CODE_SIZE] = { 0xB0, 0x01, 0xC3 };
#else
// the code to inject on x86 is:
// B0 01 mov al, 1
// C2 0C 00 ret 0Ch different calling convention than x64
static const uint8_t PATCH[ExecutablePatcher::INJECTED_CODE_SIZE] = { 0xB0, 0x01, 0xC2, 0x0C, 0x00 };
#endif
}
ExecutablePatcher::ExecutablePatcher(executable::Image* image, executable::ImageSectionDB* imageSections)
{
LC_ASSERT(image, "Invalid image.");
const uint32_t entryPointRva = executable::GetEntryPointRva(image);
const uint32_t entryPointFileOffset = executable::RvaToFileOffset(imageSections, entryPointRva);
executable::ReadFromFileOffset(image, entryPointFileOffset, m_originalCode, INJECTED_CODE_SIZE);
}
ExecutablePatcher::ExecutablePatcher(const uint8_t* entryPointCode)
{
memcpy(m_originalCode, entryPointCode, INJECTED_CODE_SIZE);
}
uint32_t ExecutablePatcher::DisableEntryPointInImage(executable::Image* image, executable::ImageSectionDB* imageSections)
{
LC_ASSERT(image, "Invalid image.");
const uint32_t entryPointRva = executable::GetEntryPointRva(image);
const uint32_t entryPointFileOffset = executable::RvaToFileOffset(imageSections, entryPointRva);
executable::WriteToFileOffset(image, entryPointFileOffset, PATCH, INJECTED_CODE_SIZE);
return entryPointRva;
}
void ExecutablePatcher::DisableEntryPoint(process::Handle processHandle, void* moduleBase, uint32_t entryPointRva)
{
for (size_t i = 0u; i < INJECTED_CODE_SIZE; ++i)
{
uint8_t* address = pointer::Offset<uint8_t*>(moduleBase, entryPointRva + i);
process::WriteProcessMemory(processHandle, address, PATCH[i]);
}
}
void ExecutablePatcher::RestoreEntryPoint(process::Handle processHandle, void* moduleBase, uint32_t entryPointRva)
{
for (size_t i = 0u; i < INJECTED_CODE_SIZE; ++i)
{
uint8_t* address = pointer::Offset<uint8_t*>(moduleBase, entryPointRva + i);
process::WriteProcessMemory(processHandle, address, m_originalCode[i]);
}
}