Exemplo n.º 1
0
static void
test_x64_abs_addr(void *dc)
{
    /* 48 a1 ef be ad de ef be ad de    mov    0xdeadbeefdeadbeef -> %rax 
     * 48 a3 ef be ad de ef be ad de    mov    %rax -> 0xdeadbeefdeadbeef 
     */
    instr_t *instr;
    opnd_t abs_addr = opnd_create_abs_addr((void*)0xdeadbeefdeadbeef, OPSZ_8);

    /* movabs load */
    instr = INSTR_CREATE_mov_ld(dc, opnd_create_reg(DR_REG_RAX), abs_addr);
    test_instr_encode(dc, instr, 10);  /* REX + op + 8 */

    /* movabs store */
    instr = INSTR_CREATE_mov_st(dc, abs_addr, opnd_create_reg(DR_REG_RAX));
    test_instr_encode(dc, instr, 10);  /* REX + op + 8 */
}
Exemplo n.º 2
0
static void
reachability_test(void)
{
    void *drcontext = dr_get_current_drcontext();
    instrlist_t *ilist = instrlist_create(drcontext);
    byte *gencode = (byte *)
        dr_nonheap_alloc(PAGE_SIZE, DR_MEMPROT_READ|DR_MEMPROT_WRITE|DR_MEMPROT_EXEC);
    byte *pc;
    int res;
    byte *highmem = PREFERRED_ADDR;
    pc = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ|DR_MEMPROT_WRITE|
                               DR_MEMPROT_EXEC, highmem);
    ASSERT(pc == highmem);

    dr_fprintf(STDERR, "  reachability test...");

    /* Test auto-magically turning rip-rel that won't reach but targets xax
     * into absmem.
     */
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_EAX),
                      opnd_create_rel_addr(highmem, OPSZ_4)));
    instrlist_append(ilist, INSTR_CREATE_ret(drcontext));
    pc = instrlist_encode(drcontext, ilist, gencode, false);
    instrlist_clear(drcontext, ilist);
    ASSERT(pc < gencode + PAGE_SIZE);
    *(int*)highmem = 0x12345678;
    res = ((int (*)(void))gencode)();
    ASSERT(res == 0x12345678);

    /* Test auto-magically turning a reachable absmem into a rip-rel. */
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_ECX),
                      opnd_create_abs_addr(highmem + 0x800, OPSZ_4)));
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_EAX),
                      opnd_create_reg(DR_REG_ECX)));
    instrlist_append(ilist, INSTR_CREATE_ret(drcontext));
    pc = instrlist_encode(drcontext, ilist, highmem, false);
    instrlist_clear(drcontext, ilist);
    ASSERT(pc < highmem + PAGE_SIZE);
    *(int*)(highmem + 0x800) = 0x12345678;
    res = ((int (*)(void))highmem)();
    ASSERT(res == 0x12345678);

    dr_raw_mem_free(highmem, PAGE_SIZE);

    /* Test targeting upper 2GB of low 4GB */
    highmem = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ|DR_MEMPROT_WRITE|
                               DR_MEMPROT_EXEC, (byte *)0xabcd0000);
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_ECX),
                      opnd_create_abs_addr(highmem, OPSZ_4)));
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_EAX),
                      opnd_create_reg(DR_REG_ECX)));
    instrlist_append(ilist, INSTR_CREATE_ret(drcontext));
    pc = instrlist_encode(drcontext, ilist, gencode, false);
    instrlist_clear(drcontext, ilist);
    ASSERT(pc < gencode + PAGE_SIZE);
    *(int*)highmem = 0x12345678;
    res = ((int (*)(void))gencode)();
    ASSERT(res == 0x12345678);
    dr_raw_mem_free(highmem, PAGE_SIZE);

    /* Test targeting lower 2GB of low 4GB */
    highmem = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ|DR_MEMPROT_WRITE|
                               DR_MEMPROT_EXEC, (byte *)0x143d0000);
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_ECX),
                      opnd_create_abs_addr(highmem, OPSZ_4)));
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_EAX),
                      opnd_create_reg(DR_REG_ECX)));
    instrlist_append(ilist, INSTR_CREATE_ret(drcontext));
    pc = instrlist_encode(drcontext, ilist, gencode, false);
    instrlist_clear(drcontext, ilist);
    ASSERT(pc < gencode + PAGE_SIZE);
    *(int*)highmem = 0x12345678;
    res = ((int (*)(void))gencode)();
    ASSERT(res == 0x12345678);
    dr_raw_mem_free(highmem, PAGE_SIZE);

    instrlist_clear_and_destroy(drcontext, ilist);
    dr_nonheap_free(gencode, PAGE_SIZE);

    test_instr_as_immed();

    dr_fprintf(STDERR, "success\n");
}