gpt4 book ai didi

c++ - exec 如何改变执行程序的行为

转载 作者:IT王子 更新时间:2023-10-29 00:34:19 26 4
gpt4 key购买 nike

我正试图追踪一个非常奇怪的崩溃。奇怪的是有人发现了我无法解释的解决方法。

解决方法是这个我称之为“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;
}

如您所见,该程序所做的只是使用 execvp 将自身替换为不同的程序。

直接从命令行调用程序崩溃:

/path/to/prog args  # this crashes

但是当它通过我的 runner shim 间接调用时工作正常:

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

在我的一生中,我可以弄清楚额外的 exec 如何改变正在运行的程序的行为(如您所见,该程序不会改变环境)。

崩溃的一些背景。崩溃本身发生在 C++ 运行时。具体来说,当程序执行 throw 时,崩溃版本错误地认为没有匹配的 catch(尽管有)并调用 terminate。当我通过 runner 调用程序时,异常被正确捕获。

我的问题是,为什么额外的 exec 会改变被执行程序的行为?

最佳答案

运行器加载的 .so 文件可能导致运行器正常工作。尝试 ldd'ing 每个二进制文件,看看是否有任何库正在加载不同的版本/位置。

关于c++ - exec 如何改变执行程序的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com