简体   繁体   English

我可以禁止某些功能崩溃程序吗?

[英]Can I prohibit certain functions from crashing a program?

I wish to make interactive code learning system, it allows users, (young programmers normally) to write contents of one function in c++ language, send it to server and there it will be compiled into dynamic library and called from main program. 我希望制作交互式代码学习系统,它允许用户(通常是年轻程序员)用c ++语言编写一个函数的内容,将其发送到服务器,然后将其编译成动态库并从主程序调用。

Program expects function to return correct answer depending on given parameters. 程序期望函数根据给定的参数返回正确的答案。

Of course, there will be some kids, that will cause errors like segmentation fault. 当然,会有一些孩子,会导致分段错误等错误。 (server is Linux powered). (服务器是Linux驱动的)。

So, can I make signal handler that would exit function? 那么,我可以创建退出函数的信号处理程序吗?

What I wish to accomplish: 我想要完成的事情:

for (int i = 0; i < PLAYER_NUM; i++) {
    snprintf(buf, sizeof(buf), "players/%s.so", player[i]);
    handle = dlopen(buf, RTLD_LAZY);
    add[i] = (int (*)(int, int))dlsym(handle, "sum");
} // that was simply loading of functions from libraries.

for (int x = 0; x < 10; x++)
    for (int i = 0; i < PLAYER_NUM; i++) {
        if(failed[i]) continue;
        ret = add[i](x, 5);

    if(sigfault_received() || res != (x + 5)) {
        failed[i] = true;
    }
}

Faulty code can cause all kinds of issues which might not be recoverable. 错误的代码可能导致各种可能无法恢复的问题。 So handling SIGSEGV won't really help. 所以处理SIGSEGV并没有真正帮助。

The solution is to run that code in a separate process and use IPC, pipes or sockets to communicate with the main process. 解决方案是在单独的进程中运行该代码,并使用IPC,管道或套接字与主进程通信。

Use a proper sandbox, and not one you built yourself. 使用合适的沙箱,而不是自己构建的沙箱。 You can't be expected to be as creative predicting mischief as 10 kids together. 你不能期望像10个孩子一样有创造力地预测恶作剧。 Eg system("rm -rf /") won't immediately segfault your program, but it certainly is undesirable. 例如, system("rm -rf /")不会立即对您的程序进行分段,但这肯定是不可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM