Compiling for drwrap_replace

31 views
Skip to first unread message

Mohammad Ewais

unread,
Apr 8, 2022, 1:30:38 PM4/8/22
to DynamoRIO Users
Hello,

I am trying to insert multiple threads into an application. Here's my procedure:
1. The original application uses pthread_create, and is compiled and linked against it.
2. I use drwrap_replace to replace the application's main with my own. This works pretty well, I inserted a bunch of couts in the replacement main and they print fine, it exits without errors too.
3. In the same replacement main, I started inserting calls to pthread_create and pthread_join. The prints still execute OK, but once it reaches pthread_create for the first time, it segfaults right away.
4. So far, I have been compiling without linking with the pthread library (probably why it seg faults when it hits pthread_create?). If I start linking against it, as expected, the entire client breaks down.
5. It is worth noting that I also drwrap_wrap the calls to pthread_create so I can have the freedom to modify its arguments, the pre wrappers are never called too.

So, how do I solve this? what is the proper way to compile this replacement main function so that pthread_create would execute?

For reference, here's the replacement function:
void* PlaceholderThread(void* args)
{
    return nullptr;
}

int ReplacementMain(int argc, char** argv)
{
    pthread_t thread[argc];
    std::cout << "Start\n";
    for (int i = 0; i < argc; i++)
    {
        pthread_create(&thread[i], nullptr, &PlaceholderThread, nullptr);
    }
    std::cout << "Middle\n";
    for (int i = 0; i < argc; i++)
    {
        pthread_join(0, nullptr);
    }
    std::cout << "End\n";
    return 0;
}

Mohammad Ewais

unread,
Apr 8, 2022, 1:33:05 PM4/8/22
to DynamoRIO Users
It may also be worth noting, that I also drwrap_wrap the replaced main, and put the number of threads I want to create as argc. Although I doubt this has anything to do with the issue I am facing.

Mohammad Ewais

unread,
Apr 8, 2022, 4:42:38 PM4/8/22
to DynamoRIO Users
I was able to overcome this issue by passing the addresses of pthread_create amd pthread_join to the replaced main. Then called the functions using these as pointers rather than explicit functions. 
The addresses where obtained during module loads, this way there is no need to link against libpthread. This is very hacky however, any cleaner alternatives??

Derek Bruening

unread,
Apr 12, 2022, 3:38:18 PM4/12/22
to Mohammad Ewais, DynamoRIO Users
It sounds like you're hoping for a mechanism where code compiled as part of your client is automatically linked against the application's libpthread, but for code run in a client context we generally want to have strong isolation from app libraries so you'd need some separate compilation unit exempt from private loading.  Using direct pointers as you did is probably the simplest solution.  Dr. Memory used to do something like this when it wrapped the app's allocator instead of completely replacing it but wanted to replace some functions like realloc: it had a template function to run as the app and it would replace certain function calls once it found the right address: https://github.com/DynamoRIO/drmemory/blob/master/common/alloc_unopt.c#L67https://github.com/DynamoRIO/drmemory/blob/master/common/alloc.c#L1011.

--
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/50690a2a-a616-4f3e-be79-4715e88309bbn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages