Recent

Author Topic: Interesting work on Universal Executables (Cosmopolitan)  (Read 2195 times)

WayneSherman

  • Sr. Member
  • ****
  • Posts: 250
Interesting work on Universal Executables (Cosmopolitan)
« on: December 02, 2023, 08:28:54 pm »
Cosmopolitan

Quote
Cosmopolitan Libc makes C a build-once run-anywhere language, like Java, except it doesn't need an interpreter or virtual machine. Instead, it reconfigures stock GCC and Clang to output a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS on AMD64 and ARM64 with the best possible performance and the tiniest footprint imaginable.

https://justine.lol/ape.html
https://justine.lol/cosmo3/
https://justine.lol/cosmopolitan/

Pulling this off required...
A new libc implementation
https://github.com/jart/cosmopolitan/tree/3.0/libc

A new linker that lets you build fat binaries
Quote
It's called apelink.c and it's a fine piece of poetry that weaves together the Portable Executable, ELF, Mach-O, and PKZIP file formats into shell scripts that run on most PCs and servers without needing to be installed.
https://github.com/jart/cosmopolitan/blob/3.0/tool/build/apelink.c

A POSIX change
Quote
POSIX even changed their rules about binary in shell scripts specifically to let us do it.
https://austingroupbugs.net/view.php?id=1250

Modifications to the C language and GCC:
https://ahgamut.github.io/2023/07/13/patching-gcc-cosmo/

What about performance and size?
Quote
Cosmo Libc will make your software faster and use less memory too. For example, when I build Emacs using the cosmocc toolchain, Emacs thinks it's building for Linux. Then, when I run it on Windows...It actually goes 2x faster than the native WIN32 port that the Emacs authors wrote on their own. Cosmo Emacs loads my dotfiles in 1.2 seconds whereas GNU Emacs on Windows loads them in 2.3 seconds. Many years ago when I started this project, I had this unproven belief that portability toil could be abstracted by having a better C library. Now I think this is all the proof we need that it's not only possible to make software instantly portable, but actually better too.

For example, one of the things you may be wondering is, "these fat binary files are huge, wouldn't that waste memory?"  The answer is no, because Cosmo only pages into memory the parts of the executable you need. Take for example one of Linux's greatest hits: the Debian Almquist shell.

$ ls -hal /usr/bin/dash
-rwxr-xr-x 1 root root 107K Nov 21  2022 /usr/bin/dash
$ ls -hal /opt/cosmos/bin/dash
-rwxr-xr-x 1 jart jart 983K Oct 15 19:14 /opt/cosmos/bin/dash

Here we see Cosmo's six OS + two architecture fat binary dash is 30% bigger than the one that comes with Alpine Linux (which only supports x86-Linux and dynamically links a separate 600kb Musl library). But if I run them:

$ rusage /usr/bin/dash -c true
took 231µs wall time
ballooned to 688kb in size
needed 183us cpu (0% kernel)
caused 34 page faults (100% memcpy)

$ rusage /opt/cosmos/bin/dash -c true
took 217µs wall time
ballooned to 544kb in size
needed 172us cpu (0% kernel)
caused 36 page faults (100% memcpy)

Here we see Cosmo's fat binary version of dash went faster and used less memory than an x86-Linux-only binary built for Musl Libc. This is due to (1) the magic of modern memory management, where CPU MMUs lazily load 4096 byte blocks at a time; and (2) how carefully apelink plans your executable layout.

For another performance comparison, see Production Web Servers

« Last Edit: December 02, 2023, 08:56:56 pm by WayneSherman »

domasz

  • Hero Member
  • *****
  • Posts: 553
Re: Interesting work on Universal Executables (Cosmopolitan)
« Reply #1 on: December 02, 2023, 08:50:17 pm »
I was skeptical but it does work. At least under Windows. Nice!

VisualLab

  • Hero Member
  • *****
  • Posts: 573
Re: Interesting work on Universal Executables (Cosmopolitan)
« Reply #2 on: December 02, 2023, 10:29:16 pm »
What are the hidden costs of this solution?

Because it is obvious that nothing is free. For users of software that runs only on Windows or only on Linux, this solution does not matter. In such cases, there is no point in bearing the costs of compromises included in this solution.

In my opinion, it's just a technical curiosity, showing that such a solution can be implemented. But that doesn't mean that it should be done or that it is worth doing. Either way, it is more ambitious than the primitive, makeshift and naive script-based solutions: JavaScript (Electron, etc.), Python, etc.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11942
  • FPC developer.
Re: Interesting work on Universal Executables (Cosmopolitan)
« Reply #3 on: December 02, 2023, 10:49:42 pm »
The issues with deployment was always the need for a mac to generate binaries for a Mac and that the Linux target is very fragmented. 

So it would be interesting how they deal with that.


WayneSherman

  • Sr. Member
  • ****
  • Posts: 250
Re: Interesting work on Universal Executables (Cosmopolitan)
« Reply #4 on: December 03, 2023, 12:58:30 am »
At this time, it compiles executables that run on a command line (console mode only).  But that includes web servers which can generate a gui in a (separate) browser.

The "Hermit" project uses it for WASM to bundle a self-contained command line executable that runs on multiple platforms (i.e. it makes the WASM interpreter and runtime universal):
Quote
Hermit bundles a WebAssembly runtime, WASI configuration, and a WebAssembly module into one executable (called a hermit). The same hermit executable runs on multiple platforms, bringing the portability of Wasm to a new level!
https://dylibso.com/blog/hermit-actually-portable-wasm/

Blackorzar

  • Newbie
  • Posts: 1
Re: Interesting work on Universal Executables (Cosmopolitan)
« Reply #5 on: December 08, 2023, 03:55:41 am »
I like the concept that Cosmo brings to executable file formats.
It is basically ELF (Linux executable) with a wrapper that allows other OS to execute it.

They also have some powerful features that could be interesting:
1. Their executable is a self-executable zip file. So, files can be added to the zip portion of the file using regular zip tools (taking advantage on zip files having their metadata at the end of the file).
2. The ZIP section of the executable allows them to have a readonly filesystem embedded in the exe itself. They have a library to access the zip section.
3. The Executable files are also bootable in bare metal. This is currently very limited. But allows them the possibility on  the future to make Cosmo applications to be Cloud/VM friendly.
4. They have been able to have big executables (up to 4GB) that are used in an AI (LLM) app.

I wonder if Lazarus could take advantage of a medium like this to make portable GUI apps. The zip section could allow to include a portable XWin and GTK along to platform specific versions of a graphics library (they have been able to compile SDL in Cosmo already).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8032
Re: Interesting work on Universal Executables (Cosmopolitan)
« Reply #6 on: December 08, 2023, 09:56:59 am »
However we do have to remember that no matter how good an idea is, if the Linux core developers don't like it it's dead in the water.

Just look at what happened to https://icculus.org/fatelf/ as an example.

And I'd particularly say that POSIX is irrelevant with people like Lennart Poettering actively advocating against strict compliance.

I'm not saying that I'm happy with any of the above, but they're the brutal realities.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018