EvilZone

Programming and Scripting => C - C++ => : Vacrin October 14, 2012, 02:30:04 AM

: Program that can write its own code in C++?
: Vacrin October 14, 2012, 02:30:04 AM
Hello everyone, im looking for code that i can make write its own kind of code if i tell it to. I know that it can be done, but i have yet to see the code for such a program.


Thank you
: Re: Program that can write its own code in C++?
: ca0s October 14, 2012, 01:21:35 PM
You mean a quine?
http://en.wikipedia.org/wiki/Quine_(computing)
: Re: Program that can write its own code in C++?
: Polyphony October 14, 2012, 07:21:03 PM
I know there are ways to do this with zip files too.
:
http://www.steike.com/code/useless/zip-file-quine/droste.zip for example

: Re: Program that can write its own code in C++?
: pllaybuoy October 14, 2012, 07:26:05 PM
Ugh ugh , provide more details please
: Re: Program that can write its own code in C++?
: Vacrin October 14, 2012, 08:11:39 PM
a quine just displays its own source code, im looking for a program or the code of a program that can write more code into itself while it is compiled or in runtime.

so basically if i have a program that does nothing but accept code, then i can give it the location of a piece of code i want to insert lets say it was cout << "hello"; well then now the code can say hello when it is activated, but i dont want to go into the source code to edit it, i would like to edit it while it is in runtime or something.

this is much different from a quine because a quine only displays its own code which can be done with cout << easily
: Re: Program that can write its own code in C++?
: flowjob October 14, 2012, 08:32:40 PM
AFAIK, you won't be able to do this with a AOT (ahead of time) compiled program (e.g. most C compilers), but it can be possible by using a JIT (just in time) compiler (like py2exe)
: Re: Program that can write its own code in C++?
: ca0s October 14, 2012, 09:12:51 PM
You can make a process execute dynamic code that it reads / receives from somewhere.
If you are using a compiled language you will have to send it pre-compiled (or include a compiler in your executable, which is not very efficient).
With scripting languages you can use eval().
: Re: Program that can write its own code in C++?
: flowjob October 14, 2012, 09:21:41 PM
If you are using a compiled language you will have to send it pre-compiled (or include a compiler in your executable, which is not very efficient).

JIT compiler have a compiler included wich compiles bytecode to machine code.That makes loading the application much more efficient than with AOT compiler, and as it compiles the code segment just before executing,executing it is faster than with a interpreter...

So a JIT compiler combine the advantages of a AOT compiler and a Interpreter
: Re: Program that can write its own code in C++?
: Vacrin October 14, 2012, 10:46:19 PM
So using one of these compilers code i write a program that can then access its own code through the compiler and then write and recompile itself all while it is still running or would it have to restart itself?
: Re: Program that can write its own code in C++?
: Live Wire October 14, 2012, 11:01:07 PM
I'm 90 percent sure that it would have to restart itself. Whenever I've tried something like this, I always end up writing a program to execute, and a program to modify. I'm actually interested in the answer now.
: Re: Program that can write its own code in C++?
: Vacrin October 15, 2012, 06:30:31 AM
i had that idea or a back and forth kinda idea, meaning i would make 2 programs, but they can edit each other so when one has to write a new code it adds it to the other and runs it and closes itself and then when the other one has to add a line it adds what was added to it and the line it has to add and then runs the other one and closes itself... dont know how productive it would be however
: Re: Program that can write its own code in C++?
: Satan911 October 15, 2012, 07:01:38 AM
What can be done is have a compiled exe (since we are in the C/C++ section) that does nothing until you append data (or information) to the existing binary file. See http://stackoverflow.com/questions/2266735/make-an-executable-at-runtime

You could also right code that invoke a compiler (GCC).

But having code compiled at runtime by a compiled executable is far from simple. It is way easier to do with scripting languages.
: Re: Program that can write its own code in C++?
: techb October 15, 2012, 01:15:29 PM
Yes, just use a scripting language. You can modify the source at runtime, the have itself restart or like mentioned something like eval() (not recommended though). Using eval() or similar method to execute code from an outside source is a big security hole.
: Re: Program that can write its own code in C++?
: Daemon October 15, 2012, 08:07:41 PM
isn't that just self-modifying code?

 http://en.m.wikipedia.org/wiki/Self-modifying_code
: Re: Program that can write its own code in C++?
: Vacrin October 16, 2012, 04:42:46 AM
Yes, that is what it is, i just said it differently xD
: Re: Program that can write its own code in C++?
: Satan911 October 16, 2012, 06:52:15 AM
Again, good luck doing that in C/C++.

However language running on a virtual machine like C# or Java can use reflection to modify their behavior at runtime. What exactly are you trying to achieve?
: Re: Program that can write its own code in C++?
: Vacrin October 16, 2012, 07:38:33 PM
i guess it would be to create a program that can create new classes while in runtime and either add predetermined statements or new ones that it comes up with.
: Re: Program that can write its own code in C++?
: Xires October 29, 2012, 09:30:28 PM
OOP was probably most popularized by this idea in conjunction with AI.  The basic thought is actually really simple and is often found with modular design for plugins & what-not.  Basically, create a program that manages a priority queue of function pointers that uses linked lists to mitigate conflicts.  Then you can create other code modules that, provided they use the API appropriately, can add code to be called by the manager.  Replacing the manager is possible by adding an updated manager to the priority queue that essentially just takes over control of the priority queue and doesn't return.  It may even be possible to release the old management and free the memory it used.  Updating or replacing the priority queue itself is done by adding a routine to copy memory to another location where the new PQ can manage it and adjusting the pointers that are used by the manager.  The old PQ gets freed/deleted/released and you eliminate memory leaks.

So, there ya go, how to create Skynet in a few easy steps.
: Re: Program that can write its own code in C++?
: EmilKXZ October 30, 2012, 04:58:07 AM
He perfectly refers to polymorphic code I'd say. There used to be a lot of examples in VX Heavens. Of course, no point saying this now (it's useless because the source I cited is down). However, polymorphic code for compiled languages does exist, it did these days with MS-DOS malware.

In fact, there used to be an example around (do your Google research) of Delphi code that "kinda" assembled itself and changed behaviour. Polymorphic code like the one you're asking, requires some sort of AI to determine what is going to be assembled "within itself". Basically your program requires to know how to code, and then what to do with that code he can code for itself. Polymorphic code also requires to be modular and to be prioritized somehow, a module for watching others, what to change and whatnot. Morphing stuff without an orchestrator can lead to undesirable results, yet... the order emerges from chaos.  ;)

If you aim for the Skynet-kind (soft/mal)ware, along with the community I can offer my help and opinion. Just start a thread, though (don't PM me for that, we EZ are a community, love & share). Of course, all of this for educational purposes.