how can exec change the behavior of exec'ed program

Posted by R Samuel Klatchko on Stack Overflow See other posts from Stack Overflow or by R Samuel Klatchko
Published on 2010-03-22T20:08:47Z Indexed on 2010/03/22 20:11 UTC
Read the original article Hit count: 328

Filed under:
|
|
|

I am trying to track down a very odd crash. What is so odd about it is a workaround that someone discovered and which I cannot explain.

The workaround is this small program which I'll refer to as 'runner':

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    if (argc == 1)
    {
        fprintf(stderr, "Usage: %s prog [args ...]\n", argv[0]);
        return 1;
    }

    execvp(argv[1], argv + 1);

    fprintf(stderr, "execv failed: %s\n", strerror(errno));

    // If exec returns because the program is not found or we
    // don't have the appropriate permission
    return 255;
}

As you can see, all this program does is use execvp to replace itself with a different program.

The program crashes when it is directly invoked from the command line:

/path/to/prog args  # this crashes

but works fine when it is indirectly invoked via my runner shim:

/path/to/runner /path/to/prog args   # works successfully

For the life of me, I can figure out how having an extra exec can change the behavior of the program being run (as you can see the program does not change the environment).

Some background on the crash. The crash itself is happening in the C++ runtime. Specifically, when the program does a throw, the crashing version incorrectly thinks there is no matching catch (although there is) and calls terminate. When I invoke the program via runner, the exception is properly caught.

My question is any idea why the extra exec changes the behavior of the exec'ed program?

© Stack Overflow or respective owner

Related posts about linux

Related posts about exec