Skip to content

Commit

Permalink
attempt to fix wrappers overlapping
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Jun 19, 2024
1 parent e15aaee commit d869c7e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
54 changes: 39 additions & 15 deletions src/generator/X64Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,7 @@ Result<> X64HandlerGenerator::relocateRIPInstruction(cs_insn* insn, uint8_t* buf
return X86HandlerGenerator::relocateRIPInstruction(insn, buffer, trampolineAddress, originalAddress, disp);
}

Result<FunctionData> X64WrapperGenerator::generateWrapper() {
if (!m_metadata.m_convention->needsWrapper(m_metadata.m_abstract)) {
return Ok(FunctionData{m_address, 0});
}

// this is silly, butt
auto codeSize = this->wrapperBytes(0).size();
auto areaSize = (codeSize + (0x20 - codeSize) % 0x20);

TULIP_HOOK_UNWRAP_INTO(auto area, Target::get().allocateArea(areaSize));
auto address = reinterpret_cast<uint64_t>(area);

std::vector<uint8_t> X64WrapperGenerator::wrapperBytes(uint64_t address) {
X64Assembler a(address);
using enum X64Register;

Expand Down Expand Up @@ -504,9 +493,12 @@ Result<FunctionData> X64WrapperGenerator::generateWrapper() {

a.align16();

auto codeSize2 = a.m_buffer.size();
return std::move(a.m_buffer);
}

#ifdef TULIP_HOOK_WINDOWS
std::vector<uint8_t> X64WrapperGenerator::unwindInfoBytes(uint64_t address) {
X64Assembler a(address);

{
auto const offsetBegin = address & 0xffff;
Expand All @@ -520,6 +512,8 @@ Result<FunctionData> X64WrapperGenerator::generateWrapper() {

// RUNTIME_FUNCTION

a.label("wrapper-unwind-info");

a.write32(offsetBegin); // BeginAddress
a.write32(offsetEnd); // EndAddress
a.write32(offsetEnd + 0xc); // UnwindData
Expand Down Expand Up @@ -552,11 +546,41 @@ Result<FunctionData> X64WrapperGenerator::generateWrapper() {
);
}

return std::move(a.m_buffer);
}
#endif

Result<FunctionData> X64WrapperGenerator::generateWrapper() {
if (!m_metadata.m_convention->needsWrapper(m_metadata.m_abstract)) {
return Ok(FunctionData{m_address, 0});
}

// this is silly, butt
auto codeSize = this->wrapperBytes(0).size();

#ifdef TULIP_HOOK_WINDOWS
auto unwindInfoSize = this->unwindInfoBytes(0).size();
auto totalSize = codeSize + unwindInfoSize;
#else
auto totalSize = codeSize;
#endif

auto areaSize = (totalSize + (0x20 - totalSize) % 0x20);

TULIP_HOOK_UNWRAP_INTO(auto area, Target::get().allocateArea(areaSize));
auto address = reinterpret_cast<uint64_t>(area);

auto code = this->wrapperBytes(address);
codeSize = code.size();

#ifdef TULIP_HOOK_WINDOWS
auto unwindInfo = this->unwindInfoBytes(address + codeSize);
code.insert(code.end(), unwindInfo.begin(), unwindInfo.end());
#endif

TULIP_HOOK_UNWRAP(Target::get().writeMemory(area, a.m_buffer.data(), a.m_buffer.size()));
TULIP_HOOK_UNWRAP(Target::get().writeMemory(area, code.data(), code.size()));

return Ok(FunctionData{area, codeSize2});
return Ok(FunctionData{area, codeSize});
}

// std::vector<uint8_t> X64WrapperGenerator::reverseWrapperBytes(uint64_t address) {
Expand Down
6 changes: 5 additions & 1 deletion src/generator/X64Generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ namespace tulip::hook {
public:
using X86WrapperGenerator::X86WrapperGenerator;

// std::vector<uint8_t> wrapperBytes(uint64_t address) override;
std::vector<uint8_t> wrapperBytes(uint64_t address) override;

#ifdef TULIP_HOOK_WINDOWS
std::vector<uint8_t> unwindInfoBytes(uint64_t address);
#endif

Result<FunctionData> generateWrapper() override;
// std::vector<uint8_t> reverseWrapperBytes(uint64_t address) override;
Expand Down

0 comments on commit d869c7e

Please sign in to comment.