Self-Modifying Programs?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 8010 Views - Last Post: 20 December 2010 - 11:43 AM

#1 chris772   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

Self-Modifying Programs?

Posted 24 January 2010 - 12:29 AM

We all know there is self-replicating code out there, but what about self-modifying code? Is it even possible? For example, you have a calculator programmed with the capability to search code and mathematical databases. If you tell it (though a specialized input window or something) to incorporate a new button for square roots, could it do it? The program could search through the databases, add a new function to its code and recompile itself. Possible or impossible? Discuss if you will.

Is This A Good Question/Topic? 0
  • +

Replies To: Self-Modifying Programs?

#2 abgorn   User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 31
  • View blog
  • Posts: 1,415
  • Joined: 05-June 08

Re: Self-Modifying Programs?

Posted 24 January 2010 - 09:21 AM

I remember writing a blog entry about this but I'm still not sure if it's completely possible because even the best compilers can't find the exact source of a bug not to mention knowing the objective of the code it has the bug in and editing the source to keep that goal. I'm sure you could write something for it for a simple application but the algorithm would probably be massive compared to the small amount of code used in the actual app and would just be overkill.
Was This Post Helpful? 0
  • +
  • -

#3 olibenu   User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: Self-Modifying Programs?

Posted 24 January 2010 - 11:03 AM

programs can't think, you'll have to tell them what to do.
adding a new function to its code and recompiling itself does not seem feasible.
you can define a set of parameters and mathematical operations (since this is what makes up functions) and chose which to use (based on searched code and mathematical databases)
Was This Post Helpful? 0
  • +
  • -

#4 chris772   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 190
  • Joined: 13-March 09

Re: Self-Modifying Programs?

Posted 24 January 2010 - 12:52 PM

So with current technology highly implausible or impossible? Thank you for your answers.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6703
  • View blog
  • Posts: 31,084
  • Joined: 10-May 07

Re: Self-Modifying Programs?

Posted 24 January 2010 - 12:59 PM

View Postolibenu, on 24 Jan, 2010 - 12:03 PM, said:

adding a new function to its code and recompiling itself does not seem feasible.databases)

You can't take a binary executable, add code & recompile it.

Viruses often infect exe's, or rename the original & then call it after being ran.
Was This Post Helpful? 0
  • +
  • -

#6 abgorn   User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 31
  • View blog
  • Posts: 1,415
  • Joined: 05-June 08

Re: Self-Modifying Programs?

Posted 24 January 2010 - 02:12 PM

But wouldn't it be hypothetically possible if it had its own copy of the source code and by executing some make file to compile that? Or if was an interpreted language (Ruby or Beanshell?) it wouldn't need compiling and it could just edit itself?

This post has been edited by abgorn: 24 January 2010 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Self-Modifying Programs?

Posted 24 January 2010 - 05:18 PM

Self Modifying Code is possible, I know that on the old Arm CPU it was possible.


It the assembly level it depends a lot on the architecture of the cpu Harvard / von Neumann.
von Neumann is a easier. You also to be aware of the pipeline depth.

It also possible to do it with some high lever language, in which you can treat code as data.
Was This Post Helpful? 0
  • +
  • -

#8 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 551
  • View blog
  • Posts: 1,432
  • Joined: 22-August 09

Re: Self-Modifying Programs?

Posted 26 January 2010 - 01:20 PM

Back in the good old days (some 30-35 years ago), there existed a COBOL compiler called XEKB from ICL. This compiler relied completely on the ability to modify code. The exit point in a PERFORM THRU statement set the exit point to be the address of the statement after the perform. This mechanism proved to be extremely lucrative for sad gits like myself, as we could set up exit points jump out of the perform statement, do other things then return back into the performed code, which would exit at the desired point. ( I hope you understood that! :blink: ).

Furthermore, ICL used an assembler called PLAN. It had a little known sister assembler which could compile a section of user code and then execute it. This was the way they built their GEORGE 2, 3 and 4 operating systems.

You can certainly write self modifying code in C++, but as the code segment is read and execute only, you have to use the VirtualProtect API call to set the code sement page to read and write, make the changes and then change back to read and execute.

This type of programming is not for the faint-hearted and requires knowledge of machine code. Nethertheless, it is most definitely possible to acheive if you were crazy enough to attempt it!

This post has been edited by Martyn.Rae: 26 January 2010 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#9 Shane Hudson   User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Self-Modifying Programs?

Posted 27 January 2010 - 12:43 PM

I think it depends on what exactly you want it to do.
For instance, you could have a input window where you type: enable sqrt or something. The program could then just do something like sqrt.enable();.

Of course that is not as advanced as you are talking about, and it would have to be programmed beforehand.
Was This Post Helpful? 0
  • +
  • -

#10 GWatt   User is offline

  • member icon

Reputation: 308
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Self-Modifying Programs?

Posted 27 January 2010 - 02:19 PM

I would also like to add that lisp and scheme (and probably other repl languages) have the ability to redefine the environment at runtime.
Was This Post Helpful? 0
  • +
  • -

#11 MentalFloss   User is online

  • .
  • member icon

Reputation: 595
  • View blog
  • Posts: 1,552
  • Joined: 02-September 09

Re: Self-Modifying Programs?

Posted 27 January 2010 - 09:02 PM

Quote

If you tell it (though a specialized input window or something) to incorporate a new button for square roots, could it do it?


In C#, there is the ability to compile code at run-time. It can take a text file and compile it for use even after you have compiled your application. If your application were sentient enough to write its own code after perhaps parsing a wikipedia page about square roots to identify the algorithm I suppose it's quite possible to do some pretty amazing things if one were so inclined.
Was This Post Helpful? 0
  • +
  • -

#12 GWatt   User is offline

  • member icon

Reputation: 308
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Self-Modifying Programs?

Posted 28 January 2010 - 12:40 AM

Well, hadn't thought about this before, but you could, in C, write a plugin interface, and then compile and add plugins on the fly.

[edit] used C as an example but there's nothing stopping you from doing it in any language

This post has been edited by GWatt: 28 January 2010 - 12:42 AM

Was This Post Helpful? 0
  • +
  • -

#13 Xantix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 10

Re: Self-Modifying Programs?

Posted 19 November 2010 - 03:16 PM

Yes.

A simple way: (A program which contains a string sourceCode as a literal.)
  • Program Version 1 begins
  • After a while it decides to make a change
  • Using String operations the program edits the sourceCode
  • Version 1 writes the new string to a new file
  • Version 1 starts a compiler to compile the new file
  • Version 1 starts Version 2 running
  • Version 1 closes
  • Version 2 renames/deletes Version 1 then renames itself
  • Repeat


(Provisions could be added for Version 1 to start Version 2 with state information which may or may not be able to be used by the new version).

The Difficult Way (changing the program while it runs)

Based on Visual Studio 2010's Edit and Continue, we let our program pause/break program execution, at which point certain legal editings can occur in the code (by legal consider the following rules by VS's Edit and Continue: Supported Code Changes (C#). Note this only works in a debugging mode since using an optimizing compiler, program statements would be rearranged and lead to problems. So using an in-order debug mode or interpreted language there are many changes which affect code only in a local area and if that area is not currently active it can be changed. The downside is we don't get fully optimized code.

As was mentioned above one could specify the interface and then change the part on the other side. For example a web browser reads http, html, etc. as a common interface and then polls the webpage for the current version. At any time you can replace the webpage with new info like a new e-mail message or a chat response, you can change the entire layout of the webpage, but since it is accessed through an interface its replacement is interchangeble. Similarly with a class library.

A calculator client program that polls the actual program server to see how many buttons to show, their positions, the names of the buttons, and what functions/events to call when clicked could easily add a square root or shift operation if the server gets replaced with a new version as in the simple way.

Implementing a self-teaching math program, an automatic Wikipedia biography page writer, or a homework solver just by having the program search the web, is a task left to the reader.

This post has been edited by Xantix: 19 November 2010 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#14 Xantix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-November 10

Re: Self-Modifying Programs?

Posted 19 November 2010 - 03:31 PM



The simple way in the previous post is how we humans do it

This is what happens if you visit your favorite online calculator, decide you want a new feature, and send an e-mail (common interface) to the webmaster. The webmaster sees the feature request, figures out how to implement it (in our case automated), then programs that in a local copy of the file, the new feature isn't implemented until he replaces the file on the server by uploading.


Was This Post Helpful? 0
  • +
  • -

#15 -shadow-   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Self-Modifying Programs?

Posted 19 November 2010 - 05:09 PM

sure its possible. its like any program update.
you have an update manager for the software which
downloads the program including whatever button
you want added and installs it. This can also be
done with plugins to add specific options.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2