self modifying code

Feb 17, 2013 at 2:07pm
I was wondering if it'd be possible to write self modifying code in C? I guess if you knew where the program is in memory it shouldn't be too hard to do, but outside of that... I was wondering if maybe anyone here knew of any good resources on this topic. Self modifying code in general, really. I'm not looking for a solution for meta-programming, like those provided in most modern day languages. It's a learning exercise more than anything.


edit: I am aware that modern OS tend to protect running processes from just this thing. I'll install an ancient build of linux on an emulator if I have to :P
Last edited on Feb 17, 2013 at 2:09pm
Feb 17, 2013 at 2:09pm
And that's how Skynet was born...
Feb 17, 2013 at 2:16pm
Well, do you know asm? Then all you need is http://download.intel.com/products/processor/manual/325383.pdf
I'm not sure what you could possibly want to achieve though...
Are you, instead, looking for advice on how to write your code so that it is easy to modify?
Feb 17, 2013 at 2:18pm
After searching round a bit, the best solution I have come up with is building a small VM, and have it parse a string that you can then mutate at run time.
Feb 17, 2013 at 2:28pm
@hamsterman

I'm rusty but I used to do a lot of work with z80 asm with my graphic calculator and SOME arm asm when I found my old Ipaq pda a few years ago.


The exercise I've set up for my self is to write a program that rather than using a for-loop, modifies it's self so that it prints 1 through 10.


Ultimately I'm trying to get a better grasp of what my software looks like under the hood (so to speak). My next step in this project will be make a function return to a different point on the execution stack than it was called from. Though, I think I might need to do that to achieve the self modifying program I described above anyways. I dunno, this is all pretty new to me.
Last edited on Feb 17, 2013 at 2:37pm
Feb 17, 2013 at 2:29pm
I wonder if you could simulate evolution at a very high speed?
Feb 17, 2013 at 2:32pm
I wonder if you could simulate evolution at a very high speed?

not trying to sound rude, but how does that relate to the thread?
Feb 17, 2013 at 2:52pm
@devonrevenge

Implementing a genetic algorithm as a self modifying program? Sounds fun :'D


@Script Coder
I think he was getting at doing what I described above.
Last edited on Feb 17, 2013 at 2:52pm
Feb 17, 2013 at 2:58pm
This used to be done a lot a long time ago, but sadly it's pretty shut down anymore. Ended up turning into a security risk.
Feb 17, 2013 at 3:28pm
This used to be done a lot a long time ago, but sadly it's pretty shut down anymore. Ended up turning into a security risk.


Hence most modern OS do their darnedest to prevent this from happening.

Part of the reason I want to do this is that I've found that the comp sci curriculum at ever liberal arts college I've looked into are dime-a-dozen web dev farms at best and over glorified Tech support programs at worst. As I cant afford/no longer have the grades to get into a good engineering/science school I'm learning a lot of things on my own. Not that I mind doing so, I'm starting to think I'll drop out and continue to be self taught before all is said and done. I've heard from a number of advisors that it is not uncommon for self taught applicants to be on par with MIT/Cal Tech/Berkly/etc grad applicants when selecting a new employee.
Feb 17, 2013 at 5:52pm

This used to be done a lot a long time ago, but sadly it's pretty shut down anymore. Ended up turning into a security risk.

Hence most modern OS do their darnedest to prevent this from happening.


Nope, you are wrong. They only do their darndest to prevent this from happening *unintentionally*. But nothing is shut down. See VirtualProtect in Windows or mprotect in Linux.
Last edited on Feb 17, 2013 at 5:52pm
Feb 17, 2013 at 10:33pm
O: The more you know
Feb 18, 2013 at 12:57am
And that's how Skynet was born...


That's the first thing I thought when I read the title of the thread.
Feb 18, 2013 at 3:32am
Nah, if anything google and IBM are working on skynet. Just not consciously. Or together.


Or maybe that project that the Eu Commission just OKd will become skynet.
Topic archived. No new replies allowed.