Can someone explain to me how this works? How can a language actually change its own code? Is this possible in real time? A bit of pseudo-code would be helpful.
Self Modifying Code
Page 1 of 110 Replies - 1263 Views - Last Post: 18 April 2012 - 07:42 PM
Topic Sponsor:
Replies To: Self Modifying Code
#2
Re: Self Modifying Code
Posted 31 December 2011 - 07:58 PM
It is sort of language dependent, but yes it can work. Plugins, updates, and that sort of fun could be considered 'self modifying'. There's options in .NET that I've looked into as a plugin system. Drop a dll in a folder and have the code look to access it. It's pretty high level though.
#3
Re: Self Modifying Code
Posted 01 January 2012 - 02:24 AM
You can write self modifying code using assembly language. The code writes over itself to change the code and then loops back to run it. I am not that familiar with it though, so I can't post any example code.
However, doesn't life in general give you enough of a headache without having to mess with self modifying code??
However, doesn't life in general give you enough of a headache without having to mess with self modifying code??
#4
Re: Self Modifying Code
Posted 01 January 2012 - 04:25 AM
This would be pretty easy in interpreted languages. You just edit the script and call it again. For example, using PHP:
I can't see how this could be useful though; where whatever your goal is couldn't be accomplished with the proper, static logic. (Although that may well just be a failure of imagination on my part.)
<?php
$value = 0;
echo "Value is: $value." . PHP_EOL;
// Read in the code of this script and alter the $value
// value above, adding one to it.
$scriptPath = realpath($_SERVER['SCRIPT_FILENAME']);
if (file_exists($scriptPath))
{
$script_text = file_get_contents($scriptPath);
$searchPattern = '/\$value = (\d+);/i';
$replace = '\$value = ' . ($value + 1) . ';';
$script_text = preg_replace($searchPattern, $replace, $script_text);
file_put_contents($scriptPath, $script_text);
}
else
trigger_error("Script not found at '$scriptPath'", E_USER_ERROR);
// If a certain condition hasn't been met, recall this script
// to start it all over again.
if ($value < 50)
include $scriptPath;
?>
I can't see how this could be useful though; where whatever your goal is couldn't be accomplished with the proper, static logic. (Although that may well just be a failure of imagination on my part.)
#5
Re: Self Modifying Code
Posted 02 January 2012 - 02:44 AM
The concept of self modifying code is often used in malware, where a certain bit of software might change its own signature often as to avoid getting picked up by scanners. Rather useful for that kind of thing.
#6
Re: Self Modifying Code
Posted 11 April 2012 - 08:22 AM
What about this idea? Say you have your program compiled and in .exe format. and after lots of time and hard work you discern the binary "signature" of certian types of code. Would there be a way, given that your code knows the binary, that your code could add it to the .exe. Like inject it thus increasing the size of the code or maybe decreasing if you took out more than you replace or just took something out but that would probably mean youd have to fix the gaps. Then all the code has to do is call its added code like it was there all the time. This is just a hypothetical idea, i have no idea if this could even be done. I kinda have the feeling that it cant but itd be cool though.
#7
Re: Self Modifying Code
Posted 11 April 2012 - 09:16 AM
Vodkacannon, on 31 December 2011 - 09:16 PM, said:
Can someone explain to me how this works? How can a language actually change its own code? Is this possible in real time? A bit of pseudo-code would be helpful.
As Atli says, this is mostly done in interpreted languages. Look into Lisp, it's a common thing. For example, when you start up clisp and enter (+ 4 5), it'll evaluate that and return 9. What you've just done is to insert a piece of code into the running process. Conrad Barski gives some pretty deep examples of this in his Land Of Lisp book, in the form of games.
Not something I feel I can explain in brief, but explore Lisp and Scheme and you'll find that this is happening almost without you knowing it.
You also do some of this in javascript. For example, imagine an html document laid out in sections, each of which is a div, which are shown one at a time. You might use jQuery to take a div (initially empty) with the class "bottomNav" and insert a bit of navigation framework (left arrow, right arrow, top of page, all in a table). You might then use another bit of jQuery to give each left arrow a click function that shows the previous section - $(this).closest('.section').prev(). This page, on load, now modifies itself in two ways. First, it changes its actual content: in the file on disk, each section ended with an empty div <div class="bottomNav"></div>, but when it loads, that div is populated with a bunch of html that shows a table. Then, the arrow images within that new content (which didn't exist a second ago) gets a function associated with it, and that function shows the previous section, which it has looked up dynamically, on the fly.
This is not great code, mind you, but I've done it this way to show you something important. Notice when this all happens. This is not a function that, when you click on the left arrow, does a lookup and finds the right section - I could do that, but I didn't. This is a piece of code which goes to a particular section, which it knows about because it looked it up when the page loads and created a static function which shows that section.
This is probably a confusing example, and I apologize for that, but it might clarify things to consider a flaw in this code. This code is dynamic, but it's not very dynamic. If I were to insert a new section into this page in real time, it would break this navigation. Why? Because the arrows only know one place to go.
But wait - the arrows didn't exist when the page was created. How can this be?
The only way is: this page has created a static function - "show this particular section, please" - on page load, and it has modified itself to perform this function in response to a particular event.
Capiche?
#8
Re: Self Modifying Code
Posted 11 April 2012 - 09:44 AM
Quote
Can someone explain to me how this works? How can a language actually change its own code? Is this possible in real time? A bit of pseudo-code would be helpful.
What OS and CPU are you working on? Have you researched how to change the permissions of a virtual page?
#9
Re: Self Modifying Code
Posted 11 April 2012 - 09:53 AM
As an afterthought - "self-modifying code" generally doesn't refer to rewriting the source file on disk. That would be more a bit of stunt programming, or special-purpose stuff like virus writing. What you're talking about is usually rewriting the code in memory.
#10
Re: Self Modifying Code
Posted 14 April 2012 - 04:07 AM
jon.kiparsky already brought up Lisp.
The fact that in lisp self-modifying code is easy to write made Lisp the "assembly of AI", where self-modification has been used quite a lot as a means of machine learning.
Actually in lisp, the function can even change its name (implode/explode) and due to the call-by-name the possibilities are practically unlimited.
I think Common Lisp or Scheme can't do it all, but almost.
When it comes to assembly, there was "core wars".
Sometimes self-modifying code has also been used as protection against reverse engineering or illegal copying.
The fact that in lisp self-modifying code is easy to write made Lisp the "assembly of AI", where self-modification has been used quite a lot as a means of machine learning.
Actually in lisp, the function can even change its name (implode/explode) and due to the call-by-name the possibilities are practically unlimited.
I think Common Lisp or Scheme can't do it all, but almost.
When it comes to assembly, there was "core wars".
Sometimes self-modifying code has also been used as protection against reverse engineering or illegal copying.
This post has been edited by turboscrew: 14 April 2012 - 04:09 AM
#11
Re: Self Modifying Code
Posted 18 April 2012 - 07:42 PM
I see others already brought up LISP and AI so I won't rehash it. The best way to think about it is to think about how your mind works, or specfically the mind of a young child; what are they doing but modifying their source?
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote







|