frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Writing a Self-Mutating x86_64 C Program (2013)

https://ephemeral.cx/2013/12/writing-a-self-mutating-x86_64-c-program/
54•kepler471•4h ago

Comments

belter•2h ago
I guess in OpenBSD because of W ^ X this would not work?
akdas•2h ago
I was thinking the same thing. Usually, you'd want to write the new code to a page that you mark as read and write, then switch that page to read and execute. This becomes tricky if the code that's doing the modifying is in the same page as the code being modified.
timewizard•2h ago
The way it's coded it wouldn't; however, you can map the same shared memory twice. Once with R|W and a second time with R|X. Then you can write into one region and execute out of it's mirrored mapping.
rkeene2•2h ago
In Linux it also needs mprotect() to change the permissions on the page so it can write it. The OpenBSD man page[0] indicate that it supports this as well, though notes that not all implementations are guaranteed to allow it, but my guess is it would generally work.

[0] https://man.openbsd.org/mprotect.2

Retr0id•1h ago
It's not required on linux, if the ELF headers are set up such that the page is mapped rwx to begin with. (but rwx mappings are generally frowned upon from a security perspective)
mananaysiempre•2h ago
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/port...

alcover•1h ago
I often think this could maybe allow fantastic runtime optimisations. I realise this would be hardly debuggable but still..
Retr0id•1h ago
It already does, in the form of JIT compilation.
alcover•1h ago
OK but I meant in already native code, like in a C program - no bytecode.
Retr0id•42m ago
I mean that, too.
vbezhenar•1h ago
I used GNU lightning library once for such optimisation. I think it was ICFPC 2006 task. I had to write an interpreter for virtual machine. Naive approach worked but was slow, so I decided to speed it up a bit using JIT. It wasn't a 100% JIT, I think I just implemented it for loops but it was enough to tremendously speed it up.
userbinator•1h ago
Programs from the 80s-90s are likely to have such tricks. I have done something similar to "hardcode" semi-constants like frame sizes and quantisers in critical loops related to audio and video decompression, and the performance gain is indeed measurable.
alcover•40m ago
> "hardcode" semi-constants

You mean you somehow avoided a load. But what if the constant was already placed in a register ? Also how could you pinpoint the reference to your constant in the machine code ? I'm quite profane about all this.

ronsor•26m ago
> Also how could you pinpoint the reference to your constant in the machine code?

Not OP, but often one uses an easily identifiable dummy pattern like 0xC0DECA57 or 0xDEADBEEF which can be substituted without also messing up the machine code.

oxcabe•1h ago
It'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.
ivanjermakov•1h ago
I had a great experience writing self modified programs is a single instruction programming game SIC-1: https://store.steampowered.com/app/2124440/SIC1/

Plwm – An X11 window manager written in Prolog

https://github.com/Seeker04/plwm
90•jedeusus•3h ago•14 comments

Lottie is an open format for animated vector graphics

https://lottie.github.io/
205•marcodiego•6h ago•84 comments

Path to a free self-taught education in Computer Science

https://github.com/ossu/computer-science
95•saikatsg•4h ago•51 comments

Writing your own CUPS printer driver in 100 lines of Python (2018)

https://behind.pretix.eu/2018/01/20/cups-driver/
106•todsacerdoti•5h ago•8 comments

Ask HN: What are you working on? (May 2025)

28•david927•1h ago•78 comments

Lisping at JPL (2002)

https://flownet.com/gat/jpl-lisp.html
73•adityaathalye•3d ago•16 comments

Claude 4 System Card

https://simonwillison.net/2025/May/25/claude-4-system-card/
491•pvg•15h ago•194 comments

Writing a Self-Mutating x86_64 C Program (2013)

https://ephemeral.cx/2013/12/writing-a-self-mutating-x86_64-c-program/
54•kepler471•4h ago•16 comments

Design Pressure: The Invisible Hand That Shapes Your Code

https://hynek.me/talks/design-pressure/
107•NeutralForest•7h ago•29 comments

Show HN: Zli – A Batteries-Included CLI Framework for Zig

https://github.com/xcaeser/zli
39•caeser•4h ago•10 comments

Show HN: DaedalOS – Desktop Environment in the Browser

https://github.com/DustinBrett/daedalOS
76•DustinBrett•5h ago•15 comments

Koog, a Kotlin-based framework to build and run Al agents in idiomatic Kotlin

https://github.com/JetBrains/koog
14•prof18•3d ago•0 comments

Denmark to raise retirement age to 70

https://www.telegraph.co.uk/world-news/2025/05/23/denmark-raise-retirement-age-70/
175•wslh•4h ago•412 comments

CAPTCHAs are over (in ticketing)

https://behind.pretix.eu/2025/05/23/captchas-are-over/
72•pabs3•20h ago•71 comments

Martin (YC S23) Is Hiring Founding AI/Product Engineers to Build a Better Siri

https://www.ycombinator.com/companies/martin/jobs
1•darweenist•4h ago

Wrench Attacks: Physical attacks targeting cryptocurrency users (2024) [pdf]

https://drops.dagstuhl.de/storage/00lipics/lipics-vol316-aft2024/LIPIcs.AFT.2024.24/LIPIcs.AFT.2024.24.pdf
78•pulisse•9h ago•55 comments

'Strange metals' point to a whole new way to understand electricity

https://www.science.org/content/article/strange-metals-point-whole-new-way-understand-electricity
81•pseudolus•7h ago•25 comments

Can a corporation be pardoned?

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=5202339
34•megamike•4h ago•55 comments

Tariffs in American History

https://imprimis.hillsdale.edu/tariffs-in-american-history/
55•smitty1e•1d ago•84 comments

Is TfL losing the battle against heat on the Victoria line?

https://www.swlondoner.co.uk/news/16052025-is-tfl-losing-the-battle-against-heat-on-the-victoria-line
52•zeristor•12h ago•76 comments

What happens after you run Git push?

https://www.blacksmith.sh/blog/security
5•tsaifu•2d ago•0 comments

Show HN: SVG Animation Software

https://expressive.app/expressive-animator/
148•msarca•9h ago•62 comments

On File Formats

https://solhsa.com/oldernews2025.html#ON-FILE-FORMATS
99•ibobev•4d ago•61 comments

Dependency injection frameworks add confusion

http://rednafi.com/go/di_frameworks_bleh/
78•ingve•13h ago•84 comments

Reinvent the Wheel

https://endler.dev/2025/reinvent-the-wheel/
549•zdw•1d ago•209 comments

Programming on 34 Keys (2022)

https://oppi.li/posts/programming_on_34_keys/
48•todsacerdoti•8h ago•67 comments

Now you can watch the Internet Archive preserve documents in real time

https://www.theverge.com/news/672682/internet-archive-microfiche-lo-fi-beats-channel
95•LorenDB•2d ago•9 comments

Show HN: AI Baby Monitor – local Video-LLM that beeps when safety rules break

https://github.com/zeenolife/ai-baby-monitor
60•zeenolife•4d ago•45 comments

The Newark airport crisis

https://www.theverge.com/planes/673462/newark-airport-delay-air-traffic-control-tracon-radar
83•01-_-•4h ago•62 comments

Show HN: Wall Go – browser remake of a Devil's Plan 2 mini-game

https://schaoss.github.io/wall-go/
20•sychu•6h ago•7 comments