spacewasm 0.1.2

A no_std WebAssembly 1.0 decoder, validator, and interpreter for on-board spacecraft use
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
name: CI

on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

env:
  RUST_BACKTRACE: 1
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test (${{ matrix.os }}, ${{ matrix.profile }})
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # macOS ARM64
          - os: macos-arm64
            runner: macos-14
            profile: debug
          - os: macos-arm64
            runner: macos-14
            profile: release

          # Linux x86_64
          - os: linux-x86
            runner: ubuntu-latest
            profile: debug
          - os: linux-x86
            runner: ubuntu-latest
            profile: release

          # Linux ARM64
          - os: linux-arm64
            runner: ubuntu-24.04-arm
            profile: debug
          - os: linux-arm64
            runner: ubuntu-24.04-arm
            profile: release

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Install WABT (WebAssembly Binary Toolkit)
        run: |
          WABT_VERSION=1.0.41
          if [ "$RUNNER_OS" == "macOS" ]; then
            if [ "$RUNNER_ARCH" == "ARM64" ]; then
              WABT_PLATFORM="macos-arm64"
            else
              WABT_PLATFORM="macos-x64"
            fi
          else
            # Linux
            if [ "$RUNNER_ARCH" == "ARM64" ]; then
              WABT_PLATFORM="linux-arm64"
            else
              WABT_PLATFORM="linux-x64"
            fi
          fi
          wget https://github.com/WebAssembly/wabt/releases/download/${WABT_VERSION}/wabt-${WABT_VERSION}-${WABT_PLATFORM}.tar.gz
          tar -xzf wabt-${WABT_VERSION}-${WABT_PLATFORM}.tar.gz
          sudo cp wabt-${WABT_VERSION}/bin/* /usr/local/bin/
        shell: bash

      - name: Cache Cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-${{ runner.arch }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ runner.arch }}-cargo-registry-

      - name: Cache Cargo git
        uses: actions/cache@v4
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-${{ runner.arch }}-cargo-git-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ runner.arch }}-cargo-git-

      - name: Cache build artifacts
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-${{ runner.arch }}-cargo-build-${{ matrix.profile }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ runner.arch }}-cargo-build-${{ matrix.profile }}-

      - name: Run tests (debug)
        if: matrix.profile == 'debug'
        run: cargo test --workspace --verbose

      - name: Run tests (release)
        if: matrix.profile == 'release'
        run: cargo test --workspace --release --verbose

      - name: Run tests with strict-assertions (debug)
        if: matrix.profile == 'debug'
        run: cargo test --workspace --features strict-assertions --verbose

      - name: Run tests with strict-assertions (release)
        if: matrix.profile == 'release'
        run: cargo test --workspace --release --features strict-assertions --verbose

  test-32bit:
    name: Test (i686-unknown-linux-gnu, ${{ matrix.profile }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        profile:
          - debug
          - release

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install 32-bit system libraries
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-multilib

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: i686-unknown-linux-gnu

      - name: Install WABT (WebAssembly Binary Toolkit)
        run: |
          WABT_VERSION=1.0.41
          wget https://github.com/WebAssembly/wabt/releases/download/${WABT_VERSION}/wabt-${WABT_VERSION}-linux-x64.tar.gz
          tar -xzf wabt-${WABT_VERSION}-linux-x64.tar.gz
          sudo cp wabt-${WABT_VERSION}/bin/* /usr/local/bin/
        shell: bash

      # Only the interpreter library is deployed to the 32-bit target, so the
      # host-only workspace tooling is not built here. Running the suite on a
      # 32-bit `usize` guards pointer-width regressions (e.g. memory
      # effective-address overflow) that the 64-bit test matrix cannot observe.
      - name: Run tests (debug)
        if: matrix.profile == 'debug'
        run: cargo test -p spacewasm --target i686-unknown-linux-gnu --verbose

      - name: Run tests (release)
        if: matrix.profile == 'release'
        run: cargo test -p spacewasm --release --target i686-unknown-linux-gnu --verbose

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Run clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

  build:
    name: Build (${{ matrix.os }})
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-arm64
            runner: macos-14
          - os: linux-x86
            runner: ubuntu-latest
          - os: linux-arm64
            runner: ubuntu-24.04-arm

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Build all targets
        run: cargo build --workspace --all-targets --verbose

      - name: Build release
        run: cargo build --workspace --release --verbose

  benchmark:
    name: Benchmark
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Install WABT (WebAssembly Binary Toolkit)
        run: |
          WABT_VERSION=1.0.41
          WABT_PLATFORM="linux-x64"
          wget https://github.com/WebAssembly/wabt/releases/download/${WABT_VERSION}/wabt-${WABT_VERSION}-${WABT_PLATFORM}.tar.gz
          tar -xzf wabt-${WABT_VERSION}-${WABT_PLATFORM}.tar.gz
          sudo cp wabt-${WABT_VERSION}/bin/* /usr/local/bin/

      - name: Cache Cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-registry-

      - name: Cache Cargo git
        uses: actions/cache@v4
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-git-

      - name: Cache build artifacts
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-bench-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-build-bench-

      - name: Run CoreMark benchmark
        id: benchmark
        run: |
          # Build and run the benchmark from workspace root, capturing output
          output=$(cargo bench -p spacewasm_std --bench coremark --no-fail-fast 2>&1)
          echo "$output"

          # Extract the CoreMark score from the output
          score=$(echo "$output" | grep "CoreMark Score:" | awk '{print $3}')
          if [ -z "$score" ]; then
            echo "Failed to extract CoreMark score"
            exit 1
          fi

          echo "score=$score" >> $GITHUB_OUTPUT
          echo "Benchmark score: $score"

      - name: Download baseline benchmark
        id: baseline
        continue-on-error: true
        run: |
          # Try to get baseline from main branch
          if [ "${{ github.event_name }}" == "pull_request" ]; then
            git fetch origin main:main
            git checkout main
            baseline_output=$(cargo bench -p spacewasm_std --bench coremark --no-fail-fast 2>&1) || true
            baseline_score=$(echo "$baseline_output" | grep "CoreMark Score:" | awk '{print $3}')
            git checkout -

            if [ -n "$baseline_score" ]; then
              echo "baseline=$baseline_score" >> $GITHUB_OUTPUT
              echo "Baseline score: $baseline_score"
            else
              echo "baseline=" >> $GITHUB_OUTPUT
            fi
          else
            echo "baseline=" >> $GITHUB_OUTPUT
          fi

      - name: Save benchmark results
        if: github.event_name == 'pull_request'
        run: |
          cat > benchmark-results.json <<EOF
          {
            "prNumber": "${{ github.event.pull_request.number }}",
            "currentScore": "${{ steps.benchmark.outputs.score }}",
            "baselineScore": "${{ steps.baseline.outputs.baseline }}"
          }
          EOF

      - name: Upload benchmark results
        if: github.event_name == 'pull_request'
        uses: actions/upload-artifact@v4
        with:
          name: benchmark-results
          path: benchmark-results.json

  model-checking:
    name: Formal Verification
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: 'Run Kani on repo'
        uses: model-checking/kani-github-action@v1.1
        with:
          args: '-j --output-format=terse'

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - name: Install WABT (WebAssembly Binary Toolkit)
        run: |
          WABT_VERSION=1.0.41
          WABT_PLATFORM="linux-x64"
          wget https://github.com/WebAssembly/wabt/releases/download/${WABT_VERSION}/wabt-${WABT_VERSION}-${WABT_PLATFORM}.tar.gz
          tar -xzf wabt-${WABT_VERSION}-${WABT_PLATFORM}.tar.gz
          sudo cp wabt-${WABT_VERSION}/bin/* /usr/local/bin/

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Cache Cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry/index
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-registry-

      - name: Cache Cargo git
        uses: actions/cache@v4
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-git-

      - name: Generate coverage
        run: |
          # Only measure coverage for the main spacewasm library (src/)
          cargo llvm-cov --package spacewasm --lcov --output-path lcov.info
          cargo llvm-cov --package spacewasm --json --output-path coverage.json

      - name: Extract coverage percentage
        id: coverage
        run: |
          # Extract coverage percentage from JSON output
          coverage=$(jq -r '.data[0].totals.lines.percent' coverage.json)
          echo "percentage=$coverage" >> $GITHUB_OUTPUT
          echo "Current coverage: $coverage%"

      - name: Download baseline coverage
        id: baseline-coverage
        continue-on-error: true
        run: |
          # Try to get baseline from main branch
          if [ "${{ github.event_name }}" == "pull_request" ]; then
            git fetch origin main:main
            git checkout main
            cargo llvm-cov --package spacewasm --json --output-path baseline-coverage.json 2>&1 || true
            git checkout -

            if [ -f baseline-coverage.json ]; then
              baseline=$(jq -r '.data[0].totals.lines.percent' baseline-coverage.json)
              echo "baseline=$baseline" >> $GITHUB_OUTPUT
              echo "Baseline coverage: $baseline%"
            else
              echo "baseline=" >> $GITHUB_OUTPUT
            fi
          else
            echo "baseline=" >> $GITHUB_OUTPUT
          fi

      - name: Save coverage results
        if: github.event_name == 'pull_request'
        run: |
          cat > coverage-results.json <<EOF
          {
            "prNumber": "${{ github.event.pull_request.number }}",
            "currentCoverage": "${{ steps.coverage.outputs.percentage }}",
            "baselineCoverage": "${{ steps.baseline-coverage.outputs.baseline }}"
          }
          EOF

      - name: Upload coverage results
        if: github.event_name == 'pull_request'
        uses: actions/upload-artifact@v4
        with:
          name: coverage-results
          path: coverage-results.json

      - name: Upload coverage to Codecov
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: codecov/codecov-action@v4
        with:
          files: lcov.info
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: false
          verbose: true