1

I'm looking at the possibility of building a system where when a query hits the server, we turn the query into c++ code, compile it as shared object and the run the code.

The time for compilation itself needs to be small for it to be worthwhile. My code can generate the corresponding c++ code but if I have to write it out on disk and then invoke gcc to get a .so file and then run it, it does not seem to be worth it.

Are there ways in which I can get a small snippet of code to compile and be ready as a share object fast (can have a significant start up time before the queries arrive). If such a tool has a permissive license thats a further plus.

Edit: I have a very restrictive query language that the users can use so the security threat is not relevant. My own code translates the query into c++ code. The answer mentioning clang is perfect.

Amit Prakash
  • 1,171
  • 10
  • 19
  • 3
    "a query hits the server, we turn the query into c++ code, compile it as shared object and the run the code" - I bet someone will get root on your server in less than an hour. –  Sep 24 '12 at 07:05
  • @H2CO3: If the input is constrained, the C++ generated is a finite subset of all possible C++ programs. With reasonable constraints, you *can* solve the halting problem etc. – MSalters Sep 24 '12 at 08:18
  • A bit annoyed by the closing of the question. I was asking for a specific tool/library that can compile code in run time and do it fast. Why is that a discussion! – Amit Prakash Sep 24 '12 at 15:43

2 Answers2

3

Running Clang in JIT mode should provide the speed you need, and example can be found here, safety on the other hand is something else...

Ch also had a JIT added, and seeing as its an interpreter, it might provided an easier sandboxed/controlled environment.

Necrolis
  • 25,836
  • 3
  • 63
  • 101
2

In addition to Necrolis answer, there's also specialized C++ parser Cling. Might come in handy.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135