I'm trying to organize a programming contest for signal processing; originally it was going to be in Python, but the question came up if I could expand allowable entries to C.
The type of programming needed for the entries is really pretty limited:
- no stdin/stdout needed
- contestants can declare 1 struct containing state variables
- entries can declare functions
- I will create my own trusted C code for a test harness that calls into the contestants' entries
So I am wondering: is it possible to declare a particular C file as "safe" by parsing, if there are severe restrictions on the type of calculations allowed? The one thing I can't seem to figure out is how to easily prevent casting pointers or pointer arithmetic.
Entries would be of this form (more or less):
#include "contest.h"
// includes stdint.h and math.h and some other things
// no "#" signs after this line allowed
typedef struct MyState {
int16_t somevar;
int16_t anothervar;
...
} MyState_t;
void dosomething(MyState *pstate)
{
...
}
void dosomethingelse(MyState *pstate)
{
...
}
void calculate_timestep(MyState *pstate, ContestResults *presults)
{
...
}
I've read some of the sandboxing questions (this and this) and it looks a bit difficult to find a way to sandbox one part of C code but allow other trusted parts of C code. So I'm hoping that parsing may be able to help "bless" C code that meets certain constraints.
Any advice? I don't really care if it gets stuck in an infinite loop (I can kill it if the time takes too long) but I do want to prevent OS access or unwanted memory access.