From 67ebaf0d22fefa885d29c3c697fbe61956d18354 Mon Sep 17 00:00:00 2001 From: comex Date: Sat, 14 Feb 2015 23:14:14 -0500 Subject: Trampoline fixes. The transformed code was incorrect because it assumed the pointer it was writing to was where the code would execute, but it was actually 'rewritten_temp'. Changed transform_dis_main to take a pc_trampoline pointer, which also helps the test harness. However, this means that it has to be called after the trampoline has been allocated, while before the trampoline allocation depended on the generated size; this change doesn't bother to use two passes or anything, but just allocates a new code buffer if the maximum possible size isn't available - not the end of the world, since trampoline_ptr will still only be increased by the actual size before the next hook in the series (if any). --- test/test-transform-dis.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'test/test-transform-dis.c') diff --git a/test/test-transform-dis.c b/test/test-transform-dis.c index f55daa9..2d7de9e 100644 --- a/test/test-transform-dis.c +++ b/test/test-transform-dis.c @@ -29,11 +29,13 @@ static void do_manual(uint8_t *in, size_t in_size, int patch_size, printf("\n#if 0\n"); uint_tptr pc_patch_start = 0x10000; uint_tptr pc_patch_end = pc_patch_start + patch_size; + uint_tptr pc_trampoline = 0xf000; int ret = transform_dis_main( in, &rewritten_ptr, pc_patch_start, &pc_patch_end, + pc_trampoline, &arch, offsets); printf("=> %d\n", ret); @@ -95,12 +97,17 @@ static void do_auto(uint8_t *in, size_t in_size, struct arch_dis_ctx arch) { if (!memcmp(expect, "_ERR", 4)) { expect_err = true; in += 4; + assert(!memcmp(in, "GIVEN", 5)); + in += 5; } else { - uint8_t *next = memmem(in, end - in, "GIVEN", 5); - if (!next) - next = end; - expect_size = next - expect; - in = next; + in = memmem(in, end - in, "GIVEN", 5); + if (in) { + expect_size = in - expect; + in += 5; + } else { + in = end; + expect_size = in - expect; + } } size_t patch_size = given_size; int offsets[patch_size + 15]; @@ -108,15 +115,19 @@ static void do_auto(uint8_t *in, size_t in_size, struct arch_dis_ctx arch) { void *rewritten_ptr = out; uint_tptr pc_patch_start = 0xdead0000; uint_tptr pc_patch_end = pc_patch_start + patch_size; + uint_tptr pc_trampoline = 0xdeac0000; int ret = transform_dis_main( given, &rewritten_ptr, pc_patch_start, &pc_patch_end, + pc_trampoline, &arch, offsets); if (ret) { - if (!expect_err) { + if (expect_err) { + printf("OK\n"); + } else { print_given(given, given_size); printf("got ret %d, expected success\n\n", ret); } @@ -132,6 +143,8 @@ static void do_auto(uint8_t *in, size_t in_size, struct arch_dis_ctx arch) { printf("but expected:\n"); hex_dump(expect, expect_size); printf("\n"); + } else { + printf("OK\n"); } } -- cgit v1.2.3