10

Quines, which are programs that generate their own code as part or all of their output are a neat idea for a programming puzzle. However, do they have any use beyond that?

1
  • 2
    There isn't a practical application that I know of. Sep 28, 2011 at 17:07

4 Answers 4

8

The only time I ever heard of any practical use for quines is when Ken Thompson used it to hide a Trojan horse in the Unix login program.

The Trojan consisted of something like if (login == "Trojan") login(); (more probably written in proper C), but something like that would be too obvious in the code. So what he did was to embed it in a quine and hide it in the binaries of the C compiler.

The quine's self replicating properties were enough to ensure that even though there was no trace of this backdoor in any source code, it would effectively compile every time a C compiler would come across a Unix login() function.

I guess clearer explications could come from reading the paper. It's a good paper.

See also: Is Ken Thompson's compiler hack still a threat?

2
  • 2
    That's my understanding too. The only practical use of a quine is in malware.
    – JohnFx
    Sep 28, 2011 at 18:38
  • @JohnFx If this qualifies as a quine, then every self-hosting compiler is a quine.
    – 8bittree
    Mar 21, 2022 at 21:44
4

A quine was used to seed Tierra, a digital organism simulator, and life then evolved from it. The quine was used because it guaranteed that the first generation would produce viable offspring.

The evolution led to many interesting results, including parasitic and symbiotic life forms, and even meta-parasites. Might that be considered 'useful'?

4

After some Googling, the surprising answer is that there is at least some theoretical academic effort to use them to make code self repairing, apparently.

One of my search terms was "genetic programming", if anyone wants to look further - it's the only potentially useful related programming area I could think of.

EDIT - Just found the use of Quines in an English outline of a Mathematical proof (Ron Maimon's answer) of Godel's Theorem.

1

I've used Quine-like techniques in web pages in the past. Mind you, this was in 1998-99...

I had what we now call a "web app", a set of CGI-BIN programs, some of which performed some time-consuming interactions with mainframes. In order to save doing the time-consuming part twice, in certain cases I would send along JavaScript that could re-write the original page into a form to send back pre-computed information to "reserve a spot in the future". I can't recall what peculiarity of JavaScript interpretation at the time required it, but Quine-like encoding, and decoding on output, of the "reserve a spot in the future" HTML and associated JavaScript became necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.