Function wrapping is unreliable when using drwrap_skip_call
Created by: kholia
Function wrapping in drwrap extension seems to be unreliable, possibly when combined with drwrap_skip_call
feature.
Here is my drwrap client code (contained in nop.c
),
#include "dr_api.h"
#include "utils.h"
#include "drwrap.h"
#include "drmgr.h"
#include <stdio.h>
void wrap_pre_sleep(void *wrapcxt, OUT void **user_data)
{
unsigned long int status = 0;
drwrap_skip_call(wrapcxt, (void *) status, 0);
}
static void module_load_event(void *drcontext, const module_data_t * mod, bool loaded)
{
bool ok;
app_pc towrap = (app_pc) dr_get_proc_address(mod->handle, "sleep");
if (towrap != NULL) {
ok = drwrap_wrap(towrap, wrap_pre_sleep, NULL);
}
}
DR_EXPORT void
dr_init(client_id_t id)
{
drmgr_init();
drwrap_init();
drmgr_register_module_load_event(module_load_event);
}
Here is my application code (contained in application.c
),
#include <stdio.h>
#include <unistd.h>
int main()
{
while (1) {
fprintf(stderr, ".");
sleep(256); // we will try to nop this
}
return 0;
}
Expected behavior,
$ ./bin64/drrun -c api/bin/libnop.so -- ../application
......... (this should keep printing without any long pauses)
sleep
from application.c
should never be called.
Actual behavior,
$ ./bin64/drrun -c api/bin/libnop.so -- ../application
.................................................................(long pause occurs here)
Sometimes the original wrapped function sleep
is called, which is not expected!
This outcome is very puzzling to me, and I can't explain it. Hopefully, this is not a bug / limitation in DynamoRIO.
All this code is available at https://github.com/kholia/dynamorio/tree/missed-call-wrapping for testing.