News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Run Time Code Modification ?

Started by cman, October 01, 2007, 06:25:44 PM

Previous topic - Next topic

cman

I was reading about logic-based programming languages like Lisp , Prolog , ect. and noticed that in one ( Lisp ) "data structures can executed as programs and programs can be modified as data ".  This got me to wondering if an assembly language program can modify its own code segment during runtime as long as it does not change the actual length of the code segment ( just change some hex code here and there but don't add or delete lines ). Is this possible ? Just curious. Thanks.... :bg

Jimg

Sure, search the forum for "self modifying code".

And be prepared for an onslaught of people telling you why it's not a good idea.

Tedd

Yep, quite possible. Although you have to be prepared to deal with the protection issues (executable code shouldn't be writable - think viruses; but it's possible to modify the page's access and then set it back again.) Also, it can mean a performance hit since it invalidates the cpu's internal cache.
To simulate what other languages do, you can allocate a block of memory and write the code into that to get a code 'object' and then execute it (use VirtualAlloc and remember to set execute access.)
No snowflake in an avalanche feels responsible.

cman

Thanks for the information!  :U I was wondering what one would do with a program that can modify its own instructions ( I here this capability is popular in AI , but I don't know too much about that ). I'm guessing some sort of adaptive code could be written with the feature ( a program could change in response to user need ?? ). Thanks again. :bg

Shell

The SMC feature is also a well known aproach for protecting executables i.e. PE Cypters, especially the polymorphic variety. Sadly though, it is more widely used by Malware.

Back in the 16-bit DOS days, SMC was also widely used for saving space due to the 640K memory constraint. People even went as far as constructing Overlay managers. My deepest exploration on this topic back then was creating a bloated TSR stub (>1.5K  :green ) that would load various utils (e.g. a programmer's calc, a clock, etc...) on demand ... meaning that those various MODULES would only be loaded in memory when needed and thrown back to the overlay heap (on disk) when no longer required. It even utilized a timer to cycle through ACTIVE MODULES if needed. If only I could do those things again in windows  :boohoo: Sadly, the information required is not easily acquired here but can be found scattered all over the net.

If you want to explore this further then google is your friend  :wink Just be wary that most sites that cover these topics in detail have a somewhat questionable disposition. I hope I've obfuscated my response enough to not garner any unwanted attention (not very likely but one can always hope).

Good Luck