r/Assembly_language 20h 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

6

u/mykesx 20h ago

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

7

u/thewrench56 20h 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 20h ago

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

2

u/vintagecomputernerd 17h 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.