Ejemplo n.º 1
0
void
terminate_IPC(int idx)
{
    ipc_channel_t *channel = &IPC[idx];

    if (channel->standalone)
    {
        dr_raw_mem_free(channel->shared_mem, sizeof(Sigil2DBISharedData));
    }
    else
    {
        /* send terminate sequence */
        uint finished = SIGIL2_IPC_FINISHED;
        uint last_buffer = channel->shmem_buf_idx;
        if(dr_write_file(channel->full_fifo, &last_buffer, sizeof(last_buffer)) != sizeof(last_buffer) ||
           dr_write_file(channel->full_fifo, &finished,    sizeof(finished))    != sizeof(finished))
            DR_ABORT_MSG("error writing finish sequence sigil2 fifos");

        /* wait for sigil2 to disconnect */
        while(dr_read_file(channel->empty_fifo, &finished, sizeof(finished)) > 0);

        dr_close_file(channel->empty_fifo);
        dr_close_file(channel->full_fifo);
        dr_unmap_file(channel->shared_mem, sizeof(Sigil2DBISharedData));
    }

    dr_global_free((void*)channel->ticket_queue.head, sizeof(ticket_queue_t));
    dr_mutex_destroy(channel->queue_lock);
}
Ejemplo n.º 2
0
static
void custom_unix_test(void)
{
    void *array;
    bool ok;

    /* "linux" is replaced by "1" in .template so we use "Linux" */
    dr_fprintf(STDERR, "  testing custom Linux alloc....");

    array = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, NULL);
    if (array == NULL)
        dr_fprintf(STDERR, "error: unable to mmap\n");
    write_array(array);

# ifdef LINUX
    array = dr_raw_mremap(array, PAGE_SIZE, PAGE_SIZE*2, MREMAP_MAYMOVE, NULL);
    if ((ptr_int_t)array <= 0 && (ptr_int_t)array >= -PAGE_SIZE)
        dr_fprintf(STDERR, "error: unable to mremap\n");
    write_array(array);
# endif

    ok = dr_raw_mem_free(array, PAGE_SIZE*2);
    if (!ok)
        dr_fprintf(STDERR, "error: failed to munmap\n");

# ifdef LINUX
    array = dr_raw_brk(0);
    if (array == NULL)
        dr_fprintf(STDERR, "error: unable to query brk\n");
# endif

    dr_fprintf(STDERR, "success\n");
}
Ejemplo n.º 3
0
static void
event_thread_exit(void *drcontext)
{
    per_thread_t *data = dr_get_tls_field(drcontext);
    dr_raw_mem_free(data->buf_base, TLS_BUF_SIZE);
    dr_thread_free(drcontext, data, sizeof(*data));
}
Ejemplo n.º 4
0
static void
event_thread_exit(void *drcontext)
{
    per_thread_t *data;
    memtrace(drcontext); /* dump any remaining buffer entries */
    data = drmgr_get_tls_field(drcontext, tls_idx);
    dr_mutex_lock(mutex);
    num_refs += data->num_refs;
    dr_mutex_unlock(mutex);
    log_stream_close(data->logf); /* closes fd too */
    dr_raw_mem_free(data->buf_base, MEM_BUF_SIZE);
    dr_thread_free(drcontext, data, sizeof(per_thread_t));
}
Ejemplo n.º 5
0
static
void raw_alloc_test(void)
{
    uint prot;
    char *array, *preferred;
    dr_mem_info_t info;
    bool res;
    dr_fprintf(STDERR, "  testing raw memory alloc...");

    /* Find a free region of memory without inadvertently "preloading" it.
     * First probe by allocating 2x the platform allocation alignment unit.
     */
    array = dr_raw_mem_alloc(HINT_ALLOC_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, NULL);
    /* Then select the second half as the preferred address for the allocation test. */
    preferred = (void *)((ptr_uint_t)array + HINT_OFFSET);
    /* Free the probe allocation. */
    dr_raw_mem_free(array, HINT_ALLOC_SIZE);
    array = preferred;

    /* Now `array` is guaranteed to be available. */
    res = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE,
                           array) != NULL;
    if (!res) {
        dr_fprintf(STDERR, "[error: fail to alloc at "PFX"]\n", array);
        return;
    }
    write_array(array);
    dr_query_memory((const byte *)array, NULL, NULL, &prot);
    if (prot != get_os_mem_prot(DR_MEMPROT_READ|DR_MEMPROT_WRITE))
        dr_fprintf(STDERR, "[error: prot %d doesn't match rw]\n", prot);
    dr_raw_mem_free(array, PAGE_SIZE);
    dr_query_memory_ex((const byte *)array, &info);
    if (info.prot != DR_MEMPROT_NONE)
        dr_fprintf(STDERR, "[error: prot %d doesn't match none]\n", info.prot);
    dr_fprintf(STDERR, "success\n");
}
Ejemplo n.º 6
0
static void
test_instr_as_immed(void)
{
    void *drcontext = dr_get_current_drcontext();
    instrlist_t *ilist = instrlist_create(drcontext);
    byte *pc;
    instr_t *ins0, *ins1, *ins2;
    opnd_t opnd;
    byte *highmem = PREFERRED_ADDR;
    pc = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ|DR_MEMPROT_WRITE|
                               DR_MEMPROT_EXEC, highmem);
    ASSERT(pc == highmem);

    /* Test push_imm of instr */
    ins0 = INSTR_CREATE_nop(drcontext);
    instrlist_append(ilist, ins0);
    instrlist_insert_push_instr_addr(drcontext, ins0, highmem,
                                     ilist, NULL, &ins1, &ins2);
    ASSERT(ins2 != NULL);
    instrlist_append(ilist, INSTR_CREATE_pop
                     (drcontext, opnd_create_reg(DR_REG_RAX)));
    instrlist_append(ilist, INSTR_CREATE_ret(drcontext));
    pc = instrlist_encode(drcontext, ilist, highmem, true);
    instrlist_clear(drcontext, ilist);
    ASSERT(pc < highmem + PAGE_SIZE);
    pc = ((byte* (*)(void))highmem)();
    ASSERT(pc == highmem);

    /* Test mov_imm of instr */
    ins0 = INSTR_CREATE_nop(drcontext);
    instrlist_append(ilist, ins0);
    /* Beyond TOS, but a convenient mem dest */
    opnd = opnd_create_base_disp(DR_REG_RSP, DR_REG_NULL, 0, -8, OPSZ_8);
    instrlist_insert_mov_instr_addr(drcontext, ins0, highmem, opnd,
                                    ilist, NULL, &ins1, &ins2);
    ASSERT(ins2 != NULL);
    instrlist_append(ilist, INSTR_CREATE_mov_ld
                     (drcontext, opnd_create_reg(DR_REG_RAX), opnd));
    instrlist_append(ilist, INSTR_CREATE_ret(drcontext));
    pc = instrlist_encode(drcontext, ilist, highmem, true);
    instrlist_clear(drcontext, ilist);
    ASSERT(pc < highmem + PAGE_SIZE);
    pc = ((byte* (*)(void))highmem)();
    ASSERT(pc == highmem);

    instrlist_clear_and_destroy(drcontext, ilist);
    dr_raw_mem_free(highmem, PAGE_SIZE);
}
Ejemplo n.º 7
0
static
void raw_alloc_test(void)
{
    uint prot;
    char *array = PREFERRED_ADDR;
    dr_mem_info_t info;
    bool res;
    dr_fprintf(STDERR, "  testing raw memory alloc...");
    res = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE,
                           array) != NULL;
    if (!res) {
        dr_fprintf(STDERR, "[error: fail to alloc at "PFX"]\n", array);
        return;
    }
    write_array(array);
    dr_query_memory((const byte *)array, NULL, NULL, &prot);
    if (prot != get_os_mem_prot(DR_MEMPROT_READ|DR_MEMPROT_WRITE))
        dr_fprintf(STDERR, "[error: prot %d doesn't match rw]\n", prot);
    dr_raw_mem_free(array, PAGE_SIZE);
    dr_query_memory_ex((const byte *)array, &info);
    if (info.prot != DR_MEMPROT_NONE)
        dr_fprintf(STDERR, "[error: prot %d doesn't match none]\n", info.prot);
    dr_fprintf(STDERR, "success\n");
}
Ejemplo n.º 8
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");
}