-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SH] Changes to compile with wasi-sdk
Wasi-sdk is much less Unix-like than the environment provided by Emscripten and requires more profound changes for support. Unix mman emulation requires: - `-lwasi-emulated-mman` Unfortunately JS and C++ exceptions are both a mess. Wasi-sdk [does not support C++ exceptions yet](https://github.com/WebAssembly/wasi-sdk/tree/a4d918fa119c9beb712bf08bbb8fa9996aab0a71?tab=readme-ov-file#notable-limitations). We can live with that, since Hermes for the most part compiles with exceptions disabled anyway. However JSI relies on exceptions and it is part of the compiled library. To get things working one needs to rely on the following hack (which isn't included in this commit): ```c++ extern "C" void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*)) { llvh::report_fatal_error("C++ exceptions not supported on Wasi"); } extern "C" void* __cxa_allocate_exception(size_t) { llvh::report_fatal_error("C++ exceptions not supported on Wasi"); } ``` This causes any JSI exception to simply abort. JS exceptions work fine with the bytecode interpreter, however code produced by the *native backend* relies on `setjmp/longjmp`. Alas, `setjmp/longjmp` are not well supported by Wasm. This fact was hidden by Emscripten, which used its own JS shim and exceptions to implement them. That, however, does not work with standalone WASI. According to the [Wasi-sdk documentation](https://github.com/WebAssembly/wasi-sdk/blob/a4d918fa119c9beb712bf08bbb8fa9996aab0a71/SetjmpLongjmp.md), `setjmp/longjmp` can be supported, using the Wasm exception handling proposal. This requires: - `-mllvm -wasm-enable-sjlj` - `-lsetjmp` I was able to successfully compile the shermes-generated code to a Wasm binary with the following flags, however I was **not** able to find a single standalone runtime that was able to run it out of the box! Wasmtime and Wasmer refused to load the module with the error "exceptions proposal not enabled". Iwasm failed to load it some error about Wasm operand stack. I am sure that it is possible to either find or configure a Wasm runtime to support the exceptions proposal, but since none of the major runtimes support if out of the box, I consider it useless for now. Sadly. I am sure v8 supports it, but what's the point of using v8 for this? First, v8 already supports JS, and second, Emscripten has that covered already. There is an experimental followup commit which turns setjmp() into noop and longjmp() into abort(). That at least allows natively compiled JS to run, until the first exception...
- Loading branch information
Showing
34 changed files
with
2,369 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements the UNIX Host support. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
//===----------------------------------------------------------------------===// | ||
//=== WARNING: Implementation here must contain only generic UNIX code that | ||
//=== is guaranteed to work on *all* UNIX variants. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Unix.h" | ||
#include "llvh/ADT/StringRef.h" | ||
#include "llvh/Config/config.h" | ||
#include <cctype> | ||
#include <string> | ||
#include <sys/utsname.h> | ||
|
||
using namespace llvh; | ||
|
||
static std::string getOSVersion() { | ||
struct utsname info; | ||
|
||
if (uname(&info)) | ||
return ""; | ||
|
||
return info.release; | ||
} | ||
|
||
static std::string updateTripleOSVersion(std::string TargetTripleString) { | ||
// On darwin, we want to update the version to match that of the target. | ||
std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin"); | ||
if (DarwinDashIdx != std::string::npos) { | ||
TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); | ||
TargetTripleString += getOSVersion(); | ||
return TargetTripleString; | ||
} | ||
std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos"); | ||
if (MacOSDashIdx != std::string::npos) { | ||
TargetTripleString.resize(MacOSDashIdx); | ||
// Reset the OS to darwin as the OS version from `uname` doesn't use the | ||
// macOS version scheme. | ||
TargetTripleString += "-darwin"; | ||
TargetTripleString += getOSVersion(); | ||
} | ||
return TargetTripleString; | ||
} | ||
|
||
std::string sys::getDefaultTargetTriple() { | ||
std::string TargetTripleString = | ||
updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE); | ||
|
||
// Override the default target with an environment variable named by | ||
// LLVM_TARGET_TRIPLE_ENV. | ||
#if defined(LLVM_TARGET_TRIPLE_ENV) | ||
if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV)) | ||
TargetTripleString = EnvTriple; | ||
#endif | ||
|
||
return TargetTripleString; | ||
} |
Oops, something went wrong.