From 078ba6dcefc2aabd2a201401056112bd3a474638 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sat, 4 May 2019 14:23:47 +0200 Subject: [PATCH 1/6] Migrate to wasi-common crate --- src/wasmtime.rs | 35 +- wasmtime-wasi/Cargo.toml | 8 +- wasmtime-wasi/build.rs | 118 - .../CMakeLists.txt | 8 - .../sandboxed-system-primitives/LICENSE | 7 - .../sandboxed-system-primitives/README.md | 9 - .../include/LICENSE | 121 - .../include/wasmtime_ssp.h | 842 ----- .../sandboxed-system-primitives/src/LICENSE | 24 - .../sandboxed-system-primitives/src/README.md | 14 - .../sandboxed-system-primitives/src/config.h | 100 - .../sandboxed-system-primitives/src/locking.h | 215 -- .../src/numeric_limits.h | 42 - .../sandboxed-system-primitives/src/posix.c | 2724 ----------------- .../sandboxed-system-primitives/src/posix.h | 63 - .../sandboxed-system-primitives/src/queue.h | 92 - .../sandboxed-system-primitives/src/random.c | 92 - .../sandboxed-system-primitives/src/random.h | 20 - .../src/refcount.h | 65 - .../sandboxed-system-primitives/src/rights.h | 83 - .../sandboxed-system-primitives/src/signals.h | 17 - .../sandboxed-system-primitives/src/str.c | 33 - .../sandboxed-system-primitives/src/str.h | 19 - wasmtime-wasi/src/host.rs | 88 - wasmtime-wasi/src/host_impls.rs | 183 -- wasmtime-wasi/src/instantiate.rs | 110 +- wasmtime-wasi/src/lib.rs | 7 +- wasmtime-wasi/src/syscalls.rs | 1063 +++---- wasmtime-wasi/src/translate.rs | 573 ---- wasmtime-wasi/src/wasm32.rs | 1442 --------- 30 files changed, 475 insertions(+), 7742 deletions(-) delete mode 100644 wasmtime-wasi/build.rs delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/CMakeLists.txt delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/LICENSE delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/README.md delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/include/LICENSE delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/LICENSE delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/README.md delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/config.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/locking.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/numeric_limits.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/posix.c delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/posix.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/queue.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/random.c delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/random.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/refcount.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/rights.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/signals.h delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/str.c delete mode 100644 wasmtime-wasi/sandboxed-system-primitives/src/str.h delete mode 100644 wasmtime-wasi/src/host.rs delete mode 100644 wasmtime-wasi/src/host_impls.rs delete mode 100644 wasmtime-wasi/src/translate.rs delete mode 100644 wasmtime-wasi/src/wasm32.rs diff --git a/src/wasmtime.rs b/src/wasmtime.rs index b50c8d5c2447..bdfda4825f3f 100644 --- a/src/wasmtime.rs +++ b/src/wasmtime.rs @@ -37,11 +37,10 @@ use cranelift_codegen::settings; use cranelift_codegen::settings::Configurable; use cranelift_native; use docopt::Docopt; -use errno::errno; use file_per_thread_logger; use pretty_env_logger; use std::error::Error; -use std::ffi::{CString, OsStr}; +use std::ffi::OsStr; use std::fs::File; use std::io; use std::io::prelude::*; @@ -114,22 +113,15 @@ fn read_wasm(path: PathBuf) -> Result, String> { }) } -fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(String, libc::c_int)> { +fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(String, File)> { let mut preopen_dirs = Vec::new(); for dir in flag_dir { - let fd = unsafe { - libc::open( - CString::new(dir.as_bytes()).unwrap().as_ptr(), - libc::O_RDONLY | libc::O_DIRECTORY, - ) - }; - if fd < 0 { - println!("error while pre-opening directory {}: {}", dir, errno()); + let preopen_dir = File::open(dir).unwrap_or_else(|err| { + println!("error while pre-opening directory {}: {}", dir, err); exit(1); - } - - preopen_dirs.push((dir.clone(), fd)); + }); + preopen_dirs.push((dir.clone(), preopen_dir)); } for mapdir in flag_mapdir { @@ -139,18 +131,11 @@ fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(Str exit(1); } let (key, value) = (parts[0], parts[1]); - let fd = unsafe { - libc::open( - CString::new(value.as_bytes()).unwrap().as_ptr(), - libc::O_RDONLY | libc::O_DIRECTORY, - ) - }; - if fd < 0 { - println!("error while pre-opening directory {}: {}", value, errno()); + let preopen_dir = File::open(value).unwrap_or_else(|err| { + println!("error while pre-opening directory {}: {}", value, err); exit(1); - } - - preopen_dirs.push((key.to_string(), fd)); + }); + preopen_dirs.push((key.to_string(), preopen_dir)); } preopen_dirs diff --git a/wasmtime-wasi/Cargo.toml b/wasmtime-wasi/Cargo.toml index 7b4161cd1f70..511dc505bd0e 100644 --- a/wasmtime-wasi/Cargo.toml +++ b/wasmtime-wasi/Cargo.toml @@ -13,18 +13,12 @@ readme = "README.md" wasmtime-runtime = { path = "../wasmtime-runtime" } wasmtime-environ = { path = "../wasmtime-environ" } wasmtime-jit = { path = "../wasmtime-jit" } +wasi-common = { git = "https://github.com/CraneStation/wasi-common" } cranelift-codegen = "0.30.0" cranelift-entity = "0.30.0" cranelift-wasm = "0.30.0" target-lexicon = "0.3.0" -cast = { version = "0.2.2", default-features = false } log = { version = "0.4.6", default-features = false } -libc = "0.2.50" -errno = "0.2.4" - -[build-dependencies] -cmake = "0.1.35" -bindgen = "0.49.0" [badges] maintenance = { status = "experimental" } diff --git a/wasmtime-wasi/build.rs b/wasmtime-wasi/build.rs deleted file mode 100644 index 4145068a572d..000000000000 --- a/wasmtime-wasi/build.rs +++ /dev/null @@ -1,118 +0,0 @@ -extern crate bindgen; -extern crate cmake; - -use cmake::Config; -use std::env; -use std::path::PathBuf; - -fn main() { - let dst = Config::new("sandboxed-system-primitives") - .cflag("-std=gnu99") - .build(); - - println!("cargo:rustc-link-search=native={}", dst.display()); - println!("cargo:rustc-link-lib=static=SandboxedSystemPrimitives"); - - let bindings_builder = bindgen::Builder::default() - .header("sandboxed-system-primitives/include/wasmtime_ssp.h") - .header("sandboxed-system-primitives/src/posix.h") - .whitelist_function("wasmtime_ssp_.*") - .whitelist_function("fd_table_init") - .whitelist_function("fd_table_insert_existing") - .whitelist_function("fd_prestats_init") - .whitelist_function("fd_prestats_insert") - .whitelist_function("argv_environ_init") - .whitelist_function("rwlock_.*") - .whitelist_type("__wasi_.*") - .whitelist_type("fd_table") - .whitelist_type("fd_prestat") - .whitelist_type("fd_prestats") - .whitelist_type("argv_environ_values") - .whitelist_var("__WASI_.*") - .blacklist_item("__WASI_ESUCCESS") - .blacklist_item("__WASI_E2BIG") - .blacklist_item("__WASI_EACCES") - .blacklist_item("__WASI_EADDRINUSE") - .blacklist_item("__WASI_EADDRNOTAVAIL") - .blacklist_item("__WASI_EAFNOSUPPORT") - .blacklist_item("__WASI_EAGAIN") - .blacklist_item("__WASI_EALREADY") - .blacklist_item("__WASI_EBADF") - .blacklist_item("__WASI_EBADMSG") - .blacklist_item("__WASI_EBUSY") - .blacklist_item("__WASI_ECANCELED") - .blacklist_item("__WASI_ECHILD") - .blacklist_item("__WASI_ECONNABORTED") - .blacklist_item("__WASI_ECONNREFUSED") - .blacklist_item("__WASI_ECONNRESET") - .blacklist_item("__WASI_EDEADLK") - .blacklist_item("__WASI_EDESTADDRREQ") - .blacklist_item("__WASI_EDOM") - .blacklist_item("__WASI_EDQUOT") - .blacklist_item("__WASI_EEXIST") - .blacklist_item("__WASI_EFAULT") - .blacklist_item("__WASI_EFBIG") - .blacklist_item("__WASI_EHOSTUNREACH") - .blacklist_item("__WASI_EIDRM") - .blacklist_item("__WASI_EILSEQ") - .blacklist_item("__WASI_EINPROGRESS") - .blacklist_item("__WASI_EINTR") - .blacklist_item("__WASI_EINVAL") - .blacklist_item("__WASI_EIO") - .blacklist_item("__WASI_EISCONN") - .blacklist_item("__WASI_EISDIR") - .blacklist_item("__WASI_ELOOP") - .blacklist_item("__WASI_EMFILE") - .blacklist_item("__WASI_EMLINK") - .blacklist_item("__WASI_EMSGSIZE") - .blacklist_item("__WASI_EMULTIHOP") - .blacklist_item("__WASI_ENAMETOOLONG") - .blacklist_item("__WASI_ENETDOWN") - .blacklist_item("__WASI_ENETRESET") - .blacklist_item("__WASI_ENETUNREACH") - .blacklist_item("__WASI_ENFILE") - .blacklist_item("__WASI_ENOBUFS") - .blacklist_item("__WASI_ENODEV") - .blacklist_item("__WASI_ENOENT") - .blacklist_item("__WASI_ENOEXEC") - .blacklist_item("__WASI_ENOLCK") - .blacklist_item("__WASI_ENOLINK") - .blacklist_item("__WASI_ENOMEM") - .blacklist_item("__WASI_ENOMSG") - .blacklist_item("__WASI_ENOPROTOOPT") - .blacklist_item("__WASI_ENOSPC") - .blacklist_item("__WASI_ENOSYS") - .blacklist_item("__WASI_ENOTCONN") - .blacklist_item("__WASI_ENOTDIR") - .blacklist_item("__WASI_ENOTEMPTY") - .blacklist_item("__WASI_ENOTRECOVERABLE") - .blacklist_item("__WASI_ENOTSOCK") - .blacklist_item("__WASI_ENOTSUP") - .blacklist_item("__WASI_ENOTTY") - .blacklist_item("__WASI_ENXIO") - .blacklist_item("__WASI_EOVERFLOW") - .blacklist_item("__WASI_EOWNERDEAD") - .blacklist_item("__WASI_EPERM") - .blacklist_item("__WASI_EPIPE") - .blacklist_item("__WASI_EPROTO") - .blacklist_item("__WASI_EPROTONOSUPPORT") - .blacklist_item("__WASI_EPROTOTYPE") - .blacklist_item("__WASI_ERANGE") - .blacklist_item("__WASI_EROFS") - .blacklist_item("__WASI_ESPIPE") - .blacklist_item("__WASI_ESRCH") - .blacklist_item("__WASI_ESTALE") - .blacklist_item("__WASI_ETIMEDOUT") - .blacklist_item("__WASI_ETXTBSY") - .blacklist_item("__WASI_EXDEV") - .blacklist_item("__WASI_ENOTCAPABLE") - .blacklist_item("__WASI_PREOPENTYPE_DIR"); - - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - - bindings_builder - .generate() - .expect("Unable to generate bindings") - .write_to_file(out_path.join("wasmtime_ssp.rs")) - .expect("Couldn't write bindings!"); -} diff --git a/wasmtime-wasi/sandboxed-system-primitives/CMakeLists.txt b/wasmtime-wasi/sandboxed-system-primitives/CMakeLists.txt deleted file mode 100644 index 3fb11b3b77ea..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.0) -project(SandboxedSystemPrimitives C) - -include_directories(include) - -add_library(SandboxedSystemPrimitives STATIC src/posix.c src/random.c src/str.c) - -install(TARGETS SandboxedSystemPrimitives DESTINATION .) diff --git a/wasmtime-wasi/sandboxed-system-primitives/LICENSE b/wasmtime-wasi/sandboxed-system-primitives/LICENSE deleted file mode 100644 index 83386f80ae94..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/LICENSE +++ /dev/null @@ -1,7 +0,0 @@ -Please see the LICENSE file in each top-level directory for the terms applicable to that directory and its relative sub-directories. - -The relevant directories and licenses are: - -src/ - BSD-2-Clause; see src/LICENSE for details -include/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication -polyfill/clang/ - MIT; see the header of polyfill/clang/stdatomic.h for details diff --git a/wasmtime-wasi/sandboxed-system-primitives/README.md b/wasmtime-wasi/sandboxed-system-primitives/README.md deleted file mode 100644 index feba35e9c329..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/README.md +++ /dev/null @@ -1,9 +0,0 @@ -This repository contains adapted forms of the CloudABI project's "libemulator" -library, which includes implementations the CloudABI system calls using -standard native platform support. See the README.md and LICENSE files in -the individual subdirectories for details. - -src/ - cloudabi-utils' libemulator; see src/README.md for details -include/ - wasi headers - -This is currently an experimental prototype. diff --git a/wasmtime-wasi/sandboxed-system-primitives/include/LICENSE b/wasmtime-wasi/sandboxed-system-primitives/include/LICENSE deleted file mode 100644 index 0e259d42c996..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/include/LICENSE +++ /dev/null @@ -1,121 +0,0 @@ -Creative Commons Legal Code - -CC0 1.0 Universal - - CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE - LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN - ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS - INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES - REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS - PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM - THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED - HEREUNDER. - -Statement of Purpose - -The laws of most jurisdictions throughout the world automatically confer -exclusive Copyright and Related Rights (defined below) upon the creator -and subsequent owner(s) (each and all, an "owner") of an original work of -authorship and/or a database (each, a "Work"). - -Certain owners wish to permanently relinquish those rights to a Work for -the purpose of contributing to a commons of creative, cultural and -scientific works ("Commons") that the public can reliably and without fear -of later claims of infringement build upon, modify, incorporate in other -works, reuse and redistribute as freely as possible in any form whatsoever -and for any purposes, including without limitation commercial purposes. -These owners may contribute to the Commons to promote the ideal of a free -culture and the further production of creative, cultural and scientific -works, or to gain reputation or greater distribution for their Work in -part through the use and efforts of others. - -For these and/or other purposes and motivations, and without any -expectation of additional consideration or compensation, the person -associating CC0 with a Work (the "Affirmer"), to the extent that he or she -is an owner of Copyright and Related Rights in the Work, voluntarily -elects to apply CC0 to the Work and publicly distribute the Work under its -terms, with knowledge of his or her Copyright and Related Rights in the -Work and the meaning and intended legal effect of CC0 on those rights. - -1. Copyright and Related Rights. A Work made available under CC0 may be -protected by copyright and related or neighboring rights ("Copyright and -Related Rights"). Copyright and Related Rights include, but are not -limited to, the following: - - i. the right to reproduce, adapt, distribute, perform, display, - communicate, and translate a Work; - ii. moral rights retained by the original author(s) and/or performer(s); -iii. publicity and privacy rights pertaining to a person's image or - likeness depicted in a Work; - iv. rights protecting against unfair competition in regards to a Work, - subject to the limitations in paragraph 4(a), below; - v. rights protecting the extraction, dissemination, use and reuse of data - in a Work; - vi. database rights (such as those arising under Directive 96/9/EC of the - European Parliament and of the Council of 11 March 1996 on the legal - protection of databases, and under any national implementation - thereof, including any amended or successor version of such - directive); and -vii. other similar, equivalent or corresponding rights throughout the - world based on applicable law or treaty, and any national - implementations thereof. - -2. Waiver. To the greatest extent permitted by, but not in contravention -of, applicable law, Affirmer hereby overtly, fully, permanently, -irrevocably and unconditionally waives, abandons, and surrenders all of -Affirmer's Copyright and Related Rights and associated claims and causes -of action, whether now known or unknown (including existing as well as -future claims and causes of action), in the Work (i) in all territories -worldwide, (ii) for the maximum duration provided by applicable law or -treaty (including future time extensions), (iii) in any current or future -medium and for any number of copies, and (iv) for any purpose whatsoever, -including without limitation commercial, advertising or promotional -purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each -member of the public at large and to the detriment of Affirmer's heirs and -successors, fully intending that such Waiver shall not be subject to -revocation, rescission, cancellation, termination, or any other legal or -equitable action to disrupt the quiet enjoyment of the Work by the public -as contemplated by Affirmer's express Statement of Purpose. - -3. Public License Fallback. Should any part of the Waiver for any reason -be judged legally invalid or ineffective under applicable law, then the -Waiver shall be preserved to the maximum extent permitted taking into -account Affirmer's express Statement of Purpose. In addition, to the -extent the Waiver is so judged Affirmer hereby grants to each affected -person a royalty-free, non transferable, non sublicensable, non exclusive, -irrevocable and unconditional license to exercise Affirmer's Copyright and -Related Rights in the Work (i) in all territories worldwide, (ii) for the -maximum duration provided by applicable law or treaty (including future -time extensions), (iii) in any current or future medium and for any number -of copies, and (iv) for any purpose whatsoever, including without -limitation commercial, advertising or promotional purposes (the -"License"). The License shall be deemed effective as of the date CC0 was -applied by Affirmer to the Work. Should any part of the License for any -reason be judged legally invalid or ineffective under applicable law, such -partial invalidity or ineffectiveness shall not invalidate the remainder -of the License, and in such case Affirmer hereby affirms that he or she -will not (i) exercise any of his or her remaining Copyright and Related -Rights in the Work or (ii) assert any associated claims and causes of -action with respect to the Work, in either case contrary to Affirmer's -express Statement of Purpose. - -4. Limitations and Disclaimers. - - a. No trademark or patent rights held by Affirmer are waived, abandoned, - surrendered, licensed or otherwise affected by this document. - b. Affirmer offers the Work as-is and makes no representations or - warranties of any kind concerning the Work, express, implied, - statutory or otherwise, including without limitation warranties of - title, merchantability, fitness for a particular purpose, non - infringement, or the absence of latent or other defects, accuracy, or - the present or absence of errors, whether or not discoverable, all to - the greatest extent permissible under applicable law. - c. Affirmer disclaims responsibility for clearing rights of other persons - that may apply to the Work or any use thereof, including without - limitation any person's Copyright and Related Rights in the Work. - Further, Affirmer disclaims responsibility for obtaining any necessary - consents, permissions or other rights required for any use of the - Work. - d. Affirmer understands and acknowledges that Creative Commons is not a - party to this document and has no duty or obligation with respect to - this CC0 or use of the Work. diff --git a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h deleted file mode 100644 index ff46aa235df7..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h +++ /dev/null @@ -1,842 +0,0 @@ -/* - * Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. - * See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. - * - * This file declares an interface similar to WASI, but augmented to expose - * some implementation details such as the curfds arguments that we pass - * around to avoid storing them in TLS. - */ - -#ifndef WASMTIME_SSP_H -#define WASMTIME_SSP_H - -#include -#include - -_Static_assert(_Alignof(int8_t) == 1, "non-wasi data layout"); -_Static_assert(_Alignof(uint8_t) == 1, "non-wasi data layout"); -_Static_assert(_Alignof(int16_t) == 2, "non-wasi data layout"); -_Static_assert(_Alignof(uint16_t) == 2, "non-wasi data layout"); -_Static_assert(_Alignof(int32_t) == 4, "non-wasi data layout"); -_Static_assert(_Alignof(uint32_t) == 4, "non-wasi data layout"); -_Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout"); -_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout"); - -#ifdef __cplusplus -extern "C" { -#endif - -typedef uint8_t __wasi_advice_t; -#define __WASI_ADVICE_NORMAL (0) -#define __WASI_ADVICE_SEQUENTIAL (1) -#define __WASI_ADVICE_RANDOM (2) -#define __WASI_ADVICE_WILLNEED (3) -#define __WASI_ADVICE_DONTNEED (4) -#define __WASI_ADVICE_NOREUSE (5) - -typedef uint32_t __wasi_clockid_t; -#define __WASI_CLOCK_REALTIME (0) -#define __WASI_CLOCK_MONOTONIC (1) -#define __WASI_CLOCK_PROCESS_CPUTIME_ID (2) -#define __WASI_CLOCK_THREAD_CPUTIME_ID (3) - -typedef uint64_t __wasi_device_t; - -typedef uint64_t __wasi_dircookie_t; -#define __WASI_DIRCOOKIE_START (0) - -typedef uint16_t __wasi_errno_t; -#define __WASI_ESUCCESS (0) -#define __WASI_E2BIG (1) -#define __WASI_EACCES (2) -#define __WASI_EADDRINUSE (3) -#define __WASI_EADDRNOTAVAIL (4) -#define __WASI_EAFNOSUPPORT (5) -#define __WASI_EAGAIN (6) -#define __WASI_EALREADY (7) -#define __WASI_EBADF (8) -#define __WASI_EBADMSG (9) -#define __WASI_EBUSY (10) -#define __WASI_ECANCELED (11) -#define __WASI_ECHILD (12) -#define __WASI_ECONNABORTED (13) -#define __WASI_ECONNREFUSED (14) -#define __WASI_ECONNRESET (15) -#define __WASI_EDEADLK (16) -#define __WASI_EDESTADDRREQ (17) -#define __WASI_EDOM (18) -#define __WASI_EDQUOT (19) -#define __WASI_EEXIST (20) -#define __WASI_EFAULT (21) -#define __WASI_EFBIG (22) -#define __WASI_EHOSTUNREACH (23) -#define __WASI_EIDRM (24) -#define __WASI_EILSEQ (25) -#define __WASI_EINPROGRESS (26) -#define __WASI_EINTR (27) -#define __WASI_EINVAL (28) -#define __WASI_EIO (29) -#define __WASI_EISCONN (30) -#define __WASI_EISDIR (31) -#define __WASI_ELOOP (32) -#define __WASI_EMFILE (33) -#define __WASI_EMLINK (34) -#define __WASI_EMSGSIZE (35) -#define __WASI_EMULTIHOP (36) -#define __WASI_ENAMETOOLONG (37) -#define __WASI_ENETDOWN (38) -#define __WASI_ENETRESET (39) -#define __WASI_ENETUNREACH (40) -#define __WASI_ENFILE (41) -#define __WASI_ENOBUFS (42) -#define __WASI_ENODEV (43) -#define __WASI_ENOENT (44) -#define __WASI_ENOEXEC (45) -#define __WASI_ENOLCK (46) -#define __WASI_ENOLINK (47) -#define __WASI_ENOMEM (48) -#define __WASI_ENOMSG (49) -#define __WASI_ENOPROTOOPT (50) -#define __WASI_ENOSPC (51) -#define __WASI_ENOSYS (52) -#define __WASI_ENOTCONN (53) -#define __WASI_ENOTDIR (54) -#define __WASI_ENOTEMPTY (55) -#define __WASI_ENOTRECOVERABLE (56) -#define __WASI_ENOTSOCK (57) -#define __WASI_ENOTSUP (58) -#define __WASI_ENOTTY (59) -#define __WASI_ENXIO (60) -#define __WASI_EOVERFLOW (61) -#define __WASI_EOWNERDEAD (62) -#define __WASI_EPERM (63) -#define __WASI_EPIPE (64) -#define __WASI_EPROTO (65) -#define __WASI_EPROTONOSUPPORT (66) -#define __WASI_EPROTOTYPE (67) -#define __WASI_ERANGE (68) -#define __WASI_EROFS (69) -#define __WASI_ESPIPE (70) -#define __WASI_ESRCH (71) -#define __WASI_ESTALE (72) -#define __WASI_ETIMEDOUT (73) -#define __WASI_ETXTBSY (74) -#define __WASI_EXDEV (75) -#define __WASI_ENOTCAPABLE (76) - -typedef uint16_t __wasi_eventrwflags_t; -#define __WASI_EVENT_FD_READWRITE_HANGUP (0x0001) - -typedef uint8_t __wasi_eventtype_t; -#define __WASI_EVENTTYPE_CLOCK (0) -#define __WASI_EVENTTYPE_FD_READ (1) -#define __WASI_EVENTTYPE_FD_WRITE (2) - -typedef uint32_t __wasi_exitcode_t; - -typedef uint32_t __wasi_fd_t; - -typedef uint16_t __wasi_fdflags_t; -#define __WASI_FDFLAG_APPEND (0x0001) -#define __WASI_FDFLAG_DSYNC (0x0002) -#define __WASI_FDFLAG_NONBLOCK (0x0004) -#define __WASI_FDFLAG_RSYNC (0x0008) -#define __WASI_FDFLAG_SYNC (0x0010) - -typedef int64_t __wasi_filedelta_t; - -typedef uint64_t __wasi_filesize_t; - -typedef uint8_t __wasi_filetype_t; -#define __WASI_FILETYPE_UNKNOWN (0) -#define __WASI_FILETYPE_BLOCK_DEVICE (1) -#define __WASI_FILETYPE_CHARACTER_DEVICE (2) -#define __WASI_FILETYPE_DIRECTORY (3) -#define __WASI_FILETYPE_REGULAR_FILE (4) -#define __WASI_FILETYPE_SOCKET_DGRAM (5) -#define __WASI_FILETYPE_SOCKET_STREAM (6) -#define __WASI_FILETYPE_SYMBOLIC_LINK (7) - -typedef uint16_t __wasi_fstflags_t; -#define __WASI_FILESTAT_SET_ATIM (0x0001) -#define __WASI_FILESTAT_SET_ATIM_NOW (0x0002) -#define __WASI_FILESTAT_SET_MTIM (0x0004) -#define __WASI_FILESTAT_SET_MTIM_NOW (0x0008) - -typedef uint64_t __wasi_inode_t; - -typedef uint32_t __wasi_linkcount_t; - -typedef uint32_t __wasi_lookupflags_t; -#define __WASI_LOOKUP_SYMLINK_FOLLOW (0x00000001) - -typedef uint16_t __wasi_oflags_t; -#define __WASI_O_CREAT (0x0001) -#define __WASI_O_DIRECTORY (0x0002) -#define __WASI_O_EXCL (0x0004) -#define __WASI_O_TRUNC (0x0008) - -typedef uint16_t __wasi_riflags_t; -#define __WASI_SOCK_RECV_PEEK (0x0001) -#define __WASI_SOCK_RECV_WAITALL (0x0002) - -typedef uint64_t __wasi_rights_t; -#define __WASI_RIGHT_FD_DATASYNC (0x0000000000000001) -#define __WASI_RIGHT_FD_READ (0x0000000000000002) -#define __WASI_RIGHT_FD_SEEK (0x0000000000000004) -#define __WASI_RIGHT_FD_FDSTAT_SET_FLAGS (0x0000000000000008) -#define __WASI_RIGHT_FD_SYNC (0x0000000000000010) -#define __WASI_RIGHT_FD_TELL (0x0000000000000020) -#define __WASI_RIGHT_FD_WRITE (0x0000000000000040) -#define __WASI_RIGHT_FD_ADVISE (0x0000000000000080) -#define __WASI_RIGHT_FD_ALLOCATE (0x0000000000000100) -#define __WASI_RIGHT_PATH_CREATE_DIRECTORY (0x0000000000000200) -#define __WASI_RIGHT_PATH_CREATE_FILE (0x0000000000000400) -#define __WASI_RIGHT_PATH_LINK_SOURCE (0x0000000000000800) -#define __WASI_RIGHT_PATH_LINK_TARGET (0x0000000000001000) -#define __WASI_RIGHT_PATH_OPEN (0x0000000000002000) -#define __WASI_RIGHT_FD_READDIR (0x0000000000004000) -#define __WASI_RIGHT_PATH_READLINK (0x0000000000008000) -#define __WASI_RIGHT_PATH_RENAME_SOURCE (0x0000000000010000) -#define __WASI_RIGHT_PATH_RENAME_TARGET (0x0000000000020000) -#define __WASI_RIGHT_PATH_FILESTAT_GET (0x0000000000040000) -#define __WASI_RIGHT_PATH_FILESTAT_SET_SIZE (0x0000000000080000) -#define __WASI_RIGHT_PATH_FILESTAT_SET_TIMES (0x0000000000100000) -#define __WASI_RIGHT_FD_FILESTAT_GET (0x0000000000200000) -#define __WASI_RIGHT_FD_FILESTAT_SET_SIZE (0x0000000000400000) -#define __WASI_RIGHT_FD_FILESTAT_SET_TIMES (0x0000000000800000) -#define __WASI_RIGHT_PATH_SYMLINK (0x0000000001000000) -#define __WASI_RIGHT_PATH_REMOVE_DIRECTORY (0x0000000002000000) -#define __WASI_RIGHT_PATH_UNLINK_FILE (0x0000000004000000) -#define __WASI_RIGHT_POLL_FD_READWRITE (0x0000000008000000) -#define __WASI_RIGHT_SOCK_SHUTDOWN (0x0000000010000000) - -typedef uint16_t __wasi_roflags_t; -#define __WASI_SOCK_RECV_DATA_TRUNCATED (0x0001) - -typedef uint8_t __wasi_sdflags_t; -#define __WASI_SHUT_RD (0x01) -#define __WASI_SHUT_WR (0x02) - -typedef uint16_t __wasi_siflags_t; - -typedef uint8_t __wasi_signal_t; -// 0 is reserved; POSIX has special semantics for kill(pid, 0). -#define __WASI_SIGHUP (1) -#define __WASI_SIGINT (2) -#define __WASI_SIGQUIT (3) -#define __WASI_SIGILL (4) -#define __WASI_SIGTRAP (5) -#define __WASI_SIGABRT (6) -#define __WASI_SIGBUS (7) -#define __WASI_SIGFPE (8) -#define __WASI_SIGKILL (9) -#define __WASI_SIGUSR1 (10) -#define __WASI_SIGSEGV (11) -#define __WASI_SIGUSR2 (12) -#define __WASI_SIGPIPE (13) -#define __WASI_SIGALRM (14) -#define __WASI_SIGTERM (15) -#define __WASI_SIGCHLD (16) -#define __WASI_SIGCONT (17) -#define __WASI_SIGSTOP (18) -#define __WASI_SIGTSTP (19) -#define __WASI_SIGTTIN (20) -#define __WASI_SIGTTOU (21) -#define __WASI_SIGURG (22) -#define __WASI_SIGXCPU (23) -#define __WASI_SIGXFSZ (24) -#define __WASI_SIGVTALRM (25) -#define __WASI_SIGPROF (26) -#define __WASI_SIGWINCH (27) -#define __WASI_SIGPOLL (28) -#define __WASI_SIGPWR (29) -#define __WASI_SIGSYS (30) - -typedef uint16_t __wasi_subclockflags_t; -#define __WASI_SUBSCRIPTION_CLOCK_ABSTIME (0x0001) - -typedef uint64_t __wasi_timestamp_t; - -typedef uint64_t __wasi_userdata_t; - -typedef uint8_t __wasi_whence_t; -#define __WASI_WHENCE_CUR (0) -#define __WASI_WHENCE_END (1) -#define __WASI_WHENCE_SET (2) - -typedef uint8_t __wasi_preopentype_t; -#define __WASI_PREOPENTYPE_DIR (0) - -struct fd_table; -struct fd_prestats; -struct argv_environ_values; - -typedef struct __wasi_dirent_t { - __wasi_dircookie_t d_next; - __wasi_inode_t d_ino; - uint32_t d_namlen; - __wasi_filetype_t d_type; -} __wasi_dirent_t; -_Static_assert(offsetof(__wasi_dirent_t, d_next) == 0, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_dirent_t, d_ino) == 8, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_dirent_t, d_namlen) == 16, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_dirent_t, d_type) == 20, "non-wasi data layout"); -_Static_assert(sizeof(__wasi_dirent_t) == 24, "non-wasi data layout"); -_Static_assert(_Alignof(__wasi_dirent_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_event_t { - __wasi_userdata_t userdata; - __wasi_errno_t error; - __wasi_eventtype_t type; - union __wasi_event_u { - struct __wasi_event_u_fd_readwrite_t { - __wasi_filesize_t nbytes; - __wasi_eventrwflags_t flags; - } fd_readwrite; - } u; -} __wasi_event_t; -_Static_assert(offsetof(__wasi_event_t, userdata) == 0, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_event_t, error) == 8, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_event_t, type) == 10, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_event_t, u.fd_readwrite.nbytes) == 16, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_event_t, u.fd_readwrite.flags) == 24, "non-wasi data layout"); -_Static_assert(sizeof(__wasi_event_t) == 32, "non-wasi data layout"); -_Static_assert(_Alignof(__wasi_event_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_prestat_t { - __wasi_preopentype_t pr_type; - union __wasi_prestat_u { - struct __wasi_prestat_u_dir_t { - size_t pr_name_len; - } dir; - } u; -} __wasi_prestat_t; -_Static_assert(offsetof(__wasi_prestat_t, pr_type) == 0, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - offsetof(__wasi_prestat_t, u.dir.pr_name_len) == 4, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - offsetof(__wasi_prestat_t, u.dir.pr_name_len) == 8, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - sizeof(__wasi_prestat_t) == 8, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - sizeof(__wasi_prestat_t) == 16, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - _Alignof(__wasi_prestat_t) == 4, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - _Alignof(__wasi_prestat_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_fdstat_t { - __wasi_filetype_t fs_filetype; - __wasi_fdflags_t fs_flags; - __wasi_rights_t fs_rights_base; - __wasi_rights_t fs_rights_inheriting; -} __wasi_fdstat_t; -_Static_assert( - offsetof(__wasi_fdstat_t, fs_filetype) == 0, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_fdstat_t, fs_flags) == 2, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_fdstat_t, fs_rights_base) == 8, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_fdstat_t, fs_rights_inheriting) == 16, - "non-wasi data layout"); -_Static_assert(sizeof(__wasi_fdstat_t) == 24, "non-wasi data layout"); -_Static_assert(_Alignof(__wasi_fdstat_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_filestat_t { - __wasi_device_t st_dev; - __wasi_inode_t st_ino; - __wasi_filetype_t st_filetype; - __wasi_linkcount_t st_nlink; - __wasi_filesize_t st_size; - __wasi_timestamp_t st_atim; - __wasi_timestamp_t st_mtim; - __wasi_timestamp_t st_ctim; -} __wasi_filestat_t; -_Static_assert(offsetof(__wasi_filestat_t, st_dev) == 0, "non-wasi data layout"); -_Static_assert(offsetof(__wasi_filestat_t, st_ino) == 8, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_filestat_t, st_filetype) == 16, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_filestat_t, st_nlink) == 20, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_filestat_t, st_size) == 24, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_filestat_t, st_atim) == 32, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_filestat_t, st_mtim) == 40, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_filestat_t, st_ctim) == 48, "non-wasi data layout"); -_Static_assert(sizeof(__wasi_filestat_t) == 56, "non-wasi data layout"); -_Static_assert(_Alignof(__wasi_filestat_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_ciovec_t { - const void *buf; - size_t buf_len; -} __wasi_ciovec_t; -_Static_assert(offsetof(__wasi_ciovec_t, buf) == 0, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - offsetof(__wasi_ciovec_t, buf_len) == 4, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - offsetof(__wasi_ciovec_t, buf_len) == 8, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - sizeof(__wasi_ciovec_t) == 8, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - sizeof(__wasi_ciovec_t) == 16, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - _Alignof(__wasi_ciovec_t) == 4, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - _Alignof(__wasi_ciovec_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_iovec_t { - void *buf; - size_t buf_len; -} __wasi_iovec_t; -_Static_assert(offsetof(__wasi_iovec_t, buf) == 0, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - offsetof(__wasi_iovec_t, buf_len) == 4, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - offsetof(__wasi_iovec_t, buf_len) == 8, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - sizeof(__wasi_iovec_t) == 8, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - sizeof(__wasi_iovec_t) == 16, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 4 || - _Alignof(__wasi_iovec_t) == 4, "non-wasi data layout"); -_Static_assert(sizeof(void *) != 8 || - _Alignof(__wasi_iovec_t) == 8, "non-wasi data layout"); - -typedef struct __wasi_subscription_t { - __wasi_userdata_t userdata; - __wasi_eventtype_t type; - union __wasi_subscription_u { - struct __wasi_subscription_u_clock_t { - __wasi_userdata_t identifier; - __wasi_clockid_t clock_id; - __wasi_timestamp_t timeout; - __wasi_timestamp_t precision; - __wasi_subclockflags_t flags; - } clock; - struct __wasi_subscription_u_fd_readwrite_t { - __wasi_fd_t fd; - } fd_readwrite; - } u; -} __wasi_subscription_t; -_Static_assert( - offsetof(__wasi_subscription_t, userdata) == 0, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, type) == 8, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, u.clock.identifier) == 16, - "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, u.clock.clock_id) == 24, - "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, u.clock.timeout) == 32, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, u.clock.precision) == 40, - "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, u.clock.flags) == 48, "non-wasi data layout"); -_Static_assert( - offsetof(__wasi_subscription_t, u.fd_readwrite.fd) == 16, - "non-wasi data layout"); -_Static_assert(sizeof(__wasi_subscription_t) == 56, "non-wasi data layout"); -_Static_assert(_Alignof(__wasi_subscription_t) == 8, "non-wasi data layout"); - -#if defined(WASMTIME_SSP_WASI_API) -#define WASMTIME_SSP_SYSCALL_NAME(name) \ - asm("__wasi_" #name) -#else -#define WASMTIME_SSP_SYSCALL_NAME(name) -#endif - -__wasi_errno_t wasmtime_ssp_args_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *arg_environ, -#endif - char **argv, - char *argv_buf -) WASMTIME_SSP_SYSCALL_NAME(args_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_args_sizes_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *arg_environ, -#endif - size_t *argc, - size_t *argv_buf_size -) WASMTIME_SSP_SYSCALL_NAME(args_sizes_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_clock_res_get( - __wasi_clockid_t clock_id, - __wasi_timestamp_t *resolution -) WASMTIME_SSP_SYSCALL_NAME(clock_res_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_clock_time_get( - __wasi_clockid_t clock_id, - __wasi_timestamp_t precision, - __wasi_timestamp_t *time -) WASMTIME_SSP_SYSCALL_NAME(clock_time_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_environ_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *arg_environ, -#endif - char **environ, - char *environ_buf -) WASMTIME_SSP_SYSCALL_NAME(environ_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_environ_sizes_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *arg_environ, -#endif - size_t *environ_count, - size_t *environ_buf_size -) WASMTIME_SSP_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_close( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, - struct fd_prestats *prestats, -#endif - __wasi_fd_t fd -) WASMTIME_SSP_SYSCALL_NAME(fd_close) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_datasync( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd -) WASMTIME_SSP_SYSCALL_NAME(fd_datasync) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_pread( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_iovec_t *iovs, - size_t iovs_len, - __wasi_filesize_t offset, - size_t *nread -) WASMTIME_SSP_SYSCALL_NAME(fd_pread) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_pwrite( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_ciovec_t *iovs, - size_t iovs_len, - __wasi_filesize_t offset, - size_t *nwritten -) WASMTIME_SSP_SYSCALL_NAME(fd_pwrite) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_read( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_iovec_t *iovs, - size_t iovs_len, - size_t *nread -) WASMTIME_SSP_SYSCALL_NAME(fd_read) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_renumber( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, - struct fd_prestats *prestats, -#endif - __wasi_fd_t from, - __wasi_fd_t to -) WASMTIME_SSP_SYSCALL_NAME(fd_renumber) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_seek( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filedelta_t offset, - __wasi_whence_t whence, - __wasi_filesize_t *newoffset -) WASMTIME_SSP_SYSCALL_NAME(fd_seek) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_tell( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t *newoffset -) WASMTIME_SSP_SYSCALL_NAME(fd_tell) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_fdstat_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_fdstat_t *buf -) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_fdstat_set_flags( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_fdflags_t flags -) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_set_flags) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_fdstat_set_rights( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_rights_t fs_rights_base, - __wasi_rights_t fs_rights_inheriting -) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_set_rights) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_sync( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd -) WASMTIME_SSP_SYSCALL_NAME(fd_sync) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_write( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_ciovec_t *iovs, - size_t iovs_len, - size_t *nwritten -) WASMTIME_SSP_SYSCALL_NAME(fd_write) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_advise( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t offset, - __wasi_filesize_t len, - __wasi_advice_t advice -) WASMTIME_SSP_SYSCALL_NAME(fd_advise) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_allocate( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t offset, - __wasi_filesize_t len -) WASMTIME_SSP_SYSCALL_NAME(fd_allocate) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_create_directory( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t path_len -) WASMTIME_SSP_SYSCALL_NAME(path_create_directory) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_link( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t old_fd, - __wasi_lookupflags_t old_flags, - const char *old_path, - size_t old_path_len, - __wasi_fd_t new_fd, - const char *new_path, - size_t new_path_len -) WASMTIME_SSP_SYSCALL_NAME(path_link) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_open( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t dirfd, - __wasi_lookupflags_t dirflags, - const char *path, - size_t path_len, - __wasi_oflags_t oflags, - __wasi_rights_t fs_rights_base, - __wasi_rights_t fs_rights_inheriting, - __wasi_fdflags_t fs_flags, - __wasi_fd_t *fd -) WASMTIME_SSP_SYSCALL_NAME(path_open) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_readdir( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - void *buf, - size_t buf_len, - __wasi_dircookie_t cookie, - size_t *bufused -) WASMTIME_SSP_SYSCALL_NAME(fd_readdir) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_readlink( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t path_len, - char *buf, - size_t buf_len, - size_t *bufused -) WASMTIME_SSP_SYSCALL_NAME(path_readlink) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_rename( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t old_fd, - const char *old_path, - size_t old_path_len, - __wasi_fd_t new_fd, - const char *new_path, - size_t new_path_len -) WASMTIME_SSP_SYSCALL_NAME(path_rename) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_filestat_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filestat_t *buf -) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_filestat_set_times( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_timestamp_t st_atim, - __wasi_timestamp_t st_mtim, - __wasi_fstflags_t fstflags -) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_set_times) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_fd_filestat_set_size( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t st_size -) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_set_size) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_filestat_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_lookupflags_t flags, - const char *path, - size_t path_len, - __wasi_filestat_t *buf -) WASMTIME_SSP_SYSCALL_NAME(path_filestat_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_filestat_set_times( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_lookupflags_t flags, - const char *path, - size_t path_len, - __wasi_timestamp_t st_atim, - __wasi_timestamp_t st_mtim, - __wasi_fstflags_t fstflags -) WASMTIME_SSP_SYSCALL_NAME(path_filestat_set_times) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_symlink( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - const char *old_path, - size_t old_path_len, - __wasi_fd_t fd, - const char *new_path, - size_t new_path_len -) WASMTIME_SSP_SYSCALL_NAME(path_symlink) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_unlink_file( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t path_len -) WASMTIME_SSP_SYSCALL_NAME(path_unlink_file) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_path_remove_directory( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t path_len -) WASMTIME_SSP_SYSCALL_NAME(path_remove_directory) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_poll_oneoff( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - const __wasi_subscription_t *in, - __wasi_event_t *out, - size_t nsubscriptions, - size_t *nevents -) WASMTIME_SSP_SYSCALL_NAME(poll_oneoff) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_proc_raise( - __wasi_signal_t sig -) WASMTIME_SSP_SYSCALL_NAME(proc_raise) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_random_get( - void *buf, - size_t buf_len -) WASMTIME_SSP_SYSCALL_NAME(random_get) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_sock_recv( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t sock, - const __wasi_iovec_t *ri_data, - size_t ri_data_len, - __wasi_riflags_t ri_flags, - size_t *ro_datalen, - __wasi_roflags_t *ro_flags -) WASMTIME_SSP_SYSCALL_NAME(sock_recv) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_sock_send( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t sock, - const __wasi_ciovec_t *si_data, - size_t si_data_len, - __wasi_siflags_t si_flags, - size_t *so_datalen -) WASMTIME_SSP_SYSCALL_NAME(sock_send) __attribute__((__warn_unused_result__)); - -__wasi_errno_t wasmtime_ssp_sock_shutdown( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t sock, - __wasi_sdflags_t how -) WASMTIME_SSP_SYSCALL_NAME(sock_shutdown) __attribute__((__warn_unused_result__)); - -#ifdef __cplusplus -} -#endif - -#undef WASMTIME_SSP_SYSCALL_NAME - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/LICENSE b/wasmtime-wasi/sandboxed-system-primitives/src/LICENSE deleted file mode 100644 index 04c6f48a27d7..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/LICENSE +++ /dev/null @@ -1,24 +0,0 @@ -All code is distributed under the following license: - - Copyright (c) 2015 Nuxi, https://nuxi.nl/ - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - SUCH DAMAGE. diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/README.md b/wasmtime-wasi/sandboxed-system-primitives/src/README.md deleted file mode 100644 index 77a34bb5c3c7..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/README.md +++ /dev/null @@ -1,14 +0,0 @@ -This directory consists of selected files copied from the [libemulator] -directory in the [cloudabi-utils] repository, with minor modifications, -along with the accompanying LICENSE file from that repository. - -The modifications are marked with `WASMTIME_*` preprocessor macros. - -The files were copied at git revision -be1ce21e1dded9c0c0a6ebe144cbea01cf44a874 -which is dated -Sun Jan 13 23:26:03 2019 +0100 -. - -[libemulator]: https://github.com/NuxiNL/cloudabi-utils/tree/master/src/libemulator -[cloudabi-utils]: https://github.com/NuxiNL/cloudabi-utils diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/config.h b/wasmtime-wasi/sandboxed-system-primitives/src/config.h deleted file mode 100644 index 714e29d24fce..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/config.h +++ /dev/null @@ -1,100 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef CONFIG_H -#define CONFIG_H - -#include - -#if defined(__FreeBSD__) || defined(__APPLE__) -#define CONFIG_HAS_ARC4RANDOM_BUF 1 -#else -#define CONFIG_HAS_ARC4RANDOM_BUF 0 -#endif - -// On Linux, prefer to use getrandom, though it isn't available in -// GLIBC before 2.25. -#if defined(__linux__) && \ - (!defined(__GLIBC__) || \ - __GLIBC__ > 2 || \ - (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)) -#define CONFIG_HAS_GETRANDOM 1 -#else -#define CONFIG_HAS_GETRANDOM 0 -#endif - -#if defined(__CloudABI__) -#define CONFIG_HAS_CAP_ENTER 1 -#else -#define CONFIG_HAS_CAP_ENTER 0 -#endif - -#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__) -#define CONFIG_HAS_CLOCK_NANOSLEEP 1 -#else -#define CONFIG_HAS_CLOCK_NANOSLEEP 0 -#endif - -#if !defined(__APPLE__) && !defined(__FreeBSD__) -#define CONFIG_HAS_FDATASYNC 1 -#else -#define CONFIG_HAS_FDATASYNC 0 -#endif - -#ifndef __CloudABI__ -#define CONFIG_HAS_ISATTY 1 -#else -#define CONFIG_HAS_ISATTY 0 -#endif - -#ifndef __APPLE__ -#define CONFIG_HAS_POSIX_FALLOCATE 1 -#else -#define CONFIG_HAS_POSIX_FALLOCATE 0 -#endif - -#ifndef __APPLE__ -#define CONFIG_HAS_PREADV 1 -#else -#define CONFIG_HAS_PREADV 0 -#endif - -#if defined(__APPLE__) || defined(__CloudABI__) -#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 1 -#else -#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 0 -#endif - -#ifndef __APPLE__ -#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 1 -#else -#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0 -#endif - -#ifndef __APPLE__ -#define CONFIG_HAS_PWRITEV 1 -#else -#define CONFIG_HAS_PWRITEV 0 -#endif - -#ifdef __APPLE__ -#define st_atimespec st_atim -#define st_mtimespec st_mtim -#define st_ctimespec st_ctim -#endif - -#ifdef __APPLE__ -#define CONFIG_TLS_USE_GSBASE 1 -#else -#define CONFIG_TLS_USE_GSBASE 0 -#endif - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/locking.h b/wasmtime-wasi/sandboxed-system-primitives/src/locking.h deleted file mode 100644 index 3c606235ebb3..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/locking.h +++ /dev/null @@ -1,215 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef LOCKING_H -#define LOCKING_H - -#include "config.h" - -#include -#include -#include -#include -#include - -#ifndef __has_extension -#define __has_extension(x) 0 -#endif - -#if __has_extension(c_thread_safety_attributes) -#define LOCK_ANNOTATE(x) __attribute__((x)) -#else -#define LOCK_ANNOTATE(x) -#endif - -// Lock annotation macros. - -#define LOCKABLE LOCK_ANNOTATE(lockable) - -#define LOCKS_EXCLUSIVE(...) LOCK_ANNOTATE(exclusive_lock_function(__VA_ARGS__)) -#define LOCKS_SHARED(...) LOCK_ANNOTATE(shared_lock_function(__VA_ARGS__)) - -#define TRYLOCKS_EXCLUSIVE(...) \ - LOCK_ANNOTATE(exclusive_trylock_function(__VA_ARGS__)) -#define TRYLOCKS_SHARED(...) LOCK_ANNOTATE(shared_trylock_function(__VA_ARGS__)) - -#define UNLOCKS(...) LOCK_ANNOTATE(unlock_function(__VA_ARGS__)) - -#define REQUIRES_EXCLUSIVE(...) \ - LOCK_ANNOTATE(exclusive_locks_required(__VA_ARGS__)) -#define REQUIRES_SHARED(...) LOCK_ANNOTATE(shared_locks_required(__VA_ARGS__)) -#define REQUIRES_UNLOCKED(...) LOCK_ANNOTATE(locks_excluded(__VA_ARGS__)) - -#define NO_LOCK_ANALYSIS LOCK_ANNOTATE(no_thread_safety_analysis) - -// Mutex that uses the lock annotations. - -struct LOCKABLE mutex { - pthread_mutex_t object; -}; - -#define MUTEX_INITIALIZER \ - { PTHREAD_MUTEX_INITIALIZER } - -static inline void mutex_init(struct mutex *lock) REQUIRES_UNLOCKED(*lock) { - pthread_mutex_init(&lock->object, NULL); -} - -static inline void mutex_destroy(struct mutex *lock) REQUIRES_UNLOCKED(*lock) { - pthread_mutex_destroy(&lock->object); -} - -static inline void mutex_lock(struct mutex *lock) - LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { - pthread_mutex_lock(&lock->object); -} - -static inline void mutex_unlock(struct mutex *lock) - UNLOCKS(*lock) NO_LOCK_ANALYSIS { - pthread_mutex_unlock(&lock->object); -} - -// Read-write lock that uses the lock annotations. - -struct LOCKABLE rwlock { - pthread_rwlock_t object; -}; - -void rwlock_init(struct rwlock *lock) REQUIRES_UNLOCKED(*lock) { - pthread_rwlock_init(&lock->object, NULL); -} - -void rwlock_rdlock(struct rwlock *lock) - LOCKS_SHARED(*lock) NO_LOCK_ANALYSIS { - pthread_rwlock_rdlock(&lock->object); -} - -void rwlock_wrlock(struct rwlock *lock) - LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { - pthread_rwlock_wrlock(&lock->object); -} - -void rwlock_unlock(struct rwlock *lock) - UNLOCKS(*lock) NO_LOCK_ANALYSIS { - pthread_rwlock_unlock(&lock->object); -} - -// Condition variable that uses the lock annotations. - -struct LOCKABLE cond { - pthread_cond_t object; -#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \ - !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP - clockid_t clock; -#endif -}; - -static inline void cond_init_monotonic(struct cond *cond) { -#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK - pthread_condattr_t attr; - pthread_condattr_init(&attr); - pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); - pthread_cond_init(&cond->object, &attr); - pthread_condattr_destroy(&attr); -#else - pthread_cond_init(&cond->object, NULL); -#endif -#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \ - !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP - cond->clock = CLOCK_MONOTONIC; -#endif -} - -static inline void cond_init_realtime(struct cond *cond) { - pthread_cond_init(&cond->object, NULL); -#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \ - !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP - cond->clock = CLOCK_REALTIME; -#endif -} - -static inline void cond_destroy(struct cond *cond) { - pthread_cond_destroy(&cond->object); -} - -static inline void cond_signal(struct cond *cond) { - pthread_cond_signal(&cond->object); -} - -static inline bool cond_timedwait(struct cond *cond, struct mutex *lock, - uint64_t timeout, bool abstime) - REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { - struct timespec ts = { - .tv_sec = (time_t)(timeout / 1000000000), - .tv_nsec = (long)(timeout % 1000000000), - }; - - if (abstime) { -#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK - // No native support for sleeping on monotonic clocks. Convert the - // timeout to a relative value and then to an absolute value for the - // realtime clock. - if (cond->clock != CLOCK_REALTIME) { - struct timespec ts_monotonic; - clock_gettime(cond->clock, &ts_monotonic); - ts.tv_sec -= ts_monotonic.tv_sec; - ts.tv_nsec -= ts_monotonic.tv_nsec; - if (ts.tv_nsec < 0) { - ts.tv_nsec += 1000000000; - --ts.tv_sec; - } - - struct timespec ts_realtime; - clock_gettime(CLOCK_REALTIME, &ts_realtime); - ts.tv_sec += ts_realtime.tv_sec; - ts.tv_nsec += ts_realtime.tv_nsec; - if (ts.tv_nsec >= 1000000000) { - ts.tv_nsec -= 1000000000; - ++ts.tv_sec; - } - } -#endif - } else { -#if CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP - // Implementation supports relative timeouts. - int ret = - pthread_cond_timedwait_relative_np(&cond->object, &lock->object, &ts); - assert((ret == 0 || ret == ETIMEDOUT) && - "pthread_cond_timedwait_relative_np() failed"); - return ret == ETIMEDOUT; -#else - // Convert to absolute timeout. - struct timespec ts_now; -#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK - clock_gettime(cond->clock, &ts_now); -#else - clock_gettime(CLOCK_REALTIME, &ts_now); -#endif - ts.tv_sec += ts_now.tv_sec; - ts.tv_nsec += ts_now.tv_nsec; - if (ts.tv_nsec >= 1000000000) { - ts.tv_nsec -= 1000000000; - ++ts.tv_sec; - } -#endif - } - - int ret = pthread_cond_timedwait(&cond->object, &lock->object, &ts); - assert((ret == 0 || ret == ETIMEDOUT) && "pthread_cond_timedwait() failed"); - return ret == ETIMEDOUT; -} - -static inline void cond_wait(struct cond *cond, struct mutex *lock) - REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { - pthread_cond_wait(&cond->object, &lock->object); -} - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/numeric_limits.h b/wasmtime-wasi/sandboxed-system-primitives/src/numeric_limits.h deleted file mode 100644 index 30dddd19aae3..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/numeric_limits.h +++ /dev/null @@ -1,42 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2015 Nuxi, https://nuxi.nl/ - -#ifndef COMMON_LIMITS_H -#define COMMON_LIMITS_H - -#include - -#define NUMERIC_MIN(t) \ - _Generic((t)0, char \ - : CHAR_MIN, signed char \ - : SCHAR_MIN, unsigned char : 0, short \ - : SHRT_MIN, unsigned short : 0, int \ - : INT_MIN, unsigned int : 0, long \ - : LONG_MIN, unsigned long : 0, long long \ - : LLONG_MIN, unsigned long long : 0, default \ - : (void)0) - -#define NUMERIC_MAX(t) \ - _Generic((t)0, char \ - : CHAR_MAX, signed char \ - : SCHAR_MAX, unsigned char \ - : UCHAR_MAX, short \ - : SHRT_MAX, unsigned short \ - : USHRT_MAX, int \ - : INT_MAX, unsigned int \ - : UINT_MAX, long \ - : LONG_MAX, unsigned long \ - : ULONG_MAX, long long \ - : LLONG_MAX, unsigned long long \ - : ULLONG_MAX, default \ - : (void)0) - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c deleted file mode 100644 index 1aa533f3b56b..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c +++ /dev/null @@ -1,2724 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/ - -#include "config.h" - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "locking.h" -#include "numeric_limits.h" -#include "posix.h" -#include "random.h" -#include "refcount.h" -#include "rights.h" -#include "str.h" - -// static_assert is not defined in older compilers. -#ifdef static_assert - -// struct iovec must have the same layout as __wasi_iovec_t. -static_assert(offsetof(struct iovec, iov_base) == - offsetof(__wasi_iovec_t, buf), - "Offset mismatch"); -static_assert(sizeof(((struct iovec *)0)->iov_base) == - sizeof(((__wasi_iovec_t *)0)->buf), - "Size mismatch"); -static_assert(offsetof(struct iovec, iov_len) == - offsetof(__wasi_iovec_t, buf_len), - "Offset mismatch"); -static_assert(sizeof(((struct iovec *)0)->iov_len) == - sizeof(((__wasi_iovec_t *)0)->buf_len), - "Size mismatch"); -static_assert(sizeof(struct iovec) == sizeof(__wasi_iovec_t), - "Size mismatch"); - -// struct iovec must have the same layout as __wasi_ciovec_t. -static_assert(offsetof(struct iovec, iov_base) == - offsetof(__wasi_ciovec_t, buf), - "Offset mismatch"); -static_assert(sizeof(((struct iovec *)0)->iov_base) == - sizeof(((__wasi_ciovec_t *)0)->buf), - "Size mismatch"); -static_assert(offsetof(struct iovec, iov_len) == - offsetof(__wasi_ciovec_t, buf_len), - "Offset mismatch"); -static_assert(sizeof(((struct iovec *)0)->iov_len) == - sizeof(((__wasi_ciovec_t *)0)->buf_len), - "Size mismatch"); -static_assert(sizeof(struct iovec) == sizeof(__wasi_ciovec_t), - "Size mismatch"); - -#endif - -#if defined(WASMTIME_SSP_STATIC_CURFDS) -static __thread struct fd_table *curfds; -static __thread struct fd_prestats *prestats; -static __thread struct argv_environ_values *argv_environ; -#endif - -// Converts a POSIX error code to a CloudABI error code. -static __wasi_errno_t convert_errno(int error) { - static const __wasi_errno_t errors[] = { -#define X(v) [v] = __WASI_##v - X(E2BIG), - X(EACCES), - X(EADDRINUSE), - X(EADDRNOTAVAIL), - X(EAFNOSUPPORT), - X(EAGAIN), - X(EALREADY), - X(EBADF), - X(EBADMSG), - X(EBUSY), - X(ECANCELED), - X(ECHILD), - X(ECONNABORTED), - X(ECONNREFUSED), - X(ECONNRESET), - X(EDEADLK), - X(EDESTADDRREQ), - X(EDOM), - X(EDQUOT), - X(EEXIST), - X(EFAULT), - X(EFBIG), - X(EHOSTUNREACH), - X(EIDRM), - X(EILSEQ), - X(EINPROGRESS), - X(EINTR), - X(EINVAL), - X(EIO), - X(EISCONN), - X(EISDIR), - X(ELOOP), - X(EMFILE), - X(EMLINK), - X(EMSGSIZE), - X(EMULTIHOP), - X(ENAMETOOLONG), - X(ENETDOWN), - X(ENETRESET), - X(ENETUNREACH), - X(ENFILE), - X(ENOBUFS), - X(ENODEV), - X(ENOENT), - X(ENOEXEC), - X(ENOLCK), - X(ENOLINK), - X(ENOMEM), - X(ENOMSG), - X(ENOPROTOOPT), - X(ENOSPC), - X(ENOSYS), -#ifdef ENOTCAPABLE - X(ENOTCAPABLE), -#endif - X(ENOTCONN), - X(ENOTDIR), - X(ENOTEMPTY), - X(ENOTRECOVERABLE), - X(ENOTSOCK), - X(ENOTSUP), - X(ENOTTY), - X(ENXIO), - X(EOVERFLOW), - X(EOWNERDEAD), - X(EPERM), - X(EPIPE), - X(EPROTO), - X(EPROTONOSUPPORT), - X(EPROTOTYPE), - X(ERANGE), - X(EROFS), - X(ESPIPE), - X(ESRCH), - X(ESTALE), - X(ETIMEDOUT), - X(ETXTBSY), - X(EXDEV), -#undef X -#if EOPNOTSUPP != ENOTSUP - [EOPNOTSUPP] = __WASI_ENOTSUP, -#endif -#if EWOULDBLOCK != EAGAIN - [EWOULDBLOCK] = __WASI_EAGAIN, -#endif - }; - if (error < 0 || (size_t)error >= sizeof(errors) / sizeof(errors[0]) || - errors[error] == 0) - return __WASI_ENOSYS; - return errors[error]; -} - -// Converts a POSIX timespec to a CloudABI timestamp. -static __wasi_timestamp_t convert_timespec( - const struct timespec *ts -) { - if (ts->tv_sec < 0) - return 0; - if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / 1000000000) - return UINT64_MAX; - return (__wasi_timestamp_t)ts->tv_sec * 1000000000 + ts->tv_nsec; -} - -// Converts a CloudABI clock identifier to a POSIX clock identifier. -static bool convert_clockid( - __wasi_clockid_t in, - clockid_t *out -) { - switch (in) { - case __WASI_CLOCK_MONOTONIC: - *out = CLOCK_MONOTONIC; - return true; - case __WASI_CLOCK_PROCESS_CPUTIME_ID: - *out = CLOCK_PROCESS_CPUTIME_ID; - return true; - case __WASI_CLOCK_REALTIME: - *out = CLOCK_REALTIME; - return true; - case __WASI_CLOCK_THREAD_CPUTIME_ID: - *out = CLOCK_THREAD_CPUTIME_ID; - return true; - default: - return false; - } -} - -__wasi_errno_t wasmtime_ssp_clock_res_get( - __wasi_clockid_t clock_id, - __wasi_timestamp_t *resolution -) { - clockid_t nclock_id; - if (!convert_clockid(clock_id, &nclock_id)) - return __WASI_EINVAL; - struct timespec ts; - if (clock_getres(nclock_id, &ts) < 0) - return convert_errno(errno); - *resolution = convert_timespec(&ts); - return 0; -} - -__wasi_errno_t wasmtime_ssp_clock_time_get( - __wasi_clockid_t clock_id, - __wasi_timestamp_t precision, - __wasi_timestamp_t *time -) { - clockid_t nclock_id; - if (!convert_clockid(clock_id, &nclock_id)) - return __WASI_EINVAL; - struct timespec ts; - if (clock_gettime(nclock_id, &ts) < 0) - return convert_errno(errno); - *time = convert_timespec(&ts); - return 0; -} - -void fd_prestats_init( - struct fd_prestats *pt -) { - rwlock_init(&pt->lock); - pt->prestats = NULL; - pt->size = 0; - pt->used = 0; -#if defined(WASMTIME_SSP_STATIC_CURFDS) - prestats = pt; -#endif -} - -// Grows the preopened resource table to a required lower bound and a -// minimum number of free preopened resource table entries. -static bool fd_prestats_grow( - struct fd_prestats *pt, - size_t min, - size_t incr -) REQUIRES_EXCLUSIVE(pt->lock) { - if (pt->size <= min || pt->size < (pt->used + incr) * 2) { - // Keep on doubling the table size until we've met our constraints. - size_t size = pt->size == 0 ? 1 : pt->size; - while (size <= min || size < (pt->used + incr) * 2) - size *= 2; - - // Grow the file descriptor table's allocation. - struct fd_prestat *prestats = realloc(pt->prestats, sizeof(*prestats) * size); - if (prestats == NULL) - return false; - - // Mark all new file descriptors as unused. - for (size_t i = pt->size; i < size; ++i) { - prestats[i].dir_name = NULL; - prestats[i].dir_name_len = 0; - } - pt->prestats = prestats; - pt->size = size; - } - return true; -} - -// Inserts a preopened resource record into the preopened resource table. -bool fd_prestats_insert( - struct fd_prestats *pt, - const char *dir, - __wasi_fd_t fd -) { - // Grow the preopened resource table if needed. - rwlock_wrlock(&pt->lock); - if (!fd_prestats_grow(pt, fd, 1)) { - rwlock_unlock(&pt->lock); - return false; - } - - pt->prestats[fd].dir_name = strdup(dir); - pt->prestats[fd].dir_name_len = strlen(dir); - rwlock_unlock(&pt->lock); - return true; -} - -// Looks up a preopened resource table entry by number. -static __wasi_errno_t fd_prestats_get_entry( - struct fd_prestats *pt, - __wasi_fd_t fd, - struct fd_prestat **ret -) REQUIRES_SHARED(pt->lock) { - // Test for file descriptor existence. - if (fd >= pt->size) - return __WASI_EBADF; - struct fd_prestat *prestat = &pt->prestats[fd]; - if (prestat->dir_name == NULL) - return __WASI_EBADF; - - *ret = prestat; - return 0; -} - -struct fd_object { - struct refcount refcount; - __wasi_filetype_t type; - int number; - - union { - // Data associated with directory file descriptors. - struct { - struct mutex lock; // Lock to protect members below. - DIR *handle; // Directory handle. - __wasi_dircookie_t offset; // Offset of the directory. - } directory; - }; -}; - -struct fd_entry { - struct fd_object *object; - __wasi_rights_t rights_base; - __wasi_rights_t rights_inheriting; -}; - -void fd_table_init( - struct fd_table *ft -) { - rwlock_init(&ft->lock); - ft->entries = NULL; - ft->size = 0; - ft->used = 0; -#if defined(WASMTIME_SSP_STATIC_CURFDS) - curfds = ft; -#endif -} - -// Looks up a file descriptor table entry by number and required rights. -static __wasi_errno_t fd_table_get_entry( - struct fd_table *ft, - __wasi_fd_t fd, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting, - struct fd_entry **ret -) REQUIRES_SHARED(ft->lock) { - // Test for file descriptor existence. - if (fd >= ft->size) - return __WASI_EBADF; - struct fd_entry *fe = &ft->entries[fd]; - if (fe->object == NULL) - return __WASI_EBADF; - - // Validate rights. - if ((~fe->rights_base & rights_base) != 0 || - (~fe->rights_inheriting & rights_inheriting) != 0) - return __WASI_ENOTCAPABLE; - *ret = fe; - return 0; -} - -// Grows the file descriptor table to a required lower bound and a -// minimum number of free file descriptor table entries. -static bool fd_table_grow( - struct fd_table *ft, - size_t min, - size_t incr -) REQUIRES_EXCLUSIVE(ft->lock) { - if (ft->size <= min || ft->size < (ft->used + incr) * 2) { - // Keep on doubling the table size until we've met our constraints. - size_t size = ft->size == 0 ? 1 : ft->size; - while (size <= min || size < (ft->used + incr) * 2) - size *= 2; - - // Grow the file descriptor table's allocation. - struct fd_entry *entries = realloc(ft->entries, sizeof(*entries) * size); - if (entries == NULL) - return false; - - // Mark all new file descriptors as unused. - for (size_t i = ft->size; i < size; ++i) - entries[i].object = NULL; - ft->entries = entries; - ft->size = size; - } - return true; -} - -// Allocates a new file descriptor object. -static __wasi_errno_t fd_object_new( - __wasi_filetype_t type, - struct fd_object **fo -) TRYLOCKS_SHARED(0, (*fo)->refcount) { - *fo = malloc(sizeof(**fo)); - if (*fo == NULL) - return __WASI_ENOMEM; - refcount_init(&(*fo)->refcount, 1); - (*fo)->type = type; - (*fo)->number = -1; - return 0; -} - -// Attaches a file descriptor to the file descriptor table. -static void fd_table_attach( - struct fd_table *ft, - __wasi_fd_t fd, - struct fd_object *fo, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting -) REQUIRES_EXCLUSIVE(ft->lock) CONSUMES(fo->refcount) { - assert(ft->size > fd && "File descriptor table too small"); - struct fd_entry *fe = &ft->entries[fd]; - assert(fe->object == NULL && "Attempted to overwrite an existing descriptor"); - fe->object = fo; - fe->rights_base = rights_base; - fe->rights_inheriting = rights_inheriting; - ++ft->used; - assert(ft->size >= ft->used * 2 && "File descriptor too full"); -} - -// Detaches a file descriptor from the file descriptor table. -static void fd_table_detach( - struct fd_table *ft, - __wasi_fd_t fd, - struct fd_object **fo -) REQUIRES_EXCLUSIVE(ft->lock) PRODUCES((*fo)->refcount) { - assert(ft->size > fd && "File descriptor table too small"); - struct fd_entry *fe = &ft->entries[fd]; - *fo = fe->object; - assert(*fo != NULL && "Attempted to detach nonexistent descriptor"); - fe->object = NULL; - assert(ft->used > 0 && "Reference count mismatch"); - --ft->used; -} - -// Determines the type of a file descriptor and its maximum set of -// rights that should be attached to it. -static __wasi_errno_t fd_determine_type_rights( - int fd, - __wasi_filetype_t *type, - __wasi_rights_t *rights_base, - __wasi_rights_t *rights_inheriting -) { - struct stat sb; - if (fstat(fd, &sb) < 0) - return convert_errno(errno); - if (S_ISBLK(sb.st_mode)) { - *type = __WASI_FILETYPE_BLOCK_DEVICE; - *rights_base = RIGHTS_BLOCK_DEVICE_BASE; - *rights_inheriting = RIGHTS_BLOCK_DEVICE_INHERITING; - } else if (S_ISCHR(sb.st_mode)) { - *type = __WASI_FILETYPE_CHARACTER_DEVICE; -#if CONFIG_HAS_ISATTY - if (isatty(fd)) { - *rights_base = RIGHTS_TTY_BASE; - *rights_inheriting = RIGHTS_TTY_INHERITING; - } else -#endif - { - *rights_base = RIGHTS_CHARACTER_DEVICE_BASE; - *rights_inheriting = RIGHTS_CHARACTER_DEVICE_INHERITING; - } - } else if (S_ISDIR(sb.st_mode)) { - *type = __WASI_FILETYPE_DIRECTORY; - *rights_base = RIGHTS_DIRECTORY_BASE; - *rights_inheriting = RIGHTS_DIRECTORY_INHERITING; - } else if (S_ISREG(sb.st_mode)) { - *type = __WASI_FILETYPE_REGULAR_FILE; - *rights_base = RIGHTS_REGULAR_FILE_BASE; - *rights_inheriting = RIGHTS_REGULAR_FILE_INHERITING; - } else if (S_ISSOCK(sb.st_mode)) { - int socktype; - socklen_t socktypelen = sizeof(socktype); - if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &socktype, &socktypelen) < 0) - return convert_errno(errno); - switch (socktype) { - case SOCK_DGRAM: - *type = __WASI_FILETYPE_SOCKET_DGRAM; - break; - case SOCK_STREAM: - *type = __WASI_FILETYPE_SOCKET_STREAM; - break; - default: - return __WASI_EINVAL; - } - *rights_base = RIGHTS_SOCKET_BASE; - *rights_inheriting = RIGHTS_SOCKET_INHERITING; - } else if (S_ISFIFO(sb.st_mode)) { - *type = __WASI_FILETYPE_SOCKET_STREAM; - *rights_base = RIGHTS_SOCKET_BASE; - *rights_inheriting = RIGHTS_SOCKET_INHERITING; - } else { - return __WASI_EINVAL; - } - - // Strip off read/write bits based on the access mode. - switch (fcntl(fd, F_GETFL) & O_ACCMODE) { - case O_RDONLY: - *rights_base &= ~__WASI_RIGHT_FD_WRITE; - break; - case O_WRONLY: - *rights_base &= ~__WASI_RIGHT_FD_READ; - break; - } - return 0; -} - -// Returns the underlying file descriptor number of a file descriptor -// object. This function can only be applied to objects that have an -// underlying file descriptor number. -static int fd_number( - const struct fd_object *fo -) { - int number = fo->number; - assert(number >= 0 && "fd_number() called on virtual file descriptor"); - return number; -} - -// Lowers the reference count on a file descriptor object. When the -// reference count reaches zero, its resources are cleaned up. -static void fd_object_release( - struct fd_object *fo -) UNLOCKS(fo->refcount) { - if (refcount_release(&fo->refcount)) { - switch (fo->type) { - case __WASI_FILETYPE_DIRECTORY: - // For directories we may keep track of a DIR object. Calling - // closedir() on it also closes the underlying file descriptor. - mutex_destroy(&fo->directory.lock); - if (fo->directory.handle == NULL) { - close(fd_number(fo)); - } else { - closedir(fo->directory.handle); - } - break; - default: - close(fd_number(fo)); - break; - } - free(fo); - } -} - -// Inserts an already existing file descriptor into the file descriptor -// table. -bool fd_table_insert_existing( - struct fd_table *ft, - __wasi_fd_t in, - int out -) { - __wasi_filetype_t type; - __wasi_rights_t rights_base, rights_inheriting; - if (fd_determine_type_rights(out, &type, &rights_base, &rights_inheriting) != - 0) - return false; - - struct fd_object *fo; - __wasi_errno_t error = fd_object_new(type, &fo); - if (error != 0) - return false; - fo->number = out; - if (type == __WASI_FILETYPE_DIRECTORY) { - mutex_init(&fo->directory.lock); - fo->directory.handle = NULL; - } - - // Grow the file descriptor table if needed. - rwlock_wrlock(&ft->lock); - if (!fd_table_grow(ft, in, 1)) { - rwlock_unlock(&ft->lock); - fd_object_release(fo); - return false; - } - - fd_table_attach(ft, in, fo, rights_base, rights_inheriting); - rwlock_unlock(&ft->lock); - return true; -} - -// Picks an unused slot from the file descriptor table. -static __wasi_fd_t fd_table_unused( - struct fd_table *ft -) REQUIRES_SHARED(ft->lock) { - assert(ft->size > ft->used && "File descriptor table has no free slots"); - for (;;) { - __wasi_fd_t fd = random_uniform(ft->size); - if (ft->entries[fd].object == NULL) - return fd; - } -} - -// Inserts a file descriptor object into an unused slot of the file -// descriptor table. -static __wasi_errno_t fd_table_insert( - struct fd_table *ft, - struct fd_object *fo, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting, - __wasi_fd_t *out -) REQUIRES_UNLOCKED(ft->lock) UNLOCKS(fo->refcount) { - // Grow the file descriptor table if needed. - rwlock_wrlock(&ft->lock); - if (!fd_table_grow(ft, 0, 1)) { - rwlock_unlock(&ft->lock); - fd_object_release(fo); - return convert_errno(errno); - } - - *out = fd_table_unused(ft); - fd_table_attach(ft, *out, fo, rights_base, rights_inheriting); - rwlock_unlock(&ft->lock); - return 0; -} - -// Inserts a numerical file descriptor into the file descriptor table. -static __wasi_errno_t fd_table_insert_fd( - struct fd_table *ft, - int in, - __wasi_filetype_t type, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting, - __wasi_fd_t *out -) REQUIRES_UNLOCKED(ft->lock) { - struct fd_object *fo; - __wasi_errno_t error = fd_object_new(type, &fo); - if (error != 0) { - close(in); - return error; - } - fo->number = in; - if (type == __WASI_FILETYPE_DIRECTORY) { - mutex_init(&fo->directory.lock); - fo->directory.handle = NULL; - } - return fd_table_insert(ft, fo, rights_base, rights_inheriting, out); -} - -__wasi_errno_t wasmtime_ssp_fd_close( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, - struct fd_prestats *prestats, -#endif - __wasi_fd_t fd -) { - // Don't allow closing a pre-opened resource. - // TODO: Eventually, we do want to permit this, once libpreopen in - // userspace is capable of removing entries from its tables as well. - { - rwlock_rdlock(&prestats->lock); - struct fd_prestat *prestat; - __wasi_errno_t error = fd_prestats_get_entry(prestats, fd, &prestat); - rwlock_unlock(&prestats->lock); - if (error == 0) { - return __WASI_ENOTSUP; - } - } - - // Validate the file descriptor. - struct fd_table *ft = curfds; - rwlock_wrlock(&ft->lock); - struct fd_entry *fe; - __wasi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe); - if (error != 0) { - rwlock_unlock(&ft->lock); - return error; - } - - // Remove it from the file descriptor table. - struct fd_object *fo; - fd_table_detach(ft, fd, &fo); - rwlock_unlock(&ft->lock); - fd_object_release(fo); - return 0; -} - -// Look up a file descriptor object in a locked file descriptor table -// and increases its reference count. -static __wasi_errno_t fd_object_get_locked( - struct fd_object **fo, - struct fd_table *ft, - __wasi_fd_t fd, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting -) TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) REQUIRES_EXCLUSIVE(ft->lock) { - // Test whether the file descriptor number is valid. - struct fd_entry *fe; - __wasi_errno_t error = - fd_table_get_entry(ft, fd, rights_base, rights_inheriting, &fe); - if (error != 0) - return error; - - // Increase the reference count on the file descriptor object. A copy - // of the rights are also stored, so callers can still access those if - // needed. - *fo = fe->object; - refcount_acquire(&(*fo)->refcount); - return 0; -} - -// Temporarily locks the file descriptor table to look up a file -// descriptor object, increases its reference count and drops the lock. -static __wasi_errno_t fd_object_get( - struct fd_table *curfds, - struct fd_object **fo, - __wasi_fd_t fd, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting -) TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) { - struct fd_table *ft = curfds; - rwlock_rdlock(&ft->lock); - __wasi_errno_t error = - fd_object_get_locked(fo, ft, fd, rights_base, rights_inheriting); - rwlock_unlock(&ft->lock); - return error; -} - -__wasi_errno_t wasmtime_ssp_fd_datasync( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd -) { - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_DATASYNC, 0); - if (error != 0) - return error; - -#if CONFIG_HAS_FDATASYNC - int ret = fdatasync(fd_number(fo)); -#else - int ret = fsync(fd_number(fo)); -#endif - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_pread( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_iovec_t *iov, - size_t iovcnt, - __wasi_filesize_t offset, - size_t *nread -) { - if (iovcnt == 0) - return __WASI_EINVAL; - - struct fd_object *fo; - __wasi_errno_t error = fd_object_get(curfds, - &fo, fd, __WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_SEEK, 0); - if (error != 0) - return error; - -#if CONFIG_HAS_PREADV - ssize_t len = - preadv(fd_number(fo), (const struct iovec *)iov, iovcnt, offset); - fd_object_release(fo); - if (len < 0) - return convert_errno(errno); - *nread = len; - return 0; -#else - if (iovcnt == 1) { - ssize_t len = pread(fd_number(fo), iov->buf, iov->buf_len, offset); - fd_object_release(fo); - if (len < 0) - return convert_errno(errno); - *nread = len; - return 0; - } else { - // Allocate a single buffer to fit all data. - size_t totalsize = 0; - for (size_t i = 0; i < iovcnt; ++i) - totalsize += iov[i].buf_len; - char *buf = malloc(totalsize); - if (buf == NULL) { - fd_object_release(fo); - return __WASI_ENOMEM; - } - - // Perform a single read operation. - ssize_t len = pread(fd_number(fo), buf, totalsize, offset); - fd_object_release(fo); - if (len < 0) { - free(buf); - return convert_errno(errno); - } - - // Copy data back to vectors. - size_t bufoff = 0; - for (size_t i = 0; i < iovcnt; ++i) { - if (bufoff + iov[i].buf_len < len) { - memcpy(iov[i].buf, buf + bufoff, iov[i].buf_len); - bufoff += iov[i].buf_len; - } else { - memcpy(iov[i].buf, buf + bufoff, len - bufoff); - break; - } - } - free(buf); - *nread = len; - return 0; - } -#endif -} - -__wasi_errno_t wasmtime_ssp_fd_pwrite( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_ciovec_t *iov, - size_t iovcnt, - __wasi_filesize_t offset, - size_t *nwritten -) { - if (iovcnt == 0) - return __WASI_EINVAL; - - struct fd_object *fo; - __wasi_errno_t error = fd_object_get(curfds, - &fo, fd, __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_SEEK, 0); - if (error != 0) - return error; - - ssize_t len; -#if CONFIG_HAS_PWRITEV - len = pwritev(fd_number(fo), (const struct iovec *)iov, iovcnt, offset); -#else - if (iovcnt == 1) { - len = pwrite(fd_number(fo), iov->buf, iov->buf_len, offset); - } else { - // Allocate a single buffer to fit all data. - size_t totalsize = 0; - for (size_t i = 0; i < iovcnt; ++i) - totalsize += iov[i].buf_len; - char *buf = malloc(totalsize); - if (buf == NULL) { - fd_object_release(fo); - return __WASI_ENOMEM; - } - size_t bufoff = 0; - for (size_t i = 0; i < iovcnt; ++i) { - memcpy(buf + bufoff, iov[i].buf, iov[i].buf_len); - bufoff += iov[i].buf_len; - } - - // Perform a single write operation. - len = pwrite(fd_number(fo), buf, totalsize, offset); - free(buf); - } -#endif - fd_object_release(fo); - if (len < 0) - return convert_errno(errno); - *nwritten = len; - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_read( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_iovec_t *iov, - size_t iovcnt, - size_t *nread -) { - struct fd_object *fo; - __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_READ, 0); - if (error != 0) - return error; - - ssize_t len = readv(fd_number(fo), (const struct iovec *)iov, iovcnt); - fd_object_release(fo); - if (len < 0) - return convert_errno(errno); - *nread = len; - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_renumber( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, - struct fd_prestats *prestats, -#endif - __wasi_fd_t from, - __wasi_fd_t to -) { - // Don't allow renumbering over a pre-opened resource. - // TODO: Eventually, we do want to permit this, once libpreopen in - // userspace is capable of removing entries from its tables as well. - { - rwlock_rdlock(&prestats->lock); - struct fd_prestat *prestat; - __wasi_errno_t error = fd_prestats_get_entry(prestats, to, &prestat); - if (error != 0) { - error = fd_prestats_get_entry(prestats, from, &prestat); - } - rwlock_unlock(&prestats->lock); - if (error == 0) { - return __WASI_ENOTSUP; - } - } - - struct fd_table *ft = curfds; - rwlock_wrlock(&ft->lock); - struct fd_entry *fe_from; - __wasi_errno_t error = fd_table_get_entry(ft, from, 0, 0, &fe_from); - if (error != 0) { - rwlock_unlock(&ft->lock); - return error; - } - struct fd_entry *fe_to; - error = fd_table_get_entry(ft, to, 0, 0, &fe_to); - if (error != 0) { - rwlock_unlock(&ft->lock); - return error; - } - - struct fd_object *fo; - fd_table_detach(ft, to, &fo); - refcount_acquire(&fe_from->object->refcount); - fd_table_attach(ft, to, fe_from->object, fe_from->rights_base, - fe_from->rights_inheriting); - rwlock_unlock(&ft->lock); - fd_object_release(fo); - - // Remove the old fd from the file descriptor table. - fd_table_detach(ft, from, &fo); - fd_object_release(fo); - --ft->used; - - rwlock_unlock(&ft->lock); - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_seek( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filedelta_t offset, - __wasi_whence_t whence, - __wasi_filesize_t *newoffset -) { - int nwhence; - switch (whence) { - case __WASI_WHENCE_CUR: - nwhence = SEEK_CUR; - break; - case __WASI_WHENCE_END: - nwhence = SEEK_END; - break; - case __WASI_WHENCE_SET: - nwhence = SEEK_SET; - break; - default: - return __WASI_EINVAL; - } - - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, - offset == 0 && whence == __WASI_WHENCE_CUR - ? __WASI_RIGHT_FD_TELL - : __WASI_RIGHT_FD_SEEK | __WASI_RIGHT_FD_TELL, - 0); - if (error != 0) - return error; - - off_t ret = lseek(fd_number(fo), offset, nwhence); - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - *newoffset = ret; - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_tell( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t *newoffset -) { - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_TELL, 0); - if (error != 0) - return error; - - off_t ret = lseek(fd_number(fo), 0, SEEK_CUR); - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - *newoffset = ret; - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_fdstat_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_fdstat_t *buf -) { - struct fd_table *ft = curfds; - rwlock_rdlock(&ft->lock); - struct fd_entry *fe; - __wasi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe); - if (error != 0) { - rwlock_unlock(&ft->lock); - return error; - } - - // Extract file descriptor type and rights. - struct fd_object *fo = fe->object; - *buf = (__wasi_fdstat_t){ - .fs_filetype = fo->type, - .fs_rights_base = fe->rights_base, - .fs_rights_inheriting = fe->rights_inheriting, - }; - - // Fetch file descriptor flags. - int ret; - switch (fo->type) { - default: - ret = fcntl(fd_number(fo), F_GETFL); - break; - } - rwlock_unlock(&ft->lock); - if (ret < 0) - return convert_errno(errno); - - if ((ret & O_APPEND) != 0) - buf->fs_flags |= __WASI_FDFLAG_APPEND; -#ifdef O_DSYNC - if ((ret & O_DSYNC) != 0) - buf->fs_flags |= __WASI_FDFLAG_DSYNC; -#endif - if ((ret & O_NONBLOCK) != 0) - buf->fs_flags |= __WASI_FDFLAG_NONBLOCK; -#ifdef O_RSYNC - if ((ret & O_RSYNC) != 0) - buf->fs_flags |= __WASI_FDFLAG_RSYNC; -#endif - if ((ret & O_SYNC) != 0) - buf->fs_flags |= __WASI_FDFLAG_SYNC; - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_fdstat_set_flags( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_fdflags_t fs_flags -) { - int noflags = 0; - if ((fs_flags & __WASI_FDFLAG_APPEND) != 0) - noflags |= O_APPEND; - if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) -#ifdef O_DSYNC - noflags |= O_DSYNC; -#else - noflags |= O_SYNC; -#endif - if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0) - noflags |= O_NONBLOCK; - if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) -#ifdef O_RSYNC - noflags |= O_RSYNC; -#else - noflags |= O_SYNC; -#endif - if ((fs_flags & __WASI_FDFLAG_SYNC) != 0) - noflags |= O_SYNC; - - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FDSTAT_SET_FLAGS, 0); - if (error != 0) - return error; - - int ret = fcntl(fd_number(fo), F_SETFL, noflags); - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_fdstat_set_rights( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_rights_t fs_rights_base, - __wasi_rights_t fs_rights_inheriting -) { - struct fd_table *ft = curfds; - rwlock_wrlock(&ft->lock); - struct fd_entry *fe; - __wasi_errno_t error = - fd_table_get_entry(ft, fd, fs_rights_base, fs_rights_inheriting, &fe); - if (error != 0) { - rwlock_unlock(&ft->lock); - return error; - } - - // Restrict the rights on the file descriptor. - fe->rights_base = fs_rights_base; - fe->rights_inheriting = fs_rights_inheriting; - rwlock_unlock(&ft->lock); - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_sync( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd -) { - struct fd_object *fo; - __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_SYNC, 0); - if (error != 0) - return error; - - int ret = fsync(fd_number(fo)); - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_write( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const __wasi_ciovec_t *iov, - size_t iovcnt, - size_t *nwritten -) { - struct fd_object *fo; - __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_WRITE, 0); - if (error != 0) - return error; - - ssize_t len = writev(fd_number(fo), (const struct iovec *)iov, iovcnt); - fd_object_release(fo); - if (len < 0) - return convert_errno(errno); - *nwritten = len; - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_advise( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t offset, - __wasi_filesize_t len, - __wasi_advice_t advice -) { -#ifdef POSIX_FADV_NORMAL - int nadvice; - switch (advice) { - case __WASI_ADVICE_DONTNEED: - nadvice = POSIX_FADV_DONTNEED; - break; - case __WASI_ADVICE_NOREUSE: - nadvice = POSIX_FADV_NOREUSE; - break; - case __WASI_ADVICE_NORMAL: - nadvice = POSIX_FADV_NORMAL; - break; - case __WASI_ADVICE_RANDOM: - nadvice = POSIX_FADV_RANDOM; - break; - case __WASI_ADVICE_SEQUENTIAL: - nadvice = POSIX_FADV_SEQUENTIAL; - break; - case __WASI_ADVICE_WILLNEED: - nadvice = POSIX_FADV_WILLNEED; - break; - default: - return __WASI_EINVAL; - } - - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_ADVISE, 0); - if (error != 0) - return error; - - int ret = posix_fadvise(fd_number(fo), offset, len, nadvice); - fd_object_release(fo); - if (ret != 0) - return convert_errno(ret); - return 0; -#else - // Advisory information can safely be ignored if unsupported. - switch (advice) { - case __WASI_ADVICE_DONTNEED: - case __WASI_ADVICE_NOREUSE: - case __WASI_ADVICE_NORMAL: - case __WASI_ADVICE_RANDOM: - case __WASI_ADVICE_SEQUENTIAL: - case __WASI_ADVICE_WILLNEED: - break; - default: - return __WASI_EINVAL; - } - - // At least check for file descriptor existence. - struct fd_table *ft = curfds; - rwlock_rdlock(&ft->lock); - struct fd_entry *fe; - __wasi_errno_t error = - fd_table_get_entry(ft, fd, __WASI_RIGHT_FD_ADVISE, 0, &fe); - rwlock_unlock(&ft->lock); - return error; -#endif -} - -__wasi_errno_t wasmtime_ssp_fd_allocate( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t offset, - __wasi_filesize_t len -) { - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_ALLOCATE, 0); - if (error != 0) - return error; - -#if CONFIG_HAS_POSIX_FALLOCATE - int ret = posix_fallocate(fd_number(fo), offset, len); -#else - // At least ensure that the file is grown to the right size. - // TODO(ed): See if this can somehow be implemented without any race - // conditions. We may end up shrinking the file right now. - struct stat sb; - int ret = fstat(fd_number(fo), &sb); - if (ret == 0 && sb.st_size < offset + len) - ret = ftruncate(fd_number(fo), offset + len); -#endif - - fd_object_release(fo); - if (ret != 0) - return convert_errno(ret); - return 0; -} - -// Reads the entire contents of a symbolic link, returning the contents -// in an allocated buffer. The allocated buffer is large enough to fit -// at least one extra byte, so the caller may append a trailing slash to -// it. This is needed by path_get(). -static char *readlinkat_dup( - int fd, - const char *path -) { - char *buf = NULL; - size_t len = 32; - for (;;) { - char *newbuf = realloc(buf, len); - if (newbuf == NULL) { - free(buf); - return NULL; - } - buf = newbuf; - ssize_t ret = readlinkat(fd, path, buf, len); - if (ret < 0) { - free(buf); - return NULL; - } - if ((size_t)ret + 1 < len) { - buf[ret] = '\0'; - return buf; - } - len *= 2; - } -} - -// Lease to a directory, so a path underneath it can be accessed. -// -// This structure is used by system calls that operate on pathnames. In -// this environment, pathnames always consist of a pair of a file -// descriptor representing the directory where the lookup needs to start -// and the actual pathname string. -struct path_access { - int fd; // Directory file descriptor. - const char *path; // Pathname. - bool follow; // Whether symbolic links should be followed. - char *path_start; // Internal: pathname to free. - struct fd_object *fd_object; // Internal: directory file descriptor object. -}; - -// Creates a lease to a file descriptor and pathname pair. If the -// operating system does not implement Capsicum, it also normalizes the -// pathname to ensure the target path is placed underneath the -// directory. -static __wasi_errno_t path_get( - struct fd_table *curfds, - struct path_access *pa, - __wasi_fd_t fd, - __wasi_lookupflags_t flags, - const char *upath, - size_t upathlen, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting, - bool needs_final_component -) TRYLOCKS_EXCLUSIVE(0, pa->fd_object->refcount) { - char *path = str_nullterminate(upath, upathlen); - if (path == NULL) - return convert_errno(errno); - - // Fetch the directory file descriptor. - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, rights_base, rights_inheriting); - if (error != 0) { - free(path); - return error; - } - -#if CONFIG_HAS_CAP_ENTER - // Rely on the kernel to constrain access to automatically constrain - // access to files stored underneath this directory. - pa->fd = fd_number(fo); - pa->path = pa->path_start = path; - pa->follow = (flags & __WASI_LOOKUP_SYMLINK_FOLLOW) != 0; - pa->fd_object = fo; - return 0; -#else - // The implementation provides no mechanism to constrain lookups to a - // directory automatically. Emulate this logic by resolving the - // pathname manually. - - // Stack of directory file descriptors. Index 0 always corresponds - // with the directory provided to this function. Entering a directory - // causes a file descriptor to be pushed, while handling ".." entries - // causes an entry to be popped. Index 0 cannot be popped, as this - // would imply escaping the base directory. - int fds[128]; - fds[0] = fd_number(fo); - size_t curfd = 0; - - // Stack of pathname strings used for symlink expansion. By using a - // stack, there is no need to concatenate any pathname strings while - // expanding symlinks. - char *paths[32]; - char *paths_start[32]; - paths[0] = paths_start[0] = path; - size_t curpath = 0; - size_t expansions = 0; - - char *symlink; - for (;;) { - // Extract the next pathname component from 'paths[curpath]', null - // terminate it and store it in 'file'. 'ends_with_slashes' stores - // whether the pathname component is followed by one or more - // trailing slashes, as this requires it to be a directory. - char *file = paths[curpath]; - char *file_end = file + strcspn(file, "/"); - paths[curpath] = file_end + strspn(file_end, "/"); - bool ends_with_slashes = *file_end == '/'; - *file_end = '\0'; - - // Test for empty pathname strings and absolute paths. - if (file == file_end) { - error = ends_with_slashes ? __WASI_ENOTCAPABLE : __WASI_ENOENT; - goto fail; - } - - if (strcmp(file, ".") == 0) { - // Skip component. - } else if (strcmp(file, "..") == 0) { - // Pop a directory off the stack. - if (curfd == 0) { - // Attempted to go to parent directory of the directory file - // descriptor. - error = __WASI_ENOTCAPABLE; - goto fail; - } - close(fds[curfd--]); - } else if (curpath > 0 || *paths[curpath] != '\0' || - (ends_with_slashes && !needs_final_component)) { - // A pathname component whose name we're not interested in that is - // followed by a slash or is followed by other pathname - // components. In other words, a pathname component that must be a - // directory. First attempt to obtain a directory file descriptor - // for it. - int newdir = -#ifdef O_SEARCH - openat(fds[curfd], file, O_SEARCH | O_DIRECTORY | O_NOFOLLOW); -#else - openat(fds[curfd], file, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); -#endif - if (newdir != -1) { - // Success. Push it onto the directory stack. - if (curfd + 1 == sizeof(fds) / sizeof(fds[0])) { - close(newdir); - error = __WASI_ENAMETOOLONG; - goto fail; - } - fds[++curfd] = newdir; - } else { - // Failed to open it. Attempt symlink expansion. - if (errno != ELOOP && errno != EMLINK && errno != ENOTDIR) { - error = convert_errno(errno); - goto fail; - } - symlink = readlinkat_dup(fds[curfd], file); - if (symlink != NULL) - goto push_symlink; - - // readlink returns EINVAL if the path isn't a symlink. In that case, - // it's more informative to return ENOTDIR. - if (errno == EINVAL) - errno = ENOTDIR; - - error = convert_errno(errno); - goto fail; - } - } else { - // The final pathname component. Depending on whether it ends with - // a slash or the symlink-follow flag is set, perform symlink - // expansion. - if (ends_with_slashes || - (flags & __WASI_LOOKUP_SYMLINK_FOLLOW) != 0) { - symlink = readlinkat_dup(fds[curfd], file); - if (symlink != NULL) - goto push_symlink; - if (errno != EINVAL && errno != ENOENT) { - error = convert_errno(errno); - goto fail; - } - } - - // Not a symlink, meaning we're done. Return the filename, - // together with the directory containing this file. - // - // If the file was followed by a trailing slash, we must retain - // it, to ensure system calls properly return ENOTDIR. - // Unfortunately, this opens up a race condition, because this - // means that users of path_get() will perform symlink expansion a - // second time. There is nothing we can do to mitigate this, as - // far as I know. - if (ends_with_slashes) - *file_end = '/'; - pa->path = file; - pa->path_start = paths_start[0]; - goto success; - } - - if (*paths[curpath] == '\0') { - if (curpath == 0) { - // No further pathname components to process. We may end up here - // when called on paths like ".", "a/..", but also if the path - // had trailing slashes and the caller is not interested in the - // name of the pathname component. - free(paths_start[0]); - pa->path = "."; - pa->path_start = NULL; - goto success; - } - - // Finished expanding symlink. Continue processing along the - // original path. - free(paths_start[curpath--]); - } - continue; - - push_symlink: - // Prevent infinite loops by placing an upper limit on the number of - // symlink expansions. - if (++expansions == 128) { - free(symlink); - error = __WASI_ELOOP; - goto fail; - } - - if (*paths[curpath] == '\0') { - // The original path already finished processing. Replace it by - // this symlink entirely. - free(paths_start[curpath]); - } else if (curpath + 1 == sizeof(paths) / sizeof(paths[0])) { - // Too many nested symlinks. Stop processing. - free(symlink); - error = __WASI_ELOOP; - goto fail; - } else { - // The original path still has components left. Retain the - // components that remain, so we can process them afterwards. - ++curpath; - } - - // Append a trailing slash to the symlink if the path leading up to - // it also contained one. Otherwise we would not throw ENOTDIR if - // the target is not a directory. - if (ends_with_slashes) - strcat(symlink, "/"); - paths[curpath] = paths_start[curpath] = symlink; - } - -success: - // Return the lease. Close all directories, except the one the caller - // needs to use. - for (size_t i = 1; i < curfd; ++i) - close(fds[i]); - pa->fd = fds[curfd]; - pa->follow = false; - pa->fd_object = fo; - return 0; - -fail: - // Failure. Free all resources. - for (size_t i = 1; i <= curfd; ++i) - close(fds[i]); - for (size_t i = 0; i <= curpath; ++i) - free(paths_start[i]); - fd_object_release(fo); - return error; -#endif -} - -static __wasi_errno_t path_get_nofollow( - struct fd_table *curfds, - struct path_access *pa, - __wasi_fd_t fd, - const char *path, - size_t pathlen, - __wasi_rights_t rights_base, - __wasi_rights_t rights_inheriting, - bool needs_final_component -) TRYLOCKS_EXCLUSIVE(0, pa->fd_object->refcount) { - __wasi_lookupflags_t flags = 0; - return path_get(curfds, pa, fd, flags, path, pathlen, rights_base, rights_inheriting, - needs_final_component); -} - -static void path_put( - struct path_access *pa -) UNLOCKS(pa->fd_object->refcount) { - free(pa->path_start); - if (fd_number(pa->fd_object) != pa->fd) - close(pa->fd); - fd_object_release(pa->fd_object); -} - -__wasi_errno_t wasmtime_ssp_path_create_directory( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t pathlen -) { - struct path_access pa; - __wasi_errno_t error = - path_get_nofollow(curfds, &pa, fd, path, pathlen, - __WASI_RIGHT_PATH_CREATE_DIRECTORY, 0, true); - if (error != 0) - return error; - - int ret = mkdirat(pa.fd, pa.path, 0777); - path_put(&pa); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_link( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t old_fd, - __wasi_lookupflags_t old_flags, - const char *old_path, - size_t old_path_len, - __wasi_fd_t new_fd, - const char *new_path, - size_t new_path_len -) { - struct path_access old_pa; - __wasi_errno_t error = path_get(curfds, &old_pa, old_fd, old_flags, old_path, old_path_len, - __WASI_RIGHT_PATH_LINK_SOURCE, 0, false); - if (error != 0) - return error; - - struct path_access new_pa; - error = path_get_nofollow(curfds, &new_pa, new_fd, new_path, new_path_len, - __WASI_RIGHT_PATH_LINK_TARGET, 0, true); - if (error != 0) { - path_put(&old_pa); - return error; - } - - int ret = linkat(old_pa.fd, old_pa.path, new_pa.fd, new_pa.path, - old_pa.follow ? AT_SYMLINK_FOLLOW : 0); - if (ret < 0 && errno == ENOTSUP && !old_pa.follow) { - // OS X doesn't allow creating hardlinks to symbolic links. - // Duplicate the symbolic link instead. - char *target = readlinkat_dup(old_pa.fd, old_pa.path); - if (target != NULL) { - ret = symlinkat(target, new_pa.fd, new_pa.path); - free(target); - } - } - path_put(&old_pa); - path_put(&new_pa); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_open( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t dirfd, - __wasi_lookupflags_t dirflags, - const char *path, - size_t pathlen, - __wasi_oflags_t oflags, - __wasi_rights_t fs_rights_base, - __wasi_rights_t fs_rights_inheriting, - __wasi_fdflags_t fs_flags, - __wasi_fd_t *fd -) { - // Rights that should be installed on the new file descriptor. - __wasi_rights_t rights_base = fs_rights_base; - __wasi_rights_t rights_inheriting = fs_rights_inheriting; - - // Which open() mode should be used to satisfy the needed rights. - bool read = - (rights_base & (__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_READDIR)) != 0; - bool write = - (rights_base & (__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_WRITE | - __WASI_RIGHT_FD_ALLOCATE | - __WASI_RIGHT_FD_FILESTAT_SET_SIZE)) != 0; - int noflags = write ? read ? O_RDWR : O_WRONLY : O_RDONLY; - - // Which rights are needed on the directory file descriptor. - __wasi_rights_t needed_base = __WASI_RIGHT_PATH_OPEN; - __wasi_rights_t needed_inheriting = rights_base | rights_inheriting; - - // Convert open flags. - if ((oflags & __WASI_O_CREAT) != 0) { - noflags |= O_CREAT; - needed_base |= __WASI_RIGHT_PATH_CREATE_FILE; - } - if ((oflags & __WASI_O_DIRECTORY) != 0) - noflags |= O_DIRECTORY; - if ((oflags & __WASI_O_EXCL) != 0) - noflags |= O_EXCL; - if ((oflags & __WASI_O_TRUNC) != 0) { - noflags |= O_TRUNC; - needed_base |= __WASI_RIGHT_PATH_FILESTAT_SET_SIZE; - } - - // Convert file descriptor flags. - if ((fs_flags & __WASI_FDFLAG_APPEND) != 0) - noflags |= O_APPEND; - if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) { -#ifdef O_DSYNC - noflags |= O_DSYNC; -#else - noflags |= O_SYNC; -#endif - needed_inheriting |= __WASI_RIGHT_FD_DATASYNC; - } - if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0) - noflags |= O_NONBLOCK; - if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) { -#ifdef O_RSYNC - noflags |= O_RSYNC; -#else - noflags |= O_SYNC; -#endif - needed_inheriting |= __WASI_RIGHT_FD_SYNC; - } - if ((fs_flags & __WASI_FDFLAG_SYNC) != 0) { - noflags |= O_SYNC; - needed_inheriting |= __WASI_RIGHT_FD_SYNC; - } - if (write && (noflags & (O_APPEND | O_TRUNC)) == 0) - needed_inheriting |= __WASI_RIGHT_FD_SEEK; - - struct path_access pa; - __wasi_errno_t error = - path_get(curfds, &pa, dirfd, dirflags, path, pathlen, needed_base, needed_inheriting, - (oflags & __WASI_O_CREAT) != 0); - if (error != 0) - return error; - if (!pa.follow) - noflags |= O_NOFOLLOW; - - int nfd = openat(pa.fd, pa.path, noflags, 0666); - if (nfd < 0) { - int openat_errno = errno; - // Linux returns ENXIO instead of EOPNOTSUPP when opening a socket. - if (openat_errno == ENXIO) { - struct stat sb; - int ret = - fstatat(pa.fd, pa.path, &sb, pa.follow ? 0 : AT_SYMLINK_NOFOLLOW); - path_put(&pa); - return ret == 0 && S_ISSOCK(sb.st_mode) ? __WASI_ENOTSUP - : __WASI_ENXIO; - } - // Linux returns ENOTDIR instead of ELOOP when using O_NOFOLLOW|O_DIRECTORY - // on a symlink. - if (openat_errno == ENOTDIR && (noflags & (O_NOFOLLOW | O_DIRECTORY)) != 0) { - struct stat sb; - int ret = fstatat(pa.fd, pa.path, &sb, AT_SYMLINK_NOFOLLOW); - if (S_ISLNK(sb.st_mode)) { - path_put(&pa); - return __WASI_ELOOP; - } - } - path_put(&pa); - // FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on - // a symlink. - if (!pa.follow && openat_errno == EMLINK) - return __WASI_ELOOP; - return convert_errno(openat_errno); - } - path_put(&pa); - - // Determine the type of the new file descriptor and which rights - // contradict with this type. - __wasi_filetype_t type; - __wasi_rights_t max_base, max_inheriting; - error = fd_determine_type_rights(nfd, &type, &max_base, &max_inheriting); - if (error != 0) { - close(nfd); - return error; - } - return fd_table_insert_fd(curfds, nfd, type, rights_base & max_base, - rights_inheriting & max_inheriting, fd); -} - -// Copies out directory entry metadata or filename, potentially -// truncating it in the process. -static void fd_readdir_put( - void *buf, - size_t bufsize, - size_t *bufused, - const void *elem, - size_t elemsize -) { - size_t bufavail = bufsize - *bufused; - if (elemsize > bufavail) - elemsize = bufavail; - memcpy((char *)buf + *bufused, elem, elemsize); - *bufused += elemsize; -} - -__wasi_errno_t wasmtime_ssp_fd_readdir( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - void *buf, - size_t nbyte, - __wasi_dircookie_t cookie, - size_t *bufused -) { - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_READDIR, 0); - if (error != 0) { - return error; - } - - // Create a directory handle if none has been opened yet. - mutex_lock(&fo->directory.lock); - DIR *dp = fo->directory.handle; - if (dp == NULL) { - dp = fdopendir(fd_number(fo)); - if (dp == NULL) { - mutex_unlock(&fo->directory.lock); - fd_object_release(fo); - return convert_errno(errno); - } - fo->directory.handle = dp; - fo->directory.offset = __WASI_DIRCOOKIE_START; - } - - // Seek to the right position if the requested offset does not match - // the current offset. - if (fo->directory.offset != cookie) { - if (cookie == __WASI_DIRCOOKIE_START) - rewinddir(dp); - else - seekdir(dp, cookie); - fo->directory.offset = cookie; - } - - *bufused = 0; - while (nbyte > 0) { - // Read the next directory entry. - errno = 0; - struct dirent *de = readdir(dp); - if (de == NULL) { - mutex_unlock(&fo->directory.lock); - fd_object_release(fo); - return errno == 0 || *bufused > 0 ? 0 : convert_errno(errno); - } - fo->directory.offset = telldir(dp); - - // Craft a directory entry and copy that back. - size_t namlen = strlen(de->d_name); - __wasi_dirent_t cde = { - .d_next = fo->directory.offset, - .d_ino = de->d_ino, - .d_namlen = namlen, - }; - switch (de->d_type) { - case DT_BLK: - cde.d_type = __WASI_FILETYPE_BLOCK_DEVICE; - break; - case DT_CHR: - cde.d_type = __WASI_FILETYPE_CHARACTER_DEVICE; - break; - case DT_DIR: - cde.d_type = __WASI_FILETYPE_DIRECTORY; - break; - case DT_FIFO: - cde.d_type = __WASI_FILETYPE_SOCKET_STREAM; - break; - case DT_LNK: - cde.d_type = __WASI_FILETYPE_SYMBOLIC_LINK; - break; - case DT_REG: - cde.d_type = __WASI_FILETYPE_REGULAR_FILE; - break; -#ifdef DT_SOCK - case DT_SOCK: - // Technically not correct, but good enough. - cde.d_type = __WASI_FILETYPE_SOCKET_STREAM; - break; -#endif - default: - cde.d_type = __WASI_FILETYPE_UNKNOWN; - break; - } - fd_readdir_put(buf, nbyte, bufused, &cde, sizeof(cde)); - fd_readdir_put(buf, nbyte, bufused, de->d_name, namlen); - } - mutex_unlock(&fo->directory.lock); - fd_object_release(fo); - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_readlink( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t pathlen, - char *buf, - size_t bufsize, - size_t *bufused -) { - struct path_access pa; - __wasi_errno_t error = path_get_nofollow(curfds, - &pa, fd, path, pathlen, __WASI_RIGHT_PATH_READLINK, 0, false); - if (error != 0) - return error; - - // Linux requires that the buffer size is positive. whereas POSIX does - // not. Use a fake buffer to store the results if the size is zero. - char fakebuf[1]; - ssize_t len = readlinkat(pa.fd, pa.path, bufsize == 0 ? fakebuf : buf, - bufsize == 0 ? sizeof(fakebuf) : bufsize); - path_put(&pa); - if (len < 0) - return convert_errno(errno); - *bufused = (size_t)len < bufsize ? len : bufsize; - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_rename( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t old_fd, - const char *old_path, - size_t old_path_len, - __wasi_fd_t new_fd, - const char *new_path, - size_t new_path_len -) { - struct path_access old_pa; - __wasi_errno_t error = path_get_nofollow(curfds, &old_pa, old_fd, old_path, old_path_len, - __WASI_RIGHT_PATH_RENAME_SOURCE, 0, true); - if (error != 0) - return error; - - struct path_access new_pa; - error = path_get_nofollow(curfds, &new_pa, new_fd, new_path, new_path_len, - __WASI_RIGHT_PATH_RENAME_TARGET, 0, true); - if (error != 0) { - path_put(&old_pa); - return error; - } - - int ret = renameat(old_pa.fd, old_pa.path, new_pa.fd, new_pa.path); - path_put(&old_pa); - path_put(&new_pa); - if (ret < 0) { - return convert_errno(errno); - } - return 0; -} - -// Converts a POSIX stat structure to a CloudABI filestat structure. -static void convert_stat( - const struct stat *in, - __wasi_filestat_t *out -) { - *out = (__wasi_filestat_t){ - .st_dev = in->st_dev, - .st_ino = in->st_ino, - .st_nlink = in->st_nlink, - .st_size = in->st_size, - .st_atim = convert_timespec(&in->st_atim), - .st_mtim = convert_timespec(&in->st_mtim), - .st_ctim = convert_timespec(&in->st_ctim), - }; -} - -__wasi_errno_t wasmtime_ssp_fd_filestat_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filestat_t *buf -) { - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FILESTAT_GET, 0); - if (error != 0) - return error; - - int ret; - switch (fo->type) { - default: { - struct stat sb; - ret = fstat(fd_number(fo), &sb); - convert_stat(&sb, buf); - break; - } - } - buf->st_filetype = fo->type; - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -static void convert_timestamp( - __wasi_timestamp_t in, - struct timespec *out -) { - // Store sub-second remainder. - out->tv_nsec = in % 1000000000; - in /= 1000000000; - - // Clamp to the maximum in case it would overflow our system's time_t. - time_t time_max; - switch (sizeof(time_t)) { - case sizeof(int32_t): time_max = INT32_MAX; - case sizeof(int64_t): time_max = INT64_MAX; - default: assert(0 && "Unrecognized time_t type"); - } - out->tv_sec = in < time_max ? in : time_max; -} - -// Converts the provided timestamps and flags to a set of arguments for -// futimens() and utimensat(). -static void convert_utimens_arguments( - __wasi_timestamp_t st_atim, - __wasi_timestamp_t st_mtim, - __wasi_fstflags_t fstflags, - struct timespec *ts -) { - if ((fstflags & __WASI_FILESTAT_SET_ATIM_NOW) != 0) { - ts[0].tv_nsec = UTIME_NOW; - } else if ((fstflags & __WASI_FILESTAT_SET_ATIM) != 0) { - convert_timestamp(st_atim, &ts[0]); - } else { - ts[0].tv_nsec = UTIME_OMIT; - } - - if ((fstflags & __WASI_FILESTAT_SET_MTIM_NOW) != 0) { - ts[1].tv_nsec = UTIME_NOW; - } else if ((fstflags & __WASI_FILESTAT_SET_MTIM) != 0) { - convert_timestamp(st_mtim, &ts[1]); - } else { - ts[1].tv_nsec = UTIME_OMIT; - } -} - -__wasi_errno_t wasmtime_ssp_fd_filestat_set_size( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_filesize_t st_size -) { - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FILESTAT_SET_SIZE, 0); - if (error != 0) - return error; - - int ret = ftruncate(fd_number(fo), st_size); - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_fd_filestat_set_times( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_timestamp_t st_atim, - __wasi_timestamp_t st_mtim, - __wasi_fstflags_t fstflags -) { - if ((fstflags & ~(__WASI_FILESTAT_SET_ATIM | __WASI_FILESTAT_SET_ATIM_NOW | - __WASI_FILESTAT_SET_MTIM | __WASI_FILESTAT_SET_MTIM_NOW)) != 0) - return __WASI_EINVAL; - - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FILESTAT_SET_TIMES, 0); - if (error != 0) - return error; - - struct timespec ts[2]; - convert_utimens_arguments(st_atim, st_mtim, fstflags, ts); - int ret = futimens(fd_number(fo), ts); - - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_filestat_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_lookupflags_t flags, - const char *path, - size_t pathlen, - __wasi_filestat_t *buf -) { - struct path_access pa; - __wasi_errno_t error = - path_get(curfds, &pa, fd, flags, path, pathlen, __WASI_RIGHT_PATH_FILESTAT_GET, 0, false); - if (error != 0) - return error; - - struct stat sb; - int ret = fstatat(pa.fd, pa.path, &sb, pa.follow ? 0 : AT_SYMLINK_NOFOLLOW); - path_put(&pa); - if (ret < 0) - return convert_errno(errno); - convert_stat(&sb, buf); - - // Convert the file type. In the case of sockets there is no way we - // can easily determine the exact socket type. - if (S_ISBLK(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_BLOCK_DEVICE; - else if (S_ISCHR(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_CHARACTER_DEVICE; - else if (S_ISDIR(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_DIRECTORY; - else if (S_ISFIFO(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_SOCKET_STREAM; - else if (S_ISLNK(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_SYMBOLIC_LINK; - else if (S_ISREG(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_REGULAR_FILE; - else if (S_ISSOCK(sb.st_mode)) - buf->st_filetype = __WASI_FILETYPE_SOCKET_STREAM; - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_filestat_set_times( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - __wasi_lookupflags_t flags, - const char *path, - size_t pathlen, - __wasi_timestamp_t st_atim, - __wasi_timestamp_t st_mtim, - __wasi_fstflags_t fstflags -) { - if ((fstflags & ~(__WASI_FILESTAT_SET_ATIM | __WASI_FILESTAT_SET_ATIM_NOW | - __WASI_FILESTAT_SET_MTIM | __WASI_FILESTAT_SET_MTIM_NOW)) != 0) - return __WASI_EINVAL; - - struct path_access pa; - __wasi_errno_t error = path_get(curfds, - &pa, fd, flags, path, pathlen, __WASI_RIGHT_PATH_FILESTAT_SET_TIMES, 0, false); - if (error != 0) - return error; - - struct timespec ts[2]; - convert_utimens_arguments(st_atim, st_mtim, fstflags, ts); - int ret = utimensat(pa.fd, pa.path, ts, pa.follow ? 0 : AT_SYMLINK_NOFOLLOW); - - path_put(&pa); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_symlink( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - const char *old_path, - size_t old_path_len, - __wasi_fd_t fd, - const char *new_path, - size_t new_path_len -) { - char *target = str_nullterminate(old_path, old_path_len); - if (target == NULL) - return convert_errno(errno); - - struct path_access pa; - __wasi_errno_t error = path_get_nofollow(curfds, - &pa, fd, new_path, new_path_len, __WASI_RIGHT_PATH_SYMLINK, 0, true); - if (error != 0) { - free(target); - return error; - } - - int ret = symlinkat(target, pa.fd, pa.path); - path_put(&pa); - free(target); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_unlink_file( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t pathlen -) { - struct path_access pa; - __wasi_errno_t error = path_get_nofollow(curfds, - &pa, fd, path, pathlen, __WASI_RIGHT_PATH_UNLINK_FILE, 0, true); - if (error != 0) - return error; - - int ret = unlinkat(pa.fd, pa.path, 0); -#ifndef __linux__ - // Non-Linux implementations may return EPERM when attempting to remove a - // directory without REMOVEDIR. While that's what POSIX specifies, it's - // less useful. Adjust this to EISDIR. It doesn't matter that this is not - // atomic with the unlinkat, because if the file is removed and a directory - // is created before fstatat sees it, we're racing with that change anyway - // and unlinkat could have legitimately seen the directory if the race had - // turned out differently. - if (ret < 0 && errno == EPERM) { - struct stat statbuf; - if (fstatat(pa.fd, pa.path, &statbuf, AT_SYMLINK_NOFOLLOW) == 0 && - S_ISDIR(statbuf.st_mode)) { - errno = EISDIR; - } - } -#endif - path_put(&pa); - if (ret < 0) { - return convert_errno(errno); - } - return 0; -} - -__wasi_errno_t wasmtime_ssp_path_remove_directory( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t fd, - const char *path, - size_t pathlen -) { - struct path_access pa; - __wasi_errno_t error = path_get_nofollow(curfds, - &pa, fd, path, pathlen, __WASI_RIGHT_PATH_REMOVE_DIRECTORY, 0, true); - if (error != 0) - return error; - - int ret = unlinkat(pa.fd, pa.path, AT_REMOVEDIR); -#ifndef __linux__ - // POSIX permits either EEXIST or ENOTEMPTY when the directory is not empty. - // Map it to ENOTEMPTY. - if (ret < 0 && errno == EEXIST) { - errno = ENOTEMPTY; - } -#endif - path_put(&pa); - if (ret < 0) { - return convert_errno(errno); - } - return 0; -} - -__wasi_errno_t wasmtime_ssp_poll_oneoff( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - const __wasi_subscription_t *in, - __wasi_event_t *out, - size_t nsubscriptions, - size_t *nevents -) NO_LOCK_ANALYSIS { - // Sleeping. - if (nsubscriptions == 1 && in[0].type == __WASI_EVENTTYPE_CLOCK) { - out[0] = (__wasi_event_t){ - .userdata = in[0].userdata, - .type = in[0].type, - }; -#if CONFIG_HAS_CLOCK_NANOSLEEP - clockid_t clock_id; - if (convert_clockid(in[0].u.clock.clock_id, &clock_id)) { - struct timespec ts; - convert_timestamp(in[0].u.clock.timeout, &ts); - int ret = clock_nanosleep( - clock_id, - (in[0].u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) != 0 - ? TIMER_ABSTIME - : 0, - &ts, NULL); - if (ret != 0) - out[0].error = convert_errno(ret); - } else { - out[0].error = __WASI_ENOTSUP; - } -#else - switch (in[0].u.clock.clock_id) { - case __WASI_CLOCK_MONOTONIC: - if ((in[0].u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) != 0) { - // TODO(ed): Implement. - fputs("Unimplemented absolute sleep on monotonic clock\n", stderr); - out[0].error = __WASI_ENOSYS; - } else { - // Perform relative sleeps on the monotonic clock also using - // nanosleep(). This is incorrect, but good enough for now. - struct timespec ts; - convert_timestamp(in[0].u.clock.timeout, &ts); - nanosleep(&ts, NULL); - } - break; - case __WASI_CLOCK_REALTIME: - if ((in[0].u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) != 0) { - // Sleeping to an absolute point in time can only be done - // by waiting on a condition variable. - struct mutex mutex; - mutex_init(&mutex); - struct cond cond; - cond_init_realtime(&cond); - mutex_lock(&mutex); - cond_timedwait(&cond, &mutex, in[0].u.clock.timeout, true); - mutex_unlock(&mutex); - mutex_destroy(&mutex); - cond_destroy(&cond); - } else { - // Relative sleeps can be done using nanosleep(). - struct timespec ts; - convert_timestamp(in[0].u.clock.timeout, &ts); - nanosleep(&ts, NULL); - } - break; - default: - out[0].error = __WASI_ENOTSUP; - break; - } -#endif - *nevents = 1; - return 0; - } - - // Last option: call into poll(). This can only be done in case all - // subscriptions consist of __WASI_EVENTTYPE_FD_READ and - // __WASI_EVENTTYPE_FD_WRITE entries. There may be up to one - // __WASI_EVENTTYPE_CLOCK entry to act as a timeout. These are also - // the subscriptions generate by cloudlibc's poll() and select(). - struct fd_object **fos = malloc(nsubscriptions * sizeof(*fos)); - if (fos == NULL) - return __WASI_ENOMEM; - struct pollfd *pfds = malloc(nsubscriptions * sizeof(*pfds)); - if (pfds == NULL) { - free(fos); - return __WASI_ENOMEM; - } - - // Convert subscriptions to pollfd entries. Increase the reference - // count on the file descriptors to ensure they remain valid across - // the call to poll(). - struct fd_table *ft = curfds; - rwlock_rdlock(&ft->lock); - *nevents = 0; - const __wasi_subscription_t *clock_subscription = NULL; - for (size_t i = 0; i < nsubscriptions; ++i) { - const __wasi_subscription_t *s = &in[i]; - switch (s->type) { - case __WASI_EVENTTYPE_FD_READ: - case __WASI_EVENTTYPE_FD_WRITE: { - __wasi_errno_t error = - fd_object_get_locked(&fos[i], ft, s->u.fd_readwrite.fd, - __WASI_RIGHT_POLL_FD_READWRITE, 0); - if (error == 0) { - // Proper file descriptor on which we can poll(). - pfds[i] = (struct pollfd){ - .fd = fd_number(fos[i]), - .events = s->type == __WASI_EVENTTYPE_FD_READ ? POLLRDNORM - : POLLWRNORM, - }; - } else { - // Invalid file descriptor or rights missing. - fos[i] = NULL; - pfds[i] = (struct pollfd){.fd = -1}; - out[(*nevents)++] = (__wasi_event_t){ - .userdata = s->userdata, - .error = error, - .type = s->type, - }; - } - break; - } - case __WASI_EVENTTYPE_CLOCK: - if (clock_subscription == NULL && - (s->u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) == 0) { - // Relative timeout. - fos[i] = NULL; - pfds[i] = (struct pollfd){.fd = -1}; - clock_subscription = s; - break; - } - // Fallthrough. - default: - // Unsupported event. - fos[i] = NULL; - pfds[i] = (struct pollfd){.fd = -1}; - out[(*nevents)++] = (__wasi_event_t){ - .userdata = s->userdata, - .error = __WASI_ENOSYS, - .type = s->type, - }; - break; - } - } - rwlock_unlock(&ft->lock); - - // Use a zero-second timeout in case we've already generated events in - // the loop above. - int timeout; - if (*nevents != 0) { - timeout = 0; - } else if (clock_subscription != NULL) { - __wasi_timestamp_t ts = clock_subscription->u.clock.timeout / 1000000; - timeout = ts > INT_MAX ? -1 : ts; - } else { - timeout = -1; - } - int ret = poll(pfds, nsubscriptions, timeout); - - __wasi_errno_t error = 0; - if (ret == -1) { - error = convert_errno(errno); - } else if (ret == 0 && *nevents == 0 && clock_subscription != NULL) { - // No events triggered. Trigger the clock event. - out[(*nevents)++] = (__wasi_event_t){ - .userdata = clock_subscription->userdata, - .type = __WASI_EVENTTYPE_CLOCK, - }; - } else { - // Events got triggered. Don't trigger the clock event. - for (size_t i = 0; i < nsubscriptions; ++i) { - if (pfds[i].fd >= 0) { - __wasi_filesize_t nbytes = 0; - if (in[i].type == __WASI_EVENTTYPE_FD_READ) { - int l; - if (ioctl(fd_number(fos[i]), FIONREAD, &l) == 0) - nbytes = l; - } - if ((pfds[i].revents & POLLNVAL) != 0) { - // Bad file descriptor. This normally cannot occur, as - // referencing the file descriptor object will always ensure - // the descriptor is valid. Still, macOS may sometimes return - // this on FIFOs when reaching end-of-file. - out[(*nevents)++] = (__wasi_event_t){ - .userdata = in[i].userdata, -#ifdef __APPLE__ - .u.fd_readwrite.nbytes = nbytes, - .u.fd_readwrite.flags = __WASI_EVENT_FD_READWRITE_HANGUP, -#else - .error = __WASI_EBADF, -#endif - .type = in[i].type, - }; - } else if ((pfds[i].revents & POLLERR) != 0) { - // File descriptor is in an error state. - out[(*nevents)++] = (__wasi_event_t){ - .userdata = in[i].userdata, - .error = __WASI_EIO, - .type = in[i].type, - }; - } else if ((pfds[i].revents & POLLHUP) != 0) { - // End-of-file. - out[(*nevents)++] = (__wasi_event_t){ - .userdata = in[i].userdata, - .type = in[i].type, - .u.fd_readwrite.nbytes = nbytes, - .u.fd_readwrite.flags = __WASI_EVENT_FD_READWRITE_HANGUP, - }; - } else if ((pfds[i].revents & (POLLRDNORM | POLLWRNORM)) != 0) { - // Read or write possible. - out[(*nevents)++] = (__wasi_event_t){ - .userdata = in[i].userdata, - .type = in[i].type, - .u.fd_readwrite.nbytes = nbytes, - }; - } - } - } - } - - for (size_t i = 0; i < nsubscriptions; ++i) - if (fos[i] != NULL) - fd_object_release(fos[i]); - free(fos); - free(pfds); - return error; -} - -__wasi_errno_t wasmtime_ssp_proc_raise( - __wasi_signal_t sig -) { - static const int signals[] = { -#define X(v) [__WASI_##v] = v - X(SIGABRT), X(SIGALRM), X(SIGBUS), X(SIGCHLD), X(SIGCONT), X(SIGFPE), - X(SIGHUP), X(SIGILL), X(SIGINT), X(SIGKILL), X(SIGPIPE), X(SIGQUIT), - X(SIGSEGV), X(SIGSTOP), X(SIGSYS), X(SIGTERM), X(SIGTRAP), X(SIGTSTP), - X(SIGTTIN), X(SIGTTOU), X(SIGURG), X(SIGUSR1), X(SIGUSR2), X(SIGVTALRM), - X(SIGXCPU), X(SIGXFSZ), -#undef X - }; - if (sig >= sizeof(signals) / sizeof(signals[0]) || signals[sig] == 0) - return __WASI_EINVAL; - -#if CONFIG_TLS_USE_GSBASE - // TLS on OS X depends on installing a SIGSEGV handler. Reset SIGSEGV - // to the default action before raising. - if (sig == __WASI_SIGSEGV) { - struct sigaction sa = { - .sa_handler = SIG_DFL, - }; - sigemptyset(&sa.sa_mask); - sigaction(SIGSEGV, &sa, NULL); - } -#endif - - if (raise(signals[sig]) < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_random_get( - void *buf, - size_t nbyte -) { - random_buf(buf, nbyte); - return 0; -} - -__wasi_errno_t wasmtime_ssp_sock_recv( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t sock, - const __wasi_iovec_t *ri_data, - size_t ri_data_len, - __wasi_riflags_t ri_flags, - size_t *ro_datalen, - __wasi_roflags_t *ro_flags -) { - // Convert input to msghdr. - struct msghdr hdr = { - .msg_iov = (struct iovec *)ri_data, - .msg_iovlen = ri_data_len, - }; - int nflags = 0; - if ((ri_flags & __WASI_SOCK_RECV_PEEK) != 0) - nflags |= MSG_PEEK; - if ((ri_flags & __WASI_SOCK_RECV_WAITALL) != 0) - nflags |= MSG_WAITALL; - - struct fd_object *fo; - __wasi_errno_t error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_READ, 0); - if (error != 0) { - return error; - } - - ssize_t datalen = recvmsg(fd_number(fo), &hdr, nflags); - fd_object_release(fo); - if (datalen < 0) { - return convert_errno(errno); - } - - - // Convert msghdr to output. - *ro_datalen = datalen; - *ro_flags = 0; - if ((hdr.msg_flags & MSG_TRUNC) != 0) - *ro_flags |= __WASI_SOCK_RECV_DATA_TRUNCATED; - return 0; -} - -__wasi_errno_t wasmtime_ssp_sock_send( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t sock, - const __wasi_ciovec_t *si_data, - size_t si_data_len, - __wasi_siflags_t si_flags, - size_t *so_datalen -) NO_LOCK_ANALYSIS { - // Convert input to msghdr. - struct msghdr hdr = { - .msg_iov = (struct iovec *)si_data, - .msg_iovlen = si_data_len, - }; - - // Attach file descriptors if present. - __wasi_errno_t error; - - // Send message. - struct fd_object *fo; - error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_WRITE, 0); - if (error != 0) - goto out; - ssize_t len = sendmsg(fd_number(fo), &hdr, 0); - fd_object_release(fo); - if (len < 0) { - error = convert_errno(errno); - } else { - *so_datalen = len; - } - -out: - return error; -} - -__wasi_errno_t wasmtime_ssp_sock_shutdown( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - __wasi_fd_t sock, - __wasi_sdflags_t how -) { - int nhow; - switch (how) { - case __WASI_SHUT_RD: - nhow = SHUT_RD; - break; - case __WASI_SHUT_WR: - nhow = SHUT_WR; - break; - case __WASI_SHUT_RD | __WASI_SHUT_WR: - nhow = SHUT_RDWR; - break; - default: - return __WASI_EINVAL; - } - - struct fd_object *fo; - __wasi_errno_t error = - fd_object_get(curfds, &fo, sock, __WASI_RIGHT_SOCK_SHUTDOWN, 0); - if (error != 0) - return error; - - int ret = shutdown(fd_number(fo), nhow); - fd_object_release(fo); - if (ret < 0) - return convert_errno(errno); - return 0; -} - -__wasi_errno_t wasmtime_ssp_args_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *argv_environ, -#endif - char **argv, - char *argv_buf -) { - for (size_t i = 0; i < argv_environ->argc; ++i) { - argv[i] = argv_buf + (argv_environ->argv[i] - argv_environ->argv_buf); - } - memcpy(argv_buf, argv_environ->argv_buf, argv_environ->argv_buf_size); - return __WASI_ESUCCESS; -} - -__wasi_errno_t wasmtime_ssp_args_sizes_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *argv_environ, -#endif - size_t *argc, - size_t *argv_buf_size -) { - *argc = argv_environ->argc; - *argv_buf_size = argv_environ->argv_buf_size; - return __WASI_ESUCCESS; -} - -__wasi_errno_t wasmtime_ssp_environ_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *argv_environ, -#endif - char **environ, - char *environ_buf -) { - for (size_t i = 0; i < argv_environ->environ_count; ++i) { - environ[i] = environ_buf + (argv_environ->environ[i] - argv_environ->environ_buf); - } - memcpy(environ_buf, argv_environ->environ_buf, argv_environ->environ_buf_size); - return __WASI_ESUCCESS; -} - -__wasi_errno_t wasmtime_ssp_environ_sizes_get( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct argv_environ_values *argv_environ, -#endif - size_t *environ_count, - size_t *environ_buf_size -) { - *environ_count = argv_environ->environ_count; - *environ_buf_size = argv_environ->environ_buf_size; - return __WASI_ESUCCESS; -} - -void argv_environ_init(struct argv_environ_values *argv_environ, - const size_t *argv_offsets, size_t argv_offsets_len, - const char *argv_buf, size_t argv_buf_len, - const size_t *environ_offsets, size_t environ_offsets_len, - const char *environ_buf, size_t environ_buf_len) -{ - argv_environ->argc = argv_offsets_len; - argv_environ->argv_buf_size = argv_buf_len; - argv_environ->argv = malloc(argv_offsets_len * sizeof(char *)); - argv_environ->argv_buf = malloc(argv_buf_len); - if (argv_environ->argv == NULL || argv_environ->argv_buf == NULL) { - abort(); - } - for (size_t i = 0; i < argv_offsets_len; ++i) { - argv_environ->argv[i] = argv_environ->argv_buf + argv_offsets[i]; - } - memcpy(argv_environ->argv_buf, argv_buf, argv_buf_len); - - argv_environ->environ_count = environ_offsets_len; - argv_environ->environ_buf_size = environ_buf_len; - argv_environ->environ = malloc(environ_offsets_len * sizeof(char *)); - argv_environ->environ_buf = malloc(environ_buf_len); - if (argv_environ->environ == NULL || argv_environ->environ_buf == NULL) { - abort(); - } - for (size_t i = 0; i < environ_offsets_len; ++i) { - argv_environ->environ[i] = argv_environ->environ_buf + environ_offsets[i]; - } - memcpy(argv_environ->environ_buf, environ_buf, environ_buf_len); -} diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/posix.h b/wasmtime-wasi/sandboxed-system-primitives/src/posix.h deleted file mode 100644 index 6eb7011f7abc..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/posix.h +++ /dev/null @@ -1,63 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/ - -#ifndef POSIX_H -#define POSIX_H - -#include -#include - -#include "locking.h" - -struct fd_entry; -struct syscalls; - -struct fd_prestat { - const char *dir_name; - size_t dir_name_len; -}; - -struct fd_table { - struct rwlock lock; - struct fd_entry *entries; - size_t size; - size_t used; -}; - -struct fd_prestats { - struct rwlock lock; - struct fd_prestat *prestats; - size_t size; - size_t used; -}; - -struct argv_environ_values { - size_t argc; - size_t argv_buf_size; - char **argv; - char *argv_buf; - size_t environ_count; - size_t environ_buf_size; - char **environ; - char *environ_buf; -}; - -void fd_table_init(struct fd_table *); -bool fd_table_insert_existing(struct fd_table *, __wasi_fd_t, int); -void fd_prestats_init(struct fd_prestats *); -bool fd_prestats_insert(struct fd_prestats *, const char *, __wasi_fd_t); -void argv_environ_init(struct argv_environ_values *, - const size_t *argv_offsets, size_t argv_offsets_len, - const char *argv_buf, size_t argv_buf_len, - const size_t *environ_offsets, size_t environ_offsets_len, - const char *environ_buf, size_t environ_buf_len); - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/queue.h b/wasmtime-wasi/sandboxed-system-primitives/src/queue.h deleted file mode 100644 index c46190a85395..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/queue.h +++ /dev/null @@ -1,92 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef QUEUE_H -#define QUEUE_H - -#include - -// LIST: Double-linked list. - -#define LIST_HEAD(name, type) \ - struct name { \ - struct type *l_first; \ - } -#define LIST_HEAD_INITIALIZER(head) \ - { NULL } - -#define LIST_ENTRY(type) \ - struct { \ - struct type *l_next; \ - struct type **l_prev; \ - } - -#define LIST_FOREACH(var, head, field) \ - for ((var) = (head)->l_first; (var) != NULL; (var) = (var)->field.l_next) -#define LIST_INIT(head) \ - do { \ - (head)->l_first = NULL; \ - } while (0) -#define LIST_INSERT_HEAD(head, element, field) \ - do { \ - (element)->field.l_next = (head)->l_first; \ - if ((head)->l_first != NULL) \ - (head)->l_first->field.l_prev = &(element)->field.l_next; \ - (head)->l_first = (element); \ - (element)->field.l_prev = &(head)->l_first; \ - } while (0) -#define LIST_REMOVE(element, field) \ - do { \ - if ((element)->field.l_next != NULL) \ - (element)->field.l_next->field.l_prev = (element)->field.l_prev; \ - *(element)->field.l_prev = (element)->field.l_next; \ - } while (0) - -// TAILQ: Double-linked list with tail pointer. - -#define TAILQ_HEAD(name, type) \ - struct name { \ - struct type *t_first; \ - struct type **t_last; \ - } - -#define TAILQ_ENTRY(type) \ - struct { \ - struct type *t_next; \ - struct type **t_prev; \ - } - -#define TAILQ_EMPTY(head) ((head)->t_first == NULL) -#define TAILQ_FIRST(head) ((head)->t_first) -#define TAILQ_FOREACH(var, head, field) \ - for ((var) = (head)->t_first; (var) != NULL; (var) = (var)->field.t_next) -#define TAILQ_INIT(head) \ - do { \ - (head)->t_first = NULL; \ - (head)->t_last = &(head)->t_first; \ - } while (0) -#define TAILQ_INSERT_TAIL(head, elm, field) \ - do { \ - (elm)->field.t_next = NULL; \ - (elm)->field.t_prev = (head)->t_last; \ - *(head)->t_last = (elm); \ - (head)->t_last = &(elm)->field.t_next; \ - } while (0) -#define TAILQ_REMOVE(head, element, field) \ - do { \ - if ((element)->field.t_next != NULL) \ - (element)->field.t_next->field.t_prev = (element)->field.t_prev; \ - else \ - (head)->t_last = (element)->field.t_prev; \ - *(element)->field.t_prev = (element)->field.t_next; \ - } while (0) - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/random.c b/wasmtime-wasi/sandboxed-system-primitives/src/random.c deleted file mode 100644 index 9fc3e33be269..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/random.c +++ /dev/null @@ -1,92 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "random.h" - -#if CONFIG_HAS_ARC4RANDOM_BUF - -void random_buf(void *buf, size_t len) { - arc4random_buf(buf, len); -} - -#elif CONFIG_HAS_GETRANDOM - -#include - -void random_buf(void *buf, size_t len) { - for (;;) { - ssize_t x = getrandom(buf, len, 0); - if (x < 0) { - if (errno == EINTR) - continue; - fprintf(stderr, "getrandom failed: %s", strerror(errno)); - abort(); - } - if (x == len) - return; - buf = (void *)((unsigned char *)buf + x); - len -= x; - } -} - -#else - -static int urandom; - -static void open_urandom(void) { - urandom = open("/dev/urandom", O_RDONLY); - if (urandom < 0) { - fputs("Failed to open /dev/urandom\n", stderr); - abort(); - } -} - -void random_buf(void *buf, size_t len) { - static pthread_once_t open_once = PTHREAD_ONCE_INIT; - pthread_once(&open_once, open_urandom); - - if (read(urandom, buf, len) != len) { - fputs("Short read on /dev/urandom\n", stderr); - abort(); - } -} - -#endif - -// Calculates a random number within the range [0, upper - 1] without -// any modulo bias. -// -// The function below repeatedly obtains a random number from -// arc4random() until it lies within the range [2^k % upper, 2^k). As -// this range has length k * upper, we can safely obtain a number -// without any modulo bias. -uintmax_t random_uniform(uintmax_t upper) { - // Compute 2^k % upper - // == (2^k - upper) % upper - // == -upper % upper. - uintmax_t lower = -upper % upper; - for (;;) { - uintmax_t value; - random_buf(&value, sizeof(value)); - if (value >= lower) - return value % upper; - } -} diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/random.h b/wasmtime-wasi/sandboxed-system-primitives/src/random.h deleted file mode 100644 index d87a292cf0c8..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/random.h +++ /dev/null @@ -1,20 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef RANDOM_H -#define RANDOM_H - -#include - -void random_buf(void *, size_t); -uintmax_t random_uniform(uintmax_t); - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/refcount.h b/wasmtime-wasi/sandboxed-system-primitives/src/refcount.h deleted file mode 100644 index a259104c1080..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/refcount.h +++ /dev/null @@ -1,65 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef REFCOUNT_H -#define REFCOUNT_H - -#include -#if !defined(__GNUC__) -#include -#endif -#include - -#include "locking.h" - -// Simple reference counter. -struct LOCKABLE refcount { -#if defined(__GNUC__) - unsigned count; -#else - atomic_uint count; -#endif -}; - -#define PRODUCES(...) LOCKS_SHARED(__VA_ARGS__) NO_LOCK_ANALYSIS -#define CONSUMES(...) UNLOCKS(__VA_ARGS__) NO_LOCK_ANALYSIS - -// Initialize the reference counter. -static void refcount_init(struct refcount *r, unsigned int count) PRODUCES(*r) { -#if defined(__GNUC__) - __atomic_store_n(&r->count, count, __ATOMIC_SEQ_CST); -#else - atomic_init(&r->count, count); -#endif -} - -// Increment the reference counter. -static inline void refcount_acquire(struct refcount *r) PRODUCES(*r) { -#if defined(__GNUC__) - __atomic_fetch_add(&r->count, 1, __ATOMIC_ACQUIRE); -#else - atomic_fetch_add_explicit(&r->count, 1, memory_order_acquire); -#endif -} - -// Decrement the reference counter, returning whether the reference -// dropped to zero. -static inline bool refcount_release(struct refcount *r) CONSUMES(*r) { -#if defined(__GNUC__) - int old = __atomic_fetch_sub(&r->count, 1, __ATOMIC_RELEASE); -#else - int old = atomic_fetch_sub_explicit(&r->count, 1, memory_order_release); -#endif - assert(old != 0 && "Reference count becoming negative"); - return old == 1; -} - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/rights.h b/wasmtime-wasi/sandboxed-system-primitives/src/rights.h deleted file mode 100644 index 69349e45ca09..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/rights.h +++ /dev/null @@ -1,83 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef RIGHTS_H -#define RIGHTS_H - -#define RIGHTS_ALL \ - (__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_READ | \ - __WASI_RIGHT_FD_SEEK | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ - __WASI_RIGHT_FD_SYNC | __WASI_RIGHT_FD_TELL | __WASI_RIGHT_FD_WRITE | \ - __WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_ALLOCATE | \ - __WASI_RIGHT_PATH_CREATE_DIRECTORY | __WASI_RIGHT_PATH_CREATE_FILE | \ - __WASI_RIGHT_PATH_LINK_SOURCE | __WASI_RIGHT_PATH_LINK_TARGET | \ - __WASI_RIGHT_PATH_OPEN | __WASI_RIGHT_FD_READDIR | \ - __WASI_RIGHT_PATH_READLINK | __WASI_RIGHT_PATH_RENAME_SOURCE | \ - __WASI_RIGHT_PATH_RENAME_TARGET | __WASI_RIGHT_PATH_FILESTAT_GET | \ - __WASI_RIGHT_PATH_FILESTAT_SET_SIZE | \ - __WASI_RIGHT_PATH_FILESTAT_SET_TIMES | \ - __WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_FILESTAT_SET_TIMES | \ - __WASI_RIGHT_FD_FILESTAT_SET_SIZE | \ - __WASI_RIGHT_PATH_SYMLINK | __WASI_RIGHT_PATH_UNLINK_FILE | \ - __WASI_RIGHT_PATH_REMOVE_DIRECTORY | \ - __WASI_RIGHT_POLL_FD_READWRITE | __WASI_RIGHT_SOCK_SHUTDOWN) - -// Block and character device interaction is outside the scope of -// CloudABI. Simply allow everything. -#define RIGHTS_BLOCK_DEVICE_BASE RIGHTS_ALL -#define RIGHTS_BLOCK_DEVICE_INHERITING RIGHTS_ALL -#define RIGHTS_CHARACTER_DEVICE_BASE RIGHTS_ALL -#define RIGHTS_CHARACTER_DEVICE_INHERITING RIGHTS_ALL - -// Only allow directory operations on directories. Directories can only -// yield file descriptors to other directories and files. -#define RIGHTS_DIRECTORY_BASE \ - (__WASI_RIGHT_FD_FDSTAT_SET_FLAGS | __WASI_RIGHT_FD_SYNC | \ - __WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_PATH_CREATE_DIRECTORY | \ - __WASI_RIGHT_PATH_CREATE_FILE | __WASI_RIGHT_PATH_LINK_SOURCE | \ - __WASI_RIGHT_PATH_LINK_TARGET | __WASI_RIGHT_PATH_OPEN | \ - __WASI_RIGHT_FD_READDIR | __WASI_RIGHT_PATH_READLINK | \ - __WASI_RIGHT_PATH_RENAME_SOURCE | __WASI_RIGHT_PATH_RENAME_TARGET | \ - __WASI_RIGHT_PATH_FILESTAT_GET | \ - __WASI_RIGHT_PATH_FILESTAT_SET_SIZE | \ - __WASI_RIGHT_PATH_FILESTAT_SET_TIMES | \ - __WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_FILESTAT_SET_TIMES | \ - __WASI_RIGHT_PATH_SYMLINK | __WASI_RIGHT_PATH_UNLINK_FILE | \ - __WASI_RIGHT_PATH_REMOVE_DIRECTORY | \ - __WASI_RIGHT_POLL_FD_READWRITE) -#define RIGHTS_DIRECTORY_INHERITING \ - (RIGHTS_DIRECTORY_BASE | RIGHTS_REGULAR_FILE_BASE) - -// Operations that apply to regular files. -#define RIGHTS_REGULAR_FILE_BASE \ - (__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_READ | \ - __WASI_RIGHT_FD_SEEK | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ - __WASI_RIGHT_FD_SYNC | __WASI_RIGHT_FD_TELL | __WASI_RIGHT_FD_WRITE | \ - __WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_ALLOCATE | \ - __WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_FILESTAT_SET_SIZE | \ - __WASI_RIGHT_FD_FILESTAT_SET_TIMES | __WASI_RIGHT_POLL_FD_READWRITE) -#define RIGHTS_REGULAR_FILE_INHERITING 0 - -// Operations that apply to sockets and socket pairs. -#define RIGHTS_SOCKET_BASE \ - (__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ - __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_FILESTAT_GET | \ - __WASI_RIGHT_POLL_FD_READWRITE | __WASI_RIGHT_SOCK_SHUTDOWN) -#define RIGHTS_SOCKET_INHERITING RIGHTS_ALL - -// Operations that apply to TTYs. -#define RIGHTS_TTY_BASE \ - (__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ - __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_FILESTAT_GET | \ - __WASI_RIGHT_POLL_FD_READWRITE) -#define RIGHTS_TTY_INHERITING 0 - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/signals.h b/wasmtime-wasi/sandboxed-system-primitives/src/signals.h deleted file mode 100644 index 16f5d575ca2e..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/signals.h +++ /dev/null @@ -1,17 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef SIGNALS_H -#define SIGNALS_H - -void signals_init(void); - -#endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/str.c b/wasmtime-wasi/sandboxed-system-primitives/src/str.c deleted file mode 100644 index 02a9518f472f..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/str.c +++ /dev/null @@ -1,33 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#include "config.h" - -#include -#include -#include - -#include "str.h" - -char *str_nullterminate(const char *s, size_t len) { - // Copy string. - char *ret = strndup(s, len); - if (ret == NULL) - return NULL; - - // Ensure that it contains no null bytes within. - if (strlen(ret) != len) { - free(ret); - errno = EILSEQ; - return NULL; - } - return ret; -} diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/str.h b/wasmtime-wasi/sandboxed-system-primitives/src/str.h deleted file mode 100644 index a9c5f226fa72..000000000000 --- a/wasmtime-wasi/sandboxed-system-primitives/src/str.h +++ /dev/null @@ -1,19 +0,0 @@ -// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. -// -// Significant parts of this file are derived from cloudabi-utils. See -// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE -// for license information. -// -// The upstream file contains the following copyright notice: -// -// Copyright (c) 2016 Nuxi, https://nuxi.nl/ - -#ifndef STR_H -#define STR_H - -#include "config.h" - -char *str_nullterminate(const char *, size_t); - -#endif diff --git a/wasmtime-wasi/src/host.rs b/wasmtime-wasi/src/host.rs deleted file mode 100644 index afd01bc1637d..000000000000 --- a/wasmtime-wasi/src/host.rs +++ /dev/null @@ -1,88 +0,0 @@ -#![allow(non_camel_case_types, dead_code)] - -include!(concat!(env!("OUT_DIR"), "/wasmtime_ssp.rs")); - -pub type char = ::std::os::raw::c_char; -pub type void = ::std::os::raw::c_void; -// pub type __wasi_errno_t = u16; - -pub const __WASI_ESUCCESS: __wasi_errno_t = 0; -pub const __WASI_E2BIG: __wasi_errno_t = 1; -pub const __WASI_EACCES: __wasi_errno_t = 2; -pub const __WASI_EADDRINUSE: __wasi_errno_t = 3; -pub const __WASI_EADDRNOTAVAIL: __wasi_errno_t = 4; -pub const __WASI_EAFNOSUPPORT: __wasi_errno_t = 5; -pub const __WASI_EAGAIN: __wasi_errno_t = 6; -pub const __WASI_EALREADY: __wasi_errno_t = 7; -pub const __WASI_EBADF: __wasi_errno_t = 8; -pub const __WASI_EBADMSG: __wasi_errno_t = 9; -pub const __WASI_EBUSY: __wasi_errno_t = 10; -pub const __WASI_ECANCELED: __wasi_errno_t = 11; -pub const __WASI_ECHILD: __wasi_errno_t = 12; -pub const __WASI_ECONNABORTED: __wasi_errno_t = 13; -pub const __WASI_ECONNREFUSED: __wasi_errno_t = 14; -pub const __WASI_ECONNRESET: __wasi_errno_t = 15; -pub const __WASI_EDEADLK: __wasi_errno_t = 16; -pub const __WASI_EDESTADDRREQ: __wasi_errno_t = 17; -pub const __WASI_EDOM: __wasi_errno_t = 18; -pub const __WASI_EDQUOT: __wasi_errno_t = 19; -pub const __WASI_EEXIST: __wasi_errno_t = 20; -pub const __WASI_EFAULT: __wasi_errno_t = 21; -pub const __WASI_EFBIG: __wasi_errno_t = 22; -pub const __WASI_EHOSTUNREACH: __wasi_errno_t = 23; -pub const __WASI_EIDRM: __wasi_errno_t = 24; -pub const __WASI_EILSEQ: __wasi_errno_t = 25; -pub const __WASI_EINPROGRESS: __wasi_errno_t = 26; -pub const __WASI_EINTR: __wasi_errno_t = 27; -pub const __WASI_EINVAL: __wasi_errno_t = 28; -pub const __WASI_EIO: __wasi_errno_t = 29; -pub const __WASI_EISCONN: __wasi_errno_t = 30; -pub const __WASI_EISDIR: __wasi_errno_t = 31; -pub const __WASI_ELOOP: __wasi_errno_t = 32; -pub const __WASI_EMFILE: __wasi_errno_t = 33; -pub const __WASI_EMLINK: __wasi_errno_t = 34; -pub const __WASI_EMSGSIZE: __wasi_errno_t = 35; -pub const __WASI_EMULTIHOP: __wasi_errno_t = 36; -pub const __WASI_ENAMETOOLONG: __wasi_errno_t = 37; -pub const __WASI_ENETDOWN: __wasi_errno_t = 38; -pub const __WASI_ENETRESET: __wasi_errno_t = 39; -pub const __WASI_ENETUNREACH: __wasi_errno_t = 40; -pub const __WASI_ENFILE: __wasi_errno_t = 41; -pub const __WASI_ENOBUFS: __wasi_errno_t = 42; -pub const __WASI_ENODEV: __wasi_errno_t = 43; -pub const __WASI_ENOENT: __wasi_errno_t = 44; -pub const __WASI_ENOEXEC: __wasi_errno_t = 45; -pub const __WASI_ENOLCK: __wasi_errno_t = 46; -pub const __WASI_ENOLINK: __wasi_errno_t = 47; -pub const __WASI_ENOMEM: __wasi_errno_t = 48; -pub const __WASI_ENOMSG: __wasi_errno_t = 49; -pub const __WASI_ENOPROTOOPT: __wasi_errno_t = 50; -pub const __WASI_ENOSPC: __wasi_errno_t = 51; -pub const __WASI_ENOSYS: __wasi_errno_t = 52; -pub const __WASI_ENOTCONN: __wasi_errno_t = 53; -pub const __WASI_ENOTDIR: __wasi_errno_t = 54; -pub const __WASI_ENOTEMPTY: __wasi_errno_t = 55; -pub const __WASI_ENOTRECOVERABLE: __wasi_errno_t = 56; -pub const __WASI_ENOTSOCK: __wasi_errno_t = 57; -pub const __WASI_ENOTSUP: __wasi_errno_t = 58; -pub const __WASI_ENOTTY: __wasi_errno_t = 59; -pub const __WASI_ENXIO: __wasi_errno_t = 60; -pub const __WASI_EOVERFLOW: __wasi_errno_t = 61; -pub const __WASI_EOWNERDEAD: __wasi_errno_t = 62; -pub const __WASI_EPERM: __wasi_errno_t = 63; -pub const __WASI_EPIPE: __wasi_errno_t = 64; -pub const __WASI_EPROTO: __wasi_errno_t = 65; -pub const __WASI_EPROTONOSUPPORT: __wasi_errno_t = 66; -pub const __WASI_EPROTOTYPE: __wasi_errno_t = 67; -pub const __WASI_ERANGE: __wasi_errno_t = 68; -pub const __WASI_EROFS: __wasi_errno_t = 69; -pub const __WASI_ESPIPE: __wasi_errno_t = 70; -pub const __WASI_ESRCH: __wasi_errno_t = 71; -pub const __WASI_ESTALE: __wasi_errno_t = 72; -pub const __WASI_ETIMEDOUT: __wasi_errno_t = 73; -pub const __WASI_ETXTBSY: __wasi_errno_t = 74; -pub const __WASI_EXDEV: __wasi_errno_t = 75; -pub const __WASI_ENOTCAPABLE: __wasi_errno_t = 76; - -// pub type __wasi_preopentype_t = u8; -pub const __WASI_PREOPENTYPE_DIR: __wasi_preopentype_t = 0; diff --git a/wasmtime-wasi/src/host_impls.rs b/wasmtime-wasi/src/host_impls.rs deleted file mode 100644 index e545c834f76d..000000000000 --- a/wasmtime-wasi/src/host_impls.rs +++ /dev/null @@ -1,183 +0,0 @@ -use super::host; -use super::wasm32; -use errno::{errno, Errno}; - -/// Convert POSIX error code to host's WASI error code -fn convert_errno(error: Errno) -> host::__wasi_errno_t { - #[allow(unreachable_patterns)] - match error.into() { - libc::E2BIG => host::__WASI_E2BIG, - libc::EACCES => host::__WASI_EACCES, - libc::EADDRINUSE => host::__WASI_EADDRINUSE, - libc::EADDRNOTAVAIL => host::__WASI_EADDRNOTAVAIL, - libc::EAFNOSUPPORT => host::__WASI_EAFNOSUPPORT, - libc::EAGAIN | libc::EWOULDBLOCK => host::__WASI_EAGAIN, - libc::EALREADY => host::__WASI_EALREADY, - libc::EBADF => host::__WASI_EBADF, - libc::EBADMSG => host::__WASI_EBADMSG, - libc::EBUSY => host::__WASI_EBUSY, - libc::ECANCELED => host::__WASI_ECANCELED, - libc::ECHILD => host::__WASI_ECHILD, - libc::ECONNABORTED => host::__WASI_ECONNABORTED, - libc::ECONNREFUSED => host::__WASI_ECONNREFUSED, - libc::ECONNRESET => host::__WASI_ECONNRESET, - libc::EDEADLK => host::__WASI_EDEADLK, - libc::EDESTADDRREQ => host::__WASI_EDESTADDRREQ, - libc::EDOM => host::__WASI_EDOM, - libc::EDQUOT => host::__WASI_EDQUOT, - libc::EEXIST => host::__WASI_EEXIST, - libc::EFAULT => host::__WASI_EFAULT, - libc::EFBIG => host::__WASI_EFBIG, - libc::EHOSTUNREACH => host::__WASI_EHOSTUNREACH, - libc::EIDRM => host::__WASI_EIDRM, - libc::EILSEQ => host::__WASI_EILSEQ, - libc::EINPROGRESS => host::__WASI_EINPROGRESS, - libc::EINTR => host::__WASI_EINTR, - libc::EINVAL => host::__WASI_EINVAL, - libc::EIO => host::__WASI_EIO, - libc::EISCONN => host::__WASI_EISCONN, - libc::EISDIR => host::__WASI_EISDIR, - libc::ELOOP => host::__WASI_ELOOP, - libc::EMFILE => host::__WASI_EMFILE, - libc::EMLINK => host::__WASI_EMLINK, - libc::EMSGSIZE => host::__WASI_EMSGSIZE, - libc::EMULTIHOP => host::__WASI_EMULTIHOP, - libc::ENAMETOOLONG => host::__WASI_ENAMETOOLONG, - libc::ENETDOWN => host::__WASI_ENETDOWN, - libc::ENETRESET => host::__WASI_ENETRESET, - libc::ENETUNREACH => host::__WASI_ENETUNREACH, - libc::ENFILE => host::__WASI_ENFILE, - libc::ENOBUFS => host::__WASI_ENOBUFS, - libc::ENODEV => host::__WASI_ENODEV, - libc::ENOENT => host::__WASI_ENOENT, - libc::ENOEXEC => host::__WASI_ENOEXEC, - libc::ENOLCK => host::__WASI_ENOLCK, - libc::ENOLINK => host::__WASI_ENOLINK, - libc::ENOMEM => host::__WASI_ENOMEM, - libc::ENOMSG => host::__WASI_ENOMSG, - libc::ENOPROTOOPT => host::__WASI_ENOPROTOOPT, - libc::ENOSPC => host::__WASI_ENOSPC, - libc::ENOSYS => host::__WASI_ENOSYS, - // TODO: verify if this is correct - #[cfg(target_os = "freebsd")] - libc::ENOTCAPABLE => host::__WASI_ENOTCAPABLE, - libc::ENOTCONN => host::__WASI_ENOTCONN, - libc::ENOTDIR => host::__WASI_ENOTDIR, - libc::ENOTEMPTY => host::__WASI_ENOTEMPTY, - libc::ENOTRECOVERABLE => host::__WASI_ENOTRECOVERABLE, - libc::ENOTSOCK => host::__WASI_ENOTSOCK, - libc::ENOTSUP | libc::EOPNOTSUPP => host::__WASI_ENOTSUP, - libc::ENOTTY => host::__WASI_ENOTTY, - libc::ENXIO => host::__WASI_ENXIO, - libc::EOVERFLOW => host::__WASI_EOVERFLOW, - libc::EOWNERDEAD => host::__WASI_EOWNERDEAD, - libc::EPERM => host::__WASI_EPERM, - libc::EPIPE => host::__WASI_EPIPE, - libc::EPROTO => host::__WASI_EPROTO, - libc::EPROTONOSUPPORT => host::__WASI_EPROTONOSUPPORT, - libc::EPROTOTYPE => host::__WASI_EPROTOTYPE, - libc::ERANGE => host::__WASI_ERANGE, - libc::EROFS => host::__WASI_EROFS, - libc::ESPIPE => host::__WASI_ESPIPE, - libc::ESRCH => host::__WASI_ESRCH, - libc::ESTALE => host::__WASI_ESTALE, - libc::ETIMEDOUT => host::__WASI_ETIMEDOUT, - libc::ETXTBSY => host::__WASI_ETXTBSY, - libc::EXDEV => host::__WASI_EXDEV, - _ => host::__WASI_ENOSYS, - } -} - -fn fd_prestats_get_entry( - pt: &host::fd_prestats, - fd: host::__wasi_fd_t, -) -> Option<&host::fd_prestat> { - // Test for file descriptor existence - if fd as usize >= pt.size { - return None; - } - - let prestat = unsafe { &*pt.prestats.add(fd as usize) }; - if prestat.dir_name == ::std::ptr::null() { - return None; - } - - Some(prestat) -} - -macro_rules! rwlock_rdlock { - ($prestats:expr) => { - unsafe { - host::rwlock_rdlock(&mut (*$prestats).lock as *mut host::rwlock); - } - }; -} - -macro_rules! rwlock_unlock { - ($prestats:expr) => { - unsafe { - host::rwlock_unlock(&mut (*$prestats).lock as *mut host::rwlock); - } - }; -} - -pub fn wasmtime_ssp_proc_exit(rval: wasm32::__wasi_exitcode_t) { - ::std::process::exit(rval as i32) -} - -pub fn wasmtime_ssp_fd_prestat_get( - prestats: &mut host::fd_prestats, - fd: host::__wasi_fd_t, - buf: &mut host::__wasi_prestat_t, -) -> host::__wasi_errno_t { - rwlock_rdlock!(prestats); - - let ret_code = if let Some(prestat) = fd_prestats_get_entry(prestats, fd) { - buf.pr_type = host::__WASI_PREOPENTYPE_DIR; - unsafe { - buf.u.dir.pr_name_len = prestat.dir_name_len; - } - host::__WASI_ESUCCESS - } else { - host::__WASI_EBADF - }; - - rwlock_unlock!(prestats); - - ret_code -} - -pub fn wasmtime_ssp_fd_prestat_dir_name( - prestats: &mut host::fd_prestats, - fd: host::__wasi_fd_t, - path: &mut [host::char], -) -> host::__wasi_errno_t { - rwlock_rdlock!(prestats); - - let ret_code = if let Some(prestat) = fd_prestats_get_entry(prestats, fd) { - if path.len() != prestat.dir_name_len { - host::__WASI_EINVAL - } else { - path.copy_from_slice(unsafe { - ::std::slice::from_raw_parts(prestat.dir_name, prestat.dir_name_len) - }); - host::__WASI_ESUCCESS - } - } else { - host::__WASI_EBADF - }; - - rwlock_unlock!(prestats); - - ret_code -} - -pub fn wasmtime_ssp_sched_yield() -> host::__wasi_errno_t { - unsafe { - if libc::sched_yield() < 0 { - return convert_errno(errno()); - } - } - - host::__WASI_ESUCCESS -} diff --git a/wasmtime-wasi/src/instantiate.rs b/wasmtime-wasi/src/instantiate.rs index 9a550784764f..ec189642d319 100644 --- a/wasmtime-wasi/src/instantiate.rs +++ b/wasmtime-wasi/src/instantiate.rs @@ -1,32 +1,22 @@ -use crate::host::{ - argv_environ_init, argv_environ_values, fd_prestats, fd_prestats_init, fd_prestats_insert, - fd_table, fd_table_init, fd_table_insert_existing, -}; use cranelift_codegen::ir::types; use cranelift_codegen::{ir, isa}; use cranelift_entity::PrimaryMap; use cranelift_wasm::DefinedFuncIndex; use std::cell::RefCell; use std::collections::HashMap; -use std::ffi::CString; -use std::mem; +use std::fs::File; use std::rc::Rc; use syscalls; use target_lexicon::HOST; +use wasi_common::WasiCtxBuilder; use wasmtime_environ::{translate_signature, Export, Module}; use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMFunctionBody}; -pub(crate) struct WASIState { - pub curfds: Box, - pub prestats: Box, - pub argv_environ: Box, -} - /// Return an instance implementing the "wasi" interface. pub fn instantiate_wasi( prefix: &str, global_exports: Rc>>>, - preopened_dirs: &[(String, libc::c_int)], + preopened_dirs: &[(String, File)], argv: &[String], environ: &[(String, String)], ) -> Result { @@ -110,54 +100,29 @@ pub fn instantiate_wasi( let imports = Imports::none(); let data_initializers = Vec::new(); let signatures = PrimaryMap::new(); - let mut curfds = Box::new(unsafe { mem::zeroed::() }); - let mut prestats = Box::new(unsafe { mem::zeroed::() }); - let mut argv_environ = Box::new(unsafe { mem::zeroed::() }); - - unsafe { - let argv_environ: &mut argv_environ_values = &mut *argv_environ; - let (argv_offsets, argv_buf, environ_offsets, environ_buf) = - allocate_argv_environ(argv, environ); - argv_environ_init( - argv_environ, - argv_offsets.as_ptr(), - argv_offsets.len(), - argv_buf.as_ptr(), - argv_buf.len(), - environ_offsets.as_ptr(), - environ_offsets.len(), - environ_buf.as_ptr(), - environ_buf.len(), - ); - let curfds: *mut fd_table = &mut *curfds; - fd_table_init(curfds); + let args: Vec<&str> = argv.iter().map(AsRef::as_ref).collect(); + let mut wasi_ctx_builder = WasiCtxBuilder::new().args(&args).inherit_stdio(); - let prestats: *mut fd_prestats = &mut *prestats; - fd_prestats_init(prestats); - - // Prepopulate curfds with stdin, stdout, and stderr file descriptors. - assert!(fd_table_insert_existing(curfds, 0, 0)); - assert!(fd_table_insert_existing(curfds, 1, 1)); - assert!(fd_table_insert_existing(curfds, 2, 2)); + for (k, v) in environ { + wasi_ctx_builder = wasi_ctx_builder.env(k, v); + } - let mut wasm_fd = 3; - for (dir, fd) in preopened_dirs { - assert!(fd_table_insert_existing(curfds, wasm_fd, *fd)); - assert!(fd_prestats_insert( - prestats, - CString::new(dir.as_str()).unwrap().as_ptr(), - wasm_fd, - )); - wasm_fd += 1; - } + for (dir, f) in preopened_dirs { + wasi_ctx_builder = wasi_ctx_builder.preopened_dir( + f.try_clone().map_err(|err| { + InstantiationError::Resource(format!( + "couldn't clone an instance handle to pre-opened dir: {}", + err + )) + })?, + dir, + ); } - let host_state = WASIState { - curfds, - prestats, - argv_environ, - }; + let wasi_ctx = wasi_ctx_builder.build().map_err(|err| { + InstantiationError::Resource(format!("couldn't assemble WASI context object: {}", err)) + })?; InstanceHandle::new( Rc::new(module), @@ -167,37 +132,6 @@ pub fn instantiate_wasi( &data_initializers, signatures.into_boxed_slice(), None, - Box::new(host_state), + Box::new(wasi_ctx), ) } - -fn allocate_argv_environ( - argv: &[String], - environ: &[(String, String)], -) -> (Vec, Vec, Vec, Vec) { - let mut argv_offsets = Vec::new(); - let mut argv_buf = Vec::new(); - let mut environ_offsets = Vec::new(); - let mut environ_buf = Vec::new(); - - for arg in argv { - argv_offsets.push(argv_buf.len()); - for c in arg.bytes() { - argv_buf.push(c as libc::c_char); - } - argv_buf.push('\0' as libc::c_char); - } - for (key, value) in environ { - environ_offsets.push(environ_buf.len()); - for c in key.bytes() { - environ_buf.push(c as libc::c_char); - } - environ_buf.push('=' as libc::c_char); - for c in value.bytes() { - environ_buf.push(c as libc::c_char); - } - environ_buf.push('\0' as libc::c_char); - } - - (argv_offsets, argv_buf, environ_offsets, environ_buf) -} diff --git a/wasmtime-wasi/src/lib.rs b/wasmtime-wasi/src/lib.rs index d38f1d155b2f..52978224fed9 100644 --- a/wasmtime-wasi/src/lib.rs +++ b/wasmtime-wasi/src/lib.rs @@ -1,4 +1,3 @@ -extern crate cast; extern crate cranelift_codegen; extern crate cranelift_entity; extern crate cranelift_wasm; @@ -8,13 +7,9 @@ extern crate wasmtime_jit; extern crate wasmtime_runtime; #[macro_use] extern crate log; -extern crate errno; +extern crate wasi_common; -mod host; -mod host_impls; mod instantiate; mod syscalls; -mod translate; -mod wasm32; pub use instantiate::instantiate_wasi; diff --git a/wasmtime-wasi/src/syscalls.rs b/wasmtime-wasi/src/syscalls.rs index 6bdf465d991a..ce74f544e36f 100644 --- a/wasmtime-wasi/src/syscalls.rs +++ b/wasmtime-wasi/src/syscalls.rs @@ -1,45 +1,42 @@ -use crate::host::{argv_environ_values, fd_prestats, fd_table}; -use crate::instantiate::WASIState; use cranelift_codegen::ir::types::{Type, I32, I64}; -use host; -use host_impls; -use std::{mem, ptr, slice, str}; -use translate::*; -use wasm32; -use wasmtime_runtime::VMContext; - -fn str_for_trace<'str>(ptr: *const i8, len: usize) -> Result<&'str str, str::Utf8Error> { - str::from_utf8(unsafe { slice::from_raw_parts(ptr as *const u8, len) }) -} +use wasi_common::{host, hostcalls, memory, wasm32, WasiCtx}; +use wasmtime_runtime::{Export, VMContext}; fn return_encoded_errno(e: host::__wasi_errno_t) -> wasm32::__wasi_errno_t { - let errno = encode_errno(e); + let errno = memory::enc_errno(e); trace!(" -> errno={}", wasm32::strerror(errno)); errno } -unsafe fn get_curfds(vmctx: *mut VMContext) -> *mut fd_table { - (&mut *(&mut *vmctx) - .host_state() - .downcast_mut::() - .unwrap() - .curfds) as *mut fd_table -} - -unsafe fn get_prestats(vmctx: *mut VMContext) -> *mut fd_prestats { - (&mut *(&mut *vmctx) - .host_state() - .downcast_mut::() - .unwrap() - .prestats) as *mut fd_prestats +fn get_wasi_ctx(vmctx: &mut VMContext) -> Result<&mut WasiCtx, host::__wasi_errno_t> { + unsafe { + vmctx.host_state().downcast_mut::().ok_or_else(|| { + println!("!!! no host state named WasiCtx available"); + host::__WASI_EINVAL + }) + } } -unsafe fn get_argv_environ(vmctx: *mut VMContext) -> *mut argv_environ_values { - (&mut *(&mut *vmctx) - .host_state() - .downcast_mut::() - .unwrap() - .argv_environ) as *mut argv_environ_values +fn get_memory(vmctx: &mut VMContext) -> Result<&mut [u8], host::__wasi_errno_t> { + unsafe { + match vmctx.lookup_global_export("memory") { + Some(Export::Memory { + definition, + vmctx: _, + memory: _, + }) => Ok(std::slice::from_raw_parts_mut( + (*definition).base, + (*definition).current_length, + )), + x => { + println!( + "!!! no export named __wasi_memory, or the export isn't a mem: {:?}", + x + ); + Err(host::__WASI_EINVAL) + } + } + } } pub trait AbiRet { @@ -157,7 +154,6 @@ macro_rules! syscalls { } syscalls! { - pub unsafe extern "C" fn args_get( vmctx: *mut VMContext, argv: wasm32::uintptr_t, @@ -169,34 +165,17 @@ syscalls! { argv_buf, ); - let vmctx = &mut *vmctx; - let argv_environ = get_argv_environ(vmctx); - let argc = match cast::u32((*argv_environ).argc) { - Ok(argc) => argc, - Err(_) => return wasm32::__WASI_ENOMEM, - }; - let argv_buf_size = match cast::u32((*argv_environ).argv_buf_size) { - Ok(argc) => argc, - Err(_) => return wasm32::__WASI_ENOMEM, - }; - - let (host_argv_buf, _argv_buf_size) = match decode_char_slice(vmctx, argv_buf, argv_buf_size) { - Ok((argv_buf, argv_buf_size)) => (argv_buf, argv_buf_size), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - // Add 1 so that we can add an extra NULL pointer at the end. - let (argv, _argc) = match decode_charstar_slice(vmctx, argv, argc + 1) { - Ok((argv, argc)) => (argv, argc), + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - let mut host_argv = Vec::new(); - host_argv.resize((*argv_environ).argc + 1, ptr::null_mut()); - - let e = host::wasmtime_ssp_args_get(argv_environ, host_argv.as_mut_ptr(), host_argv_buf); - encode_charstar_slice(argv, host_argv, argv_buf, host_argv_buf); - - return_encoded_errno(e) + hostcalls::args_get(wasi_ctx, memory, argv, argv_buf) } pub unsafe extern "C" fn args_sizes_get( @@ -210,28 +189,17 @@ syscalls! { argv_buf_size, ); - let vmctx = &mut *vmctx; - let mut host_argc = 0; - if let Err(e) = decode_usize_byref(vmctx, argc) { - return return_encoded_errno(e); - } - let mut host_argv_buf_size = 0; - if let Err(e) = decode_usize_byref(vmctx, argv_buf_size) { - return return_encoded_errno(e); - } - - let vmctx = &mut *vmctx; - let argv_environ = get_argv_environ(vmctx); - - let e = host::wasmtime_ssp_args_sizes_get(argv_environ, &mut host_argc, &mut host_argv_buf_size); - - trace!(" | *argc={:?}", host_argc); - encode_usize_byref(vmctx, argc, host_argc); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | *argv_buf_size={:?}", host_argv_buf_size); - encode_usize_byref(vmctx, argv_buf_size, host_argv_buf_size); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::args_sizes_get(wasi_ctx, memory, argc, argv_buf_size) } pub unsafe extern "C" fn clock_res_get( @@ -245,19 +213,12 @@ syscalls! { resolution, ); - let vmctx = &mut *vmctx; - let clock_id = decode_clockid(clock_id); - let mut host_resolution = 0; - if let Err(e) = decode_timestamp_byref(vmctx, resolution) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_clock_res_get(clock_id, &mut host_resolution); - - trace!(" | *resolution={:?}", host_resolution); - encode_timestamp_byref(vmctx, resolution, host_resolution); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::clock_res_get(memory, clock_id, resolution) } pub unsafe extern "C" fn clock_time_get( @@ -273,20 +234,12 @@ syscalls! { time, ); - let vmctx = &mut *vmctx; - let clock_id = decode_clockid(clock_id); - let precision = decode_timestamp(precision); - let mut host_time = 0; - if let Err(e) = decode_timestamp_byref(vmctx, time) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_clock_time_get(clock_id, precision, &mut host_time); - - trace!(" | *time={:?}", host_time); - encode_timestamp_byref(vmctx, time, host_time); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::clock_time_get(memory, clock_id, precision, time) } pub unsafe extern "C" fn environ_get( @@ -300,34 +253,17 @@ syscalls! { environ_buf, ); - let vmctx = &mut *vmctx; - let argv_environ = get_argv_environ(vmctx); - let environ_count = match cast::u32((*argv_environ).environ_count) { - Ok(host_environ_count) => host_environ_count, - Err(_) => return wasm32::__WASI_ENOMEM, - }; - let environ_buf_size = match cast::u32((*argv_environ).environ_buf_size) { - Ok(host_environ_buf_size) => host_environ_buf_size, - Err(_) => return wasm32::__WASI_ENOMEM, - }; - - let (host_environ_buf, _environ_buf_len) = match decode_char_slice(vmctx, environ_buf, environ_buf_size) { - Ok((environ_buf, environ_buf_len)) => (environ_buf, environ_buf_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - // Add 1 so that we can add an extra NULL pointer at the end. - let (environ, _environ_count) = match decode_charstar_slice(vmctx, environ, environ_count + 1) { - Ok((environ, environ_count)) => (environ, environ_count), + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - let mut host_environ = Vec::new(); - host_environ.resize((*argv_environ).environ_count + 1, ptr::null_mut()); - let e = host::wasmtime_ssp_environ_get(argv_environ, host_environ.as_mut_ptr(), host_environ_buf); - - encode_charstar_slice(environ, host_environ, environ_buf, host_environ_buf); - - return_encoded_errno(e) + hostcalls::environ_get(wasi_ctx, memory, environ, environ_buf) } pub unsafe extern "C" fn environ_sizes_get( @@ -341,28 +277,17 @@ syscalls! { environ_buf_size, ); - let vmctx = &mut *vmctx; - let mut host_environ_count = 0; - if let Err(e) = decode_usize_byref(vmctx, environ_count) { - return return_encoded_errno(e); - } - let mut host_environ_buf_size = 0; - if let Err(e) = decode_usize_byref(vmctx, environ_buf_size) { - return return_encoded_errno(e); - } - - let vmctx = &mut *vmctx; - let argv_environ = get_argv_environ(vmctx); - - let e = host::wasmtime_ssp_environ_sizes_get(argv_environ, &mut host_environ_count, &mut host_environ_buf_size); - - trace!(" | *environ_count={:?}", host_environ_count); - encode_usize_byref(vmctx, environ_count, host_environ_count); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | *environ_buf_size={:?}", host_environ_buf_size); - encode_usize_byref(vmctx, environ_buf_size, host_environ_buf_size); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::environ_sizes_get(wasi_ctx, memory, environ_count, environ_buf_size) } pub unsafe extern "C" fn fd_prestat_get( @@ -372,19 +297,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_prestat_get(fd={:?}, buf={:#x?})", fd, buf); - let vmctx = &mut *vmctx; - let prestats = get_prestats(vmctx); - let fd = decode_fd(fd); - let mut host_buf = std::mem::zeroed(); - if let Err(e) = decode_prestat_byref(vmctx, buf) { - return return_encoded_errno(e); - } - - let e = host_impls::wasmtime_ssp_fd_prestat_get(&mut *prestats, fd, &mut host_buf); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - encode_prestat_byref(vmctx, buf, host_buf); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_prestat_get(wasi_ctx, memory, fd, buf) } pub unsafe extern "C" fn fd_prestat_dir_name( @@ -395,22 +318,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_prestat_dir_name(fd={:?}, path={:#x?}, path_len={})", fd, path, path_len); - let vmctx = &mut *vmctx; - let prestats = get_prestats(vmctx); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - - let e = host_impls::wasmtime_ssp_fd_prestat_dir_name( - &mut *prestats, - fd, - ::std::slice::from_raw_parts_mut(path, path_len), - ); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_prestat_dir_name(wasi_ctx, memory, fd, path, path_len) } pub unsafe extern "C" fn fd_close( @@ -419,14 +337,12 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_close(fd={:?})", fd); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let prestats = get_prestats(vmctx); - let fd = decode_fd(fd); - - let e = host::wasmtime_ssp_fd_close(curfds, prestats, fd); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_close(wasi_ctx, fd) } pub unsafe extern "C" fn fd_datasync( @@ -435,13 +351,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_datasync(fd={:?})", fd); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_datasync(curfds, fd); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_datasync(wasi_ctx, memory, fd) } pub unsafe extern "C" fn fd_pread( @@ -461,32 +381,25 @@ syscalls! { nread ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let iovs = match decode_iovec_slice(vmctx, iovs, iovs_len) { - Ok(iovs) => iovs, + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - let offset = decode_filesize(offset); - let mut host_nread = 0; - if let Err(e) = decode_usize_byref(vmctx, nread) { - return return_encoded_errno(e); - } - let e = host::wasmtime_ssp_fd_pread( - curfds, + hostcalls::fd_pread( + wasi_ctx, + memory, fd, - iovs.as_ptr(), - iovs.len(), + iovs, + iovs_len, offset, - &mut host_nread, - ); - - trace!(" | *nread={:?}", host_nread); - encode_usize_byref(vmctx, nread, host_nread); - - return_encoded_errno(e) + nread + ) } pub unsafe extern "C" fn fd_pwrite( @@ -506,32 +419,25 @@ syscalls! { nwritten ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let iovs = match decode_ciovec_slice(vmctx, iovs, iovs_len) { - Ok(iovs) => iovs, + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let offset = decode_filesize(offset); - let mut host_nwritten = 0; - if let Err(e) = decode_usize_byref(vmctx, nwritten) { - return return_encoded_errno(e); - } - let e = host::wasmtime_ssp_fd_pwrite( - curfds, + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; + + hostcalls::fd_pwrite( + wasi_ctx, + memory, fd, - iovs.as_ptr(), - iovs.len(), + iovs, + iovs_len, offset, - &mut host_nwritten, - ); - - trace!(" | *nwritten={:?}", host_nwritten); - encode_usize_byref(vmctx, nwritten, host_nwritten); - - return_encoded_errno(e) + nwritten + ) } pub unsafe extern "C" fn fd_read( @@ -549,24 +455,17 @@ syscalls! { nread ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let iovs = match decode_iovec_slice(vmctx, iovs, iovs_len) { - Ok(iovs) => iovs, + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let mut host_nread = 0; - if let Err(e) = decode_usize_byref(vmctx, nread) { - return return_encoded_errno(e); - } - let e = host::wasmtime_ssp_fd_read(curfds, fd, iovs.as_ptr(), iovs.len(), &mut host_nread); - - trace!(" | *nread={:?}", host_nread); - encode_usize_byref(vmctx, nread, host_nread); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_read(wasi_ctx, memory, fd, iovs, iovs_len, nread) } pub unsafe extern "C" fn fd_renumber( @@ -576,15 +475,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_renumber(from={:?}, to={:?})", from, to); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let prestats = get_prestats(vmctx); - let from = decode_fd(from); - let to = decode_fd(to); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_renumber(curfds, prestats, from, to); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_renumber(wasi_ctx, memory, from, to) } pub unsafe extern "C" fn fd_seek( @@ -602,22 +503,17 @@ syscalls! { newoffset ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let offset = decode_filedelta(offset); - let whence = decode_whence(whence); - let mut host_newoffset = 0; - if let Err(e) = decode_filesize_byref(vmctx, newoffset) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_fd_seek(curfds, fd, offset, whence, &mut host_newoffset); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | *newoffset={:?}", host_newoffset); - encode_filesize_byref(vmctx, newoffset, host_newoffset); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_seek(wasi_ctx, memory, fd, offset, whence, newoffset) } pub unsafe extern "C" fn fd_tell( @@ -627,20 +523,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_tell(fd={:?}, newoffset={:#x?})", fd, newoffset); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let mut host_newoffset = 0; - if let Err(e) = decode_filesize_byref(vmctx, newoffset) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_fd_tell(curfds, fd, &mut host_newoffset); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | *newoffset={:?}", host_newoffset); - encode_filesize_byref(vmctx, newoffset, host_newoffset); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_tell(wasi_ctx, memory, fd, newoffset) } pub unsafe extern "C" fn fd_fdstat_get( @@ -650,20 +543,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_fdstat_get(fd={:?}, buf={:#x?})", fd, buf); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let mut host_buf = std::mem::zeroed(); - if let Err(e) = decode_fdstat_byref(vmctx, buf) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_fd_fdstat_get(curfds, fd, &mut host_buf); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | *buf={:?}", host_buf); - encode_fdstat_byref(vmctx, buf, host_buf); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_fdstat_get(wasi_ctx, memory, fd, buf) } pub unsafe extern "C" fn fd_fdstat_set_flags( @@ -677,14 +567,12 @@ syscalls! { flags ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let flags = decode_fdflags(flags); - - let e = host::wasmtime_ssp_fd_fdstat_set_flags(curfds, fd, flags); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_fdstat_set_flags(wasi_ctx, fd, flags) } pub unsafe extern "C" fn fd_fdstat_set_rights( @@ -700,15 +588,23 @@ syscalls! { fs_rights_inheriting ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let fs_rights_base = decode_rights(fs_rights_base); - let fs_rights_inheriting = decode_rights(fs_rights_inheriting); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_fdstat_set_rights(curfds, fd, fs_rights_base, fs_rights_inheriting); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_fdstat_set_rights( + wasi_ctx, + memory, + fd, + fs_rights_base, + fs_rights_inheriting + ) } pub unsafe extern "C" fn fd_sync( @@ -717,13 +613,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_sync(fd={:?})", fd); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_sync(curfds, fd); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_sync(wasi_ctx, memory, fd) } pub unsafe extern "C" fn fd_write( @@ -741,24 +641,17 @@ syscalls! { nwritten ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let iovs = match decode_ciovec_slice(vmctx, iovs, iovs_len) { - Ok(iovs) => iovs, + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let mut host_nwritten = 0; - if let Err(e) = decode_usize_byref(vmctx, nwritten) { - return return_encoded_errno(e); - } - let e = host::wasmtime_ssp_fd_write(curfds, fd, iovs.as_ptr(), iovs.len(), &mut host_nwritten); - - trace!(" | *nwritten={:?}", host_nwritten); - encode_usize_byref(vmctx, nwritten, host_nwritten); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_write(wasi_ctx, memory, fd, iovs, iovs_len, nwritten) } pub unsafe extern "C" fn fd_advise( @@ -776,16 +669,17 @@ syscalls! { advice ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let offset = decode_filesize(offset); - let len = decode_filesize(len); - let advice = decode_advice(advice); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_advise(curfds, fd, offset, len, advice); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_advise(wasi_ctx, memory, fd, offset, len, advice) } pub unsafe extern "C" fn fd_allocate( @@ -796,15 +690,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_allocate(fd={:?}, offset={}, len={})", fd, offset, len); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let offset = decode_filesize(offset); - let len = decode_filesize(len); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_allocate(curfds, fd, offset, len); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_allocate(wasi_ctx, memory, fd, offset, len) } pub unsafe extern "C" fn path_create_directory( @@ -820,19 +716,17 @@ syscalls! { path_len, ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - - let e = host::wasmtime_ssp_path_create_directory(curfds, fd, path, path_len); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::path_create_directory(wasi_ctx, memory, fd, path, path_len) } pub unsafe extern "C" fn path_link( @@ -856,27 +750,27 @@ syscalls! { path_len1 ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd0 = decode_fd(fd0); - let flags0 = decode_lookupflags(flags0); - let (path0, path_len0) = match decode_char_slice(vmctx, path0, path_len0) { - Ok((path0, path_len0)) => (path0, path_len0), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let fd1 = decode_fd(fd1); - let (path1, path_len1) = match decode_char_slice(vmctx, path1, path_len1) { - Ok((path1, path_len1)) => (path1, path_len1), + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path0,path_len0)={:?}", str_for_trace(path0, path_len0)); - trace!(" | (path1,path_len1)={:?}", str_for_trace(path1, path_len1)); - - let e = - host::wasmtime_ssp_path_link(curfds, fd0, flags0, path0, path_len0, fd1, path1, path_len1); - - return_encoded_errno(e) + hostcalls::path_link( + wasi_ctx, + memory, + fd0, + flags0, + path0, + path_len0, + fd1, + path1, + path_len1 + ) } // TODO: When multi-value happens, switch to that instead of passing @@ -906,27 +800,19 @@ syscalls! { fd ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let dirfd = decode_fd(dirfd); - let dirflags = decode_lookupflags(dirflags); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), - Err(e) => return return_encoded_errno(e), - }; - let oflags = decode_oflags(oflags); - let fs_rights_base = decode_rights(fs_rights_base); - let fs_rights_inheriting = decode_rights(fs_rights_inheriting); - let fs_flags = decode_fdflags(fs_flags); - let mut host_fd = wasm32::__wasi_fd_t::max_value(); - if let Err(e) = decode_fd_byref(vmctx, fd) { - return return_encoded_errno(e); - } + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_path_open( - curfds, + hostcalls::path_open( + wasi_ctx, + memory, dirfd, dirflags, path, @@ -935,13 +821,8 @@ syscalls! { fs_rights_base, fs_rights_inheriting, fs_flags, - &mut host_fd, - ); - - trace!(" | *fd={:?}", host_fd); - encode_fd_byref(vmctx, fd, host_fd); - - return_encoded_errno(e) + fd + ) } pub unsafe extern "C" fn fd_readdir( @@ -961,34 +842,25 @@ syscalls! { buf_used, ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let (buf, buf_len) = match decode_char_slice(vmctx, buf, buf_len) { - Ok((buf, buf_len)) => (buf, buf_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let cookie = decode_dircookie(cookie); - let mut host_buf_used = 0; - if let Err(e) = decode_usize_byref(vmctx, buf_used) { - return return_encoded_errno(e); - } - trace!(" | (buf,buf_len)={:?}", str_for_trace(buf, buf_len)); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_readdir( - curfds, + hostcalls::fd_readdir( + wasi_ctx, + memory, fd, - buf as *mut host::void, + buf, buf_len, cookie, - &mut host_buf_used, - ); - - trace!(" | *buf_used={:?}", host_buf_used); - encode_usize_byref(vmctx, buf_used, host_buf_used); - - return_encoded_errno(e) + buf_used + ) } pub unsafe extern "C" fn path_readlink( @@ -1010,39 +882,26 @@ syscalls! { buf_used, ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let (buf, buf_len) = match decode_char_slice(vmctx, buf, buf_len) { - Ok((buf, buf_len)) => (buf, buf_len), + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - let mut host_buf_used = 0; - if let Err(e) = decode_usize_byref(vmctx, buf_used) { - return return_encoded_errno(e); - } - - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - let e = host::wasmtime_ssp_path_readlink( - curfds, + hostcalls::path_readlink( + wasi_ctx, + memory, fd, path, path_len, buf, buf_len, - &mut host_buf_used, - ); - - trace!(" | *buf_used={:?}", host_buf_used); - trace!(" | (buf,*buf_used)={:?}", str_for_trace(buf, host_buf_used)); - encode_usize_byref(vmctx, buf_used, host_buf_used); - - return_encoded_errno(e) + buf_used + ) } pub unsafe extern "C" fn path_rename( @@ -1064,25 +923,26 @@ syscalls! { path_len1, ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd0 = decode_fd(fd0); - let (path0, path_len0) = match decode_char_slice(vmctx, path0, path_len0) { - Ok((path0, path_len0)) => (path0, path_len0), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let fd1 = decode_fd(fd1); - let (path1, path_len1) = match decode_char_slice(vmctx, path1, path_len1) { - Ok((path1, path_len1)) => (path1, path_len1), + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path0,path_len0)={:?}", str_for_trace(path0, path_len0)); - trace!(" | (path1,path_len1)={:?}", str_for_trace(path1, path_len1)); - - let e = host::wasmtime_ssp_path_rename(curfds, fd0, path0, path_len0, fd1, path1, path_len1); - - return_encoded_errno(e) + hostcalls::path_rename( + wasi_ctx, + memory, + fd0, + path0, + path_len0, + fd1, + path1, + path_len1 + ) } pub unsafe extern "C" fn fd_filestat_get( @@ -1092,20 +952,17 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("fd_filestat_get(fd={:?}, buf={:#x?})", fd, buf); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let mut host_buf = std::mem::zeroed(); - if let Err(e) = decode_filestat_byref(vmctx, buf) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_fd_filestat_get(curfds, fd, &mut host_buf); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - trace!(" | *buf={:?}", host_buf); - encode_filestat_byref(vmctx, buf, host_buf); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_filestat_get(wasi_ctx, memory, fd, buf) } pub unsafe extern "C" fn fd_filestat_set_times( @@ -1122,16 +979,17 @@ syscalls! { fstflags ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let st_atim = decode_timestamp(st_atim); - let st_mtim = decode_timestamp(st_mtim); - let fstflags = decode_fstflags(fstflags); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_filestat_set_times(curfds, fd, st_atim, st_mtim, fstflags); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_filestat_set_times(wasi_ctx, memory, fd, st_atim, st_mtim, fstflags) } pub unsafe extern "C" fn fd_filestat_set_size( @@ -1145,14 +1003,17 @@ syscalls! { size ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let size = decode_filesize(size); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_fd_filestat_set_size(curfds, fd, size); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::fd_filestat_set_size(wasi_ctx, memory, fd, size) } pub unsafe extern "C" fn path_filestat_get( @@ -1172,27 +1033,17 @@ syscalls! { buf ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let flags = decode_lookupflags(flags); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let mut host_buf = std::mem::zeroed(); - if let Err(e) = decode_filestat_byref(vmctx, buf) { - return return_encoded_errno(e); - } - - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - let e = host::wasmtime_ssp_path_filestat_get(curfds, fd, flags, path, path_len, &mut host_buf); - - trace!(" | *buf={:?}", host_buf); - encode_filestat_byref(vmctx, buf, host_buf); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::path_filestat_get(wasi_ctx, memory, fd, flags, path, path_len, buf) } pub unsafe extern "C" fn path_filestat_set_times( @@ -1215,23 +1066,27 @@ syscalls! { fstflags ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let flags = decode_lookupflags(flags); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let st_atim = decode_timestamp(st_atim); - let st_mtim = decode_timestamp(st_mtim); - let fstflags = decode_fstflags(fstflags); - - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - let e = host::wasmtime_ssp_path_filestat_set_times(curfds, fd, flags, path, path_len, st_atim, st_mtim, fstflags); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::path_filestat_set_times( + wasi_ctx, + memory, + fd, + flags, + path, + path_len, + st_atim, + st_mtim, + fstflags + ) } pub unsafe extern "C" fn path_symlink( @@ -1251,24 +1106,25 @@ syscalls! { path_len1 ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let (path0, path_len0) = match decode_char_slice(vmctx, path0, path_len0) { - Ok((path0, path_len0)) => (path0, path_len0), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let fd = decode_fd(fd); - let (path1, path_len1) = match decode_char_slice(vmctx, path1, path_len1) { - Ok((path1, path_len1)) => (path1, path_len1), + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path0,path_len0)={:?}", str_for_trace(path0, path_len0)); - trace!(" | (path1,path_len1)={:?}", str_for_trace(path1, path_len1)); - - let e = host::wasmtime_ssp_path_symlink(curfds, path0, path_len0, fd, path1, path_len1); - - return_encoded_errno(e) + hostcalls::path_symlink( + wasi_ctx, + memory, + path0, + path_len0, + fd, + path1, + path_len1 + ) } pub unsafe extern "C" fn path_unlink_file( @@ -1284,19 +1140,17 @@ syscalls! { path_len ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - - let e = host::wasmtime_ssp_path_unlink_file(curfds, fd, path, path_len); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::path_unlink_file(wasi_ctx, memory, fd, path, path_len) } pub unsafe extern "C" fn path_remove_directory( @@ -1312,19 +1166,17 @@ syscalls! { path_len ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let fd = decode_fd(fd); - let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { - Ok((path, path_len)) => (path, path_len), + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); - - let e = host::wasmtime_ssp_path_remove_directory(curfds, fd, path, path_len); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::path_remove_directory(wasi_ctx, memory, fd, path, path_len) } pub unsafe extern "C" fn poll_oneoff( @@ -1342,63 +1194,37 @@ syscalls! { nevents, ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let in_ = match decode_subscription_slice(vmctx, in_, nsubscriptions) { - Ok(in_) => in_, - Err(e) => return return_encoded_errno(e), - }; - let (out, out_len) = match decode_event_slice(vmctx, out, nsubscriptions) { - Ok((out, out_len)) => (out, out_len), + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - let mut host_out = Vec::new(); - host_out.resize(out_len, mem::zeroed()); - let mut host_nevents = 0; - if let Err(e) = decode_usize_byref(vmctx, nevents) { - return return_encoded_errno(e); - } - assert!(in_.len() == host_out.len()); - - let e = host::wasmtime_ssp_poll_oneoff( - curfds, - in_.as_ptr(), - host_out.as_mut_ptr(), - in_.len(), - &mut host_nevents, - ); - - trace!(" | *nevents={:?}", host_nevents); - encode_usize_byref(vmctx, nevents, host_nevents); - - host_out.truncate(host_nevents); - if log_enabled!(log::Level::Trace) { - for (index, _event) in host_out.iter().enumerate() { - // TODO: Format the output for tracing. - trace!(" | *out[{}]=...", index); - } - } - encode_event_slice(out, host_out); - - return_encoded_errno(e) + hostcalls::poll_oneoff(memory, in_, out, nsubscriptions, nevents) } pub unsafe extern "C" fn proc_exit(_vmctx: *mut VMContext, rval: u32,) -> () { trace!("proc_exit(rval={:?})", rval); - let rval = decode_exitcode(rval); - - // TODO: Rather than call __wasi_proc_exit here, we should trigger a - // stack unwind similar to a trap. - host_impls::wasmtime_ssp_proc_exit(rval); + hostcalls::proc_exit(rval) } pub unsafe extern "C" fn proc_raise( - _vmctx: *mut VMContext, - _sig: wasm32::__wasi_signal_t, + vmctx: *mut VMContext, + sig: wasm32::__wasi_signal_t, ) -> wasm32::__wasi_errno_t { - unimplemented!("__wasi_proc_raise"); + trace!("proc_raise(sig={:?})", sig); + + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; + + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; + + hostcalls::proc_raise(wasi_ctx, memory, sig) } pub unsafe extern "C" fn random_get( @@ -1408,21 +1234,18 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("random_get(buf={:#x?}, buf_len={:?})", buf, buf_len); - let vmctx = &mut *vmctx; - let (buf, buf_len) = match decode_char_slice(vmctx, buf, buf_len) { - Ok((buf, buf_len)) => (buf, buf_len), + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, Err(e) => return return_encoded_errno(e), }; - let e = host::wasmtime_ssp_random_get(buf as *mut host::void, buf_len); - - return_encoded_errno(e) + hostcalls::random_get(memory, buf, buf_len) } pub unsafe extern "C" fn sched_yield(_vmctx: *mut VMContext,) -> wasm32::__wasi_errno_t { - let e = host_impls::wasmtime_ssp_sched_yield(); + trace!("sched_yield(void)"); - return_encoded_errno(e) + hostcalls::sched_yield() } pub unsafe extern "C" fn sock_recv( @@ -1441,33 +1264,26 @@ syscalls! { ro_datalen, ro_flags ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let sock = decode_fd(sock); - let ri_data = match decode_iovec_slice(vmctx, ri_data, ri_data_len) { - Ok(ri_data) => ri_data, + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let ri_flags = decode_riflags(ri_flags); - let mut host_ro_datalen = 0; - if let Err(e) = decode_usize_byref(vmctx, ro_datalen) { - return return_encoded_errno(e); - } - let mut host_ro_flags = 0; - if let Err(e) = decode_roflags_byref(vmctx, ro_flags) { - return return_encoded_errno(e); - } - - let e = host::wasmtime_ssp_sock_recv(curfds, sock, ri_data.as_ptr(), ri_data.len(), ri_flags, - &mut host_ro_datalen, &mut host_ro_flags); - // TODO: Format the output for tracing. - trace!(" | *ro_datalen={}", host_ro_datalen); - trace!(" | *ro_flags={}", host_ro_flags); - encode_usize_byref(vmctx, ro_datalen, host_ro_datalen); - encode_roflags_byref(vmctx, ro_flags, host_ro_flags); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::sock_recv( + wasi_ctx, + memory, + sock, + ri_data, + ri_data_len, + ri_flags, + ro_datalen, + ro_flags + ) } pub unsafe extern "C" fn sock_send( @@ -1484,25 +1300,25 @@ syscalls! { si_data, si_data_len, si_flags, so_datalen, ); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let sock = decode_fd(sock); - let si_data = match decode_ciovec_slice(vmctx, si_data, si_data_len) { - Ok(si_data) => si_data, + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, Err(e) => return return_encoded_errno(e), }; - let si_flags = decode_siflags(si_flags); - let mut host_so_datalen = 0; - if let Err(e) = decode_usize_byref(vmctx, so_datalen) { - return return_encoded_errno(e); - } - let e = host::wasmtime_ssp_sock_send(curfds, sock, si_data.as_ptr(), si_data.len(), si_flags, &mut host_so_datalen); - - trace!(" | *so_datalen={:?}", host_so_datalen); - encode_usize_byref(vmctx, so_datalen, host_so_datalen); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::sock_send( + wasi_ctx, + memory, + sock, + si_data, + si_data_len, + si_flags, + so_datalen + ) } pub unsafe extern "C" fn sock_shutdown( @@ -1512,13 +1328,16 @@ syscalls! { ) -> wasm32::__wasi_errno_t { trace!("sock_shutdown(sock={:?}, how={:?})", sock, how); - let vmctx = &mut *vmctx; - let curfds = get_curfds(vmctx); - let sock = decode_fd(sock); - let how = decode_sdflags(how); + let wasi_ctx = match get_wasi_ctx(&mut *vmctx) { + Ok(ctx) => ctx, + Err(e) => return return_encoded_errno(e), + }; - let e = host::wasmtime_ssp_sock_shutdown(curfds, sock, how); + let memory = match get_memory(&mut *vmctx) { + Ok(memory) => memory, + Err(e) => return return_encoded_errno(e), + }; - return_encoded_errno(e) + hostcalls::sock_shutdown(wasi_ctx, memory, sock, how) } } diff --git a/wasmtime-wasi/src/translate.rs b/wasmtime-wasi/src/translate.rs deleted file mode 100644 index 2eb0c811da0d..000000000000 --- a/wasmtime-wasi/src/translate.rs +++ /dev/null @@ -1,573 +0,0 @@ -use cast; -use cast::From as _0; -use host; -use std::mem::{align_of, size_of, zeroed}; -use std::slice; -use wasm32; -use wasmtime_runtime::{Export, VMContext}; - -/// Translate a wasm pointer into a native pointer. -/// -/// This is unsafe due to trusting the contents of vmctx. The pointer result -/// is bounds and alignment checked. -unsafe fn decode_ptr( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: usize, - align: usize, -) -> Result<*mut u8, host::__wasi_errno_t> { - match vmctx.lookup_global_export("memory") { - Some(Export::Memory { - definition, - vmctx: _, - memory: _, - }) => { - if len > 0 { - // Check for overflow within the access. - let last = match (ptr as usize).checked_add(len - 1) { - Some(sum) => sum, - None => { - println!("!!! overflow"); - return Err(host::__WASI_EFAULT as host::__wasi_errno_t); - } - }; - // Check for out of bounds. - if last >= (*definition).current_length { - println!("!!! out of bounds"); - return Err(host::__WASI_EFAULT as host::__wasi_errno_t); - } - } - // Check alignment. - if (ptr as usize) % align != 0 { - println!("!!! bad alignment: {} % {}", ptr, align); - return Err(host::__WASI_EINVAL as host::__wasi_errno_t); - } - // Ok, translate the address. - Ok((((*definition).base as usize) + (ptr as usize)) as *mut u8) - } - // No export named "__wasi_memory", or the export isn't a memory. - // FIXME: Is EINVAL the best code here? - x => { - println!( - "!!! no export named __wasi_memory, or the export isn't a mem: {:?}", - x - ); - Err(host::__WASI_EINVAL as host::__wasi_errno_t) - } - } -} - -unsafe fn decode_ptr_to( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, -) -> Result<*mut T, host::__wasi_errno_t> { - decode_ptr(vmctx, ptr, size_of::(), align_of::()).map(|ptr| ptr as *mut T) -} - -unsafe fn decode_pointee( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - // Check bounds and alignment. - decode_ptr_to::(vmctx, ptr)?; - Ok(()) -} - -pub unsafe fn encode_pointee(vmctx: &mut VMContext, ptr: wasm32::uintptr_t, t: T) { - // Bounds and alignment are checked in `decode_pointee`. - let ptr = decode_ptr_to::(vmctx, ptr).unwrap(); - - ptr.write(t); -} - -pub unsafe fn decode_slice_of( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result<(*mut T, usize), host::__wasi_errno_t> { - let len = cast::usize(len); - - let ptr = decode_ptr( - vmctx, - ptr, - size_of::().checked_mul(len).unwrap(), - align_of::(), - )? as *mut T; - - Ok((ptr, len)) -} - -pub fn encode_usize(len: usize) -> wasm32::size_t { - cast::u32(len).unwrap() -} - -pub fn encode_device(device: host::__wasi_device_t) -> wasm32::__wasi_device_t { - device -} - -pub fn encode_inode(inode: host::__wasi_inode_t) -> wasm32::__wasi_inode_t { - inode -} - -pub fn encode_linkcount(linkcount: host::__wasi_linkcount_t) -> wasm32::__wasi_linkcount_t { - linkcount -} - -pub fn decode_userdata(userdata: wasm32::__wasi_userdata_t) -> host::__wasi_userdata_t { - userdata -} - -pub fn encode_userdata(userdata: host::__wasi_userdata_t) -> wasm32::__wasi_userdata_t { - userdata -} - -pub fn decode_eventtype(eventtype: wasm32::__wasi_eventtype_t) -> host::__wasi_eventtype_t { - eventtype -} - -pub fn encode_eventtype(eventtype: host::__wasi_eventtype_t) -> wasm32::__wasi_eventtype_t { - eventtype -} - -pub fn decode_filesize(filesize: wasm32::__wasi_filesize_t) -> host::__wasi_filesize_t { - filesize -} - -pub fn encode_filesize(filesize: host::__wasi_filesize_t) -> wasm32::__wasi_filesize_t { - filesize -} - -pub fn encode_eventrwflags( - eventrwflags: host::__wasi_eventrwflags_t, -) -> wasm32::__wasi_eventrwflags_t { - eventrwflags -} - -pub fn decode_subclockflags( - subclockflags: wasm32::__wasi_subclockflags_t, -) -> host::__wasi_subclockflags_t { - subclockflags -} - -pub fn decode_fd(fd: wasm32::__wasi_fd_t) -> host::__wasi_fd_t { - fd -} - -pub fn decode_filedelta(filedelta: wasm32::__wasi_filedelta_t) -> host::__wasi_filedelta_t { - filedelta -} - -pub fn decode_whence(whence: wasm32::__wasi_whence_t) -> host::__wasi_whence_t { - whence -} - -pub fn decode_clockid(clockid: wasm32::__wasi_clockid_t) -> host::__wasi_clockid_t { - clockid -} - -pub fn decode_timestamp(timestamp: wasm32::__wasi_timestamp_t) -> host::__wasi_timestamp_t { - timestamp -} - -pub fn encode_timestamp(timestamp: host::__wasi_timestamp_t) -> wasm32::__wasi_timestamp_t { - timestamp -} - -pub fn decode_exitcode(exitcode: wasm32::__wasi_exitcode_t) -> host::__wasi_exitcode_t { - exitcode -} - -pub fn decode_lookupflags(lookupflags: wasm32::__wasi_lookupflags_t) -> host::__wasi_lookupflags_t { - lookupflags -} - -pub fn decode_oflags(oflags: wasm32::__wasi_oflags_t) -> host::__wasi_oflags_t { - oflags -} - -pub fn decode_advice(advice: wasm32::__wasi_advice_t) -> host::__wasi_advice_t { - advice -} - -pub fn decode_dircookie(dircookie: wasm32::__wasi_dircookie_t) -> host::__wasi_dircookie_t { - dircookie -} - -pub fn encode_preopentype(preopentype: host::__wasi_preopentype_t) -> wasm32::__wasi_preopentype_t { - preopentype -} - -pub fn encode_filetype(filetype: host::__wasi_filetype_t) -> wasm32::__wasi_filetype_t { - filetype -} - -pub fn decode_fstflags(fstflags: wasm32::__wasi_fstflags_t) -> host::__wasi_fstflags_t { - fstflags -} - -#[allow(dead_code)] -pub fn encode_fstflags(fstflags: host::__wasi_fstflags_t) -> wasm32::__wasi_fstflags_t { - fstflags -} - -pub fn decode_fdflags(fdflags: wasm32::__wasi_fdflags_t) -> host::__wasi_fdflags_t { - fdflags -} - -pub fn encode_fdflags(fdflags: host::__wasi_fdflags_t) -> wasm32::__wasi_fdflags_t { - fdflags -} - -pub fn decode_sdflags(sdflags: wasm32::__wasi_sdflags_t) -> host::__wasi_sdflags_t { - sdflags -} - -pub fn decode_rights(rights: wasm32::__wasi_rights_t) -> host::__wasi_rights_t { - rights -} - -pub fn encode_rights(rights: host::__wasi_rights_t) -> wasm32::__wasi_rights_t { - rights -} - -pub fn decode_riflags(riflags: wasm32::__wasi_riflags_t) -> host::__wasi_riflags_t { - riflags -} - -pub fn decode_siflags(siflags: wasm32::__wasi_siflags_t) -> host::__wasi_siflags_t { - siflags -} - -pub unsafe fn decode_char_slice( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result<(*mut host::char, usize), host::__wasi_errno_t> { - decode_slice_of::(vmctx, ptr, len) -} - -pub unsafe fn decode_charstar_slice( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - count: wasm32::size_t, -) -> Result<(*mut wasm32::uintptr_t, usize), host::__wasi_errno_t> { - decode_slice_of::(vmctx, ptr, count) -} - -pub unsafe fn encode_charstar_slice( - ptr: *mut wasm32::uintptr_t, - host_vec: Vec<*mut libc::c_char>, - guest_base: wasm32::uintptr_t, - host_base: *mut libc::c_char, -) { - for (i, host) in host_vec.iter().enumerate() { - let guest = if host.is_null() { - 0 - } else { - guest_base + (*host as usize - host_base as usize) as wasm32::uintptr_t - }; - ptr.add(i).write(guest); - } -} - -pub unsafe fn decode_ciovec( - vmctx: &mut VMContext, - ciovec: &wasm32::__wasi_ciovec_t, -) -> Result { - let len = cast::usize(ciovec.buf_len); - Ok(host::__wasi_ciovec_t { - buf: decode_ptr(vmctx, ciovec.buf, len, 1)? as *const host::void, - buf_len: len, - }) -} - -pub unsafe fn decode_iovec( - vmctx: &mut VMContext, - iovec: &wasm32::__wasi_iovec_t, -) -> Result { - let len = cast::usize(iovec.buf_len); - Ok(host::__wasi_iovec_t { - buf: decode_ptr(vmctx, iovec.buf, len, 1)? as *mut host::void, - buf_len: len, - }) -} - -pub unsafe fn decode_ciovec_slice( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result, host::__wasi_errno_t> { - let slice = decode_slice_of::(vmctx, ptr, len)?; - let slice = slice::from_raw_parts(slice.0, slice.1); - slice.iter().map(|iov| decode_ciovec(vmctx, iov)).collect() -} - -pub unsafe fn decode_iovec_slice( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result, host::__wasi_errno_t> { - let slice = decode_slice_of::(vmctx, ptr, len)?; - let slice = slice::from_raw_parts(slice.0, slice.1); - slice.iter().map(|iov| decode_iovec(vmctx, iov)).collect() -} - -pub fn decode_subscription( - guest_subscription: wasm32::__wasi_subscription_t, -) -> Result { - let mut host_subscription = host::__wasi_subscription_t { - userdata: decode_userdata(guest_subscription.userdata), - type_: decode_eventtype(guest_subscription.type_), - u: unsafe { zeroed() }, - }; - - match guest_subscription.type_ { - wasm32::__WASI_EVENTTYPE_CLOCK => unsafe { - host_subscription.u.clock = - host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t { - identifier: decode_userdata(guest_subscription.u.clock.identifier), - clock_id: decode_clockid(guest_subscription.u.clock.clock_id), - timeout: decode_timestamp(guest_subscription.u.clock.timeout), - precision: decode_timestamp(guest_subscription.u.clock.precision), - flags: decode_subclockflags(guest_subscription.u.clock.flags), - }; - }, - wasm32::__WASI_EVENTTYPE_FD_READ | wasm32::__WASI_EVENTTYPE_FD_WRITE => unsafe { - host_subscription - .u - .fd_readwrite = - host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t { - fd: decode_fd(guest_subscription.u.fd_readwrite.fd), - } - }, - _ => return Err(host::__WASI_EINVAL as host::__wasi_errno_t), - }; - - Ok(host_subscription) -} - -pub unsafe fn decode_subscription_slice( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result, host::__wasi_errno_t> { - let slice = decode_slice_of::(vmctx, ptr, len)?; - let slice = slice::from_raw_parts(slice.0, slice.1); - slice - .iter() - .map(|subscription| decode_subscription(*subscription)) - .collect() -} - -pub fn encode_event(host_event: host::__wasi_event_t) -> wasm32::__wasi_event_t { - let mut guest_event = wasm32::__wasi_event_t { - userdata: encode_userdata(host_event.userdata), - error: encode_errno(host_event.error), - type_: encode_eventtype(host_event.type_), - u: unsafe { zeroed() }, - __bindgen_padding_0: 0, - }; - - match u32::from(host_event.type_) { - host::__WASI_EVENTTYPE_CLOCK => {} - host::__WASI_EVENTTYPE_FD_READ | host::__WASI_EVENTTYPE_FD_WRITE => unsafe { - guest_event.u.fd_readwrite = - wasm32::__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t { - nbytes: encode_filesize(host_event.u.fd_readwrite.nbytes), - flags: encode_eventrwflags(host_event.u.fd_readwrite.flags), - __bindgen_padding_0: zeroed(), - } - }, - _ => panic!("unrecognized event type"), - }; - - guest_event -} - -pub unsafe fn decode_event_slice( - vmctx: &mut VMContext, - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result<(*mut wasm32::__wasi_event_t, usize), host::__wasi_errno_t> { - decode_slice_of::(vmctx, ptr, len) -} - -pub unsafe fn encode_event_slice( - ptr: *mut wasm32::__wasi_event_t, - host_vec: Vec, -) { - for (i, host) in host_vec.iter().enumerate() { - let guest = encode_event(*host); - - ptr.add(i * size_of::()) - .write(guest); - } -} - -pub unsafe fn decode_fd_byref( - vmctx: &mut VMContext, - fd_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, fd_ptr) -} - -pub unsafe fn encode_fd_byref( - vmctx: &mut VMContext, - fd_ptr: wasm32::uintptr_t, - fd: host::__wasi_fd_t, -) { - encode_pointee::(vmctx, fd_ptr, wasm32::size_t::cast(fd)) -} - -pub unsafe fn decode_timestamp_byref( - vmctx: &mut VMContext, - timestamp_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, timestamp_ptr) -} - -pub unsafe fn encode_timestamp_byref( - vmctx: &mut VMContext, - timestamp_ptr: wasm32::uintptr_t, - host_timestamp: host::__wasi_timestamp_t, -) { - encode_pointee::( - vmctx, - timestamp_ptr, - wasm32::__wasi_timestamp_t::cast(host_timestamp), - ) -} - -pub unsafe fn decode_filesize_byref( - vmctx: &mut VMContext, - filesize_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, filesize_ptr) -} - -pub unsafe fn encode_filesize_byref( - vmctx: &mut VMContext, - filesize_ptr: wasm32::uintptr_t, - host_filesize: host::__wasi_filesize_t, -) { - encode_pointee::( - vmctx, - filesize_ptr, - wasm32::__wasi_filesize_t::cast(host_filesize), - ) -} - -pub unsafe fn decode_roflags_byref( - vmctx: &mut VMContext, - roflags_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, roflags_ptr) -} - -pub unsafe fn encode_roflags_byref( - vmctx: &mut VMContext, - roflags_ptr: wasm32::uintptr_t, - host_roflags: host::__wasi_roflags_t, -) { - encode_pointee::( - vmctx, - roflags_ptr, - wasm32::__wasi_roflags_t::cast(host_roflags), - ) -} - -pub unsafe fn decode_usize_byref( - vmctx: &mut VMContext, - usize_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, usize_ptr) -} - -pub unsafe fn encode_usize_byref( - vmctx: &mut VMContext, - usize_ptr: wasm32::uintptr_t, - host_usize: usize, -) { - encode_pointee::(vmctx, usize_ptr, wasm32::size_t::cast(host_usize).unwrap()) -} - -pub unsafe fn decode_prestat_byref( - vmctx: &mut VMContext, - prestat_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, prestat_ptr)?; - Ok(()) -} - -pub unsafe fn encode_prestat_byref( - vmctx: &mut VMContext, - prestat_ptr: wasm32::uintptr_t, - host_prestat: host::__wasi_prestat_t, -) { - let wasm32_prestat = wasm32::__wasi_prestat_t { - pr_type: encode_preopentype(host_prestat.pr_type), - u: wasm32::__wasi_prestat_t___wasi_prestat_u { - dir: wasm32::__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t { - pr_name_len: encode_usize(host_prestat.u.dir.pr_name_len), - }, - }, - }; - - encode_pointee::(vmctx, prestat_ptr, wasm32_prestat) -} - -pub unsafe fn decode_fdstat_byref( - vmctx: &mut VMContext, - fdstat_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, fdstat_ptr)?; - Ok(()) -} - -pub unsafe fn encode_fdstat_byref( - vmctx: &mut VMContext, - fdstat_ptr: wasm32::uintptr_t, - host_fdstat: host::__wasi_fdstat_t, -) { - let wasm32_fdstat = wasm32::__wasi_fdstat_t { - fs_filetype: encode_filetype(host_fdstat.fs_filetype), - fs_flags: encode_fdflags(host_fdstat.fs_flags), - __bindgen_padding_0: 0, - fs_rights_base: encode_rights(host_fdstat.fs_rights_base), - fs_rights_inheriting: encode_rights(host_fdstat.fs_rights_inheriting), - }; - - encode_pointee::(vmctx, fdstat_ptr, wasm32_fdstat) -} - -pub unsafe fn decode_filestat_byref( - vmctx: &mut VMContext, - filestat_ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { - decode_pointee::(vmctx, filestat_ptr)?; - Ok(()) -} - -pub unsafe fn encode_filestat_byref( - vmctx: &mut VMContext, - filestat_ptr: wasm32::uintptr_t, - host_filestat: host::__wasi_filestat_t, -) { - let wasm32_filestat = wasm32::__wasi_filestat_t { - st_dev: encode_device(host_filestat.st_dev), - st_ino: encode_inode(host_filestat.st_ino), - st_filetype: encode_filetype(host_filestat.st_filetype), - st_nlink: encode_linkcount(host_filestat.st_nlink), - st_size: encode_filesize(host_filestat.st_size), - st_atim: encode_timestamp(host_filestat.st_atim), - st_mtim: encode_timestamp(host_filestat.st_mtim), - st_ctim: encode_timestamp(host_filestat.st_ctim), - }; - - encode_pointee::(vmctx, filestat_ptr, wasm32_filestat) -} - -pub fn encode_errno(e: host::__wasi_errno_t) -> wasm32::__wasi_errno_t { - assert!(e <= wasm32::__WASI_ENOTCAPABLE); - e -} diff --git a/wasmtime-wasi/src/wasm32.rs b/wasmtime-wasi/src/wasm32.rs deleted file mode 100644 index 8847674ce83e..000000000000 --- a/wasmtime-wasi/src/wasm32.rs +++ /dev/null @@ -1,1442 +0,0 @@ -//! WASI types as defined in wasm32. This file was originally generated -//! by running bindgen over wasi/core.h with a wasm32 target, and the content -//! still largely reflects that, however it's been heavily modified, to -//! be host-independent, to avoid exposing libc implementation details, -//! to clean up cases where the headers use complex preprocessor macros, -//! and to - -#![allow(non_camel_case_types)] -#![allow(dead_code)] - -// C types -pub type char = i8; -pub type schar = i8; -pub type uchar = u8; -pub type short = i16; -pub type ushort = u16; -pub type int = i32; -pub type uint = u32; -pub type long = i32; -pub type ulong = u32; -pub type longlong = i64; -pub type ulonglong = u64; - -// libc stdint types -pub type int8_t = i8; -pub type uint8_t = u8; -pub type int16_t = i16; -pub type uint16_t = u16; -pub type int32_t = i32; -pub type uint32_t = u32; -pub type int64_t = i64; -pub type uint64_t = u64; -pub type intmax_t = i64; -pub type uintmax_t = u64; -pub type int_least8_t = i8; -pub type int_least16_t = i16; -pub type int_least32_t = i32; -pub type int_least64_t = i64; -pub type uint_least8_t = u8; -pub type uint_least16_t = u16; -pub type uint_least32_t = u32; -pub type uint_least64_t = u64; -pub type int_fast8_t = i8; -pub type int_fast16_t = i32; -pub type int_fast32_t = i32; -pub type int_fast64_t = i64; -pub type uint_fast8_t = u8; -pub type uint_fast16_t = u32; -pub type uint_fast32_t = u32; -pub type uint_fast64_t = u64; -pub type size_t = ulong; -pub type intptr_t = long; -pub type uintptr_t = ulong; -pub type wchar_t = i32; - -// libc types -pub type dev_t = u64; -pub type uid_t = u32; -pub type gid_t = u32; -pub type ino_t = u64; -pub type ino64_t = u64; -pub type mode_t = u32; -pub type nlink_t = u64; -pub type off_t = i64; -pub type off64_t = i64; -pub type pid_t = i32; -pub type clock_t = i64; -pub type rlim_t = u64; -pub type rlim64_t = u64; -pub type id_t = u32; -pub type time_t = i64; -pub type useconds_t = u32; -pub type suseconds_t = i64; -pub type daddr_t = i32; -pub type key_t = i32; -pub type clockid_t = i32; -pub type timer_t = uintptr_t; // *mut ::std::os::raw::c_void -pub type blksize_t = i64; -pub type blkcnt_t = i64; -pub type blkcnt64_t = i64; -pub type fsblkcnt_t = u64; -pub type fsblkcnt64_t = u64; -pub type fsfilcnt_t = u64; -pub type fsfilcnt64_t = u64; -pub type fsword_t = i64; -pub type ssize_t = i32; -pub type loff_t = off64_t; -pub type caddr_t = uintptr_t; // *mut i8 -pub type socklen_t = u32; -pub type sig_atomic_t = i32; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct fsid_t { - pub __val: [i32; 2usize], -} -#[allow(non_snake_case)] -#[test] -fn bindgen_test_layout_fsid_t() { - assert_eq!( - ::std::mem::size_of::(), - 8usize, - concat!("Size of: ", stringify!(fsid_t)) - ); - assert_eq!( - ::std::mem::align_of::(), - 4usize, - concat!("Alignment of ", stringify!(fsid_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::())).__val as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(fsid_t), - "::", - stringify!(__val) - ) - ); -} - -// WASI types -pub type __wasi_advice_t = u8; -pub type __wasi_clockid_t = u32; -pub type __wasi_device_t = u64; -pub type __wasi_dircookie_t = u64; -pub type __wasi_errno_t = u16; -pub type __wasi_eventrwflags_t = u16; -pub type __wasi_eventtype_t = u8; -pub type __wasi_exitcode_t = u32; -pub type __wasi_fd_t = u32; -pub type __wasi_fdflags_t = u16; -pub type __wasi_fdsflags_t = u16; -pub type __wasi_filedelta_t = i64; -pub type __wasi_filesize_t = u64; -pub type __wasi_filetype_t = u8; -pub type __wasi_preopentype_t = u8; -pub type __wasi_fstflags_t = u16; -pub type __wasi_inode_t = u64; -pub type __wasi_linkcount_t = u32; -pub type __wasi_lookupflags_t = u32; -pub type __wasi_oflags_t = u16; -pub type __wasi_riflags_t = u16; -pub type __wasi_rights_t = u64; -pub type __wasi_roflags_t = u16; -pub type __wasi_sdflags_t = u8; -pub type __wasi_siflags_t = u16; -pub type __wasi_signal_t = u8; -pub type __wasi_subclockflags_t = u16; -pub type __wasi_timestamp_t = u64; -pub type __wasi_userdata_t = u64; -pub type __wasi_whence_t = u8; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_dirent_t { - pub d_next: __wasi_dircookie_t, - pub d_ino: __wasi_inode_t, - pub d_namlen: u32, - pub d_type: __wasi_filetype_t, - pub __bindgen_padding_0: [u8; 3usize], -} -#[test] -fn bindgen_test_layout_wasi_dirent_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_dirent_t>(), - 24usize, - concat!("Size of: ", stringify!(__wasi_dirent_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_next as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_dirent_t), - "::", - stringify!(d_next) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_ino as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_dirent_t), - "::", - stringify!(d_ino) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_namlen as *const _ as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(__wasi_dirent_t), - "::", - stringify!(d_namlen) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_type as *const _ as usize }, - 20usize, - concat!( - "Offset of field: ", - stringify!(__wasi_dirent_t), - "::", - stringify!(d_type) - ) - ); -} -#[repr(C)] -#[derive(Copy, Clone)] -pub struct __wasi_event_t { - pub userdata: __wasi_userdata_t, - pub error: __wasi_errno_t, - pub type_: __wasi_eventtype_t, - pub __bindgen_padding_0: u32, - pub u: __wasi_event_t___wasi_event_u, -} -#[repr(C)] -#[derive(Copy, Clone)] -pub union __wasi_event_t___wasi_event_u { - pub fd_readwrite: __wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t, - _bindgen_union_align: [u64; 2usize], -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t { - pub nbytes: __wasi_filesize_t, - pub flags: __wasi_eventrwflags_t, - pub __bindgen_padding_0: [u16; 3usize], -} -#[test] -fn bindgen_test_layout___wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>(), - 16usize, - concat!( - "Size of: ", - stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t) - ) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>(), - 8usize, - concat!( - "Alignment of ", - stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>())) - .nbytes as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t), - "::", - stringify!(nbytes) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>())) - .flags as *const _ as usize - }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t), - "::", - stringify!(flags) - ) - ); -} -#[test] -fn bindgen_test_layout___wasi_event_t___wasi_event_u() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t___wasi_event_u>(), - 16usize, - concat!("Size of: ", stringify!(__wasi_event_t___wasi_event_u)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_event_t___wasi_event_u>(), - 8usize, - concat!("Alignment of ", stringify!(__wasi_event_t___wasi_event_u)) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t___wasi_event_u>())).fd_readwrite as *const _ - as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t___wasi_event_u), - "::", - stringify!(fd_readwrite) - ) - ); -} -#[test] -fn bindgen_test_layout___wasi_event_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t>(), - 32usize, - concat!("Size of: ", stringify!(__wasi_event_t)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_event_t>(), - 8usize, - concat!("Alignment of ", stringify!(__wasi_event_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).userdata as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(userdata) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).error as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(error) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).type_ as *const _ as usize }, - 10usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(type_) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).u as *const _ as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(u) - ) - ); -} -#[repr(C)] -#[derive(Copy, Clone)] -pub struct __wasi_prestat_t { - pub pr_type: __wasi_preopentype_t, - pub u: __wasi_prestat_t___wasi_prestat_u, -} -#[repr(C)] -#[derive(Copy, Clone)] -pub union __wasi_prestat_t___wasi_prestat_u { - pub dir: __wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t, -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t { - pub pr_name_len: size_t, -} -#[test] -fn bindgen_test_layout___wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t>(), - 4usize, - concat!( - "Size of: ", - stringify!(__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t) - ) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t>(), - 4usize, - concat!( - "Alignment of ", - stringify!(__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t>())) - .pr_name_len as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t), - "::", - stringify!(pr_name_len) - ) - ); -} -#[test] -fn bindgen_test_layout___wasi_prestat_t___wasi_prestat_u() { - assert_eq!( - ::std::mem::size_of::<__wasi_prestat_t___wasi_prestat_u>(), - 4usize, - concat!("Size of: ", stringify!(__wasi_prestat_t___wasi_prestat_u)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_prestat_t___wasi_prestat_u>(), - 4usize, - concat!( - "Alignment of ", - stringify!(__wasi_prestat_t___wasi_prestat_u) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_prestat_t___wasi_prestat_u>())).dir as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_prestat_t___wasi_prestat_u), - "::", - stringify!(dir) - ) - ); -} -#[test] -fn bindgen_test_layout___wasi_prestat_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_prestat_t>(), - 8usize, - concat!("Size of: ", stringify!(__wasi_prestat_t)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_prestat_t>(), - 4usize, - concat!("Alignment of ", stringify!(__wasi_prestat_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_prestat_t>())).pr_type as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_prestat_t), - "::", - stringify!(pr_type) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_prestat_t>())).u as *const _ as usize }, - 4usize, - concat!( - "Offset of field: ", - stringify!(__wasi_prestat_t), - "::", - stringify!(u) - ) - ); -} -#[allow(non_snake_case)] -#[repr(C)] -#[derive(Copy, Clone)] -pub union __wasi_event_t__bindgen_ty_1 { - pub fd_readwrite: __wasi_event_t__bindgen_ty_1__bindgen_ty_1, - _bindgen_union_align: [u64; 2usize], -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_event_t__bindgen_ty_1__bindgen_ty_1 { - pub nbytes: __wasi_filesize_t, - pub flags: __wasi_eventrwflags_t, - pub __bindgen_padding_0: [u16; 3usize], -} -#[allow(non_snake_case)] -#[test] -fn bindgen_test_layout_wasi_event_t__bindgen_ty_1__bindgen_ty_1() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t__bindgen_ty_1__bindgen_ty_1>(), - 16usize, - concat!( - "Size of: ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_1) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_1>())).nbytes - as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_1), - "::", - stringify!(nbytes) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_1>())).flags as *const _ - as usize - }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_1), - "::", - stringify!(flags) - ) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_event_t__bindgen_ty_1__bindgen_ty_2 { - pub signal: __wasi_signal_t, - pub exitcode: __wasi_exitcode_t, -} -#[allow(non_snake_case)] -#[test] -fn bindgen_test_layout_wasi_event_t__bindgen_ty_1__bindgen_ty_2() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>(), - 8usize, - concat!( - "Size of: ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2) - ) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>(), - 4usize, - concat!( - "Alignment of ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>())).signal - as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2), - "::", - stringify!(signal) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>())).exitcode - as *const _ as usize - }, - 4usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2), - "::", - stringify!(exitcode) - ) - ); -} -#[allow(non_snake_case)] -#[test] -fn bindgen_test_layout_wasi_event_t__bindgen_ty_1() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t__bindgen_ty_1>(), - 16usize, - concat!("Size of: ", stringify!(__wasi_event_t__bindgen_ty_1)) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1>())).fd_readwrite as *const _ - as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t__bindgen_ty_1), - "::", - stringify!(fd_readwrite) - ) - ); -} -#[test] -fn bindgen_test_layout_wasi_event_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_event_t>(), - 32usize, - concat!("Size of: ", stringify!(__wasi_event_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).userdata as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(userdata) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).error as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(error) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).type_ as *const _ as usize }, - 10usize, - concat!( - "Offset of field: ", - stringify!(__wasi_event_t), - "::", - stringify!(type_) - ) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_fdstat_t { - pub fs_filetype: __wasi_filetype_t, - pub fs_flags: __wasi_fdflags_t, - pub __bindgen_padding_0: u32, - pub fs_rights_base: __wasi_rights_t, - pub fs_rights_inheriting: __wasi_rights_t, -} -#[test] -fn bindgen_test_layout_wasi_fdstat_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_fdstat_t>(), - 24usize, - concat!("Size of: ", stringify!(__wasi_fdstat_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_filetype as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_fdstat_t), - "::", - stringify!(fs_filetype) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_flags as *const _ as usize }, - 2usize, - concat!( - "Offset of field: ", - stringify!(__wasi_fdstat_t), - "::", - stringify!(fs_flags) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_rights_base as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_fdstat_t), - "::", - stringify!(fs_rights_base) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_rights_inheriting as *const _ as usize - }, - 16usize, - concat!( - "Offset of field: ", - stringify!(__wasi_fdstat_t), - "::", - stringify!(fs_rights_inheriting) - ) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_filestat_t { - pub st_dev: __wasi_device_t, - pub st_ino: __wasi_inode_t, - pub st_filetype: __wasi_filetype_t, - pub st_nlink: __wasi_linkcount_t, - pub st_size: __wasi_filesize_t, - pub st_atim: __wasi_timestamp_t, - pub st_mtim: __wasi_timestamp_t, - pub st_ctim: __wasi_timestamp_t, -} -#[test] -fn bindgen_test_layout_wasi_filestat_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_filestat_t>(), - 56usize, - concat!("Size of: ", stringify!(__wasi_filestat_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_dev as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_dev) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_ino as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_ino) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_filetype as *const _ as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_filetype) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_nlink as *const _ as usize }, - 20usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_nlink) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_size as *const _ as usize }, - 24usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_size) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_atim as *const _ as usize }, - 32usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_atim) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_mtim as *const _ as usize }, - 40usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_mtim) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_ctim as *const _ as usize }, - 48usize, - concat!( - "Offset of field: ", - stringify!(__wasi_filestat_t), - "::", - stringify!(st_ctim) - ) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_ciovec_t { - pub buf: uintptr_t, // *const ::std::os::raw::c_void - pub buf_len: size_t, -} -#[test] -fn bindgen_test_layout_wasi_ciovec_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_ciovec_t>(), - 8usize, - concat!("Size of: ", stringify!(__wasi_ciovec_t)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_ciovec_t>(), - 4usize, - concat!("Alignment of ", stringify!(__wasi_ciovec_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_ciovec_t>())).buf as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_ciovec_t), - "::", - stringify!(buf) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_ciovec_t>())).buf_len as *const _ as usize }, - 4usize, - concat!( - "Offset of field: ", - stringify!(__wasi_ciovec_t), - "::", - stringify!(buf_len) - ) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_iovec_t { - pub buf: uintptr_t, // *mut ::std::os::raw::c_void - pub buf_len: size_t, -} -#[test] -fn bindgen_test_layout_wasi_iovec_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_iovec_t>(), - 8usize, - concat!("Size of: ", stringify!(__wasi_iovec_t)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_iovec_t>(), - 4usize, - concat!("Alignment of ", stringify!(__wasi_iovec_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_iovec_t>())).buf as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_iovec_t), - "::", - stringify!(buf) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_iovec_t>())).buf_len as *const _ as usize }, - 4usize, - concat!( - "Offset of field: ", - stringify!(__wasi_iovec_t), - "::", - stringify!(buf_len) - ) - ); -} -#[repr(C)] -#[derive(Copy, Clone)] -pub struct __wasi_subscription_t { - pub userdata: __wasi_userdata_t, - pub type_: __wasi_eventtype_t, - pub __bindgen_padding_0: u32, - pub u: __wasi_subscription_t___wasi_subscription_u, -} -#[repr(C)] -#[derive(Copy, Clone)] -pub union __wasi_subscription_t___wasi_subscription_u { - pub clock: __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - pub fd_readwrite: - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, - _bindgen_union_align: [u64; 5usize], -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t { - pub identifier: __wasi_userdata_t, - pub clock_id: __wasi_clockid_t, - pub __bindgen_padding_0: u32, - pub timeout: __wasi_timestamp_t, - pub precision: __wasi_timestamp_t, - pub flags: __wasi_subclockflags_t, - pub __bindgen_padding_1: [u16; 3usize], -} -#[test] -fn bindgen_test_layout___wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t() { - assert_eq!( - ::std::mem::size_of::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >(), - 40usize, - concat!( - "Size of: ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t) - ) - ); - assert_eq!( - ::std::mem::align_of::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >(), - 8usize, - concat!( - "Alignment of ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >())) - .identifier as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), - "::", - stringify!(identifier) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >())) - .clock_id as *const _ as usize - }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), - "::", - stringify!(clock_id) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >())) - .timeout as *const _ as usize - }, - 16usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), - "::", - stringify!(timeout) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >())) - .precision as *const _ as usize - }, - 24usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), - "::", - stringify!(precision) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, - >())) - .flags as *const _ as usize - }, - 32usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), - "::", - stringify!(flags) - ) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t { - pub fd: __wasi_fd_t, -} -#[test] -fn bindgen_test_layout___wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t( -) { - assert_eq!( - ::std::mem::size_of::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, - >(), - 4usize, - concat!( - "Size of: ", - stringify!( - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t - ) - ) - ); - assert_eq!( - ::std::mem::align_of::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, - >(), - 4usize, - concat!( - "Alignment of ", - stringify!( - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t - ) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::< - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, - >())) - .fd as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!( - __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t - ), - "::", - stringify!(fd) - ) - ); -} -#[test] -fn bindgen_test_layout___wasi_subscription_t___wasi_subscription_u() { - assert_eq!( - ::std::mem::size_of::<__wasi_subscription_t___wasi_subscription_u>(), - 40usize, - concat!( - "Size of: ", - stringify!(__wasi_subscription_t___wasi_subscription_u) - ) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_subscription_t___wasi_subscription_u>(), - 8usize, - concat!( - "Alignment of ", - stringify!(__wasi_subscription_t___wasi_subscription_u) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_subscription_t___wasi_subscription_u>())).clock - as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u), - "::", - stringify!(clock) - ) - ); - assert_eq!( - unsafe { - &(*(::std::ptr::null::<__wasi_subscription_t___wasi_subscription_u>())).fd_readwrite - as *const _ as usize - }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t___wasi_subscription_u), - "::", - stringify!(fd_readwrite) - ) - ); -} -#[test] -fn bindgen_test_layout___wasi_subscription_t() { - assert_eq!( - ::std::mem::size_of::<__wasi_subscription_t>(), - 56usize, - concat!("Size of: ", stringify!(__wasi_subscription_t)) - ); - assert_eq!( - ::std::mem::align_of::<__wasi_subscription_t>(), - 8usize, - concat!("Alignment of ", stringify!(__wasi_subscription_t)) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_subscription_t>())).userdata as *const _ as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t), - "::", - stringify!(userdata) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_subscription_t>())).type_ as *const _ as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t), - "::", - stringify!(type_) - ) - ); - assert_eq!( - unsafe { &(*(::std::ptr::null::<__wasi_subscription_t>())).u as *const _ as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(__wasi_subscription_t), - "::", - stringify!(u) - ) - ); -} -pub fn strerror(errno: __wasi_errno_t) -> &'static str { - match errno { - __WASI_ESUCCESS => "__WASI_ESUCCESS", - __WASI_E2BIG => "__WASI_E2BIG", - __WASI_EACCES => "__WASI_EACCES", - __WASI_EADDRINUSE => "__WASI_EADDRINUSE", - __WASI_EADDRNOTAVAIL => "__WASI_EADDRNOTAVAIL", - __WASI_EAFNOSUPPORT => "__WASI_EAFNOSUPPORT", - __WASI_EAGAIN => "__WASI_EAGAIN", - __WASI_EALREADY => "__WASI_EALREADY", - __WASI_EBADF => "__WASI_EBADF", - __WASI_EBADMSG => "__WASI_EBADMSG", - __WASI_EBUSY => "__WASI_EBUSY", - __WASI_ECANCELED => "__WASI_ECANCELED", - __WASI_ECHILD => "__WASI_ECHILD", - __WASI_ECONNABORTED => "__WASI_ECONNABORTED", - __WASI_ECONNREFUSED => "__WASI_ECONNREFUSED", - __WASI_ECONNRESET => "__WASI_ECONNRESET", - __WASI_EDEADLK => "__WASI_EDEADLK", - __WASI_EDESTADDRREQ => "__WASI_EDESTADDRREQ", - __WASI_EDOM => "__WASI_EDOM", - __WASI_EDQUOT => "__WASI_EDQUOT", - __WASI_EEXIST => "__WASI_EEXIST", - __WASI_EFAULT => "__WASI_EFAULT", - __WASI_EFBIG => "__WASI_EFBIG", - __WASI_EHOSTUNREACH => "__WASI_EHOSTUNREACH", - __WASI_EIDRM => "__WASI_EIDRM", - __WASI_EILSEQ => "__WASI_EILSEQ", - __WASI_EINPROGRESS => "__WASI_EINPROGRESS", - __WASI_EINTR => "__WASI_EINTR", - __WASI_EINVAL => "__WASI_EINVAL", - __WASI_EIO => "__WASI_EIO", - __WASI_EISCONN => "__WASI_EISCONN", - __WASI_EISDIR => "__WASI_EISDIR", - __WASI_ELOOP => "__WASI_ELOOP", - __WASI_EMFILE => "__WASI_EMFILE", - __WASI_EMLINK => "__WASI_EMLINK", - __WASI_EMSGSIZE => "__WASI_EMSGSIZE", - __WASI_EMULTIHOP => "__WASI_EMULTIHOP", - __WASI_ENAMETOOLONG => "__WASI_ENAMETOOLONG", - __WASI_ENETDOWN => "__WASI_ENETDOWN", - __WASI_ENETRESET => "__WASI_ENETRESET", - __WASI_ENETUNREACH => "__WASI_ENETUNREACH", - __WASI_ENFILE => "__WASI_ENFILE", - __WASI_ENOBUFS => "__WASI_ENOBUFS", - __WASI_ENODEV => "__WASI_ENODEV", - __WASI_ENOENT => "__WASI_ENOENT", - __WASI_ENOEXEC => "__WASI_ENOEXEC", - __WASI_ENOLCK => "__WASI_ENOLCK", - __WASI_ENOLINK => "__WASI_ENOLINK", - __WASI_ENOMEM => "__WASI_ENOMEM", - __WASI_ENOMSG => "__WASI_ENOMSG", - __WASI_ENOPROTOOPT => "__WASI_ENOPROTOOPT", - __WASI_ENOSPC => "__WASI_ENOSPC", - __WASI_ENOSYS => "__WASI_ENOSYS", - __WASI_ENOTCONN => "__WASI_ENOTCONN", - __WASI_ENOTDIR => "__WASI_ENOTDIR", - __WASI_ENOTEMPTY => "__WASI_ENOTEMPTY", - __WASI_ENOTRECOVERABLE => "__WASI_ENOTRECOVERABLE", - __WASI_ENOTSOCK => "__WASI_ENOTSOCK", - __WASI_ENOTSUP => "__WASI_ENOTSUP", - __WASI_ENOTTY => "__WASI_ENOTTY", - __WASI_ENXIO => "__WASI_ENXIO", - __WASI_EOVERFLOW => "__WASI_EOVERFLOW", - __WASI_EOWNERDEAD => "__WASI_EOWNERDEAD", - __WASI_EPERM => "__WASI_EPERM", - __WASI_EPIPE => "__WASI_EPIPE", - __WASI_EPROTO => "__WASI_EPROTO", - __WASI_EPROTONOSUPPORT => "__WASI_EPROTONOSUPPORT", - __WASI_EPROTOTYPE => "__WASI_EPROTOTYPE", - __WASI_ERANGE => "__WASI_ERANGE", - __WASI_EROFS => "__WASI_EROFS", - __WASI_ESPIPE => "__WASI_ESPIPE", - __WASI_ESRCH => "__WASI_ESRCH", - __WASI_ESTALE => "__WASI_ESTALE", - __WASI_ETIMEDOUT => "__WASI_ETIMEDOUT", - __WASI_ETXTBSY => "__WASI_ETXTBSY", - __WASI_EXDEV => "__WASI_EXDEV", - __WASI_ENOTCAPABLE => "__WASI_ENOTCAPABLE", - other => panic!("Undefined errno value {:?}", other), - } -} - -pub fn whence_to_str(whence: __wasi_whence_t) -> &'static str { - match whence { - __WASI_WHENCE_CUR => "__WASI_WHENCE_CUR", - __WASI_WHENCE_END => "__WASI_WHENCE_END", - __WASI_WHENCE_SET => "__WASI_WHENCE_SET", - other => panic!("Undefined whence value {:?}", other), - } -} - -// libc constants -pub const INT8_MIN: i32 = -128; -pub const INT16_MIN: i32 = -32768; -pub const INT32_MIN: i32 = -2147483648; -pub const INT8_MAX: u32 = 127; -pub const INT16_MAX: u32 = 32767; -pub const INT32_MAX: u32 = 2147483647; -pub const UINT8_MAX: u32 = 255; -pub const UINT16_MAX: u32 = 65535; -pub const UINT32_MAX: u32 = 4294967295; -pub const INT_LEAST8_MIN: i32 = -128; -pub const INT_LEAST16_MIN: i32 = -32768; -pub const INT_LEAST32_MIN: i32 = -2147483648; -pub const INT_LEAST8_MAX: u32 = 127; -pub const INT_LEAST16_MAX: u32 = 32767; -pub const INT_LEAST32_MAX: u32 = 2147483647; -pub const UINT_LEAST8_MAX: u32 = 255; -pub const UINT_LEAST16_MAX: u32 = 65535; -pub const UINT_LEAST32_MAX: u32 = 4294967295; -pub const INT_FAST8_MIN: i32 = -128; -pub const INT_FAST16_MIN: i32 = -2147483648; -pub const INT_FAST32_MIN: i32 = -2147483648; -pub const INT_FAST8_MAX: u32 = 127; -pub const INT_FAST16_MAX: u32 = 2147483647; -pub const INT_FAST32_MAX: u32 = 2147483647; -pub const UINT_FAST8_MAX: u32 = 255; -pub const UINT_FAST16_MAX: u32 = 4294967295; -pub const UINT_FAST32_MAX: u32 = 4294967295; -pub const INTPTR_MIN: i32 = -2147483648; -pub const INTPTR_MAX: u32 = 2147483647; -pub const UINTPTR_MAX: u32 = 4294967295; -pub const PTRDIFF_MIN: i32 = -2147483648; -pub const PTRDIFF_MAX: u32 = 2147483647; -pub const SIG_ATOMIC_MIN: i32 = -2147483648; -pub const SIG_ATOMIC_MAX: u32 = 2147483647; -pub const SIZE_MAX: u32 = 4294967295; -pub const WINT_MIN: i32 = -2147483648; -pub const WINT_MAX: i32 = 2147483647; - -// WASI constants -pub const __WASI_ADVICE_NORMAL: __wasi_advice_t = 0; -pub const __WASI_ADVICE_SEQUENTIAL: __wasi_advice_t = 1; -pub const __WASI_ADVICE_RANDOM: __wasi_advice_t = 2; -pub const __WASI_ADVICE_WILLNEED: __wasi_advice_t = 3; -pub const __WASI_ADVICE_DONTNEED: __wasi_advice_t = 4; -pub const __WASI_ADVICE_NOREUSE: __wasi_advice_t = 5; -pub const __WASI_CLOCK_REALTIME: __wasi_clockid_t = 0; -pub const __WASI_CLOCK_MONOTONIC: __wasi_clockid_t = 1; -pub const __WASI_CLOCK_PROCESS_CPUTIME_ID: __wasi_clockid_t = 2; -pub const __WASI_CLOCK_THREAD_CPUTIME_ID: __wasi_clockid_t = 3; -pub const __WASI_DIRCOOKIE_START: __wasi_dircookie_t = 0; -pub const __WASI_ESUCCESS: __wasi_errno_t = 0; -pub const __WASI_E2BIG: __wasi_errno_t = 1; -pub const __WASI_EACCES: __wasi_errno_t = 2; -pub const __WASI_EADDRINUSE: __wasi_errno_t = 3; -pub const __WASI_EADDRNOTAVAIL: __wasi_errno_t = 4; -pub const __WASI_EAFNOSUPPORT: __wasi_errno_t = 5; -pub const __WASI_EAGAIN: __wasi_errno_t = 6; -pub const __WASI_EALREADY: __wasi_errno_t = 7; -pub const __WASI_EBADF: __wasi_errno_t = 8; -pub const __WASI_EBADMSG: __wasi_errno_t = 9; -pub const __WASI_EBUSY: __wasi_errno_t = 10; -pub const __WASI_ECANCELED: __wasi_errno_t = 11; -pub const __WASI_ECHILD: __wasi_errno_t = 12; -pub const __WASI_ECONNABORTED: __wasi_errno_t = 13; -pub const __WASI_ECONNREFUSED: __wasi_errno_t = 14; -pub const __WASI_ECONNRESET: __wasi_errno_t = 15; -pub const __WASI_EDEADLK: __wasi_errno_t = 16; -pub const __WASI_EDESTADDRREQ: __wasi_errno_t = 17; -pub const __WASI_EDOM: __wasi_errno_t = 18; -pub const __WASI_EDQUOT: __wasi_errno_t = 19; -pub const __WASI_EEXIST: __wasi_errno_t = 20; -pub const __WASI_EFAULT: __wasi_errno_t = 21; -pub const __WASI_EFBIG: __wasi_errno_t = 22; -pub const __WASI_EHOSTUNREACH: __wasi_errno_t = 23; -pub const __WASI_EIDRM: __wasi_errno_t = 24; -pub const __WASI_EILSEQ: __wasi_errno_t = 25; -pub const __WASI_EINPROGRESS: __wasi_errno_t = 26; -pub const __WASI_EINTR: __wasi_errno_t = 27; -pub const __WASI_EINVAL: __wasi_errno_t = 28; -pub const __WASI_EIO: __wasi_errno_t = 29; -pub const __WASI_EISCONN: __wasi_errno_t = 30; -pub const __WASI_EISDIR: __wasi_errno_t = 31; -pub const __WASI_ELOOP: __wasi_errno_t = 32; -pub const __WASI_EMFILE: __wasi_errno_t = 33; -pub const __WASI_EMLINK: __wasi_errno_t = 34; -pub const __WASI_EMSGSIZE: __wasi_errno_t = 35; -pub const __WASI_EMULTIHOP: __wasi_errno_t = 36; -pub const __WASI_ENAMETOOLONG: __wasi_errno_t = 37; -pub const __WASI_ENETDOWN: __wasi_errno_t = 38; -pub const __WASI_ENETRESET: __wasi_errno_t = 39; -pub const __WASI_ENETUNREACH: __wasi_errno_t = 40; -pub const __WASI_ENFILE: __wasi_errno_t = 41; -pub const __WASI_ENOBUFS: __wasi_errno_t = 42; -pub const __WASI_ENODEV: __wasi_errno_t = 43; -pub const __WASI_ENOENT: __wasi_errno_t = 44; -pub const __WASI_ENOEXEC: __wasi_errno_t = 45; -pub const __WASI_ENOLCK: __wasi_errno_t = 46; -pub const __WASI_ENOLINK: __wasi_errno_t = 47; -pub const __WASI_ENOMEM: __wasi_errno_t = 48; -pub const __WASI_ENOMSG: __wasi_errno_t = 49; -pub const __WASI_ENOPROTOOPT: __wasi_errno_t = 50; -pub const __WASI_ENOSPC: __wasi_errno_t = 51; -pub const __WASI_ENOSYS: __wasi_errno_t = 52; -pub const __WASI_ENOTCONN: __wasi_errno_t = 53; -pub const __WASI_ENOTDIR: __wasi_errno_t = 54; -pub const __WASI_ENOTEMPTY: __wasi_errno_t = 55; -pub const __WASI_ENOTRECOVERABLE: __wasi_errno_t = 56; -pub const __WASI_ENOTSOCK: __wasi_errno_t = 57; -pub const __WASI_ENOTSUP: __wasi_errno_t = 58; -pub const __WASI_ENOTTY: __wasi_errno_t = 59; -pub const __WASI_ENXIO: __wasi_errno_t = 60; -pub const __WASI_EOVERFLOW: __wasi_errno_t = 61; -pub const __WASI_EOWNERDEAD: __wasi_errno_t = 62; -pub const __WASI_EPERM: __wasi_errno_t = 63; -pub const __WASI_EPIPE: __wasi_errno_t = 64; -pub const __WASI_EPROTO: __wasi_errno_t = 65; -pub const __WASI_EPROTONOSUPPORT: __wasi_errno_t = 66; -pub const __WASI_EPROTOTYPE: __wasi_errno_t = 67; -pub const __WASI_ERANGE: __wasi_errno_t = 68; -pub const __WASI_EROFS: __wasi_errno_t = 69; -pub const __WASI_ESPIPE: __wasi_errno_t = 70; -pub const __WASI_ESRCH: __wasi_errno_t = 71; -pub const __WASI_ESTALE: __wasi_errno_t = 72; -pub const __WASI_ETIMEDOUT: __wasi_errno_t = 73; -pub const __WASI_ETXTBSY: __wasi_errno_t = 74; -pub const __WASI_EXDEV: __wasi_errno_t = 75; -pub const __WASI_ENOTCAPABLE: __wasi_errno_t = 76; -pub const __WASI_EVENT_FD_READWRITE_HANGUP: __wasi_eventrwflags_t = 1; -pub const __WASI_EVENTTYPE_CLOCK: __wasi_eventtype_t = 0; -pub const __WASI_EVENTTYPE_FD_READ: __wasi_eventtype_t = 1; -pub const __WASI_EVENTTYPE_FD_WRITE: __wasi_eventtype_t = 2; -pub const __WASI_FDFLAG_APPEND: __wasi_fdflags_t = 1; -pub const __WASI_FDFLAG_DSYNC: __wasi_fdflags_t = 2; -pub const __WASI_FDFLAG_NONBLOCK: __wasi_fdflags_t = 4; -pub const __WASI_FDFLAG_RSYNC: __wasi_fdflags_t = 8; -pub const __WASI_FDFLAG_SYNC: __wasi_fdflags_t = 16; -pub const __WASI_PREOPENTYPE_DIR: __wasi_preopentype_t = 0; -pub const __WASI_FILETYPE_UNKNOWN: __wasi_filetype_t = 0; -pub const __WASI_FILETYPE_BLOCK_DEVICE: __wasi_filetype_t = 1; -pub const __WASI_FILETYPE_CHARACTER_DEVICE: __wasi_filetype_t = 2; -pub const __WASI_FILETYPE_DIRECTORY: __wasi_filetype_t = 3; -pub const __WASI_FILETYPE_REGULAR_FILE: __wasi_filetype_t = 4; -pub const __WASI_FILETYPE_SOCKET_DGRAM: __wasi_filetype_t = 5; -pub const __WASI_FILETYPE_SOCKET_STREAM: __wasi_filetype_t = 6; -pub const __WASI_FILETYPE_SYMBOLIC_LINK: __wasi_filetype_t = 7; -pub const __WASI_FILESTAT_SET_ATIM: __wasi_fstflags_t = 1; -pub const __WASI_FILESTAT_SET_ATIM_NOW: __wasi_fstflags_t = 2; -pub const __WASI_FILESTAT_SET_MTIM: __wasi_fstflags_t = 4; -pub const __WASI_FILESTAT_SET_MTIM_NOW: __wasi_fstflags_t = 8; -pub const __WASI_LOOKUP_SYMLINK_FOLLOW: __wasi_lookupflags_t = 1; -pub const __WASI_O_CREAT: __wasi_oflags_t = 1; -pub const __WASI_O_DIRECTORY: __wasi_oflags_t = 2; -pub const __WASI_O_EXCL: __wasi_oflags_t = 4; -pub const __WASI_O_TRUNC: __wasi_oflags_t = 8; -pub const __WASI_SOCK_RECV_PEEK: __wasi_riflags_t = 1; -pub const __WASI_SOCK_RECV_WAITALL: __wasi_riflags_t = 2; -pub const __WASI_RIGHT_FD_DATASYNC: __wasi_rights_t = 1; -pub const __WASI_RIGHT_FD_READ: __wasi_rights_t = 2; -pub const __WASI_RIGHT_FD_SEEK: __wasi_rights_t = 4; -pub const __WASI_RIGHT_FD_FDSTAT_SET_FLAGS: __wasi_rights_t = 8; -pub const __WASI_RIGHT_FD_SYNC: __wasi_rights_t = 16; -pub const __WASI_RIGHT_FD_TELL: __wasi_rights_t = 32; -pub const __WASI_RIGHT_FD_WRITE: __wasi_rights_t = 64; -pub const __WASI_RIGHT_FD_ADVISE: __wasi_rights_t = 128; -pub const __WASI_RIGHT_FD_ALLOCATE: __wasi_rights_t = 256; -pub const __WASI_RIGHT_PATH_CREATE_DIRECTORY: __wasi_rights_t = 512; -pub const __WASI_RIGHT_PATH_CREATE_FILE: __wasi_rights_t = 1024; -pub const __WASI_RIGHT_PATH_LINK_SOURCE: __wasi_rights_t = 2048; -pub const __WASI_RIGHT_PATH_LINK_TARGET: __wasi_rights_t = 4096; -pub const __WASI_RIGHT_PATH_OPEN: __wasi_rights_t = 8192; -pub const __WASI_RIGHT_FD_READDIR: __wasi_rights_t = 16384; -pub const __WASI_RIGHT_PATH_READLINK: __wasi_rights_t = 32768; -pub const __WASI_RIGHT_PATH_RENAME_SOURCE: __wasi_rights_t = 65536; -pub const __WASI_RIGHT_PATH_RENAME_TARGET: __wasi_rights_t = 131072; -pub const __WASI_RIGHT_PATH_FILESTAT_GET: __wasi_rights_t = 262144; -pub const __WASI_RIGHT_PATH_FILESTAT_SET_SIZE: __wasi_rights_t = 524288; -pub const __WASI_RIGHT_PATH_FILESTAT_SET_TIMES: __wasi_rights_t = 1048576; -pub const __WASI_RIGHT_FD_FILESTAT_GET: __wasi_rights_t = 2097152; -pub const __WASI_RIGHT_FD_FILESTAT_SET_SIZE: __wasi_rights_t = 4194304; -pub const __WASI_RIGHT_FD_FILESTAT_SET_TIMES: __wasi_rights_t = 8388608; -pub const __WASI_RIGHT_PATH_SYMLINK: __wasi_rights_t = 16777216; -pub const __WASI_RIGHT_PATH_REMOVE_DIRECTORY: __wasi_rights_t = 33554432; -pub const __WASI_RIGHT_PATH_UNLINK_FILE: __wasi_rights_t = 67108864; -pub const __WASI_RIGHT_POLL_FD_READWRITE: __wasi_rights_t = 134217728; -pub const __WASI_RIGHT_SOCK_SHUTDOWN: __wasi_rights_t = 268435456; -pub const __WASI_SOCK_RECV_DATA_TRUNCATED: __wasi_roflags_t = 1; -pub const __WASI_SHUT_RD: __wasi_sdflags_t = 1; -pub const __WASI_SHUT_WR: __wasi_sdflags_t = 2; -pub const __WASI_SIGHUP: __wasi_signal_t = 1; -pub const __WASI_SIGINT: __wasi_signal_t = 2; -pub const __WASI_SIGQUIT: __wasi_signal_t = 3; -pub const __WASI_SIGILL: __wasi_signal_t = 4; -pub const __WASI_SIGTRAP: __wasi_signal_t = 5; -pub const __WASI_SIGABRT: __wasi_signal_t = 6; -pub const __WASI_SIGBUS: __wasi_signal_t = 7; -pub const __WASI_SIGFPE: __wasi_signal_t = 8; -pub const __WASI_SIGKILL: __wasi_signal_t = 9; -pub const __WASI_SIGUSR1: __wasi_signal_t = 10; -pub const __WASI_SIGSEGV: __wasi_signal_t = 11; -pub const __WASI_SIGUSR2: __wasi_signal_t = 12; -pub const __WASI_SIGPIPE: __wasi_signal_t = 13; -pub const __WASI_SIGALRM: __wasi_signal_t = 14; -pub const __WASI_SIGTERM: __wasi_signal_t = 15; -pub const __WASI_SIGCHLD: __wasi_signal_t = 16; -pub const __WASI_SIGCONT: __wasi_signal_t = 17; -pub const __WASI_SIGSTOP: __wasi_signal_t = 18; -pub const __WASI_SIGTSTP: __wasi_signal_t = 19; -pub const __WASI_SIGTTIN: __wasi_signal_t = 20; -pub const __WASI_SIGTTOU: __wasi_signal_t = 21; -pub const __WASI_SIGURG: __wasi_signal_t = 22; -pub const __WASI_SIGXCPU: __wasi_signal_t = 23; -pub const __WASI_SIGXFSZ: __wasi_signal_t = 24; -pub const __WASI_SIGVTALRM: __wasi_signal_t = 25; -pub const __WASI_SIGPROF: __wasi_signal_t = 26; -pub const __WASI_SIGWINCH: __wasi_signal_t = 27; -pub const __WASI_SIGPOLL: __wasi_signal_t = 28; -pub const __WASI_SIGPWR: __wasi_signal_t = 29; -pub const __WASI_SIGSYS: __wasi_signal_t = 30; -pub const __WASI_SUBSCRIPTION_CLOCK_ABSTIME: __wasi_subclockflags_t = 1; -pub const __WASI_WHENCE_CUR: __wasi_whence_t = 0; -pub const __WASI_WHENCE_END: __wasi_whence_t = 1; -pub const __WASI_WHENCE_SET: __wasi_whence_t = 2; From 649f3b0c89216caf2d6cb797eae00434312210b5 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sun, 12 May 2019 12:08:22 +0200 Subject: [PATCH 2/6] Update min rustc to 1.34 as required by wasi-common --- .travis.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 395c3a7399d4..ef7ab74114e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ os: - osx language: rust rust: - - 1.32.0 + - 1.34.0 - beta - nightly matrix: diff --git a/README.md b/README.md index 1259d4ce70db..0ccd29568624 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ utility or as a library embedded in a larger application. [![Travis Status](https://travis-ci.org/CraneStation/wasmtime.svg?branch=master)](https://travis-ci.org/CraneStation/wasmtime) [![Appveyor Status](https://ci.appveyor.com/api/projects/status/vxvpt2plriy5s0mc?svg=true)](https://ci.appveyor.com/project/CraneStation/cranelift) [![Gitter chat](https://badges.gitter.im/CraneStation/CraneStation.svg)](https://gitter.im/CraneStation/Lobby) -![Minimum rustc 1.32](https://img.shields.io/badge/rustc-1.32+-green.svg) +![Minimum rustc 1.34](https://img.shields.io/badge/rustc-1.34+-green.svg) Wasmtime passes the WebAssembly spec testsuite, and supports a new system API proposal called [WebAssembly System Interface], or WASI. From 20e13da32db75de065c30e7a1d454f1a6c9d73a4 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sun, 19 May 2019 10:29:24 +0200 Subject: [PATCH 3/6] Sync with wasi-common --- wasmtime-wasi/src/syscalls.rs | 55 +++++------------------------------ 1 file changed, 7 insertions(+), 48 deletions(-) diff --git a/wasmtime-wasi/src/syscalls.rs b/wasmtime-wasi/src/syscalls.rs index ce74f544e36f..c400b39917dd 100644 --- a/wasmtime-wasi/src/syscalls.rs +++ b/wasmtime-wasi/src/syscalls.rs @@ -356,12 +356,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_datasync(wasi_ctx, memory, fd) + hostcalls::fd_datasync(wasi_ctx, fd) } pub unsafe extern "C" fn fd_pread( @@ -480,12 +475,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_renumber(wasi_ctx, memory, from, to) + hostcalls::fd_renumber(wasi_ctx, from, to) } pub unsafe extern "C" fn fd_seek( @@ -593,14 +583,8 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - hostcalls::fd_fdstat_set_rights( wasi_ctx, - memory, fd, fs_rights_base, fs_rights_inheriting @@ -618,12 +602,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_sync(wasi_ctx, memory, fd) + hostcalls::fd_sync(wasi_ctx, fd) } pub unsafe extern "C" fn fd_write( @@ -674,12 +653,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_advise(wasi_ctx, memory, fd, offset, len, advice) + hostcalls::fd_advise(wasi_ctx, fd, offset, len, advice) } pub unsafe extern "C" fn fd_allocate( @@ -695,12 +669,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_allocate(wasi_ctx, memory, fd, offset, len) + hostcalls::fd_allocate(wasi_ctx, fd, offset, len) } pub unsafe extern "C" fn path_create_directory( @@ -984,12 +953,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_filestat_set_times(wasi_ctx, memory, fd, st_atim, st_mtim, fstflags) + hostcalls::fd_filestat_set_times(wasi_ctx, fd, st_atim, st_mtim, fstflags) } pub unsafe extern "C" fn fd_filestat_set_size( @@ -1008,12 +972,7 @@ syscalls! { Err(e) => return return_encoded_errno(e), }; - let memory = match get_memory(&mut *vmctx) { - Ok(memory) => memory, - Err(e) => return return_encoded_errno(e), - }; - - hostcalls::fd_filestat_set_size(wasi_ctx, memory, fd, size) + hostcalls::fd_filestat_set_size(wasi_ctx, fd, size) } pub unsafe extern "C" fn path_filestat_get( From bb4fef1e8529cc800a67ad5824fc984e10454816 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sun, 19 May 2019 20:17:38 -0700 Subject: [PATCH 4/6] Provide the C WASI implementation as an option. This adds the C WASI implementation as a new crate, wasmtime-wasi-c, and adds a command-line flag to the wasmtime command-line driver to select which WASI implementation to use. --- Cargo.toml | 1 + src/wasmtime.rs | 26 +- wasmtime-wasi-c/Cargo.toml | 30 + wasmtime-wasi-c/LICENSE | 220 ++ wasmtime-wasi-c/README.md | 8 + wasmtime-wasi-c/build.rs | 36 + wasmtime-wasi-c/js-polyfill/WASI-small.png | Bin 0 -> 21583 bytes wasmtime-wasi-c/js-polyfill/build.sh | 18 + wasmtime-wasi-c/js-polyfill/clang/stdatomic.h | 190 ++ wasmtime-wasi-c/js-polyfill/polyfill.c | 45 + wasmtime-wasi-c/js-polyfill/shell.html | 88 + wasmtime-wasi-c/js-polyfill/wasi.js | 497 +++ .../CMakeLists.txt | 8 + .../sandboxed-system-primitives/LICENSE | 7 + .../sandboxed-system-primitives/README.md | 9 + .../include/LICENSE | 121 + .../include/wasmtime_ssp.h | 866 +++++ .../sandboxed-system-primitives/src/LICENSE | 24 + .../sandboxed-system-primitives/src/README.md | 14 + .../sandboxed-system-primitives/src/config.h | 100 + .../sandboxed-system-primitives/src/locking.h | 215 ++ .../src/numeric_limits.h | 42 + .../sandboxed-system-primitives/src/posix.c | 2781 +++++++++++++++++ .../sandboxed-system-primitives/src/posix.h | 59 + .../sandboxed-system-primitives/src/queue.h | 92 + .../sandboxed-system-primitives/src/random.c | 92 + .../sandboxed-system-primitives/src/random.h | 20 + .../src/refcount.h | 47 + .../sandboxed-system-primitives/src/rights.h | 83 + .../sandboxed-system-primitives/src/signals.h | 17 + .../sandboxed-system-primitives/src/str.c | 33 + .../sandboxed-system-primitives/src/str.h | 19 + wasmtime-wasi-c/src/host.rs | 6 + wasmtime-wasi-c/src/instantiate.rs | 205 ++ wasmtime-wasi-c/src/lib.rs | 18 + wasmtime-wasi-c/src/syscalls.rs | 1557 +++++++++ wasmtime-wasi-c/src/translate.rs | 573 ++++ wasmtime-wasi-c/src/wasm32.rs | 1442 +++++++++ wasmtime-wasi/README.md | 2 +- 39 files changed, 9596 insertions(+), 15 deletions(-) create mode 100644 wasmtime-wasi-c/Cargo.toml create mode 100644 wasmtime-wasi-c/LICENSE create mode 100644 wasmtime-wasi-c/README.md create mode 100644 wasmtime-wasi-c/build.rs create mode 100644 wasmtime-wasi-c/js-polyfill/WASI-small.png create mode 100755 wasmtime-wasi-c/js-polyfill/build.sh create mode 100644 wasmtime-wasi-c/js-polyfill/clang/stdatomic.h create mode 100644 wasmtime-wasi-c/js-polyfill/polyfill.c create mode 100644 wasmtime-wasi-c/js-polyfill/shell.html create mode 100644 wasmtime-wasi-c/js-polyfill/wasi.js create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/CMakeLists.txt create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/LICENSE create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/README.md create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/include/LICENSE create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/include/wasmtime_ssp.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/LICENSE create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/README.md create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/config.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/numeric_limits.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/posix.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/queue.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/random.c create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/random.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/refcount.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/rights.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/signals.h create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/str.c create mode 100644 wasmtime-wasi-c/sandboxed-system-primitives/src/str.h create mode 100644 wasmtime-wasi-c/src/host.rs create mode 100644 wasmtime-wasi-c/src/instantiate.rs create mode 100644 wasmtime-wasi-c/src/lib.rs create mode 100644 wasmtime-wasi-c/src/syscalls.rs create mode 100644 wasmtime-wasi-c/src/translate.rs create mode 100644 wasmtime-wasi-c/src/wasm32.rs diff --git a/Cargo.toml b/Cargo.toml index 0f3dddf96983..28d9d9e7c2f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ wasmtime-jit = { path = "wasmtime-jit" } wasmtime-obj = { path = "wasmtime-obj" } wasmtime-wast = { path = "wasmtime-wast" } wasmtime-wasi = { path = "wasmtime-wasi" } +wasmtime-wasi-c = { path = "wasmtime-wasi-c" } docopt = "1.0.1" serde = "1.0.75" serde_derive = "1.0.75" diff --git a/src/wasmtime.rs b/src/wasmtime.rs index bdfda4825f3f..c573e3a7de12 100644 --- a/src/wasmtime.rs +++ b/src/wasmtime.rs @@ -50,6 +50,7 @@ use std::process::exit; use wabt; use wasmtime_jit::{ActionOutcome, Context}; use wasmtime_wasi::instantiate_wasi; +use wasmtime_wasi_c::instantiate_wasi_c; use wasmtime_wast::instantiate_spectest; static LOG_FILENAME_PREFIX: &str = "wasmtime.dbg."; @@ -62,8 +63,8 @@ including calling the start function if one is present. Additional functions given with --invoke are then called. Usage: - wasmtime [-odg] [--preload=...] [--env=...] [--dir=...] [--mapdir=...] [...] - wasmtime [-odg] [--preload=...] [--env=...] [--dir=...] [--mapdir=...] --invoke= [...] + wasmtime [-odg] [--wasi-common] [--preload=...] [--env=...] [--dir=...] [--mapdir=...] [...] + wasmtime [-odg] [--wasi-common] [--preload=...] [--env=...] [--dir=...] [--mapdir=...] --invoke= [...] wasmtime --help | --version Options: @@ -71,6 +72,7 @@ Options: -o, --optimize runs optimization passes on the translated functions -g generate debug information -d, --debug enable debug output on stderr/stdout + --wasi-common enable the wasi-common implementation of WASI --preload= load an additional wasm module before loading the main module --env= pass an environment variable (\"key=value\") to the program --dir= grant access to the given host directory @@ -92,6 +94,7 @@ struct Args { flag_env: Vec, flag_dir: Vec, flag_mapdir: Vec, + flag_wasi_common: bool, } fn read_to_end(path: PathBuf) -> Result, io::Error> { @@ -228,20 +231,15 @@ fn main() { let preopen_dirs = compute_preopen_dirs(&args.flag_dir, &args.flag_mapdir); let argv = compute_argv(&args.arg_file, &args.arg_arg); let environ = compute_environ(&args.flag_env); - context.name_instance( - "wasi_unstable".to_owned(), + + let wasi = if args.flag_wasi_common { instantiate_wasi("", global_exports, &preopen_dirs, &argv, &environ) - .expect("instantiating wasi"), - ); + } else { + instantiate_wasi_c("", global_exports, &preopen_dirs, &argv, &environ) + } + .expect("instantiating wasi"); - // FIXME: Also recognize "env", for compatibility with clang/llvm 8.0. And use - // "__wasi_" prefixes for compatibility with prototype reference-sysroot. - let global_exports = context.get_global_exports(); - context.name_instance( - "env".to_owned(), - instantiate_wasi("__wasi_", global_exports, &preopen_dirs, &argv, &environ) - .expect("instantiating wasi"), - ); + context.name_instance("wasi_unstable".to_owned(), wasi); // Enable/disable producing of debug info. context.set_debug_info(args.flag_g); diff --git a/wasmtime-wasi-c/Cargo.toml b/wasmtime-wasi-c/Cargo.toml new file mode 100644 index 000000000000..9952b4942c6a --- /dev/null +++ b/wasmtime-wasi-c/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "wasmtime-wasi-c" +version = "0.0.0" +authors = ["The Cranelift Project Developers"] +publish = false +description = "WASI API support for Wasmtime" +categories = ["wasm"] +repository = "https://github.com/CraneStation/wasmtime" +license = "Apache-2.0 WITH LLVM-exception" +readme = "README.md" + +[dependencies] +wasmtime-runtime = { path = "../wasmtime-runtime" } +wasmtime-environ = { path = "../wasmtime-environ" } +wasmtime-jit = { path = "../wasmtime-jit" } +cranelift-codegen = "0.30.0" +cranelift-entity = "0.30.0" +cranelift-wasm = "0.30.0" +target-lexicon = "0.3.0" +cast = { version = "0.2.2", default-features = false } +log = { version = "0.4.6", default-features = false } +libc = "0.2.50" + +[build-dependencies] +cmake = "0.1.35" +bindgen = "0.49.0" + +[badges] +maintenance = { status = "experimental" } +travis-ci = { repository = "CraneStation/wasmtime" } diff --git a/wasmtime-wasi-c/LICENSE b/wasmtime-wasi-c/LICENSE new file mode 100644 index 000000000000..f9d81955f4bc --- /dev/null +++ b/wasmtime-wasi-c/LICENSE @@ -0,0 +1,220 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +--- LLVM Exceptions to the Apache 2.0 License ---- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into an Object form of such source code, you +may redistribute such embedded portions in such Object form without complying +with the conditions of Sections 4(a), 4(b) and 4(d) of the License. + +In addition, if you combine or link compiled forms of this Software with +software that is licensed under the GPLv2 ("Combined Software") and if a +court of competent jurisdiction determines that the patent provision (Section +3), the indemnity provision (Section 9) or other Section of the License +conflicts with the conditions of the GPLv2, you may retroactively and +prospectively choose to deem waived or otherwise exclude such Section(s) of +the License, but only in their entirety and only with respect to the Combined +Software. + diff --git a/wasmtime-wasi-c/README.md b/wasmtime-wasi-c/README.md new file mode 100644 index 000000000000..56585967b457 --- /dev/null +++ b/wasmtime-wasi-c/README.md @@ -0,0 +1,8 @@ +This is the `wasmtime-wasi-c` crate, which implements the +WebAssembly System Interface (WASI) API in C. + +WASI is greatly inspired by and directly derived from [CloudABI]. +It differs in that it has aspirations to expand to a greater +scope, and to better support the needs of WebAssembly engines. + +[CloudABI]: https://cloudabi.org/ diff --git a/wasmtime-wasi-c/build.rs b/wasmtime-wasi-c/build.rs new file mode 100644 index 000000000000..ed070ea88b4a --- /dev/null +++ b/wasmtime-wasi-c/build.rs @@ -0,0 +1,36 @@ +extern crate bindgen; +extern crate cmake; + +use cmake::Config; +use std::env; +use std::path::PathBuf; + +fn main() { + let dst = Config::new("sandboxed-system-primitives").build(); + + println!("cargo:rustc-link-search=native={}", dst.display()); + println!("cargo:rustc-link-lib=static=SandboxedSystemPrimitives"); + + let bindings_builder = bindgen::Builder::default() + .header("sandboxed-system-primitives/include/wasmtime_ssp.h") + .header("sandboxed-system-primitives/src/posix.h") + .whitelist_function("wasmtime_ssp_.*") + .whitelist_function("fd_table_init") + .whitelist_function("fd_table_insert_existing") + .whitelist_function("fd_prestats_init") + .whitelist_function("fd_prestats_insert") + .whitelist_function("argv_environ_init") + .whitelist_type("__wasi_.*") + .whitelist_type("fd_table") + .whitelist_type("fd_prestats") + .whitelist_type("argv_environ_values") + .whitelist_var("__WASI_.*"); + + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + + bindings_builder + .generate() + .expect("Unable to generate bindings") + .write_to_file(out_path.join("wasmtime_ssp.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/wasmtime-wasi-c/js-polyfill/WASI-small.png b/wasmtime-wasi-c/js-polyfill/WASI-small.png new file mode 100644 index 0000000000000000000000000000000000000000..ef55a0bf6df48d1685d327c087d8be17ace9acfb GIT binary patch literal 21583 zcmeFZcT`i`_b;jU!(l%nfSE5pT(6ckFF@cdZFt#yN!WlnL(iImjps`V2OdlglNIMLTB60 zX$z@`GyZQk9_GISbOp*=axbaF?_P-(fAMBQcq>DL;=8uyO%zYEP3NLc%J`X>&6??-HJA} zB)M_*N)Joi1el+9izsvz&8uU@(GjS0KgX)Rh1ztn1;`a#;X4+Cq1T`=Kmqkr7&BvcE`od0H2mMZ{W zxQTP`QE|KM5x+ws^b&m9t3YQp|NZ}X>&!^GonUuL7m)zE2ugeb3?;~EaWg)V6yI54 zx06Q9JLUfLYqkSFEYRH((y5+ktUz+*N(ZAYIw6Ct4t!oDF0vfVPaH65KkRlBQ&HaI zjRwp)Flk=i@b8xFUG9hQ-+~mUzg;lz**53?nP&Q}m~F7govGpy^93tvfZ#VKIuQuQ z%MOVKcU)YP_n?^%v_rBfXFk03&-xsVVrCak_F2?y9|f0dBhRL15%;5hT)pyD@6pq* zVGwjf+nLYgEFOAj5)<7+b54K>Zf=L=%*AY1&{15u;x78xBmBO?X+uqLg%%zWnwZXe zvQwjxD0EcwpYeO`Guv#u2|MR*7x9@!0^&}}Ly ztO%G`@+nU3BeLDbYsyV^4Y6Crs_LC`_1$g+9mINA1yQ#;jB|^@f1R%i6o*{O<}v+I zM4`#?GbR%;f{e$4c;!re+gx;7+#$4V}j)u&4dKKBLmd}64(M?!s6Glr%B>6+etcrBTZ z6-y$$C(AMoyoF0r#xE#u@|J*VC9*2!J?*Bl>(`uu%z&|Z2U`oWps4-C9AFtw%`hlY z$B-XhnXjS0MM=Hz*=7ii0`r&Bf0!TprDLl>U!d}VhUveOFyW_u-`+gGF8&|6snoPJGr$)Rc-wLJv;k&FsFA*ms zOfV%<-LB_(IHROZO&!@6A7nDas|>Ha2iE_#x(tGxT>u8}Choa!4%ITi_cG}} zh7GVyz%TzfKX(V@3mD!1dA8LIs?WWxbg4v zKPDT{O<oAiH$fq#+y-(uk3r2h*H{Cn2_x&B`;;4Wx!^NK}KoxHjS1zZdd9 z*Z+?){kJCl=lb7s|G&xf-?RSTV&Gq@{QqZt`p?|s*+}mI>SBOoORP+(v`!qj99I!e zJdF93l|a!k7Q)QA7pdQ)!cm{d<|J@=Nw1S3Cz5!0hlKpyqFi|>gGxOv7~Z7Kn6|Kf z1nrY(Cu3K}f@d&4W&0q<#D0~?0zObY_3>(;>+6Jf&Zd+r%7(X>mFG(lP1>R3yrR!o zYu?k*&}SIsS$5W}>@lG_Hrm}neg$k$shp5^mAAgpC7#!M(%uHH-W=v#CA~;cv$nD1 z6|C3ZTi*W*&~QqMBNHe#^RS*W^sKL-E_l5F+P~VrUY^4?79IjK7MUH>gytmp%F$tY z5#o<(qF>rDDnmM?Yo^?Wr2{pjy@W?gK$B@Rb{SHbamA35Vm=aiMpeX#YUd`t>Dn0L zZ)NkWpZy!ZhGN>?O_EOMcx>b~m4|gai?*s#)Ywi8_MbZxE~R#SDp_Gibv!}`CZt3- zJA9I>FPD=0QrrteH8v@pM7?HzpFp9siv+!q%xVRAz8}q!{C%>c8+UIl|LD2a3xMH` z99q?ewbSxSPp?&zCZTxlh7>w2YoBEoNj+36Y!)n$iK$4DNKLJx)H5@ILuyhTa$-#* zvpZ+L{kZ*(=$>!s@+C@KywuJ=8{koNBm-f(>Dl&-W9RE}ey){snQ6~jbmG9&0Zz|n z)7Yd$9>EtT8&R%7S+@2DLGNp-eKBnA-xZOVC7$=8IHGD8QEUVyFUK%az8|fu$UT&^h>$8q0@0I3i$?o8nbx+>R_ zRt=^oo1_8W72#U@?&(%OKJ%rm-$cW&yyoJT6|LW=>Ij2xko{5=nv|*{Tgx`-9}^;* z-cv$r^%PqMwJ~tHA!4RTxg5HNO>@c)NGmElj|$I@I7Mdr?Q9$K$4VXQ&oX~R9=`XL zrAui)iazR6;cJr2$(|>87e4JG{TVfTA># zXEP???-*7GO!2mHQ1Za9zU~2x+?lbmoraCLx`cnmdSjK%toa4UZ2zL? zbAAq~{mBQiyJ-~@ZFO*JHUG!^AEK1=geHbH1t;Fe4eFULrT9WgpS$m;eVB_{ z-eh<=qZLfb_*#EU+=g(iq)P3>S^uFcXn*pIe5P}L*uukO)|I{W{`7qZdj<13c%y0W z<@mwC`(zLQnato9z`m`i0-NMjHysKKEipZ6UG@+AfchOjw@`Qb=}jNkLMeTwEK1xK zA~BS+9(URm$;NNC;~TauzaYr-bHo3q%B18FLhF4+5 zzfYLATidUMEV6p}0EfTG)w}53EQ!S7T;D+P^oEf%Q2&z6@ONjsuXfF+kqRdqF3}Ss zE3Q&9u|+z)4OvUAP& z1_J&9)Hsoo*&89%Z}YB$-iy|T0c|(5OC9H__?Hvr2IhM$>h~MpT%ns7T)^!!OE&7Scr!9e2za!nxOuP;}bGa(L zaTG~Di&kEAQH`9pGrVRGvE7B&0B)n!?^95;XG^-!bq@<`jXL7KZ_`Fqw|N}+TGU5) zkGuGnAeO)3ABddEc(8SDQdmLq)}v=k`+KC544FD1sj>4SNk+bkz%FgR+^rw8RsZ7v z^zJ^)mRj>=U;SY7{+HIK0eX77tX=Gk{9==#ODoV&`5yL@0SxNycYGE$Xq$NPrSU%2 zP}D9D`~Z5f#MNkm-3YYZL_1{`2^%K6Lb>~tJ&LIH()uBuX0P?K>9tW2FHnq(Z+5<2 z)nj4MMeV-O%VE}^zTfvsA9&6{S=n6WWnb#J*|bX7+M6?wSemQI^EO+RPme<1tf-d6 zc4r`dU@8xOt-jCy$#b)TYJ|a+Df_FqO@`pYRH^qwS!tqiaT{#;$h>|NrX|0enk$Z-k7HkJ@*S9-E55Qh1S z?D2E=Tt7;jSIAU)!`E?a(u@WuFz1HrY-bfV(eYA?~_c3k@boc>JKubHY%K?qvthk@6qi zj>D_Ma=aOO>TyK^yeLt>0%L;?;@TVKo|=(@pHkqTZEkkH-#V-3b;b+pLbyN6_9D*> z-xX{Jv{ge}B}2o7SV=n*^n;GyrCzLwl*^9Pw3Y{{^d@GN)N+oY1#?DbVG!=A1Prgj zedWLqFF6t1m2c2rw_XwTjT>N9+l7zcTA_xzCC%IZhRPPO&3t_q7XzDvZ zj4!@*W}LLxQLi_gG%0VCA*vEX?>%bOtbKn}Sb%KJrp$WAK1bIpQ9paoKFY88JBV<& zCMdSxp*(h<>Ve1(WuxQrF}INave}6iR`??|5}S|0!irmKoW#fSk<8r z&P7HG5(TewWs6&%WP<%Sv&UB{yezL?C)_G-;q;hwYOkBI4%jcNA_!9r83_9uH-~h6 z_id>erP_{Mu{$W9H+Goi3qfaDwJdxSD7P}plN(8Ic_xk6=O<3Q6_uN;Vq**>Q%l@` zhE+5(KJ8;oH{BPUH?1g5HGcqz@B2`1LMJNPn;HmdQ)b#V?Lx{MugFI&WGtD}^Q zbM3STj@$06cq;f%uo3FEj|GR5K^wn66N=iv5_zYXPOEm`xf==;a{^_na1EngKW6r_18lGTu^cUcvmcfwydPHc%qjX zAo2Z9GpFpSd<)Z6eV02TxA=kmWv0g*;r2H0OX=q!3iim#;$d&`&Za}fu)&ul>V3C(}OW7+>xgvB%O@91T_RRFMLJ zMJ`t~r|fS0T-nkmj)JG5eENLG{9qI2rqLoZtA-OZ5o65o8_@6U{Eo_D6;>BN)G;~E z$@TVXL)7LpOpZgJb3s;Q)4tb zJZ#Twx_PZ2@-~5Xs>fE-hho+1n9mV{aPK#EDCklT&R3H1eA>VLxDYh|6Pqg!&GO4HqnWT*y+7ny&ibtkiUAl_=$XR{I|Nhk4A7 zjVS5$l4T1ti`GrRS~Ey!fXyINqS+4ff5_T2M93!8y9zce!rjx)5S)X`>Pct}o%FCR zEzd7Q<7N)Vmi6f|)QjRw!^dHZ<~svYcXCj@pZWtsi?)(M2JWYeZuiO{kG}x{j^=Y% zjyo)LGt%e1Wq4-V^s5XxSVusJxVqh_)%{_N!lNbC# z7WesWQC@0R1DzFv5-Oam=kRXEbouGVJD3rKm&VwWvEnON!Ci0a?deUiFIDKfJh3;j z!UowAI5hcd?BG4h3d&DP7-;pT=u60TTI39WI(P6?MB6sE(N%IK4rl#cQ+~&8)_GwD z;(Io?cS;0NkXt?S=FpcP$27gJgX?;ETVzq?>mn+E45r+gQQeX)&maT0Z1Vun9i?^w z4*HFT@^X*+l}?+{mV>U2-Lq-@TI@Z=BK^$jgF{0znOtkJ z>Znwh^kK+xP1or@h`FQuIk68pRqm6Mxk4 zyzUxf9h30(>aC}aTq$&9QPsD6 zwzXb_=Xr>U@F^p=V};4nJ=lhfR!Y^1xw0kcz&nWK#6uth7r7aGGZyGs8(=zN$pD7VgA znCh_VKRpPhm)KI3>qPC?$py0wJ7-0{u8<^0Unjs-`$U-_(9Y(G8{-M$owE2!>pR#z zvu4M}v^n)yLZGVUD$Tu;O8`UUEEFk0-)Nfgg1)8NBUD6M=*s*N?K>*z z^9;5q2Q99NR1sgEz}VE3(KtM$mu;BC*ex8)X32mbk~Gi}zrfyXbZdt=u`k~N!b%_w zN|UJQ4Hjta)jF+t#y}s_Axw!ArTh?>s8qF&DMA#GSy52f?w`&WJB)8(P%GPh=d+HJ zvS6v!-ks#*N=Y_?r(F#7oz}p2BCoMyd&W2D0j!dh(GR&15su~`tf*E;?5pY~PrC9f zpn`jnPX|gao>S9Qt?~O3GA{D)G%I8r4MN6vcfwSOg1Z{a^p037(qtR3R1aJ4>t*@Q zT`hGBPEq^u{!cwC;ZBF3tS@2?jv3G|VGsj0+$WR%K$6%LhHJ06*S|^#gn05Lx?7ylXSm@28{vQ`qjUeT9_R%{i)_ zIGWhS_t@pljx&yYLzJw$rn-Yf;YhHr`0-oc*O_0xy3SZh zS*NSSZx)3IzwthuaSNa@X-b2-YPgL*&7;l# z!PsqFLZuIgA$SEb_=d14^`DVl5(^u5V;dIDt+eO-IARMW$;T;vXv&&S%-D_N*+x?> z$sSr27Wd&alE@GSKb^fEfu0(7e8M4BXr}ef>9-yBC6tHY={ToCPIMHh!=(nKuRX6%;*2?ga*J;0xYw(mj&DLNq46w< z9MuHR$$k!Glpcq^GHcDvkkwC*Qtp~eZ4}MMutgNX=U-`!Zq6)YZ#2_YnLEO4CQ%Hl zyy)xjB*o>y+2<-_(PEI2vu4GDtxdd}43_M2yP0XxWIi2Y92-i_oJct2-cS`jgPwt; z6oFi(=GlN|Fd2|`^F}Q;z-D*Kp1!98`8Gh$-FX_ZW2jztwR>3`}BR`$Gr zEV22TFhGB~XBjd4aPikg3j#d4x2&k&o3ZS@$LJM5F<<+9Vc7>Zgh zpPK6N$(pLMu&HMQ^g<+5zy?X9y{^C+*T(yoVcTOR8x99(KZ78XG&MJm%KMNVn2ULg zBzX;}kRzgTS0553Nd;KF3b(l1Vuwdxkm39$vloW}@^yHIZ0ICL-uW&)O5CH57ou~y z^C6Kh^NJZwi>jQlt>nBpkc+0y98UG@1YF+U231s1 zthhq^go}>gTSG5-KMWOL+wC%+92oa*r*wUVn_Jd)Sze#|!-&YS#dqpUhAA2>aZEYn zV}tr}X1W8W<5rk*DIpmi#2njL9#x~`9PO;n3bY~R`X@TSKU7DSpspmQ^81I-pqu(@_f8xKgdG(2wj%Gn_xB;+ zUMD$M1Z$1u`$zi2i5(?IU=V-ISYeBDApjJe%w;qlI`JM3)*$t!+5^(_i!P<#Y0*Bt zUVx8ZUYvi{w{Z}Wa7l1-OxYYxx`67FTpXR8;=Wz2SbALXG;JPl?|igERwQ-!*JZRa zF0pHpEUJICUX`Q5)twfd8~j8&kt^kN*Cno9mUedPdKt5wd%?-BD^n*mcR5q1Npjn{ zDW)yYoYt*8t@7DL=DO%AoH8ss;XKc9Ql#MM-Bk-q%bw0VrknMfSWodJ5xdc!frhBf}7-4p|&oV&6{n-$4pLj#qv&mP8aMNI5{eLdqDI4qn<5#4Tp za$Ls?OiUt-%P_gdrOP0Yd|K+{PX>(@D)xyU688^fC7#Fku3@oNukGeW1x+7Bw^|pr zPulNIc*`%$GYOtsMJM>Ws3KnFLtRS4;Rf;I9}bHqQ+tL|s-tEmT1Vw4RAp(PGUcNSJf(O?BVUy0;+sx4{f5oiOx@)Rx4!}C(`Ht?)NBG z(**7A%=m`Q`0)BbI2@HcUTn0Ryp;WoXC=qE>|?U&6)AtvGxNySwxzO-xw!dGnc&e4 zeSh~5N+bh$e5E*a?EK2BMR^{46sFZXG2Sba57-+R7=S0E*%I5(cS_t`Q%=|PQv_Uc ztfAZ+niIz>?4qt)`WM4;{#|uyeCZ$4{jXdn2$i(V?Q{pu|9U>h+>x6p?ZU#$ z=c7|r8NB2&yrivGz<1DWQvO4F7t;W7yGt zq&$DjOgFGA+y7#J0SG=9IZzBvhhzjt02@n*=$T)*JRb}i9`DX%n5Zjzp^zy4`6o*> z&C#p!koFep4ai~2_h5?+cB;jGg3qTfG6#pLQdO9oZLuWS#&F){9frU!C_nKaP9845BfoV zh>p7X{&91hzE|6i;_17NUz)T_@Q$On%ceI}Ax<&PA+G6^#7)L*f2Sw`{xYq50Yw@9 z##+}NwWgsuxerM*Kn|zQml?DxG*S9H72__@dOTL?h2QEAZwtuj<64{R7~Nm5GQ`H! z-4PQuG&Bv;)$|#%C07sAWhp+ z6Ip`{lMW~IFUC{$6C+u!o6mV?k?VmawBgLK}BU<)r#zo+bGc=c;$W zKK0Jmb;w6N?+h`v42jsHz=u|l8mOMnx_wQrc@2mI}~?F&O|n0#px05 zN2_kFb{gy|+QUuzX*Z@ic()4sa(H;qYC14K34h7%KY0-b)dITsF@TU&Ud}5(Ou5|i zACTTF!%veF&cD5Knun?KFW}#p3hl}v4r3BW{**cX)F0w5b{t(U7X|=W$;$_Km_&^_ z-XpVo(0T1T#5%8{Ybd&MJ*C~jH0mf>yjiqfmF#q}l30_PI~f-fu@-R#=Gb=J+mig9d{Dhxn!tJ)c2=d07Ogg88-0EzWz)ySOj3h^TaWZ+1W6qsY3Xw05ryIjz+Xv52E*!Rsg5$>dP& zDLcHy*MYP0I1%-xfyCZx6zW6cc{hNLDt=86)>Wa|1r9+KQHc5|phU4lBZj<8==+_P z3_mo;8d_Cr-mXc49jK+s=jP9VQc&7Zr2(U|3psj-1Jod`z1_OWc{7?4=X3VSa@i&C zcGt(xb;AJ)ZPob?LQenI58ilJ9CVql=A5$fFmwG{P$FTXrPg@vl5mgdc*FD%S(XeM z)Zqp8$6*-E1veJIo?gMjP#597?1I&xpgJ(>HV{h3JppnF@58Ij<^2+wG$7xn!<7&} zYNMth2rZiB^o7tNlMeVES{|E@aos|jte=#Z=Y6#3P}<8W*SO<2oSQQegGX0~A$4kI9SCsA_J-w!ITECFP&#-aTSjlnLIdR``NI^7Pxs?1;cH zm-$WNVFzEC@#5`2c_4XTB*AidG5FbPJw&@EIq~;r4%rFr8Vec&W#11PRz{jqCoIK| z>vj*E>{VjDt^&0pBs;ID4;J6--vs*}kaMsij~x!a@xp-&Wf0*0 zG-ov5R~&*5hlAtOMx9fI{{)IB z`2hcLmS@_N)EaVX#7o8ZrwT~{bU%)IYjAIWJSWteF0S5E(;Z|%YOP|^2gXWu*>)o*fGiPGim=lQJl@jwlYK{T4XNh1hi@r z_rzgH9XmW}E|JC+QRSV2F-|Phjn!KCY<`S(S1k=8BDz3_T0RPynbu}}Di~T9TXLPQ ztm-l#77y54+7?UL3&MWIZIn-PnZ`-ar%Zu^T%RVjLzBHcQ?^%0s<5MgKB0@)4yaM5 zkxthR`6{#kV{hhIlbXol_U>7=A@;l~W;0W#ECtaVN5li^Sxq6{Q`URjTAbVRln=a) zG(B;>Pg5y|P6VRA;HUcT6#Ng8UTwmXOW~uMha%zro!i%poK(rB%((6tQP@p*K28EA76^0 zXC0Mp6XG>0BwiI0pc3s17rjll3SC_%>nD9t&x)P8)t^udmya|HHam4I9u45*us=zF z%3(cN#o}Q4g0%C5iAE!s*f=A|f_B>MoEW$f%M@ADrna&;Zz!TY-`3PTTw1c#4;e4D z7Y(V)TnygytCY~@cq<-T=?V>n3Ggwk-=q0dH6AM}C^ll}Q#ODJtz;vGYsn{-R7Xva zEv_0Z$Sn95ogRNFkE3$jgC^3l3%{;dQkL|YA1RH^WO#;7G%#J|uHLhWvEF`2zOT>o zEG%G;cc5tv3ZpTPyorBKx7h728O#j_xW)DVWlKzczWt>4f7VV0*|XI7+i2q_@lx&&il^L7g-PjdL$n5Ke$D*;U| z8{eK+rk+VP9<)$8pvVaJLubBD7>sn&B2`=kw$3&E2hT0`t+*;=%Y|ktciAI>E35D5 z2%a4p^kuMd9-jU_+vg3lGOtHdRH@bkR4(Hs0)EfRA6IjZg}4I++3~)DT^e!946y;7 zjncmcCRuC@jDMADTuK7LEXXJ3W5t_`t`LSvAisS_m;(O_n!uLbttnX z8J5|LViPpfgKKr_^}E}jhxJKPsP|O>c+a1p?DE|4wI0u$FayKG9pc2xDq@jrBySKL z45YcVQIYyx@<$ntT{Tjz$bA~cBX*IJKj__O-0QXqRxNOx-fL77G?uG=b5m(Tg1gfw zhlb(hAINXDdL-53P(i%x;`@wtc-GT`swul+4{2WCbN6RF>s`-tC&p87A698irkP92 zXH8L*h_9lgU}UndYf1trxpZ$Oo=20i47=-^UOLfa3xt(GT2cy8j=51{U~g}19;SeB ze(;rYuzUm_H#GhoB+)~A8_-0{Zp$)ob5wJ1Qi-2I)(C(JG?QX)AUIiK6%Kwckty_2 zbpv6y&$oe3^(hPhBABFDpP3gMIhL5>8ZxunRgmNe;BA3C`p|+oWQ!%V6o8#bt?zAw zY z7IhXVcn_vWbzOs#euE(!a@?bI25HOPaRc9fvmQGNX)92#hLyH~rSg0({nPX>X3f=4 z%)=8^V$AKE$L25V-gvN&{&4{8FKo6#aSv9SYJwdy%v-oI0kLceQi5!()p$97zKa=* zBzY+BJe$)N27QZw8?0`$9;4Q5^1OVHOE0jvL5B$$)lRD^l0LxJ;fU@iJ< zfiYI>|=yI*?|r|C}|u|1T0C93el>^SEcuURKxFVZnjkSIyQN z1MnWvXHWMQgO7AiMaf0x&S^g)*b*jPJn#EKWc3EUIfwNafaFRdQNSn0l|mWV*gJm_ z4%gjUc{#sS()9%Z1;^Zxj6ewQg4WN2sX$Wf2$}H-^E<7tza~71o*%|4_bVl_$SeA& zhyFP^RCRwKk!)P!kQEhmwG+qB{X&f9Ye#VA{9CC4ln$fONgC}(_B!t_7uRd}KRqtq zr0w^dFet2Q(O6Y(pj*+e(LtQPfoVZ}^I_)8i)AoHT@+7G57hF}WI1@g0+4F44*v6# zn)so%>4pw{j?qC> zpAc$M&LNU>|9(g^ixPQSnV}wYyhnsI}L+QGzlN%-H4}Sv_H|mcHuN^T$pBjUA zjDS{xu{=VHt+v#C)XAoiaaB9?!u#`8;U5j2>C)S^!%(6nSd4q9-A&DZQZlwMNilJY z90zY)`1WhKs3MqgB9lW>iqzTReife~FTXjnsfQJ}Da>@<-nODQTDm8h?O@{EJxxEz z8*bnsZh1@3`(b-R#mqLPPo6ZXh^D?l(yRr^+AD2cvDzkKa! z&+n5rlM;PHx}!y^`cT;!^hn~X_Cxgvn-Xl*W!}rbcF;mM^l~0*pSdw>n=T#G8acSz zns*17qk*KM<56-E26-;& zx^2d39^M?BKGO6X3Xw)==nhQXC@-~Ud@6?DJd_r6m`~GayEFqli^tB@n>au7yANGL zCWa=!(1MbF$AU7EajF_J!jG~E*$3f8vHO6r$)INi#pmav_L^K>rKo}-h8zVA74W_S zwVwBrCwF*yirEgHt-_f4eOg0Yo;@uopGdq1Y^9>^q(De3Z<>Si(K8=~)9`OOJJ|D~ zo}`>0E=;B-e2nW(On$z$+3{&s-iwOZeX#~Q!YA6MqgPT7n#fe9U(;Bnq|AbXpGPe7 zwCB(?3IFw{zP9Y2)?U>R<_UyOQ3k~<=2S(EyYUGJKjg7T;^gvW&BOLUd_#vCj@^D4 zNUh8}rDL`Ey=Tso=gxhlr@pIFQRNFW>j#j77XAL;@5ljTd;eP}X9DADcxo9uwU`kd zCzE8U4o}cvj?l?HG4+uzB~`^-Cye(l55~rnS~M-`@u~T|J>}CpjOpnAI&A|j=p#V- zrGlDSKASz(;`tLtScN^t&Lp`OKf|4#+*Z#>#J#PB1SW6x$Ol_^FR8WXhG2&B)&za; zqjQ5KV7_ckha5@j5gg?v^YM0fP5;1I<01Dkg^KN2sRL~OWrn>bphvt}E>=tPYszs8 z;E_SF5&~gh<#j6F48Z&++_siT-ljCQhW(+eFn9w+R8BzQXQQ&g#FVV>0#)-5fzM4a z+EToa+A_98dFT{S2A8UP>N+mbR!(9%yM)B$`cRr%c3a++~Y>4}eu8ZI^Z_eqWpQ0minoAS!0-b>u9_Gic*= zKZ>=?d9rZz5XK=<7&E`MW{uYjQIQ6o7ca-J9sO;5b|bEGh9y}U{@9h34R|S{0B;(KJz|7N`VfQzV!*@ zys*!5eU*|b!ddU%TC3l_dgv|FxAFwbAS&ij=AC$A$y>95%M#MxY%TM!B!lq2Mp1%E z==<>NDb#>VP~8rwH=GPv_#KaWj>u%NzN>Q9FVpH4((!plBeO@z$?dizw=n%yTNKn+ z%jW@_>60Tb#qeN-A;<$slVsSE3rhHodqO4xj1-kE!I*aVq>U)IRY?*qG&VE7nw`ab z?)oBFfqlKLL(4{V{gu9XizEPSOPO?D%|;Ya$O3;#$u8bt9n4K&gAY$4pcrV(cLoVDb%Z6*WMRWQ8PE*OW6c0p}~2* z4EA_w&#d$EhkR_HFy~m#&g#kH*n(4bbby2k`!OhY|HBm(tMphI0xA9j(saB!4GTvL^=6jYii3~%?|!|1G1 zNCz>i7pe|8p;~HHM3Hx*&wewbNCSNLsm1fmip(75h}+cUI`5;kctm=R;f;ris|Es| z+ZI{MpOq7}W&PX2dL9*oxoK$`*Z1$-A6vSZf#x)>>?y>Zk6ifIIa+O4ht@?@pH6=P zER*#f>?p`G?^AKy<>8CpxDb65z;tzi=n zb_)rVt9!0hHHXpOA{=-u0+69;%u=3qpOU1cmoUs#9M+OD6*>SnoAGH+m+I2dinW$rgi4X^v3K~cjW(#6E3qO8YroPe5_p7ZTq;xxPdg%@wr4*9?{ z8BzTCTkB1Tz?eFKz%5_8;Wa{cI=0T}J z?(44?Dx-+$$d2-Mofl$`GD-A9*1f4jKaZ1Pl7^-ufAat>H}Z?0Bn>6#uM;dXg3NFT zhaLr9%fQs-qoXE{bLfcyvAa3hr&>^-O%u8}=6w2FG#zfC6&ZXfR-{7TU}HPX@bf67 zjV}(}^aP2(W>GgBPHgIhz*(ajn?o0eRU3{nFaAPHXRjRB1+aJz>1)kdOy}B+xBEgd zAIn!diexEqRKt^EBh3cu%S)l1AeI`F8I`ah?E5QM?*I7ne*tE0$+}PW6to?yo^3+! z4s(RNnr0n_1VEeTJsrS=z}$`?E(SSYvq|XQr|t7~+G3%kSvh<@NkiLj{E?GOxW6?s zL1!W_)P^HYUzVlxxFI=vVpfk0>l-nBq|12{b|QO2FNJX$jB2i8P%x3r_G}o(4lf1s zAhpa>F>Y4FaxE<_&EjzAqY$6o-8*qNpLzvXw?vu}+p=f&k;666*x&g{+X2Wi8N&Yh zdRA}j${x(u*7{6WDl}%cM3!$>kA8<|^$lxaFtKABy6>OfZp|gK-lXl_zCSi~=BcyR zv*0iD+93$Ayd4L>XL;&((uX?)e>6H=IQI)T`KBl*?m$QMinrOqDU+LAwwMs;IU>o< z43ADy2~qw8E{I(D#H^X#c95@-YCiFEwCkms+e^!o6a-9inFUcz{A$XvYQc&`5VyDe z=Vm0g&U(FwEmXF9WbVjH?v;U9`VHS#^qYNB}YPOr9__ ztV-?zI`GK#vlh$NaKsvqIkQ*HkIc;oQZkha%bH!PPMe+$i`(wuiCap@mi=Z3f4?mF zGi9d|vTw0ryzN{NV!JhzR`@w?pi{*-sc*%mEB(A_Nw=zyQAwd0xss(wwYnXnDAIA^ zmm#7Tsga_Kxsp?Uc2bc<=+0L{DUEFq&70`cek1D_A^@+8J}rvO^IE*tcQmJXN$drq(=v1_Y6+;*LPgGuVm>P7;Ooj?wPtnUJx zUgolZltmC20yg03I_;T*$|O;dGIFECKD>c}fv{a4l9N5mIC;M91^s*))WOYmnB^yq zVEQI-Dn8`EV^+B#TTRd?c|F*{A?K{92!r4nr@$#k@?e5px+9o=MqB~2>5aM+T`Dd- z%DS_ma{;+oZtJ;wIhPhi^vxZignnO~&;eiwq?W*3u%hPH)fY;vW5wJ$D{^mdRP-^ZHcT5v?qQzE8aaZ`{wOfuM(Ie_x+gl(B*jBi$U`8!@PMp ztOcThJ(&wZH+d|`c7oY#yEpZ3grlQ2Z{hnyLmJr!KrI#Xq!}6|&!3vvc880tpS!VC zLU}ZvyMvF(Y7(5G$<~M)93(t?y;T$&E8r@|X-qcs`O0{H5f2I;2iFBUDuVsCArIF-|u< z?(iw)w%|IHa!Z5LKY;nhhK8ozOj8ql#~hYVP+yAEY@`MFwg|x^61XE*W~Z9H)5=7x zA4n2l;d||M6m)2jf`v0)srRs|?Wyz8Nz+R*&0yp` zvqgm*o2Oo`;&eI0g0WK<`76H!cZN1a<81mu$=ah-08l^{ev;bPqqs#?18_T@wH|IG z-in++;FotA9cKwgYAFLXn=VmjRtwo?Z!YG*5}?GOi_0T8Y_NVHL(;9x1RG>@)NR+W z2XYzyeN>hly16^98{=w$Sll`jIW>t}k3`~>3e2$dBp;-kn7%6P?F4R7;gC4KGs|Io zfnwrv@21NRny6ON3`Lxt>LcY3f6V$ClaS;4%$_CIT&TyC*KmT0vbe2cB!-_}z z;!M45@BW;Kdtr9JH5bUVz*gA3G`olZ$`wbS8&-EfO+NXmmoUapFX=9m_vpxGQKzS+ z+Z@SGv1A8i##kd8Y*N8`3rPXJlYkV<6I+Ou_!8taQO(X=A5qmcs8$bwq!-Z=e96Gl zv;CpqOru&K8Jb4LjJDtN7k`eK6G4IgBZAp8d_~n!x2$J<5-+HUA23lHVW~b|4S+k9 zDKB67{f);iTL(FGys^X+PNh$`5z0LScy(m8hRZi7Ay&9IHrYs5o`sOFq~BRbNZZ&C zq;+j~5KF4kgak07!CqFpBIA0WK9S}JyJKm6*T2``hKSsRtf=w@ZJ^P^vM0D37H`NTgMoZc{8#@Qiv9z%3A3Uc&HpF%X{xXAuSrO!U# zp=ab179N{(X`1W%wq8Lx(|b|aw2(2hHIJUF=CjMuTiN5yoM|w*?*@Km`z)$4*2i;n z>S#5;1~<@vH_zyF0|_ctvnUHCk)rN_MU%=?qMfsce+~84uVffs}c-I69Ci~mPCR~pn*mW9)kWADnSv={;uCm_*HuX1(fL6)loBGrxFrw{^UrI&BJ`F6Va;xPo@B2sia2 z8P{hJDUX_V;1a_qtzG;uVG+l|)N9|jr4RM8Oeex;3#gy_#A!XDkPzI69Ef@|9yrID z$LE;Ohp5>`Uh|?v9hz+_H4@1pXe$EnGl>CEC6qHI zc^vaOl7EzZvtx==0`qr=ny`_guSV(yPlMt9h0V~CQnfl|Lx%#6n_T4~max#U`fDq< zLK}%Js93HsoYnA)wDog(L(y24yd}hLmehgmFr&FEuHnl4Y`cDQubh*G;ml*?Jlyr6 z25-%kRq7RDPrDM+7ll{-t=Rz+WcUx&^Sd0?#}+H(!*QZ zl=LyK{Ll5st7bc}>MP!fwq7|cq9eU$<#-utz3OdU0-weY{*~;&vV`kFiZ6ETh114Z zT;`_=hXJgwnzBa*ZhVhS6t=i!h>^aXm%$2^16y`btph2RnqXFv7rUjy%Tzmb+25cs zKJb19FwfRIpw^n6PrS22b}nt3^X!fZhg+#Od3QckN3T+VJ10{^2UrJ&XOs1v+c2gT z2iYxYEZtr##U{V@XeTQjPt+|p^p%MkYA^D_m$%#@>1E8=F7gtgimXNvdtrc~cbWC( zaJ`R}4^V}{)xllfn=6}kn(mBH%Ti`QWzP6q+IOA_`u_` zpw&%Hk3`A~^}G#M1TwAJjNW1Hbe~MQfHv{%%yKBF4mmbU_P`YuytU6!a3+2Dja4_J3OGVv{RWbH^>1L7muVX zk47IuLIcP%DZ2sk@xDq3fgQOF+WL0FZ=hTd^BaGDg*?hT03D0W@HHnJq9tPoi$5}J zEhRxoB`AUCm?@wi62fnV7*g`YrKe~7#2Jw4$i?0V@d=DP(!ww9>nd^438b+|xiwB1 zgf+nx#^;Viw@ciuFknmV#3%koCxcPew>D35ihDGXh1F)NlWtf+45q31qh(32%>-di z(p@{x(5ln2xH-|vAEsGR%|9=V?CH)>k_Wim6g;VsygKY%yp}|&6)u{sOxM`Il$@|2 zo?~elSS}o<144E|?tr+Bcf<>QC`=sA&I(;{AB=pR7v}FBu>h26mYCBxDkeY>D7_WM z>m#wx`5hw2KvX`Mu|6T@nfzMHR1fYylhk*_#^-pfXJ~5wmk%m{5{fJ?6#~buHUs7b zmA-b%#IbFCXZ7&`Js zT{P^c&yR1|&$(mR{ML*_y#V7S9+EnKzgq_$WYK}sRjI&IlSIT(cJVVc&JGwed=jUQF}3l)eC* z28UU}hT9 zcQ6zncnBki_Ihh}PF#eYLC0m`0`l0zVhzXS(W=G-AgW9mm?1!GE}F2_KikGo;&;Rb zNg&6e?}}QBq>>>E1(atDfcUVs0X^?lKO=DshqeX04}2E$^hPrMCm`b{ZD(Dr>Q~z3 z4~WCg7Z}u$BF4t|biYS!1Hf|zQ<@sS3NH7Q!U!|YdfCFJDrst!GN-6^Pqs-r>wv3g zYyCb2iORU8yQbRbiOsEi-Q;!h=&jff$L{R{9XERHSWoaUcz;&oyqx#G17O_p6m~|3 z@aSjL$Wt)-wMGGoPKT>bZa%>SHN@GdGP8k?%-}J5JlUT_5a-hA-)L6~zN*SwcZLPQuD zoDbN*%C?s+%od8thoo@zm*m%xI~}W^FY295-4l)3Amu!!k2sP{Dv${fDg@Rlem5(p zFEuz}T2Jc5zCrvQ&}I3syEX;y8{yh5#;|x0pKfVH zntb}Ukipft!H{UE + +"$EMCC" ../sandboxed-system-primitives/src/*.c \ + -DWASMTIME_SSP_WASI_API \ + -DWASMTIME_SSP_STATIC_CURFDS \ + -I../sandboxed-system-primitives/include \ + -Iclang \ + --shell-file shell.html \ + polyfill.c \ + -s WARN_ON_UNDEFINED_SYMBOLS=0 \ + -s EXPORTED_FUNCTIONS="['_main', '_handleFiles', '___wasi_args_get', '___wasi_args_sizes_get', '___wasi_clock_res_get', '___wasi_clock_time_get', '___wasi_environ_get', '___wasi_environ_sizes_get', '___wasi_fd_prestat_get', '___wasi_fd_prestat_dir_name', '___wasi_fd_close', '___wasi_fd_datasync', '___wasi_fd_pread', '___wasi_fd_pwrite', '___wasi_fd_read', '___wasi_fd_renumber', '___wasi_fd_seek', '___wasi_fd_tell', '___wasi_fd_fdstat_get', '___wasi_fd_fdstat_set_flags', '___wasi_fd_fdstat_set_rights', '___wasi_fd_sync', '___wasi_fd_write', '___wasi_fd_advise', '___wasi_fd_allocate', '___wasi_path_create_directory', '___wasi_path_link', '___wasi_path_open', '___wasi_fd_readdir', '___wasi_path_readlink', '___wasi_path_rename', '___wasi_fd_filestat_get', '___wasi_fd_filestat_set_times', '___wasi_fd_filestat_set_size', '___wasi_path_filestat_get', '___wasi_path_filestat_set_times', '___wasi_path_symlink', '___wasi_path_unlink_file', '___wasi_path_remove_directory', '___wasi_poll_oneoff', '___wasi_proc_exit', '___wasi_proc_raise', '___wasi_random_get', '___wasi_sched_yield', '___wasi_sock_recv', '___wasi_sock_send', '___wasi_sock_shutdown']" \ + --pre-js wasi.js \ + -o polyfill.html diff --git a/wasmtime-wasi-c/js-polyfill/clang/stdatomic.h b/wasmtime-wasi-c/js-polyfill/clang/stdatomic.h new file mode 100644 index 000000000000..b4845a74e494 --- /dev/null +++ b/wasmtime-wasi-c/js-polyfill/clang/stdatomic.h @@ -0,0 +1,190 @@ +/*===---- stdatomic.h - Standard header for atomic types and operations -----=== + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + *===-----------------------------------------------------------------------=== + */ + +#ifndef __CLANG_STDATOMIC_H +#define __CLANG_STDATOMIC_H + +/* If we're hosted, fall back to the system's stdatomic.h. FreeBSD, for + * example, already has a Clang-compatible stdatomic.h header. + */ +#if __STDC_HOSTED__ && __has_include_next() +# include_next +#else + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* 7.17.1 Introduction */ + +#define ATOMIC_BOOL_LOCK_FREE __CLANG_ATOMIC_BOOL_LOCK_FREE +#define ATOMIC_CHAR_LOCK_FREE __CLANG_ATOMIC_CHAR_LOCK_FREE +#define ATOMIC_CHAR16_T_LOCK_FREE __CLANG_ATOMIC_CHAR16_T_LOCK_FREE +#define ATOMIC_CHAR32_T_LOCK_FREE __CLANG_ATOMIC_CHAR32_T_LOCK_FREE +#define ATOMIC_WCHAR_T_LOCK_FREE __CLANG_ATOMIC_WCHAR_T_LOCK_FREE +#define ATOMIC_SHORT_LOCK_FREE __CLANG_ATOMIC_SHORT_LOCK_FREE +#define ATOMIC_INT_LOCK_FREE __CLANG_ATOMIC_INT_LOCK_FREE +#define ATOMIC_LONG_LOCK_FREE __CLANG_ATOMIC_LONG_LOCK_FREE +#define ATOMIC_LLONG_LOCK_FREE __CLANG_ATOMIC_LLONG_LOCK_FREE +#define ATOMIC_POINTER_LOCK_FREE __CLANG_ATOMIC_POINTER_LOCK_FREE + +/* 7.17.2 Initialization */ + +#define ATOMIC_VAR_INIT(value) (value) +#define atomic_init __c11_atomic_init + +/* 7.17.3 Order and consistency */ + +typedef enum memory_order { + memory_order_relaxed = __ATOMIC_RELAXED, + memory_order_consume = __ATOMIC_CONSUME, + memory_order_acquire = __ATOMIC_ACQUIRE, + memory_order_release = __ATOMIC_RELEASE, + memory_order_acq_rel = __ATOMIC_ACQ_REL, + memory_order_seq_cst = __ATOMIC_SEQ_CST +} memory_order; + +#define kill_dependency(y) (y) + +/* 7.17.4 Fences */ + +/* These should be provided by the libc implementation. */ +void atomic_thread_fence(memory_order); +void atomic_signal_fence(memory_order); + +#define atomic_thread_fence(order) __c11_atomic_thread_fence(order) +#define atomic_signal_fence(order) __c11_atomic_signal_fence(order) + +/* 7.17.5 Lock-free property */ + +#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj))) + +/* 7.17.6 Atomic integer types */ + +#ifdef __cplusplus +typedef _Atomic(bool) atomic_bool; +#else +typedef _Atomic(_Bool) atomic_bool; +#endif +typedef _Atomic(char) atomic_char; +typedef _Atomic(signed char) atomic_schar; +typedef _Atomic(unsigned char) atomic_uchar; +typedef _Atomic(short) atomic_short; +typedef _Atomic(unsigned short) atomic_ushort; +typedef _Atomic(int) atomic_int; +typedef _Atomic(unsigned int) atomic_uint; +typedef _Atomic(long) atomic_long; +typedef _Atomic(unsigned long) atomic_ulong; +typedef _Atomic(long long) atomic_llong; +typedef _Atomic(unsigned long long) atomic_ullong; +typedef _Atomic(uint_least16_t) atomic_char16_t; +typedef _Atomic(uint_least32_t) atomic_char32_t; +typedef _Atomic(wchar_t) atomic_wchar_t; +typedef _Atomic(int_least8_t) atomic_int_least8_t; +typedef _Atomic(uint_least8_t) atomic_uint_least8_t; +typedef _Atomic(int_least16_t) atomic_int_least16_t; +typedef _Atomic(uint_least16_t) atomic_uint_least16_t; +typedef _Atomic(int_least32_t) atomic_int_least32_t; +typedef _Atomic(uint_least32_t) atomic_uint_least32_t; +typedef _Atomic(int_least64_t) atomic_int_least64_t; +typedef _Atomic(uint_least64_t) atomic_uint_least64_t; +typedef _Atomic(int_fast8_t) atomic_int_fast8_t; +typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t; +typedef _Atomic(int_fast16_t) atomic_int_fast16_t; +typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t; +typedef _Atomic(int_fast32_t) atomic_int_fast32_t; +typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t; +typedef _Atomic(int_fast64_t) atomic_int_fast64_t; +typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t; +typedef _Atomic(intptr_t) atomic_intptr_t; +typedef _Atomic(uintptr_t) atomic_uintptr_t; +typedef _Atomic(size_t) atomic_size_t; +typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t; +typedef _Atomic(intmax_t) atomic_intmax_t; +typedef _Atomic(uintmax_t) atomic_uintmax_t; + +/* 7.17.7 Operations on atomic types */ + +#define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST) +#define atomic_store_explicit __c11_atomic_store + +#define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST) +#define atomic_load_explicit __c11_atomic_load + +#define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST) +#define atomic_exchange_explicit __c11_atomic_exchange + +#define atomic_compare_exchange_strong(object, expected, desired) __c11_atomic_compare_exchange_strong(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) +#define atomic_compare_exchange_strong_explicit __c11_atomic_compare_exchange_strong + +#define atomic_compare_exchange_weak(object, expected, desired) __c11_atomic_compare_exchange_weak(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) +#define atomic_compare_exchange_weak_explicit __c11_atomic_compare_exchange_weak + +#define atomic_fetch_add(object, operand) __c11_atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_add_explicit __c11_atomic_fetch_add + +#define atomic_fetch_sub(object, operand) __c11_atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_explicit __c11_atomic_fetch_sub + +#define atomic_fetch_or(object, operand) __c11_atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_or_explicit __c11_atomic_fetch_or + +#define atomic_fetch_xor(object, operand) __c11_atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_explicit __c11_atomic_fetch_xor + +#define atomic_fetch_and(object, operand) __c11_atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_and_explicit __c11_atomic_fetch_and + +/* 7.17.8 Atomic flag type and operations */ + +typedef struct atomic_flag { atomic_bool _Value; } atomic_flag; + +#define ATOMIC_FLAG_INIT { 0 } + +/* These should be provided by the libc implementation. */ +#ifdef __cplusplus +bool atomic_flag_test_and_set(volatile atomic_flag *); +bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order); +#else +_Bool atomic_flag_test_and_set(volatile atomic_flag *); +_Bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order); +#endif +void atomic_flag_clear(volatile atomic_flag *); +void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order); + +#define atomic_flag_test_and_set(object) __c11_atomic_exchange(&(object)->_Value, 1, __ATOMIC_SEQ_CST) +#define atomic_flag_test_and_set_explicit(object, order) __c11_atomic_exchange(&(object)->_Value, 1, order) + +#define atomic_flag_clear(object) __c11_atomic_store(&(object)->_Value, 0, __ATOMIC_SEQ_CST) +#define atomic_flag_clear_explicit(object, order) __c11_atomic_store(&(object)->_Value, 0, order) + +#ifdef __cplusplus +} +#endif + +#endif /* __STDC_HOSTED__ */ +#endif /* __CLANG_STDATOMIC_H */ + diff --git a/wasmtime-wasi-c/js-polyfill/polyfill.c b/wasmtime-wasi-c/js-polyfill/polyfill.c new file mode 100644 index 000000000000..d4c63cebf27c --- /dev/null +++ b/wasmtime-wasi-c/js-polyfill/polyfill.c @@ -0,0 +1,45 @@ +#include +#include "wasmtime_ssp.h" +#include "../src/posix.h" + +static __thread struct fd_table curfds_pointee; + +int main(int argc, char *argv[]) { + return 0; +} + +void handleFiles(void) { + struct fd_table *curfds = &curfds_pointee; + + fd_table_init(curfds); + + // Prepopulate curfds with stdin, stdout, and stderr file descriptors. + if (!fd_table_insert_existing(curfds, 0, 0)) + __builtin_trap(); + if (!fd_table_insert_existing(curfds, 1, 1)) + __builtin_trap(); + if (!fd_table_insert_existing(curfds, 2, 2)) + __builtin_trap(); + + EM_ASM(" \ + const imports = { wasi_unstable: WASIPolyfill }; \ + let file = document.getElementById('input').files[0]; \ + let file_with_mime_type = file.slice(0, file.size, 'application/wasm'); \ + let response = new Response(file_with_mime_type); \ + wasi_instantiateStreaming(response, imports) \ + .then(obj => { \ + setInstance(obj.instance); \ + try { \ + obj.instance.exports._start(); \ + } catch (e) { \ + if (e instanceof WASIExit) { \ + handleWASIExit(e); \ + } else { \ + } \ + } \ + }) \ + .catch(error => { \ + console.log('error! ' + error); \ + }); \ + "); +} diff --git a/wasmtime-wasi-c/js-polyfill/shell.html b/wasmtime-wasi-c/js-polyfill/shell.html new file mode 100644 index 000000000000..f44eda52ca03 --- /dev/null +++ b/wasmtime-wasi-c/js-polyfill/shell.html @@ -0,0 +1,88 @@ + + + + + + + WASI Web Polyfill + + + +
WASI
+
Downloading...
+
+ +
+ WASI logo + +
+ + + {{{ SCRIPT }}} + + diff --git a/wasmtime-wasi-c/js-polyfill/wasi.js b/wasmtime-wasi-c/js-polyfill/wasi.js new file mode 100644 index 000000000000..a30412f88675 --- /dev/null +++ b/wasmtime-wasi-c/js-polyfill/wasi.js @@ -0,0 +1,497 @@ +// To implement `proc_exit`, we define a custom exception object +// that we can throw to unwind the stack and carry the exit value. +function WASIExit(return_value, message, fileName, lineNumber) { + let instance = new Error(message, fileName, lineNumber); + instance.return_value = return_value; + Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); + if (Error.captureStackTrace) { + Error.captureStackTrace(instance, WASIExit); + } + return instance; +} + +WASIExit.prototype = Object.create(Error.prototype, { + constructor: { + value: Error, + enumerable: false, + writable: true, + configurable: true + } +}); + +if (Object.setPrototypeOf) { + Object.setPrototypeOf(WASIExit, Error); +} else { + WASIExit.__proto__ = Error; +} + +function handleWASIExit(e) { + if (e.return_value != 0) { + console.log('program exited with non-zero exit status ' + e.return_value); + } +} + +// Safari doesn't have instantiateStreaming +function wasi_instantiateStreaming(response, imports) { + if (WebAssembly && WebAssembly.instantiateStreaming) { + return WebAssembly.instantiateStreaming(response, imports); + } + return response.arrayBuffer() + .then(function(buffer) { + return WebAssembly.instantiate(buffer, imports); + }); +} + +// The current guest wasm instance. +var currentInstance; + +// There are two heaps in play, the guest heap, which belongs to the WASI-using +// program, and the host heap, which belongs to the Emscripten-compiled polyfill +// library. The following declare support for the guest heap in a similar manner +// to Emscripten's heap. + +var GUEST_HEAP, +/** @type {ArrayBuffer} */ + GUEST_buffer, +/** @type {Int8Array} */ + GUEST_HEAP8, +/** @type {Uint8Array} */ + GUEST_HEAPU8, +/** @type {Int16Array} */ + GUEST_HEAP16, +/** @type {Uint16Array} */ + GUEST_HEAPU16, +/** @type {Int32Array} */ + GUEST_HEAP32, +/** @type {Uint32Array} */ + GUEST_HEAPU32, +/** @type {Float32Array} */ + GUEST_HEAPF32, +/** @type {Float64Array} */ + GUEST_HEAPF64; + +function setInstance(instance) { + currentInstance = instance; + updateGuestBuffer(); +} + +/// We call updateGuestBuffer any time the guest's memory may have changed, +/// such as when creating a new instance, or after calling _malloc. +function updateGuestBuffer() { + var buf = currentInstance.exports.memory.buffer; + Module['GUEST_buffer'] = GUEST_buffer = buf; + Module['GUEST_HEAP8'] = GUEST_HEAP8 = new Int8Array(GUEST_buffer); + Module['GUEST_HEAP16'] = GUEST_HEAP16 = new Int16Array(GUEST_buffer); + Module['GUEST_HEAP32'] = GUEST_HEAP32 = new Int32Array(GUEST_buffer); + Module['GUEST_HEAPU8'] = GUEST_HEAPU8 = new Uint8Array(GUEST_buffer); + Module['GUEST_HEAPU16'] = GUEST_HEAPU16 = new Uint16Array(GUEST_buffer); + Module['GUEST_HEAPU32'] = GUEST_HEAPU32 = new Uint32Array(GUEST_buffer); + Module['GUEST_HEAPF32'] = GUEST_HEAPF32 = new Float32Array(GUEST_buffer); + Module['GUEST_HEAPF64'] = GUEST_HEAPF64 = new Float64Array(GUEST_buffer); +} + +function copyin_bytes(src, len) { + let dst = _malloc(len); + updateGuestBuffer(); + + for (let i = 0; i < len; ++i) { + HEAP8[dst + i] = GUEST_HEAP8[src + i]; + } + return dst; +} + +function copyout_bytes(dst, src, len) { + updateGuestBuffer(); + + for (let i = 0; i < len; ++i) { + GUEST_HEAP8[dst + i] = HEAP8[src + i]; + } + _free(src); +} + +function copyout_i32(dst, src) { + updateGuestBuffer(); + + GUEST_HEAP32[dst>>2] = HEAP32[src>>2]; + _free(src); +} + +function copyout_i64(dst, src) { + updateGuestBuffer(); + + GUEST_HEAP32[dst>>2] = HEAP32[src>>2]; + GUEST_HEAP32[(dst + 4)>>2] = HEAP32[(src + 4)>>2]; + _free(src); +} + +function translate_ciovs(iovs, iovs_len) { + host_iovs = _malloc(8 * iovs_len); + updateGuestBuffer(); + + for (let i = 0; i < iovs_len; ++i) { + let ptr = GUEST_HEAP32[(iovs + i * 8 + 0) >> 2]; + let len = GUEST_HEAP32[(iovs + i * 8 + 4) >> 2]; + let buf = copyin_bytes(ptr, len); + HEAP32[(host_iovs + i * 8 + 0)>>2] = buf; + HEAP32[(host_iovs + i * 8 + 4)>>2] = len; + } + return host_iovs; +} + +function free_ciovs(host_iovs, iovs_len) { + for (let i = 0; i < iovs_len; ++i) { + let buf = HEAP32[(host_iovs + i * 8 + 0) >> 2]; + _free(buf); + } + _free(host_iovs); +} + +function translate_iovs(iovs, iovs_len) { + host_iovs = _malloc(8 * iovs_len); + updateGuestBuffer(); + + for (let i = 0; i < iovs_len; ++i) { + let len = GUEST_HEAP32[(iovs + i * 8 + 4) >> 2]; + let buf = _malloc(len); + updateGuestBuffer(); + HEAP32[(host_iovs + i * 8 + 0)>>2] = buf; + HEAP32[(host_iovs + i * 8 + 4)>>2] = len; + } + return host_iovs; +} + +function free_iovs(host_iovs, iovs_len, iovs) { + updateGuestBuffer(); + for (let i = 0; i < iovs_len; ++i) { + let buf = HEAP32[(host_iovs + i * 8 + 0) >> 2]; + let len = HEAP32[(host_iovs + i * 8 + 4) >> 2]; + let ptr = GUEST_HEAP32[(iovs + i * 8 + 0) >> 2]; + copyout_bytes(ptr, buf, len); + } + _free(host_iovs); +} + +var WASIPolyfill = { + +args_get: function(argv, argv_buf) { + return 0; +}, + +args_sizes_get: function(argc, argv_buf_size) { + updateGuestBuffer(); + + // TODO: Implement command-line arguments. + GUEST_HEAP32[(argc) >> 2] = 0; + GUEST_HEAP32[(argv_buf_size) >> 2] = 0; + return 0; +}, + +clock_res_get: function(clock_id, resolution) { + let host_resolution = _malloc(8); + let ret = ___wasi_clock_res_get(clock_id, host_resolution); + copyout_i64(resolution, host_resolution); + return ret; +}, + +clock_time_get: function(clock_id, precision, time) { + let host_time = _malloc(8); + let ret = ___wasi_clock_time_get(clock_id, precision, host_time); + copyout_i64(time, host_time); + return ret; +}, + +environ_get: function(environ, environ_buf) { + return 0; +}, + +environ_sizes_get: function(environ_size, environ_buf_size) { + updateGuestBuffer(); + + // TODO: Implement environment variables. + GUEST_HEAP32[(environ_size) >> 2] = 0; + GUEST_HEAP32[(environ_buf_size) >> 2] = 0; + return 0; +}, + +fd_prestat_get: function(fd, buf) { + let host_buf = _malloc(8); // sizeof __wasi_prestat_t + let ret = ___wasi_fd_prestat_get(fd, host_buf); + copyout_bytes(buf, host_buf, 8); + return ret; +}, + +fd_prestat_dir_name: function(fd, path, path_len) { + let host_buf = _malloc(path_len); + let ret = ___wasi_fd_prestat_get(fd, host_buf, path_len); + copyout_bytes(buf, host_buf, path_len); + return ret; +}, + +fd_close: function(fd) { + return ___wasi_fd_close(fd); +}, + +fd_datasync: function(fd) { + return ___wasi_fd_datasync(fd); +}, + +fd_pread: function(fd, iovs, iovs_len, offset, nread) { + let host_iovs = translate_iovs(iovs, iovs_len); + let host_nread = _malloc(4); + let ret = ___wasi_fd_pread(fd, host_iovs, iovs_len, offset, host_nread); + copyout_i32(nread, host_nread); + free_iovs(host_iovs, iovs_len, iovs); + return ret; +}, + +fd_pwrite: function(fd, iovs, iovs_len, offset, nwritten) { + let host_iovs = translate_ciovs(iovs, iovs_len); + let host_nwritten = _malloc(4); + let ret = ___wasi_fd_pwrite(fd, host_iovs, iovs_len, offset, host_nwritten); + copyout_i32(nwritten, host_nwritten); + free_ciovs(host_iovs, iovs_len); + return ret; +}, + +fd_read: function(fd, iovs, iovs_len, nread) { + let host_iovs = translate_iovs(iovs, iovs_len); + let host_nread = _malloc(4); + let ret = ___wasi_fd_read(fd, host_iovs, iovs_len, host_nread); + copyout_i32(nread, host_nread); + free_iovs(host_iovs, iovs_len, iovs); + return ret; +}, + +fd_renumber: function(from, to) { + return ___wasi_fd_renumber(from, to); +}, + +fd_seek: function(fd, offset, whence, newoffset) { + let host_newoffset = _malloc(8); + let ret = ___wasi_fd_seek(fd, offset, whence, host_newoffset); + copyout_i64(newoffset, host_newoffset); + return ret; +}, + +fd_tell: function(fd, newoffset) { + let host_newoffset = _malloc(8); + let ret = ___wasi_fd_seek(fd, host_newoffset); + copyout_i64(newoffset, host_newoffset); + return ret; +}, + +fd_fdstat_get: function(fd, buf) { + let host_buf = _malloc(24); // sizeof __wasi_fdstat_t + let ret = ___wasi_fd_fdstat_get(fd, host_buf); + copyout_bytes(buf, host_buf, 24); + return ret; +}, + +fd_fdstat_set_flags: function(fd, flags) { + return ___wasi_fd_fdstat_set_flags(fd, flags); +}, + +fd_fdstat_set_rights: function(fd, fs_rights_base, fs_rights_inheriting) { + return ___wasi_fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting); +}, + +fd_sync: function(fd) { + return ___wasi_fd_sync(fd); +}, + +fd_write: function(fd, iovs, iovs_len, nwritten) { + let host_iovs = translate_ciovs(iovs, iovs_len); + let host_nwritten = _malloc(4); + let ret = ___wasi_fd_write(fd, host_iovs, iovs_len, host_nwritten); + copyout_i32(nwritten, host_nwritten); + free_ciovs(host_iovs, iovs_len); + return ret; +}, + +fd_advise: function(fd, offset, len, advice) { + return ___wasi_fd_advise(fd, offset, len, advice); +}, + +fd_allocate: function(fd, offset, len) { + return ___wasi_fd_allocate(fd, offset, len); +}, + +path_create_directory: function(fd, path, path_len) { + let host_path = copyin_bytes(path, path_len); + let ret = ___wasi_path_create_directory(fd, host_path, path_len); + _free(host_path); + return ret; +}, + +path_link: function(fd0, path0, path_len0, fd1, path1, path_len1) { + let host_path0 = copyin_bytes(path0, path_len0); + let host_path1 = copyin_bytes(path1, path_len1); + let ret = ___wasi_path_link(fd, host_path0, path_len0, fd1, host_path1, path1_len); + _free(host_path1); + _free(host_path0); + return ret; +}, + +path_open: function(dirfd, dirflags, path, path_len, oflags, fs_rights_base, fs_rights_inheriting, fs_flags, fd) { + let host_path = copyin_bytes(path, path_len); + let host_fd = _malloc(4); + let ret = ___wasi_path_open(dirfd, dirflags, host_path, path_len, oflags, fs_rights_base, fs_rights_inheriting, fs_flags, host_fd); + copyout_i32(fd, host_fd); + _free(host_path); + return ret; +}, + +fd_readdir: function(fd, buf, buf_len, cookie, buf_used) { + let host_buf = _malloc(buf_len); + let host_buf_used = _malloc(4); + let ret = ___wasi_fd_readdir(fd, buf, buf_len, cookie, host_buf_used); + copyout_i32(buf_used, host_buf_used); + copyout_bytes(buf, host_buf, buf_len); + return ret; +}, + +path_readlink: function(fd, path, path_len, buf, buf_len, buf_used) { + let host_path = copyin_bytes(path, path_len); + let host_buf = _malloc(buf_len); + let host_buf_used = _malloc(4); + let ret = ___wasi_path_readlink(fd, path, path_len, buf, buf_len, host_buf_used); + copyout_i32(buf_used, host_buf_used); + copyout_bytes(buf, host_buf, buf_len); + _free(host_path); + return ret; +}, + +path_rename: function(fd0, path0, path_len0, fd1, path1, path_len1) { + let host_path0 = copyin_bytes(path0, path_len0); + let host_path1 = copyin_bytes(path1, path_len1); + let ret = ___wasi_path_rename(fd, host_path0, path_len0, fd1, host_path1, path1_len); + _free(host_path1); + _free(host_path0); + return ret; +}, + +fd_filestat_get: function(fd, buf) { + let host_buf = _malloc(56); // sizeof __wasi_filestat_t + let ret = ___wasi_fd_filestat_get(host_buf); + copyout_bytes(buf, host_buf, 56); + return ret; +}, + +fd_filestat_set_size: function(fd, size) { + return ___wasi_fd_filestat_set_size(fd, size); +}, + +fd_filestat_set_times: function(fd, st_atim, st_mtim, fstflags) { + return ___wasi_fd_filestat_set_times(fd, st_atim, st_mtim, fstflags); +}, + +path_filestat_get: function(fd, path, path_len, buf) { + let host_path = copyin_bytes(path, path_len); + let host_buf = _malloc(56); // sizeof __wasi_filestat_t + let ret = ___wasi_path_filestat_get(fd, host_path, path_len, host_buf); + copyout_bytes(buf, host_buf, 56); + _free(host_path); + return ret; +}, + +path_filestat_set_times: function(fd, path, path_len, st_atim, st_mtim, flags) { + let host_path = copyin_bytes(path, path_len); + let ret = ___wasi_path_filestat_set_times(fd, host_path, st_atim, st_mtim, fstflags); + _free(host_path); + return ret; +}, + +path_symlink: function(path0, path_len0, fd, path1, path_len1) { + let host_path0 = copyin_bytes(path0, path0_len); + let host_path1 = copyin_bytes(path1, path1_len); + let ret = ___wasi_path_symlink(host_path0, path_len0, fd, host_path1, path_len1); + _free(host_path1); + _free(host_path0); + return ret; +}, + +path_unlink_file: function(fd, path, path_len, flags) { + let host_path = copyin_bytes(path, path_len); + let ret = ___wasi_path_unlink_file(fd, host_path, path_len, flags); + _free(host_path); + return ret; +}, + +path_remove_directory: function(fd, path, path_len, flags) { + let host_path = copyin_bytes(path, path_len); + let ret = ___wasi_path_remove_directory(fd, host_path, path_len, flags); + _free(host_path); + return ret; +}, + +poll_oneoff: function(in_, out, nsubscriptions, nevents) { + let host_in = copyin_bytes(in_, nsubscriptions * 56); // sizeof __wasi_subscription_t + let host_out = _malloc(nsubscriptions * 32); // sizeof __wasi_event_t + let host_nevents = _malloc(4); + let ret = ___wasi_poll_oneoff(host_in, host_out, host_nevents); + copyout_bytes(out, host_out, nsubscriptions * 32); + copyout_i32(nevents, host_nevents); + _free(host_in); + return ret; +}, + +proc_exit: function(rval) { + let message; + if (rval == 0) { + message = "success"; + } else { + message = "error code " + rval; + } + throw new WASIExit(rval, message); +}, + +proc_raise: function(sig) { + if (sig == 18 || // SIGSTOP + sig == 19 || // SIGTSTP + sig == 20 || // SIGTTIN + sig == 21 || // SIGTTOU + sig == 22 || // SIGURG + sig == 16 || // SIGCHLD + sig == 13) // SIGPIPE + { + return 0; + } + + let message = "raised signal " + sig; + throw new WASIExit(128 + sig, message); +}, + +random_get: function(buf, buf_len) { + let host_buf = _malloc(buf_len); + let ret = ___wasi_random_get(host_buf, buf_len); + copyout_bytes(buf, host_buf, buf_len); + return ret; +}, + +sched_yield: function() { + return ___wasi_sched_yield(); +}, + +sock_recv: function(sock, ri_data, ri_data_len, ri_flags, ro_datalen, ro_flags) { + let host_ri_data = translate_iovs(ri_data, ri_data_len); + let host_ro_datalen = _malloc(4); + let ret = ___wasi_sock_recv(sock, host_ri_data, ri_data_len, ri_flags, host_ro_data, ro_flags); + copyout_i32(ro_datalen, host_ro_datalen); + free_iovs(host_ri_data, ri_data_len, ri_data); + return ret; +}, + +sock_send: function(sock, si_data, si_data_len, si_flags, so_datalen) { + let host_si_data = translate_ciovs(si_data, si_data_len); + let host_so_datalen = _malloc(4); + let ret = ___wasi_sock_send(sock, host_si_data, si_data_len, si_flags, host_so_datalen); + copyout_i32(so_datalen, host_so_datalen); + free_ciovs(host_si_data, si_data_len); + return ret; +}, + +sock_shutdown: function(sock, how) { + return ___wasi_sock_shutdown(sock, how); +} + +}; diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/CMakeLists.txt b/wasmtime-wasi-c/sandboxed-system-primitives/CMakeLists.txt new file mode 100644 index 000000000000..3fb11b3b77ea --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.0) +project(SandboxedSystemPrimitives C) + +include_directories(include) + +add_library(SandboxedSystemPrimitives STATIC src/posix.c src/random.c src/str.c) + +install(TARGETS SandboxedSystemPrimitives DESTINATION .) diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/LICENSE b/wasmtime-wasi-c/sandboxed-system-primitives/LICENSE new file mode 100644 index 000000000000..83386f80ae94 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/LICENSE @@ -0,0 +1,7 @@ +Please see the LICENSE file in each top-level directory for the terms applicable to that directory and its relative sub-directories. + +The relevant directories and licenses are: + +src/ - BSD-2-Clause; see src/LICENSE for details +include/ - CC0 1.0 Universal (CC0 1.0) Public Domain Dedication +polyfill/clang/ - MIT; see the header of polyfill/clang/stdatomic.h for details diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/README.md b/wasmtime-wasi-c/sandboxed-system-primitives/README.md new file mode 100644 index 000000000000..feba35e9c329 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/README.md @@ -0,0 +1,9 @@ +This repository contains adapted forms of the CloudABI project's "libemulator" +library, which includes implementations the CloudABI system calls using +standard native platform support. See the README.md and LICENSE files in +the individual subdirectories for details. + +src/ - cloudabi-utils' libemulator; see src/README.md for details +include/ - wasi headers + +This is currently an experimental prototype. diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/include/LICENSE b/wasmtime-wasi-c/sandboxed-system-primitives/include/LICENSE new file mode 100644 index 000000000000..0e259d42c996 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/include/LICENSE @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/include/wasmtime_ssp.h b/wasmtime-wasi-c/sandboxed-system-primitives/include/wasmtime_ssp.h new file mode 100644 index 000000000000..6a0d348c4fe2 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/include/wasmtime_ssp.h @@ -0,0 +1,866 @@ +/* + * Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. + * + * This file declares an interface similar to WASI, but augmented to expose + * some implementation details such as the curfds arguments that we pass + * around to avoid storing them in TLS. + */ + +#ifndef WASMTIME_SSP_H +#define WASMTIME_SSP_H + +#include +#include + +_Static_assert(_Alignof(int8_t) == 1, "non-wasi data layout"); +_Static_assert(_Alignof(uint8_t) == 1, "non-wasi data layout"); +_Static_assert(_Alignof(int16_t) == 2, "non-wasi data layout"); +_Static_assert(_Alignof(uint16_t) == 2, "non-wasi data layout"); +_Static_assert(_Alignof(int32_t) == 4, "non-wasi data layout"); +_Static_assert(_Alignof(uint32_t) == 4, "non-wasi data layout"); +_Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout"); +_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout"); + +#ifdef __cplusplus +extern "C" { +#endif + +typedef uint8_t __wasi_advice_t; +#define __WASI_ADVICE_NORMAL (0) +#define __WASI_ADVICE_SEQUENTIAL (1) +#define __WASI_ADVICE_RANDOM (2) +#define __WASI_ADVICE_WILLNEED (3) +#define __WASI_ADVICE_DONTNEED (4) +#define __WASI_ADVICE_NOREUSE (5) + +typedef uint32_t __wasi_clockid_t; +#define __WASI_CLOCK_REALTIME (0) +#define __WASI_CLOCK_MONOTONIC (1) +#define __WASI_CLOCK_PROCESS_CPUTIME_ID (2) +#define __WASI_CLOCK_THREAD_CPUTIME_ID (3) + +typedef uint64_t __wasi_device_t; + +typedef uint64_t __wasi_dircookie_t; +#define __WASI_DIRCOOKIE_START (0) + +typedef uint16_t __wasi_errno_t; +#define __WASI_ESUCCESS (0) +#define __WASI_E2BIG (1) +#define __WASI_EACCES (2) +#define __WASI_EADDRINUSE (3) +#define __WASI_EADDRNOTAVAIL (4) +#define __WASI_EAFNOSUPPORT (5) +#define __WASI_EAGAIN (6) +#define __WASI_EALREADY (7) +#define __WASI_EBADF (8) +#define __WASI_EBADMSG (9) +#define __WASI_EBUSY (10) +#define __WASI_ECANCELED (11) +#define __WASI_ECHILD (12) +#define __WASI_ECONNABORTED (13) +#define __WASI_ECONNREFUSED (14) +#define __WASI_ECONNRESET (15) +#define __WASI_EDEADLK (16) +#define __WASI_EDESTADDRREQ (17) +#define __WASI_EDOM (18) +#define __WASI_EDQUOT (19) +#define __WASI_EEXIST (20) +#define __WASI_EFAULT (21) +#define __WASI_EFBIG (22) +#define __WASI_EHOSTUNREACH (23) +#define __WASI_EIDRM (24) +#define __WASI_EILSEQ (25) +#define __WASI_EINPROGRESS (26) +#define __WASI_EINTR (27) +#define __WASI_EINVAL (28) +#define __WASI_EIO (29) +#define __WASI_EISCONN (30) +#define __WASI_EISDIR (31) +#define __WASI_ELOOP (32) +#define __WASI_EMFILE (33) +#define __WASI_EMLINK (34) +#define __WASI_EMSGSIZE (35) +#define __WASI_EMULTIHOP (36) +#define __WASI_ENAMETOOLONG (37) +#define __WASI_ENETDOWN (38) +#define __WASI_ENETRESET (39) +#define __WASI_ENETUNREACH (40) +#define __WASI_ENFILE (41) +#define __WASI_ENOBUFS (42) +#define __WASI_ENODEV (43) +#define __WASI_ENOENT (44) +#define __WASI_ENOEXEC (45) +#define __WASI_ENOLCK (46) +#define __WASI_ENOLINK (47) +#define __WASI_ENOMEM (48) +#define __WASI_ENOMSG (49) +#define __WASI_ENOPROTOOPT (50) +#define __WASI_ENOSPC (51) +#define __WASI_ENOSYS (52) +#define __WASI_ENOTCONN (53) +#define __WASI_ENOTDIR (54) +#define __WASI_ENOTEMPTY (55) +#define __WASI_ENOTRECOVERABLE (56) +#define __WASI_ENOTSOCK (57) +#define __WASI_ENOTSUP (58) +#define __WASI_ENOTTY (59) +#define __WASI_ENXIO (60) +#define __WASI_EOVERFLOW (61) +#define __WASI_EOWNERDEAD (62) +#define __WASI_EPERM (63) +#define __WASI_EPIPE (64) +#define __WASI_EPROTO (65) +#define __WASI_EPROTONOSUPPORT (66) +#define __WASI_EPROTOTYPE (67) +#define __WASI_ERANGE (68) +#define __WASI_EROFS (69) +#define __WASI_ESPIPE (70) +#define __WASI_ESRCH (71) +#define __WASI_ESTALE (72) +#define __WASI_ETIMEDOUT (73) +#define __WASI_ETXTBSY (74) +#define __WASI_EXDEV (75) +#define __WASI_ENOTCAPABLE (76) + +typedef uint16_t __wasi_eventrwflags_t; +#define __WASI_EVENT_FD_READWRITE_HANGUP (0x0001) + +typedef uint8_t __wasi_eventtype_t; +#define __WASI_EVENTTYPE_CLOCK (0) +#define __WASI_EVENTTYPE_FD_READ (1) +#define __WASI_EVENTTYPE_FD_WRITE (2) + +typedef uint32_t __wasi_exitcode_t; + +typedef uint32_t __wasi_fd_t; + +typedef uint16_t __wasi_fdflags_t; +#define __WASI_FDFLAG_APPEND (0x0001) +#define __WASI_FDFLAG_DSYNC (0x0002) +#define __WASI_FDFLAG_NONBLOCK (0x0004) +#define __WASI_FDFLAG_RSYNC (0x0008) +#define __WASI_FDFLAG_SYNC (0x0010) + +typedef int64_t __wasi_filedelta_t; + +typedef uint64_t __wasi_filesize_t; + +typedef uint8_t __wasi_filetype_t; +#define __WASI_FILETYPE_UNKNOWN (0) +#define __WASI_FILETYPE_BLOCK_DEVICE (1) +#define __WASI_FILETYPE_CHARACTER_DEVICE (2) +#define __WASI_FILETYPE_DIRECTORY (3) +#define __WASI_FILETYPE_REGULAR_FILE (4) +#define __WASI_FILETYPE_SOCKET_DGRAM (5) +#define __WASI_FILETYPE_SOCKET_STREAM (6) +#define __WASI_FILETYPE_SYMBOLIC_LINK (7) + +typedef uint16_t __wasi_fstflags_t; +#define __WASI_FILESTAT_SET_ATIM (0x0001) +#define __WASI_FILESTAT_SET_ATIM_NOW (0x0002) +#define __WASI_FILESTAT_SET_MTIM (0x0004) +#define __WASI_FILESTAT_SET_MTIM_NOW (0x0008) + +typedef uint64_t __wasi_inode_t; + +typedef uint32_t __wasi_linkcount_t; + +typedef uint32_t __wasi_lookupflags_t; +#define __WASI_LOOKUP_SYMLINK_FOLLOW (0x00000001) + +typedef uint16_t __wasi_oflags_t; +#define __WASI_O_CREAT (0x0001) +#define __WASI_O_DIRECTORY (0x0002) +#define __WASI_O_EXCL (0x0004) +#define __WASI_O_TRUNC (0x0008) + +typedef uint16_t __wasi_riflags_t; +#define __WASI_SOCK_RECV_PEEK (0x0001) +#define __WASI_SOCK_RECV_WAITALL (0x0002) + +typedef uint64_t __wasi_rights_t; +#define __WASI_RIGHT_FD_DATASYNC (0x0000000000000001) +#define __WASI_RIGHT_FD_READ (0x0000000000000002) +#define __WASI_RIGHT_FD_SEEK (0x0000000000000004) +#define __WASI_RIGHT_FD_FDSTAT_SET_FLAGS (0x0000000000000008) +#define __WASI_RIGHT_FD_SYNC (0x0000000000000010) +#define __WASI_RIGHT_FD_TELL (0x0000000000000020) +#define __WASI_RIGHT_FD_WRITE (0x0000000000000040) +#define __WASI_RIGHT_FD_ADVISE (0x0000000000000080) +#define __WASI_RIGHT_FD_ALLOCATE (0x0000000000000100) +#define __WASI_RIGHT_PATH_CREATE_DIRECTORY (0x0000000000000200) +#define __WASI_RIGHT_PATH_CREATE_FILE (0x0000000000000400) +#define __WASI_RIGHT_PATH_LINK_SOURCE (0x0000000000000800) +#define __WASI_RIGHT_PATH_LINK_TARGET (0x0000000000001000) +#define __WASI_RIGHT_PATH_OPEN (0x0000000000002000) +#define __WASI_RIGHT_FD_READDIR (0x0000000000004000) +#define __WASI_RIGHT_PATH_READLINK (0x0000000000008000) +#define __WASI_RIGHT_PATH_RENAME_SOURCE (0x0000000000010000) +#define __WASI_RIGHT_PATH_RENAME_TARGET (0x0000000000020000) +#define __WASI_RIGHT_PATH_FILESTAT_GET (0x0000000000040000) +#define __WASI_RIGHT_PATH_FILESTAT_SET_SIZE (0x0000000000080000) +#define __WASI_RIGHT_PATH_FILESTAT_SET_TIMES (0x0000000000100000) +#define __WASI_RIGHT_FD_FILESTAT_GET (0x0000000000200000) +#define __WASI_RIGHT_FD_FILESTAT_SET_SIZE (0x0000000000400000) +#define __WASI_RIGHT_FD_FILESTAT_SET_TIMES (0x0000000000800000) +#define __WASI_RIGHT_PATH_SYMLINK (0x0000000001000000) +#define __WASI_RIGHT_PATH_REMOVE_DIRECTORY (0x0000000002000000) +#define __WASI_RIGHT_PATH_UNLINK_FILE (0x0000000004000000) +#define __WASI_RIGHT_POLL_FD_READWRITE (0x0000000008000000) +#define __WASI_RIGHT_SOCK_SHUTDOWN (0x0000000010000000) + +typedef uint16_t __wasi_roflags_t; +#define __WASI_SOCK_RECV_DATA_TRUNCATED (0x0001) + +typedef uint8_t __wasi_sdflags_t; +#define __WASI_SHUT_RD (0x01) +#define __WASI_SHUT_WR (0x02) + +typedef uint16_t __wasi_siflags_t; + +typedef uint8_t __wasi_signal_t; +// 0 is reserved; POSIX has special semantics for kill(pid, 0). +#define __WASI_SIGHUP (1) +#define __WASI_SIGINT (2) +#define __WASI_SIGQUIT (3) +#define __WASI_SIGILL (4) +#define __WASI_SIGTRAP (5) +#define __WASI_SIGABRT (6) +#define __WASI_SIGBUS (7) +#define __WASI_SIGFPE (8) +#define __WASI_SIGKILL (9) +#define __WASI_SIGUSR1 (10) +#define __WASI_SIGSEGV (11) +#define __WASI_SIGUSR2 (12) +#define __WASI_SIGPIPE (13) +#define __WASI_SIGALRM (14) +#define __WASI_SIGTERM (15) +#define __WASI_SIGCHLD (16) +#define __WASI_SIGCONT (17) +#define __WASI_SIGSTOP (18) +#define __WASI_SIGTSTP (19) +#define __WASI_SIGTTIN (20) +#define __WASI_SIGTTOU (21) +#define __WASI_SIGURG (22) +#define __WASI_SIGXCPU (23) +#define __WASI_SIGXFSZ (24) +#define __WASI_SIGVTALRM (25) +#define __WASI_SIGPROF (26) +#define __WASI_SIGWINCH (27) +#define __WASI_SIGPOLL (28) +#define __WASI_SIGPWR (29) +#define __WASI_SIGSYS (30) + +typedef uint16_t __wasi_subclockflags_t; +#define __WASI_SUBSCRIPTION_CLOCK_ABSTIME (0x0001) + +typedef uint64_t __wasi_timestamp_t; + +typedef uint64_t __wasi_userdata_t; + +typedef uint8_t __wasi_whence_t; +#define __WASI_WHENCE_CUR (0) +#define __WASI_WHENCE_END (1) +#define __WASI_WHENCE_SET (2) + +typedef uint8_t __wasi_preopentype_t; +#define __WASI_PREOPENTYPE_DIR (0) + +struct fd_table; +struct fd_prestats; +struct argv_environ_values; + +typedef struct __wasi_dirent_t { + __wasi_dircookie_t d_next; + __wasi_inode_t d_ino; + uint32_t d_namlen; + __wasi_filetype_t d_type; +} __wasi_dirent_t; +_Static_assert(offsetof(__wasi_dirent_t, d_next) == 0, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_dirent_t, d_ino) == 8, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_dirent_t, d_namlen) == 16, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_dirent_t, d_type) == 20, "non-wasi data layout"); +_Static_assert(sizeof(__wasi_dirent_t) == 24, "non-wasi data layout"); +_Static_assert(_Alignof(__wasi_dirent_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_event_t { + __wasi_userdata_t userdata; + __wasi_errno_t error; + __wasi_eventtype_t type; + union __wasi_event_u { + struct __wasi_event_u_fd_readwrite_t { + __wasi_filesize_t nbytes; + __wasi_eventrwflags_t flags; + } fd_readwrite; + } u; +} __wasi_event_t; +_Static_assert(offsetof(__wasi_event_t, userdata) == 0, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_event_t, error) == 8, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_event_t, type) == 10, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_event_t, u.fd_readwrite.nbytes) == 16, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_event_t, u.fd_readwrite.flags) == 24, "non-wasi data layout"); +_Static_assert(sizeof(__wasi_event_t) == 32, "non-wasi data layout"); +_Static_assert(_Alignof(__wasi_event_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_prestat_t { + __wasi_preopentype_t pr_type; + union __wasi_prestat_u { + struct __wasi_prestat_u_dir_t { + size_t pr_name_len; + } dir; + } u; +} __wasi_prestat_t; +_Static_assert(offsetof(__wasi_prestat_t, pr_type) == 0, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + offsetof(__wasi_prestat_t, u.dir.pr_name_len) == 4, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + offsetof(__wasi_prestat_t, u.dir.pr_name_len) == 8, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + sizeof(__wasi_prestat_t) == 8, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + sizeof(__wasi_prestat_t) == 16, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + _Alignof(__wasi_prestat_t) == 4, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + _Alignof(__wasi_prestat_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_fdstat_t { + __wasi_filetype_t fs_filetype; + __wasi_fdflags_t fs_flags; + __wasi_rights_t fs_rights_base; + __wasi_rights_t fs_rights_inheriting; +} __wasi_fdstat_t; +_Static_assert( + offsetof(__wasi_fdstat_t, fs_filetype) == 0, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_fdstat_t, fs_flags) == 2, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_fdstat_t, fs_rights_base) == 8, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_fdstat_t, fs_rights_inheriting) == 16, + "non-wasi data layout"); +_Static_assert(sizeof(__wasi_fdstat_t) == 24, "non-wasi data layout"); +_Static_assert(_Alignof(__wasi_fdstat_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_filestat_t { + __wasi_device_t st_dev; + __wasi_inode_t st_ino; + __wasi_filetype_t st_filetype; + __wasi_linkcount_t st_nlink; + __wasi_filesize_t st_size; + __wasi_timestamp_t st_atim; + __wasi_timestamp_t st_mtim; + __wasi_timestamp_t st_ctim; +} __wasi_filestat_t; +_Static_assert(offsetof(__wasi_filestat_t, st_dev) == 0, "non-wasi data layout"); +_Static_assert(offsetof(__wasi_filestat_t, st_ino) == 8, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_filestat_t, st_filetype) == 16, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_filestat_t, st_nlink) == 20, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_filestat_t, st_size) == 24, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_filestat_t, st_atim) == 32, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_filestat_t, st_mtim) == 40, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_filestat_t, st_ctim) == 48, "non-wasi data layout"); +_Static_assert(sizeof(__wasi_filestat_t) == 56, "non-wasi data layout"); +_Static_assert(_Alignof(__wasi_filestat_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_ciovec_t { + const void *buf; + size_t buf_len; +} __wasi_ciovec_t; +_Static_assert(offsetof(__wasi_ciovec_t, buf) == 0, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + offsetof(__wasi_ciovec_t, buf_len) == 4, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + offsetof(__wasi_ciovec_t, buf_len) == 8, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + sizeof(__wasi_ciovec_t) == 8, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + sizeof(__wasi_ciovec_t) == 16, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + _Alignof(__wasi_ciovec_t) == 4, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + _Alignof(__wasi_ciovec_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_iovec_t { + void *buf; + size_t buf_len; +} __wasi_iovec_t; +_Static_assert(offsetof(__wasi_iovec_t, buf) == 0, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + offsetof(__wasi_iovec_t, buf_len) == 4, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + offsetof(__wasi_iovec_t, buf_len) == 8, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + sizeof(__wasi_iovec_t) == 8, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + sizeof(__wasi_iovec_t) == 16, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 4 || + _Alignof(__wasi_iovec_t) == 4, "non-wasi data layout"); +_Static_assert(sizeof(void *) != 8 || + _Alignof(__wasi_iovec_t) == 8, "non-wasi data layout"); + +typedef struct __wasi_subscription_t { + __wasi_userdata_t userdata; + __wasi_eventtype_t type; + union __wasi_subscription_u { + struct __wasi_subscription_u_clock_t { + __wasi_userdata_t identifier; + __wasi_clockid_t clock_id; + __wasi_timestamp_t timeout; + __wasi_timestamp_t precision; + __wasi_subclockflags_t flags; + } clock; + struct __wasi_subscription_u_fd_readwrite_t { + __wasi_fd_t fd; + } fd_readwrite; + } u; +} __wasi_subscription_t; +_Static_assert( + offsetof(__wasi_subscription_t, userdata) == 0, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, type) == 8, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, u.clock.identifier) == 16, + "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, u.clock.clock_id) == 24, + "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, u.clock.timeout) == 32, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, u.clock.precision) == 40, + "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, u.clock.flags) == 48, "non-wasi data layout"); +_Static_assert( + offsetof(__wasi_subscription_t, u.fd_readwrite.fd) == 16, + "non-wasi data layout"); +_Static_assert(sizeof(__wasi_subscription_t) == 56, "non-wasi data layout"); +_Static_assert(_Alignof(__wasi_subscription_t) == 8, "non-wasi data layout"); + +#if defined(WASMTIME_SSP_WASI_API) +#define WASMTIME_SSP_SYSCALL_NAME(name) \ + asm("__wasi_" #name) +#else +#define WASMTIME_SSP_SYSCALL_NAME(name) +#endif + +__wasi_errno_t wasmtime_ssp_args_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *arg_environ, +#endif + char **argv, + char *argv_buf +) WASMTIME_SSP_SYSCALL_NAME(args_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_args_sizes_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *arg_environ, +#endif + size_t *argc, + size_t *argv_buf_size +) WASMTIME_SSP_SYSCALL_NAME(args_sizes_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_clock_res_get( + __wasi_clockid_t clock_id, + __wasi_timestamp_t *resolution +) WASMTIME_SSP_SYSCALL_NAME(clock_res_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_clock_time_get( + __wasi_clockid_t clock_id, + __wasi_timestamp_t precision, + __wasi_timestamp_t *time +) WASMTIME_SSP_SYSCALL_NAME(clock_time_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_environ_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *arg_environ, +#endif + char **environ, + char *environ_buf +) WASMTIME_SSP_SYSCALL_NAME(environ_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_environ_sizes_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *arg_environ, +#endif + size_t *environ_count, + size_t *environ_buf_size +) WASMTIME_SSP_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_prestat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_prestats *prestats, +#endif + __wasi_fd_t fd, + __wasi_prestat_t *buf +) WASMTIME_SSP_SYSCALL_NAME(fd_prestat_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_prestat_dir_name( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_prestats *prestats, +#endif + __wasi_fd_t fd, + char *path, + size_t path_len +) WASMTIME_SSP_SYSCALL_NAME(fd_prestat_dir_name) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_close( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, + struct fd_prestats *prestats, +#endif + __wasi_fd_t fd +) WASMTIME_SSP_SYSCALL_NAME(fd_close) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_datasync( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd +) WASMTIME_SSP_SYSCALL_NAME(fd_datasync) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_pread( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_iovec_t *iovs, + size_t iovs_len, + __wasi_filesize_t offset, + size_t *nread +) WASMTIME_SSP_SYSCALL_NAME(fd_pread) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_pwrite( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_ciovec_t *iovs, + size_t iovs_len, + __wasi_filesize_t offset, + size_t *nwritten +) WASMTIME_SSP_SYSCALL_NAME(fd_pwrite) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_read( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_iovec_t *iovs, + size_t iovs_len, + size_t *nread +) WASMTIME_SSP_SYSCALL_NAME(fd_read) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_renumber( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, + struct fd_prestats *prestats, +#endif + __wasi_fd_t from, + __wasi_fd_t to +) WASMTIME_SSP_SYSCALL_NAME(fd_renumber) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_seek( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filedelta_t offset, + __wasi_whence_t whence, + __wasi_filesize_t *newoffset +) WASMTIME_SSP_SYSCALL_NAME(fd_seek) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_tell( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t *newoffset +) WASMTIME_SSP_SYSCALL_NAME(fd_tell) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_fdstat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_fdstat_t *buf +) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_fdstat_set_flags( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_fdflags_t flags +) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_set_flags) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_fdstat_set_rights( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_rights_t fs_rights_base, + __wasi_rights_t fs_rights_inheriting +) WASMTIME_SSP_SYSCALL_NAME(fd_fdstat_set_rights) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_sync( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd +) WASMTIME_SSP_SYSCALL_NAME(fd_sync) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_write( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_ciovec_t *iovs, + size_t iovs_len, + size_t *nwritten +) WASMTIME_SSP_SYSCALL_NAME(fd_write) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_advise( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t offset, + __wasi_filesize_t len, + __wasi_advice_t advice +) WASMTIME_SSP_SYSCALL_NAME(fd_advise) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_allocate( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t offset, + __wasi_filesize_t len +) WASMTIME_SSP_SYSCALL_NAME(fd_allocate) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_create_directory( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t path_len +) WASMTIME_SSP_SYSCALL_NAME(path_create_directory) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_link( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t old_fd, + __wasi_lookupflags_t old_flags, + const char *old_path, + size_t old_path_len, + __wasi_fd_t new_fd, + const char *new_path, + size_t new_path_len +) WASMTIME_SSP_SYSCALL_NAME(path_link) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_open( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t dirfd, + __wasi_lookupflags_t dirflags, + const char *path, + size_t path_len, + __wasi_oflags_t oflags, + __wasi_rights_t fs_rights_base, + __wasi_rights_t fs_rights_inheriting, + __wasi_fdflags_t fs_flags, + __wasi_fd_t *fd +) WASMTIME_SSP_SYSCALL_NAME(path_open) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_readdir( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + void *buf, + size_t buf_len, + __wasi_dircookie_t cookie, + size_t *bufused +) WASMTIME_SSP_SYSCALL_NAME(fd_readdir) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_readlink( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t path_len, + char *buf, + size_t buf_len, + size_t *bufused +) WASMTIME_SSP_SYSCALL_NAME(path_readlink) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_rename( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t old_fd, + const char *old_path, + size_t old_path_len, + __wasi_fd_t new_fd, + const char *new_path, + size_t new_path_len +) WASMTIME_SSP_SYSCALL_NAME(path_rename) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_filestat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filestat_t *buf +) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_filestat_set_times( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_timestamp_t st_atim, + __wasi_timestamp_t st_mtim, + __wasi_fstflags_t fstflags +) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_set_times) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_fd_filestat_set_size( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t st_size +) WASMTIME_SSP_SYSCALL_NAME(fd_filestat_set_size) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_filestat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_lookupflags_t flags, + const char *path, + size_t path_len, + __wasi_filestat_t *buf +) WASMTIME_SSP_SYSCALL_NAME(path_filestat_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_filestat_set_times( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_lookupflags_t flags, + const char *path, + size_t path_len, + __wasi_timestamp_t st_atim, + __wasi_timestamp_t st_mtim, + __wasi_fstflags_t fstflags +) WASMTIME_SSP_SYSCALL_NAME(path_filestat_set_times) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_symlink( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + const char *old_path, + size_t old_path_len, + __wasi_fd_t fd, + const char *new_path, + size_t new_path_len +) WASMTIME_SSP_SYSCALL_NAME(path_symlink) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_unlink_file( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t path_len +) WASMTIME_SSP_SYSCALL_NAME(path_unlink_file) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_path_remove_directory( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t path_len +) WASMTIME_SSP_SYSCALL_NAME(path_remove_directory) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_poll_oneoff( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + const __wasi_subscription_t *in, + __wasi_event_t *out, + size_t nsubscriptions, + size_t *nevents +) WASMTIME_SSP_SYSCALL_NAME(poll_oneoff) __attribute__((__warn_unused_result__)); + +_Noreturn void wasmtime_ssp_proc_exit( + __wasi_exitcode_t rval +) WASMTIME_SSP_SYSCALL_NAME(proc_exit); + +__wasi_errno_t wasmtime_ssp_proc_raise( + __wasi_signal_t sig +) WASMTIME_SSP_SYSCALL_NAME(proc_raise) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_random_get( + void *buf, + size_t buf_len +) WASMTIME_SSP_SYSCALL_NAME(random_get) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_sock_recv( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t sock, + const __wasi_iovec_t *ri_data, + size_t ri_data_len, + __wasi_riflags_t ri_flags, + size_t *ro_datalen, + __wasi_roflags_t *ro_flags +) WASMTIME_SSP_SYSCALL_NAME(sock_recv) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_sock_send( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t sock, + const __wasi_ciovec_t *si_data, + size_t si_data_len, + __wasi_siflags_t si_flags, + size_t *so_datalen +) WASMTIME_SSP_SYSCALL_NAME(sock_send) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_sock_shutdown( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t sock, + __wasi_sdflags_t how +) WASMTIME_SSP_SYSCALL_NAME(sock_shutdown) __attribute__((__warn_unused_result__)); + +__wasi_errno_t wasmtime_ssp_sched_yield(void) + WASMTIME_SSP_SYSCALL_NAME(sched_yield) __attribute__((__warn_unused_result__)); + +#ifdef __cplusplus +} +#endif + +#undef WASMTIME_SSP_SYSCALL_NAME + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/LICENSE b/wasmtime-wasi-c/sandboxed-system-primitives/src/LICENSE new file mode 100644 index 000000000000..04c6f48a27d7 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/LICENSE @@ -0,0 +1,24 @@ +All code is distributed under the following license: + + Copyright (c) 2015 Nuxi, https://nuxi.nl/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/README.md b/wasmtime-wasi-c/sandboxed-system-primitives/src/README.md new file mode 100644 index 000000000000..77a34bb5c3c7 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/README.md @@ -0,0 +1,14 @@ +This directory consists of selected files copied from the [libemulator] +directory in the [cloudabi-utils] repository, with minor modifications, +along with the accompanying LICENSE file from that repository. + +The modifications are marked with `WASMTIME_*` preprocessor macros. + +The files were copied at git revision +be1ce21e1dded9c0c0a6ebe144cbea01cf44a874 +which is dated +Sun Jan 13 23:26:03 2019 +0100 +. + +[libemulator]: https://github.com/NuxiNL/cloudabi-utils/tree/master/src/libemulator +[cloudabi-utils]: https://github.com/NuxiNL/cloudabi-utils diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/config.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/config.h new file mode 100644 index 000000000000..714e29d24fce --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/config.h @@ -0,0 +1,100 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef CONFIG_H +#define CONFIG_H + +#include + +#if defined(__FreeBSD__) || defined(__APPLE__) +#define CONFIG_HAS_ARC4RANDOM_BUF 1 +#else +#define CONFIG_HAS_ARC4RANDOM_BUF 0 +#endif + +// On Linux, prefer to use getrandom, though it isn't available in +// GLIBC before 2.25. +#if defined(__linux__) && \ + (!defined(__GLIBC__) || \ + __GLIBC__ > 2 || \ + (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)) +#define CONFIG_HAS_GETRANDOM 1 +#else +#define CONFIG_HAS_GETRANDOM 0 +#endif + +#if defined(__CloudABI__) +#define CONFIG_HAS_CAP_ENTER 1 +#else +#define CONFIG_HAS_CAP_ENTER 0 +#endif + +#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__) +#define CONFIG_HAS_CLOCK_NANOSLEEP 1 +#else +#define CONFIG_HAS_CLOCK_NANOSLEEP 0 +#endif + +#if !defined(__APPLE__) && !defined(__FreeBSD__) +#define CONFIG_HAS_FDATASYNC 1 +#else +#define CONFIG_HAS_FDATASYNC 0 +#endif + +#ifndef __CloudABI__ +#define CONFIG_HAS_ISATTY 1 +#else +#define CONFIG_HAS_ISATTY 0 +#endif + +#ifndef __APPLE__ +#define CONFIG_HAS_POSIX_FALLOCATE 1 +#else +#define CONFIG_HAS_POSIX_FALLOCATE 0 +#endif + +#ifndef __APPLE__ +#define CONFIG_HAS_PREADV 1 +#else +#define CONFIG_HAS_PREADV 0 +#endif + +#if defined(__APPLE__) || defined(__CloudABI__) +#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 1 +#else +#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 0 +#endif + +#ifndef __APPLE__ +#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 1 +#else +#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0 +#endif + +#ifndef __APPLE__ +#define CONFIG_HAS_PWRITEV 1 +#else +#define CONFIG_HAS_PWRITEV 0 +#endif + +#ifdef __APPLE__ +#define st_atimespec st_atim +#define st_mtimespec st_mtim +#define st_ctimespec st_ctim +#endif + +#ifdef __APPLE__ +#define CONFIG_TLS_USE_GSBASE 1 +#else +#define CONFIG_TLS_USE_GSBASE 0 +#endif + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h new file mode 100644 index 000000000000..0efb788ed5e4 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h @@ -0,0 +1,215 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef LOCKING_H +#define LOCKING_H + +#include "config.h" + +#include +#include +#include +#include +#include + +#ifndef __has_extension +#define __has_extension(x) 0 +#endif + +#if __has_extension(c_thread_safety_attributes) +#define LOCK_ANNOTATE(x) __attribute__((x)) +#else +#define LOCK_ANNOTATE(x) +#endif + +// Lock annotation macros. + +#define LOCKABLE LOCK_ANNOTATE(lockable) + +#define LOCKS_EXCLUSIVE(...) LOCK_ANNOTATE(exclusive_lock_function(__VA_ARGS__)) +#define LOCKS_SHARED(...) LOCK_ANNOTATE(shared_lock_function(__VA_ARGS__)) + +#define TRYLOCKS_EXCLUSIVE(...) \ + LOCK_ANNOTATE(exclusive_trylock_function(__VA_ARGS__)) +#define TRYLOCKS_SHARED(...) LOCK_ANNOTATE(shared_trylock_function(__VA_ARGS__)) + +#define UNLOCKS(...) LOCK_ANNOTATE(unlock_function(__VA_ARGS__)) + +#define REQUIRES_EXCLUSIVE(...) \ + LOCK_ANNOTATE(exclusive_locks_required(__VA_ARGS__)) +#define REQUIRES_SHARED(...) LOCK_ANNOTATE(shared_locks_required(__VA_ARGS__)) +#define REQUIRES_UNLOCKED(...) LOCK_ANNOTATE(locks_excluded(__VA_ARGS__)) + +#define NO_LOCK_ANALYSIS LOCK_ANNOTATE(no_thread_safety_analysis) + +// Mutex that uses the lock annotations. + +struct LOCKABLE mutex { + pthread_mutex_t object; +}; + +#define MUTEX_INITIALIZER \ + { PTHREAD_MUTEX_INITIALIZER } + +static inline void mutex_init(struct mutex *lock) REQUIRES_UNLOCKED(*lock) { + pthread_mutex_init(&lock->object, NULL); +} + +static inline void mutex_destroy(struct mutex *lock) REQUIRES_UNLOCKED(*lock) { + pthread_mutex_destroy(&lock->object); +} + +static inline void mutex_lock(struct mutex *lock) + LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { + pthread_mutex_lock(&lock->object); +} + +static inline void mutex_unlock(struct mutex *lock) + UNLOCKS(*lock) NO_LOCK_ANALYSIS { + pthread_mutex_unlock(&lock->object); +} + +// Read-write lock that uses the lock annotations. + +struct LOCKABLE rwlock { + pthread_rwlock_t object; +}; + +static inline void rwlock_init(struct rwlock *lock) REQUIRES_UNLOCKED(*lock) { + pthread_rwlock_init(&lock->object, NULL); +} + +static inline void rwlock_rdlock(struct rwlock *lock) + LOCKS_SHARED(*lock) NO_LOCK_ANALYSIS { + pthread_rwlock_rdlock(&lock->object); +} + +static inline void rwlock_wrlock(struct rwlock *lock) + LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { + pthread_rwlock_wrlock(&lock->object); +} + +static inline void rwlock_unlock(struct rwlock *lock) + UNLOCKS(*lock) NO_LOCK_ANALYSIS { + pthread_rwlock_unlock(&lock->object); +} + +// Condition variable that uses the lock annotations. + +struct LOCKABLE cond { + pthread_cond_t object; +#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \ + !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP + clockid_t clock; +#endif +}; + +static inline void cond_init_monotonic(struct cond *cond) { +#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK + pthread_condattr_t attr; + pthread_condattr_init(&attr); + pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); + pthread_cond_init(&cond->object, &attr); + pthread_condattr_destroy(&attr); +#else + pthread_cond_init(&cond->object, NULL); +#endif +#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \ + !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP + cond->clock = CLOCK_MONOTONIC; +#endif +} + +static inline void cond_init_realtime(struct cond *cond) { + pthread_cond_init(&cond->object, NULL); +#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \ + !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP + cond->clock = CLOCK_REALTIME; +#endif +} + +static inline void cond_destroy(struct cond *cond) { + pthread_cond_destroy(&cond->object); +} + +static inline void cond_signal(struct cond *cond) { + pthread_cond_signal(&cond->object); +} + +static inline bool cond_timedwait(struct cond *cond, struct mutex *lock, + uint64_t timeout, bool abstime) + REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { + struct timespec ts = { + .tv_sec = (time_t)(timeout / 1000000000), + .tv_nsec = (long)(timeout % 1000000000), + }; + + if (abstime) { +#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK + // No native support for sleeping on monotonic clocks. Convert the + // timeout to a relative value and then to an absolute value for the + // realtime clock. + if (cond->clock != CLOCK_REALTIME) { + struct timespec ts_monotonic; + clock_gettime(cond->clock, &ts_monotonic); + ts.tv_sec -= ts_monotonic.tv_sec; + ts.tv_nsec -= ts_monotonic.tv_nsec; + if (ts.tv_nsec < 0) { + ts.tv_nsec += 1000000000; + --ts.tv_sec; + } + + struct timespec ts_realtime; + clock_gettime(CLOCK_REALTIME, &ts_realtime); + ts.tv_sec += ts_realtime.tv_sec; + ts.tv_nsec += ts_realtime.tv_nsec; + if (ts.tv_nsec >= 1000000000) { + ts.tv_nsec -= 1000000000; + ++ts.tv_sec; + } + } +#endif + } else { +#if CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP + // Implementation supports relative timeouts. + int ret = + pthread_cond_timedwait_relative_np(&cond->object, &lock->object, &ts); + assert((ret == 0 || ret == ETIMEDOUT) && + "pthread_cond_timedwait_relative_np() failed"); + return ret == ETIMEDOUT; +#else + // Convert to absolute timeout. + struct timespec ts_now; +#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK + clock_gettime(cond->clock, &ts_now); +#else + clock_gettime(CLOCK_REALTIME, &ts_now); +#endif + ts.tv_sec += ts_now.tv_sec; + ts.tv_nsec += ts_now.tv_nsec; + if (ts.tv_nsec >= 1000000000) { + ts.tv_nsec -= 1000000000; + ++ts.tv_sec; + } +#endif + } + + int ret = pthread_cond_timedwait(&cond->object, &lock->object, &ts); + assert((ret == 0 || ret == ETIMEDOUT) && "pthread_cond_timedwait() failed"); + return ret == ETIMEDOUT; +} + +static inline void cond_wait(struct cond *cond, struct mutex *lock) + REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { + pthread_cond_wait(&cond->object, &lock->object); +} + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/numeric_limits.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/numeric_limits.h new file mode 100644 index 000000000000..30dddd19aae3 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/numeric_limits.h @@ -0,0 +1,42 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2015 Nuxi, https://nuxi.nl/ + +#ifndef COMMON_LIMITS_H +#define COMMON_LIMITS_H + +#include + +#define NUMERIC_MIN(t) \ + _Generic((t)0, char \ + : CHAR_MIN, signed char \ + : SCHAR_MIN, unsigned char : 0, short \ + : SHRT_MIN, unsigned short : 0, int \ + : INT_MIN, unsigned int : 0, long \ + : LONG_MIN, unsigned long : 0, long long \ + : LLONG_MIN, unsigned long long : 0, default \ + : (void)0) + +#define NUMERIC_MAX(t) \ + _Generic((t)0, char \ + : CHAR_MAX, signed char \ + : SCHAR_MAX, unsigned char \ + : UCHAR_MAX, short \ + : SHRT_MAX, unsigned short \ + : USHRT_MAX, int \ + : INT_MAX, unsigned int \ + : UINT_MAX, long \ + : LONG_MAX, unsigned long \ + : ULONG_MAX, long long \ + : LLONG_MAX, unsigned long long \ + : ULLONG_MAX, default \ + : (void)0) + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c b/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c new file mode 100644 index 000000000000..daf135fe8b57 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c @@ -0,0 +1,2781 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/ + +#include "config.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "locking.h" +#include "numeric_limits.h" +#include "posix.h" +#include "random.h" +#include "refcount.h" +#include "rights.h" +#include "str.h" + +// struct iovec must have the same layout as __wasi_iovec_t. +static_assert(offsetof(struct iovec, iov_base) == + offsetof(__wasi_iovec_t, buf), + "Offset mismatch"); +static_assert(sizeof(((struct iovec *)0)->iov_base) == + sizeof(((__wasi_iovec_t *)0)->buf), + "Size mismatch"); +static_assert(offsetof(struct iovec, iov_len) == + offsetof(__wasi_iovec_t, buf_len), + "Offset mismatch"); +static_assert(sizeof(((struct iovec *)0)->iov_len) == + sizeof(((__wasi_iovec_t *)0)->buf_len), + "Size mismatch"); +static_assert(sizeof(struct iovec) == sizeof(__wasi_iovec_t), + "Size mismatch"); + +// struct iovec must have the same layout as __wasi_ciovec_t. +static_assert(offsetof(struct iovec, iov_base) == + offsetof(__wasi_ciovec_t, buf), + "Offset mismatch"); +static_assert(sizeof(((struct iovec *)0)->iov_base) == + sizeof(((__wasi_ciovec_t *)0)->buf), + "Size mismatch"); +static_assert(offsetof(struct iovec, iov_len) == + offsetof(__wasi_ciovec_t, buf_len), + "Offset mismatch"); +static_assert(sizeof(((struct iovec *)0)->iov_len) == + sizeof(((__wasi_ciovec_t *)0)->buf_len), + "Size mismatch"); +static_assert(sizeof(struct iovec) == sizeof(__wasi_ciovec_t), + "Size mismatch"); + +#if defined(WASMTIME_SSP_STATIC_CURFDS) +static __thread struct fd_table *curfds; +static __thread struct fd_prestats *prestats; +static __thread struct argv_environ_values *argv_environ; +#endif + +// Converts a POSIX error code to a CloudABI error code. +static __wasi_errno_t convert_errno(int error) { + static const __wasi_errno_t errors[] = { +#define X(v) [v] = __WASI_##v + X(E2BIG), + X(EACCES), + X(EADDRINUSE), + X(EADDRNOTAVAIL), + X(EAFNOSUPPORT), + X(EAGAIN), + X(EALREADY), + X(EBADF), + X(EBADMSG), + X(EBUSY), + X(ECANCELED), + X(ECHILD), + X(ECONNABORTED), + X(ECONNREFUSED), + X(ECONNRESET), + X(EDEADLK), + X(EDESTADDRREQ), + X(EDOM), + X(EDQUOT), + X(EEXIST), + X(EFAULT), + X(EFBIG), + X(EHOSTUNREACH), + X(EIDRM), + X(EILSEQ), + X(EINPROGRESS), + X(EINTR), + X(EINVAL), + X(EIO), + X(EISCONN), + X(EISDIR), + X(ELOOP), + X(EMFILE), + X(EMLINK), + X(EMSGSIZE), + X(EMULTIHOP), + X(ENAMETOOLONG), + X(ENETDOWN), + X(ENETRESET), + X(ENETUNREACH), + X(ENFILE), + X(ENOBUFS), + X(ENODEV), + X(ENOENT), + X(ENOEXEC), + X(ENOLCK), + X(ENOLINK), + X(ENOMEM), + X(ENOMSG), + X(ENOPROTOOPT), + X(ENOSPC), + X(ENOSYS), +#ifdef ENOTCAPABLE + X(ENOTCAPABLE), +#endif + X(ENOTCONN), + X(ENOTDIR), + X(ENOTEMPTY), + X(ENOTRECOVERABLE), + X(ENOTSOCK), + X(ENOTSUP), + X(ENOTTY), + X(ENXIO), + X(EOVERFLOW), + X(EOWNERDEAD), + X(EPERM), + X(EPIPE), + X(EPROTO), + X(EPROTONOSUPPORT), + X(EPROTOTYPE), + X(ERANGE), + X(EROFS), + X(ESPIPE), + X(ESRCH), + X(ESTALE), + X(ETIMEDOUT), + X(ETXTBSY), + X(EXDEV), +#undef X +#if EOPNOTSUPP != ENOTSUP + [EOPNOTSUPP] = __WASI_ENOTSUP, +#endif +#if EWOULDBLOCK != EAGAIN + [EWOULDBLOCK] = __WASI_EAGAIN, +#endif + }; + if (error < 0 || (size_t)error >= sizeof(errors) / sizeof(errors[0]) || + errors[error] == 0) + return __WASI_ENOSYS; + return errors[error]; +} + +// Converts a POSIX timespec to a CloudABI timestamp. +static __wasi_timestamp_t convert_timespec( + const struct timespec *ts +) { + if (ts->tv_sec < 0) + return 0; + if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / 1000000000) + return UINT64_MAX; + return (__wasi_timestamp_t)ts->tv_sec * 1000000000 + ts->tv_nsec; +} + +// Converts a CloudABI clock identifier to a POSIX clock identifier. +static bool convert_clockid( + __wasi_clockid_t in, + clockid_t *out +) { + switch (in) { + case __WASI_CLOCK_MONOTONIC: + *out = CLOCK_MONOTONIC; + return true; + case __WASI_CLOCK_PROCESS_CPUTIME_ID: + *out = CLOCK_PROCESS_CPUTIME_ID; + return true; + case __WASI_CLOCK_REALTIME: + *out = CLOCK_REALTIME; + return true; + case __WASI_CLOCK_THREAD_CPUTIME_ID: + *out = CLOCK_THREAD_CPUTIME_ID; + return true; + default: + return false; + } +} + +__wasi_errno_t wasmtime_ssp_clock_res_get( + __wasi_clockid_t clock_id, + __wasi_timestamp_t *resolution +) { + clockid_t nclock_id; + if (!convert_clockid(clock_id, &nclock_id)) + return __WASI_EINVAL; + struct timespec ts; + if (clock_getres(nclock_id, &ts) < 0) + return convert_errno(errno); + *resolution = convert_timespec(&ts); + return 0; +} + +__wasi_errno_t wasmtime_ssp_clock_time_get( + __wasi_clockid_t clock_id, + __wasi_timestamp_t precision, + __wasi_timestamp_t *time +) { + clockid_t nclock_id; + if (!convert_clockid(clock_id, &nclock_id)) + return __WASI_EINVAL; + struct timespec ts; + if (clock_gettime(nclock_id, &ts) < 0) + return convert_errno(errno); + *time = convert_timespec(&ts); + return 0; +} + +struct fd_prestat { + const char *dir; +}; + +void fd_prestats_init( + struct fd_prestats *pt +) { + rwlock_init(&pt->lock); + pt->prestats = NULL; + pt->size = 0; + pt->used = 0; +#if defined(WASMTIME_SSP_STATIC_CURFDS) + prestats = pt; +#endif +} + +// Grows the preopened resource table to a required lower bound and a +// minimum number of free preopened resource table entries. +static bool fd_prestats_grow( + struct fd_prestats *pt, + size_t min, + size_t incr +) REQUIRES_EXCLUSIVE(pt->lock) { + if (pt->size <= min || pt->size < (pt->used + incr) * 2) { + // Keep on doubling the table size until we've met our constraints. + size_t size = pt->size == 0 ? 1 : pt->size; + while (size <= min || size < (pt->used + incr) * 2) + size *= 2; + + // Grow the file descriptor table's allocation. + struct fd_prestat *prestats = realloc(pt->prestats, sizeof(*prestats) * size); + if (prestats == NULL) + return false; + + // Mark all new file descriptors as unused. + for (size_t i = pt->size; i < size; ++i) + prestats[i].dir = NULL; + pt->prestats = prestats; + pt->size = size; + } + return true; +} + +// Inserts a preopened resource record into the preopened resource table. +bool fd_prestats_insert( + struct fd_prestats *pt, + const char *dir, + __wasi_fd_t fd +) { + // Grow the preopened resource table if needed. + rwlock_wrlock(&pt->lock); + if (!fd_prestats_grow(pt, fd, 1)) { + rwlock_unlock(&pt->lock); + return false; + } + + pt->prestats[fd].dir = strdup(dir); + rwlock_unlock(&pt->lock); + return true; +} + +// Looks up a preopened resource table entry by number. +static __wasi_errno_t fd_prestats_get_entry( + struct fd_prestats *pt, + __wasi_fd_t fd, + struct fd_prestat **ret +) REQUIRES_SHARED(pt->lock) { + // Test for file descriptor existence. + if (fd >= pt->size) + return __WASI_EBADF; + struct fd_prestat *prestat = &pt->prestats[fd]; + if (prestat->dir == NULL) + return __WASI_EBADF; + + *ret = prestat; + return 0; +} + +struct fd_object { + struct refcount refcount; + __wasi_filetype_t type; + int number; + + union { + // Data associated with directory file descriptors. + struct { + struct mutex lock; // Lock to protect members below. + DIR *handle; // Directory handle. + __wasi_dircookie_t offset; // Offset of the directory. + } directory; + }; +}; + +struct fd_entry { + struct fd_object *object; + __wasi_rights_t rights_base; + __wasi_rights_t rights_inheriting; +}; + +void fd_table_init( + struct fd_table *ft +) { + rwlock_init(&ft->lock); + ft->entries = NULL; + ft->size = 0; + ft->used = 0; +#if defined(WASMTIME_SSP_STATIC_CURFDS) + curfds = ft; +#endif +} + +// Looks up a file descriptor table entry by number and required rights. +static __wasi_errno_t fd_table_get_entry( + struct fd_table *ft, + __wasi_fd_t fd, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + struct fd_entry **ret +) REQUIRES_SHARED(ft->lock) { + // Test for file descriptor existence. + if (fd >= ft->size) + return __WASI_EBADF; + struct fd_entry *fe = &ft->entries[fd]; + if (fe->object == NULL) + return __WASI_EBADF; + + // Validate rights. + if ((~fe->rights_base & rights_base) != 0 || + (~fe->rights_inheriting & rights_inheriting) != 0) + return __WASI_ENOTCAPABLE; + *ret = fe; + return 0; +} + +// Grows the file descriptor table to a required lower bound and a +// minimum number of free file descriptor table entries. +static bool fd_table_grow( + struct fd_table *ft, + size_t min, + size_t incr +) REQUIRES_EXCLUSIVE(ft->lock) { + if (ft->size <= min || ft->size < (ft->used + incr) * 2) { + // Keep on doubling the table size until we've met our constraints. + size_t size = ft->size == 0 ? 1 : ft->size; + while (size <= min || size < (ft->used + incr) * 2) + size *= 2; + + // Grow the file descriptor table's allocation. + struct fd_entry *entries = realloc(ft->entries, sizeof(*entries) * size); + if (entries == NULL) + return false; + + // Mark all new file descriptors as unused. + for (size_t i = ft->size; i < size; ++i) + entries[i].object = NULL; + ft->entries = entries; + ft->size = size; + } + return true; +} + +// Allocates a new file descriptor object. +static __wasi_errno_t fd_object_new( + __wasi_filetype_t type, + struct fd_object **fo +) TRYLOCKS_SHARED(0, (*fo)->refcount) { + *fo = malloc(sizeof(**fo)); + if (*fo == NULL) + return __WASI_ENOMEM; + refcount_init(&(*fo)->refcount, 1); + (*fo)->type = type; + (*fo)->number = -1; + return 0; +} + +// Attaches a file descriptor to the file descriptor table. +static void fd_table_attach( + struct fd_table *ft, + __wasi_fd_t fd, + struct fd_object *fo, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting +) REQUIRES_EXCLUSIVE(ft->lock) CONSUMES(fo->refcount) { + assert(ft->size > fd && "File descriptor table too small"); + struct fd_entry *fe = &ft->entries[fd]; + assert(fe->object == NULL && "Attempted to overwrite an existing descriptor"); + fe->object = fo; + fe->rights_base = rights_base; + fe->rights_inheriting = rights_inheriting; + ++ft->used; + assert(ft->size >= ft->used * 2 && "File descriptor too full"); +} + +// Detaches a file descriptor from the file descriptor table. +static void fd_table_detach( + struct fd_table *ft, + __wasi_fd_t fd, + struct fd_object **fo +) REQUIRES_EXCLUSIVE(ft->lock) PRODUCES((*fo)->refcount) { + assert(ft->size > fd && "File descriptor table too small"); + struct fd_entry *fe = &ft->entries[fd]; + *fo = fe->object; + assert(*fo != NULL && "Attempted to detach nonexistent descriptor"); + fe->object = NULL; + assert(ft->used > 0 && "Reference count mismatch"); + --ft->used; +} + +// Determines the type of a file descriptor and its maximum set of +// rights that should be attached to it. +static __wasi_errno_t fd_determine_type_rights( + int fd, + __wasi_filetype_t *type, + __wasi_rights_t *rights_base, + __wasi_rights_t *rights_inheriting +) { + struct stat sb; + if (fstat(fd, &sb) < 0) + return convert_errno(errno); + if (S_ISBLK(sb.st_mode)) { + *type = __WASI_FILETYPE_BLOCK_DEVICE; + *rights_base = RIGHTS_BLOCK_DEVICE_BASE; + *rights_inheriting = RIGHTS_BLOCK_DEVICE_INHERITING; + } else if (S_ISCHR(sb.st_mode)) { + *type = __WASI_FILETYPE_CHARACTER_DEVICE; +#if CONFIG_HAS_ISATTY + if (isatty(fd)) { + *rights_base = RIGHTS_TTY_BASE; + *rights_inheriting = RIGHTS_TTY_INHERITING; + } else +#endif + { + *rights_base = RIGHTS_CHARACTER_DEVICE_BASE; + *rights_inheriting = RIGHTS_CHARACTER_DEVICE_INHERITING; + } + } else if (S_ISDIR(sb.st_mode)) { + *type = __WASI_FILETYPE_DIRECTORY; + *rights_base = RIGHTS_DIRECTORY_BASE; + *rights_inheriting = RIGHTS_DIRECTORY_INHERITING; + } else if (S_ISREG(sb.st_mode)) { + *type = __WASI_FILETYPE_REGULAR_FILE; + *rights_base = RIGHTS_REGULAR_FILE_BASE; + *rights_inheriting = RIGHTS_REGULAR_FILE_INHERITING; + } else if (S_ISSOCK(sb.st_mode)) { + int socktype; + socklen_t socktypelen = sizeof(socktype); + if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &socktype, &socktypelen) < 0) + return convert_errno(errno); + switch (socktype) { + case SOCK_DGRAM: + *type = __WASI_FILETYPE_SOCKET_DGRAM; + break; + case SOCK_STREAM: + *type = __WASI_FILETYPE_SOCKET_STREAM; + break; + default: + return __WASI_EINVAL; + } + *rights_base = RIGHTS_SOCKET_BASE; + *rights_inheriting = RIGHTS_SOCKET_INHERITING; + } else if (S_ISFIFO(sb.st_mode)) { + *type = __WASI_FILETYPE_SOCKET_STREAM; + *rights_base = RIGHTS_SOCKET_BASE; + *rights_inheriting = RIGHTS_SOCKET_INHERITING; + } else { + return __WASI_EINVAL; + } + + // Strip off read/write bits based on the access mode. + switch (fcntl(fd, F_GETFL) & O_ACCMODE) { + case O_RDONLY: + *rights_base &= ~__WASI_RIGHT_FD_WRITE; + break; + case O_WRONLY: + *rights_base &= ~__WASI_RIGHT_FD_READ; + break; + } + return 0; +} + +// Returns the underlying file descriptor number of a file descriptor +// object. This function can only be applied to objects that have an +// underlying file descriptor number. +static int fd_number( + const struct fd_object *fo +) { + int number = fo->number; + assert(number >= 0 && "fd_number() called on virtual file descriptor"); + return number; +} + +// Lowers the reference count on a file descriptor object. When the +// reference count reaches zero, its resources are cleaned up. +static void fd_object_release( + struct fd_object *fo +) UNLOCKS(fo->refcount) { + if (refcount_release(&fo->refcount)) { + switch (fo->type) { + case __WASI_FILETYPE_DIRECTORY: + // For directories we may keep track of a DIR object. Calling + // closedir() on it also closes the underlying file descriptor. + mutex_destroy(&fo->directory.lock); + if (fo->directory.handle == NULL) { + close(fd_number(fo)); + } else { + closedir(fo->directory.handle); + } + break; + default: + close(fd_number(fo)); + break; + } + free(fo); + } +} + +// Inserts an already existing file descriptor into the file descriptor +// table. +bool fd_table_insert_existing( + struct fd_table *ft, + __wasi_fd_t in, + int out +) { + __wasi_filetype_t type; + __wasi_rights_t rights_base, rights_inheriting; + if (fd_determine_type_rights(out, &type, &rights_base, &rights_inheriting) != + 0) + return false; + + struct fd_object *fo; + __wasi_errno_t error = fd_object_new(type, &fo); + if (error != 0) + return false; + fo->number = out; + if (type == __WASI_FILETYPE_DIRECTORY) { + mutex_init(&fo->directory.lock); + fo->directory.handle = NULL; + } + + // Grow the file descriptor table if needed. + rwlock_wrlock(&ft->lock); + if (!fd_table_grow(ft, in, 1)) { + rwlock_unlock(&ft->lock); + fd_object_release(fo); + return false; + } + + fd_table_attach(ft, in, fo, rights_base, rights_inheriting); + rwlock_unlock(&ft->lock); + return true; +} + +// Picks an unused slot from the file descriptor table. +static __wasi_fd_t fd_table_unused( + struct fd_table *ft +) REQUIRES_SHARED(ft->lock) { + assert(ft->size > ft->used && "File descriptor table has no free slots"); + for (;;) { + __wasi_fd_t fd = random_uniform(ft->size); + if (ft->entries[fd].object == NULL) + return fd; + } +} + +// Inserts a file descriptor object into an unused slot of the file +// descriptor table. +static __wasi_errno_t fd_table_insert( + struct fd_table *ft, + struct fd_object *fo, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + __wasi_fd_t *out +) REQUIRES_UNLOCKED(ft->lock) UNLOCKS(fo->refcount) { + // Grow the file descriptor table if needed. + rwlock_wrlock(&ft->lock); + if (!fd_table_grow(ft, 0, 1)) { + rwlock_unlock(&ft->lock); + fd_object_release(fo); + return convert_errno(errno); + } + + *out = fd_table_unused(ft); + fd_table_attach(ft, *out, fo, rights_base, rights_inheriting); + rwlock_unlock(&ft->lock); + return 0; +} + +// Inserts a numerical file descriptor into the file descriptor table. +static __wasi_errno_t fd_table_insert_fd( + struct fd_table *ft, + int in, + __wasi_filetype_t type, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + __wasi_fd_t *out +) REQUIRES_UNLOCKED(ft->lock) { + struct fd_object *fo; + __wasi_errno_t error = fd_object_new(type, &fo); + if (error != 0) { + close(in); + return error; + } + fo->number = in; + if (type == __WASI_FILETYPE_DIRECTORY) { + mutex_init(&fo->directory.lock); + fo->directory.handle = NULL; + } + return fd_table_insert(ft, fo, rights_base, rights_inheriting, out); +} + +__wasi_errno_t wasmtime_ssp_fd_prestat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_prestats *prestats, +#endif + __wasi_fd_t fd, + __wasi_prestat_t *buf +) { + rwlock_rdlock(&prestats->lock); + struct fd_prestat *prestat; + __wasi_errno_t error = fd_prestats_get_entry(prestats, fd, &prestat); + if (error != 0) { + rwlock_unlock(&prestats->lock); + return error; + } + + *buf = (__wasi_prestat_t) { + .pr_type = __WASI_PREOPENTYPE_DIR, + }; + + buf->u.dir.pr_name_len = strlen(prestat->dir); + + rwlock_unlock(&prestats->lock); + + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_prestat_dir_name( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_prestats *prestats, +#endif + __wasi_fd_t fd, + char *path, + size_t path_len +) { + rwlock_rdlock(&prestats->lock); + struct fd_prestat *prestat; + __wasi_errno_t error = fd_prestats_get_entry(prestats, fd, &prestat); + if (error != 0) { + rwlock_unlock(&prestats->lock); + return error; + } + if (path_len != strlen(prestat->dir)) { + rwlock_unlock(&prestats->lock); + return EINVAL; + } + + memcpy(path, prestat->dir, path_len); + + rwlock_unlock(&prestats->lock); + + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_close( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, + struct fd_prestats *prestats, +#endif + __wasi_fd_t fd +) { + // Don't allow closing a pre-opened resource. + // TODO: Eventually, we do want to permit this, once libpreopen in + // userspace is capable of removing entries from its tables as well. + { + rwlock_rdlock(&prestats->lock); + struct fd_prestat *prestat; + __wasi_errno_t error = fd_prestats_get_entry(prestats, fd, &prestat); + rwlock_unlock(&prestats->lock); + if (error == 0) { + return __WASI_ENOTSUP; + } + } + + // Validate the file descriptor. + struct fd_table *ft = curfds; + rwlock_wrlock(&ft->lock); + struct fd_entry *fe; + __wasi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe); + if (error != 0) { + rwlock_unlock(&ft->lock); + return error; + } + + // Remove it from the file descriptor table. + struct fd_object *fo; + fd_table_detach(ft, fd, &fo); + rwlock_unlock(&ft->lock); + fd_object_release(fo); + return 0; +} + +// Look up a file descriptor object in a locked file descriptor table +// and increases its reference count. +static __wasi_errno_t fd_object_get_locked( + struct fd_object **fo, + struct fd_table *ft, + __wasi_fd_t fd, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting +) TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) REQUIRES_EXCLUSIVE(ft->lock) { + // Test whether the file descriptor number is valid. + struct fd_entry *fe; + __wasi_errno_t error = + fd_table_get_entry(ft, fd, rights_base, rights_inheriting, &fe); + if (error != 0) + return error; + + // Increase the reference count on the file descriptor object. A copy + // of the rights are also stored, so callers can still access those if + // needed. + *fo = fe->object; + refcount_acquire(&(*fo)->refcount); + return 0; +} + +// Temporarily locks the file descriptor table to look up a file +// descriptor object, increases its reference count and drops the lock. +static __wasi_errno_t fd_object_get( + struct fd_table *curfds, + struct fd_object **fo, + __wasi_fd_t fd, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting +) TRYLOCKS_EXCLUSIVE(0, (*fo)->refcount) { + struct fd_table *ft = curfds; + rwlock_rdlock(&ft->lock); + __wasi_errno_t error = + fd_object_get_locked(fo, ft, fd, rights_base, rights_inheriting); + rwlock_unlock(&ft->lock); + return error; +} + +__wasi_errno_t wasmtime_ssp_fd_datasync( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd +) { + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_DATASYNC, 0); + if (error != 0) + return error; + +#if CONFIG_HAS_FDATASYNC + int ret = fdatasync(fd_number(fo)); +#else + int ret = fsync(fd_number(fo)); +#endif + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_pread( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_iovec_t *iov, + size_t iovcnt, + __wasi_filesize_t offset, + size_t *nread +) { + if (iovcnt == 0) + return __WASI_EINVAL; + + struct fd_object *fo; + __wasi_errno_t error = fd_object_get(curfds, + &fo, fd, __WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_SEEK, 0); + if (error != 0) + return error; + +#if CONFIG_HAS_PREADV + ssize_t len = + preadv(fd_number(fo), (const struct iovec *)iov, iovcnt, offset); + fd_object_release(fo); + if (len < 0) + return convert_errno(errno); + *nread = len; + return 0; +#else + if (iovcnt == 1) { + ssize_t len = pread(fd_number(fo), iov->buf, iov->buf_len, offset); + fd_object_release(fo); + if (len < 0) + return convert_errno(errno); + *nread = len; + return 0; + } else { + // Allocate a single buffer to fit all data. + size_t totalsize = 0; + for (size_t i = 0; i < iovcnt; ++i) + totalsize += iov[i].buf_len; + char *buf = malloc(totalsize); + if (buf == NULL) { + fd_object_release(fo); + return __WASI_ENOMEM; + } + + // Perform a single read operation. + ssize_t len = pread(fd_number(fo), buf, totalsize, offset); + fd_object_release(fo); + if (len < 0) { + free(buf); + return convert_errno(errno); + } + + // Copy data back to vectors. + size_t bufoff = 0; + for (size_t i = 0; i < iovcnt; ++i) { + if (bufoff + iov[i].buf_len < len) { + memcpy(iov[i].buf, buf + bufoff, iov[i].buf_len); + bufoff += iov[i].buf_len; + } else { + memcpy(iov[i].buf, buf + bufoff, len - bufoff); + break; + } + } + free(buf); + *nread = len; + return 0; + } +#endif +} + +__wasi_errno_t wasmtime_ssp_fd_pwrite( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_ciovec_t *iov, + size_t iovcnt, + __wasi_filesize_t offset, + size_t *nwritten +) { + if (iovcnt == 0) + return __WASI_EINVAL; + + struct fd_object *fo; + __wasi_errno_t error = fd_object_get(curfds, + &fo, fd, __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_SEEK, 0); + if (error != 0) + return error; + + ssize_t len; +#if CONFIG_HAS_PWRITEV + len = pwritev(fd_number(fo), (const struct iovec *)iov, iovcnt, offset); +#else + if (iovcnt == 1) { + len = pwrite(fd_number(fo), iov->buf, iov->buf_len, offset); + } else { + // Allocate a single buffer to fit all data. + size_t totalsize = 0; + for (size_t i = 0; i < iovcnt; ++i) + totalsize += iov[i].buf_len; + char *buf = malloc(totalsize); + if (buf == NULL) { + fd_object_release(fo); + return __WASI_ENOMEM; + } + size_t bufoff = 0; + for (size_t i = 0; i < iovcnt; ++i) { + memcpy(buf + bufoff, iov[i].buf, iov[i].buf_len); + bufoff += iov[i].buf_len; + } + + // Perform a single write operation. + len = pwrite(fd_number(fo), buf, totalsize, offset); + free(buf); + } +#endif + fd_object_release(fo); + if (len < 0) + return convert_errno(errno); + *nwritten = len; + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_read( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_iovec_t *iov, + size_t iovcnt, + size_t *nread +) { + struct fd_object *fo; + __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_READ, 0); + if (error != 0) + return error; + + ssize_t len = readv(fd_number(fo), (const struct iovec *)iov, iovcnt); + fd_object_release(fo); + if (len < 0) + return convert_errno(errno); + *nread = len; + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_renumber( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, + struct fd_prestats *prestats, +#endif + __wasi_fd_t from, + __wasi_fd_t to +) { + // Don't allow renumbering over a pre-opened resource. + // TODO: Eventually, we do want to permit this, once libpreopen in + // userspace is capable of removing entries from its tables as well. + { + rwlock_rdlock(&prestats->lock); + struct fd_prestat *prestat; + __wasi_errno_t error = fd_prestats_get_entry(prestats, to, &prestat); + if (error != 0) { + error = fd_prestats_get_entry(prestats, from, &prestat); + } + rwlock_unlock(&prestats->lock); + if (error == 0) { + return __WASI_ENOTSUP; + } + } + + struct fd_table *ft = curfds; + rwlock_wrlock(&ft->lock); + struct fd_entry *fe_from; + __wasi_errno_t error = fd_table_get_entry(ft, from, 0, 0, &fe_from); + if (error != 0) { + rwlock_unlock(&ft->lock); + return error; + } + struct fd_entry *fe_to; + error = fd_table_get_entry(ft, to, 0, 0, &fe_to); + if (error != 0) { + rwlock_unlock(&ft->lock); + return error; + } + + struct fd_object *fo; + fd_table_detach(ft, to, &fo); + refcount_acquire(&fe_from->object->refcount); + fd_table_attach(ft, to, fe_from->object, fe_from->rights_base, + fe_from->rights_inheriting); + rwlock_unlock(&ft->lock); + fd_object_release(fo); + + // Remove the old fd from the file descriptor table. + fd_table_detach(ft, from, &fo); + fd_object_release(fo); + --ft->used; + + rwlock_unlock(&ft->lock); + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_seek( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filedelta_t offset, + __wasi_whence_t whence, + __wasi_filesize_t *newoffset +) { + int nwhence; + switch (whence) { + case __WASI_WHENCE_CUR: + nwhence = SEEK_CUR; + break; + case __WASI_WHENCE_END: + nwhence = SEEK_END; + break; + case __WASI_WHENCE_SET: + nwhence = SEEK_SET; + break; + default: + return __WASI_EINVAL; + } + + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, + offset == 0 && whence == __WASI_WHENCE_CUR + ? __WASI_RIGHT_FD_TELL + : __WASI_RIGHT_FD_SEEK | __WASI_RIGHT_FD_TELL, + 0); + if (error != 0) + return error; + + off_t ret = lseek(fd_number(fo), offset, nwhence); + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + *newoffset = ret; + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_tell( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t *newoffset +) { + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_TELL, 0); + if (error != 0) + return error; + + off_t ret = lseek(fd_number(fo), 0, SEEK_CUR); + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + *newoffset = ret; + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_fdstat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_fdstat_t *buf +) { + struct fd_table *ft = curfds; + rwlock_rdlock(&ft->lock); + struct fd_entry *fe; + __wasi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe); + if (error != 0) { + rwlock_unlock(&ft->lock); + return error; + } + + // Extract file descriptor type and rights. + struct fd_object *fo = fe->object; + *buf = (__wasi_fdstat_t){ + .fs_filetype = fo->type, + .fs_rights_base = fe->rights_base, + .fs_rights_inheriting = fe->rights_inheriting, + }; + + // Fetch file descriptor flags. + int ret; + switch (fo->type) { + default: + ret = fcntl(fd_number(fo), F_GETFL); + break; + } + rwlock_unlock(&ft->lock); + if (ret < 0) + return convert_errno(errno); + + if ((ret & O_APPEND) != 0) + buf->fs_flags |= __WASI_FDFLAG_APPEND; +#ifdef O_DSYNC + if ((ret & O_DSYNC) != 0) + buf->fs_flags |= __WASI_FDFLAG_DSYNC; +#endif + if ((ret & O_NONBLOCK) != 0) + buf->fs_flags |= __WASI_FDFLAG_NONBLOCK; +#ifdef O_RSYNC + if ((ret & O_RSYNC) != 0) + buf->fs_flags |= __WASI_FDFLAG_RSYNC; +#endif + if ((ret & O_SYNC) != 0) + buf->fs_flags |= __WASI_FDFLAG_SYNC; + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_fdstat_set_flags( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_fdflags_t fs_flags +) { + int noflags = 0; + if ((fs_flags & __WASI_FDFLAG_APPEND) != 0) + noflags |= O_APPEND; + if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) +#ifdef O_DSYNC + noflags |= O_DSYNC; +#else + noflags |= O_SYNC; +#endif + if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0) + noflags |= O_NONBLOCK; + if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) +#ifdef O_RSYNC + noflags |= O_RSYNC; +#else + noflags |= O_SYNC; +#endif + if ((fs_flags & __WASI_FDFLAG_SYNC) != 0) + noflags |= O_SYNC; + + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FDSTAT_SET_FLAGS, 0); + if (error != 0) + return error; + + int ret = fcntl(fd_number(fo), F_SETFL, noflags); + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_fdstat_set_rights( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_rights_t fs_rights_base, + __wasi_rights_t fs_rights_inheriting +) { + struct fd_table *ft = curfds; + rwlock_wrlock(&ft->lock); + struct fd_entry *fe; + __wasi_errno_t error = + fd_table_get_entry(ft, fd, fs_rights_base, fs_rights_inheriting, &fe); + if (error != 0) { + rwlock_unlock(&ft->lock); + return error; + } + + // Restrict the rights on the file descriptor. + fe->rights_base = fs_rights_base; + fe->rights_inheriting = fs_rights_inheriting; + rwlock_unlock(&ft->lock); + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_sync( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd +) { + struct fd_object *fo; + __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_SYNC, 0); + if (error != 0) + return error; + + int ret = fsync(fd_number(fo)); + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_write( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const __wasi_ciovec_t *iov, + size_t iovcnt, + size_t *nwritten +) { + struct fd_object *fo; + __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_WRITE, 0); + if (error != 0) + return error; + + ssize_t len = writev(fd_number(fo), (const struct iovec *)iov, iovcnt); + fd_object_release(fo); + if (len < 0) + return convert_errno(errno); + *nwritten = len; + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_advise( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t offset, + __wasi_filesize_t len, + __wasi_advice_t advice +) { +#ifdef POSIX_FADV_NORMAL + int nadvice; + switch (advice) { + case __WASI_ADVICE_DONTNEED: + nadvice = POSIX_FADV_DONTNEED; + break; + case __WASI_ADVICE_NOREUSE: + nadvice = POSIX_FADV_NOREUSE; + break; + case __WASI_ADVICE_NORMAL: + nadvice = POSIX_FADV_NORMAL; + break; + case __WASI_ADVICE_RANDOM: + nadvice = POSIX_FADV_RANDOM; + break; + case __WASI_ADVICE_SEQUENTIAL: + nadvice = POSIX_FADV_SEQUENTIAL; + break; + case __WASI_ADVICE_WILLNEED: + nadvice = POSIX_FADV_WILLNEED; + break; + default: + return __WASI_EINVAL; + } + + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_ADVISE, 0); + if (error != 0) + return error; + + int ret = posix_fadvise(fd_number(fo), offset, len, nadvice); + fd_object_release(fo); + if (ret != 0) + return convert_errno(ret); + return 0; +#else + // Advisory information can safely be ignored if unsupported. + switch (advice) { + case __WASI_ADVICE_DONTNEED: + case __WASI_ADVICE_NOREUSE: + case __WASI_ADVICE_NORMAL: + case __WASI_ADVICE_RANDOM: + case __WASI_ADVICE_SEQUENTIAL: + case __WASI_ADVICE_WILLNEED: + break; + default: + return __WASI_EINVAL; + } + + // At least check for file descriptor existence. + struct fd_table *ft = curfds; + rwlock_rdlock(&ft->lock); + struct fd_entry *fe; + __wasi_errno_t error = + fd_table_get_entry(ft, fd, __WASI_RIGHT_FD_ADVISE, 0, &fe); + rwlock_unlock(&ft->lock); + return error; +#endif +} + +__wasi_errno_t wasmtime_ssp_fd_allocate( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t offset, + __wasi_filesize_t len +) { + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_ALLOCATE, 0); + if (error != 0) + return error; + +#if CONFIG_HAS_POSIX_FALLOCATE + int ret = posix_fallocate(fd_number(fo), offset, len); +#else + // At least ensure that the file is grown to the right size. + // TODO(ed): See if this can somehow be implemented without any race + // conditions. We may end up shrinking the file right now. + struct stat sb; + int ret = fstat(fd_number(fo), &sb); + if (ret == 0 && sb.st_size < offset + len) + ret = ftruncate(fd_number(fo), offset + len); +#endif + + fd_object_release(fo); + if (ret != 0) + return convert_errno(ret); + return 0; +} + +// Reads the entire contents of a symbolic link, returning the contents +// in an allocated buffer. The allocated buffer is large enough to fit +// at least one extra byte, so the caller may append a trailing slash to +// it. This is needed by path_get(). +static char *readlinkat_dup( + int fd, + const char *path +) { + char *buf = NULL; + size_t len = 32; + for (;;) { + char *newbuf = realloc(buf, len); + if (newbuf == NULL) { + free(buf); + return NULL; + } + buf = newbuf; + ssize_t ret = readlinkat(fd, path, buf, len); + if (ret < 0) { + free(buf); + return NULL; + } + if ((size_t)ret + 1 < len) { + buf[ret] = '\0'; + return buf; + } + len *= 2; + } +} + +// Lease to a directory, so a path underneath it can be accessed. +// +// This structure is used by system calls that operate on pathnames. In +// this environment, pathnames always consist of a pair of a file +// descriptor representing the directory where the lookup needs to start +// and the actual pathname string. +struct path_access { + int fd; // Directory file descriptor. + const char *path; // Pathname. + bool follow; // Whether symbolic links should be followed. + char *path_start; // Internal: pathname to free. + struct fd_object *fd_object; // Internal: directory file descriptor object. +}; + +// Creates a lease to a file descriptor and pathname pair. If the +// operating system does not implement Capsicum, it also normalizes the +// pathname to ensure the target path is placed underneath the +// directory. +static __wasi_errno_t path_get( + struct fd_table *curfds, + struct path_access *pa, + __wasi_fd_t fd, + __wasi_lookupflags_t flags, + const char *upath, + size_t upathlen, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + bool needs_final_component +) TRYLOCKS_EXCLUSIVE(0, pa->fd_object->refcount) { + char *path = str_nullterminate(upath, upathlen); + if (path == NULL) + return convert_errno(errno); + + // Fetch the directory file descriptor. + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, rights_base, rights_inheriting); + if (error != 0) { + free(path); + return error; + } + +#if CONFIG_HAS_CAP_ENTER + // Rely on the kernel to constrain access to automatically constrain + // access to files stored underneath this directory. + pa->fd = fd_number(fo); + pa->path = pa->path_start = path; + pa->follow = (flags & __WASI_LOOKUP_SYMLINK_FOLLOW) != 0; + pa->fd_object = fo; + return 0; +#else + // The implementation provides no mechanism to constrain lookups to a + // directory automatically. Emulate this logic by resolving the + // pathname manually. + + // Stack of directory file descriptors. Index 0 always corresponds + // with the directory provided to this function. Entering a directory + // causes a file descriptor to be pushed, while handling ".." entries + // causes an entry to be popped. Index 0 cannot be popped, as this + // would imply escaping the base directory. + int fds[128]; + fds[0] = fd_number(fo); + size_t curfd = 0; + + // Stack of pathname strings used for symlink expansion. By using a + // stack, there is no need to concatenate any pathname strings while + // expanding symlinks. + char *paths[32]; + char *paths_start[32]; + paths[0] = paths_start[0] = path; + size_t curpath = 0; + size_t expansions = 0; + + char *symlink; + for (;;) { + // Extract the next pathname component from 'paths[curpath]', null + // terminate it and store it in 'file'. 'ends_with_slashes' stores + // whether the pathname component is followed by one or more + // trailing slashes, as this requires it to be a directory. + char *file = paths[curpath]; + char *file_end = file + strcspn(file, "/"); + paths[curpath] = file_end + strspn(file_end, "/"); + bool ends_with_slashes = *file_end == '/'; + *file_end = '\0'; + + // Test for empty pathname strings and absolute paths. + if (file == file_end) { + error = ends_with_slashes ? __WASI_ENOTCAPABLE : __WASI_ENOENT; + goto fail; + } + + if (strcmp(file, ".") == 0) { + // Skip component. + } else if (strcmp(file, "..") == 0) { + // Pop a directory off the stack. + if (curfd == 0) { + // Attempted to go to parent directory of the directory file + // descriptor. + error = __WASI_ENOTCAPABLE; + goto fail; + } + close(fds[curfd--]); + } else if (curpath > 0 || *paths[curpath] != '\0' || + (ends_with_slashes && !needs_final_component)) { + // A pathname component whose name we're not interested in that is + // followed by a slash or is followed by other pathname + // components. In other words, a pathname component that must be a + // directory. First attempt to obtain a directory file descriptor + // for it. + int newdir = +#ifdef O_SEARCH + openat(fds[curfd], file, O_SEARCH | O_DIRECTORY | O_NOFOLLOW); +#else + openat(fds[curfd], file, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); +#endif + if (newdir != -1) { + // Success. Push it onto the directory stack. + if (curfd + 1 == sizeof(fds) / sizeof(fds[0])) { + close(newdir); + error = __WASI_ENAMETOOLONG; + goto fail; + } + fds[++curfd] = newdir; + } else { + // Failed to open it. Attempt symlink expansion. + if (errno != ELOOP && errno != EMLINK && errno != ENOTDIR) { + error = convert_errno(errno); + goto fail; + } + symlink = readlinkat_dup(fds[curfd], file); + if (symlink != NULL) + goto push_symlink; + + // readlink returns EINVAL if the path isn't a symlink. In that case, + // it's more informative to return ENOTDIR. + if (errno == EINVAL) + errno = ENOTDIR; + + error = convert_errno(errno); + goto fail; + } + } else { + // The final pathname component. Depending on whether it ends with + // a slash or the symlink-follow flag is set, perform symlink + // expansion. + if (ends_with_slashes || + (flags & __WASI_LOOKUP_SYMLINK_FOLLOW) != 0) { + symlink = readlinkat_dup(fds[curfd], file); + if (symlink != NULL) + goto push_symlink; + if (errno != EINVAL && errno != ENOENT) { + error = convert_errno(errno); + goto fail; + } + } + + // Not a symlink, meaning we're done. Return the filename, + // together with the directory containing this file. + // + // If the file was followed by a trailing slash, we must retain + // it, to ensure system calls properly return ENOTDIR. + // Unfortunately, this opens up a race condition, because this + // means that users of path_get() will perform symlink expansion a + // second time. There is nothing we can do to mitigate this, as + // far as I know. + if (ends_with_slashes) + *file_end = '/'; + pa->path = file; + pa->path_start = paths_start[0]; + goto success; + } + + if (*paths[curpath] == '\0') { + if (curpath == 0) { + // No further pathname components to process. We may end up here + // when called on paths like ".", "a/..", but also if the path + // had trailing slashes and the caller is not interested in the + // name of the pathname component. + free(paths_start[0]); + pa->path = "."; + pa->path_start = NULL; + goto success; + } + + // Finished expanding symlink. Continue processing along the + // original path. + free(paths_start[curpath--]); + } + continue; + + push_symlink: + // Prevent infinite loops by placing an upper limit on the number of + // symlink expansions. + if (++expansions == 128) { + free(symlink); + error = __WASI_ELOOP; + goto fail; + } + + if (*paths[curpath] == '\0') { + // The original path already finished processing. Replace it by + // this symlink entirely. + free(paths_start[curpath]); + } else if (curpath + 1 == sizeof(paths) / sizeof(paths[0])) { + // Too many nested symlinks. Stop processing. + free(symlink); + error = __WASI_ELOOP; + goto fail; + } else { + // The original path still has components left. Retain the + // components that remain, so we can process them afterwards. + ++curpath; + } + + // Append a trailing slash to the symlink if the path leading up to + // it also contained one. Otherwise we would not throw ENOTDIR if + // the target is not a directory. + if (ends_with_slashes) + strcat(symlink, "/"); + paths[curpath] = paths_start[curpath] = symlink; + } + +success: + // Return the lease. Close all directories, except the one the caller + // needs to use. + for (size_t i = 1; i < curfd; ++i) + close(fds[i]); + pa->fd = fds[curfd]; + pa->follow = false; + pa->fd_object = fo; + return 0; + +fail: + // Failure. Free all resources. + for (size_t i = 1; i <= curfd; ++i) + close(fds[i]); + for (size_t i = 0; i <= curpath; ++i) + free(paths_start[i]); + fd_object_release(fo); + return error; +#endif +} + +static __wasi_errno_t path_get_nofollow( + struct fd_table *curfds, + struct path_access *pa, + __wasi_fd_t fd, + const char *path, + size_t pathlen, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + bool needs_final_component +) TRYLOCKS_EXCLUSIVE(0, pa->fd_object->refcount) { + __wasi_lookupflags_t flags = 0; + return path_get(curfds, pa, fd, flags, path, pathlen, rights_base, rights_inheriting, + needs_final_component); +} + +static void path_put( + struct path_access *pa +) UNLOCKS(pa->fd_object->refcount) { + free(pa->path_start); + if (fd_number(pa->fd_object) != pa->fd) + close(pa->fd); + fd_object_release(pa->fd_object); +} + +__wasi_errno_t wasmtime_ssp_path_create_directory( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t pathlen +) { + struct path_access pa; + __wasi_errno_t error = + path_get_nofollow(curfds, &pa, fd, path, pathlen, + __WASI_RIGHT_PATH_CREATE_DIRECTORY, 0, true); + if (error != 0) + return error; + + int ret = mkdirat(pa.fd, pa.path, 0777); + path_put(&pa); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_link( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t old_fd, + __wasi_lookupflags_t old_flags, + const char *old_path, + size_t old_path_len, + __wasi_fd_t new_fd, + const char *new_path, + size_t new_path_len +) { + struct path_access old_pa; + __wasi_errno_t error = path_get(curfds, &old_pa, old_fd, old_flags, old_path, old_path_len, + __WASI_RIGHT_PATH_LINK_SOURCE, 0, false); + if (error != 0) + return error; + + struct path_access new_pa; + error = path_get_nofollow(curfds, &new_pa, new_fd, new_path, new_path_len, + __WASI_RIGHT_PATH_LINK_TARGET, 0, true); + if (error != 0) { + path_put(&old_pa); + return error; + } + + int ret = linkat(old_pa.fd, old_pa.path, new_pa.fd, new_pa.path, + old_pa.follow ? AT_SYMLINK_FOLLOW : 0); + if (ret < 0 && errno == ENOTSUP && !old_pa.follow) { + // OS X doesn't allow creating hardlinks to symbolic links. + // Duplicate the symbolic link instead. + char *target = readlinkat_dup(old_pa.fd, old_pa.path); + if (target != NULL) { + ret = symlinkat(target, new_pa.fd, new_pa.path); + free(target); + } + } + path_put(&old_pa); + path_put(&new_pa); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_open( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t dirfd, + __wasi_lookupflags_t dirflags, + const char *path, + size_t pathlen, + __wasi_oflags_t oflags, + __wasi_rights_t fs_rights_base, + __wasi_rights_t fs_rights_inheriting, + __wasi_fdflags_t fs_flags, + __wasi_fd_t *fd +) { + // Rights that should be installed on the new file descriptor. + __wasi_rights_t rights_base = fs_rights_base; + __wasi_rights_t rights_inheriting = fs_rights_inheriting; + + // Which open() mode should be used to satisfy the needed rights. + bool read = + (rights_base & (__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_READDIR)) != 0; + bool write = + (rights_base & (__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_WRITE | + __WASI_RIGHT_FD_ALLOCATE | + __WASI_RIGHT_FD_FILESTAT_SET_SIZE)) != 0; + int noflags = write ? read ? O_RDWR : O_WRONLY : O_RDONLY; + + // Which rights are needed on the directory file descriptor. + __wasi_rights_t needed_base = __WASI_RIGHT_PATH_OPEN; + __wasi_rights_t needed_inheriting = rights_base | rights_inheriting; + + // Convert open flags. + if ((oflags & __WASI_O_CREAT) != 0) { + noflags |= O_CREAT; + needed_base |= __WASI_RIGHT_PATH_CREATE_FILE; + } + if ((oflags & __WASI_O_DIRECTORY) != 0) + noflags |= O_DIRECTORY; + if ((oflags & __WASI_O_EXCL) != 0) + noflags |= O_EXCL; + if ((oflags & __WASI_O_TRUNC) != 0) { + noflags |= O_TRUNC; + needed_base |= __WASI_RIGHT_PATH_FILESTAT_SET_SIZE; + } + + // Convert file descriptor flags. + if ((fs_flags & __WASI_FDFLAG_APPEND) != 0) + noflags |= O_APPEND; + if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) { +#ifdef O_DSYNC + noflags |= O_DSYNC; +#else + noflags |= O_SYNC; +#endif + needed_inheriting |= __WASI_RIGHT_FD_DATASYNC; + } + if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0) + noflags |= O_NONBLOCK; + if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) { +#ifdef O_RSYNC + noflags |= O_RSYNC; +#else + noflags |= O_SYNC; +#endif + needed_inheriting |= __WASI_RIGHT_FD_SYNC; + } + if ((fs_flags & __WASI_FDFLAG_SYNC) != 0) { + noflags |= O_SYNC; + needed_inheriting |= __WASI_RIGHT_FD_SYNC; + } + if (write && (noflags & (O_APPEND | O_TRUNC)) == 0) + needed_inheriting |= __WASI_RIGHT_FD_SEEK; + + struct path_access pa; + __wasi_errno_t error = + path_get(curfds, &pa, dirfd, dirflags, path, pathlen, needed_base, needed_inheriting, + (oflags & __WASI_O_CREAT) != 0); + if (error != 0) + return error; + if (!pa.follow) + noflags |= O_NOFOLLOW; + + int nfd = openat(pa.fd, pa.path, noflags, 0666); + if (nfd < 0) { + int openat_errno = errno; + // Linux returns ENXIO instead of EOPNOTSUPP when opening a socket. + if (openat_errno == ENXIO) { + struct stat sb; + int ret = + fstatat(pa.fd, pa.path, &sb, pa.follow ? 0 : AT_SYMLINK_NOFOLLOW); + path_put(&pa); + return ret == 0 && S_ISSOCK(sb.st_mode) ? __WASI_ENOTSUP + : __WASI_ENXIO; + } + // Linux returns ENOTDIR instead of ELOOP when using O_NOFOLLOW|O_DIRECTORY + // on a symlink. + if (openat_errno == ENOTDIR && (noflags & (O_NOFOLLOW | O_DIRECTORY)) != 0) { + struct stat sb; + int ret = fstatat(pa.fd, pa.path, &sb, AT_SYMLINK_NOFOLLOW); + if (S_ISLNK(sb.st_mode)) { + path_put(&pa); + return __WASI_ELOOP; + } + } + path_put(&pa); + // FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on + // a symlink. + if (!pa.follow && openat_errno == EMLINK) + return __WASI_ELOOP; + return convert_errno(openat_errno); + } + path_put(&pa); + + // Determine the type of the new file descriptor and which rights + // contradict with this type. + __wasi_filetype_t type; + __wasi_rights_t max_base, max_inheriting; + error = fd_determine_type_rights(nfd, &type, &max_base, &max_inheriting); + if (error != 0) { + close(nfd); + return error; + } + return fd_table_insert_fd(curfds, nfd, type, rights_base & max_base, + rights_inheriting & max_inheriting, fd); +} + +// Copies out directory entry metadata or filename, potentially +// truncating it in the process. +static void fd_readdir_put( + void *buf, + size_t bufsize, + size_t *bufused, + const void *elem, + size_t elemsize +) { + size_t bufavail = bufsize - *bufused; + if (elemsize > bufavail) + elemsize = bufavail; + memcpy((char *)buf + *bufused, elem, elemsize); + *bufused += elemsize; +} + +__wasi_errno_t wasmtime_ssp_fd_readdir( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + void *buf, + size_t nbyte, + __wasi_dircookie_t cookie, + size_t *bufused +) { + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_READDIR, 0); + if (error != 0) { + return error; + } + + // Create a directory handle if none has been opened yet. + mutex_lock(&fo->directory.lock); + DIR *dp = fo->directory.handle; + if (dp == NULL) { + dp = fdopendir(fd_number(fo)); + if (dp == NULL) { + mutex_unlock(&fo->directory.lock); + fd_object_release(fo); + return convert_errno(errno); + } + fo->directory.handle = dp; + fo->directory.offset = __WASI_DIRCOOKIE_START; + } + + // Seek to the right position if the requested offset does not match + // the current offset. + if (fo->directory.offset != cookie) { + if (cookie == __WASI_DIRCOOKIE_START) + rewinddir(dp); + else + seekdir(dp, cookie); + fo->directory.offset = cookie; + } + + *bufused = 0; + while (nbyte > 0) { + // Read the next directory entry. + errno = 0; + struct dirent *de = readdir(dp); + if (de == NULL) { + mutex_unlock(&fo->directory.lock); + fd_object_release(fo); + return errno == 0 || *bufused > 0 ? 0 : convert_errno(errno); + } + fo->directory.offset = telldir(dp); + + // Craft a directory entry and copy that back. + size_t namlen = strlen(de->d_name); + __wasi_dirent_t cde = { + .d_next = fo->directory.offset, + .d_ino = de->d_ino, + .d_namlen = namlen, + }; + switch (de->d_type) { + case DT_BLK: + cde.d_type = __WASI_FILETYPE_BLOCK_DEVICE; + break; + case DT_CHR: + cde.d_type = __WASI_FILETYPE_CHARACTER_DEVICE; + break; + case DT_DIR: + cde.d_type = __WASI_FILETYPE_DIRECTORY; + break; + case DT_FIFO: + cde.d_type = __WASI_FILETYPE_SOCKET_STREAM; + break; + case DT_LNK: + cde.d_type = __WASI_FILETYPE_SYMBOLIC_LINK; + break; + case DT_REG: + cde.d_type = __WASI_FILETYPE_REGULAR_FILE; + break; +#ifdef DT_SOCK + case DT_SOCK: + // Technically not correct, but good enough. + cde.d_type = __WASI_FILETYPE_SOCKET_STREAM; + break; +#endif + default: + cde.d_type = __WASI_FILETYPE_UNKNOWN; + break; + } + fd_readdir_put(buf, nbyte, bufused, &cde, sizeof(cde)); + fd_readdir_put(buf, nbyte, bufused, de->d_name, namlen); + } + mutex_unlock(&fo->directory.lock); + fd_object_release(fo); + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_readlink( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t pathlen, + char *buf, + size_t bufsize, + size_t *bufused +) { + struct path_access pa; + __wasi_errno_t error = path_get_nofollow(curfds, + &pa, fd, path, pathlen, __WASI_RIGHT_PATH_READLINK, 0, false); + if (error != 0) + return error; + + // Linux requires that the buffer size is positive. whereas POSIX does + // not. Use a fake buffer to store the results if the size is zero. + char fakebuf[1]; + ssize_t len = readlinkat(pa.fd, pa.path, bufsize == 0 ? fakebuf : buf, + bufsize == 0 ? sizeof(fakebuf) : bufsize); + path_put(&pa); + if (len < 0) + return convert_errno(errno); + *bufused = (size_t)len < bufsize ? len : bufsize; + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_rename( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t old_fd, + const char *old_path, + size_t old_path_len, + __wasi_fd_t new_fd, + const char *new_path, + size_t new_path_len +) { + struct path_access old_pa; + __wasi_errno_t error = path_get_nofollow(curfds, &old_pa, old_fd, old_path, old_path_len, + __WASI_RIGHT_PATH_RENAME_SOURCE, 0, true); + if (error != 0) + return error; + + struct path_access new_pa; + error = path_get_nofollow(curfds, &new_pa, new_fd, new_path, new_path_len, + __WASI_RIGHT_PATH_RENAME_TARGET, 0, true); + if (error != 0) { + path_put(&old_pa); + return error; + } + + int ret = renameat(old_pa.fd, old_pa.path, new_pa.fd, new_pa.path); + path_put(&old_pa); + path_put(&new_pa); + if (ret < 0) { + return convert_errno(errno); + } + return 0; +} + +// Converts a POSIX stat structure to a CloudABI filestat structure. +static void convert_stat( + const struct stat *in, + __wasi_filestat_t *out +) { + *out = (__wasi_filestat_t){ + .st_dev = in->st_dev, + .st_ino = in->st_ino, + .st_nlink = in->st_nlink, + .st_size = in->st_size, + .st_atim = convert_timespec(&in->st_atim), + .st_mtim = convert_timespec(&in->st_mtim), + .st_ctim = convert_timespec(&in->st_ctim), + }; +} + +__wasi_errno_t wasmtime_ssp_fd_filestat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filestat_t *buf +) { + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FILESTAT_GET, 0); + if (error != 0) + return error; + + int ret; + switch (fo->type) { + default: { + struct stat sb; + ret = fstat(fd_number(fo), &sb); + convert_stat(&sb, buf); + break; + } + } + buf->st_filetype = fo->type; + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +static void convert_timestamp( + __wasi_timestamp_t in, + struct timespec *out +) { + // Store sub-second remainder. + out->tv_nsec = in % 1000000000; + in /= 1000000000; + + // Clamp to the maximum in case it would overflow our system's time_t. + out->tv_sec = in < NUMERIC_MAX(time_t) ? in : NUMERIC_MAX(time_t); +} + +// Converts the provided timestamps and flags to a set of arguments for +// futimens() and utimensat(). +static void convert_utimens_arguments( + __wasi_timestamp_t st_atim, + __wasi_timestamp_t st_mtim, + __wasi_fstflags_t fstflags, + struct timespec *ts +) { + if ((fstflags & __WASI_FILESTAT_SET_ATIM_NOW) != 0) { + ts[0].tv_nsec = UTIME_NOW; + } else if ((fstflags & __WASI_FILESTAT_SET_ATIM) != 0) { + convert_timestamp(st_atim, &ts[0]); + } else { + ts[0].tv_nsec = UTIME_OMIT; + } + + if ((fstflags & __WASI_FILESTAT_SET_MTIM_NOW) != 0) { + ts[1].tv_nsec = UTIME_NOW; + } else if ((fstflags & __WASI_FILESTAT_SET_MTIM) != 0) { + convert_timestamp(st_mtim, &ts[1]); + } else { + ts[1].tv_nsec = UTIME_OMIT; + } +} + +__wasi_errno_t wasmtime_ssp_fd_filestat_set_size( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_filesize_t st_size +) { + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FILESTAT_SET_SIZE, 0); + if (error != 0) + return error; + + int ret = ftruncate(fd_number(fo), st_size); + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_fd_filestat_set_times( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_timestamp_t st_atim, + __wasi_timestamp_t st_mtim, + __wasi_fstflags_t fstflags +) { + if ((fstflags & ~(__WASI_FILESTAT_SET_ATIM | __WASI_FILESTAT_SET_ATIM_NOW | + __WASI_FILESTAT_SET_MTIM | __WASI_FILESTAT_SET_MTIM_NOW)) != 0) + return __WASI_EINVAL; + + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, fd, __WASI_RIGHT_FD_FILESTAT_SET_TIMES, 0); + if (error != 0) + return error; + + struct timespec ts[2]; + convert_utimens_arguments(st_atim, st_mtim, fstflags, ts); + int ret = futimens(fd_number(fo), ts); + + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_filestat_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_lookupflags_t flags, + const char *path, + size_t pathlen, + __wasi_filestat_t *buf +) { + struct path_access pa; + __wasi_errno_t error = + path_get(curfds, &pa, fd, flags, path, pathlen, __WASI_RIGHT_PATH_FILESTAT_GET, 0, false); + if (error != 0) + return error; + + struct stat sb; + int ret = fstatat(pa.fd, pa.path, &sb, pa.follow ? 0 : AT_SYMLINK_NOFOLLOW); + path_put(&pa); + if (ret < 0) + return convert_errno(errno); + convert_stat(&sb, buf); + + // Convert the file type. In the case of sockets there is no way we + // can easily determine the exact socket type. + if (S_ISBLK(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_BLOCK_DEVICE; + else if (S_ISCHR(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_CHARACTER_DEVICE; + else if (S_ISDIR(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_DIRECTORY; + else if (S_ISFIFO(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_SOCKET_STREAM; + else if (S_ISLNK(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_SYMBOLIC_LINK; + else if (S_ISREG(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_REGULAR_FILE; + else if (S_ISSOCK(sb.st_mode)) + buf->st_filetype = __WASI_FILETYPE_SOCKET_STREAM; + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_filestat_set_times( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + __wasi_lookupflags_t flags, + const char *path, + size_t pathlen, + __wasi_timestamp_t st_atim, + __wasi_timestamp_t st_mtim, + __wasi_fstflags_t fstflags +) { + if ((fstflags & ~(__WASI_FILESTAT_SET_ATIM | __WASI_FILESTAT_SET_ATIM_NOW | + __WASI_FILESTAT_SET_MTIM | __WASI_FILESTAT_SET_MTIM_NOW)) != 0) + return __WASI_EINVAL; + + struct path_access pa; + __wasi_errno_t error = path_get(curfds, + &pa, fd, flags, path, pathlen, __WASI_RIGHT_PATH_FILESTAT_SET_TIMES, 0, false); + if (error != 0) + return error; + + struct timespec ts[2]; + convert_utimens_arguments(st_atim, st_mtim, fstflags, ts); + int ret = utimensat(pa.fd, pa.path, ts, pa.follow ? 0 : AT_SYMLINK_NOFOLLOW); + + path_put(&pa); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_symlink( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + const char *old_path, + size_t old_path_len, + __wasi_fd_t fd, + const char *new_path, + size_t new_path_len +) { + char *target = str_nullterminate(old_path, old_path_len); + if (target == NULL) + return convert_errno(errno); + + struct path_access pa; + __wasi_errno_t error = path_get_nofollow(curfds, + &pa, fd, new_path, new_path_len, __WASI_RIGHT_PATH_SYMLINK, 0, true); + if (error != 0) { + free(target); + return error; + } + + int ret = symlinkat(target, pa.fd, pa.path); + path_put(&pa); + free(target); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_unlink_file( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t pathlen +) { + struct path_access pa; + __wasi_errno_t error = path_get_nofollow(curfds, + &pa, fd, path, pathlen, __WASI_RIGHT_PATH_UNLINK_FILE, 0, true); + if (error != 0) + return error; + + int ret = unlinkat(pa.fd, pa.path, 0); +#ifndef __linux__ + // Non-Linux implementations may return EPERM when attempting to remove a + // directory without REMOVEDIR. While that's what POSIX specifies, it's + // less useful. Adjust this to EISDIR. It doesn't matter that this is not + // atomic with the unlinkat, because if the file is removed and a directory + // is created before fstatat sees it, we're racing with that change anyway + // and unlinkat could have legitimately seen the directory if the race had + // turned out differently. + if (ret < 0 && errno == EPERM) { + struct stat statbuf; + if (fstatat(pa.fd, pa.path, &statbuf, AT_SYMLINK_NOFOLLOW) == 0 && + S_ISDIR(statbuf.st_mode)) { + errno = EISDIR; + } + } +#endif + path_put(&pa); + if (ret < 0) { + return convert_errno(errno); + } + return 0; +} + +__wasi_errno_t wasmtime_ssp_path_remove_directory( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t fd, + const char *path, + size_t pathlen +) { + struct path_access pa; + __wasi_errno_t error = path_get_nofollow(curfds, + &pa, fd, path, pathlen, __WASI_RIGHT_PATH_REMOVE_DIRECTORY, 0, true); + if (error != 0) + return error; + + int ret = unlinkat(pa.fd, pa.path, AT_REMOVEDIR); +#ifndef __linux__ + // POSIX permits either EEXIST or ENOTEMPTY when the directory is not empty. + // Map it to ENOTEMPTY. + if (ret < 0 && errno == EEXIST) { + errno = ENOTEMPTY; + } +#endif + path_put(&pa); + if (ret < 0) { + return convert_errno(errno); + } + return 0; +} + +__wasi_errno_t wasmtime_ssp_poll_oneoff( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + const __wasi_subscription_t *in, + __wasi_event_t *out, + size_t nsubscriptions, + size_t *nevents +) NO_LOCK_ANALYSIS { + // Sleeping. + if (nsubscriptions == 1 && in[0].type == __WASI_EVENTTYPE_CLOCK) { + out[0] = (__wasi_event_t){ + .userdata = in[0].userdata, + .type = in[0].type, + }; +#if CONFIG_HAS_CLOCK_NANOSLEEP + clockid_t clock_id; + if (convert_clockid(in[0].u.clock.clock_id, &clock_id)) { + struct timespec ts; + convert_timestamp(in[0].u.clock.timeout, &ts); + int ret = clock_nanosleep( + clock_id, + (in[0].u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) != 0 + ? TIMER_ABSTIME + : 0, + &ts, NULL); + if (ret != 0) + out[0].error = convert_errno(ret); + } else { + out[0].error = __WASI_ENOTSUP; + } +#else + switch (in[0].u.clock.clock_id) { + case __WASI_CLOCK_MONOTONIC: + if ((in[0].u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) != 0) { + // TODO(ed): Implement. + fputs("Unimplemented absolute sleep on monotonic clock\n", stderr); + out[0].error = __WASI_ENOSYS; + } else { + // Perform relative sleeps on the monotonic clock also using + // nanosleep(). This is incorrect, but good enough for now. + struct timespec ts; + convert_timestamp(in[0].u.clock.timeout, &ts); + nanosleep(&ts, NULL); + } + break; + case __WASI_CLOCK_REALTIME: + if ((in[0].u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) != 0) { + // Sleeping to an absolute point in time can only be done + // by waiting on a condition variable. + struct mutex mutex; + mutex_init(&mutex); + struct cond cond; + cond_init_realtime(&cond); + mutex_lock(&mutex); + cond_timedwait(&cond, &mutex, in[0].u.clock.timeout, true); + mutex_unlock(&mutex); + mutex_destroy(&mutex); + cond_destroy(&cond); + } else { + // Relative sleeps can be done using nanosleep(). + struct timespec ts; + convert_timestamp(in[0].u.clock.timeout, &ts); + nanosleep(&ts, NULL); + } + break; + default: + out[0].error = __WASI_ENOTSUP; + break; + } +#endif + *nevents = 1; + return 0; + } + + // Last option: call into poll(). This can only be done in case all + // subscriptions consist of __WASI_EVENTTYPE_FD_READ and + // __WASI_EVENTTYPE_FD_WRITE entries. There may be up to one + // __WASI_EVENTTYPE_CLOCK entry to act as a timeout. These are also + // the subscriptions generate by cloudlibc's poll() and select(). + struct fd_object **fos = malloc(nsubscriptions * sizeof(*fos)); + if (fos == NULL) + return __WASI_ENOMEM; + struct pollfd *pfds = malloc(nsubscriptions * sizeof(*pfds)); + if (pfds == NULL) { + free(fos); + return __WASI_ENOMEM; + } + + // Convert subscriptions to pollfd entries. Increase the reference + // count on the file descriptors to ensure they remain valid across + // the call to poll(). + struct fd_table *ft = curfds; + rwlock_rdlock(&ft->lock); + *nevents = 0; + const __wasi_subscription_t *clock_subscription = NULL; + for (size_t i = 0; i < nsubscriptions; ++i) { + const __wasi_subscription_t *s = &in[i]; + switch (s->type) { + case __WASI_EVENTTYPE_FD_READ: + case __WASI_EVENTTYPE_FD_WRITE: { + __wasi_errno_t error = + fd_object_get_locked(&fos[i], ft, s->u.fd_readwrite.fd, + __WASI_RIGHT_POLL_FD_READWRITE, 0); + if (error == 0) { + // Proper file descriptor on which we can poll(). + pfds[i] = (struct pollfd){ + .fd = fd_number(fos[i]), + .events = s->type == __WASI_EVENTTYPE_FD_READ ? POLLRDNORM + : POLLWRNORM, + }; + } else { + // Invalid file descriptor or rights missing. + fos[i] = NULL; + pfds[i] = (struct pollfd){.fd = -1}; + out[(*nevents)++] = (__wasi_event_t){ + .userdata = s->userdata, + .error = error, + .type = s->type, + }; + } + break; + } + case __WASI_EVENTTYPE_CLOCK: + if (clock_subscription == NULL && + (s->u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) == 0) { + // Relative timeout. + fos[i] = NULL; + pfds[i] = (struct pollfd){.fd = -1}; + clock_subscription = s; + break; + } + // Fallthrough. + default: + // Unsupported event. + fos[i] = NULL; + pfds[i] = (struct pollfd){.fd = -1}; + out[(*nevents)++] = (__wasi_event_t){ + .userdata = s->userdata, + .error = __WASI_ENOSYS, + .type = s->type, + }; + break; + } + } + rwlock_unlock(&ft->lock); + + // Use a zero-second timeout in case we've already generated events in + // the loop above. + int timeout; + if (*nevents != 0) { + timeout = 0; + } else if (clock_subscription != NULL) { + __wasi_timestamp_t ts = clock_subscription->u.clock.timeout / 1000000; + timeout = ts > INT_MAX ? -1 : ts; + } else { + timeout = -1; + } + int ret = poll(pfds, nsubscriptions, timeout); + + __wasi_errno_t error = 0; + if (ret == -1) { + error = convert_errno(errno); + } else if (ret == 0 && *nevents == 0 && clock_subscription != NULL) { + // No events triggered. Trigger the clock event. + out[(*nevents)++] = (__wasi_event_t){ + .userdata = clock_subscription->userdata, + .type = __WASI_EVENTTYPE_CLOCK, + }; + } else { + // Events got triggered. Don't trigger the clock event. + for (size_t i = 0; i < nsubscriptions; ++i) { + if (pfds[i].fd >= 0) { + __wasi_filesize_t nbytes = 0; + if (in[i].type == __WASI_EVENTTYPE_FD_READ) { + int l; + if (ioctl(fd_number(fos[i]), FIONREAD, &l) == 0) + nbytes = l; + } + if ((pfds[i].revents & POLLNVAL) != 0) { + // Bad file descriptor. This normally cannot occur, as + // referencing the file descriptor object will always ensure + // the descriptor is valid. Still, macOS may sometimes return + // this on FIFOs when reaching end-of-file. + out[(*nevents)++] = (__wasi_event_t){ + .userdata = in[i].userdata, +#ifdef __APPLE__ + .u.fd_readwrite.nbytes = nbytes, + .u.fd_readwrite.flags = __WASI_EVENT_FD_READWRITE_HANGUP, +#else + .error = __WASI_EBADF, +#endif + .type = in[i].type, + }; + } else if ((pfds[i].revents & POLLERR) != 0) { + // File descriptor is in an error state. + out[(*nevents)++] = (__wasi_event_t){ + .userdata = in[i].userdata, + .error = __WASI_EIO, + .type = in[i].type, + }; + } else if ((pfds[i].revents & POLLHUP) != 0) { + // End-of-file. + out[(*nevents)++] = (__wasi_event_t){ + .userdata = in[i].userdata, + .type = in[i].type, + .u.fd_readwrite.nbytes = nbytes, + .u.fd_readwrite.flags = __WASI_EVENT_FD_READWRITE_HANGUP, + }; + } else if ((pfds[i].revents & (POLLRDNORM | POLLWRNORM)) != 0) { + // Read or write possible. + out[(*nevents)++] = (__wasi_event_t){ + .userdata = in[i].userdata, + .type = in[i].type, + .u.fd_readwrite.nbytes = nbytes, + }; + } + } + } + } + + for (size_t i = 0; i < nsubscriptions; ++i) + if (fos[i] != NULL) + fd_object_release(fos[i]); + free(fos); + free(pfds); + return error; +} + +void wasmtime_ssp_proc_exit( + __wasi_exitcode_t rval +) { + _Exit(rval); +} + +__wasi_errno_t wasmtime_ssp_proc_raise( + __wasi_signal_t sig +) { + static const int signals[] = { +#define X(v) [__WASI_##v] = v + X(SIGABRT), X(SIGALRM), X(SIGBUS), X(SIGCHLD), X(SIGCONT), X(SIGFPE), + X(SIGHUP), X(SIGILL), X(SIGINT), X(SIGKILL), X(SIGPIPE), X(SIGQUIT), + X(SIGSEGV), X(SIGSTOP), X(SIGSYS), X(SIGTERM), X(SIGTRAP), X(SIGTSTP), + X(SIGTTIN), X(SIGTTOU), X(SIGURG), X(SIGUSR1), X(SIGUSR2), X(SIGVTALRM), + X(SIGXCPU), X(SIGXFSZ), +#undef X + }; + if (sig >= sizeof(signals) / sizeof(signals[0]) || signals[sig] == 0) + return __WASI_EINVAL; + +#if CONFIG_TLS_USE_GSBASE + // TLS on OS X depends on installing a SIGSEGV handler. Reset SIGSEGV + // to the default action before raising. + if (sig == __WASI_SIGSEGV) { + struct sigaction sa = { + .sa_handler = SIG_DFL, + }; + sigemptyset(&sa.sa_mask); + sigaction(SIGSEGV, &sa, NULL); + } +#endif + + if (raise(signals[sig]) < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_random_get( + void *buf, + size_t nbyte +) { + random_buf(buf, nbyte); + return 0; +} + +__wasi_errno_t wasmtime_ssp_sock_recv( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t sock, + const __wasi_iovec_t *ri_data, + size_t ri_data_len, + __wasi_riflags_t ri_flags, + size_t *ro_datalen, + __wasi_roflags_t *ro_flags +) { + // Convert input to msghdr. + struct msghdr hdr = { + .msg_iov = (struct iovec *)ri_data, + .msg_iovlen = ri_data_len, + }; + int nflags = 0; + if ((ri_flags & __WASI_SOCK_RECV_PEEK) != 0) + nflags |= MSG_PEEK; + if ((ri_flags & __WASI_SOCK_RECV_WAITALL) != 0) + nflags |= MSG_WAITALL; + + struct fd_object *fo; + __wasi_errno_t error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_READ, 0); + if (error != 0) { + return error; + } + + ssize_t datalen = recvmsg(fd_number(fo), &hdr, nflags); + fd_object_release(fo); + if (datalen < 0) { + return convert_errno(errno); + } + + + // Convert msghdr to output. + *ro_datalen = datalen; + *ro_flags = 0; + if ((hdr.msg_flags & MSG_TRUNC) != 0) + *ro_flags |= __WASI_SOCK_RECV_DATA_TRUNCATED; + return 0; +} + +__wasi_errno_t wasmtime_ssp_sock_send( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t sock, + const __wasi_ciovec_t *si_data, + size_t si_data_len, + __wasi_siflags_t si_flags, + size_t *so_datalen +) NO_LOCK_ANALYSIS { + // Convert input to msghdr. + struct msghdr hdr = { + .msg_iov = (struct iovec *)si_data, + .msg_iovlen = si_data_len, + }; + + // Attach file descriptors if present. + __wasi_errno_t error; + + // Send message. + struct fd_object *fo; + error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_WRITE, 0); + if (error != 0) + goto out; + ssize_t len = sendmsg(fd_number(fo), &hdr, 0); + fd_object_release(fo); + if (len < 0) { + error = convert_errno(errno); + } else { + *so_datalen = len; + } + +out: + return error; +} + +__wasi_errno_t wasmtime_ssp_sock_shutdown( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct fd_table *curfds, +#endif + __wasi_fd_t sock, + __wasi_sdflags_t how +) { + int nhow; + switch (how) { + case __WASI_SHUT_RD: + nhow = SHUT_RD; + break; + case __WASI_SHUT_WR: + nhow = SHUT_WR; + break; + case __WASI_SHUT_RD | __WASI_SHUT_WR: + nhow = SHUT_RDWR; + break; + default: + return __WASI_EINVAL; + } + + struct fd_object *fo; + __wasi_errno_t error = + fd_object_get(curfds, &fo, sock, __WASI_RIGHT_SOCK_SHUTDOWN, 0); + if (error != 0) + return error; + + int ret = shutdown(fd_number(fo), nhow); + fd_object_release(fo); + if (ret < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_sched_yield(void) { + if (sched_yield() < 0) + return convert_errno(errno); + return 0; +} + +__wasi_errno_t wasmtime_ssp_args_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *argv_environ, +#endif + char **argv, + char *argv_buf +) { + for (size_t i = 0; i < argv_environ->argc; ++i) { + argv[i] = argv_buf + (argv_environ->argv[i] - argv_environ->argv_buf); + } + argv[argv_environ->argc] = NULL; + memcpy(argv_buf, argv_environ->argv_buf, argv_environ->argv_buf_size); + return __WASI_ESUCCESS; +} + +__wasi_errno_t wasmtime_ssp_args_sizes_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *argv_environ, +#endif + size_t *argc, + size_t *argv_buf_size +) { + *argc = argv_environ->argc; + *argv_buf_size = argv_environ->argv_buf_size; + return __WASI_ESUCCESS; +} + +__wasi_errno_t wasmtime_ssp_environ_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *argv_environ, +#endif + char **environ, + char *environ_buf +) { + for (size_t i = 0; i < argv_environ->environ_count; ++i) { + environ[i] = environ_buf + (argv_environ->environ[i] - argv_environ->environ_buf); + } + environ[argv_environ->environ_count] = NULL; + memcpy(environ_buf, argv_environ->environ_buf, argv_environ->environ_buf_size); + return __WASI_ESUCCESS; +} + +__wasi_errno_t wasmtime_ssp_environ_sizes_get( +#if !defined(WASMTIME_SSP_STATIC_CURFDS) + struct argv_environ_values *argv_environ, +#endif + size_t *environ_count, + size_t *environ_buf_size +) { + *environ_count = argv_environ->environ_count; + *environ_buf_size = argv_environ->environ_buf_size; + return __WASI_ESUCCESS; +} + +void argv_environ_init(struct argv_environ_values *argv_environ, + const size_t *argv_offsets, size_t argv_offsets_len, + const char *argv_buf, size_t argv_buf_len, + const size_t *environ_offsets, size_t environ_offsets_len, + const char *environ_buf, size_t environ_buf_len) +{ + argv_environ->argc = argv_offsets_len; + argv_environ->argv_buf_size = argv_buf_len; + argv_environ->argv = malloc(argv_offsets_len * sizeof(char *)); + argv_environ->argv_buf = malloc(argv_buf_len); + if (argv_environ->argv == NULL || argv_environ->argv_buf == NULL) { + abort(); + } + for (size_t i = 0; i < argv_offsets_len; ++i) { + argv_environ->argv[i] = argv_environ->argv_buf + argv_offsets[i]; + } + memcpy(argv_environ->argv_buf, argv_buf, argv_buf_len); + + argv_environ->environ_count = environ_offsets_len; + argv_environ->environ_buf_size = environ_buf_len; + argv_environ->environ = malloc(environ_offsets_len * sizeof(char *)); + argv_environ->environ_buf = malloc(environ_buf_len); + if (argv_environ->environ == NULL || argv_environ->environ_buf == NULL) { + abort(); + } + for (size_t i = 0; i < environ_offsets_len; ++i) { + argv_environ->environ[i] = argv_environ->environ_buf + environ_offsets[i]; + } + memcpy(argv_environ->environ_buf, environ_buf, environ_buf_len); +} diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.h new file mode 100644 index 000000000000..6711c9b2e37f --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.h @@ -0,0 +1,59 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/ + +#ifndef POSIX_H +#define POSIX_H + +#include +#include + +#include "locking.h" + +struct fd_entry; +struct fd_prestat; +struct syscalls; + +struct fd_table { + struct rwlock lock; + struct fd_entry *entries; + size_t size; + size_t used; +}; + +struct fd_prestats { + struct rwlock lock; + struct fd_prestat *prestats; + size_t size; + size_t used; +}; + +struct argv_environ_values { + size_t argc; + size_t argv_buf_size; + char **argv; + char *argv_buf; + size_t environ_count; + size_t environ_buf_size; + char **environ; + char *environ_buf; +}; + +void fd_table_init(struct fd_table *); +bool fd_table_insert_existing(struct fd_table *, __wasi_fd_t, int); +void fd_prestats_init(struct fd_prestats *); +bool fd_prestats_insert(struct fd_prestats *, const char *, __wasi_fd_t); +void argv_environ_init(struct argv_environ_values *, + const size_t *argv_offsets, size_t argv_offsets_len, + const char *argv_buf, size_t argv_buf_len, + const size_t *environ_offsets, size_t environ_offsets_len, + const char *environ_buf, size_t environ_buf_len); + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/queue.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/queue.h new file mode 100644 index 000000000000..c46190a85395 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/queue.h @@ -0,0 +1,92 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef QUEUE_H +#define QUEUE_H + +#include + +// LIST: Double-linked list. + +#define LIST_HEAD(name, type) \ + struct name { \ + struct type *l_first; \ + } +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define LIST_ENTRY(type) \ + struct { \ + struct type *l_next; \ + struct type **l_prev; \ + } + +#define LIST_FOREACH(var, head, field) \ + for ((var) = (head)->l_first; (var) != NULL; (var) = (var)->field.l_next) +#define LIST_INIT(head) \ + do { \ + (head)->l_first = NULL; \ + } while (0) +#define LIST_INSERT_HEAD(head, element, field) \ + do { \ + (element)->field.l_next = (head)->l_first; \ + if ((head)->l_first != NULL) \ + (head)->l_first->field.l_prev = &(element)->field.l_next; \ + (head)->l_first = (element); \ + (element)->field.l_prev = &(head)->l_first; \ + } while (0) +#define LIST_REMOVE(element, field) \ + do { \ + if ((element)->field.l_next != NULL) \ + (element)->field.l_next->field.l_prev = (element)->field.l_prev; \ + *(element)->field.l_prev = (element)->field.l_next; \ + } while (0) + +// TAILQ: Double-linked list with tail pointer. + +#define TAILQ_HEAD(name, type) \ + struct name { \ + struct type *t_first; \ + struct type **t_last; \ + } + +#define TAILQ_ENTRY(type) \ + struct { \ + struct type *t_next; \ + struct type **t_prev; \ + } + +#define TAILQ_EMPTY(head) ((head)->t_first == NULL) +#define TAILQ_FIRST(head) ((head)->t_first) +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = (head)->t_first; (var) != NULL; (var) = (var)->field.t_next) +#define TAILQ_INIT(head) \ + do { \ + (head)->t_first = NULL; \ + (head)->t_last = &(head)->t_first; \ + } while (0) +#define TAILQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.t_next = NULL; \ + (elm)->field.t_prev = (head)->t_last; \ + *(head)->t_last = (elm); \ + (head)->t_last = &(elm)->field.t_next; \ + } while (0) +#define TAILQ_REMOVE(head, element, field) \ + do { \ + if ((element)->field.t_next != NULL) \ + (element)->field.t_next->field.t_prev = (element)->field.t_prev; \ + else \ + (head)->t_last = (element)->field.t_prev; \ + *(element)->field.t_prev = (element)->field.t_next; \ + } while (0) + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c b/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c new file mode 100644 index 000000000000..9fc3e33be269 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c @@ -0,0 +1,92 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "random.h" + +#if CONFIG_HAS_ARC4RANDOM_BUF + +void random_buf(void *buf, size_t len) { + arc4random_buf(buf, len); +} + +#elif CONFIG_HAS_GETRANDOM + +#include + +void random_buf(void *buf, size_t len) { + for (;;) { + ssize_t x = getrandom(buf, len, 0); + if (x < 0) { + if (errno == EINTR) + continue; + fprintf(stderr, "getrandom failed: %s", strerror(errno)); + abort(); + } + if (x == len) + return; + buf = (void *)((unsigned char *)buf + x); + len -= x; + } +} + +#else + +static int urandom; + +static void open_urandom(void) { + urandom = open("/dev/urandom", O_RDONLY); + if (urandom < 0) { + fputs("Failed to open /dev/urandom\n", stderr); + abort(); + } +} + +void random_buf(void *buf, size_t len) { + static pthread_once_t open_once = PTHREAD_ONCE_INIT; + pthread_once(&open_once, open_urandom); + + if (read(urandom, buf, len) != len) { + fputs("Short read on /dev/urandom\n", stderr); + abort(); + } +} + +#endif + +// Calculates a random number within the range [0, upper - 1] without +// any modulo bias. +// +// The function below repeatedly obtains a random number from +// arc4random() until it lies within the range [2^k % upper, 2^k). As +// this range has length k * upper, we can safely obtain a number +// without any modulo bias. +uintmax_t random_uniform(uintmax_t upper) { + // Compute 2^k % upper + // == (2^k - upper) % upper + // == -upper % upper. + uintmax_t lower = -upper % upper; + for (;;) { + uintmax_t value; + random_buf(&value, sizeof(value)); + if (value >= lower) + return value % upper; + } +} diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/random.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/random.h new file mode 100644 index 000000000000..d87a292cf0c8 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/random.h @@ -0,0 +1,20 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef RANDOM_H +#define RANDOM_H + +#include + +void random_buf(void *, size_t); +uintmax_t random_uniform(uintmax_t); + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/refcount.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/refcount.h new file mode 100644 index 000000000000..51752bc9b1e3 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/refcount.h @@ -0,0 +1,47 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef REFCOUNT_H +#define REFCOUNT_H + +#include +#include +#include + +#include "locking.h" + +// Simple reference counter. +struct LOCKABLE refcount { + atomic_uint count; +}; + +#define PRODUCES(...) LOCKS_SHARED(__VA_ARGS__) NO_LOCK_ANALYSIS +#define CONSUMES(...) UNLOCKS(__VA_ARGS__) NO_LOCK_ANALYSIS + +// Initialize the reference counter. +static void refcount_init(struct refcount *r, unsigned int count) PRODUCES(*r) { + atomic_init(&r->count, count); +} + +// Increment the reference counter. +static inline void refcount_acquire(struct refcount *r) PRODUCES(*r) { + atomic_fetch_add_explicit(&r->count, 1, memory_order_acquire); +} + +// Decrement the reference counter, returning whether the reference +// dropped to zero. +static inline bool refcount_release(struct refcount *r) CONSUMES(*r) { + int old = atomic_fetch_sub_explicit(&r->count, 1, memory_order_release); + assert(old != 0 && "Reference count becoming negative"); + return old == 1; +} + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/rights.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/rights.h new file mode 100644 index 000000000000..69349e45ca09 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/rights.h @@ -0,0 +1,83 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef RIGHTS_H +#define RIGHTS_H + +#define RIGHTS_ALL \ + (__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_READ | \ + __WASI_RIGHT_FD_SEEK | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ + __WASI_RIGHT_FD_SYNC | __WASI_RIGHT_FD_TELL | __WASI_RIGHT_FD_WRITE | \ + __WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_ALLOCATE | \ + __WASI_RIGHT_PATH_CREATE_DIRECTORY | __WASI_RIGHT_PATH_CREATE_FILE | \ + __WASI_RIGHT_PATH_LINK_SOURCE | __WASI_RIGHT_PATH_LINK_TARGET | \ + __WASI_RIGHT_PATH_OPEN | __WASI_RIGHT_FD_READDIR | \ + __WASI_RIGHT_PATH_READLINK | __WASI_RIGHT_PATH_RENAME_SOURCE | \ + __WASI_RIGHT_PATH_RENAME_TARGET | __WASI_RIGHT_PATH_FILESTAT_GET | \ + __WASI_RIGHT_PATH_FILESTAT_SET_SIZE | \ + __WASI_RIGHT_PATH_FILESTAT_SET_TIMES | \ + __WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_FILESTAT_SET_TIMES | \ + __WASI_RIGHT_FD_FILESTAT_SET_SIZE | \ + __WASI_RIGHT_PATH_SYMLINK | __WASI_RIGHT_PATH_UNLINK_FILE | \ + __WASI_RIGHT_PATH_REMOVE_DIRECTORY | \ + __WASI_RIGHT_POLL_FD_READWRITE | __WASI_RIGHT_SOCK_SHUTDOWN) + +// Block and character device interaction is outside the scope of +// CloudABI. Simply allow everything. +#define RIGHTS_BLOCK_DEVICE_BASE RIGHTS_ALL +#define RIGHTS_BLOCK_DEVICE_INHERITING RIGHTS_ALL +#define RIGHTS_CHARACTER_DEVICE_BASE RIGHTS_ALL +#define RIGHTS_CHARACTER_DEVICE_INHERITING RIGHTS_ALL + +// Only allow directory operations on directories. Directories can only +// yield file descriptors to other directories and files. +#define RIGHTS_DIRECTORY_BASE \ + (__WASI_RIGHT_FD_FDSTAT_SET_FLAGS | __WASI_RIGHT_FD_SYNC | \ + __WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_PATH_CREATE_DIRECTORY | \ + __WASI_RIGHT_PATH_CREATE_FILE | __WASI_RIGHT_PATH_LINK_SOURCE | \ + __WASI_RIGHT_PATH_LINK_TARGET | __WASI_RIGHT_PATH_OPEN | \ + __WASI_RIGHT_FD_READDIR | __WASI_RIGHT_PATH_READLINK | \ + __WASI_RIGHT_PATH_RENAME_SOURCE | __WASI_RIGHT_PATH_RENAME_TARGET | \ + __WASI_RIGHT_PATH_FILESTAT_GET | \ + __WASI_RIGHT_PATH_FILESTAT_SET_SIZE | \ + __WASI_RIGHT_PATH_FILESTAT_SET_TIMES | \ + __WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_FILESTAT_SET_TIMES | \ + __WASI_RIGHT_PATH_SYMLINK | __WASI_RIGHT_PATH_UNLINK_FILE | \ + __WASI_RIGHT_PATH_REMOVE_DIRECTORY | \ + __WASI_RIGHT_POLL_FD_READWRITE) +#define RIGHTS_DIRECTORY_INHERITING \ + (RIGHTS_DIRECTORY_BASE | RIGHTS_REGULAR_FILE_BASE) + +// Operations that apply to regular files. +#define RIGHTS_REGULAR_FILE_BASE \ + (__WASI_RIGHT_FD_DATASYNC | __WASI_RIGHT_FD_READ | \ + __WASI_RIGHT_FD_SEEK | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ + __WASI_RIGHT_FD_SYNC | __WASI_RIGHT_FD_TELL | __WASI_RIGHT_FD_WRITE | \ + __WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_ALLOCATE | \ + __WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_FILESTAT_SET_SIZE | \ + __WASI_RIGHT_FD_FILESTAT_SET_TIMES | __WASI_RIGHT_POLL_FD_READWRITE) +#define RIGHTS_REGULAR_FILE_INHERITING 0 + +// Operations that apply to sockets and socket pairs. +#define RIGHTS_SOCKET_BASE \ + (__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ + __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_FILESTAT_GET | \ + __WASI_RIGHT_POLL_FD_READWRITE | __WASI_RIGHT_SOCK_SHUTDOWN) +#define RIGHTS_SOCKET_INHERITING RIGHTS_ALL + +// Operations that apply to TTYs. +#define RIGHTS_TTY_BASE \ + (__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_FDSTAT_SET_FLAGS | \ + __WASI_RIGHT_FD_WRITE | __WASI_RIGHT_FD_FILESTAT_GET | \ + __WASI_RIGHT_POLL_FD_READWRITE) +#define RIGHTS_TTY_INHERITING 0 + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/signals.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/signals.h new file mode 100644 index 000000000000..16f5d575ca2e --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/signals.h @@ -0,0 +1,17 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef SIGNALS_H +#define SIGNALS_H + +void signals_init(void); + +#endif diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c b/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c new file mode 100644 index 000000000000..02a9518f472f --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c @@ -0,0 +1,33 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#include "config.h" + +#include +#include +#include + +#include "str.h" + +char *str_nullterminate(const char *s, size_t len) { + // Copy string. + char *ret = strndup(s, len); + if (ret == NULL) + return NULL; + + // Ensure that it contains no null bytes within. + if (strlen(ret) != len) { + free(ret); + errno = EILSEQ; + return NULL; + } + return ret; +} diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h b/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h new file mode 100644 index 000000000000..a9c5f226fa72 --- /dev/null +++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h @@ -0,0 +1,19 @@ +// Part of the Wasmtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/CraneStation/wasmtime/blob/master/LICENSE for license information. +// +// Significant parts of this file are derived from cloudabi-utils. See +// https://github.com/CraneStation/wasmtime/blob/master/lib/wasi/sandboxed-system-primitives/src/LICENSE +// for license information. +// +// The upstream file contains the following copyright notice: +// +// Copyright (c) 2016 Nuxi, https://nuxi.nl/ + +#ifndef STR_H +#define STR_H + +#include "config.h" + +char *str_nullterminate(const char *, size_t); + +#endif diff --git a/wasmtime-wasi-c/src/host.rs b/wasmtime-wasi-c/src/host.rs new file mode 100644 index 000000000000..918ea92f5eef --- /dev/null +++ b/wasmtime-wasi-c/src/host.rs @@ -0,0 +1,6 @@ +#![allow(non_camel_case_types, dead_code)] + +include!(concat!(env!("OUT_DIR"), "/wasmtime_ssp.rs")); + +pub type char = ::std::os::raw::c_char; +pub type void = ::std::os::raw::c_void; diff --git a/wasmtime-wasi-c/src/instantiate.rs b/wasmtime-wasi-c/src/instantiate.rs new file mode 100644 index 000000000000..991aaaa50a22 --- /dev/null +++ b/wasmtime-wasi-c/src/instantiate.rs @@ -0,0 +1,205 @@ +use crate::host::{ + argv_environ_init, argv_environ_values, fd_prestats, fd_prestats_init, fd_prestats_insert, + fd_table, fd_table_init, fd_table_insert_existing, +}; +use cranelift_codegen::ir::types; +use cranelift_codegen::{ir, isa}; +use cranelift_entity::PrimaryMap; +use cranelift_wasm::DefinedFuncIndex; +use std::cell::RefCell; +use std::collections::HashMap; +use std::ffi::CString; +use std::fs::File; +use std::mem; +use std::os::unix::io::AsRawFd; +use std::rc::Rc; +use syscalls; +use target_lexicon::HOST; +use wasmtime_environ::{translate_signature, Export, Module}; +use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMFunctionBody}; + +pub(crate) struct WASIState { + pub curfds: Box, + pub prestats: Box, + pub argv_environ: Box, +} + +/// Return an instance implementing the "wasi" interface. +pub fn instantiate_wasi_c( + prefix: &str, + global_exports: Rc>>>, + preopened_dirs: &[(String, File)], + argv: &[String], + environ: &[(String, String)], +) -> Result { + let pointer_type = types::Type::triple_pointer_type(&HOST); + let mut module = Module::new(); + let mut finished_functions: PrimaryMap = + PrimaryMap::new(); + let call_conv = isa::CallConv::triple_default(&HOST); + + macro_rules! signature { + ($name:ident) => {{ + let sig = module.signatures.push(translate_signature( + ir::Signature { + params: syscalls::$name::params() + .into_iter() + .map(ir::AbiParam::new) + .collect(), + returns: syscalls::$name::results() + .into_iter() + .map(ir::AbiParam::new) + .collect(), + call_conv, + }, + pointer_type, + )); + let func = module.functions.push(sig); + module.exports.insert( + prefix.to_owned() + stringify!($name), + Export::Function(func), + ); + finished_functions.push(syscalls::$name::SHIM as *const VMFunctionBody); + }}; + } + + signature!(args_get); + signature!(args_sizes_get); + signature!(clock_res_get); + signature!(clock_time_get); + signature!(environ_get); + signature!(environ_sizes_get); + signature!(fd_prestat_get); + signature!(fd_prestat_dir_name); + signature!(fd_close); + signature!(fd_datasync); + signature!(fd_pread); + signature!(fd_pwrite); + signature!(fd_read); + signature!(fd_renumber); + signature!(fd_seek); + signature!(fd_tell); + signature!(fd_fdstat_get); + signature!(fd_fdstat_set_flags); + signature!(fd_fdstat_set_rights); + signature!(fd_sync); + signature!(fd_write); + signature!(fd_advise); + signature!(fd_allocate); + signature!(path_create_directory); + signature!(path_link); + signature!(path_open); + signature!(fd_readdir); + signature!(path_readlink); + signature!(path_rename); + signature!(fd_filestat_get); + signature!(fd_filestat_set_times); + signature!(fd_filestat_set_size); + signature!(path_filestat_get); + signature!(path_filestat_set_times); + signature!(path_symlink); + signature!(path_unlink_file); + signature!(path_remove_directory); + signature!(poll_oneoff); + signature!(proc_exit); + signature!(proc_raise); + signature!(random_get); + signature!(sched_yield); + signature!(sock_recv); + signature!(sock_send); + signature!(sock_shutdown); + + let imports = Imports::none(); + let data_initializers = Vec::new(); + let signatures = PrimaryMap::new(); + let mut curfds = Box::new(unsafe { mem::zeroed::() }); + let mut prestats = Box::new(unsafe { mem::zeroed::() }); + let mut argv_environ = Box::new(unsafe { mem::zeroed::() }); + + unsafe { + let argv_environ: &mut argv_environ_values = &mut *argv_environ; + let (argv_offsets, argv_buf, environ_offsets, environ_buf) = + allocate_argv_environ(argv, environ); + argv_environ_init( + argv_environ, + argv_offsets.as_ptr(), + argv_offsets.len(), + argv_buf.as_ptr(), + argv_buf.len(), + environ_offsets.as_ptr(), + environ_offsets.len(), + environ_buf.as_ptr(), + environ_buf.len(), + ); + + let curfds: *mut fd_table = &mut *curfds; + fd_table_init(curfds); + + let prestats: *mut fd_prestats = &mut *prestats; + fd_prestats_init(prestats); + + // Prepopulate curfds with stdin, stdout, and stderr file descriptors. + assert!(fd_table_insert_existing(curfds, 0, 0)); + assert!(fd_table_insert_existing(curfds, 1, 1)); + assert!(fd_table_insert_existing(curfds, 2, 2)); + + let mut wasm_fd = 3; + for (dir, file) in preopened_dirs { + assert!(fd_table_insert_existing(curfds, wasm_fd, file.as_raw_fd())); + assert!(fd_prestats_insert( + prestats, + CString::new(dir.as_str()).unwrap().as_ptr(), + wasm_fd, + )); + wasm_fd += 1; + } + } + + let host_state = WASIState { + curfds, + prestats, + argv_environ, + }; + + InstanceHandle::new( + Rc::new(module), + global_exports, + finished_functions.into_boxed_slice(), + imports, + &data_initializers, + signatures.into_boxed_slice(), + None, + Box::new(host_state), + ) +} + +fn allocate_argv_environ( + argv: &[String], + environ: &[(String, String)], +) -> (Vec, Vec, Vec, Vec) { + let mut argv_offsets = Vec::new(); + let mut argv_buf = Vec::new(); + let mut environ_offsets = Vec::new(); + let mut environ_buf = Vec::new(); + + for arg in argv { + argv_offsets.push(argv_buf.len()); + for c in arg.bytes() { + argv_buf.push(c as libc::c_char); + } + argv_buf.push('\0' as libc::c_char); + } + for (key, value) in environ { + environ_offsets.push(environ_buf.len()); + for c in key.bytes() { + environ_buf.push(c as libc::c_char); + } + environ_buf.push('=' as libc::c_char); + for c in value.bytes() { + environ_buf.push(c as libc::c_char); + } + environ_buf.push('\0' as libc::c_char); + } + + (argv_offsets, argv_buf, environ_offsets, environ_buf) +} diff --git a/wasmtime-wasi-c/src/lib.rs b/wasmtime-wasi-c/src/lib.rs new file mode 100644 index 000000000000..712ee83b9c60 --- /dev/null +++ b/wasmtime-wasi-c/src/lib.rs @@ -0,0 +1,18 @@ +extern crate cast; +extern crate cranelift_codegen; +extern crate cranelift_entity; +extern crate cranelift_wasm; +extern crate target_lexicon; +extern crate wasmtime_environ; +extern crate wasmtime_jit; +extern crate wasmtime_runtime; +#[macro_use] +extern crate log; + +mod host; +mod instantiate; +mod syscalls; +mod translate; +mod wasm32; + +pub use instantiate::instantiate_wasi_c; diff --git a/wasmtime-wasi-c/src/syscalls.rs b/wasmtime-wasi-c/src/syscalls.rs new file mode 100644 index 000000000000..687e1a6f76a9 --- /dev/null +++ b/wasmtime-wasi-c/src/syscalls.rs @@ -0,0 +1,1557 @@ +use crate::host::{argv_environ_values, fd_prestats, fd_table}; +use crate::instantiate::WASIState; +use cranelift_codegen::ir::types::{Type, I32, I64}; +use host; +use std::{mem, ptr, slice, str}; +use translate::*; +use wasm32; +use wasmtime_runtime::VMContext; + +fn str_for_trace<'str>(ptr: *const i8, len: usize) -> Result<&'str str, str::Utf8Error> { + str::from_utf8(unsafe { slice::from_raw_parts(ptr as *const u8, len) }) +} + +fn return_encoded_errno(e: host::__wasi_errno_t) -> wasm32::__wasi_errno_t { + let errno = encode_errno(e); + trace!(" -> errno={}", wasm32::strerror(errno)); + errno +} + +unsafe fn get_curfds(vmctx: *mut VMContext) -> *mut fd_table { + (&mut *(&mut *vmctx) + .host_state() + .downcast_mut::() + .unwrap() + .curfds) as *mut fd_table +} + +unsafe fn get_prestats(vmctx: *mut VMContext) -> *mut fd_prestats { + (&mut *(&mut *vmctx) + .host_state() + .downcast_mut::() + .unwrap() + .prestats) as *mut fd_prestats +} + +unsafe fn get_argv_environ(vmctx: *mut VMContext) -> *mut argv_environ_values { + (&mut *(&mut *vmctx) + .host_state() + .downcast_mut::() + .unwrap() + .argv_environ) as *mut argv_environ_values +} + +pub trait AbiRet { + type Abi; + fn convert(self) -> Self::Abi; + fn codegen_tys() -> Vec; +} + +pub trait AbiParam { + type Abi; + fn convert(arg: Self::Abi) -> Self; + fn codegen_ty() -> Type; +} + +macro_rules! cast32 { + ($($i:ident)*) => ($( + impl AbiRet for $i { + type Abi = i32; + + fn convert(self) -> Self::Abi { + self as i32 + } + + fn codegen_tys() -> Vec { vec![I32] } + } + + impl AbiParam for $i { + type Abi = i32; + + fn convert(param: i32) -> Self { + param as $i + } + + fn codegen_ty() -> Type { I32 } + } + )*) +} + +macro_rules! cast64 { + ($($i:ident)*) => ($( + impl AbiRet for $i { + type Abi = i64; + + fn convert(self) -> Self::Abi { + self as i64 + } + + fn codegen_tys() -> Vec { vec![I64] } + } + + impl AbiParam for $i { + type Abi = i64; + + fn convert(param: i64) -> Self { + param as $i + } + + fn codegen_ty() -> Type { I64 } + } + )*) +} + +cast32!(i8 i16 i32 u8 u16 u32); +cast64!(i64 u64); + +impl AbiRet for () { + type Abi = (); + fn convert(self) {} + fn codegen_tys() -> Vec { + Vec::new() + } +} + +macro_rules! syscalls { + ($(pub unsafe extern "C" fn $name:ident($ctx:ident: *mut VMContext $(, $arg:ident: $ty:ty)*,) -> $ret:ty { + $($body:tt)* + })*) => ($( + pub mod $name { + use super::*; + + /// Returns the codegen types of all the parameters to the shim + /// generated + pub fn params() -> Vec { + vec![$(<$ty as AbiParam>::codegen_ty()),*] + } + + /// Returns the codegen types of all the results of the shim + /// generated + pub fn results() -> Vec { + <$ret as AbiRet>::codegen_tys() + } + + /// The actual function pointer to the shim for a syscall. + /// + /// NB: ideally we'd expose `shim` below, but it seems like there's + /// a compiler bug which prvents that from being cast to a `usize`. + pub static SHIM: unsafe extern "C" fn( + *mut VMContext, + $(<$ty as AbiParam>::Abi),* + ) -> <$ret as AbiRet>::Abi = shim; + + unsafe extern "C" fn shim( + $ctx: *mut VMContext, + $($arg: <$ty as AbiParam>::Abi,)* + ) -> <$ret as AbiRet>::Abi { + let r = super::$name($ctx, $(<$ty as AbiParam>::convert($arg),)*); + <$ret as AbiRet>::convert(r) + } + } + + pub unsafe extern "C" fn $name($ctx: *mut VMContext, $($arg: $ty,)*) -> $ret { + $($body)* + } + )*) +} + +syscalls! { + + pub unsafe extern "C" fn args_get( + vmctx: *mut VMContext, + argv: wasm32::uintptr_t, + argv_buf: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "args_get(argv={:#x?}, argv_buf={:#x?})", + argv, + argv_buf, + ); + + let vmctx = &mut *vmctx; + let argv_environ = get_argv_environ(vmctx); + let argc = match cast::u32((*argv_environ).argc) { + Ok(argc) => argc, + Err(_) => return wasm32::__WASI_ENOMEM, + }; + let argv_buf_size = match cast::u32((*argv_environ).argv_buf_size) { + Ok(argc) => argc, + Err(_) => return wasm32::__WASI_ENOMEM, + }; + + let (host_argv_buf, _argv_buf_size) = match decode_char_slice(vmctx, argv_buf, argv_buf_size) { + Ok((argv_buf, argv_buf_size)) => (argv_buf, argv_buf_size), + Err(e) => return return_encoded_errno(e), + }; + // Add 1 so that we can add an extra NULL pointer at the end. + let (argv, _argc) = match decode_charstar_slice(vmctx, argv, argc + 1) { + Ok((argv, argc)) => (argv, argc), + Err(e) => return return_encoded_errno(e), + }; + let mut host_argv = Vec::new(); + host_argv.resize((*argv_environ).argc + 1, ptr::null_mut()); + + let e = host::wasmtime_ssp_args_get(argv_environ, host_argv.as_mut_ptr(), host_argv_buf); + + encode_charstar_slice(argv, host_argv, argv_buf, host_argv_buf); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn args_sizes_get( + vmctx: *mut VMContext, + argc: wasm32::uintptr_t, + argv_buf_size: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "args_sizes_get(argc={:#x?}, argv_buf_size={:#x?})", + argc, + argv_buf_size, + ); + + let vmctx = &mut *vmctx; + let mut host_argc = 0; + if let Err(e) = decode_usize_byref(vmctx, argc) { + return return_encoded_errno(e); + } + let mut host_argv_buf_size = 0; + if let Err(e) = decode_usize_byref(vmctx, argv_buf_size) { + return return_encoded_errno(e); + } + + let vmctx = &mut *vmctx; + let argv_environ = get_argv_environ(vmctx); + + let e = host::wasmtime_ssp_args_sizes_get(argv_environ, &mut host_argc, &mut host_argv_buf_size); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *argc={:?}", host_argc); + encode_usize_byref(vmctx, argc, host_argc); + + trace!(" | *argv_buf_size={:?}", host_argv_buf_size); + encode_usize_byref(vmctx, argv_buf_size, host_argv_buf_size); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn clock_res_get( + vmctx: *mut VMContext, + clock_id: wasm32::__wasi_clockid_t, + resolution: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "clock_res_get(clock_id={:?}, resolution={:#x?})", + clock_id, + resolution, + ); + + let vmctx = &mut *vmctx; + let clock_id = decode_clockid(clock_id); + let mut host_resolution = 0; + if let Err(e) = decode_timestamp_byref(vmctx, resolution) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_clock_res_get(clock_id, &mut host_resolution); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *resolution={:?}", host_resolution); + encode_timestamp_byref(vmctx, resolution, host_resolution); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn clock_time_get( + vmctx: *mut VMContext, + clock_id: wasm32::__wasi_clockid_t, + precision: wasm32::__wasi_timestamp_t, + time: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "clock_time_get(clock_id={:?}, precision={:?}, time={:#x?})", + clock_id, + precision, + time, + ); + + let vmctx = &mut *vmctx; + let clock_id = decode_clockid(clock_id); + let precision = decode_timestamp(precision); + let mut host_time = 0; + if let Err(e) = decode_timestamp_byref(vmctx, time) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_clock_time_get(clock_id, precision, &mut host_time); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *time={:?}", host_time); + encode_timestamp_byref(vmctx, time, host_time); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn environ_get( + vmctx: *mut VMContext, + environ: wasm32::uintptr_t, + environ_buf: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "environ_get(environ={:#x?}, environ_buf={:#x?})", + environ, + environ_buf, + ); + + let vmctx = &mut *vmctx; + let argv_environ = get_argv_environ(vmctx); + let environ_count = match cast::u32((*argv_environ).environ_count) { + Ok(host_environ_count) => host_environ_count, + Err(_) => return wasm32::__WASI_ENOMEM, + }; + let environ_buf_size = match cast::u32((*argv_environ).environ_buf_size) { + Ok(host_environ_buf_size) => host_environ_buf_size, + Err(_) => return wasm32::__WASI_ENOMEM, + }; + + let (host_environ_buf, _environ_buf_len) = match decode_char_slice(vmctx, environ_buf, environ_buf_size) { + Ok((environ_buf, environ_buf_len)) => (environ_buf, environ_buf_len), + Err(e) => return return_encoded_errno(e), + }; + // Add 1 so that we can add an extra NULL pointer at the end. + let (environ, _environ_count) = match decode_charstar_slice(vmctx, environ, environ_count + 1) { + Ok((environ, environ_count)) => (environ, environ_count), + Err(e) => return return_encoded_errno(e), + }; + let mut host_environ = Vec::new(); + host_environ.resize((*argv_environ).environ_count + 1, ptr::null_mut()); + + let e = host::wasmtime_ssp_environ_get(argv_environ, host_environ.as_mut_ptr(), host_environ_buf); + + encode_charstar_slice(environ, host_environ, environ_buf, host_environ_buf); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn environ_sizes_get( + vmctx: *mut VMContext, + environ_count: wasm32::uintptr_t, + environ_buf_size: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "environ_sizes_get(environ_count={:#x?}, environ_buf_size={:#x?})", + environ_count, + environ_buf_size, + ); + + let vmctx = &mut *vmctx; + let mut host_environ_count = 0; + if let Err(e) = decode_usize_byref(vmctx, environ_count) { + return return_encoded_errno(e); + } + let mut host_environ_buf_size = 0; + if let Err(e) = decode_usize_byref(vmctx, environ_buf_size) { + return return_encoded_errno(e); + } + + let vmctx = &mut *vmctx; + let argv_environ = get_argv_environ(vmctx); + + let e = host::wasmtime_ssp_environ_sizes_get(argv_environ, &mut host_environ_count, &mut host_environ_buf_size); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *environ_count={:?}", host_environ_count); + encode_usize_byref(vmctx, environ_count, host_environ_count); + + trace!(" | *environ_buf_size={:?}", host_environ_buf_size); + encode_usize_byref(vmctx, environ_buf_size, host_environ_buf_size); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_prestat_get( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + buf: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_prestat_get(fd={:?}, buf={:#x?})", fd, buf); + + let vmctx = &mut *vmctx; + let prestats = get_prestats(vmctx); + let fd = decode_fd(fd); + let mut host_buf = std::mem::zeroed(); + if let Err(e) = decode_prestat_byref(vmctx, buf) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_prestat_get(prestats, fd, &mut host_buf); + + encode_prestat_byref(vmctx, buf, host_buf); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_prestat_dir_name( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_prestat_dir_name(fd={:?}, path={:#x?}, path_len={})", fd, path, path_len); + + let vmctx = &mut *vmctx; + let prestats = get_prestats(vmctx); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_fd_prestat_dir_name(prestats, fd, path, path_len); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_close( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_close(fd={:?})", fd); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let prestats = get_prestats(vmctx); + let fd = decode_fd(fd); + + let e = host::wasmtime_ssp_fd_close(curfds, prestats, fd); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_datasync( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_datasync(fd={:?})", fd); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + + let e = host::wasmtime_ssp_fd_datasync(curfds, fd); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_pread( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + iovs: wasm32::uintptr_t, + iovs_len: wasm32::size_t, + offset: wasm32::__wasi_filesize_t, + nread: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_pread(fd={:?}, iovs={:#x?}, iovs_len={:?}, offset={}, nread={:#x?})", + fd, + iovs, + iovs_len, + offset, + nread + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let iovs = match decode_iovec_slice(vmctx, iovs, iovs_len) { + Ok(iovs) => iovs, + Err(e) => return return_encoded_errno(e), + }; + let offset = decode_filesize(offset); + let mut host_nread = 0; + if let Err(e) = decode_usize_byref(vmctx, nread) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_pread( + curfds, + fd, + iovs.as_ptr(), + iovs.len(), + offset, + &mut host_nread, + ); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *nread={:?}", host_nread); + encode_usize_byref(vmctx, nread, host_nread); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_pwrite( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + iovs: wasm32::uintptr_t, + iovs_len: wasm32::size_t, + offset: wasm32::__wasi_filesize_t, + nwritten: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_pwrite(fd={:?}, iovs={:#x?}, iovs_len={:?}, offset={}, nwritten={:#x?})", + fd, + iovs, + iovs_len, + offset, + nwritten + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let iovs = match decode_ciovec_slice(vmctx, iovs, iovs_len) { + Ok(iovs) => iovs, + Err(e) => return return_encoded_errno(e), + }; + let offset = decode_filesize(offset); + let mut host_nwritten = 0; + if let Err(e) = decode_usize_byref(vmctx, nwritten) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_pwrite( + curfds, + fd, + iovs.as_ptr(), + iovs.len(), + offset, + &mut host_nwritten, + ); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *nwritten={:?}", host_nwritten); + encode_usize_byref(vmctx, nwritten, host_nwritten); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_read( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + iovs: wasm32::uintptr_t, + iovs_len: wasm32::size_t, + nread: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_read(fd={:?}, iovs={:#x?}, iovs_len={:?}, nread={:#x?})", + fd, + iovs, + iovs_len, + nread + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let iovs = match decode_iovec_slice(vmctx, iovs, iovs_len) { + Ok(iovs) => iovs, + Err(e) => return return_encoded_errno(e), + }; + let mut host_nread = 0; + if let Err(e) = decode_usize_byref(vmctx, nread) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_read(curfds, fd, iovs.as_ptr(), iovs.len(), &mut host_nread); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *nread={:?}", host_nread); + encode_usize_byref(vmctx, nread, host_nread); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_renumber( + vmctx: *mut VMContext, + from: wasm32::__wasi_fd_t, + to: wasm32::__wasi_fd_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_renumber(from={:?}, to={:?})", from, to); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let prestats = get_prestats(vmctx); + let from = decode_fd(from); + let to = decode_fd(to); + + let e = host::wasmtime_ssp_fd_renumber(curfds, prestats, from, to); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_seek( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + offset: wasm32::__wasi_filedelta_t, + whence: wasm32::__wasi_whence_t, + newoffset: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_seek(fd={:?}, offset={:?}, whence={}, newoffset={:#x?})", + fd, + offset, + wasm32::whence_to_str(whence), + newoffset + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let offset = decode_filedelta(offset); + let whence = decode_whence(whence); + let mut host_newoffset = 0; + if let Err(e) = decode_filesize_byref(vmctx, newoffset) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_seek(curfds, fd, offset, whence, &mut host_newoffset); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *newoffset={:?}", host_newoffset); + encode_filesize_byref(vmctx, newoffset, host_newoffset); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_tell( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + newoffset: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_tell(fd={:?}, newoffset={:#x?})", fd, newoffset); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let mut host_newoffset = 0; + if let Err(e) = decode_filesize_byref(vmctx, newoffset) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_tell(curfds, fd, &mut host_newoffset); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *newoffset={:?}", host_newoffset); + encode_filesize_byref(vmctx, newoffset, host_newoffset); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_fdstat_get( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + buf: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_fdstat_get(fd={:?}, buf={:#x?})", fd, buf); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let mut host_buf = std::mem::zeroed(); + if let Err(e) = decode_fdstat_byref(vmctx, buf) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_fdstat_get(curfds, fd, &mut host_buf); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *buf={:?}", host_buf); + encode_fdstat_byref(vmctx, buf, host_buf); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_fdstat_set_flags( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + flags: wasm32::__wasi_fdflags_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_fdstat_set_flags(fd={:?}, flags={:#x?})", + fd, + flags + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let flags = decode_fdflags(flags); + + let e = host::wasmtime_ssp_fd_fdstat_set_flags(curfds, fd, flags); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_fdstat_set_rights( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + fs_rights_base: wasm32::__wasi_rights_t, + fs_rights_inheriting: wasm32::__wasi_rights_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_fdstat_set_rights(fd={:?}, fs_rights_base={:#x?}, fs_rights_inheriting={:#x?})", + fd, + fs_rights_base, + fs_rights_inheriting + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let fs_rights_base = decode_rights(fs_rights_base); + let fs_rights_inheriting = decode_rights(fs_rights_inheriting); + + let e = host::wasmtime_ssp_fd_fdstat_set_rights(curfds, fd, fs_rights_base, fs_rights_inheriting); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_sync( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_sync(fd={:?})", fd); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + + let e = host::wasmtime_ssp_fd_sync(curfds, fd); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_write( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + iovs: wasm32::uintptr_t, + iovs_len: wasm32::size_t, + nwritten: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_write(fd={:?}, iovs={:#x?}, iovs_len={:?}, nwritten={:#x?})", + fd, + iovs, + iovs_len, + nwritten + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let iovs = match decode_ciovec_slice(vmctx, iovs, iovs_len) { + Ok(iovs) => iovs, + Err(e) => return return_encoded_errno(e), + }; + let mut host_nwritten = 0; + if let Err(e) = decode_usize_byref(vmctx, nwritten) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_write(curfds, fd, iovs.as_ptr(), iovs.len(), &mut host_nwritten); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *nwritten={:?}", host_nwritten); + encode_usize_byref(vmctx, nwritten, host_nwritten); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_advise( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + offset: wasm32::__wasi_filesize_t, + len: wasm32::__wasi_filesize_t, + advice: wasm32::__wasi_advice_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_advise(fd={:?}, offset={}, len={}, advice={:?})", + fd, + offset, + len, + advice + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let offset = decode_filesize(offset); + let len = decode_filesize(len); + let advice = decode_advice(advice); + + let e = host::wasmtime_ssp_fd_advise(curfds, fd, offset, len, advice); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_allocate( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + offset: wasm32::__wasi_filesize_t, + len: wasm32::__wasi_filesize_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_allocate(fd={:?}, offset={}, len={})", fd, offset, len); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let offset = decode_filesize(offset); + let len = decode_filesize(len); + + let e = host::wasmtime_ssp_fd_allocate(curfds, fd, offset, len); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_create_directory( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_create_directory(fd={:?}, path={:#x?}, path_len={})", + fd, + path, + path_len, + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_create_directory(curfds, fd, path, path_len); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_link( + vmctx: *mut VMContext, + fd0: wasm32::__wasi_fd_t, + flags0: wasm32::__wasi_lookupflags_t, + path0: wasm32::uintptr_t, + path_len0: wasm32::size_t, + fd1: wasm32::__wasi_fd_t, + path1: wasm32::uintptr_t, + path_len1: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_link(fd0={:?}, flags0={:?}, path0={:#x?}, path_len0={}, fd1={:?}, path1={:#x?}, path_len1={})", + fd0, + flags0, + path0, + path_len0, + fd1, + path1, + path_len1 + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd0 = decode_fd(fd0); + let flags0 = decode_lookupflags(flags0); + let (path0, path_len0) = match decode_char_slice(vmctx, path0, path_len0) { + Ok((path0, path_len0)) => (path0, path_len0), + Err(e) => return return_encoded_errno(e), + }; + let fd1 = decode_fd(fd1); + let (path1, path_len1) = match decode_char_slice(vmctx, path1, path_len1) { + Ok((path1, path_len1)) => (path1, path_len1), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path0,path_len0)={:?}", str_for_trace(path0, path_len0)); + trace!(" | (path1,path_len1)={:?}", str_for_trace(path1, path_len1)); + + let e = + host::wasmtime_ssp_path_link(curfds, fd0, flags0, path0, path_len0, fd1, path1, path_len1); + + return_encoded_errno(e) + } + + // TODO: When multi-value happens, switch to that instead of passing + // the `fd` by reference? + pub unsafe extern "C" fn path_open( + vmctx: *mut VMContext, + dirfd: wasm32::__wasi_fd_t, + dirflags: wasm32::__wasi_lookupflags_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + oflags: wasm32::__wasi_oflags_t, + fs_rights_base: wasm32::__wasi_rights_t, + fs_rights_inheriting: wasm32::__wasi_rights_t, + fs_flags: wasm32::__wasi_fdflags_t, + fd: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_open(dirfd={:?}, dirflags={:?}, path={:#x?}, path_len={:?}, oflags={:#x?}, fs_rights_base={:#x?}, fs_rights_inheriting={:#x?}, fs_flags={:#x?}, fd={:#x?})", + dirfd, + dirflags, + path, + path_len, + oflags, + fs_rights_base, + fs_rights_inheriting, + fs_flags, + fd + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let dirfd = decode_fd(dirfd); + let dirflags = decode_lookupflags(dirflags); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + let oflags = decode_oflags(oflags); + let fs_rights_base = decode_rights(fs_rights_base); + let fs_rights_inheriting = decode_rights(fs_rights_inheriting); + let fs_flags = decode_fdflags(fs_flags); + let mut host_fd = wasm32::__wasi_fd_t::max_value(); + if let Err(e) = decode_fd_byref(vmctx, fd) { + return return_encoded_errno(e); + } + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_open( + curfds, + dirfd, + dirflags, + path, + path_len, + oflags, + fs_rights_base, + fs_rights_inheriting, + fs_flags, + &mut host_fd, + ); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *fd={:?}", host_fd); + encode_fd_byref(vmctx, fd, host_fd); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_readdir( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + buf: wasm32::uintptr_t, + buf_len: wasm32::size_t, + cookie: wasm32::__wasi_dircookie_t, + buf_used: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_readdir(fd={:?}, buf={:#x?}, buf_len={}, cookie={:#x?}, buf_used={:#x?})", + fd, + buf, + buf_len, + cookie, + buf_used, + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let (buf, buf_len) = match decode_char_slice(vmctx, buf, buf_len) { + Ok((buf, buf_len)) => (buf, buf_len), + Err(e) => return return_encoded_errno(e), + }; + let cookie = decode_dircookie(cookie); + let mut host_buf_used = 0; + if let Err(e) = decode_usize_byref(vmctx, buf_used) { + return return_encoded_errno(e); + } + + trace!(" | (buf,buf_len)={:?}", str_for_trace(buf, buf_len)); + + let e = host::wasmtime_ssp_fd_readdir( + curfds, + fd, + buf as *mut host::void, + buf_len, + cookie, + &mut host_buf_used, + ); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *buf_used={:?}", host_buf_used); + encode_usize_byref(vmctx, buf_used, host_buf_used); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_readlink( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + buf: wasm32::uintptr_t, + buf_len: wasm32::size_t, + buf_used: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_readlink(fd={:?}, path={:#x?}, path_len={:?}, buf={:#x?}, buf_len={}, buf_used={:#x?})", + fd, + path, + path_len, + buf, + buf_len, + buf_used, + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + let (buf, buf_len) = match decode_char_slice(vmctx, buf, buf_len) { + Ok((buf, buf_len)) => (buf, buf_len), + Err(e) => return return_encoded_errno(e), + }; + let mut host_buf_used = 0; + if let Err(e) = decode_usize_byref(vmctx, buf_used) { + return return_encoded_errno(e); + } + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_readlink( + curfds, + fd, + path, + path_len, + buf, + buf_len, + &mut host_buf_used, + ); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | (buf,*buf_used)={:?}", str_for_trace(buf, host_buf_used)); + trace!(" | *buf_used={:?}", host_buf_used); + encode_usize_byref(vmctx, buf_used, host_buf_used); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_rename( + vmctx: *mut VMContext, + fd0: wasm32::__wasi_fd_t, + path0: wasm32::uintptr_t, + path_len0: wasm32::size_t, + fd1: wasm32::__wasi_fd_t, + path1: wasm32::uintptr_t, + path_len1: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_rename(fd0={:?}, path0={:#x?}, path_len0={:?}, fd1={:?}, path1={:#x?}, path_len1={:?})", + fd0, + path0, + path_len0, + fd1, + path1, + path_len1, + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd0 = decode_fd(fd0); + let (path0, path_len0) = match decode_char_slice(vmctx, path0, path_len0) { + Ok((path0, path_len0)) => (path0, path_len0), + Err(e) => return return_encoded_errno(e), + }; + let fd1 = decode_fd(fd1); + let (path1, path_len1) = match decode_char_slice(vmctx, path1, path_len1) { + Ok((path1, path_len1)) => (path1, path_len1), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path0,path_len0)={:?}", str_for_trace(path0, path_len0)); + trace!(" | (path1,path_len1)={:?}", str_for_trace(path1, path_len1)); + + let e = host::wasmtime_ssp_path_rename(curfds, fd0, path0, path_len0, fd1, path1, path_len1); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_filestat_get( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + buf: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!("fd_filestat_get(fd={:?}, buf={:#x?})", fd, buf); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let mut host_buf = std::mem::zeroed(); + if let Err(e) = decode_filestat_byref(vmctx, buf) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_fd_filestat_get(curfds, fd, &mut host_buf); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *buf={:?}", host_buf); + encode_filestat_byref(vmctx, buf, host_buf); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_filestat_set_times( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + st_atim: wasm32::__wasi_timestamp_t, + st_mtim: wasm32::__wasi_timestamp_t, + fstflags: wasm32::__wasi_fstflags_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_filestat_set_times(fd={:?}, st_atim={}, st_mtim={}, fstflags={:#x?})", + fd, + st_atim, st_mtim, + fstflags + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let st_atim = decode_timestamp(st_atim); + let st_mtim = decode_timestamp(st_mtim); + let fstflags = decode_fstflags(fstflags); + + let e = host::wasmtime_ssp_fd_filestat_set_times(curfds, fd, st_atim, st_mtim, fstflags); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn fd_filestat_set_size( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + size: wasm32::__wasi_filesize_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "fd_filestat_set_size(fd={:?}, size={})", + fd, + size + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let size = decode_filesize(size); + + let e = host::wasmtime_ssp_fd_filestat_set_size(curfds, fd, size); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_filestat_get( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + flags: wasm32::__wasi_lookupflags_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + buf: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_filestat_get(fd={:?}, flags={:?}, path={:#x?}, path_len={}, buf={:#x?})", + fd, + flags, + path, + path_len, + buf + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let flags = decode_lookupflags(flags); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + let mut host_buf = std::mem::zeroed(); + if let Err(e) = decode_filestat_byref(vmctx, buf) { + return return_encoded_errno(e); + } + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_filestat_get(curfds, fd, flags, path, path_len, &mut host_buf); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *buf={:?}", host_buf); + encode_filestat_byref(vmctx, buf, host_buf); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_filestat_set_times( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + flags: wasm32::__wasi_lookupflags_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + st_atim: wasm32::__wasi_timestamp_t, + st_mtim: wasm32::__wasi_timestamp_t, + fstflags: wasm32::__wasi_fstflags_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_filestat_set_times(fd={:?}, flags={:?}, path={:#x?}, path_len={}, st_atim={}, st_mtim={}, fstflags={:#x?})", + fd, + flags, + path, + path_len, + st_atim, st_mtim, + fstflags + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let flags = decode_lookupflags(flags); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + let st_atim = decode_timestamp(st_atim); + let st_mtim = decode_timestamp(st_mtim); + let fstflags = decode_fstflags(fstflags); + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_filestat_set_times(curfds, fd, flags, path, path_len, st_atim, st_mtim, fstflags); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_symlink( + vmctx: *mut VMContext, + path0: wasm32::uintptr_t, + path_len0: wasm32::size_t, + fd: wasm32::__wasi_fd_t, + path1: wasm32::uintptr_t, + path_len1: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_symlink(path0={:#x?}, path_len0={}, fd={:?}, path1={:#x?}, path_len1={})", + path0, + path_len0, + fd, + path1, + path_len1 + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let (path0, path_len0) = match decode_char_slice(vmctx, path0, path_len0) { + Ok((path0, path_len0)) => (path0, path_len0), + Err(e) => return return_encoded_errno(e), + }; + let fd = decode_fd(fd); + let (path1, path_len1) = match decode_char_slice(vmctx, path1, path_len1) { + Ok((path1, path_len1)) => (path1, path_len1), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path0,path_len0)={:?}", str_for_trace(path0, path_len0)); + trace!(" | (path1,path_len1)={:?}", str_for_trace(path1, path_len1)); + + let e = host::wasmtime_ssp_path_symlink(curfds, path0, path_len0, fd, path1, path_len1); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_unlink_file( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_unlink_file(fd={:?}, path={:#x?}, path_len={})", + fd, + path, + path_len + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_unlink_file(curfds, fd, path, path_len); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn path_remove_directory( + vmctx: *mut VMContext, + fd: wasm32::__wasi_fd_t, + path: wasm32::uintptr_t, + path_len: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "path_remove_directory(fd={:?}, path={:#x?}, path_len={})", + fd, + path, + path_len + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let fd = decode_fd(fd); + let (path, path_len) = match decode_char_slice(vmctx, path, path_len) { + Ok((path, path_len)) => (path, path_len), + Err(e) => return return_encoded_errno(e), + }; + + trace!(" | (path,path_len)={:?}", str_for_trace(path, path_len)); + + let e = host::wasmtime_ssp_path_remove_directory(curfds, fd, path, path_len); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn poll_oneoff( + vmctx: *mut VMContext, + in_: wasm32::uintptr_t, + out: wasm32::uintptr_t, + nsubscriptions: wasm32::size_t, + nevents: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "poll_oneoff(in={:#x?}, out={:#x?}, nsubscriptions={}, nevents={:#x?})", + in_, + out, + nsubscriptions, + nevents, + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let in_ = match decode_subscription_slice(vmctx, in_, nsubscriptions) { + Ok(in_) => in_, + Err(e) => return return_encoded_errno(e), + }; + let (out, out_len) = match decode_event_slice(vmctx, out, nsubscriptions) { + Ok((out, out_len)) => (out, out_len), + Err(e) => return return_encoded_errno(e), + }; + let mut host_out = Vec::new(); + host_out.resize(out_len, mem::zeroed()); + let mut host_nevents = 0; + if let Err(e) = decode_usize_byref(vmctx, nevents) { + return return_encoded_errno(e); + } + + assert!(in_.len() == host_out.len()); + + let e = host::wasmtime_ssp_poll_oneoff( + curfds, + in_.as_ptr(), + host_out.as_mut_ptr(), + in_.len(), + &mut host_nevents, + ); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *nevents={:?}", host_nevents); + encode_usize_byref(vmctx, nevents, host_nevents); + + host_out.truncate(host_nevents); + if log_enabled!(log::Level::Trace) { + for (index, _event) in host_out.iter().enumerate() { + // TODO: Format the output for tracing. + trace!(" | *out[{}]=...", index); + } + } + encode_event_slice(out, host_out); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn proc_exit(_vmctx: *mut VMContext, rval: u32,) -> () { + trace!("proc_exit(rval={:?})", rval); + + let rval = decode_exitcode(rval); + + // TODO: Rather than call __wasi_proc_exit here, we should trigger a + // stack unwind similar to a trap. + host::wasmtime_ssp_proc_exit(rval); + } + + pub unsafe extern "C" fn proc_raise( + _vmctx: *mut VMContext, + _sig: wasm32::__wasi_signal_t, + ) -> wasm32::__wasi_errno_t { + unimplemented!("__wasi_proc_raise"); + } + + pub unsafe extern "C" fn random_get( + vmctx: *mut VMContext, + buf: wasm32::uintptr_t, + buf_len: wasm32::size_t, + ) -> wasm32::__wasi_errno_t { + trace!("random_get(buf={:#x?}, buf_len={:?})", buf, buf_len); + + let vmctx = &mut *vmctx; + let (buf, buf_len) = match decode_char_slice(vmctx, buf, buf_len) { + Ok((buf, buf_len)) => (buf, buf_len), + Err(e) => return return_encoded_errno(e), + }; + + let e = host::wasmtime_ssp_random_get(buf as *mut host::void, buf_len); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn sched_yield(_vmctx: *mut VMContext,) -> wasm32::__wasi_errno_t { + let e = host::wasmtime_ssp_sched_yield(); + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn sock_recv( + vmctx: *mut VMContext, + sock: wasm32::__wasi_fd_t, + ri_data: wasm32::uintptr_t, + ri_data_len: wasm32::size_t, + ri_flags: wasm32::__wasi_riflags_t, + ro_datalen: wasm32::uintptr_t, + ro_flags: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "sock_recv(sock={:?}, ri_data={:#x?}, ri_data_len={}, ri_flags={:#x?}, ro_datalen={:#x?}, ro_flags={:#x?})", + sock, + ri_data, ri_data_len, ri_flags, + ro_datalen, ro_flags + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let sock = decode_fd(sock); + let ri_data = match decode_iovec_slice(vmctx, ri_data, ri_data_len) { + Ok(ri_data) => ri_data, + Err(e) => return return_encoded_errno(e), + }; + let ri_flags = decode_riflags(ri_flags); + let mut host_ro_datalen = 0; + if let Err(e) = decode_usize_byref(vmctx, ro_datalen) { + return return_encoded_errno(e); + } + let mut host_ro_flags = 0; + if let Err(e) = decode_roflags_byref(vmctx, ro_flags) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_sock_recv(curfds, sock, ri_data.as_ptr(), ri_data.len(), ri_flags, + &mut host_ro_datalen, &mut host_ro_flags); + + if u32::from(e) == host::__WASI_ESUCCESS { + // TODO: Format the output for tracing. + trace!(" | *ro_datalen={}", host_ro_datalen); + trace!(" | *ro_flags={}", host_ro_flags); + encode_usize_byref(vmctx, ro_datalen, host_ro_datalen); + encode_roflags_byref(vmctx, ro_flags, host_ro_flags); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn sock_send( + vmctx: *mut VMContext, + sock: wasm32::__wasi_fd_t, + si_data: wasm32::uintptr_t, + si_data_len: wasm32::size_t, + si_flags: wasm32::__wasi_siflags_t, + so_datalen: wasm32::uintptr_t, + ) -> wasm32::__wasi_errno_t { + trace!( + "sock_send(sock={:?}, si_data={:#x?}, si_data_len={}, si_flags={:#x?}, so_datalen={:#x?})", + sock, + si_data, si_data_len, si_flags, so_datalen, + ); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let sock = decode_fd(sock); + let si_data = match decode_ciovec_slice(vmctx, si_data, si_data_len) { + Ok(si_data) => si_data, + Err(e) => return return_encoded_errno(e), + }; + let si_flags = decode_siflags(si_flags); + let mut host_so_datalen = 0; + if let Err(e) = decode_usize_byref(vmctx, so_datalen) { + return return_encoded_errno(e); + } + + let e = host::wasmtime_ssp_sock_send(curfds, sock, si_data.as_ptr(), si_data.len(), si_flags, &mut host_so_datalen); + + if u32::from(e) == host::__WASI_ESUCCESS { + trace!(" | *so_datalen={:?}", host_so_datalen); + encode_usize_byref(vmctx, so_datalen, host_so_datalen); + } + + return_encoded_errno(e) + } + + pub unsafe extern "C" fn sock_shutdown( + vmctx: *mut VMContext, + sock: wasm32::__wasi_fd_t, + how: wasm32::__wasi_sdflags_t, + ) -> wasm32::__wasi_errno_t { + trace!("sock_shutdown(sock={:?}, how={:?})", sock, how); + + let vmctx = &mut *vmctx; + let curfds = get_curfds(vmctx); + let sock = decode_fd(sock); + let how = decode_sdflags(how); + + let e = host::wasmtime_ssp_sock_shutdown(curfds, sock, how); + + return_encoded_errno(e) + } +} diff --git a/wasmtime-wasi-c/src/translate.rs b/wasmtime-wasi-c/src/translate.rs new file mode 100644 index 000000000000..2eb0c811da0d --- /dev/null +++ b/wasmtime-wasi-c/src/translate.rs @@ -0,0 +1,573 @@ +use cast; +use cast::From as _0; +use host; +use std::mem::{align_of, size_of, zeroed}; +use std::slice; +use wasm32; +use wasmtime_runtime::{Export, VMContext}; + +/// Translate a wasm pointer into a native pointer. +/// +/// This is unsafe due to trusting the contents of vmctx. The pointer result +/// is bounds and alignment checked. +unsafe fn decode_ptr( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: usize, + align: usize, +) -> Result<*mut u8, host::__wasi_errno_t> { + match vmctx.lookup_global_export("memory") { + Some(Export::Memory { + definition, + vmctx: _, + memory: _, + }) => { + if len > 0 { + // Check for overflow within the access. + let last = match (ptr as usize).checked_add(len - 1) { + Some(sum) => sum, + None => { + println!("!!! overflow"); + return Err(host::__WASI_EFAULT as host::__wasi_errno_t); + } + }; + // Check for out of bounds. + if last >= (*definition).current_length { + println!("!!! out of bounds"); + return Err(host::__WASI_EFAULT as host::__wasi_errno_t); + } + } + // Check alignment. + if (ptr as usize) % align != 0 { + println!("!!! bad alignment: {} % {}", ptr, align); + return Err(host::__WASI_EINVAL as host::__wasi_errno_t); + } + // Ok, translate the address. + Ok((((*definition).base as usize) + (ptr as usize)) as *mut u8) + } + // No export named "__wasi_memory", or the export isn't a memory. + // FIXME: Is EINVAL the best code here? + x => { + println!( + "!!! no export named __wasi_memory, or the export isn't a mem: {:?}", + x + ); + Err(host::__WASI_EINVAL as host::__wasi_errno_t) + } + } +} + +unsafe fn decode_ptr_to( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, +) -> Result<*mut T, host::__wasi_errno_t> { + decode_ptr(vmctx, ptr, size_of::(), align_of::()).map(|ptr| ptr as *mut T) +} + +unsafe fn decode_pointee( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + // Check bounds and alignment. + decode_ptr_to::(vmctx, ptr)?; + Ok(()) +} + +pub unsafe fn encode_pointee(vmctx: &mut VMContext, ptr: wasm32::uintptr_t, t: T) { + // Bounds and alignment are checked in `decode_pointee`. + let ptr = decode_ptr_to::(vmctx, ptr).unwrap(); + + ptr.write(t); +} + +pub unsafe fn decode_slice_of( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: wasm32::size_t, +) -> Result<(*mut T, usize), host::__wasi_errno_t> { + let len = cast::usize(len); + + let ptr = decode_ptr( + vmctx, + ptr, + size_of::().checked_mul(len).unwrap(), + align_of::(), + )? as *mut T; + + Ok((ptr, len)) +} + +pub fn encode_usize(len: usize) -> wasm32::size_t { + cast::u32(len).unwrap() +} + +pub fn encode_device(device: host::__wasi_device_t) -> wasm32::__wasi_device_t { + device +} + +pub fn encode_inode(inode: host::__wasi_inode_t) -> wasm32::__wasi_inode_t { + inode +} + +pub fn encode_linkcount(linkcount: host::__wasi_linkcount_t) -> wasm32::__wasi_linkcount_t { + linkcount +} + +pub fn decode_userdata(userdata: wasm32::__wasi_userdata_t) -> host::__wasi_userdata_t { + userdata +} + +pub fn encode_userdata(userdata: host::__wasi_userdata_t) -> wasm32::__wasi_userdata_t { + userdata +} + +pub fn decode_eventtype(eventtype: wasm32::__wasi_eventtype_t) -> host::__wasi_eventtype_t { + eventtype +} + +pub fn encode_eventtype(eventtype: host::__wasi_eventtype_t) -> wasm32::__wasi_eventtype_t { + eventtype +} + +pub fn decode_filesize(filesize: wasm32::__wasi_filesize_t) -> host::__wasi_filesize_t { + filesize +} + +pub fn encode_filesize(filesize: host::__wasi_filesize_t) -> wasm32::__wasi_filesize_t { + filesize +} + +pub fn encode_eventrwflags( + eventrwflags: host::__wasi_eventrwflags_t, +) -> wasm32::__wasi_eventrwflags_t { + eventrwflags +} + +pub fn decode_subclockflags( + subclockflags: wasm32::__wasi_subclockflags_t, +) -> host::__wasi_subclockflags_t { + subclockflags +} + +pub fn decode_fd(fd: wasm32::__wasi_fd_t) -> host::__wasi_fd_t { + fd +} + +pub fn decode_filedelta(filedelta: wasm32::__wasi_filedelta_t) -> host::__wasi_filedelta_t { + filedelta +} + +pub fn decode_whence(whence: wasm32::__wasi_whence_t) -> host::__wasi_whence_t { + whence +} + +pub fn decode_clockid(clockid: wasm32::__wasi_clockid_t) -> host::__wasi_clockid_t { + clockid +} + +pub fn decode_timestamp(timestamp: wasm32::__wasi_timestamp_t) -> host::__wasi_timestamp_t { + timestamp +} + +pub fn encode_timestamp(timestamp: host::__wasi_timestamp_t) -> wasm32::__wasi_timestamp_t { + timestamp +} + +pub fn decode_exitcode(exitcode: wasm32::__wasi_exitcode_t) -> host::__wasi_exitcode_t { + exitcode +} + +pub fn decode_lookupflags(lookupflags: wasm32::__wasi_lookupflags_t) -> host::__wasi_lookupflags_t { + lookupflags +} + +pub fn decode_oflags(oflags: wasm32::__wasi_oflags_t) -> host::__wasi_oflags_t { + oflags +} + +pub fn decode_advice(advice: wasm32::__wasi_advice_t) -> host::__wasi_advice_t { + advice +} + +pub fn decode_dircookie(dircookie: wasm32::__wasi_dircookie_t) -> host::__wasi_dircookie_t { + dircookie +} + +pub fn encode_preopentype(preopentype: host::__wasi_preopentype_t) -> wasm32::__wasi_preopentype_t { + preopentype +} + +pub fn encode_filetype(filetype: host::__wasi_filetype_t) -> wasm32::__wasi_filetype_t { + filetype +} + +pub fn decode_fstflags(fstflags: wasm32::__wasi_fstflags_t) -> host::__wasi_fstflags_t { + fstflags +} + +#[allow(dead_code)] +pub fn encode_fstflags(fstflags: host::__wasi_fstflags_t) -> wasm32::__wasi_fstflags_t { + fstflags +} + +pub fn decode_fdflags(fdflags: wasm32::__wasi_fdflags_t) -> host::__wasi_fdflags_t { + fdflags +} + +pub fn encode_fdflags(fdflags: host::__wasi_fdflags_t) -> wasm32::__wasi_fdflags_t { + fdflags +} + +pub fn decode_sdflags(sdflags: wasm32::__wasi_sdflags_t) -> host::__wasi_sdflags_t { + sdflags +} + +pub fn decode_rights(rights: wasm32::__wasi_rights_t) -> host::__wasi_rights_t { + rights +} + +pub fn encode_rights(rights: host::__wasi_rights_t) -> wasm32::__wasi_rights_t { + rights +} + +pub fn decode_riflags(riflags: wasm32::__wasi_riflags_t) -> host::__wasi_riflags_t { + riflags +} + +pub fn decode_siflags(siflags: wasm32::__wasi_siflags_t) -> host::__wasi_siflags_t { + siflags +} + +pub unsafe fn decode_char_slice( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: wasm32::size_t, +) -> Result<(*mut host::char, usize), host::__wasi_errno_t> { + decode_slice_of::(vmctx, ptr, len) +} + +pub unsafe fn decode_charstar_slice( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + count: wasm32::size_t, +) -> Result<(*mut wasm32::uintptr_t, usize), host::__wasi_errno_t> { + decode_slice_of::(vmctx, ptr, count) +} + +pub unsafe fn encode_charstar_slice( + ptr: *mut wasm32::uintptr_t, + host_vec: Vec<*mut libc::c_char>, + guest_base: wasm32::uintptr_t, + host_base: *mut libc::c_char, +) { + for (i, host) in host_vec.iter().enumerate() { + let guest = if host.is_null() { + 0 + } else { + guest_base + (*host as usize - host_base as usize) as wasm32::uintptr_t + }; + ptr.add(i).write(guest); + } +} + +pub unsafe fn decode_ciovec( + vmctx: &mut VMContext, + ciovec: &wasm32::__wasi_ciovec_t, +) -> Result { + let len = cast::usize(ciovec.buf_len); + Ok(host::__wasi_ciovec_t { + buf: decode_ptr(vmctx, ciovec.buf, len, 1)? as *const host::void, + buf_len: len, + }) +} + +pub unsafe fn decode_iovec( + vmctx: &mut VMContext, + iovec: &wasm32::__wasi_iovec_t, +) -> Result { + let len = cast::usize(iovec.buf_len); + Ok(host::__wasi_iovec_t { + buf: decode_ptr(vmctx, iovec.buf, len, 1)? as *mut host::void, + buf_len: len, + }) +} + +pub unsafe fn decode_ciovec_slice( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: wasm32::size_t, +) -> Result, host::__wasi_errno_t> { + let slice = decode_slice_of::(vmctx, ptr, len)?; + let slice = slice::from_raw_parts(slice.0, slice.1); + slice.iter().map(|iov| decode_ciovec(vmctx, iov)).collect() +} + +pub unsafe fn decode_iovec_slice( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: wasm32::size_t, +) -> Result, host::__wasi_errno_t> { + let slice = decode_slice_of::(vmctx, ptr, len)?; + let slice = slice::from_raw_parts(slice.0, slice.1); + slice.iter().map(|iov| decode_iovec(vmctx, iov)).collect() +} + +pub fn decode_subscription( + guest_subscription: wasm32::__wasi_subscription_t, +) -> Result { + let mut host_subscription = host::__wasi_subscription_t { + userdata: decode_userdata(guest_subscription.userdata), + type_: decode_eventtype(guest_subscription.type_), + u: unsafe { zeroed() }, + }; + + match guest_subscription.type_ { + wasm32::__WASI_EVENTTYPE_CLOCK => unsafe { + host_subscription.u.clock = + host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t { + identifier: decode_userdata(guest_subscription.u.clock.identifier), + clock_id: decode_clockid(guest_subscription.u.clock.clock_id), + timeout: decode_timestamp(guest_subscription.u.clock.timeout), + precision: decode_timestamp(guest_subscription.u.clock.precision), + flags: decode_subclockflags(guest_subscription.u.clock.flags), + }; + }, + wasm32::__WASI_EVENTTYPE_FD_READ | wasm32::__WASI_EVENTTYPE_FD_WRITE => unsafe { + host_subscription + .u + .fd_readwrite = + host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t { + fd: decode_fd(guest_subscription.u.fd_readwrite.fd), + } + }, + _ => return Err(host::__WASI_EINVAL as host::__wasi_errno_t), + }; + + Ok(host_subscription) +} + +pub unsafe fn decode_subscription_slice( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: wasm32::size_t, +) -> Result, host::__wasi_errno_t> { + let slice = decode_slice_of::(vmctx, ptr, len)?; + let slice = slice::from_raw_parts(slice.0, slice.1); + slice + .iter() + .map(|subscription| decode_subscription(*subscription)) + .collect() +} + +pub fn encode_event(host_event: host::__wasi_event_t) -> wasm32::__wasi_event_t { + let mut guest_event = wasm32::__wasi_event_t { + userdata: encode_userdata(host_event.userdata), + error: encode_errno(host_event.error), + type_: encode_eventtype(host_event.type_), + u: unsafe { zeroed() }, + __bindgen_padding_0: 0, + }; + + match u32::from(host_event.type_) { + host::__WASI_EVENTTYPE_CLOCK => {} + host::__WASI_EVENTTYPE_FD_READ | host::__WASI_EVENTTYPE_FD_WRITE => unsafe { + guest_event.u.fd_readwrite = + wasm32::__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t { + nbytes: encode_filesize(host_event.u.fd_readwrite.nbytes), + flags: encode_eventrwflags(host_event.u.fd_readwrite.flags), + __bindgen_padding_0: zeroed(), + } + }, + _ => panic!("unrecognized event type"), + }; + + guest_event +} + +pub unsafe fn decode_event_slice( + vmctx: &mut VMContext, + ptr: wasm32::uintptr_t, + len: wasm32::size_t, +) -> Result<(*mut wasm32::__wasi_event_t, usize), host::__wasi_errno_t> { + decode_slice_of::(vmctx, ptr, len) +} + +pub unsafe fn encode_event_slice( + ptr: *mut wasm32::__wasi_event_t, + host_vec: Vec, +) { + for (i, host) in host_vec.iter().enumerate() { + let guest = encode_event(*host); + + ptr.add(i * size_of::()) + .write(guest); + } +} + +pub unsafe fn decode_fd_byref( + vmctx: &mut VMContext, + fd_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, fd_ptr) +} + +pub unsafe fn encode_fd_byref( + vmctx: &mut VMContext, + fd_ptr: wasm32::uintptr_t, + fd: host::__wasi_fd_t, +) { + encode_pointee::(vmctx, fd_ptr, wasm32::size_t::cast(fd)) +} + +pub unsafe fn decode_timestamp_byref( + vmctx: &mut VMContext, + timestamp_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, timestamp_ptr) +} + +pub unsafe fn encode_timestamp_byref( + vmctx: &mut VMContext, + timestamp_ptr: wasm32::uintptr_t, + host_timestamp: host::__wasi_timestamp_t, +) { + encode_pointee::( + vmctx, + timestamp_ptr, + wasm32::__wasi_timestamp_t::cast(host_timestamp), + ) +} + +pub unsafe fn decode_filesize_byref( + vmctx: &mut VMContext, + filesize_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, filesize_ptr) +} + +pub unsafe fn encode_filesize_byref( + vmctx: &mut VMContext, + filesize_ptr: wasm32::uintptr_t, + host_filesize: host::__wasi_filesize_t, +) { + encode_pointee::( + vmctx, + filesize_ptr, + wasm32::__wasi_filesize_t::cast(host_filesize), + ) +} + +pub unsafe fn decode_roflags_byref( + vmctx: &mut VMContext, + roflags_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, roflags_ptr) +} + +pub unsafe fn encode_roflags_byref( + vmctx: &mut VMContext, + roflags_ptr: wasm32::uintptr_t, + host_roflags: host::__wasi_roflags_t, +) { + encode_pointee::( + vmctx, + roflags_ptr, + wasm32::__wasi_roflags_t::cast(host_roflags), + ) +} + +pub unsafe fn decode_usize_byref( + vmctx: &mut VMContext, + usize_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, usize_ptr) +} + +pub unsafe fn encode_usize_byref( + vmctx: &mut VMContext, + usize_ptr: wasm32::uintptr_t, + host_usize: usize, +) { + encode_pointee::(vmctx, usize_ptr, wasm32::size_t::cast(host_usize).unwrap()) +} + +pub unsafe fn decode_prestat_byref( + vmctx: &mut VMContext, + prestat_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, prestat_ptr)?; + Ok(()) +} + +pub unsafe fn encode_prestat_byref( + vmctx: &mut VMContext, + prestat_ptr: wasm32::uintptr_t, + host_prestat: host::__wasi_prestat_t, +) { + let wasm32_prestat = wasm32::__wasi_prestat_t { + pr_type: encode_preopentype(host_prestat.pr_type), + u: wasm32::__wasi_prestat_t___wasi_prestat_u { + dir: wasm32::__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t { + pr_name_len: encode_usize(host_prestat.u.dir.pr_name_len), + }, + }, + }; + + encode_pointee::(vmctx, prestat_ptr, wasm32_prestat) +} + +pub unsafe fn decode_fdstat_byref( + vmctx: &mut VMContext, + fdstat_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, fdstat_ptr)?; + Ok(()) +} + +pub unsafe fn encode_fdstat_byref( + vmctx: &mut VMContext, + fdstat_ptr: wasm32::uintptr_t, + host_fdstat: host::__wasi_fdstat_t, +) { + let wasm32_fdstat = wasm32::__wasi_fdstat_t { + fs_filetype: encode_filetype(host_fdstat.fs_filetype), + fs_flags: encode_fdflags(host_fdstat.fs_flags), + __bindgen_padding_0: 0, + fs_rights_base: encode_rights(host_fdstat.fs_rights_base), + fs_rights_inheriting: encode_rights(host_fdstat.fs_rights_inheriting), + }; + + encode_pointee::(vmctx, fdstat_ptr, wasm32_fdstat) +} + +pub unsafe fn decode_filestat_byref( + vmctx: &mut VMContext, + filestat_ptr: wasm32::uintptr_t, +) -> Result<(), host::__wasi_errno_t> { + decode_pointee::(vmctx, filestat_ptr)?; + Ok(()) +} + +pub unsafe fn encode_filestat_byref( + vmctx: &mut VMContext, + filestat_ptr: wasm32::uintptr_t, + host_filestat: host::__wasi_filestat_t, +) { + let wasm32_filestat = wasm32::__wasi_filestat_t { + st_dev: encode_device(host_filestat.st_dev), + st_ino: encode_inode(host_filestat.st_ino), + st_filetype: encode_filetype(host_filestat.st_filetype), + st_nlink: encode_linkcount(host_filestat.st_nlink), + st_size: encode_filesize(host_filestat.st_size), + st_atim: encode_timestamp(host_filestat.st_atim), + st_mtim: encode_timestamp(host_filestat.st_mtim), + st_ctim: encode_timestamp(host_filestat.st_ctim), + }; + + encode_pointee::(vmctx, filestat_ptr, wasm32_filestat) +} + +pub fn encode_errno(e: host::__wasi_errno_t) -> wasm32::__wasi_errno_t { + assert!(e <= wasm32::__WASI_ENOTCAPABLE); + e +} diff --git a/wasmtime-wasi-c/src/wasm32.rs b/wasmtime-wasi-c/src/wasm32.rs new file mode 100644 index 000000000000..8847674ce83e --- /dev/null +++ b/wasmtime-wasi-c/src/wasm32.rs @@ -0,0 +1,1442 @@ +//! WASI types as defined in wasm32. This file was originally generated +//! by running bindgen over wasi/core.h with a wasm32 target, and the content +//! still largely reflects that, however it's been heavily modified, to +//! be host-independent, to avoid exposing libc implementation details, +//! to clean up cases where the headers use complex preprocessor macros, +//! and to + +#![allow(non_camel_case_types)] +#![allow(dead_code)] + +// C types +pub type char = i8; +pub type schar = i8; +pub type uchar = u8; +pub type short = i16; +pub type ushort = u16; +pub type int = i32; +pub type uint = u32; +pub type long = i32; +pub type ulong = u32; +pub type longlong = i64; +pub type ulonglong = u64; + +// libc stdint types +pub type int8_t = i8; +pub type uint8_t = u8; +pub type int16_t = i16; +pub type uint16_t = u16; +pub type int32_t = i32; +pub type uint32_t = u32; +pub type int64_t = i64; +pub type uint64_t = u64; +pub type intmax_t = i64; +pub type uintmax_t = u64; +pub type int_least8_t = i8; +pub type int_least16_t = i16; +pub type int_least32_t = i32; +pub type int_least64_t = i64; +pub type uint_least8_t = u8; +pub type uint_least16_t = u16; +pub type uint_least32_t = u32; +pub type uint_least64_t = u64; +pub type int_fast8_t = i8; +pub type int_fast16_t = i32; +pub type int_fast32_t = i32; +pub type int_fast64_t = i64; +pub type uint_fast8_t = u8; +pub type uint_fast16_t = u32; +pub type uint_fast32_t = u32; +pub type uint_fast64_t = u64; +pub type size_t = ulong; +pub type intptr_t = long; +pub type uintptr_t = ulong; +pub type wchar_t = i32; + +// libc types +pub type dev_t = u64; +pub type uid_t = u32; +pub type gid_t = u32; +pub type ino_t = u64; +pub type ino64_t = u64; +pub type mode_t = u32; +pub type nlink_t = u64; +pub type off_t = i64; +pub type off64_t = i64; +pub type pid_t = i32; +pub type clock_t = i64; +pub type rlim_t = u64; +pub type rlim64_t = u64; +pub type id_t = u32; +pub type time_t = i64; +pub type useconds_t = u32; +pub type suseconds_t = i64; +pub type daddr_t = i32; +pub type key_t = i32; +pub type clockid_t = i32; +pub type timer_t = uintptr_t; // *mut ::std::os::raw::c_void +pub type blksize_t = i64; +pub type blkcnt_t = i64; +pub type blkcnt64_t = i64; +pub type fsblkcnt_t = u64; +pub type fsblkcnt64_t = u64; +pub type fsfilcnt_t = u64; +pub type fsfilcnt64_t = u64; +pub type fsword_t = i64; +pub type ssize_t = i32; +pub type loff_t = off64_t; +pub type caddr_t = uintptr_t; // *mut i8 +pub type socklen_t = u32; +pub type sig_atomic_t = i32; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct fsid_t { + pub __val: [i32; 2usize], +} +#[allow(non_snake_case)] +#[test] +fn bindgen_test_layout_fsid_t() { + assert_eq!( + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(fsid_t)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(fsid_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).__val as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(fsid_t), + "::", + stringify!(__val) + ) + ); +} + +// WASI types +pub type __wasi_advice_t = u8; +pub type __wasi_clockid_t = u32; +pub type __wasi_device_t = u64; +pub type __wasi_dircookie_t = u64; +pub type __wasi_errno_t = u16; +pub type __wasi_eventrwflags_t = u16; +pub type __wasi_eventtype_t = u8; +pub type __wasi_exitcode_t = u32; +pub type __wasi_fd_t = u32; +pub type __wasi_fdflags_t = u16; +pub type __wasi_fdsflags_t = u16; +pub type __wasi_filedelta_t = i64; +pub type __wasi_filesize_t = u64; +pub type __wasi_filetype_t = u8; +pub type __wasi_preopentype_t = u8; +pub type __wasi_fstflags_t = u16; +pub type __wasi_inode_t = u64; +pub type __wasi_linkcount_t = u32; +pub type __wasi_lookupflags_t = u32; +pub type __wasi_oflags_t = u16; +pub type __wasi_riflags_t = u16; +pub type __wasi_rights_t = u64; +pub type __wasi_roflags_t = u16; +pub type __wasi_sdflags_t = u8; +pub type __wasi_siflags_t = u16; +pub type __wasi_signal_t = u8; +pub type __wasi_subclockflags_t = u16; +pub type __wasi_timestamp_t = u64; +pub type __wasi_userdata_t = u64; +pub type __wasi_whence_t = u8; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_dirent_t { + pub d_next: __wasi_dircookie_t, + pub d_ino: __wasi_inode_t, + pub d_namlen: u32, + pub d_type: __wasi_filetype_t, + pub __bindgen_padding_0: [u8; 3usize], +} +#[test] +fn bindgen_test_layout_wasi_dirent_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_dirent_t>(), + 24usize, + concat!("Size of: ", stringify!(__wasi_dirent_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_next as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_dirent_t), + "::", + stringify!(d_next) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_ino as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_dirent_t), + "::", + stringify!(d_ino) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_namlen as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(__wasi_dirent_t), + "::", + stringify!(d_namlen) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_dirent_t>())).d_type as *const _ as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(__wasi_dirent_t), + "::", + stringify!(d_type) + ) + ); +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __wasi_event_t { + pub userdata: __wasi_userdata_t, + pub error: __wasi_errno_t, + pub type_: __wasi_eventtype_t, + pub __bindgen_padding_0: u32, + pub u: __wasi_event_t___wasi_event_u, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __wasi_event_t___wasi_event_u { + pub fd_readwrite: __wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t, + _bindgen_union_align: [u64; 2usize], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t { + pub nbytes: __wasi_filesize_t, + pub flags: __wasi_eventrwflags_t, + pub __bindgen_padding_0: [u16; 3usize], +} +#[test] +fn bindgen_test_layout___wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>(), + 16usize, + concat!( + "Size of: ", + stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t) + ) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>(), + 8usize, + concat!( + "Alignment of ", + stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>())) + .nbytes as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t), + "::", + stringify!(nbytes) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t>())) + .flags as *const _ as usize + }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t___wasi_event_u___wasi_event_u_fd_readwrite_t), + "::", + stringify!(flags) + ) + ); +} +#[test] +fn bindgen_test_layout___wasi_event_t___wasi_event_u() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t___wasi_event_u>(), + 16usize, + concat!("Size of: ", stringify!(__wasi_event_t___wasi_event_u)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_event_t___wasi_event_u>(), + 8usize, + concat!("Alignment of ", stringify!(__wasi_event_t___wasi_event_u)) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t___wasi_event_u>())).fd_readwrite as *const _ + as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t___wasi_event_u), + "::", + stringify!(fd_readwrite) + ) + ); +} +#[test] +fn bindgen_test_layout___wasi_event_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t>(), + 32usize, + concat!("Size of: ", stringify!(__wasi_event_t)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_event_t>(), + 8usize, + concat!("Alignment of ", stringify!(__wasi_event_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).userdata as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(userdata) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).error as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(error) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).type_ as *const _ as usize }, + 10usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(type_) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).u as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(u) + ) + ); +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __wasi_prestat_t { + pub pr_type: __wasi_preopentype_t, + pub u: __wasi_prestat_t___wasi_prestat_u, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __wasi_prestat_t___wasi_prestat_u { + pub dir: __wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t { + pub pr_name_len: size_t, +} +#[test] +fn bindgen_test_layout___wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t>(), + 4usize, + concat!( + "Size of: ", + stringify!(__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t) + ) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t>(), + 4usize, + concat!( + "Alignment of ", + stringify!(__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t>())) + .pr_name_len as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t), + "::", + stringify!(pr_name_len) + ) + ); +} +#[test] +fn bindgen_test_layout___wasi_prestat_t___wasi_prestat_u() { + assert_eq!( + ::std::mem::size_of::<__wasi_prestat_t___wasi_prestat_u>(), + 4usize, + concat!("Size of: ", stringify!(__wasi_prestat_t___wasi_prestat_u)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_prestat_t___wasi_prestat_u>(), + 4usize, + concat!( + "Alignment of ", + stringify!(__wasi_prestat_t___wasi_prestat_u) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_prestat_t___wasi_prestat_u>())).dir as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_prestat_t___wasi_prestat_u), + "::", + stringify!(dir) + ) + ); +} +#[test] +fn bindgen_test_layout___wasi_prestat_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_prestat_t>(), + 8usize, + concat!("Size of: ", stringify!(__wasi_prestat_t)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_prestat_t>(), + 4usize, + concat!("Alignment of ", stringify!(__wasi_prestat_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_prestat_t>())).pr_type as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_prestat_t), + "::", + stringify!(pr_type) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_prestat_t>())).u as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(__wasi_prestat_t), + "::", + stringify!(u) + ) + ); +} +#[allow(non_snake_case)] +#[repr(C)] +#[derive(Copy, Clone)] +pub union __wasi_event_t__bindgen_ty_1 { + pub fd_readwrite: __wasi_event_t__bindgen_ty_1__bindgen_ty_1, + _bindgen_union_align: [u64; 2usize], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_event_t__bindgen_ty_1__bindgen_ty_1 { + pub nbytes: __wasi_filesize_t, + pub flags: __wasi_eventrwflags_t, + pub __bindgen_padding_0: [u16; 3usize], +} +#[allow(non_snake_case)] +#[test] +fn bindgen_test_layout_wasi_event_t__bindgen_ty_1__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t__bindgen_ty_1__bindgen_ty_1>(), + 16usize, + concat!( + "Size of: ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_1) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_1>())).nbytes + as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_1), + "::", + stringify!(nbytes) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_1>())).flags as *const _ + as usize + }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_1), + "::", + stringify!(flags) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_event_t__bindgen_ty_1__bindgen_ty_2 { + pub signal: __wasi_signal_t, + pub exitcode: __wasi_exitcode_t, +} +#[allow(non_snake_case)] +#[test] +fn bindgen_test_layout_wasi_event_t__bindgen_ty_1__bindgen_ty_2() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>(), + 8usize, + concat!( + "Size of: ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2) + ) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>(), + 4usize, + concat!( + "Alignment of ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>())).signal + as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2), + "::", + stringify!(signal) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1__bindgen_ty_2>())).exitcode + as *const _ as usize + }, + 4usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t__bindgen_ty_1__bindgen_ty_2), + "::", + stringify!(exitcode) + ) + ); +} +#[allow(non_snake_case)] +#[test] +fn bindgen_test_layout_wasi_event_t__bindgen_ty_1() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t__bindgen_ty_1>(), + 16usize, + concat!("Size of: ", stringify!(__wasi_event_t__bindgen_ty_1)) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_event_t__bindgen_ty_1>())).fd_readwrite as *const _ + as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t__bindgen_ty_1), + "::", + stringify!(fd_readwrite) + ) + ); +} +#[test] +fn bindgen_test_layout_wasi_event_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_event_t>(), + 32usize, + concat!("Size of: ", stringify!(__wasi_event_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).userdata as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(userdata) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).error as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(error) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_event_t>())).type_ as *const _ as usize }, + 10usize, + concat!( + "Offset of field: ", + stringify!(__wasi_event_t), + "::", + stringify!(type_) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_fdstat_t { + pub fs_filetype: __wasi_filetype_t, + pub fs_flags: __wasi_fdflags_t, + pub __bindgen_padding_0: u32, + pub fs_rights_base: __wasi_rights_t, + pub fs_rights_inheriting: __wasi_rights_t, +} +#[test] +fn bindgen_test_layout_wasi_fdstat_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_fdstat_t>(), + 24usize, + concat!("Size of: ", stringify!(__wasi_fdstat_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_filetype as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_fdstat_t), + "::", + stringify!(fs_filetype) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_flags as *const _ as usize }, + 2usize, + concat!( + "Offset of field: ", + stringify!(__wasi_fdstat_t), + "::", + stringify!(fs_flags) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_rights_base as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_fdstat_t), + "::", + stringify!(fs_rights_base) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_fdstat_t>())).fs_rights_inheriting as *const _ as usize + }, + 16usize, + concat!( + "Offset of field: ", + stringify!(__wasi_fdstat_t), + "::", + stringify!(fs_rights_inheriting) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_filestat_t { + pub st_dev: __wasi_device_t, + pub st_ino: __wasi_inode_t, + pub st_filetype: __wasi_filetype_t, + pub st_nlink: __wasi_linkcount_t, + pub st_size: __wasi_filesize_t, + pub st_atim: __wasi_timestamp_t, + pub st_mtim: __wasi_timestamp_t, + pub st_ctim: __wasi_timestamp_t, +} +#[test] +fn bindgen_test_layout_wasi_filestat_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_filestat_t>(), + 56usize, + concat!("Size of: ", stringify!(__wasi_filestat_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_dev as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_dev) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_ino as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_ino) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_filetype as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_filetype) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_nlink as *const _ as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_nlink) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_size as *const _ as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_size) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_atim as *const _ as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_atim) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_mtim as *const _ as usize }, + 40usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_mtim) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_filestat_t>())).st_ctim as *const _ as usize }, + 48usize, + concat!( + "Offset of field: ", + stringify!(__wasi_filestat_t), + "::", + stringify!(st_ctim) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_ciovec_t { + pub buf: uintptr_t, // *const ::std::os::raw::c_void + pub buf_len: size_t, +} +#[test] +fn bindgen_test_layout_wasi_ciovec_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_ciovec_t>(), + 8usize, + concat!("Size of: ", stringify!(__wasi_ciovec_t)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_ciovec_t>(), + 4usize, + concat!("Alignment of ", stringify!(__wasi_ciovec_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_ciovec_t>())).buf as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_ciovec_t), + "::", + stringify!(buf) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_ciovec_t>())).buf_len as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(__wasi_ciovec_t), + "::", + stringify!(buf_len) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_iovec_t { + pub buf: uintptr_t, // *mut ::std::os::raw::c_void + pub buf_len: size_t, +} +#[test] +fn bindgen_test_layout_wasi_iovec_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_iovec_t>(), + 8usize, + concat!("Size of: ", stringify!(__wasi_iovec_t)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_iovec_t>(), + 4usize, + concat!("Alignment of ", stringify!(__wasi_iovec_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_iovec_t>())).buf as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_iovec_t), + "::", + stringify!(buf) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_iovec_t>())).buf_len as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(__wasi_iovec_t), + "::", + stringify!(buf_len) + ) + ); +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct __wasi_subscription_t { + pub userdata: __wasi_userdata_t, + pub type_: __wasi_eventtype_t, + pub __bindgen_padding_0: u32, + pub u: __wasi_subscription_t___wasi_subscription_u, +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union __wasi_subscription_t___wasi_subscription_u { + pub clock: __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + pub fd_readwrite: + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, + _bindgen_union_align: [u64; 5usize], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t { + pub identifier: __wasi_userdata_t, + pub clock_id: __wasi_clockid_t, + pub __bindgen_padding_0: u32, + pub timeout: __wasi_timestamp_t, + pub precision: __wasi_timestamp_t, + pub flags: __wasi_subclockflags_t, + pub __bindgen_padding_1: [u16; 3usize], +} +#[test] +fn bindgen_test_layout___wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t() { + assert_eq!( + ::std::mem::size_of::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >(), + 40usize, + concat!( + "Size of: ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t) + ) + ); + assert_eq!( + ::std::mem::align_of::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >(), + 8usize, + concat!( + "Alignment of ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >())) + .identifier as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), + "::", + stringify!(identifier) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >())) + .clock_id as *const _ as usize + }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), + "::", + stringify!(clock_id) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >())) + .timeout as *const _ as usize + }, + 16usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), + "::", + stringify!(timeout) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >())) + .precision as *const _ as usize + }, + 24usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), + "::", + stringify!(precision) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t, + >())) + .flags as *const _ as usize + }, + 32usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t), + "::", + stringify!(flags) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t { + pub fd: __wasi_fd_t, +} +#[test] +fn bindgen_test_layout___wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t( +) { + assert_eq!( + ::std::mem::size_of::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, + >(), + 4usize, + concat!( + "Size of: ", + stringify!( + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t + ) + ) + ); + assert_eq!( + ::std::mem::align_of::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, + >(), + 4usize, + concat!( + "Alignment of ", + stringify!( + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t + ) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::< + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t, + >())) + .fd as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!( + __wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_fd_readwrite_t + ), + "::", + stringify!(fd) + ) + ); +} +#[test] +fn bindgen_test_layout___wasi_subscription_t___wasi_subscription_u() { + assert_eq!( + ::std::mem::size_of::<__wasi_subscription_t___wasi_subscription_u>(), + 40usize, + concat!( + "Size of: ", + stringify!(__wasi_subscription_t___wasi_subscription_u) + ) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_subscription_t___wasi_subscription_u>(), + 8usize, + concat!( + "Alignment of ", + stringify!(__wasi_subscription_t___wasi_subscription_u) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_subscription_t___wasi_subscription_u>())).clock + as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u), + "::", + stringify!(clock) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::<__wasi_subscription_t___wasi_subscription_u>())).fd_readwrite + as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t___wasi_subscription_u), + "::", + stringify!(fd_readwrite) + ) + ); +} +#[test] +fn bindgen_test_layout___wasi_subscription_t() { + assert_eq!( + ::std::mem::size_of::<__wasi_subscription_t>(), + 56usize, + concat!("Size of: ", stringify!(__wasi_subscription_t)) + ); + assert_eq!( + ::std::mem::align_of::<__wasi_subscription_t>(), + 8usize, + concat!("Alignment of ", stringify!(__wasi_subscription_t)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_subscription_t>())).userdata as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t), + "::", + stringify!(userdata) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_subscription_t>())).type_ as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t), + "::", + stringify!(type_) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<__wasi_subscription_t>())).u as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(__wasi_subscription_t), + "::", + stringify!(u) + ) + ); +} +pub fn strerror(errno: __wasi_errno_t) -> &'static str { + match errno { + __WASI_ESUCCESS => "__WASI_ESUCCESS", + __WASI_E2BIG => "__WASI_E2BIG", + __WASI_EACCES => "__WASI_EACCES", + __WASI_EADDRINUSE => "__WASI_EADDRINUSE", + __WASI_EADDRNOTAVAIL => "__WASI_EADDRNOTAVAIL", + __WASI_EAFNOSUPPORT => "__WASI_EAFNOSUPPORT", + __WASI_EAGAIN => "__WASI_EAGAIN", + __WASI_EALREADY => "__WASI_EALREADY", + __WASI_EBADF => "__WASI_EBADF", + __WASI_EBADMSG => "__WASI_EBADMSG", + __WASI_EBUSY => "__WASI_EBUSY", + __WASI_ECANCELED => "__WASI_ECANCELED", + __WASI_ECHILD => "__WASI_ECHILD", + __WASI_ECONNABORTED => "__WASI_ECONNABORTED", + __WASI_ECONNREFUSED => "__WASI_ECONNREFUSED", + __WASI_ECONNRESET => "__WASI_ECONNRESET", + __WASI_EDEADLK => "__WASI_EDEADLK", + __WASI_EDESTADDRREQ => "__WASI_EDESTADDRREQ", + __WASI_EDOM => "__WASI_EDOM", + __WASI_EDQUOT => "__WASI_EDQUOT", + __WASI_EEXIST => "__WASI_EEXIST", + __WASI_EFAULT => "__WASI_EFAULT", + __WASI_EFBIG => "__WASI_EFBIG", + __WASI_EHOSTUNREACH => "__WASI_EHOSTUNREACH", + __WASI_EIDRM => "__WASI_EIDRM", + __WASI_EILSEQ => "__WASI_EILSEQ", + __WASI_EINPROGRESS => "__WASI_EINPROGRESS", + __WASI_EINTR => "__WASI_EINTR", + __WASI_EINVAL => "__WASI_EINVAL", + __WASI_EIO => "__WASI_EIO", + __WASI_EISCONN => "__WASI_EISCONN", + __WASI_EISDIR => "__WASI_EISDIR", + __WASI_ELOOP => "__WASI_ELOOP", + __WASI_EMFILE => "__WASI_EMFILE", + __WASI_EMLINK => "__WASI_EMLINK", + __WASI_EMSGSIZE => "__WASI_EMSGSIZE", + __WASI_EMULTIHOP => "__WASI_EMULTIHOP", + __WASI_ENAMETOOLONG => "__WASI_ENAMETOOLONG", + __WASI_ENETDOWN => "__WASI_ENETDOWN", + __WASI_ENETRESET => "__WASI_ENETRESET", + __WASI_ENETUNREACH => "__WASI_ENETUNREACH", + __WASI_ENFILE => "__WASI_ENFILE", + __WASI_ENOBUFS => "__WASI_ENOBUFS", + __WASI_ENODEV => "__WASI_ENODEV", + __WASI_ENOENT => "__WASI_ENOENT", + __WASI_ENOEXEC => "__WASI_ENOEXEC", + __WASI_ENOLCK => "__WASI_ENOLCK", + __WASI_ENOLINK => "__WASI_ENOLINK", + __WASI_ENOMEM => "__WASI_ENOMEM", + __WASI_ENOMSG => "__WASI_ENOMSG", + __WASI_ENOPROTOOPT => "__WASI_ENOPROTOOPT", + __WASI_ENOSPC => "__WASI_ENOSPC", + __WASI_ENOSYS => "__WASI_ENOSYS", + __WASI_ENOTCONN => "__WASI_ENOTCONN", + __WASI_ENOTDIR => "__WASI_ENOTDIR", + __WASI_ENOTEMPTY => "__WASI_ENOTEMPTY", + __WASI_ENOTRECOVERABLE => "__WASI_ENOTRECOVERABLE", + __WASI_ENOTSOCK => "__WASI_ENOTSOCK", + __WASI_ENOTSUP => "__WASI_ENOTSUP", + __WASI_ENOTTY => "__WASI_ENOTTY", + __WASI_ENXIO => "__WASI_ENXIO", + __WASI_EOVERFLOW => "__WASI_EOVERFLOW", + __WASI_EOWNERDEAD => "__WASI_EOWNERDEAD", + __WASI_EPERM => "__WASI_EPERM", + __WASI_EPIPE => "__WASI_EPIPE", + __WASI_EPROTO => "__WASI_EPROTO", + __WASI_EPROTONOSUPPORT => "__WASI_EPROTONOSUPPORT", + __WASI_EPROTOTYPE => "__WASI_EPROTOTYPE", + __WASI_ERANGE => "__WASI_ERANGE", + __WASI_EROFS => "__WASI_EROFS", + __WASI_ESPIPE => "__WASI_ESPIPE", + __WASI_ESRCH => "__WASI_ESRCH", + __WASI_ESTALE => "__WASI_ESTALE", + __WASI_ETIMEDOUT => "__WASI_ETIMEDOUT", + __WASI_ETXTBSY => "__WASI_ETXTBSY", + __WASI_EXDEV => "__WASI_EXDEV", + __WASI_ENOTCAPABLE => "__WASI_ENOTCAPABLE", + other => panic!("Undefined errno value {:?}", other), + } +} + +pub fn whence_to_str(whence: __wasi_whence_t) -> &'static str { + match whence { + __WASI_WHENCE_CUR => "__WASI_WHENCE_CUR", + __WASI_WHENCE_END => "__WASI_WHENCE_END", + __WASI_WHENCE_SET => "__WASI_WHENCE_SET", + other => panic!("Undefined whence value {:?}", other), + } +} + +// libc constants +pub const INT8_MIN: i32 = -128; +pub const INT16_MIN: i32 = -32768; +pub const INT32_MIN: i32 = -2147483648; +pub const INT8_MAX: u32 = 127; +pub const INT16_MAX: u32 = 32767; +pub const INT32_MAX: u32 = 2147483647; +pub const UINT8_MAX: u32 = 255; +pub const UINT16_MAX: u32 = 65535; +pub const UINT32_MAX: u32 = 4294967295; +pub const INT_LEAST8_MIN: i32 = -128; +pub const INT_LEAST16_MIN: i32 = -32768; +pub const INT_LEAST32_MIN: i32 = -2147483648; +pub const INT_LEAST8_MAX: u32 = 127; +pub const INT_LEAST16_MAX: u32 = 32767; +pub const INT_LEAST32_MAX: u32 = 2147483647; +pub const UINT_LEAST8_MAX: u32 = 255; +pub const UINT_LEAST16_MAX: u32 = 65535; +pub const UINT_LEAST32_MAX: u32 = 4294967295; +pub const INT_FAST8_MIN: i32 = -128; +pub const INT_FAST16_MIN: i32 = -2147483648; +pub const INT_FAST32_MIN: i32 = -2147483648; +pub const INT_FAST8_MAX: u32 = 127; +pub const INT_FAST16_MAX: u32 = 2147483647; +pub const INT_FAST32_MAX: u32 = 2147483647; +pub const UINT_FAST8_MAX: u32 = 255; +pub const UINT_FAST16_MAX: u32 = 4294967295; +pub const UINT_FAST32_MAX: u32 = 4294967295; +pub const INTPTR_MIN: i32 = -2147483648; +pub const INTPTR_MAX: u32 = 2147483647; +pub const UINTPTR_MAX: u32 = 4294967295; +pub const PTRDIFF_MIN: i32 = -2147483648; +pub const PTRDIFF_MAX: u32 = 2147483647; +pub const SIG_ATOMIC_MIN: i32 = -2147483648; +pub const SIG_ATOMIC_MAX: u32 = 2147483647; +pub const SIZE_MAX: u32 = 4294967295; +pub const WINT_MIN: i32 = -2147483648; +pub const WINT_MAX: i32 = 2147483647; + +// WASI constants +pub const __WASI_ADVICE_NORMAL: __wasi_advice_t = 0; +pub const __WASI_ADVICE_SEQUENTIAL: __wasi_advice_t = 1; +pub const __WASI_ADVICE_RANDOM: __wasi_advice_t = 2; +pub const __WASI_ADVICE_WILLNEED: __wasi_advice_t = 3; +pub const __WASI_ADVICE_DONTNEED: __wasi_advice_t = 4; +pub const __WASI_ADVICE_NOREUSE: __wasi_advice_t = 5; +pub const __WASI_CLOCK_REALTIME: __wasi_clockid_t = 0; +pub const __WASI_CLOCK_MONOTONIC: __wasi_clockid_t = 1; +pub const __WASI_CLOCK_PROCESS_CPUTIME_ID: __wasi_clockid_t = 2; +pub const __WASI_CLOCK_THREAD_CPUTIME_ID: __wasi_clockid_t = 3; +pub const __WASI_DIRCOOKIE_START: __wasi_dircookie_t = 0; +pub const __WASI_ESUCCESS: __wasi_errno_t = 0; +pub const __WASI_E2BIG: __wasi_errno_t = 1; +pub const __WASI_EACCES: __wasi_errno_t = 2; +pub const __WASI_EADDRINUSE: __wasi_errno_t = 3; +pub const __WASI_EADDRNOTAVAIL: __wasi_errno_t = 4; +pub const __WASI_EAFNOSUPPORT: __wasi_errno_t = 5; +pub const __WASI_EAGAIN: __wasi_errno_t = 6; +pub const __WASI_EALREADY: __wasi_errno_t = 7; +pub const __WASI_EBADF: __wasi_errno_t = 8; +pub const __WASI_EBADMSG: __wasi_errno_t = 9; +pub const __WASI_EBUSY: __wasi_errno_t = 10; +pub const __WASI_ECANCELED: __wasi_errno_t = 11; +pub const __WASI_ECHILD: __wasi_errno_t = 12; +pub const __WASI_ECONNABORTED: __wasi_errno_t = 13; +pub const __WASI_ECONNREFUSED: __wasi_errno_t = 14; +pub const __WASI_ECONNRESET: __wasi_errno_t = 15; +pub const __WASI_EDEADLK: __wasi_errno_t = 16; +pub const __WASI_EDESTADDRREQ: __wasi_errno_t = 17; +pub const __WASI_EDOM: __wasi_errno_t = 18; +pub const __WASI_EDQUOT: __wasi_errno_t = 19; +pub const __WASI_EEXIST: __wasi_errno_t = 20; +pub const __WASI_EFAULT: __wasi_errno_t = 21; +pub const __WASI_EFBIG: __wasi_errno_t = 22; +pub const __WASI_EHOSTUNREACH: __wasi_errno_t = 23; +pub const __WASI_EIDRM: __wasi_errno_t = 24; +pub const __WASI_EILSEQ: __wasi_errno_t = 25; +pub const __WASI_EINPROGRESS: __wasi_errno_t = 26; +pub const __WASI_EINTR: __wasi_errno_t = 27; +pub const __WASI_EINVAL: __wasi_errno_t = 28; +pub const __WASI_EIO: __wasi_errno_t = 29; +pub const __WASI_EISCONN: __wasi_errno_t = 30; +pub const __WASI_EISDIR: __wasi_errno_t = 31; +pub const __WASI_ELOOP: __wasi_errno_t = 32; +pub const __WASI_EMFILE: __wasi_errno_t = 33; +pub const __WASI_EMLINK: __wasi_errno_t = 34; +pub const __WASI_EMSGSIZE: __wasi_errno_t = 35; +pub const __WASI_EMULTIHOP: __wasi_errno_t = 36; +pub const __WASI_ENAMETOOLONG: __wasi_errno_t = 37; +pub const __WASI_ENETDOWN: __wasi_errno_t = 38; +pub const __WASI_ENETRESET: __wasi_errno_t = 39; +pub const __WASI_ENETUNREACH: __wasi_errno_t = 40; +pub const __WASI_ENFILE: __wasi_errno_t = 41; +pub const __WASI_ENOBUFS: __wasi_errno_t = 42; +pub const __WASI_ENODEV: __wasi_errno_t = 43; +pub const __WASI_ENOENT: __wasi_errno_t = 44; +pub const __WASI_ENOEXEC: __wasi_errno_t = 45; +pub const __WASI_ENOLCK: __wasi_errno_t = 46; +pub const __WASI_ENOLINK: __wasi_errno_t = 47; +pub const __WASI_ENOMEM: __wasi_errno_t = 48; +pub const __WASI_ENOMSG: __wasi_errno_t = 49; +pub const __WASI_ENOPROTOOPT: __wasi_errno_t = 50; +pub const __WASI_ENOSPC: __wasi_errno_t = 51; +pub const __WASI_ENOSYS: __wasi_errno_t = 52; +pub const __WASI_ENOTCONN: __wasi_errno_t = 53; +pub const __WASI_ENOTDIR: __wasi_errno_t = 54; +pub const __WASI_ENOTEMPTY: __wasi_errno_t = 55; +pub const __WASI_ENOTRECOVERABLE: __wasi_errno_t = 56; +pub const __WASI_ENOTSOCK: __wasi_errno_t = 57; +pub const __WASI_ENOTSUP: __wasi_errno_t = 58; +pub const __WASI_ENOTTY: __wasi_errno_t = 59; +pub const __WASI_ENXIO: __wasi_errno_t = 60; +pub const __WASI_EOVERFLOW: __wasi_errno_t = 61; +pub const __WASI_EOWNERDEAD: __wasi_errno_t = 62; +pub const __WASI_EPERM: __wasi_errno_t = 63; +pub const __WASI_EPIPE: __wasi_errno_t = 64; +pub const __WASI_EPROTO: __wasi_errno_t = 65; +pub const __WASI_EPROTONOSUPPORT: __wasi_errno_t = 66; +pub const __WASI_EPROTOTYPE: __wasi_errno_t = 67; +pub const __WASI_ERANGE: __wasi_errno_t = 68; +pub const __WASI_EROFS: __wasi_errno_t = 69; +pub const __WASI_ESPIPE: __wasi_errno_t = 70; +pub const __WASI_ESRCH: __wasi_errno_t = 71; +pub const __WASI_ESTALE: __wasi_errno_t = 72; +pub const __WASI_ETIMEDOUT: __wasi_errno_t = 73; +pub const __WASI_ETXTBSY: __wasi_errno_t = 74; +pub const __WASI_EXDEV: __wasi_errno_t = 75; +pub const __WASI_ENOTCAPABLE: __wasi_errno_t = 76; +pub const __WASI_EVENT_FD_READWRITE_HANGUP: __wasi_eventrwflags_t = 1; +pub const __WASI_EVENTTYPE_CLOCK: __wasi_eventtype_t = 0; +pub const __WASI_EVENTTYPE_FD_READ: __wasi_eventtype_t = 1; +pub const __WASI_EVENTTYPE_FD_WRITE: __wasi_eventtype_t = 2; +pub const __WASI_FDFLAG_APPEND: __wasi_fdflags_t = 1; +pub const __WASI_FDFLAG_DSYNC: __wasi_fdflags_t = 2; +pub const __WASI_FDFLAG_NONBLOCK: __wasi_fdflags_t = 4; +pub const __WASI_FDFLAG_RSYNC: __wasi_fdflags_t = 8; +pub const __WASI_FDFLAG_SYNC: __wasi_fdflags_t = 16; +pub const __WASI_PREOPENTYPE_DIR: __wasi_preopentype_t = 0; +pub const __WASI_FILETYPE_UNKNOWN: __wasi_filetype_t = 0; +pub const __WASI_FILETYPE_BLOCK_DEVICE: __wasi_filetype_t = 1; +pub const __WASI_FILETYPE_CHARACTER_DEVICE: __wasi_filetype_t = 2; +pub const __WASI_FILETYPE_DIRECTORY: __wasi_filetype_t = 3; +pub const __WASI_FILETYPE_REGULAR_FILE: __wasi_filetype_t = 4; +pub const __WASI_FILETYPE_SOCKET_DGRAM: __wasi_filetype_t = 5; +pub const __WASI_FILETYPE_SOCKET_STREAM: __wasi_filetype_t = 6; +pub const __WASI_FILETYPE_SYMBOLIC_LINK: __wasi_filetype_t = 7; +pub const __WASI_FILESTAT_SET_ATIM: __wasi_fstflags_t = 1; +pub const __WASI_FILESTAT_SET_ATIM_NOW: __wasi_fstflags_t = 2; +pub const __WASI_FILESTAT_SET_MTIM: __wasi_fstflags_t = 4; +pub const __WASI_FILESTAT_SET_MTIM_NOW: __wasi_fstflags_t = 8; +pub const __WASI_LOOKUP_SYMLINK_FOLLOW: __wasi_lookupflags_t = 1; +pub const __WASI_O_CREAT: __wasi_oflags_t = 1; +pub const __WASI_O_DIRECTORY: __wasi_oflags_t = 2; +pub const __WASI_O_EXCL: __wasi_oflags_t = 4; +pub const __WASI_O_TRUNC: __wasi_oflags_t = 8; +pub const __WASI_SOCK_RECV_PEEK: __wasi_riflags_t = 1; +pub const __WASI_SOCK_RECV_WAITALL: __wasi_riflags_t = 2; +pub const __WASI_RIGHT_FD_DATASYNC: __wasi_rights_t = 1; +pub const __WASI_RIGHT_FD_READ: __wasi_rights_t = 2; +pub const __WASI_RIGHT_FD_SEEK: __wasi_rights_t = 4; +pub const __WASI_RIGHT_FD_FDSTAT_SET_FLAGS: __wasi_rights_t = 8; +pub const __WASI_RIGHT_FD_SYNC: __wasi_rights_t = 16; +pub const __WASI_RIGHT_FD_TELL: __wasi_rights_t = 32; +pub const __WASI_RIGHT_FD_WRITE: __wasi_rights_t = 64; +pub const __WASI_RIGHT_FD_ADVISE: __wasi_rights_t = 128; +pub const __WASI_RIGHT_FD_ALLOCATE: __wasi_rights_t = 256; +pub const __WASI_RIGHT_PATH_CREATE_DIRECTORY: __wasi_rights_t = 512; +pub const __WASI_RIGHT_PATH_CREATE_FILE: __wasi_rights_t = 1024; +pub const __WASI_RIGHT_PATH_LINK_SOURCE: __wasi_rights_t = 2048; +pub const __WASI_RIGHT_PATH_LINK_TARGET: __wasi_rights_t = 4096; +pub const __WASI_RIGHT_PATH_OPEN: __wasi_rights_t = 8192; +pub const __WASI_RIGHT_FD_READDIR: __wasi_rights_t = 16384; +pub const __WASI_RIGHT_PATH_READLINK: __wasi_rights_t = 32768; +pub const __WASI_RIGHT_PATH_RENAME_SOURCE: __wasi_rights_t = 65536; +pub const __WASI_RIGHT_PATH_RENAME_TARGET: __wasi_rights_t = 131072; +pub const __WASI_RIGHT_PATH_FILESTAT_GET: __wasi_rights_t = 262144; +pub const __WASI_RIGHT_PATH_FILESTAT_SET_SIZE: __wasi_rights_t = 524288; +pub const __WASI_RIGHT_PATH_FILESTAT_SET_TIMES: __wasi_rights_t = 1048576; +pub const __WASI_RIGHT_FD_FILESTAT_GET: __wasi_rights_t = 2097152; +pub const __WASI_RIGHT_FD_FILESTAT_SET_SIZE: __wasi_rights_t = 4194304; +pub const __WASI_RIGHT_FD_FILESTAT_SET_TIMES: __wasi_rights_t = 8388608; +pub const __WASI_RIGHT_PATH_SYMLINK: __wasi_rights_t = 16777216; +pub const __WASI_RIGHT_PATH_REMOVE_DIRECTORY: __wasi_rights_t = 33554432; +pub const __WASI_RIGHT_PATH_UNLINK_FILE: __wasi_rights_t = 67108864; +pub const __WASI_RIGHT_POLL_FD_READWRITE: __wasi_rights_t = 134217728; +pub const __WASI_RIGHT_SOCK_SHUTDOWN: __wasi_rights_t = 268435456; +pub const __WASI_SOCK_RECV_DATA_TRUNCATED: __wasi_roflags_t = 1; +pub const __WASI_SHUT_RD: __wasi_sdflags_t = 1; +pub const __WASI_SHUT_WR: __wasi_sdflags_t = 2; +pub const __WASI_SIGHUP: __wasi_signal_t = 1; +pub const __WASI_SIGINT: __wasi_signal_t = 2; +pub const __WASI_SIGQUIT: __wasi_signal_t = 3; +pub const __WASI_SIGILL: __wasi_signal_t = 4; +pub const __WASI_SIGTRAP: __wasi_signal_t = 5; +pub const __WASI_SIGABRT: __wasi_signal_t = 6; +pub const __WASI_SIGBUS: __wasi_signal_t = 7; +pub const __WASI_SIGFPE: __wasi_signal_t = 8; +pub const __WASI_SIGKILL: __wasi_signal_t = 9; +pub const __WASI_SIGUSR1: __wasi_signal_t = 10; +pub const __WASI_SIGSEGV: __wasi_signal_t = 11; +pub const __WASI_SIGUSR2: __wasi_signal_t = 12; +pub const __WASI_SIGPIPE: __wasi_signal_t = 13; +pub const __WASI_SIGALRM: __wasi_signal_t = 14; +pub const __WASI_SIGTERM: __wasi_signal_t = 15; +pub const __WASI_SIGCHLD: __wasi_signal_t = 16; +pub const __WASI_SIGCONT: __wasi_signal_t = 17; +pub const __WASI_SIGSTOP: __wasi_signal_t = 18; +pub const __WASI_SIGTSTP: __wasi_signal_t = 19; +pub const __WASI_SIGTTIN: __wasi_signal_t = 20; +pub const __WASI_SIGTTOU: __wasi_signal_t = 21; +pub const __WASI_SIGURG: __wasi_signal_t = 22; +pub const __WASI_SIGXCPU: __wasi_signal_t = 23; +pub const __WASI_SIGXFSZ: __wasi_signal_t = 24; +pub const __WASI_SIGVTALRM: __wasi_signal_t = 25; +pub const __WASI_SIGPROF: __wasi_signal_t = 26; +pub const __WASI_SIGWINCH: __wasi_signal_t = 27; +pub const __WASI_SIGPOLL: __wasi_signal_t = 28; +pub const __WASI_SIGPWR: __wasi_signal_t = 29; +pub const __WASI_SIGSYS: __wasi_signal_t = 30; +pub const __WASI_SUBSCRIPTION_CLOCK_ABSTIME: __wasi_subclockflags_t = 1; +pub const __WASI_WHENCE_CUR: __wasi_whence_t = 0; +pub const __WASI_WHENCE_END: __wasi_whence_t = 1; +pub const __WASI_WHENCE_SET: __wasi_whence_t = 2; diff --git a/wasmtime-wasi/README.md b/wasmtime-wasi/README.md index 4ee92fc9a85c..7f2eaad27fa3 100644 --- a/wasmtime-wasi/README.md +++ b/wasmtime-wasi/README.md @@ -1,5 +1,5 @@ This is the `wasmtime-wasi` crate, which implements the -WebAssembly System Interface (WASI) API. +WebAssembly System Interface (WASI) API in Rust. WASI is greatly inspired by and directly derived from [CloudABI]. It differs in that it has aspirations to expand to a greater From 031f03fa2582a942ce6a9d368640ad15ded6ba47 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 21 May 2019 05:44:29 -0700 Subject: [PATCH 5/6] Make the wasmtime-wasi-c dependency conditional on Unix. --- Cargo.toml | 4 +++- src/wasmtime.rs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 28d9d9e7c2f9..5731d55c5646 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,6 @@ wasmtime-jit = { path = "wasmtime-jit" } wasmtime-obj = { path = "wasmtime-obj" } wasmtime-wast = { path = "wasmtime-wast" } wasmtime-wasi = { path = "wasmtime-wasi" } -wasmtime-wasi-c = { path = "wasmtime-wasi-c" } docopt = "1.0.1" serde = "1.0.75" serde_derive = "1.0.75" @@ -43,6 +42,9 @@ wabt = "0.7" libc = "0.2.50" errno = "0.2.4" +[target.'cfg(unix)'.dependencies] +wasmtime-wasi-c = { path = "wasmtime-wasi-c" } + [workspace] [features] diff --git a/src/wasmtime.rs b/src/wasmtime.rs index c573e3a7de12..e2730a99567c 100644 --- a/src/wasmtime.rs +++ b/src/wasmtime.rs @@ -50,9 +50,11 @@ use std::process::exit; use wabt; use wasmtime_jit::{ActionOutcome, Context}; use wasmtime_wasi::instantiate_wasi; -use wasmtime_wasi_c::instantiate_wasi_c; use wasmtime_wast::instantiate_spectest; +#[cfg(unix)] +use wasmtime_wasi_c::instantiate_wasi_c; + static LOG_FILENAME_PREFIX: &str = "wasmtime.dbg."; const USAGE: &str = " @@ -235,7 +237,14 @@ fn main() { let wasi = if args.flag_wasi_common { instantiate_wasi("", global_exports, &preopen_dirs, &argv, &environ) } else { - instantiate_wasi_c("", global_exports, &preopen_dirs, &argv, &environ) + #[cfg(unix)] + { + instantiate_wasi_c("", global_exports, &preopen_dirs, &argv, &environ) + } + #[cfg(not(unix))] + { + unimplemented!("wasmtime-wasi-c requires a *nix") + } } .expect("instantiating wasi"); From 5fc30e69f7f21afb330833e69391052c19affe0c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 21 May 2019 22:59:56 +0200 Subject: [PATCH 6/6] Fix preopening dirs on Windows --- Cargo.toml | 3 +++ src/wasmtime.rs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5731d55c5646..90202005d1d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,9 @@ errno = "0.2.4" [target.'cfg(unix)'.dependencies] wasmtime-wasi-c = { path = "wasmtime-wasi-c" } +[target.'cfg(windows)'.dependencies] +winapi = "0.3" + [workspace] [features] diff --git a/src/wasmtime.rs b/src/wasmtime.rs index e2730a99567c..3994da605989 100644 --- a/src/wasmtime.rs +++ b/src/wasmtime.rs @@ -118,11 +118,38 @@ fn read_wasm(path: PathBuf) -> Result, String> { }) } +fn preopen_dir>(path: P) -> io::Result { + #[cfg(windows)] + { + use std::fs::OpenOptions; + use std::os::windows::fs::OpenOptionsExt; + use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS; + + // To open a directory using CreateFile2, specify the + // FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFileFlags... + // cf. https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfile2 + OpenOptions::new() + .create(false) + .write(true) + .read(true) + .attributes(FILE_FLAG_BACKUP_SEMANTICS) + .open(path) + } + #[cfg(unix)] + { + File::open(path) + } + #[cfg(not(any(windows, unix)))] + { + unimplemented!("this OS is currently not supported by Wasmtime") + } +} + fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(String, File)> { let mut preopen_dirs = Vec::new(); for dir in flag_dir { - let preopen_dir = File::open(dir).unwrap_or_else(|err| { + let preopen_dir = preopen_dir(dir).unwrap_or_else(|err| { println!("error while pre-opening directory {}: {}", dir, err); exit(1); }); @@ -136,7 +163,7 @@ fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(Str exit(1); } let (key, value) = (parts[0], parts[1]); - let preopen_dir = File::open(value).unwrap_or_else(|err| { + let preopen_dir = preopen_dir(value).unwrap_or_else(|err| { println!("error while pre-opening directory {}: {}", value, err); exit(1); });