Earlier quoted context omitted.
The real issue with self-modifying code is that it's basically a security bug waiting to happen, plus you have other complications like having to flush the instruction cache on architectures like ARM. Generally, the benefits are not worth it unless except for very specific cases.
I've seen points like these made before, but never really understood how. Can you give an example of self-modifying code becoming a security issue?
Writing a Self-Mutating x86_64 C Program (2013)
11–20 of 61 posts
Re: Writing a Self-Mutating x86_64 C Program (2013)
#12Are there any languages that could actually make use of self modifying code? Machines wouldn't have the same problems reasoning about it as humans would. Or is it a question of compilers not being good enough until processor tech made the optimisation not worth it?
> Are there any languages that could actually make use of self modifying code? Kernel livepatching (and by that ftrace) would come to mind.
Seems less error prone?
Re: Writing a Self-Mutating x86_64 C Program (2013)
#13Earlier quoted context omitted.
>Are there any languages that could actually make use of self modifying code? JIT languages already do this.
JIT languages don't use self-modifying code, they generate new code at runtime and execute that.
Dynamic linkers may do that, too, though glibc doesn't normally do it, as far as I know: it prefers to update a pointer to code: same result without needing memory that is both writable and executable and without having to invalidate the instruction cache.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#14Are there any languages that could actually make use of self modifying code? Machines wouldn't have the same problems reasoning about it as humans would. Or is it a question of compilers not being good enough until processor tech made the optimisation not worth it?
Really the main use of these techniques (and the article has a telltale in its use of the word "shellcode") is injecting code into a program that was not originally intended to be modifiable. Usually (although not always) across a security boundary.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#15Are there any languages that could actually make use of self modifying code? Machines wouldn't have the same problems reasoning about it as humans would. Or is it a question of compilers not being good enough until processor tech made the optimisation not worth it?
Self-modifying Lisp, perhaps? Do Turing tapes count? Really the main use of these techniques (and the article has a telltale in its use of the word "shellcode") is injecting code into a program that was not originally intended to be modifiable. Usually (although not always) across a security boundary.
By self modifying lisp, I take it you mean modifying the lisp data (/code? (/data?)) structure itself? On a lisp machine would that count as self modifying???
Re: Writing a Self-Mutating x86_64 C Program (2013)
#16Earlier quoted context omitted.
JIT languages don't use self-modifying code, they generate new code at runtime and execute that.
Sometimes they will also make small changes to code that was generated earlier, typically by changing a branch instruction to point somewhere else. Dynamic linkers may do that, too, though glibc doesn't normally do it, as far as I know: it prefers to update a pointer to code: same result without needing memory that is both writable and executable and without having to invalidate the instruction cache.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#17Are there any languages that could actually make use of self modifying code? Machines wouldn't have the same problems reasoning about it as humans would. Or is it a question of compilers not being good enough until processor tech made the optimisation not worth it?
Re: Writing a Self-Mutating x86_64 C Program (2013)
#18Are there any languages that could actually make use of self modifying code? Machines wouldn't have the same problems reasoning about it as humans would. Or is it a question of compilers not being good enough until processor tech made the optimisation not worth it?
Befunge:
Re: Writing a Self-Mutating x86_64 C Program (2013)
#19Earlier quoted context omitted.
Self-modifying Lisp, perhaps? Do Turing tapes count? Really the main use of these techniques (and the article has a telltale in its use of the word "shellcode") is injecting code into a program that was not originally intended to be modifiable. Usually (although not always) across a security boundary.
Turing tape, good point! Now where's my infinitely long piece of paper... By self modifying lisp, I take it you mean modifying the lisp data (/code? (/data?)) structure itself? On a lisp machine would that count as self modifying???
Re: Writing a Self-Mutating x86_64 C Program (2013)
#20Are there any languages that could actually make use of self modifying code? Machines wouldn't have the same problems reasoning about it as humans would. Or is it a question of compilers not being good enough until processor tech made the optimisation not worth it?
LISP macros are compile time self modifying code. So are many other macro systems.
I mentioned lisp machines in a prior answer, I don't know enough about them to say whether they would allow self modifying lisp code though.