Skip to content

Commit d9e96cd

Browse files
chore: separate invalid addr read write to a test
1 parent 721f6dc commit d9e96cd

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

src/tests/test_windows.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ TEST(BlookMemoryTests, ProcessMemoryAPI) {
204204

205205
// Test malloc/free
206206
auto ptr = proc->malloc(1024, Protect::rw);
207-
ASSERT_NE((void*)ptr, nullptr);
207+
ASSERT_NE((void *)ptr, nullptr);
208208
EXPECT_TRUE(proc->check_valid(ptr));
209209
EXPECT_TRUE(proc->check_readable(ptr, 1024));
210210
EXPECT_TRUE(proc->check_writable(ptr, 1024));
@@ -223,7 +223,7 @@ TEST(BlookMemoryTests, ProcessMemoryAPI) {
223223

224224
// Test near malloc
225225
auto near_ptr = proc->malloc(1024, Protect::rw, (void *)0x10000);
226-
ASSERT_NE((void*)near_ptr, nullptr);
226+
ASSERT_NE((void *)near_ptr, nullptr);
227227
proc->free(near_ptr);
228228
}
229229

@@ -275,10 +275,6 @@ TEST(BlookMemoryTests, NewReadWriteAPI) {
275275
ASSERT_TRUE(res.has_value()) << res.error();
276276
EXPECT_EQ(*res, 0x7fffffff);
277277

278-
Pointer invalid_ptr = (void *)0x1234; // Likely invalid
279-
auto res_invalid = invalid_ptr.try_read_s32();
280-
EXPECT_FALSE(res_invalid.has_value());
281-
282278
// Test read/write pointer
283279
int target_val = 123;
284280
void *target_ptr = &target_val;
@@ -315,6 +311,13 @@ TEST(BlookMemoryTests, NewReadWriteAPI) {
315311
(uint8_t)'g', 0, (uint8_t)'h', 0}));
316312
}
317313

314+
TEST(BlookMemoryTests, InvalidAddrReadNoCrash) {
315+
using namespace blook;
316+
Pointer invalid_ptr = (void *)0x1234; // Likely invalid
317+
auto res_invalid = invalid_ptr.try_read_s32();
318+
EXPECT_FALSE(res_invalid.has_value());
319+
}
320+
318321
TEST(BlookMemoryTests, ScopedSetMemoryRWX) {
319322
using namespace blook;
320323
int val = 123;
@@ -649,7 +652,7 @@ TEST(BlookReassemblyTests, ReassemblyWithPadding) {
649652
// Allocate executable memory for testing
650653
auto proc = Process::self();
651654
auto mem = proc->malloc(64, Protect::rwx);
652-
ASSERT_NE((void*)mem, nullptr);
655+
ASSERT_NE((void *)mem, nullptr);
653656

654657
// Write some original code (just NOPs for testing)
655658
Pointer ptr = mem;
@@ -659,7 +662,7 @@ TEST(BlookReassemblyTests, ReassemblyWithPadding) {
659662
// Create a range and reassemble with smaller code
660663
MemoryRange range(ptr, 20);
661664
auto patch_result =
662-
range.try_reassembly_with_padding([](zasm::x86::Assembler& a) {
665+
range.try_reassembly_with_padding([](zasm::x86::Assembler &a) {
663666
// Generate a small piece of code (2 bytes: ret)
664667
a.ret();
665668
});
@@ -690,14 +693,14 @@ TEST(BlookReassemblyTests, ReassemblyWithPaddingTooLarge) {
690693
// Allocate executable memory
691694
auto proc = Process::self();
692695
auto mem = proc->malloc(64, Protect::rwx);
693-
ASSERT_NE((void*)mem, nullptr);
696+
ASSERT_NE((void *)mem, nullptr);
694697

695698
Pointer ptr = mem;
696699

697700
// Create a small range and try to fit too much code
698701
MemoryRange range(ptr, 5);
699702
auto patch_result =
700-
range.try_reassembly_with_padding([](zasm::x86::Assembler& a) {
703+
range.try_reassembly_with_padding([](zasm::x86::Assembler &a) {
701704
// Generate code that's definitely larger than 5 bytes
702705
if constexpr (blook::utils::compileArchitecture() ==
703706
blook::utils::Architecture::x86_64) {
@@ -739,10 +742,8 @@ TEST(BlookReassemblyTests, ReassemblyWithTrampoline) {
739742
auto original_bytes = func_ptr.read_bytearray(20);
740743

741744
// Test 3: Apply trampoline with user code that sets eax before original code
742-
auto patch_result =
743-
func_ptr.try_reassembly_with_trampoline([](zasm::x86::Assembler& a) {
744-
a.mov(zasm::x86::ecx, zasm::Imm(999));
745-
});
745+
auto patch_result = func_ptr.try_reassembly_with_trampoline(
746+
[](zasm::x86::Assembler &a) { a.mov(zasm::x86::ecx, zasm::Imm(999)); });
746747
ASSERT_TRUE(patch_result.has_value()) << patch_result.error();
747748
ASSERT_TRUE(patch_result->patch());
748749

@@ -785,7 +786,7 @@ TEST(BlookReassemblyTests, ReassemblyWithTrampolineCounterFunction) {
785786

786787
// Test 3: Apply trampoline that modifies the increment_by parameter
787788
auto patch_result =
788-
counter_ptr.try_reassembly_with_trampoline([](zasm::x86::Assembler& a) {
789+
counter_ptr.try_reassembly_with_trampoline([](zasm::x86::Assembler &a) {
789790
// Modify the first parameter (increment_by) to 999
790791
a.mov(zasm::x86::ecx, zasm::Imm(999));
791792
});
@@ -795,12 +796,12 @@ TEST(BlookReassemblyTests, ReassemblyWithTrampolineCounterFunction) {
795796

796797
// Test 4: Verify trampoline modifies parameter
797798
g_trampoline_counter = 100;
798-
asm_counter_function(5); // Pass 5, but trampoline changes it to 999
799+
asm_counter_function(5); // Pass 5, but trampoline changes it to 999
799800
EXPECT_EQ(g_trampoline_counter, 1099)
800801
<< "Trampoline should change increment to 999";
801802

802803
g_trampoline_counter = 200;
803-
asm_counter_function(10); // Pass 10, but trampoline changes it to 999
804+
asm_counter_function(10); // Pass 10, but trampoline changes it to 999
804805
EXPECT_EQ(g_trampoline_counter, 1199);
805806

806807
// Test 5: Verify bytes were modified
@@ -826,7 +827,8 @@ TEST(BlookMemoryTests, PatternParsing) {
826827
ASSERT_NE(process, nullptr);
827828

828829
const uint8_t test_data[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22};
829-
auto range = blook::MemoryRange(process, (void*)test_data, sizeof(test_data));
830+
auto range =
831+
blook::MemoryRange(process, (void *)test_data, sizeof(test_data));
830832

831833
EXPECT_TRUE(range.find_one_pattern("aabbcc").has_value());
832834
EXPECT_TRUE(range.find_one_pattern("aa bb cc").has_value());

0 commit comments

Comments
 (0)