Discussion:
Self-modifying code
(too old to reply)
f***@gmail.com
2015-07-12 01:57:06 UTC
Permalink
Is it possible to write self-modifying code in Ada?
More in detail: is it possible compile some code at runtime and inject it into the running program, on the fly? The ability to remove that code after it is no longer needed would also be important, to prevent hogging up the system memory which may result.
Thank you

Serg
David Botton
2015-07-12 03:24:45 UTC
Permalink
Post by f***@gmail.com
More in detail: is it possible compile some code at runtime and inject it into the running program, on the fly? The ability to remove that code after it is no longer needed would also be important, to prevent hogging up the system memory which may result.
Sure. Spawn the compiler to create a shared dynamic library and load it, then unload it when done from memory would be the cleanest way to do it in most cases, aka Plug-ins.

David Botton
Paul Rubin
2015-07-12 04:38:33 UTC
Permalink
Post by f***@gmail.com
Is it possible to write self-modifying code in Ada?
More in detail: is it possible compile some code at runtime and inject
it into the running program, on the fly?
You could generate a dynamically linkable module, but this all seems
pretty un-Ada. What are you trying to do? Hot upgrade? JIT compiler
inside your application? Maybe other approaches are worth looking into.
David Botton
2015-07-12 05:09:35 UTC
Permalink
Post by Paul Rubin
You could generate a dynamically linkable module, but this all seems
pretty un-Ada.
I'm not sure the idea is about any particular language. I can think of many ways to make it work, but all depend on the environment the code will run in not really the language.

David Botton
Paul Rubin
2015-07-12 05:53:21 UTC
Permalink
Post by David Botton
Post by Paul Rubin
You could generate a dynamically linkable module, but this all seems
pretty un-Ada.
I'm not sure the idea is about any particular language. I can think of
many ways to make it work, but all depend on the environment the code
will run in not really the language.
Yeah, it's similar at a technical level, but I think philosophically out
of sync with Ada, because of the additional moving parts.
Georg Bauhaus
2015-07-12 09:10:07 UTC
Permalink
Post by Paul Rubin
Post by David Botton
Post by Paul Rubin
You could generate a dynamically linkable module, but this all seems
pretty un-Ada.
I'm not sure the idea is about any particular language. I can think of
many ways to make it work, but all depend on the environment the code
will run in not really the language.
Yeah, it's similar at a technical level, but I think philosophically out
of sync with Ada, because of the additional moving parts.
OTOH, since Ada has improved its "contractual features",
linking dynamically seems more predictable now than ever
if these features are being used. More so if the linked
part is written in Ada, too.
anon
2015-07-12 11:54:52 UTC
Permalink
Post by f***@gmail.com
Is it possible to write self-modifying code in Ada?
More in detail: is it possible compile some code at runtime and inject
it into the running program, on the fly? The ability to remove that
code after it is no longer needed would also be important, to prevent
hogging up the system memory which may result. Thank you
Serg
This question comes up once or twice a year, and with USA network cable
channel showing "Mr. Robot" this question is on every hackers mind. How
to hide code and build a "Rootkit" or "Virus" during run-time.

Ada was designed with a number of overriding concerns, One being
program reliability and maintenance. That means it was designed to
prevent "Erroneous Execution" such as self-modifying code, So the RM
by its definition of "Erroneous Execution" means that self-modifying
code is illegal in Ada.

So, except for shared (loaded at runtime) or dynamic (loaded on demand)
libraries, the answer is NO!

And, I hope that the maintainers of Ada never wants Ada to be associated
with "Rootkits" or "Viruses" type of code. So, the answer should always
be NO!


Note: Dynamic libraries are normally a complete sub-system of routines
not just a simple single function type plug-in.
David Botton
2015-07-12 15:40:31 UTC
Permalink
Post by anon
So, except for shared (loaded at runtime) or dynamic (loaded on demand)
libraries, the answer is NO!
So not true... I've done it and I am sure many others have here too. In the old days it was a much more common technique when space was tight. Almost any language can do it, and no language in and of itself can really prevent it.

Ada prevents you from shooting yourself in the foot by mistake, but anyone can do it on purpose.
Post by anon
Note: Dynamic libraries are normally a complete sub-system of routines
not just a simple single function type plug-in.
Not true, I'd almost say the majority of dynamic libraries I've used (outside basic std libs) are components. You would be surprised how often those dynalibs have no more than one function, say a factory to an IUknown interface etc.

Also tons of plugin APIs, etc. I've used in the past with one function access, etc.

Dynamic libs are a very solid way to swap code around at runtime.

David Botton
Jacob Sparre Andersen
2015-07-27 11:29:37 UTC
Permalink
Post by f***@gmail.com
More in detail: is it possible compile some code at runtime and inject
it into the running program, on the fly? The ability to remove that
code after it is no longer needed would also be important, to prevent
hogging up the system memory which may result.
Yes. AdaCore has a detailed example of how you can do it with GNAT in
their Ada Gems series.

Greetings,

Jacob
--
»Great minds discuss ideas,
Average minds discuss events,
Small minds discuss people.«
Loading...