Code mutation experiments

As a little hobby I’ve been really into code mutation and getting computers to write their own code, well at least that’s the goal anyway. What really interests me is if you can give a computer a really small amount of code but yet get it to construct something itself. I think this is pretty much how AI will work. The problem I see is the halting problem, in order for a computer to understand code it needs to know what it’s output should be but when the output has many paths and multiple outputs depending on inputs then it is suddenly very complex and even impossible to know the correct output. Let’s ignore this problem for now though, if we assume that a program knows what it’s output should be and it’s required operation what can we do?

Minifying

Minifying can be done by simply checking the syntax and making sure the output is the same when removing a character. Then continue until you cannot remove any more characters. The demo is in sorta slow motion since I use a timeout of 500 milliseconds but it helps to see how it works.

Updated…
I’ve updated the code to be aware of global variable leakage works currently only work on IE and Firefox. It will remove multi-line comments now (sometimes completely) and leave var statements because removing them will produce globals.

Mutate minify demo

The code only takes randomized character positions and removes them, you could of course just traverse from top to bottom but this is only a experiment and the code is of course a little rough.

Obfuscation

If we changed the parameters a little and instead of removing characters we replace them then we can obfuscate the code by constantly changing it’s source but retain the same output. This works by choosing two random points in the code and random characters. What was interesting here was the code “discovered” that it could create single line comments, strings and replace variables. Sometimes copying the code and executing somewhere else wouldn’t return the same output either because as it mutated it would rely on existing global variables.

Mutate obfuscate demo

Mutation

Finally we have the mutation demo which takes some input and modifies it and appends it with valid syntax. It checks to see if the result was the same as last time and the result is positive. At the moment this is a bit dumb since the code generated is random and of no value, I’m looking into genetic code to see if I can make it return more meaningful and interesting output and if it can have a basic understanding on how the source works.

Mutate demo

Update…

Mutating a function value

Can we use randomized values to change a function and return a different value? Yeah 🙂 This one took a bit of thinking about. I have a few characters that are valid JavaScript. I build a loop through some maximum mutations, pick a random position. Check the syntax and bail if invalid if not I then test the output for the expected result. The idea is the function returns something like “2” but you want it to return “4” and the mutations will continue until it finds the expected value.

Mutate function value demo

2 Responses to “Code mutation experiments”

  1. Jean Pascal Pereira writes:

    This is great!

  2. Jean Pascal Pereira writes:

    I’ve built something similar in Perl, maybe give it a try: http://goo.gl/6QxvD

    There are also some interesting techniques for adding randomness to the generated code by allowing logical exceptions.