Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cosmopolitan Libc Platform #2598

Merged
merged 3 commits into from
Oct 4, 2023

Conversation

G4Vi
Copy link
Contributor

@G4Vi G4Vi commented Sep 26, 2023

This patch adds the Cosmopolitan Libc platform enabling compatibility with multiple x86_64 operating systems with the same binary. The platform is similar to the Linux platform, but for now only x86_64 with interp modes are supported.

The only major change to the core is posix.c/convert_errno() was rewritten to use a switch statement. With Cosmopolitan errno values depend on the currently running operating system, and so they are non-constant and cannot be used in array designators. However, the cosmocc compiler allows non-constant case labels in switch statements, enabling the new version.

The spec tests can be ran via CC=cosmocc ./test_wamr.sh -j cosmopolitan -t classic-interp or CC=cosmocc ./test_wamr.sh -j cosmopolitan -t fast-interp . However, they fail in the same way they do on the Linux platform on my machine. Tests with float64 NaN fail without this patch I am in the process of upstreaming into Cosmopolitan: jart/cosmopolitan#901 .
tests/wamr-test-suites/test_wamr.sh lines 669-671 (https://github.com/dylibso/wasm-micro-runtime/blob/339facc7bf80e4a68d81eb56d4ec358c6c257176/tests/wamr-test-suites/test_wamr.sh#L669-L671) contain a hack to enable the test suite to run even though the the platform is incorrectly detected as Linux by other scripts (Linux is the only platform supported to build Cosmopolitan). If this isn't acceptable, I am happy to remove it, but would appreciate some guidance on the best way to propagate the platform to the child scripts, it looks messy how it's handled for linux sgx.

Edit: Updated test instructions after updating -j to specify platform in a3449ac.

* Multi-OS x86_64 builds with the same binary.
* Only interp modes supported.

The only major change is posix.c/convert_errno() was rewritten to use a switch
statement to accommodate errno values being non-const in the Cosmopolitan Libc
(cosmocc allows switch statements with non-const case labels).

Squashed commit of the following:

commit 25222dc
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Fri Sep 22 13:05:11 2023 -0400

    chore: add copyright headers

commit f5cd374
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Fri Sep 22 12:19:16 2023 -0400

    chore: undo unintentional formatting, add cosmo test comment

commit 248fe0a
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Wed Sep 20 13:43:40 2023 -0400

    feat add testing with cosmopolitan, set CC=cosmocc and pass -j

commit 6f5e471
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Sep 14 16:47:28 2023 -0400

    docs: add cosmo info to product mini README

commit af95d87
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Wed Sep 13 17:21:51 2023 -0400

    fix: renable ipv6 multicast on supporting platforms, apply k&r

commit eeac278
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Wed Sep 13 16:53:12 2023 -0400

    feat: add cosmopolitan platform

commit a3de045
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Wed Sep 13 14:34:14 2023 -0400

    fix: renable convert_errno and make it cosmopolitan libc compatible

commit 37113d0
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Wed Sep 13 13:43:51 2023 -0400

    renable CONFIG_HAS_POSIX_FALLOCATE and CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK on platforms that have it

commit e8181bf
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Tue Sep 12 18:12:17 2023 -0400

    remove some unneeded modifications for cosmo build

commit f103efd
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Tue Sep 12 12:05:32 2023 -0400

    chore: remove hermit

commit 9453ed7
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Tue Sep 12 11:27:46 2023 -0400

    fix: wamr building with cosmo a359de7893c33bfbd99aa82d570f97c55bf2b935

commit b2ba69a
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Jul 13 20:38:28 2023 -0400

    fix: remove _start entrypoint to pass args

commit a2d60e7
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Jul 13 18:25:55 2023 -0400

    feat: add specifying ENTRYPOINT in hermit.json

commit ab86abd
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Jul 13 17:56:37 2023 -0400

    chore: const json types

commit 4ea65ac
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Jul 13 17:48:00 2023 -0400

    feat: add json parsing to hermit loader

commit 9d1088d
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Jul 13 16:46:51 2023 -0400

    chore: rename hermit build script

commit 4c3e01c
Author: Gavin Hayes <gavin@dylibso.com>
Date:   Thu Jul 13 16:41:22 2023 -0400

    feat: APW PoC
@yamt
Copy link
Collaborator

yamt commented Sep 27, 2023

does this mean to build wamr this way: https://justine.lol/ape.html ?

@G4Vi
Copy link
Contributor Author

G4Vi commented Sep 27, 2023

Yes, exactly! This allows building Actually Portable Executables that run on multiple x86_64 systems with the same binary. iwasm.com can be built by setting up cosmocc as described in https://github.com/jart/cosmopolitan#getting-started and running ./build_cosmocc.sh from product-mini/platforms/cosmopolitan

Copy link
Contributor

@wenyongh wenyongh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wenyongh wenyongh merged commit d8ee771 into bytecodealliance:main Oct 4, 2023
368 checks passed
@G4Vi G4Vi deleted the cosmopolitan-libc branch October 27, 2023 16:10
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
This PR adds the Cosmopolitan Libc platform enabling compatibility with multiple
x86_64 operating systems with the same binary. The platform is similar to the
Linux platform, but for now only x86_64 with interpreter modes are supported.

The only major change to the core is `posix.c/convert_errno()` was rewritten to use
a switch statement. With Cosmopolitan errno values depend on the currently
running operating system, and so they are non-constant and cannot be used in array
designators. However, the `cosmocc` compiler allows non-constant case labels in
switch statements, enabling the new version.

And updated wamr-test-suites script to add `-j <platform>` option. The spec tests
can be ran via `CC=cosmocc ./test_wamr.sh -j cosmopolitan -t classic-interp`
or `CC=cosmocc ./test_wamr.sh -j cosmopolitan -t fast-interp`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants