Dynamically introducing code into the application

B

Bala

Hi,

This is what i am finding out if it is possible. I am not sure but
would like to know..

I have a Client application that sends me a condition like this

(1 == 'A' && 2 == 'B') || (3 == 10) && (4 >= 200 && 4 <= 500)

I do not want to parse anything out of this condition but my server
which is already running when it gets this request just will prepend
an "if" word and i want this if condition to get executed.

if ((1 == 'A' && 2 == 'B') || (3 == 10) && (4 >= 200 && 4 <= 500))

In short, i want to introduce "if" conditions dynamically into the
executable and send the result set back to the client.

Is this possible in any programming language (C, C++ or JAVA)? The
server application will be multithreaded, so it should support that as
well.

Thanks,
Bala
 
B

Bala

* Bala:








Yes to all.

You just have to compile the code.

And what you have then is a system wide open to attack.


Oh.

By the way, what's the C++ question, again?

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

I cant recompile the code because the server application is running.
Something like i want to hook the code in.... As far as i
understand, compiler based programming languages need precompiled
code, but this piece of if block is going to come in runtime.. How
would i handle this situation?
 
P

Pascal J. Bourguignon

Bala said:
I cant recompile the code because the server application is running.
Something like i want to hook the code in.... As far as i
understand, compiler based programming languages need precompiled
code, but this piece of if block is going to come in runtime.. How
would i handle this situation?

Have a look at:
- interpreter pattern,
- GNU lightning,
- http://vvm.lip6.fr/projects_realizations/ccg/
 
G

gpderetta

Have a look at:
- interpreter pattern,
- GNU lightning,
-http://vvm.lip6.fr/projects_realizations/ccg/

and LLVM, or even invoking the compiler and running/dlopen-ing the
generated binary.

But probably an ad-hoc interpreter is the simplest and safer solution
in this case.
 
E

Erik Wikström

Please do not quote signatures.
I cant recompile the code because the server application is running.
Something like i want to hook the code in.... As far as i
understand, compiler based programming languages need precompiled
code, but this piece of if block is going to come in runtime.. How
would i handle this situation?

You compile it to a shared library/DLL and then load that from you
application. Or you use lisp (or a few other dynamic languages) which
can modify themselves.
 
B

Bala

Please do not quote signatures.


You compile it to a shared library/DLL and then load that from you
application. Or you use lisp (or a few other dynamic languages) which
can modify themselves.

Hi Erik,
I did think about the following approach.
1. If there is no Dynamic linking then i might generate a new source
file, compile it, run and get the results into the parent process
through IPC.

2. If there is Dynamic Linking, then create a new library/dll, load
and execute it and get the results in the parent application.

But the issue to be addressed here are
1. How costly would this be in terms of performance since the
application will be getting such different requests continously.
2. If 2 different Front Ends send me the same query, then i should be
able to recognize them and use the existing shared library instead of
recompiling it.

The other approach i was thinking of is to be able to use a third
party high performance regular expression parser and evaluater if at
all that is available to execute the query.
 
J

James Kanze

On Sep 1, 9:35 pm, Erik Wikström <[email protected]> wrote:

[...]
I did think about the following approach.
1. If there is no Dynamic linking then i might generate a new source
file, compile it, run and get the results into the parent process
through IPC.
2. If there is Dynamic Linking, then create a new library/dll, load
and execute it and get the results in the parent application.
But the issue to be addressed here are
1. How costly would this be in terms of performance since the
application will be getting such different requests continously.

How much time does it take to compile and link a new dynamic
object? On the systems I use, I'd count on something around a
second or two, but it obviously depends.
2. If 2 different Front Ends send me the same query, then i
should be able to recognize them and use the existing shared
library instead of recompiling it.

For what definition of "same query". If textual identity is
sufficient, then you can maintain a hash table with the compiled
queries.
The other approach i was thinking of is to be able to use a
third party high performance regular expression parser and
evaluater if at all that is available to execute the query.

If all you need to handle is regular expressions, that's
definitely the way to go. More generally, I'd probably prefer
some sort of embedded interpreter: many script languages (perl,
python, etc.) can easily be embedded, for example. About the
only time I'd use the compile/link/dlopen solution is if there
was a strict requirement that the dynamic code support all of
C++.
 
T

Thomas J. Gritzan

Bala wrote:
[Dynamically introducing code into the application]
I did think about the following approach.
1. If there is no Dynamic linking then i might generate a new source
file, compile it, run and get the results into the parent process
through IPC.

2. If there is Dynamic Linking, then create a new library/dll, load
and execute it and get the results in the parent application.

But the issue to be addressed here are
1. How costly would this be in terms of performance since the
application will be getting such different requests continously.
2. If 2 different Front Ends send me the same query, then i should be
able to recognize them and use the existing shared library instead of
recompiling it.

The other approach i was thinking of is to be able to use a third
party high performance regular expression parser and evaluater if at
all that is available to execute the query.

If all you need is a regular expression parser, then use that. There is
one in TR1, which is available as update for some compiler systems.

If you need a type of database entry filtering, think about using a true
database system (with SQL language). The querying is very optimized in
database systems.

If you want to execute some expressions with variables and function
defined by your application, you need a parser and an interpreter.
There's Lua ( http://www.lua.org/ ), which is a very light-weight
embeddable scripting language which is used in many computer games.
The API for Lua is in C, but AFAIK there are also some C++ bindings.
 
E

Erik Wikström

Please do not quota signatures.
Hi Erik,
I did think about the following approach.
1. If there is no Dynamic linking then i might generate a new source
file, compile it, run and get the results into the parent process
through IPC.

2. If there is Dynamic Linking, then create a new library/dll, load
and execute it and get the results in the parent application.

But the issue to be addressed here are
1. How costly would this be in terms of performance since the
application will be getting such different requests continously.
2. If 2 different Front Ends send me the same query, then i should be
able to recognize them and use the existing shared library instead of
recompiling it.

To be honest I was not really serious when I wrote that reply, either of
those solutions are bad in more ways than one.
The other approach i was thinking of is to be able to use a third
party high performance regular expression parser and evaluater if at
all that is available to execute the query.

If the expressions the user can enter is limited to a reasonable number
I would spend some time to write a parser/evaluator in C++ and use it to
evaluate the expressions. In "The C++ Programming Language" you can see
an example of a quite advanced calculator. If you understand that you
should be able to expand it to solve your expressions too.

If the expressions are of a more complex nature you should go the route
of using some external/embedded evaluator. An embedded language such as
lua or python might be a good idea, just remember to add some check so
you do not allow the users to run whatever they want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top