1

For example, two source codes are written in a way that both of them return a number and print out a random string(for example:"I have died number times") that number times. The important point in here is that the number that is returned from one program is the number that is added to the other program's string. One thing that should also be considered is that both programs hypothetically intervene others source code and then runs it forever. Which means the return value of the one program is actually the main factor to manipulate the other program.

Question: Is it doable,(for example in java) if so can a program alter its own source code?

tshepang
  • 12,111
  • 21
  • 91
  • 136
aliatlii
  • 45
  • 7
  • It'd be much easier not to modify source code, but to get the data that might change from another source that can then be changed. This should essentially never be necessary. – Louis Wasserman Jun 18 '14 at 18:52
  • 1
    "Source code" doesn't exist at runtime. Bytecode/machine code manipulation does exist though. More advanced malware tend to do that kind of thing. Not sure if it's the same thing you're looking for though. – awksp Jun 18 '14 at 18:54
  • @user3580294 What should I understand from that? A program cannot chang its own source code maybe.. – aliatlii Jun 18 '14 at 18:57
  • 2
    Programs (usually) can't alter their own source code because (aside from interpreted runtimes) it isn't the source code that is executed, which means the source code usually isn't available to alter in the first place. In the case of Java, it is *bytecode* that is executed, and for other languages it is (usually) *machine code* that is executed. Mostly a nitpick. @NickB's answer covers more. – awksp Jun 18 '14 at 18:59

3 Answers3

5

Yes, it's do-able in theory. Self-modifying code is an entire branch of computer science.

It's rarely a good idea in the real world though, and if you are going to do it, Java (or other compiled languages) are probably not the best choice. Javascript, PHP¸ Python (e.g. What does Python's eval() do? ), Ruby, Groovy etc all offer ways to run arbitrary code, which itself could generate other arbitrary code... but it's nightmarish to debug, secure, test and prove that it's doing the right thing.

It's usually better to design your program carefully, and as another poster said, work with data structures, not with native source code.

Community
  • 1
  • 1
declension
  • 4,110
  • 22
  • 25
1

Yes, it is possible and it is not so uncommon.

Libraries like Javassist can be used to manipulate code (byte code) during run-time. This is used e.g. for logging frameworks, profilers or stuff like Hibernate. All of those frameworks could inject new code into your existing code of your application, e.g. to monitor it.

I wrote a load balancer once, which could be started with an existing application. The load balancer was then taking the already compiled code of the application (which could be any JDBC application) and injected some control statements. All during run-time.

Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
-2

Instead of editing source code files (which would be an unwise path to go down, because you could accidentally mess up your source code), you could use a settings file, such as "number.ini". In this file, you can write a number, then the program would read this file and parse the number, print the string X amount of times (depending on what the number was), and then write a new number to this file.

This would eliminate the possibility of your source code being destroyed accidentally, and would remove any kind of "file in use" conflicts that may arise.

admdrew
  • 3,790
  • 4
  • 27
  • 39
Mike Elofson
  • 2,017
  • 1
  • 10
  • 16