mirror of
https://github.com/wazero/wazero
synced 2026-06-21 14:12:37 +00:00
2c14bbff3d
Add experimental support for the [Typed Function References](https://github.com/WebAssembly/function-references) proposal. Closes https://github.com/wazero/wazero/issues/2483, follows up to the refactoring in https://github.com/wazero/wazero/pull/2495 and prepares to WasmGC. Typed function references extend WebAssembly's type system with non-nullable reference types and concrete function type indices (`(ref $t)`, `(ref null $t)`), enabling direct calls through typed references (`call_ref`, `return_call_ref`) and null-aware branching (`br_on_null`, `br_on_non_null`, `ref.as_non_null`). Excluding tests and spec suites, the feature amounts to roughly 1,900 lines of code. Feature flag: `experimental.CoreFeaturesTypedFunctionReferences` ## What's the use for this? Sadly, very little. This proposal is a pretty much just a prerequisite for the GC proposal. On the flip side, it completes the exception handling spec (https://github.com/wazero/wazero/pull/2489): 1. two EH spec tests were previously skipped because they required distinguishing nullable from non-nullable references. Those tests now pass. 2. The fuzzer is now enabled for both exception handling and typed function references. It could not be enabled for EH because the fuzzer would generate func refs. So I guess, technically, it has a use :D ## Type System `ValueType` (already `uint64` introduced in https://github.com/wazero/wazero/pull/2495) is now extended with bit flags to encode nullability and concrete type indices: | Bits | Purpose | |-------|---------------------------------| | 0-7 | Base type byte (same as before) | | 8 | Non-nullable flag | | 9 | Concrete ref flag | | 32-63 | Type index (for `(ref $t)`) | This encoding should be fine for WasmGC too and should not require further changes in the near future. Subtyping rules: non-nullable is a subtype of nullable (same kind/index); concrete function refs `(ref $t)` are subtypes of `funcref`. ## New Instructions | Opcode | Hex | Description | |-------------------|--------|----------------------------------------------------------| | `call_ref` | `0x14` | Indirect call through a typed function reference | | `return_call_ref` | `0x15` | Tail-call variant of `call_ref` | | `ref.as_non_null` | `0xd4` | Assert ref is non-null, trap otherwise | | `br_on_null` | `0xd5` | Branch if null, push non-null ref on fall-through | | `br_on_non_null` | `0xd6` | Branch if non-null (carrying the ref), fall-through on null | ## Validation Tricky bits: - Non-nullable local initialization tracking: `local.get` on a non-nullable ref local is rejected unless a `local.set`/`local.tee` has been executed in the same or enclosing block scope. State is saved/restored at block boundaries per the spec (needed an additional field to keep track of init'd values) - `ref.func` now pushes `(ref $t)` (the concrete non-nullable type of the referenced function) onto the validation type stack instead of plain `funcref`, so that passing it to `call_ref` type-checks without an upcast. - Block types, element segments, table types, and const expressions all support concrete ref types. - The type section now validates forward references (standalone types can only reference previously defined types; rec group members can reference each other). ## Interpreter The five new opcodes are compiled to new IR operations and executed in the interpreter loop. `call_ref` / `return_call_ref` load the function instance from the opaque reference pointer, null-check, and dispatch. `br_on_null` / `br_on_non_null` pop the reference, check nullity, and either branch or fall through with the appropriate stack state. ## Compiler (wazevo) Implemented entirely as SSA-level lowering with no backend-specific code: - `call_ref` / `return_call_ref`: load executable and module context pointers from the function instance, null-check via `ExitIfTrueWithCode(ExitCodeNullReference)`, then dispatch as an indirect call. - `br_on_null` / `br_on_non_null`: compare against zero, branch with trampoline blocks for try-table exits and listener support. - `ref.as_non_null`: null-check with trap. ## Binary Decoding - `decodeRefType` helper extracted and shared across `value.go`, `element.go`, `table.go`, and `code.go` for consistent handling of `(ref null ht)` / `(ref ht)` prefixes. - Tables support the `0x40 0x00` prefix for initializer expressions (required for non-nullable table element types). - `DecodeBlockType` handles concrete ref types as block results. ## Cross-Module Linking `call_indirect` uses `FunctionTypeID` for fast runtime type checks. The existing `FunctionType.key()` method builds the key from raw `ValueType` bytes, but with concrete refs `(ref $0)` in module A and `(ref $0)` in module B may refer to structurally identical types at different local indices. `structuralTypeKey` fixes this by replacing local type indices with the already-assigned `FunctionTypeID` of the referenced type, so two modules with the same structural signature share a single `FunctionTypeID`. ## Spec Suite The spec test suite uses `wasm-tools json-from-wast` (same as exception handling). All 22 test files pass. ## Fuzzing The fuzzer (`nodiff`) now enables both `CoreFeaturesExceptionHandling` and `CoreFeaturesTypedFunctionReferences`. Dummy import generation handles `exnref` and concrete ref types. --------- Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
386 lines
20 KiB
Makefile
386 lines
20 KiB
Makefile
|
|
gofumpt := mvdan.cc/gofumpt@v0.6.0
|
|
gosimports := github.com/rinchsan/gosimports/cmd/gosimports@v0.3.8
|
|
golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.5
|
|
asmfmt := github.com/klauspost/asmfmt/cmd/asmfmt@v1.3.2
|
|
# sync this with netlify.toml!
|
|
hugo := github.com/gohugoio/hugo@v0.115.2
|
|
|
|
# Make 3.81 doesn't support '**' globbing: Set explicitly instead of recursion.
|
|
all_sources := $(wildcard *.go */*.go */*/*.go */*/*/*.go */*/*/*.go */*/*/*/*.go)
|
|
all_testdata := $(wildcard testdata/* */testdata/* */*/testdata/* */*/testdata/*/* */*/*/testdata/*)
|
|
all_testing := $(wildcard internal/testing/* internal/testing/*/* internal/testing/*/*/*)
|
|
all_examples := $(wildcard examples/* examples/*/* examples/*/*/* */*/example/* */*/example/*/* */*/example/*/*/*)
|
|
all_it := $(wildcard internal/integration_test/* internal/integration_test/*/* internal/integration_test/*/*/*)
|
|
# main_sources exclude any test or example related code
|
|
main_sources := $(wildcard $(filter-out %_test.go $(all_testdata) $(all_testing) $(all_examples) $(all_it), $(all_sources)))
|
|
# main_packages collect the unique main source directories (sort will dedupe).
|
|
# Paths need to all start with ./, so we do that manually vs foreach which strips it.
|
|
main_packages := $(sort $(foreach f,$(dir $(main_sources)),$(if $(findstring ./,$(f)),./,./$(f))))
|
|
|
|
go_test_options ?= -timeout 300s
|
|
|
|
.PHONY: test.examples
|
|
test.examples:
|
|
@go test $(go_test_options) ./examples/... ./imports/assemblyscript/example/... ./imports/emscripten/... ./imports/wasi_snapshot_preview1/example/...
|
|
|
|
.PHONY: build.examples.as
|
|
build.examples.as:
|
|
@cd ./imports/assemblyscript/example/testdata && npm install && npm run build
|
|
|
|
%.wasm: %.zig
|
|
@(cd $(@D); zig build -Doptimize=ReleaseSmall)
|
|
@mv $(@D)/zig-out/*/$(@F) $(@D)
|
|
|
|
.PHONY: build.examples.zig
|
|
build.examples.zig: examples/allocation/zig/testdata/greet.wasm imports/wasi_snapshot_preview1/example/testdata/zig/cat.wasm imports/wasi_snapshot_preview1/testdata/zig/wasi.wasm
|
|
@cd internal/testing/dwarftestdata/testdata/zig; zig build; mv zig-out/*/main.wasm ./ # Need DWARF custom sections.
|
|
|
|
tinygo_reactor_sources_reactor := examples/basic/testdata/add.go examples/allocation/tinygo/testdata/greet.go
|
|
.PHONY: build.examples.tinygo_reactor
|
|
build.examples.tinygo_reactor: $(tinygo_sources_reactor)
|
|
@for f in $^; do \
|
|
tinygo build -o $$(echo $$f | sed -e 's/\.go/\.wasm/') -scheduler=none --no-debug --target=wasip1 -buildmode=c-shared $$f; \
|
|
done
|
|
|
|
tinygo_sources_clis := examples/cli/testdata/cli.go imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.go imports/wasi_snapshot_preview1/testdata/tinygo/wasi.go cmd/wazero/testdata/cat/cat.go
|
|
.PHONY: build.examples.tinygo_clis
|
|
build.examples.tinygo_clis: $(tinygo_sources_clis)
|
|
@for f in $^; do \
|
|
tinygo build -o $$(echo $$f | sed -e 's/\.go/\.wasm/') -scheduler=none --no-debug --target=wasip1 $$f; \
|
|
done
|
|
@mv cmd/wazero/testdata/cat/cat.wasm cmd/wazero/testdata/cat/cat-tinygo.wasm
|
|
|
|
.PHONY: build.examples.tinygo
|
|
build.examples.tinygo: build.examples.tinygo_reactor build.examples.tinygo_clis
|
|
|
|
# We use zig to build C as it is easy to install and embeds a copy of zig-cc.
|
|
# Note: Don't use "-Oz" as that breaks our wasi sock example.
|
|
c_sources := imports/wasi_snapshot_preview1/example/testdata/zig-cc/cat.c imports/wasi_snapshot_preview1/testdata/zig-cc/wasi.c internal/testing/dwarftestdata/testdata/zig-cc/main.c
|
|
.PHONY: build.examples.zig-cc
|
|
build.examples.zig-cc: $(c_sources)
|
|
@for f in $^; do \
|
|
zig cc --target=wasm32-wasi -o $$(echo $$f | sed -e 's/\.c/\.wasm/') $$f; \
|
|
done
|
|
|
|
# Here are the emcc args we use:
|
|
#
|
|
# * `-Oz` - most optimization for code size.
|
|
# * `--profiling` - adds the name section.
|
|
# * `-s STANDALONE_WASM` - ensures wasm is built for a non-js runtime.
|
|
# * `-s EXPORTED_FUNCTIONS=_malloc,_free` - export allocation functions so that
|
|
# they can be used externally as "malloc" and "free".
|
|
# * `-s WARN_ON_UNDEFINED_SYMBOLS=0` - imports not defined in JavaScript error
|
|
# otherwise. See https://github.com/emscripten-core/emscripten/issues/13641
|
|
# * `-s TOTAL_STACK=8KB -s TOTAL_MEMORY=64KB` - reduce memory default from 16MB
|
|
# to one page (64KB). To do this, we have to reduce the stack size.
|
|
# * `-s ALLOW_MEMORY_GROWTH` - allows "memory.grow" instructions to succeed, but
|
|
# requires a function import "emscripten_notify_memory_growth".
|
|
emscripten_sources := $(wildcard imports/emscripten/testdata/*.cc)
|
|
.PHONY: build.examples.emscripten
|
|
build.examples.emscripten: $(emscripten_sources)
|
|
@for f in $^; do \
|
|
em++ -Oz --profiling \
|
|
-s STANDALONE_WASM \
|
|
-s EXPORTED_FUNCTIONS=_malloc,_free \
|
|
-s WARN_ON_UNDEFINED_SYMBOLS=0 \
|
|
-s TOTAL_STACK=8KB -s TOTAL_MEMORY=64KB \
|
|
-s ALLOW_MEMORY_GROWTH \
|
|
--std=c++17 -o $$(echo $$f | sed -e 's/\.cc/\.wasm/') $$f; \
|
|
done
|
|
|
|
%/greet.wasm : cargo_target := wasm32-unknown-unknown
|
|
%/cat.wasm : cargo_target := wasm32-wasip1
|
|
%/wasi.wasm : cargo_target := wasm32-wasip1
|
|
|
|
.PHONY: build.examples.rust
|
|
build.examples.rust: examples/allocation/rust/testdata/greet.wasm imports/wasi_snapshot_preview1/example/testdata/cargo-wasi/cat.wasm imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.wasm internal/testing/dwarftestdata/testdata/rust/main.wasm.xz
|
|
|
|
# Normally, we build release because it is smaller. Testing dwarf requires the debug build.
|
|
internal/testing/dwarftestdata/testdata/rust/main.wasm.xz:
|
|
cd $(@D) && cargo build --target wasm32-wasip1
|
|
mv $(@D)/target/wasm32-wasip1/debug/main.wasm $(@D)
|
|
cd $(@D) && xz -k -f ./main.wasm # Rust's DWARF section is huge, so compress it.
|
|
|
|
# Builds rust using cargo normally
|
|
%.wasm: %.rs
|
|
@(cd $(@D); cargo build --target $(cargo_target) --release)
|
|
@mv $(@D)/target/$(cargo_target)/release/$(@F) $(@D)
|
|
|
|
spectest_base_dir := internal/integration_test/spectest
|
|
|
|
spectest_v1_dir := $(spectest_base_dir)/v1
|
|
spectest_v1_testdata_dir := $(spectest_v1_dir)/testdata
|
|
spec_version_v1 := wg-1.0
|
|
|
|
spectest_v2_dir := $(spectest_base_dir)/v2
|
|
spectest_v2_testdata_dir := $(spectest_v2_dir)/testdata
|
|
spec_version_v2 := wg-2.0
|
|
|
|
spectest_threads_dir := $(spectest_base_dir)/threads
|
|
spectest_threads_testdata_dir := $(spectest_threads_dir)/testdata
|
|
spec_version_threads := ff17701446d8e2086142423ef77ae947a025e26f
|
|
|
|
spectest_tail_call_dir := $(spectest_base_dir)/tail-call
|
|
spectest_tail_call_testdata_dir := $(spectest_tail_call_dir)/testdata
|
|
spec_version_tail_call := 88e97b0f742f4c3ee01fea683da130f344dd7b02
|
|
|
|
spectest_extended_const_dir := $(spectest_base_dir)/extended-const
|
|
spectest_extended_const_testdata_dir := $(spectest_extended_const_dir)/testdata
|
|
|
|
spectest_exception_handling_dir := $(spectest_base_dir)/exception-handling
|
|
spectest_exception_handling_testdata_dir := $(spectest_exception_handling_dir)/testdata
|
|
spec_version_exception_handling := 13734f8fb871a5dab939070f893adbd90bffe28c
|
|
|
|
spectest_typed_function_references_dir := $(spectest_base_dir)/typed-function-references
|
|
spectest_typed_function_references_testdata_dir := $(spectest_typed_function_references_dir)/testdata
|
|
spec_version_typed_function_references := 74d2ec81d15efd3c0f2fba46a023f376101d8e46
|
|
typed_function_references_wast_files := \
|
|
br_on_non_null.wast br_on_null.wast br_table.wast call_ref.wast elem.wast \
|
|
func.wast linking.wast local_init.wast ref.wast ref_as_non_null.wast \
|
|
ref_func.wast ref_is_null.wast ref_null.wast return_call_indirect.wast \
|
|
return_call_ref.wast return_call.wast select.wast table-sub.wast table.wast \
|
|
type-equivalence.wast unreached-invalid.wast unreached-valid.wast
|
|
|
|
.PHONY: build.spectest
|
|
build.spectest:
|
|
@$(MAKE) build.spectest.v1
|
|
@$(MAKE) build.spectest.v2
|
|
@$(MAKE) build.spectest.threads
|
|
@$(MAKE) build.spectest.tail_call
|
|
@$(MAKE) build.spectest.extended_const
|
|
@$(MAKE) build.spectest.exception_handling
|
|
@$(MAKE) build.spectest.typed_function_references
|
|
|
|
.PHONY: build.spectest.v1
|
|
build.spectest.v1: # Note: wabt by default uses >1.0 features, so wast2json flags might drift as they include more. See WebAssembly/wabt#1878
|
|
@rm -rf $(spectest_v1_testdata_dir)
|
|
@mkdir -p $(spectest_v1_testdata_dir)
|
|
@cd $(spectest_v1_testdata_dir) \
|
|
&& curl -sSL 'https://api.github.com/repos/WebAssembly/spec/contents/test/core?ref=$(spec_version_v1)' | jq -r '.[]| .download_url' | grep -E ".wast" | xargs -Iurl curl -sJL url -O
|
|
@cd $(spectest_v1_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
perl -pi -e 's/\(assert_return_canonical_nan\s(\(invoke\s"f32.demote_f64"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \(f32.const nan:canonical\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_arithmetic_nan\s(\(invoke\s"f32.demote_f64"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \(f32.const nan:arithmetic\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_canonical_nan\s(\(invoke\s"f64\.promote_f32"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \(f64.const nan:canonical\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_arithmetic_nan\s(\(invoke\s"f64\.promote_f32"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \(f64.const nan:arithmetic\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_canonical_nan\s(\(invoke\s"[a-z._0-9]+"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \($$2.const nan:canonical\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_arithmetic_nan\s(\(invoke\s"[a-z._0-9]+"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \($$2.const nan:arithmetic\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_canonical_nan\s(\(invoke\s"[a-z._0-9]+"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\s\([a-z0-9.\s+-:]+\)\))\)/\(assert_return $$1 \($$2.const nan:canonical\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_arithmetic_nan\s(\(invoke\s"[a-z._0-9]+"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\s\([a-z0-9.\s+-:]+\)\))\)/\(assert_return $$1 \($$2.const nan:arithmetic\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_canonical_nan\s(\(invoke\s"[a-z._0-9]+"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \($$2.const nan:canonical\)\)/g' $$f; \
|
|
perl -pi -e 's/\(assert_return_arithmetic_nan\s(\(invoke\s"[a-z._0-9]+"\s\((f[0-9]{2})\.const\s[a-z0-9.+:-]+\)\))\)/\(assert_return $$1 \($$2.const nan:arithmetic\)\)/g' $$f; \
|
|
wast2json \
|
|
--disable-saturating-float-to-int \
|
|
--disable-sign-extension \
|
|
--disable-simd \
|
|
--disable-multi-value \
|
|
--disable-bulk-memory \
|
|
--disable-reference-types \
|
|
--debug-names $$f; \
|
|
done
|
|
|
|
.PHONY: build.spectest.v2
|
|
build.spectest.v2: # Note: SIMD cases are placed in the "simd" subdirectory.
|
|
@rm -rf $(spectest_v2_testdata_dir)
|
|
@mkdir -p $(spectest_v2_testdata_dir)
|
|
@cd $(spectest_v2_testdata_dir) \
|
|
&& curl -sSL 'https://api.github.com/repos/WebAssembly/spec/contents/test/core?ref=$(spec_version_v2)' | jq -r '.[]| .download_url' | grep -E ".wast" | xargs -Iurl curl -sJL url -O
|
|
@cd $(spectest_v2_testdata_dir) \
|
|
&& curl -sSL 'https://api.github.com/repos/WebAssembly/spec/contents/test/core/simd?ref=$(spec_version_v2)' | jq -r '.[]| .download_url' | grep -E ".wast" | xargs -Iurl curl -sJL url -O
|
|
@cd $(spectest_v2_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
wast2json --debug-names --no-check $$f || true; \
|
|
done # Ignore the error here as some tests (e.g. comments.wast right now) are not supported by wast2json yet.
|
|
|
|
# Note: We currently cannot build the "threads" subdirectory that spawns threads due to missing support in wast2json.
|
|
# https://github.com/WebAssembly/wabt/issues/2348#issuecomment-1878003959
|
|
.PHONY: build.spectest.threads
|
|
build.spectest.threads:
|
|
@rm -rf $(spectest_threads_testdata_dir)
|
|
@mkdir -p $(spectest_threads_testdata_dir)
|
|
@cd $(spectest_threads_testdata_dir) \
|
|
&& curl -sSL 'https://api.github.com/repos/WebAssembly/threads/contents/test/core/threads?ref=$(spec_version_threads)' | jq -r '.[]| .download_url' | grep -E "/atomic.wast" | xargs -Iurl curl -sJL url -O
|
|
@cd $(spectest_threads_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
wast2json --enable-threads --debug-names $$f; \
|
|
done
|
|
|
|
.PHONY: build.spectest.extended_const
|
|
build.spectest.extended_const:
|
|
@cd $(spectest_extended_const_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
wast2json --enable-extended-const --debug-names $$f; \
|
|
done
|
|
|
|
.PHONY: build.spectest.tail_call
|
|
build.spectest.tail_call:
|
|
@rm -rf $(spectest_tail_call_testdata_dir)
|
|
@mkdir -p $(spectest_tail_call_testdata_dir)
|
|
@cd $(spectest_tail_call_testdata_dir) \
|
|
&& curl -sSL 'https://api.github.com/repos/WebAssembly/testsuite/contents/proposals/tail-call?ref=$(spec_version_tail_call)' | jq -r '.[]| .download_url' | grep -E ".wast" | xargs -Iurl curl -sJL url -O
|
|
@cd $(spectest_tail_call_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
wast2json --enable-tail-call --debug-names $$f; \
|
|
done
|
|
|
|
.PHONY: build.spectest.exception_handling
|
|
build.spectest.exception_handling:
|
|
@rm -rf $(spectest_exception_handling_testdata_dir)
|
|
@mkdir -p $(spectest_exception_handling_testdata_dir)
|
|
@cd $(spectest_exception_handling_testdata_dir) \
|
|
&& curl -sSL 'https://api.github.com/repos/WebAssembly/spec/contents/test/core/exceptions?ref=$(spec_version_exception_handling)' \
|
|
| jq -r '.[]| .download_url' | grep -E ".wast" | xargs -Iurl curl -sJL url -O
|
|
@cd $(spectest_exception_handling_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
wasm-tools json-from-wast --wasm-dir . -o $$(basename $$f .wast).json $$f || true; \
|
|
done
|
|
|
|
.PHONY: build.spectest.typed_function_references
|
|
build.spectest.typed_function_references:
|
|
@rm -rf $(spectest_typed_function_references_testdata_dir)
|
|
@mkdir -p $(spectest_typed_function_references_testdata_dir)
|
|
@cd $(spectest_typed_function_references_testdata_dir) \
|
|
&& for f in $(typed_function_references_wast_files); do \
|
|
curl -sJL "https://raw.githubusercontent.com/WebAssembly/function-references/$(spec_version_typed_function_references)/test/core/$$f" -O; \
|
|
done
|
|
@cd $(spectest_typed_function_references_testdata_dir) && for f in `find . -name '*.wast'`; do \
|
|
wasm-tools json-from-wast --wasm-dir . -o $$(basename $$f .wast).json $$f || true; \
|
|
done
|
|
|
|
.PHONY: test
|
|
test:
|
|
@go test $(go_test_options) ./...
|
|
@cd internal/version/testdata && go test $(go_test_options) ./...
|
|
@cd internal/integration_test/fuzz/wazerolib && CGO_ENABLED=0 WASM_BINARY_PATH=testdata/test.wasm go test ./...
|
|
|
|
.PHONY: coverage
|
|
# replace spaces with commas
|
|
coverpkg = $(shell echo $(main_packages) | tr ' ' ',')
|
|
coverage: ## Generate test coverage
|
|
@go test -coverprofile=coverage.txt -covermode=atomic --coverpkg=$(coverpkg) $(main_packages)
|
|
@go tool cover -func coverage.txt
|
|
|
|
golangci_lint_path := $(shell go env GOPATH)/bin/golangci-lint
|
|
|
|
$(golangci_lint_path):
|
|
@go install $(golangci_lint)
|
|
|
|
golangci_lint_goarch ?= $(shell go env GOARCH)
|
|
|
|
.PHONY: lint
|
|
lint: $(golangci_lint_path)
|
|
@GOARCH=$(golangci_lint_goarch) CGO_ENABLED=0 $(golangci_lint_path) run --timeout 5m -E testableexamples
|
|
|
|
.PHONY: format
|
|
format:
|
|
@go run $(gofumpt) -l -w .
|
|
@go run $(gosimports) -local github.com/tetratelabs/ -w $(shell find . -name '*.go' -type f)
|
|
@go run $(asmfmt) -w $(shell find . -name '*.s' -type f)
|
|
|
|
.PHONY: check # Pre-flight check for pull requests
|
|
check:
|
|
# The following checks help ensure our platform-specific code used for system
|
|
# calls safely falls back on a platform unsupported by the compiler engine.
|
|
# This makes sure the intepreter can be used. Most often the package that can
|
|
# drift here is "platform" or "sysfs":
|
|
#
|
|
# Ensure we build on plan9. See #1578
|
|
@GOARCH=amd64 GOOS=plan9 go build ./...
|
|
# Ensure we build on gojs. See #1526.
|
|
@GOARCH=wasm GOOS=js go build ./...
|
|
# Ensure we build on wasip1. See #1526.
|
|
@GOARCH=wasm GOOS=wasip1 go build ./...
|
|
# Ensure we build on aix. See #1723
|
|
@GOARCH=ppc64 GOOS=aix go build ./...
|
|
# Ensure we build on linux s390x. See #2412
|
|
@GOARCH=s390x GOOS=linux go build ./...
|
|
# Ensure we build on linux ppc64le. See #2412
|
|
@GOARCH=ppc64le GOOS=linux go build ./...
|
|
# Ensure we build on linux arm for Dapr:
|
|
# gh release view -R dapr/dapr --json assets --jq 'first(.assets[] | select(.name = "daprd_linux_arm.tar.gz") | {url, downloadCount})'
|
|
@GOARCH=arm GOOS=linux go build ./...
|
|
# Ensure we build on linux 386 for Trivy:
|
|
# gh release view -R aquasecurity/trivy --json assets --jq 'first(.assets[] | select(.name| test("Linux-32bit.*tar.gz")) | {url, downloadCount})'
|
|
@GOARCH=386 GOOS=linux go build ./...
|
|
# Ensure we build on FreeBSD amd64 for Trivy:
|
|
# gh release view -R aquasecurity/trivy --json assets --jq 'first(.assets[] | select(.name| test("FreeBSD-64bit.*tar.gz")) | {url, downloadCount})'
|
|
@GOARCH=amd64 GOOS=freebsd go build ./...
|
|
@$(MAKE) lint golangci_lint_goarch=arm64
|
|
@$(MAKE) lint golangci_lint_goarch=amd64
|
|
@$(MAKE) format
|
|
@go mod tidy
|
|
@if [ ! -z "`git status -s`" ]; then \
|
|
echo "The following differences will fail CI until committed:"; \
|
|
git diff --exit-code; \
|
|
fi
|
|
|
|
.PHONY: site
|
|
site: ## Serve website content
|
|
@git submodule update --init
|
|
@cd site && go run $(hugo) server --minify --disableFastRender --baseURL localhost:1313 --cleanDestinationDir -D
|
|
|
|
.PHONY: clean
|
|
clean: ## Ensure a clean build
|
|
@rm -rf dist build coverage.txt
|
|
@go clean -testcache
|
|
|
|
fuzz_default_flags := --no-trace-compares --sanitizer=none -- -rss_limit_mb=8192
|
|
|
|
fuzz_timeout_seconds ?= 10
|
|
.PHONY: fuzz
|
|
fuzz:
|
|
@cd internal/integration_test/fuzz && cargo test
|
|
@cd internal/integration_test/fuzz && cargo fuzz run logging_no_diff $(fuzz_default_flags) -max_total_time=$(fuzz_timeout_seconds)
|
|
@cd internal/integration_test/fuzz && cargo fuzz run no_diff $(fuzz_default_flags) -max_total_time=$(fuzz_timeout_seconds)
|
|
@cd internal/integration_test/fuzz && cargo fuzz run memory_no_diff $(fuzz_default_flags) -max_total_time=$(fuzz_timeout_seconds)
|
|
@cd internal/integration_test/fuzz && cargo fuzz run validation $(fuzz_default_flags) -max_total_time=$(fuzz_timeout_seconds)
|
|
|
|
libsodium:
|
|
cd ./internal/integration_test/libsodium/testdata && \
|
|
curl -s "https://api.github.com/repos/jedisct1/webassembly-benchmarks/contents/2022-12/wasm?ref=7e86d68e99e60130899fbe3b3ab6e9dce9187a7c" \
|
|
| jq -r '.[] | .download_url' | xargs -n 1 curl -LO
|
|
|
|
#### CLI release related ####
|
|
|
|
VERSION ?= dev
|
|
non_windows_platforms := darwin_amd64 darwin_arm64 linux_amd64 linux_arm64
|
|
non_windows_archives := $(non_windows_platforms:%=dist/wazero_$(VERSION)_%.tar.gz)
|
|
windows_platforms := windows_amd64 # TODO: add arm64 windows once we start testing on it.
|
|
windows_archives := $(windows_platforms:%=dist/wazero_$(VERSION)_%.zip)
|
|
checksum_txt := dist/wazero_$(VERSION)_checksums.txt
|
|
|
|
# define macros for multi-platform builds. these parse the filename being built
|
|
go-arch = $(if $(findstring amd64,$1),amd64,arm64)
|
|
go-os = $(if $(findstring .exe,$1),windows,$(if $(findstring linux,$1),linux,darwin))
|
|
|
|
build/wazero_%/wazero:
|
|
$(call go-build,$@,$<)
|
|
|
|
build/wazero_%/wazero.exe:
|
|
$(call go-build,$@,$<)
|
|
|
|
dist/wazero_$(VERSION)_%.tar.gz: build/wazero_%/wazero
|
|
@echo tar.gz "tarring $@"
|
|
@mkdir -p $(@D)
|
|
# On Windows, we pass the special flag `--mode='+rx' to ensure that we set the executable flag.
|
|
# This is only supported by GNU Tar, so we set it conditionally.
|
|
@tar -C $(<D) -cpzf $@ $(if $(findstring Windows_NT,$(OS)),--mode='+rx',) $(<F)
|
|
@echo tar.gz "ok"
|
|
|
|
define go-build
|
|
@echo "building $1"
|
|
@# $(go:go=) removes the trailing 'go', so we can insert cross-build variables
|
|
@$(go:go=) CGO_ENABLED=0 GOOS=$(call go-os,$1) GOARCH=$(call go-arch,$1) go build \
|
|
-ldflags "-s -w -X github.com/tetratelabs/wazero/internal/version.version=$(VERSION)" \
|
|
-o $1 $2 ./cmd/wazero
|
|
@echo build "ok"
|
|
endef
|
|
|
|
dist/wazero_$(VERSION)_%.zip: build/wazero_%/wazero.exe
|
|
@echo zip "zipping $@"
|
|
@mkdir -p $(@D)
|
|
@zip -qj $@ $<
|
|
@echo zip "ok"
|
|
|
|
# Darwin doesn't have sha256sum. See https://github.com/actions/virtual-environments/issues/90
|
|
sha256sum := $(if $(findstring darwin,$(shell go env GOOS)),shasum -a 256,sha256sum)
|
|
$(checksum_txt): $(non_windows_archives) $(windows_archives)
|
|
@cd $(@D); touch $(@F); $(sha256sum) * > $(@F)
|
|
|
|
dist: $(non_windows_archives) $(windows_archives) $(checksum_txt)
|