Skip to content

Commit 5687aef

Browse files
authored
Merge pull request #329 from wasmx/wasi
Add fizzy-wasi tool
2 parents 88cca3e + 105abe6 commit 5687aef

14 files changed

Lines changed: 423 additions & 5 deletions

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ include(CableBuildType)
1212
include(CableCompilerSettings)
1313
include(CMakeDependentOption)
1414

15+
option(FIZZY_WASI "Enable WASI support" OFF)
16+
1517
option(FIZZY_TESTING "Enable Fizzy internal tests" OFF)
1618
cmake_dependent_option(HUNTER_ENABLED "Enable Hunter package manager" ON
1719
"FIZZY_TESTING" OFF)
@@ -104,6 +106,10 @@ if(FIZZY_TESTING)
104106
add_subdirectory(test)
105107
endif()
106108

109+
if(FIZZY_WASI)
110+
add_subdirectory(tools/wasi)
111+
endif()
112+
107113
set(CMAKE_INSTALL_CMAKEPACKAGEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
108114

109115
write_basic_package_version_file(fizzyConfigVersion.cmake COMPATIBILITY ExactVersion)

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ $ cmake --build .
4545
This will build Fizzy as a library and since there is no public API
4646
(the so called *embedder API* in WebAssembly) yet, this is not very useful.
4747

48+
## WASI
49+
50+
Building with the `FIZZY_WASI` option will output a `fizzy-wasi` binary implementing
51+
the [WASI] API (a very limited subset of [wasi_snapshot_preview1], to be precise).
52+
It uses [uvwasi] under the hood. It can be used to execute WASI-compatible binaries on the command line.
53+
54+
```sh
55+
$ mkdir build && cd build
56+
$ cmake -DFIZZY_WASI=ON ..
57+
$ cmake --build .
58+
```
59+
60+
Try it with a Hello World example:
61+
```sh
62+
$ bin/fizzy-wasi ../test/smoketests/wasi/helloworld.wasm
63+
hello world
64+
```
65+
66+
## Testing tools
67+
4868
Building with the `FIZZY_TESTING` option will output a few useful utilities:
4969

5070
```sh
@@ -152,15 +172,19 @@ For a list of releases and changelog see the [CHANGELOG file](./CHANGELOG.md).
152172

153173
Licensed under the [Apache License, Version 2.0].
154174

155-
[webassembly]: https://webassembly.org/
175+
[webassembly]: https://webassembly.org
156176
[standard readme]: https://github.com/RichardLitt/standard-readme
157177
[circleci]: https://circleci.com/gh/wasmx/fizzy/tree/master
158-
[codecov]: https://codecov.io/gh/wasmx/fizzy/
178+
[codecov]: https://codecov.io/gh/wasmx/fizzy
159179
[Apache License, Version 2.0]: LICENSE
160180
[@axic]: https://github.com/axic
161181
[@chfast]: https://github.com/chfast
162182
[@gumb0]: https://github.com/gumb0
163183

184+
[WASI]: https://github.com/WebAssembly/WASI
185+
[uvwasi]: https://github.com/cjihrig/uvwasi
186+
[wasi_snapshot_preview1]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/wasi_snapshot_preview1.witx
187+
164188
[webassembly badge]: https://img.shields.io/badge/WebAssembly-Engine-informational.svg?logo=webassembly
165189
[readme style standard badge]: https://img.shields.io/badge/readme%20style-standard-brightgreen.svg
166190
[circleci badge]: https://img.shields.io/circleci/project/github/wasmx/fizzy/master.svg?logo=circleci

circle.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ commands:
110110
working_directory: ~/build
111111
command: |
112112
rm -f CMakeCache.txt
113-
cmake ../project -G Ninja -DCMAKE_INSTALL_PREFIX=~/install -DCMAKE_BUILD_TYPE=<<parameters.build_type>> -DFIZZY_TESTING=ON <<parameters.cmake_options>>
113+
cmake ../project -G Ninja -DCMAKE_INSTALL_PREFIX=~/install -DCMAKE_BUILD_TYPE=<<parameters.build_type>> -DFIZZY_TESTING=ON -DFIZZY_WASI=ON <<parameters.cmake_options>>
114114
- save_cache:
115115
name: "Save Hunter cache"
116116
key: *hunter-cache-key
@@ -143,7 +143,7 @@ commands:
143143
name: "Collect coverage data"
144144
working_directory: ~/build
145145
command: |
146-
binaries='-object bin/fizzy-unittests -object bin/fizzy-spectests -object bin/fizzy-bench -object bin/fizzy-testfloat'
146+
binaries='-object bin/fizzy-unittests -object bin/fizzy-spectests -object bin/fizzy-bench -object bin/fizzy-testfloat -object bin/fizzy-wasi'
147147
148148
mkdir coverage
149149
find -name '*.profraw'

cmake/ProjectUVWASI.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
if(ProjectUVWASIIncluded)
2+
return()
3+
endif()
4+
set(ProjectUVWASIIncluded TRUE)
5+
6+
include(ExternalProject)
7+
8+
set(prefix ${CMAKE_BINARY_DIR}/_deps)
9+
set(source_dir ${prefix}/src/uvwasi)
10+
set(binary_dir ${prefix}/src/uvwasi-build)
11+
set(include_dir ${source_dir}/include)
12+
set(uvwasi_library ${binary_dir}/${CMAKE_STATIC_LIBRARY_PREFIX}uvwasi_a${CMAKE_STATIC_LIBRARY_SUFFIX})
13+
set(uv_library ${binary_dir}/_deps/libuv-build/${CMAKE_STATIC_LIBRARY_PREFIX}uv_a${CMAKE_STATIC_LIBRARY_SUFFIX})
14+
15+
if(UNIX AND NOT APPLE)
16+
set(system_libs "pthread;dl;rt")
17+
endif()
18+
19+
ExternalProject_Add(uvwasi
20+
EXCLUDE_FROM_ALL 1
21+
PREFIX ${prefix}
22+
DOWNLOAD_NAME uvwasi-0.0.10.tar.gz
23+
DOWNLOAD_DIR ${prefix}/downloads
24+
SOURCE_DIR ${source_dir}
25+
BINARY_DIR ${binary_dir}
26+
URL https://github.com/cjihrig/uvwasi/archive/v0.0.10.tar.gz
27+
URL_HASH SHA256=39135f4dd4a44013399ceed7166391ffc5c09655e4bfbf851da2be039e6985df
28+
CMAKE_ARGS
29+
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
30+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
31+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
32+
-DUVWASI_DEBUG_LOG=OFF
33+
-DCODE_COVERAGE=OFF
34+
-DASAN=OFF
35+
-DUVWASI_BUILD_TESTS=OFF
36+
BUILD_BYPRODUCTS ${uvwasi_library} ${uv_library}
37+
)
38+
39+
add_library(uvwasi::uvwasi STATIC IMPORTED)
40+
41+
file(MAKE_DIRECTORY ${include_dir})
42+
43+
set_target_properties(
44+
uvwasi::uvwasi
45+
PROPERTIES
46+
IMPORTED_CONFIGURATIONS Release
47+
IMPORTED_LOCATION_RELEASE ${uvwasi_library}
48+
INTERFACE_INCLUDE_DIRECTORIES "${include_dir};${binary_dir}"
49+
INTERFACE_LINK_LIBRARIES "${uv_library};${system_libs}"
50+
)
51+
52+
add_dependencies(uvwasi::uvwasi uvwasi)

lgtm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ extraction:
99
configure:
1010
command:
1111
- cd $LGTM_SRC/_lgtm_build_dir
12-
- cmake -DFIZZY_TESTING=ON -DCMAKE_BUILD_TYPE=Release ..
12+
- cmake -DFIZZY_TESTING=ON -DFIZZY_WASI=ON -DCMAKE_BUILD_TYPE=Release ..

test/smoketests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44

55
add_subdirectory(benchmarks)
66
add_subdirectory(spectests)
7+
if(FIZZY_WASI)
8+
add_subdirectory(wasi)
9+
endif()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Fizzy: A fast WebAssembly interpreter
2+
# Copyright 2020 The Fizzy Authors.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
add_test(
6+
NAME fizzy/smoketests/wasi/cli-missing-arg
7+
COMMAND fizzy-wasi
8+
)
9+
set_tests_properties(
10+
fizzy/smoketests/wasi/cli-missing-arg
11+
PROPERTIES
12+
PASS_REGULAR_EXPRESSION "Missing executable argument"
13+
)
14+
15+
add_test(
16+
NAME fizzy/smoketests/wasi/cli-invalid-file
17+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}
18+
)
19+
set_tests_properties(
20+
fizzy/smoketests/wasi/cli-invalid-file
21+
PROPERTIES
22+
PASS_REGULAR_EXPRESSION "File does not exists or is irregular:"
23+
)
24+
25+
add_test(
26+
NAME fizzy/smoketests/wasi/cli-missing-file
27+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}/helloworld.nonexistent.wasm
28+
)
29+
set_tests_properties(
30+
fizzy/smoketests/wasi/cli-missing-file
31+
PROPERTIES
32+
PASS_REGULAR_EXPRESSION "File does not exists or is irregular:"
33+
)
34+
35+
add_test(
36+
NAME fizzy/smoketests/wasi/helloworld
37+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}/helloworld.wasm
38+
)
39+
set_tests_properties(
40+
fizzy/smoketests/wasi/helloworld
41+
PROPERTIES
42+
PASS_REGULAR_EXPRESSION "hello world"
43+
)
44+
45+
add_test(
46+
NAME fizzy/smoketests/wasi/missing-start
47+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}/missingstart.wasm
48+
)
49+
set_tests_properties(
50+
fizzy/smoketests/wasi/missing-start
51+
PROPERTIES
52+
PASS_REGULAR_EXPRESSION "File is not WASI compatible \\(_start not found\\)"
53+
)
54+
55+
add_test(
56+
NAME fizzy/smoketests/wasi/invalid-start
57+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}/invalidstart.wasm
58+
)
59+
set_tests_properties(
60+
fizzy/smoketests/wasi/invalid-start
61+
PROPERTIES
62+
PASS_REGULAR_EXPRESSION "File is not WASI compatible \\(_start has invalid signature\\)"
63+
)
64+
65+
add_test(
66+
NAME fizzy/smoketests/wasi/missing-memory
67+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}/missingmemory.wasm
68+
)
69+
set_tests_properties(
70+
fizzy/smoketests/wasi/missing-memory
71+
PROPERTIES
72+
PASS_REGULAR_EXPRESSION "File is not WASI compatible \\(no memory exported\\)"
73+
)
74+
75+
add_test(
76+
NAME fizzy/smoketests/wasi/trap
77+
COMMAND fizzy-wasi ${CMAKE_CURRENT_LIST_DIR}/trap.wasm
78+
)
79+
set_tests_properties(
80+
fizzy/smoketests/wasi/trap
81+
PROPERTIES
82+
PASS_REGULAR_EXPRESSION "Execution aborted with WebAssembly trap"
83+
)
84+
85+
# Dump coverage data to distinct files (otherwise file will be overwritten).
86+
set_tests_properties(
87+
fizzy/smoketests/wasi/cli-missing-arg
88+
fizzy/smoketests/wasi/cli-missing-file
89+
fizzy/smoketests/wasi/cli-invalid-file
90+
fizzy/smoketests/wasi/helloworld
91+
fizzy/smoketests/wasi/missing-start
92+
fizzy/smoketests/wasi/invalid-start
93+
fizzy/smoketests/wasi/missing-memory
94+
fizzy/smoketests/wasi/trap
95+
PROPERTIES
96+
ENVIRONMENT LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/wasi-%p.profraw
97+
)
140 Bytes
Binary file not shown.
140 Bytes
Binary file not shown.
131 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)