Help!!

79 views
Skip to first unread message

Андрей Неважный

unread,
Feb 15, 2021, 11:35:46 AM2/15/21
to DynamoRIO Users
Hello, I'm trying to create my client using DR, but I have problems
my client:
#include "dr_api.h"
#include "drmgr.h"
#include "drsyms.h"
#include "drwrap.h"

#if defined(TARGET_MAC)
char  *MALLOC = "_malloc";
char *FREE = "_free";
#else
char *MALLOC = "malloc";
char *FREE = "free";
#endif

static void
event_exit();
static void module_load_event(void *, const module_data_t *, bool);

static void
MallocBefore(void *wrapcxt, OUT void **user_data)
{
    dr_printf("function \"mallocBEFORE\" is running..\n");

}

static void
MallocAfter(void *wrapcxt, OUT void *user_data)
{
    dr_printf("function \"mallocAFTER\" is running..\n");
    app_pc address = (app_pc)drwrap_get_retval(wrapcxt); // возвращаемое значение malloc, которое указывает на начало где выделена память
    dr_printf(" (malloc)drwrap_get_retval -  %x \n", address);
}

static void
module_load_event(void *drcontext, const module_data_t *mod, bool loaded)
{
    app_pc orig_malloc;
    size_t offs_malloc;
    orig_malloc = (app_pc)dr_get_proc_address((module_handle_t)mod->start, MALLOC);
    if (orig_malloc != NULL) {
        dr_printf("function \"%s\" is found..\n", MALLOC);
        bool wrapped = drwrap_wrap(orig_malloc, MallocBefore, MallocAfter);
    }
}

DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
    drsym_init(0);
    drmgr_init();
    drwrap_init();
    drmgr_register_module_load_event(module_load_event);
    dr_register_exit_event(event_exit);
}


static void
event_exit() {
    drmgr_exit();
    drsym_exit();
    drwrap_exit();
}

and my test application:

#include <stdio.h>
#include <malloc.h>
#include <iostream>

int main() {
    char* t = NULL;
    t = (char*)malloc(7 * sizeof(char));
    int* n = NULL;
    n = (int*)malloc(2 * sizeof(int));
    int* k = new int;
    printf("t - %x\n", t);
    printf("n - %x\n", n);
    printf("k - %x\n", k);
    free(t);
    free(n);
    delete k;
}

It works correctly but what the trash in the end ->
function "free" is found..
function "free" is wrapped..
function "malloc" is found..
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  da3dec0
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  da3de20
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  da30850
t - da3dec0
n - da3de20
k - da30850
function "malloc" is found..
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  8e131250
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  8e131a00
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  8e131250
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  8e131300
function "mallocBEFORE" is running..
function "mallocAFTER" is running..
 (malloc)drwrap_get_retval -  8e131320

what's wrong? and how can I fix it?
thanks

Abhinav Sharma

unread,
Feb 16, 2021, 1:48:33 PM2/16/21
to dynamor...@googlegroups.com
Hi,
If I understand correctly, the question is to figure out where the italicized logs (the ones after the values of t,n and k are printed) are coming from.

My guess is they could be coming from the implementation of free, or some other cleanup after `main()` returns? To confirm where, maybe try adding some more print statements in the test app `main()`, before/after the `free`s. You can also try running under gdb and checking backtrace when the unexpected `mallocBefore` is invoked.

Abhinav

--
You received this message because you are subscribed to the Google Groups "DynamoRIO Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dynamorio-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dynamorio-users/93e61d0b-562f-4251-9b65-611ff8f6508en%40googlegroups.com.

Андрей Неважный

unread,
Feb 17, 2021, 3:22:33 AM2/17/21
to DynamoRIO Users
Hi,
Thanks a lot for reply. Actually, I'm trying to control each heap allocation by the application and how you can see in my test application I have 2 malloc and 1 new. And my tasl to register ONLY this heap allocations. What advice can you give me?

Tnaks.
вторник, 16 февраля 2021 г. в 21:48:33 UTC+3, sharma...@google.com:

Abhinav Sharma

unread,
Feb 17, 2021, 4:41:42 PM2/17/21
to dynamor...@googlegroups.com
Hi,
I think as the first step, figure out where those un-interesting heap allocations are coming from. As I mentioned, we can use gdb to get a backtrace when the unexpected `mallocBefore`s are invoked. This will be helpful in determining how we can filter out these un-interesting malloc/new calls.

Looking at the documentation of drwrap_wrap, I don't see any way to conditionally wrap only specific invocations of the function. If it turns out that the un-interesting invocations are always from a different module, maybe we can use drwrap_get_func or drwrap_get_retaddr to get the location of the wrapped function, and check whether it lies within the required module limits that you want. The module limits can be found in module_data_t arg to drmgr_register_module_load_event.

Abhinav

Reply all
Reply to author
Forward
0 new messages