2

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.

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).

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;
    }
}
MSalters
  • 173,980
  • 10
  • 155
  • 350
user1308345
  • 1,032
  • 1
  • 8
  • 14
  • 3
    About all you can do is live with the fact that it's going to crash, and figure out ways to 1) ensure it doesn't damage the rest of the system when it does, and 2) recover quickly and easily when (not if) it happens. My immediate reaction would be to run their code in a virtual machine. – Jerry Coffin Apr 30 '12 at 15:27
  • Added "sandbox" tag because that's the common name for such protections. – MSalters Apr 30 '12 at 20:48

2 Answers2

10

Faulty code can cause all kinds of issues which might not be recoverable. So handling SIGSEGV won't really help.

The solution is to run that code in a separate process and use IPC, pipes or sockets to communicate with the main process.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Young kids learning how to program shouldn't have to worry about an advanced topic like IPC. What's a good way to cleanly hide the IPC? – TJD Apr 30 '12 at 16:52
  • Your program most likely has some kind of API they'll use - make those functions communicate with your main program and either tell them not to care about the implementation of those functions or just give them object files they can link. – ThiefMaster Apr 30 '12 at 17:36
0

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. E.g. system("rm -rf /") won't immediately segfault your program, but it certainly is undesirable.

MSalters
  • 173,980
  • 10
  • 155
  • 350