Writing a Self-Mutating x86_64 C Program (2013)
21–30 of 42 posts
Re: Writing a Self-Mutating x86_64 C Program (2013)
#22I often think this could maybe allow fantastic runtime optimisations. I realise this would be hardly debuggable but still..
Also on modern chips you must wait quite a number of cycles before executing modified code or endure a catastrophic performance hit. This is ok for loops and stuff, but makes a lot of the really clever stuff pointless.
The debuggers software breakpoints _are_ self-modifying code :)
Re: Writing a Self-Mutating x86_64 C Program (2013)
#23Earlier quoted context omitted.
It already does, in the form of JIT compilation.
OK but I meant in already native code, like in a C program - no bytecode.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#24It's impressive how well laid out the content in this article is. The spacing, tables, and code segments all look pristine to me, which is especially helpful given how dense and technical the content is.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#25I often think this could maybe allow fantastic runtime optimisations. I realise this would be hardly debuggable but still..
Re: Writing a Self-Mutating x86_64 C Program (2013)
#26- assumes x86_64
- makes the invalid assumption that functions get compiled into a contiguous range of bytes (I’m not aware of any compiler that violates that, but especially with profile-guided optimization or compilers that try to minimize program size, that may not be true, and there is nothing in the standard that guarantees it)
- assumes (as the article acknowledges) that “to determine the length of foo(), we added an empty function, bar(), that immediately follows foo(). By subtracting the address of bar() from foo() we can determine the length in bytes of foo().”. Even simple “all functions align at cache lines” slightly violates that, and I can see a compiler or a linker move the otherwise unused bar away from foo for various reasons.
- makes assumptions about the OS it is running on.
- makes assumptions about the instructions that its source code gets compiled into. For example, in the original example, a sufficiently smart compiler could compile
void foo(void) {
int i=0;
i++;
printf("i: %d\n", i);
}
as void foo(void) {
printf("1\n");
}
or maybe even void foo(void) {
puts("1");
}
Changing compiler flags can already break this program.Also, why does this example work without flushing the instruction cache after modifying the code?
Re: Writing a Self-Mutating x86_64 C Program (2013)
#27The only way to do this now on macOS is remapping whole pages as JIT. This makes it quite a challenge but still it might work…
Re: Writing a Self-Mutating x86_64 C Program (2013)
#28Fun article, but the resulting code is extremely brittle: - assumes x86_64 - makes the invalid assumption that functions get compiled into a contiguous range of bytes (I’m not aware of any compiler that violates that, but especially with profile-guided optimization or compilers that try to minimize program size, that may not be true, and there is nothing in the standard that guarantees it) - assumes (as the article a…
This has burned me before while writing a binary packer for Android.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#29It's impressive how well laid out the content in this article is. The spacing, tables, and code segments all look pristine to me, which is especially helpful given how dense and technical the content is.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#30I guess in OpenBSD because of W ^ X this would not work?
Not as is, but I think OpenBSD permits you to map the same memory twice, once as W and once as X (which would be a reasonable hoop to jump through for JITs etc., except there’s no portable way to do it). ARM64 MacOS doesn’t even permit that, and you need to use OS-specific incantations[1] that essentially prohibit two JITs coexisting in the same process. [1] https://developer.apple.com/documentation/apple-silicon/por…