Skip to content

Commit c258967

Browse files
refactor: all in xmake
1 parent 705d2af commit c258967

File tree

5 files changed

+28
-82
lines changed

5 files changed

+28
-82
lines changed

CMakeLists.txt

Lines changed: 0 additions & 53 deletions
This file was deleted.

include/blook/disassembly.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "concepts.h"
55
#include "memo.h"
66
#include "utils.h"
7+
#include <print>
78
#include <utility>
89
#include <vector>
910
#include <zasm/zasm.hpp>
@@ -88,7 +89,6 @@ template <typename Range> class DisassembleRange {
8889
DisassembleIteratorR(DisassembleIteratorR &&) = default;
8990
DisassembleIteratorR &operator=(const DisassembleIteratorR &) = default;
9091
DisassembleIteratorR &operator=(DisassembleIteratorR &&) = default;
91-
9292

9393
explicit DisassembleIteratorR(Range range, const Pointer &address,
9494
zasm::MachineMode machine_mode,
@@ -122,6 +122,7 @@ template <typename Range> class DisassembleRange {
122122

123123
private:
124124
void decode_next() {
125+
retry:
125126
using namespace zasm;
126127
Decoder d(machine_mode);
127128

@@ -140,20 +141,18 @@ template <typename Range> class DisassembleRange {
140141
std::copy(ptr, ptr + buffer.size(), buffer.begin());
141142
const auto r = d.decode(buffer.data(), BufferSize, address);
142143
if (!r.hasValue()) {
143-
d = Decoder(machine_mode);
144-
ptr += 4;
145-
address += 4;
146-
return decode_next();
147-
}
148-
149-
const auto size = r->getLength();
150-
ptr += size;
151-
address += size;
152-
current_value = InstructionCtx{r.value(), address};
153-
154-
if (ptr == range_end) {
155-
over = true;
156-
return;
144+
ptr += 1;
145+
address += 1;
146+
goto retry;
147+
} else {
148+
const auto size = r->getLength();
149+
ptr += size;
150+
address += size;
151+
current_value = InstructionCtx{r.value(), address};
152+
153+
if (ptr == range_end) {
154+
over = true;
155+
}
157156
}
158157
}
159158
};

src/hook.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "blook/blook.h"
33
#include <cstdint>
44
#include <format>
5-
5+
#include <zasm/formatter/formatter.hpp>
66
namespace blook {
77
InlineHook::InlineHook(void *target, void *hook_func)
88
: target(target), hook_func(hook_func) {}
@@ -51,6 +51,7 @@ void InlineHook::install(bool try_trampoline) {
5151

5252
const auto &instrInfo = *decoderRes;
5353
const auto instr = instrInfo.getInstruction();
54+
5455
if (auto res = b.emit(instr); res != zasm::ErrorCode::None)
5556
throw std::runtime_error(
5657
std::format("Failed to emit instruction at {} {}", curAddress,
@@ -66,9 +67,9 @@ void InlineHook::install(bool try_trampoline) {
6667
}
6768

6869
if (try_trampoline && !p_trampoline) {
69-
const auto trampoline_code_size =
70+
const auto trampoline_code_size_estimated =
7071
utils::estimateCodeSize(programTrampoline);
71-
trampoline_size = trampoline_code_size;
72+
trampoline_size = trampoline_code_size_estimated;
7273
const auto trampolineCode =
7374
Pointer::malloc_near_rwx(target, trampoline_size);
7475

@@ -79,7 +80,8 @@ void InlineHook::install(bool try_trampoline) {
7980
throw std::runtime_error(std::format("JIT Serialization failure: {} {}",
8081
err.getErrorName(),
8182
err.getErrorMessage()));
82-
std::memcpy(trampolineCode, serializer2.getCode(), trampoline_code_size);
83+
std::memcpy(trampolineCode, serializer2.getCode(),
84+
trampoline_code_size_estimated);
8385
p_trampoline = trampolineCode;
8486
}
8587

src/utils.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "blook/misc.h"
33
#include <format>
44
#include <stdexcept>
5+
#include <zasm/formatter/formatter.hpp>
6+
57
namespace blook {
68

79
namespace utils {
@@ -17,11 +19,7 @@ std::size_t estimateCodeSize(const zasm::Program &program) {
1719
if (instrInfo.hasValue()) {
1820
size += instrInfo->getLength();
1921
} else {
20-
throw std::runtime_error(
21-
std::format("Failed to estimate code size, error: {} {}",
22-
instrInfo.error().getErrorName(),
23-
instrInfo.error().getErrorMessage()
24-
));
22+
size += 15; // worst case size for an instruction
2523
}
2624
} else if (auto *nodeEmbeddedLabel = node->getIf<zasm::EmbeddedLabel>();
2725
nodeEmbeddedLabel != nullptr) {
@@ -42,7 +40,7 @@ BLOOK_TEXT_SECTION uint8_t _getR11[] = {
4240
// ret
4341
0xC3};
4442

45-
getreg_fn_t getR11 = (getreg_fn_t)(void*)_getR11;
43+
getreg_fn_t getR11 = (getreg_fn_t)(void *)_getR11;
4644

4745
BLOOK_TEXT_SECTION uint8_t _getStackPointer[] = {
4846
// mov rax, rsp
@@ -57,7 +55,7 @@ BLOOK_TEXT_SECTION uint8_t _getEDX[] = {
5755
// ret
5856
0xC3};
5957

60-
getreg_fn_t getEDX = (getreg_fn_t)(void*)_getEDX;
58+
getreg_fn_t getEDX = (getreg_fn_t)(void *)_getEDX;
6159
BLOOK_TEXT_SECTION uint8_t _getStackPointer[] = {
6260
// mov eax, esp
6361
0x89, 0xE0,
@@ -81,7 +79,7 @@ BLOOK_TEXT_SECTION uint8_t get_peb_fn_buf[] = {
8179
#endif
8280
getreg_fn_t getPEB = (getreg_fn_t)(void *)get_peb_fn_buf;
8381

84-
getreg_fn_t getStackPointer = (getreg_fn_t)(void*)_getStackPointer;
82+
getreg_fn_t getStackPointer = (getreg_fn_t)(void *)_getStackPointer;
8583

8684
} // namespace utils
8785
} // namespace blook

xmake.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
set_xmakever("2.9.8")
22

33
set_allowedplats("windows", "linux")
4-
4+
add_rules("plugin.compile_commands.autoupdate", {outputdir = "build"})
55
add_rules("mode.debug", "mode.release", "mode.releasedbg")
66
set_allowedmodes("debug", "release", "releasedbg")
77

88
set_languages("c++23")
99
set_encodings("utf-8")
1010

11-
add_requires("zasm 916f28f882801c048eaececc2466c8fdc17653fa")
11+
add_requires("zasm 2025.03.02")
1212

1313
target("blook")
1414
set_kind("static")

0 commit comments

Comments
 (0)