Questions tagged [self-modifying]

Self-modifying code is a piece of software which achieves its goal by rewriting itself as it goes along. Use this tag for questions related to self-modifying code, such as the writing and running of such codes in different languages, use cases and its detection.

Self-modifying code is a piece of software which achieves its goal by rewriting itself as it goes along. Use this tag for questions related to self-modifying code, such as the writing and running of such codes in different languages, use cases and its detection.

156 questions
120
votes
16 answers

Are there any smart cases of runtime code modification?

Can you think of any legitimate (smart) uses for runtime code modification (program modifying it's own code at runtime)? Modern operating systems seem to frown upon programs that do this since this technique has been used by viruses to avoid…
59
votes
7 answers

How to write self-modifying code in x86 assembly

I'm looking at writing a JIT compiler for a hobby virtual machine I've been working on recently. I know a bit of assembly, (I'm mainly a C programmer. I can read most assembly with reference for opcodes I don't understand, and write some simple…
jakogut
  • 4,409
  • 6
  • 29
  • 41
53
votes
15 answers

What are the uses of self modifying code?

Is there any real use for self modifying code? I know that they can be used to build worms/viruses, but I was wondering whether there is some good reason that a programmer may have to use self modifying code. Any ideas? Hypothetical situations are…
Niyaz
  • 53,943
  • 55
  • 151
  • 182
40
votes
4 answers

Observing stale instruction fetching on x86 with self-modifying code

I've been told and have read from Intel's manuals that it is possible to write instructions to memory, but the instruction prefetch queue has already fetched the stale instructions and will execute those old instructions. I have been unsuccessful in…
Chris
  • 2,786
  • 1
  • 28
  • 31
37
votes
11 answers

How to write self modifying code in C?

I want to write a piece of code that changes itself continuously, even if the change is insignificant. For example maybe something like for i in 1 to 100, do begin x := 200 for j in 200 downto 1, do begin do something …
AnkurVj
  • 7,958
  • 10
  • 43
  • 55
35
votes
14 answers

Programming language for 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 allows modifying a program's own code easily. I read about C# (as a way around) and the ability to…
Betamoo
  • 14,964
  • 25
  • 75
  • 109
32
votes
4 answers

Homoiconic and "unrestricted" self modifying code + Is lisp really self modifying?

I will be forward in admiting that my knowledge of Lisp is extremely minimal. However I am extremely interested in the language and plan to begin seriously learning it in the near future. My understanding of these issues is no doubt flawed, so if I…
TheIronKnuckle
  • 7,224
  • 4
  • 33
  • 56
27
votes
9 answers

What is a code cave, and is there any legitimate use for one?

I encountered this word for the first time in the StackOverflow question "C# Theoretical: Write a JMP to a codecave in asm." I see that according to Wiktionary, a code cave is: an unused block of memory that someone, typically a software cracker,…
Eddie
  • 53,828
  • 22
  • 125
  • 145
27
votes
9 answers

Can a C program modify its executable file?

I had a little too much time on my hands and started wondering if I could write a self-modifying program. To that end, I wrote a "Hello World" in C, then used a hex editor to find the location of the "Hello World" string in the compiled executable.…
Joel
  • 1,437
  • 2
  • 18
  • 28
26
votes
2 answers

Writing a JIT compiler in assembly

I've written a virtual machine in C which has decent performance for a non-JIT VM, but I want to learn something new, and improve performance. My current implementation simply uses a switch to translate from VM bytecode to instructions, which is…
jakogut
  • 4,409
  • 6
  • 29
  • 41
25
votes
5 answers

How is x86 instruction cache synchronized?

I like examples, so I wrote a bit of self-modifying code in c... #include #include // linux int main(void) { unsigned char *c = mmap(NULL, 7, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE| …
Will
  • 2,014
  • 2
  • 19
  • 42
14
votes
3 answers

Self modifying code always segmentation faults on Linux

i found an article about self modifying code and tried to do some examples, but i get always segmentation faults. As fas as i can understand, there is a violation in memory permissions. The code segment is (r)ead/e(x)ecute and so the attempt of…
Fotinopoulos Giorgos
  • 1,027
  • 3
  • 17
  • 36
14
votes
4 answers

How can I write self-modifying code that runs efficiently on modern x64 processors?

I'm trying to speed up a variable-bitwidth integer compression scheme and I'm interested in generating and executing assembly code on-the-fly. Currently a lot of time is spent on mispredicted indirect branches, and generating code based on the…
Nathan Kurz
  • 1,649
  • 1
  • 14
  • 28
14
votes
8 answers

Is there any self-improving compiler around?

I am not aware of any self-improving compiler, but then again I am not much of a compiler-guy. Is there ANY self-improving compiler out there? Please note that I am talking about a compiler that improves itself - not a compiler that improves the…
12
votes
3 answers

Dynamic relocation of code section

Just out of curiosity I wonder if it is possible to relocate a piece of code during the execution of a program. For instance, I have a function and this function should be replaced in memory each time after it has been executed. One idea that came…
Phil
  • 121
  • 1
  • 3
1
2 3
10 11