Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running C in A Browser

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.

like image 488
user3059347 Avatar asked Sep 07 '14 17:09

user3059347


2 Answers

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.

like image 24
cjlallana Avatar answered Sep 20 '22 12:09

cjlallana


Old question but for those that land in here in 2018 it would be worth looking at Web Assembly.

like image 154
matthewdaniel Avatar answered Sep 22 '22 12:09

matthewdaniel