Obfuscated Tiny C Compiler
bellard.org
bellard.org
t = "++#m--%am*@R<^1c/@%[_[H3c%@%[_[H3c+@.B#d-@%:_^BKd<<Z/03e>>`/03e<=0f>=/f<@.f>@1f==&g!=\'g&&k||#l&@.BCh^@.BSi|@.B+j~@/%Yd!@&d*@b";
Could someone please explain what is going on in that routine? ++ -> tokl=11 tokc=0x1
-- -> tokl=11 tokc=0xff
* -> tokl=1 tokc=0xc1af
/ -> tokl=1 tokc=0xf9f7
% -> tokl=1 tokc=0xf9f7
+ -> tokl=2 tokc=0xc801
- -> tokl=2 tokc=0xd8f7
<< -> tokl=3 tokc=0xe0d3
>> -> tokl=3 tokc=0xf8d3
...
! -> tokl=2 tokc=0x4
* -> tokl=0 tokc=0x0
For example *@R<^1c
^^ operator (padded with @ when one character)
^^^^ tokc (variable length)
^ toklThe first entry looks for "++", and if found sets tokc to 1 ('#' - 'b' + 64) and tokl to 11 ('m' - 'b').
The second entry looks for "--" and if found sets tokc to 255 and tokl to 11.
The third entry looks for "*" and if found sets tokc to 12693263 and tokl to 1. (12693263 is 0xc1af0f, which is an x86 IMUL instruction).
From the website:
``` binary operators, by decreasing priority order: '*' '/' '%', '+' '-', '>>' '<<', '<' '<=' '>' '>=', '==' '!=', '&', '^', '|', '=', '&&', '||'. ```
The unary operators ++ and -- have a higher precedence, so they come first. Then there are "*@R<^1c", "/@%[_[H3c", "%@%[_[H3c" (same as for '/', though), "+@.B#d" and so.
I did not have time to read the source (my compile finished a minute ago), but maybe you can now figure out the rest? :)
//edit: ha, edflsafoiewq and xashor we're a bit faster and more thorough. But nice to see we agree on the overall idea.
[0] https://greg.technology/img/postal-codes.jpg
Edit: incredibly, pointing the iPhone camera app at the low-ish resolution photo of the QR code above works -- the QR code does decode. That's crazy.
"Arline entered the nearby Albuquerque sanatorium, from where she wrote him letters in code — for the sheer fun of it, because she knew how he cherished puzzles, but the correspondence alarmed the military censors at the laboratory’s Intelligence Office. Tasked with abating any breaches to the secrecy of the operation, they cautioned Feynman that coded messages were against the rules and demanded that his wife include a key in each letter to help them decipher it. This only amplified Arline’s sense of fun — she began cutting holes into her letters, covering passages with ink, and even mail-ordered a jigsaw puzzle kit with which to cut up the pages and completely confound the agents."
[0] https://www.brainpickings.org/2017/10/17/richard-feynman-arl...
Finding a lab that did this online was pretty difficult at the time. If others have recommendations -- please chime in! Otherwise, I would strongly recommend considering Borealis (who I'm sure can ship the negatives by mail -- otherwise reach out and I can facilitate the mailing as I live here ha)
[0] https://www.instagram.com/p/B9MhMzKHVxJ/
[1] https://www.instagram.com/p/B7wL7MKly3k/ (description in Spanish and then English)
[2] https://github.com/gregsadetsky/postal-codes (project description, "rules", and source code)
------
Tiny C Compiler is also a great project:
The relationship is actually the other way around, at least according to this page:
"NOTE: My other project TinyCC which is a fully featured ISOC99 C compiler was written by starting from the source code of OTCC"
I wasn't sure which was which, but TCC is so lovely. It's cool to hear some of its history.
https://github.com/rui314/chibicc#chibicc-a-small-c-compiler
I very much like to know his views on topics like Rust, RISC-V, Blockchain etc... I'm curious about his workflow, dev. machine, preferred distros, editors and the like :D
An AMA or a technical interview done by someone knowledgeable in the field would be awesome!
[1]: https://bellard.org/qemacs/
[2]: https://www.freelists.org/post/quickjs-devel/Please-consider...
qemacs is actually quite advanced, and even includes a basic HTML renderer (for the help). I was half joking that with quickjs, one now can make a reasonably modern web browser out of bellard-ware…
Of course, I am referring to the users of APL family languages:
https://news.ycombinator.com/item?id=25902615
https://news.ycombinator.com/item?id=13565743
If you are not ready for APLs, then you can inspect this, not "obfuscated", but still dense, self-compiling C-subset JIT:
This guy knows how to push the language's limits and he is the main reason I switched back to C for good.
2018 https://news.ycombinator.com/item?id=17335856
2011 (a bit): https://news.ycombinator.com/item?id=2722494
So it's not that it wouldn't work, but it obviously wouldn't have worked for that contest.
But the fact a C compiler can compile itself is not very impressive. Making it fit into 2048 bytes of obfuscated code is.
Hmm....
I wonder how can one prevent that? Even an open-source ISA could not be fully trusted since one can't know what is implemented on the actual die on hand.
You suggest that an adversary could use some compromised layer to send a packet over the network when something happens. For example, someone running `aesenclast xmm15, xmm10` could also secretly trigger a socket()/send()/close() syscall set that would transmit a packet over the network adapter. But what if the target code never calls the AES-NI instructions? A user could easily be using a library that processes the encryption in software; a simple ISA filter has an impossible task to determine which xor instructions are cryptographic and which are just ordinary compiler output. You could be running a virtual machine, in which you've loaded a browser Javascript engine, and using that Javascript engine to run a virtual machine (https://bellard.org/jslinux/), in which you're running a Python instance, which is finally performing the encryption.
Sure, if all those layers were efficient, they'd just pass it down the stack to eventually call `aesenc`, but modern software is anything but efficient. Yes, if your deeply nested Python calls "sum = num1 + num2", the x86 `add` instruction likely gets invoked somewhere, but to predict when some client code is handling a secret key or when some user input is actually a password seems really difficult.
The topic is interesting from an academic point of view. Maybe something along "On trustworthy, verifiable execution on untrusted, adversary hardware". Like homomorphic encryption, but I think having the user supply the input would make for a difficult programming+debugging cycle :P
Anyway, the goal of this compiler here isn't to prevent ANY kind of attack. As the page says, it is "only" a tiny C compiler, and an obfuscated one at that. The "tiny" part makes it much easier to audit than say GCC or clang (and you don't need to audit GNU binutils, either - and I've seen that code, so trust me when I say: You don't want to go that route).
//edit: Obviously I don't want to insult the fine folks investing their time into projects such as binutils, they deserve quite some praise - but as someone who is not familiar with the code base I found it to be very overwhelming.
The ultimate question is: How far do you need to go until you believe that the earth is indeed not flat? Do you trust your teachers? Stand on a tall building? Take a balloon ride? A plane? A spaceship? Then still, at which point can you rule out that the earth just appears to be a ball due to some yet-to-be-explained phenomenon (gravitational anomaly?) but is indeed flat? (The correct answer of course is "turtles all the way down").
Similar is likely possible for other prog.langs that emit C.
One helpful feature for the full round-trip to an executable file is a built-in object file linker.
Also, libtcc can be used as a library to "compile a string" and then run it which is the JIT application mentioned elsewhere in this thread.
Not many employers would know of the IOCCC.
Sure. But who wants to work flipping burgers, greeting shoppers, or doing web front-ends for the rest of their life?
source: won the IOCCC
https://android.googlesource.com/platform/system/core/+/ecla...
The productivity of Bellard is truly admirable.
I also wonder his employer might be the nicest do-whatever-you-want-in-your-free-time employer on earth.
Not only that, I wonder what kind of day-job roles he has since I assume there is significant cognitive load in his personal projects (we still think he's a human not a superhuman right?).
For context, my last employer was "Oh you're doing something in your free time? let me utterly destroy you." (and they did).
edit: Even then, now he probably has the world's nicest co-founder instead. Who does cognitively demanding open source software projects made available for free online, while running a startup?
How could they? And what was their motivation for doing this?
An employee doing something in their free time is a rare and valuable asset.
They also had a policy that everyone should spend at least 4 hours every week (outside of the 40 hour work-week) doing some personal project, or learning. The more the better. (it was a recommendation, not a requirement, and there was no monitoring).
Well what I had in mind was a bit more ambitious than four hours. It did not infringe on their bottom-line (I wasn't using their tech or competing with my day-job; it was completely unrelated). But for me they were like "Oh, no, no ... we were just kidding. We really didn't mean that you should do something in your free time".
The encouragement to do something in free-time is a lie! ... they know most of the engineers don't do that. But if someone actually does something, then the lie gets exposed.
Try it yourself. Take them up on their offer and see what happens.
You said they destroyed you.
Did they fire you for working on your project?
Were you harassed? (tall poppy syndrome?)
With success comes maturity, and a startup turns into a corrupt, org-chart, white-elephant company. 90% of the employees end up being cronies that are pushing paper. The remaining 10% are the real engine of the company (cz the revenue stream has to be maintained somehow and real work still needs to be done). But those 10% need to be kept under the thumb at all cost, made to feel they're not good enough no matter what they do; otherwise they might start questioning wtf's going on in the rest of the company. Let's just say that I was one of those 10%. They did whatever they had to do to make an example out of me. I got my freedom, but I actually feel worse for the other hard-workers who were left behind.
* Small and easily embeddable: just a few C files, no external dependency, 210 KiB of x86 code for a simple hello world program.
* Fast interpreter with very low startup time: runs the 75000 tests of the ECMAScript Test Suite in about 100 seconds on a single core of a desktop PC. The complete life cycle of a runtime instance completes in less than 300 microseconds.
* Almost complete ES2020 support including modules, asynchronous generators and full Annex B support (legacy web compatibility).
* Passes nearly 100% of the ECMAScript Test Suite tests when selecting the ES2020 features. A summary is available at Test262 Report.
* Can compile Javascript sources to executables with no external dependency.
>> bellard is a programming genius and true C hacker. his works are so great ...
It's probably related. Obviously it's his and only his decision, but I wonder if him slowing down to share the knowledge and skills could have been more productive in the sense that others could do similar work.
Looking at his output some time ago, one of the main things that I found inspiring was how much good he did by sharing his work in a form that others could build on it.
Complaining that he's done insufficient to bring others on seems really unfair.
This is literally Animal Farm / team lead "multiplier" nonsense.
And like in Animal Farm, the pigs profit.
https://bellard.org/otcc/otccn.c the TCC documentation sheds further light: https://bellard.org/tcc/tcc-doc.html#devel
Valley startup hipster culture doesn't get this.
https://smartbear.com/blog/fabrice-bellard-portrait-of-a-sup...
"Bellard doesn’t appear to promote himself—he politely declined to be interviewed for this profile"
Let's don't forget the obligatory: "Can he invert a binary tree on a whiteboard though?"
Probably yes.
His contributions to probability theory, and morphogenesis are amazing in addition to being the father of computer science, and codebreaker. All of the work was done before he was 40.
[1] https://www.jstor.org/stable/2974762?seq=1#metadata_info_tab...