I've spent days of research over the seemingly simple question: is it possible to run C code in a browser at all? Basically, I have a site set up in Appengine that needs to run some C code supplied by (a group of trusted) users and run it, and return the output of the code back to the user. I have two options from here: I either need to completely run the code in the browser, or find some way to have Python run this C code without any system calls.
I've seen mixed responses to my question. I've seen solutions like Emscripten, but that doesn't work because I need the LLVM code to be produced in the browser (I cannot run compilers in AppEngine.) I've tried various techniques, including scraping from the output page on codepad, but the output I will produce is so high that I cannot use services like codepad because they trim the output (my output will be ~20,000 lines of about 60 characters each, which is trimmed by codepad due to a timeout). My last resort is to make my own server that can serve my requests from my Appengine site, but that seems a little extreme.
The code supplied by my users will be very simple C. There are no I/O or system operations called by their code. Unfortunately, I probably cannot simply use a find/replace operation in their code to translate it to Javascript, because they may use structures like multidimensional arrays or maybe even classes.
I'm fine with limiting my users to one cross-platform browser, e.g. Chrome or Firefox. Can anyone help me find a solution to this question? I've been baffled for days.
I've spent days of research over the seemingly simple question: is it possible to run C code in a browser at all? Basically, I have a site set up in Appengine that needs to run some C code supplied by (a group of trusted) users and run it, and return the output of the code back to the user. I have two options from here: I either need to completely run the code in the browser, or find some way to have Python run this C code without any system calls.
I've seen mixed responses to my question. I've seen solutions like Emscripten, but that doesn't work because I need the LLVM code to be produced in the browser (I cannot run compilers in AppEngine.) I've tried various techniques, including scraping from the output page on codepad.org, but the output I will produce is so high that I cannot use services like codepad.org because they trim the output (my output will be ~20,000 lines of about 60 characters each, which is trimmed by codepad due to a timeout). My last resort is to make my own server that can serve my requests from my Appengine site, but that seems a little extreme.
The code supplied by my users will be very simple C. There are no I/O or system operations called by their code. Unfortunately, I probably cannot simply use a find/replace operation in their code to translate it to Javascript, because they may use structures like multidimensional arrays or maybe even classes.
I'm fine with limiting my users to one cross-platform browser, e.g. Chrome or Firefox. Can anyone help me find a solution to this question? I've been baffled for days.
Share Improve this question asked Sep 7, 2014 at 17:58 user3059347user3059347 5392 gold badges9 silver badges15 bronze badges 4- 2 Can you compile LLVM using Emscripten & run it? – SLaks Commented Sep 7, 2014 at 18:00
- Is it possible to compile C to LLVM purely using Javascript? I don't understand how to do such a thing. – user3059347 Commented Sep 7, 2014 at 18:01
- 3 Compile the LLVM compiler using Emscripten. – SLaks Commented Sep 7, 2014 at 18:05
- Have a look at Tiny CC: "Compile and execute C source directly". Maybe, with libtcc, it's easy to integrate into Python. – pmg Commented Sep 7, 2014 at 18:21
3 Answers
Reset to default 15Old question but for those that land in here in 2018 it would be worth looking at Web Assembly.
You may want to take a look at Google Native Client, which, as described, is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system, allowing web-based applications to run at near-native speeds. It also uses a code verifier to prevent use of unsafe instructions such as those that perform system calls. Native Client provides customized versions of the GNU toolchain, specifically GCC and binutils as well as LLVM.
Apart from the official link given, you can take a look at the Wikipedia article on Google NaCL which has some more useful info.
It's appallingly evil, but you may be able to compile the cint C/C++ interpreter with emscripten. That would give you a browser-only environment which can interpret (slowly) C or C++ programs.
Unfortunately, cint claims to support mixing of interpreted code with precompiled code. This means that it's going to want to do things which emscripten won't support, so you'll have to hack it.
If you're willing to restrict yourself to Chrome you may find it easier to compile cint via the NaCL plugin system, because that means you can use a real x86 toolchain. But this won't let you use a real compiler; NaCL doesn't allow the dynamic generation of machine code, so systems like tcc won't work. cint may be easier to port on this platform, though.