views:

680

answers:

12
+10  Q: 

Self Modifying Code

  • I am recently thinking about writing self-modifying programs, I think it may be powerful and fun... So I am currently looking for a language that allow modifying program own code easily..

  • I read about C# (as a way around) and the ability to compile -and execute- code in runtime, but that is too hurting..

  • I am also thinking about assembly... it is easier there to change running code but it is not very powerful (very raw)...

Can you suggest me a powerful language -or a feature- that support modifying code in runtime..?

Hints
That what I mean by modifying code in runtime:

  Start:
  a=10,b=20,c=0;
  label1: c=a+b;
  ....
  label1= c=a*b;
  goto label1;

and may be building a list of instructions:

  code1.add(c=a+b);
  code1.add(c=c*(c-1));
  code1. execute();

Thanks!

+6  A: 

Malbolge would be a good place to start. Every instruction is self-modifying, and it's a lot of fun(*) to play with.

(*) Disclaimer: May not actually be fun.

David
The programming language where it took a few years for a person to write hello world... Oh wait, the hello world program was generated by a computer. Have fun lulz.
ItzWarty
A: 

Have you looked at Java ? Java 6 has a compiler API, so you can write code and compile it within the Java VM.

Brian Agnew
+4  A: 

Personally, I find it quite strange that you find assembly easier to handle than C#. I find it even stranger that you think that assembly isn't as powerful: you can't get any more powerful than raw machine language. Anyway, to each his/her own.

C# has great reflection services, but if you have an aversion to that.. If you're really comfortable with C or C++, you could always write a program that writes C/C++ and issues it to a compiler. This would only be viable if your solution doesn't require a quick self-rewriting turn-around time (on the order of tens of seconds or more).

Javascript and Python both support reflection as well. If you're thinking of learning a new, fun programming language that's powerful but not massively technically demanding, I'd suggest Python.

Reinderien
I see nothing strange here. Assembly is about as simple as you can get, while C# is huge and complex -- and I say this as someone who's been writing C# for a couple years! As for power, I think in programming it usually means something like "expressive ability" or "abstraction-building ability" (obviously the literal meaning of "power" is useless here!), and assembly language is exceptionally poor at that.
Ken
@Reinderien: I mean in terms of changing existing code, assembly is easier than C#... Also in terms of readability and writability, assembly is less powerful...
Betamoo
I think asm is much easier than C# or Java. It is very straightforward and uncomplicated.
Paul Nathan
I agree that C# is huge and complex. That being said, compare the number of lines of code needed to open a file in C# versus assembly. For modern programming tasks, assembly is simply not an option. For sub-sub-subtasks that require heavy optimization, perhaps.As for power, I essentially meant the number of things that you're allowed to directly manipulate. This isn't always helpful, however; it's like being given a pile of carbon and being told to build a dog.Assembly can get very tricky to modify on-the-fly - you essentially have to act as a compiler. You need to do all the addressing.
Reinderien
@Ken @Reinderien @Paul: Assembly is harder than C#. **Period**. As hard as _effective_ C#? Maybe not. **Effective** assembly is **certainly** more hard than effective C#, though. You have to master your architecture, take into account increased cyclomatic complexity, caching, pipelining, interrupts, and possibly completely redesign your entire algorithm when the requirements change in order to make it as efficient as possible (to save registers, align some data, fix some ILP, etc), and If at hardware level: control the CPU (MSRs, TLB, Page Tables, I/O, control registers, etc).
Longpoke
@Longpoke: How is that different from mastering the C# semantics and the *gigantic* library packages and their different versions?
Paul Nathan
@Paul: Show me an assembly IDE with code completion and embedded documentation. The difference is that to make an effective C# application, you need to learn a very small subset of very well-documented libraries with oodles of examples. To make an effective assembly application, you need to drink from the firehose and understand the architecture from the bottom up, arcane abbreviations, electrical engineering issues and all.
Reinderien
@Reinderien: Honestly, I've used C# and assembly and I don't see a significant difference. I can get the job done in either.
Paul Nathan
Longpoke: For a given task, an assembly program to do the same thing will be more complex than a C# program to do that task. But learning to write assembly is far simpler than learning to write C#. (Saying "Period" in bold text doesn't make your argument any stronger.) Most of the things you mention (caching, pipelining, etc.) can be ignored in assembly, or taken into account in C#, too.
Ken
@Ken: Why do you need to use assembly if you aren't using the full bleeding edge of it (or writing a compiler)? If you aren't pushing it to the limits, the compiler is going to **murder** you.
Longpoke
+4  A: 

May I suggest Python, a nice very high-level dynamic language which has rich introspection included (and by e.g. usage of compile, eval or exec permits a form of self-modifying code). A very simple example based upon your question:

def label1(a,b,c):
    c=a+b
    return c

a,b,c=10,20,0    
print label1(a,b,c) # prints 30

newdef= \
"""
def label1(a,b,c):
    c=a*b
    return c
"""
exec(newdef,globals(),globals())

print label1(a,b,c) # prints 200

Note that in the code sample above c is only altered in the function scope.

ChristopheD
+2  A: 

Many languages allow you to eval code at runtime.

  • Lisp
  • Perl
  • Python
  • PHP
  • Ruby
Kevin Panko
+7  A: 

I highly recommend Lisp. Lisp data can be read and exec'd as code. Lisp code can be written out as data.

It is considered one of the canonical self-modifiable languages.

Example list(data):

'(+ 1 2 3) 

or, calling the data as code

(eval '(+ 1 2 3)) 

runs the + function.

You can also go in and edit the members of the lists on the fly.

edit:

I wrote a program to dynamically generate a program and evaluate it on the fly, then report to me how it did compared to a baseline(div by 0 was the usual report, ha).

Paul Nathan
+5  A: 

Common Lisp was designed with this sort of thing in mind. You could also try Smalltalk, where using reflection to modify running code is not unknown.

In both of these languages you are likely to be replacing an entire function or an entire method, not a single line of code. Smalltalk methods tend to be more fine-grained than Lisp functions, so that may be a good place to begin.

Norman Ramsey
+1  A: 

In high-level languages where u compile and execute code at run-time, it is not really self-modifying code, but dynamic class loading. Using inheritance principles, you can replace a class Factory and change application behavior at run-time.

Only in assembly language do you really have true self-modification, by writing directly to the code segment. But there is little practical usage for it. If you like a challenge, write a self-encrypting, maybe polymorphic virus. That would be fun.

Kenny
+3  A: 

Every answer so far is about reflection/runtime compilation, but in the comments you mentioned you're interested in actual self-modifying code.

There is no way to do this in C#, Java, or even (portably) in C - that is, you cannot modify the loaded in-memory binary using these languages.

In general, the only way to do this is with assembly, and it's highly processor-dependent. In fact, it's highly operating-system dependent as well: to protect against polymorphic viruses, most modern operating systems (including Windows XP+, Linux, and BSD) enforce W^X, meaning you have to go through some trouble to write polymorphic executables in those operating systems, for the ones that allow it at all.

It may be possible in some interpreted languages to have the program modify its own source-code while it's running. Perl, Python (see here), and every implementation of Javascript I know of do not allow this, though.

BlueRaja - Danny Pflughoeft
You have a good point. Depending on how you look at it, C# / other .NET flavours / Java use bytecode instead of proper assembly, but the issue stands.
Reinderien
I should add that, if you were to do it the slightly roundabout and hacky way of iteratively issuing source code to a compiler, running the binary, and then having that binary in turn modify the source code to re-issue it to the compiler... That would (effectively) have read-write access to the program's code, even though the read-write turnaround time is bad.
Reinderien
Although I agree with the gist of the sentiment, I would suggest the possibility (in Python for example: http://geofft.mit.edu/blog/sipb/73) of directly alterering generated/running bytecode (which conceptually then doesn't differ much from doing the same with assembly).
ChristopheD
+2  A: 
Jörg W Mittag
+1  A: 

You can do this in Maple (the computer algebra language). Unlike those many answers above which use compiled languages which only allow you to create and link in new code at run-time, here you can honest-to-goodness modify the code of a currently-running program. (Ruby and Lisp, as indicated by other answerers, also allow you to do this; probably Smalltalk too).

Actually, it used to be standard in Maple that most library functions were small stubs which would load their 'real' self from disk on first call, and then self-modify themselves to the loaded version. This is no longer the case as the library loading has been virtualized.

As others have indicated: you need an interpreted language with strong reflection and reification facilities to achieve this.

I have written an automated normalizer/simplifier for Maple code, which I proceeded to run on the whole library (including itself); and because I was not too careful in all of my code, the normalizer did modify itself. I also wrote a Partial Evaluator (recently accepted by SCP) called MapleMIX - available on sourceforge - but could not quite apply it fully to itself (that wasn't the design goal).

Jacques Carette
A: 

In Lua, you can "hook" existing code, which allows you to attach arbitrary code to function calls. It goes something like this:

local oldMyFunction = myFunction
myFunction = function(arg)
    if arg.blah then return oldMyFunction(arg) end
    else
        --do whatever
    end
end

You can also simply plow over functions, which sort of gives you self modifying code.

RCIX