Skip to content

Commit

Permalink
Merge branch 'cosmwasm/wasmvm_v1.1.1' into 'line/apply_v1.1.1'
Browse files Browse the repository at this point in the history
* wasmvm_v1.1.1: (58 commits)
  [skip ci] Built release libraries
  Bump wasmvm version to 1.1.1
  Upgrade cosmwasm to 1.1.2
  [skip ci] Built release libraries
  assert_approx_eq! - use abs_diff
  test refactor
  assert_approx_eq: make sure arg order doesn't matter
  assert_approx_eq: test more types
  assert_approx_eq: require args to be of one type
  cargo fmt
  assert_approx_eq! - panic msgs closer to std
  Refactor assert_approx_eq!
  wasmvm: assert_approx_eq macro
  [skip ci] Built release libraries
  Bump libwasmvm version to 1.1.0
  [skip ci] Built release libraries
  Use edition 2021
  Refactor get_metrics test to be less strict
  Make GetMetrics result tests less strict
  [skip ci] Built release libraries
  ...
  • Loading branch information
Expotential108 committed Dec 21, 2022
2 parents c1c6d67 + d629aeb commit 473e151
Show file tree
Hide file tree
Showing 55 changed files with 878 additions and 500 deletions.
Empty file added .circleci/config.yml
Empty file.
55 changes: 34 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: all build build-rust build-go test

# Builds the Rust library libwasmvm
BUILDERS_PREFIX := line/wasmvm-builder
BUILDERS_PREFIX := cosmwasm/go-ext-builder:0014
# Contains a full Go dev environment in order to run Go tests on the built library
ALPINE_TESTER := line/wasmvm-builder-alpine
ALPINE_TESTER := cosmwasm/go-ext-builder:0014-alpine

USER_ID := $(shell id -u)
USER_GROUP = $(shell id -g)
Expand Down Expand Up @@ -39,7 +39,7 @@ build-rust: build-rust-release
# In order to use "--features backtraces" here we need a Rust nightly toolchain, which we don't have by default
build-rust-debug:
(cd libwasmvm && cargo build)
cp libwasmvm/target/debug/$(SHARED_LIB_SRC) api/$(SHARED_LIB_DST)
cp libwasmvm/target/debug/$(SHARED_LIB_SRC) internal/api/$(SHARED_LIB_DST)
make update-bindings

# use release build to actually ship - smaller and much faster
Expand All @@ -48,7 +48,7 @@ build-rust-debug:
# enable stripping through cargo (if that is desired).
build-rust-release:
(cd libwasmvm && cargo build --release)
cp libwasmvm/target/release/$(SHARED_LIB_SRC) api/$(SHARED_LIB_DST)
cp libwasmvm/target/release/$(SHARED_LIB_SRC) internal/api/$(SHARED_LIB_DST)
make update-bindings
@ #this pulls out ELF symbols, 80% size reduction!

Expand All @@ -68,42 +68,55 @@ release-build-alpine:
rm -rf libwasmvm/target/release
# build the muslc *.a file
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/libwasmvm:/code $(BUILDERS_PREFIX)-alpine
cp libwasmvm/artifacts/libwasmvm_muslc.a api
cp libwasmvm/artifacts/libwasmvm_muslc.aarch64.a api
cp libwasmvm/artifacts/libwasmvm_muslc.a internal/api
cp libwasmvm/artifacts/libwasmvm_muslc.aarch64.a internal/api
make update-bindings
# try running go tests using this lib with muslc
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go build -tags muslc ./...
# Use package list mode to include all subdirectores. The -count=1 turns off caching.
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go test -ldflags "-linkmode=external -extldflags '-Wl,-z,muldefs -static'" -tags muslc -count=1 ./...
# # try running go tests using this lib with muslc
# docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go build -tags muslc ./...
# # Use package list mode to include all subdirectores. The -count=1 turns off caching.
# docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go test -tags muslc -count=1 ./...

# Creates a release build in a containerized build environment of the shared library for glibc Linux (.so)
release-build-linux:
rm -rf libwasmvm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/libwasmvm:/code $(BUILDERS_PREFIX)-centos7
cp libwasmvm/artifacts/libwasmvm.x86_64.so api
cp libwasmvm/artifacts/libwasmvm.aarch64.so api
cp libwasmvm/artifacts/libwasmvm.x86_64.so internal/api
cp libwasmvm/artifacts/libwasmvm.aarch64.so internal/api
make update-bindings

# Creates a release build in a containerized build environment of the shared library for macOS (.dylib)
release-build-macos:
rm -rf libwasmvm/target/x86_64-apple-darwin/release
rm -rf libwasmvm/target/aarch64-apple-darwin/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/libwasmvm:/code $(BUILDERS_PREFIX)-cross build_macos.sh
cp libwasmvm/artifacts/libwasmvm.dylib api
cp libwasmvm/artifacts/libwasmvm.dylib internal/api
make update-bindings

# Creates a release build in a containerized build environment of the shared library for Windows (.dll)
release-build-windows:
rm -rf libwasmvm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/libwasmvm:/code $(BUILDERS_PREFIX)-cross build_windows.sh
cp libwasmvm/target/x86_64-pc-windows-gnu/release/wasmvm.dll internal/api
make update-bindings

update-bindings:
# After we build libwasmvm, we have to copy the generated bindings for Go code to use.
# We cannot use symlinks as those are not reliably resolved by `go get` (https://github.com/CosmWasm/wasmvm/pull/235).
cp libwasmvm/bindings.h api
# After we build libwasmvm, we have to copy the generated bindings for Go code to use.
# We cannot use symlinks as those are not reliably resolved by `go get` (https://github.com/CosmWasm/wasmvm/pull/235).
cp libwasmvm/bindings.h internal/api

release-build:
# Write like this because those must not run in parallel
make release-build-alpine
make release-build-linux
make release-build-macos
make release-build-windows

test-alpine: release-build-alpine
# try running go tests using this lib with muslc
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go build -tags muslc ./...
# Use package list mode to include all subdirectores. The -count=1 turns off caching.
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go test -tags muslc -count=1 ./...

@# Build a Go demo binary called ./demo that links the static library from the previous step.
@# Whether the result is a statically linked or dynamically linked binary is decided by `go build`
@# and it's a bit unclear how this is decided. We use `file` to see what we got.
Expand All @@ -112,10 +125,10 @@ test-alpine: release-build-alpine

@# Run the demo binary on Alpine machines
@# See https://de.wikipedia.org/wiki/Alpine_Linux#Versionen for supported versions
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.15 ./demo ./api/testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.14 ./demo ./api/testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.13 ./demo ./api/testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.12 ./demo ./api/testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.15 ./demo ./testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.14 ./demo ./testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.13 ./demo ./testdata/hackatom.wasm
docker run --rm --read-only -v $(shell pwd):/mnt/testrun -w /mnt/testrun alpine:3.12 ./demo ./testdata/hackatom.wasm

@# Run binary locally if you are on Linux
@# ./demo ./api/testdata/hackatom.wasm
@# ./demo ./testdata/hackatom.wasm
Binary file removed api/libwasmvm.aarch64.so
Binary file not shown.
7 changes: 0 additions & 7 deletions api/link_glibclinux_x86_64.go

This file was deleted.

35 changes: 17 additions & 18 deletions builders/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# Install C compiler for cross-compilation. This is required by
# Wasmer in https://github.com/wasmerio/wasmer/blob/2.2.1/lib/vm/build.rs.
# For newer versions this might not be needed anymore since build.rs is removed
# in https://github.com/wasmerio/wasmer/pull/2807.
#
# https://unix.stackexchange.com/questions/620205/aarch64-linux-musl-cross-has-a-broken-link-for-ld-musl-aarch64-so-1

# We need aarch64-linux-musl-cross to compile, wget to musl.cc is denied by github actions.
# We will use a copy from an image that already contains aarch64-linux-musl-cross.
FROM mondoolabs/golang:1.15.0-alpine AS builder

# This image is used for two things (which is not ideal, but yeah):
# 1. Build the static Rust library
# 2. Execute Go tests that use and test this library
Expand All @@ -19,15 +8,29 @@ ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH

RUN apk add rustup

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
# with some changes to support our toolchain, etc
RUN set -eux \
&& apk add --no-cache ca-certificates build-base

RUN rustup-init -y --no-modify-path --profile minimal --default-toolchain 1.57.0
RUN wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-musl/rustup-init" \
&& chmod +x rustup-init \
&& ./rustup-init -y --no-modify-path --profile minimal --default-toolchain 1.63.0 \
&& rm rustup-init \
&& chmod -R a+w $RUSTUP_HOME $CARGO_HOME

# Install C compiler for cross-compilation. This is required by
# Wasmer in https://github.com/wasmerio/wasmer/blob/2.2.1/lib/vm/build.rs.
# For newer versions this might not be needed anymore since build.rs is removed
# in https://github.com/wasmerio/wasmer/pull/2807.
#
# https://unix.stackexchange.com/questions/620205/aarch64-linux-musl-cross-has-a-broken-link-for-ld-musl-aarch64-so-1
RUN wget https://musl.cc/aarch64-linux-musl-cross.tgz \
&& tar -xf aarch64-linux-musl-cross.tgz \
&& mv ./aarch64-linux-musl-cross /opt \
&& /opt/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc --version \
&& rm aarch64-linux-musl-cross.tgz

# prepare go cache dirs
RUN mkdir -p /.cache/go-build
Expand All @@ -39,10 +42,6 @@ RUN chmod -R 777 /usr/local/cargo
## COPY BUILD SCRIPTS
WORKDIR /code

COPY --from=builder \
/usr/local/bin/aarch64-linux-musl-cross \
/opt/aarch64-linux-musl-cross

# Add musl Rust targets
RUN rustup target add aarch64-unknown-linux-musl x86_64-unknown-linux-musl

Expand Down
2 changes: 1 addition & 1 deletion builders/Dockerfile.centos7
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ENV RUSTUP_HOME=/usr/local/rustup \

RUN wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init" \
&& chmod +x rustup-init \
&& ./rustup-init -y --no-modify-path --profile minimal --default-toolchain 1.57.0 \
&& ./rustup-init -y --no-modify-path --profile minimal --default-toolchain 1.60.0 \
&& rm rustup-init \
&& chmod -R a+w $RUSTUP_HOME $CARGO_HOME \
&& rustup --version \
Expand Down
10 changes: 6 additions & 4 deletions builders/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
FROM rust:1.57.0-bullseye
FROM rust:1.63.0-bullseye

# Install build dependencies
RUN apt-get update \
&& apt install -y clang gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev build-essential cmake
&& apt install -y clang gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev build-essential cmake \
# Support for Windows cross-compile
mingw-w64

## ADD MACOS SUPPORT

WORKDIR /opt

# Add macOS Rust targets
RUN rustup target add x86_64-apple-darwin aarch64-apple-darwin
# Add macOS and Windows Rust targets
RUN rustup target add x86_64-apple-darwin aarch64-apple-darwin x86_64-pc-windows-gnu

# Build osxcross
# See https://github.com/tpoechtrager/osxcross/blob/master/build.sh#L31-L49 for SDK overview.
Expand Down
3 changes: 3 additions & 0 deletions builders/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Versioned by a simple counter that is not bound to a specific CosmWasm version
# See builders/README.md
#BUILDERS_PREFIX := cosmwasm/go-ext-builder:0013
BUILDERS_PREFIX := line/wasmvm-builder

.PHONY: docker-image-centos7
Expand Down
6 changes: 6 additions & 0 deletions builders/guest/build_windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -o errexit -o nounset -o pipefail

# ref: https://www.reddit.com/r/rust/comments/5k8uab/crosscompiling_from_ubuntu_to_windows_with_rustup/

cargo build --release --target x86_64-pc-windows-gnu
4 changes: 4 additions & 0 deletions builders/guest/cargo-config
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ ar = "aarch64-apple-darwin20.4-ar"
[target.aarch64-unknown-linux-gnu]
linker = "/usr/aarch64-linux-gnu/bin/ld"
ar = "/usr/aarch64-linux-gnu/bin/ar"

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"
6 changes: 4 additions & 2 deletions ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"io/ioutil"
"testing"

"github.com/line/wasmvm/api"
"github.com/line/wasmvm/internal/api"
"github.com/line/wasmvm/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const IBC_TEST_CONTRACT = "./api/testdata/ibc_reflect.wasm"
const IBC_TEST_CONTRACT = "./testdata/ibc_reflect.wasm"

func TestIBC(t *testing.T) {
vm := withVM(t)
Expand Down Expand Up @@ -284,6 +284,7 @@ func TestAnalyzeCode(t *testing.T) {
require.NoError(t, err)
require.False(t, report.HasIBCEntryPoints)
require.Equal(t, "", report.RequiredFeatures)
require.Equal(t, "", report.RequiredCapabilities)

// Store IBC contract
wasm2, err := ioutil.ReadFile(IBC_TEST_CONTRACT)
Expand All @@ -295,6 +296,7 @@ func TestAnalyzeCode(t *testing.T) {
require.NoError(t, err)
require.True(t, report2.HasIBCEntryPoints)
require.Equal(t, "iterator,stargate", report2.RequiredFeatures)
require.Equal(t, "iterator,stargate", report2.RequiredCapabilities)
}

func TestIBCMsgGetChannel(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion api/api_test.go → internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestValidateAddressFailure(t *testing.T) {
defer cleanup()

// create contract
wasm, err := ioutil.ReadFile("./testdata/hackatom.wasm")
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)
checksum, err := Create(cache, wasm)
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions api/bindings.h → internal/api/bindings.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* (c) 2019 Confio UO. Licensed under Apache-2.0 */
/* (c) 2019 Confio . Licensed under Apache-2.0 */

/* Generated with cbindgen:0.18.0 */
/* Generated with cbindgen:0.24.3 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

Expand Down Expand Up @@ -196,17 +196,17 @@ typedef struct UnmanagedVector {
/**
* The result type of the FFI function analyze_code.
*
* Please note that the unmanaged vector in `required_features`
* Please note that the unmanaged vector in `required_capabilities`
* has to be destroyed exactly once. When calling `analyze_code`
* from Go this is done via `C.destroy_unmanaged_vector`.
*/
typedef struct AnalysisReport {
bool has_ibc_entry_points;
/**
* An UTF-8 encoded comma separated list of reqired features.
* An UTF-8 encoded comma separated list of reqired capabilities.
* This is never None/nil.
*/
struct UnmanagedVector required_features;
struct UnmanagedVector required_capabilities;
} AnalysisReport;

typedef struct Metrics {
Expand Down Expand Up @@ -304,7 +304,7 @@ typedef struct GoQuerier {
} GoQuerier;

struct cache_t *init_cache(struct ByteSliceView data_dir,
struct ByteSliceView supported_features,
struct ByteSliceView available_capabilities,
uint32_t cache_size,
uint32_t instance_memory_limit,
struct UnmanagedVector *error_msg);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions api/lib.go → internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ func AnalyzeCode(cache Cache, checksum []byte) (*types.AnalysisReport, error) {
if err != nil {
return nil, errorWithMessage(err, errmsg)
}
requiredCapabilities := string(copyAndDestroyUnmanagedVector(report.required_capabilities))
res := types.AnalysisReport{
HasIBCEntryPoints: bool(report.has_ibc_entry_points),
RequiredFeatures: string(copyAndDestroyUnmanagedVector(report.required_features)),
HasIBCEntryPoints: bool(report.has_ibc_entry_points),
RequiredFeatures: requiredCapabilities,
RequiredCapabilities: requiredCapabilities,
}
return &res, nil
}
Expand Down
Loading

0 comments on commit 473e151

Please sign in to comment.