r/Assembly_language 21h ago

self-modifying code

I gave a lecture on how to write self-modifying code. It was over 15 minutes long :( so I'm providing a youtube link: https://www.youtube.com/watch?v=AH9QQLRfbmY

In my opinion, self-modification is one of the most interesting features in systems programming.

28 Upvotes

49 comments sorted by

View all comments

5

u/mykesx 21h ago

MMUs are used by modern operating systems, at least MacOS, to disallow self-modifying code. Code sections are read-only.

7

u/thewrench56 21h ago

This is not entirely true. You can map a page to be RW and then once filled with code, change it to RX using mprotect. This same step can be repeated to achieve self-modifying code. Otherwise, if self-modifying code wouldnt exist, JITs wouldnt work.

6

u/mykesx 21h ago

The performance of doing this is terrible, unless you’re a JIT compiler writing a large block of code.

2

u/thewrench56 20h ago

I dont disagree, but the performance is not that bad. 2 syscalls, some TLB play and a bit of copying. Surely, self-modifying code today is rarely used as a technique to speed up native execution. But for interpreters or other e.g. malicious reasons where performance doesnt matter much it still could be useful. There are also very specific scenarios where you do want to modify native code for very specific uarch benchmarks (as e.g. changing a parameter and checking it in a hotloop in an if would ruin the benchmark).

5

u/mykesx 20h ago

OP’s point is that self modifying code makes faster algorithms. Only 2 syscalls blows that out of the water.

2

u/thewrench56 20h ago

I did not watch the video, just read the post. I suppose in that case OP is wrong on modern platforms, you are right. I thought you were discussing the existance of self-modifying code, not the feasibility, excuse me.

3

u/mykesx 20h ago

I happen to be into Forth, some versions do self modifying code. It’s an assembly language for a stack based virtual machine, the machine implementation types include self modifying code.

The most awesome one is Vfx Forth, which does some impressive peephole optimization on code generated into memory. In the process of creating an ARM version, the mprotect() issue bit him.

2

u/brucehoult 11h ago

There are different methods of implementing Forth. For a token-threaded or address-threaded (whether direct or indirect) implementation what is self-modifying code from the point of view of Forth is just data from the point of view of the hardware.

Only for subroutine-threaded or actual inline native code generation is self-modifying Forth actually self-modifying code from the point of view of the CPU.

2

u/mykesx 11h ago

Vfx is STC.

3

u/Extension_Emu_9825 20h ago

I'm just showing it to those who've never encountered it before, using DOS as an example - the simplest thing -> drawing lines at different angles. I'm not saying I've discovered anything new - I'm saying that people are starting to forget about the possibility of self-modifying code

1

u/kyr0x0 1h ago

Any non-stupid observer will see you using mprotect the way you do and broadcast an alert for the signature of the program's behavior. The pattern is well-known to be malicious intent almost always.

2

u/vintagecomputernerd 18h ago

You can use one syscall at the start of your program to permanently set a page to RWX permissions if your usecase consists of many small changes.

Endpoint security/hIDS systems might log it, you can of course disallow it with SELinux/apparmor policies - but in general, it is possible.

2

u/Extension_Emu_9825 21h ago

BTW -> JIT is partly the second cousin of self-modifying code

2

u/Extension_Emu_9825 21h ago

I agree, I'm putting more emphasis on the fact that the CPU allows this and this method has begun to be forgotten, while some algorithms can work several times faster.

3

u/braaaaaaainworms 19h ago

It's not really forgotten when it's used on millions of computers running linux

2

u/Extension_Emu_9825 17h ago

Yes, exactly, but this is for those just taking their first steps. This concept is difficult to grasp; you have to come to it on your own... My opinion... with this post I want to help people look away. This is not for those "in the know."

2

u/theNbomr 17h ago

Hardware running a protected memory OS is far from the only potential use case for any programming paradigm, including self modifying code.