Skip to content

Commit

Permalink
move writer to dobby
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Apr 21, 2024
1 parent 0880f26 commit f032e42
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "dobby/dobby_internal.h"

void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, bool branch);
void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, bool branch, void (*writer)(void*, void const*, size_t));

void GenRelocateCodeAndBranch(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated);
void GenRelocateCodeAndBranch(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, void (*writer)(void*, void const*, size_t));
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ void gen_thumb_relocate_code(relo_ctx_t *ctx) {
}
}

void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, bool branch) {
void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, bool branch, void (*writer)(void*, void const*, size_t)) {
relo_ctx_t ctx;

if ((addr_t)buffer % 2) {
Expand Down Expand Up @@ -878,7 +878,7 @@ void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, Co
arm_turbo_assembler_.SetRealizedAddress((void *)relocated_mem);

AssemblyCode *code = NULL;
code = AssemblyCodeBuilder::FinalizeFromTurboAssembler(ctx.curr_assembler);
code = AssemblyCodeBuilder::FinalizeFromTurboAssembler(ctx.curr_assembler, writer);
relocated->reset(code->addr, code->size);
}

Expand All @@ -897,8 +897,8 @@ void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, Co
}
}

void GenRelocateCodeAndBranch(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated) {
GenRelocateCode(buffer, relocated_mem, origin, relocated, true);
void GenRelocateCodeAndBranch(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, void (*writer)(void*, void const*, size_t)) {
GenRelocateCode(buffer, relocated_mem, origin, relocated, true, writer);
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class ThumbTurboAssembler : public ThumbAssembler {
};

#if 0
void GenRelocateCodeAndBranch(void *buffer, CodeMemBlock *origin, CodeMemBlock *relocated);
void GenRelocateCodeAndBranch(void *buffer, CodeMemBlock *origin, CodeMemBlock *relocated, void (*writer)(void*, void const*, size_t));
#endif

} // namespace arm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ int relo_relocate(relo_ctx_t *ctx, void* relocated_mem, bool branch) {
// Generate executable code
{
turbo_assembler_.SetRealizedAddress((void*)relocated_mem);
auto code = AssemblyCodeBuilder::FinalizeFromTurboAssembler(&turbo_assembler_);
auto code = AssemblyCodeBuilder::FinalizeFromTurboAssembler(&turbo_assembler_, writer);
ctx->relocated = code;
}
return 0;
}

void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, bool branch) {
void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, bool branch, void (*writer)(void*, void const*, size_t)) {
relo_ctx_t ctx = {0};

ctx.buffer = ctx.buffer_cursor = (uint8_t *)buffer;
Expand All @@ -360,8 +360,8 @@ void GenRelocateCode(void *buffer, void* relocated_mem, CodeMemBlock *origin, Co
relocated->reset(ctx.relocated->addr, ctx.relocated->size);
}

void GenRelocateCodeAndBranch(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated) {
GenRelocateCode(buffer, relocated_mem, origin, relocated, true);
void GenRelocateCodeAndBranch(void *buffer, void* relocated_mem, CodeMemBlock *origin, CodeMemBlock *relocated, void (*writer)(void*, void const*, size_t)) {
GenRelocateCode(buffer, relocated_mem, origin, relocated, true, writer);
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#if 0
namespace zz {
namespace arm64 {
void GenRelocateCodeAndBranch(void *buffer, CodeMemBlock *origin, CodeMemBlock *relocated);
void GenRelocateCodeAndBranch(void *buffer, CodeMemBlock *origin, CodeMemBlock *relocated, void (*writer)(void*, void const*, size_t));
} // namespace arm64
} // namespace zz
#endif
4 changes: 2 additions & 2 deletions libraries/dobby/source/MemoryAllocator/AssemblyCodeBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "dobby/dobby_internal.h"
#include "PlatformUnifiedInterface/ExecMemory/CodePatchTool.h"

AssemblyCode *AssemblyCodeBuilder::FinalizeFromTurboAssembler(AssemblerBase *assembler) {
AssemblyCode *AssemblyCodeBuilder::FinalizeFromTurboAssembler(AssemblerBase *assembler, void (*writer)(void*, void const*, size_t)) {
auto buffer = (CodeBufferBase *)assembler->GetCodeBuffer();
auto realized_addr = (addr_t)assembler->GetRealizedAddress();
#if defined(TEST_WITH_UNICORN)
Expand All @@ -15,7 +15,7 @@ AssemblyCode *AssemblyCodeBuilder::FinalizeFromTurboAssembler(AssemblerBase *ass
}

// Realize the buffer code to the executable memory address, remove the external label, etc
memcpy((void *)realized_addr, buffer->GetBuffer(), buffer->GetBufferSize());
(*writer)((void *)realized_addr, buffer->GetBuffer(), buffer->GetBufferSize());

auto block = new AssemblyCode(realized_addr, buffer->GetBufferSize());
return block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ using AssemblyCode = CodeMemBlock;

class AssemblyCodeBuilder {
public:
static AssemblyCode *FinalizeFromTurboAssembler(AssemblerBase *assembler);
static AssemblyCode *FinalizeFromTurboAssembler(AssemblerBase *assembler, void (*writer)(void*, void const*, size_t));
};
9 changes: 3 additions & 6 deletions src/generator/ArmV7Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ Result<ArmV7HandlerGenerator::RelocateReturn> ArmV7HandlerGenerator::relocateOri
auto originBuffer = m_address;
auto relocatedBuffer = m_trampoline;

TULIP_HOOK_UNWRAP_INTO(auto protection, Target::get().getProtection(m_trampoline));
TULIP_HOOK_UNWRAP(Target::get().protectMemory(m_trampoline, 0x100, Target::get().getWritableProtection()));

GenRelocateCodeAndBranch(originBuffer, relocatedBuffer, origin, relocated);

TULIP_HOOK_UNWRAP(Target::get().protectMemory(m_trampoline, 0x100, protection));
GenRelocateCodeAndBranch(originBuffer, relocatedBuffer, origin, relocated, +[](void* dest, void const* src, size_t size) {
(void)Target::get().rawWriteMemory(dest, src, size);
});

if (relocated->size == 0) {
return Err("Failed to relocate original function");
Expand Down
9 changes: 3 additions & 6 deletions src/generator/ArmV8Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ Result<ArmV8HandlerGenerator::RelocateReturn> ArmV8HandlerGenerator::relocateOri
auto originBuffer = m_address;
auto relocatedBuffer = m_trampoline;

TULIP_HOOK_UNWRAP_INTO(auto protection, Target::get().getProtection(m_trampoline));
TULIP_HOOK_UNWRAP(Target::get().protectMemory(m_trampoline, 0x100, Target::get().getWritableProtection()));

GenRelocateCodeAndBranch(originBuffer, relocatedBuffer, origin, relocated);

TULIP_HOOK_UNWRAP(Target::get().protectMemory(m_trampoline, 0x100, protection));
GenRelocateCodeAndBranch(originBuffer, relocatedBuffer, origin, relocated, +[](void* dest, void const* src, size_t size) {
(void)Target::get().rawWriteMemory(dest, src, size);
});

if (relocated->size == 0) {
return Err("Failed to relocate original function");
Expand Down

0 comments on commit f032e42

Please sign in to comment.