Advertisement

Is Self Optimizing Code Really Possible Now-A-Days ?

Started by October 28, 2014 06:53 AM
6 comments, last by Oolala 10 years ago

There was a discussion some time ago about self morphing code, and how it's imposable with modern OS security features.

Is it possible to "work around" this issue by having one program, write the code for another application ?

I tested this theory on my own. I used Java and Python to quickly prototype, however this is aimed mostly at lower level languages ...

The first experiment I wrote a simple Python program that wrote a Java application using simple system information. It than compiled and executed the application using Python's system commands interface.

In the second experiment I wrote a simple Java program that wrote another Java application based on basic system information. Using Java's Process Builder I auto compiled and executed the new program.

Can this approach be used to write more complex programs that are optimized to the user's specific computer system ???

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

The No-Execute bit prevents various forms of accidental and sometimes intentional tampering, but yes, you can modify executables as they run.

CAN it be used? Sure, if you feel the need to build stuff like that on the fly.

Are there better options? Probably. Much more common is to just include multiple sets of DLLs, each one built for a specific configuration and tested thoroughly. Then the application can load the appropriate library after a simple system compatibility check. It tended to be very common back in the DOS and early Windows days where memory and space were at a premium and each program needed to have different libraries for different hardware. The installer could just grab the correct batch of files, or a configuration program would point it all to the right audio library and graphics library for the system.

These days hardware is mostly homogeneous, abstracted, and also heavily virtualized. So you point to an abstract graphics interface or an abstract audio interface or an abstract virtual processor and threading interface, and the OS loads the right versions of the libraries necessary to work with the installed hardware.

Is there some specific piece of functionality you are trying to use?

Advertisement

It also depends what you mean by self-optimising. For example, various JIT compiled languages supposedly optimise for your actual hardware. From a completely different angle, in the past I needed to dynamically change the graphical quality of a game based upon the device performance (as I recall one of the devices lied about it's claimed capabilities and therefore seemed identical to another device). I was recording the average elapsed time every 10th frame and gradually adjusting settings accordingly if they fell outside a certain range.

Isn't there some version of Java which optimizes hotspots on the fly? I also think some PHP optimizer(s) did this a few years ago. This is optimizing applications after figuring out where their actual bottlenecks are. I'm not sure if this quilifies as self-optimizing though as it's rather some daemon which monitors and changes your program for you.

The point of JIT compilers (Java, .NET, ..) is pretty much to generate platform specific code on execution. Some of them will take it somewhat further and tweak the generated code for the specific CPU models etc while most will just generate code for the specific CPU architecture and/or OS.

It's in general very easy to add code to your program (ie load a plugin), it's more difficult and problematic to change/overwrite existing memory. Various DRM solutions do that though, they "change" (more like decrypt) their own machine code while running to make it difficult to debug and hack.

The No-Execute bit prevents various forms of accidental and sometimes intentional tampering, but yes, you can modify executables as they run.


Depending on platform. Some don't allow you to turn off the NX bit and others will trigger DRM errors if you do.

Sean Middleditch – Game Systems Engineer – Join my team!

It's certainly possible. I know that in Visual Studio you can modify code even as the executable is running and the executable is updated with the code (though, it's a bit limited -- you can't add/remove classes & methods, just change branching logic and values).

A low level assembly language can probably inject new binary code at run time as well, though that's probably just asking for trouble since all addresses get offset by the injection length and if you miss one address update, you crash or get funky behavior.

An interesting question to ponder: can you program an artificial intelligence which writes human readable code intelligently and compiles it to improve itself?
(science fiction speculation: Begin!)

Advertisement

It's certainly possible. I know that in Visual Studio you can modify code even as the executable is running and the executable is updated with the code (though, it's a bit limited -- you can't add/remove classes & methods, just change branching logic and values).

A low level assembly language can probably inject new binary code at run time as well, though that's probably just asking for trouble since all addresses get offset by the injection length and if you miss one address update, you crash or get funky behavior.

An interesting question to ponder: can you program an artificial intelligence which writes human readable code intelligently and compiles it to improve itself?
(science fiction speculation: Begin!)

It's certainly possible. I know that in Visual Studio you can modify code even as the executable is running and the executable is updated with the code (though, it's a bit limited -- you can't add/remove classes & methods, just change branching logic and values).

A low level assembly language can probably inject new binary code at run time as well, though that's probably just asking for trouble since all addresses get offset by the injection length and if you miss one address update, you crash or get funky behavior.

An interesting question to ponder: can you program an artificial intelligence which writes human readable code intelligently and compiles it to improve itself?
(science fiction speculation: Begin!)

Don't compilers do this anyway, I know gcc does?

Two projects worth taking a look at: SPIRAL, and FFTW. Not necessarily self-modifying code, but runtime-optimizing code that self-profiles and finds an optimal combination of pre-generated kernels to maximize performance.

Self-modifying code deserves to die [outside of dynamic compilation]. Self-optimizing and self-profiling code is fine.

edit: Urg, Getting links to work sucks.

This topic is closed to new replies.

Advertisement