Skip to content

Commit c1e90a3

Browse files
committed
Test WASM side module with Wasmtime CLI
1 parent dd87821 commit c1e90a3

4 files changed

Lines changed: 207 additions & 1 deletion

File tree

‎.github/workflows/publish-wasm-extension-artifact.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ on:
2222
# Any needed permissions should be configured at the job level.
2323
permissions: {}
2424

25+
env:
26+
WASMTIME_VERSION: v46.0.1
27+
WASMTIME_X86_64_LINUX_SHA256: 9ae0b17ea298bcc52277a8208d6ab7fae8e1a89579672f9d82f9d86c116edb62
28+
2529
concurrency:
2630
group: ${{ github.workflow }}-${{ github.ref }}
2731
cancel-in-progress: true
@@ -137,6 +141,18 @@ jobs:
137141
- name: Load Playground base image
138142
run: docker load --input "${RUNNER_TEMP}/playground-base-image/playground-php-wasm-base.tar"
139143

144+
- name: Install Wasmtime CLI
145+
run: |
146+
set -euo pipefail
147+
archive="wasmtime-${WASMTIME_VERSION}-x86_64-linux.tar.xz"
148+
url="https://github.com/bytecodealliance/wasmtime/releases/download/${WASMTIME_VERSION}/${archive}"
149+
curl -fsSL "$url" --output "${RUNNER_TEMP}/${archive}"
150+
echo "${WASMTIME_X86_64_LINUX_SHA256} ${RUNNER_TEMP}/${archive}" | sha256sum --check -
151+
mkdir -p "${RUNNER_TEMP}/wasmtime"
152+
tar -xJf "${RUNNER_TEMP}/${archive}" -C "${RUNNER_TEMP}/wasmtime" --strip-components=1
153+
"${RUNNER_TEMP}/wasmtime/wasmtime" --version
154+
echo "${RUNNER_TEMP}/wasmtime" >> "$GITHUB_PATH"
155+
140156
- name: Install Playground deps
141157
working-directory: wordpress-playground
142158
run: npm ci --ignore-scripts
@@ -165,6 +181,14 @@ jobs:
165181
ASYNC_MODE: jspi
166182
run: node check-playground-web-compat.mjs
167183

184+
- name: Validate the side module with Wasmtime CLI
185+
working-directory: sqlite-database-integration/packages/php-ext-wp-mysql-parser/wasm-spike
186+
env:
187+
PHP_VERSION: ${{ matrix.php }}
188+
ASYNC_MODE: jspi
189+
WASMTIME_EXPECTED_VERSION: ${{ env.WASMTIME_VERSION }}
190+
run: node check-wasmtime-cli.mjs
191+
168192
- name: Upload side module
169193
uses: actions/upload-artifact@v4
170194
with:

‎.github/workflows/wasm-spike.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ on:
2222
# Any needed permissions should be configured at the job level.
2323
permissions: {}
2424

25+
env:
26+
WASMTIME_VERSION: v46.0.1
27+
WASMTIME_X86_64_LINUX_SHA256: 9ae0b17ea298bcc52277a8208d6ab7fae8e1a89579672f9d82f9d86c116edb62
28+
2529
jobs:
2630
base-image:
2731
name: Build shared Playground base image
@@ -134,6 +138,18 @@ jobs:
134138
- name: Load Playground base image
135139
run: docker load --input "${RUNNER_TEMP}/playground-base-image/playground-php-wasm-base.tar"
136140

141+
- name: Install Wasmtime CLI
142+
run: |
143+
set -euo pipefail
144+
archive="wasmtime-${WASMTIME_VERSION}-x86_64-linux.tar.xz"
145+
url="https://github.com/bytecodealliance/wasmtime/releases/download/${WASMTIME_VERSION}/${archive}"
146+
curl -fsSL "$url" --output "${RUNNER_TEMP}/${archive}"
147+
echo "${WASMTIME_X86_64_LINUX_SHA256} ${RUNNER_TEMP}/${archive}" | sha256sum --check -
148+
mkdir -p "${RUNNER_TEMP}/wasmtime"
149+
tar -xJf "${RUNNER_TEMP}/${archive}" -C "${RUNNER_TEMP}/wasmtime" --strip-components=1
150+
"${RUNNER_TEMP}/wasmtime/wasmtime" --version
151+
echo "${RUNNER_TEMP}/wasmtime" >> "$GITHUB_PATH"
152+
137153
- name: Install Playground deps
138154
working-directory: wordpress-playground
139155
run: npm ci --ignore-scripts
@@ -175,6 +191,14 @@ jobs:
175191
ASYNC_MODE: ${{ matrix.async-mode }}
176192
run: node check-playground-web-compat.mjs
177193

194+
- name: Validate the side module with Wasmtime CLI
195+
working-directory: sqlite-database-integration/packages/php-ext-wp-mysql-parser/wasm-spike
196+
env:
197+
PHP_VERSION: ${{ matrix.php }}
198+
ASYNC_MODE: ${{ matrix.async-mode }}
199+
WASMTIME_EXPECTED_VERSION: ${{ env.WASMTIME_VERSION }}
200+
run: node check-wasmtime-cli.mjs
201+
178202
- name: Load the extension in Playground and parse a query
179203
working-directory: sqlite-database-integration/packages/php-ext-wp-mysql-parser/wasm-spike
180204
env:

‎packages/php-ext-wp-mysql-parser/wasm-spike/RESULT.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ The spike now builds and loads `wp_mysql_parser` in Playground across every
66
PHP version supported by the current `ext-php-rs` binding layer: 8.0 through
77
8.5, all JSPI. CI verifies each generated side module by loading its manifest
88
through Playground's compile-extension test harness and running a native lexer
9-
smoke test. PHP 7.4 is outside this Rust path because `ext-php-rs` 0.15
9+
smoke test. CI also validates every generated side module with the pinned
10+
Wasmtime CLI: it parses the module with Node's WebAssembly API, confirms the
11+
PHP extension import/export shape, precompiles it with `wasmtime compile`,
12+
checks the compiled output with `wasmtime objdump`, and asserts `wasmtime run`
13+
rejects it as an Emscripten PHP side module rather than a standalone WASI
14+
command. The Wasmtime compile/run probes enable all implemented WebAssembly
15+
proposal gates because the Playground JSPI PHP side modules use legacy-encoded
16+
exception instructions that are not covered by Wasmtime 46's narrower
17+
`exceptions` CLI flag. PHP 7.4 is
18+
outside this Rust path because `ext-php-rs` 0.15
1019
depends on PHP 8 Zend APIs and does not compile against PHP 7.4 headers.
1120

1221
## PHP version scope
@@ -113,6 +122,7 @@ extension build and manifest machinery.
113122
| `write-extension-manifest.mjs` | Writes the combined all-version artifact manifest for the publish workflow. |
114123
| `shim/config.m4` | Minimal phpize wrapper. |
115124
| `shim/wp_mysql_parser_shim.c` | Pulls the Rust `get_module()` symbol into the side-module link. |
125+
| `check-wasmtime-cli.mjs` | Validates the built side module with the pinned Wasmtime CLI. |
116126
| `run-spike.mjs` | Loads the generated manifest in Playground and verifies the native lexer. |
117127

118128
## Reproduce
@@ -123,6 +133,9 @@ cd packages/php-ext-wp-mysql-parser/wasm-spike
123133
PLAYGROUND_REPO=/abs/path/to/wordpress-playground \
124134
bash build-in-docker-rust.sh
125135

136+
PLAYGROUND_REPO=/abs/path/to/wordpress-playground PHP_VERSION=8.4 \
137+
node check-wasmtime-cli.mjs
138+
126139
PLAYGROUND_REPO=/abs/path/to/wordpress-playground PHP_VERSION=8.4 \
127140
node run-spike.mjs
128141
```
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env node
2+
import { spawnSync } from 'node:child_process';
3+
import {
4+
existsSync,
5+
mkdtempSync,
6+
readFileSync,
7+
rmSync,
8+
statSync,
9+
} from 'node:fs';
10+
import { tmpdir } from 'node:os';
11+
import { basename, join, resolve } from 'node:path';
12+
import { fileURLToPath } from 'node:url';
13+
14+
const SPIKE_DIR = resolve(fileURLToPath(new URL('.', import.meta.url)));
15+
const PHP_VERSION = process.env.PHP_VERSION || '8.4';
16+
const ASYNC_MODE = process.env.ASYNC_MODE || 'jspi';
17+
const EXTENSION_NAME = process.env.EXTENSION_NAME || 'wp_mysql_parser';
18+
const SIDE_MODULE = resolve(
19+
process.env.SIDE_MODULE ||
20+
`${SPIKE_DIR}/dist/${EXTENSION_NAME}-php${PHP_VERSION}-${ASYNC_MODE}.so`
21+
);
22+
const WASMTIME = process.env.WASMTIME || 'wasmtime';
23+
const EXPECTED_WASMTIME_VERSION =
24+
process.env.WASMTIME_EXPECTED_VERSION || process.env.WASMTIME_VERSION || '';
25+
const WASMTIME_WASM_FLAGS = ['-W', 'all-proposals=y'];
26+
27+
function fail(message) {
28+
console.error(`[wasmtime-cli] ${message}`);
29+
process.exit(1);
30+
}
31+
32+
function runWasmtime(args, options = {}) {
33+
return spawnSync(WASMTIME, args, {
34+
encoding: 'utf8',
35+
...options,
36+
});
37+
}
38+
39+
function requireSuccess(label, result) {
40+
if (result.status === 0) {
41+
return;
42+
}
43+
44+
fail(
45+
`${label} failed with exit ${result.status ?? 'unknown'}\n` +
46+
`stdout:\n${result.stdout || '<empty>'}\n` +
47+
`stderr:\n${result.stderr || '<empty>'}`
48+
);
49+
}
50+
51+
if (!existsSync(SIDE_MODULE)) {
52+
fail(`Missing side module: ${SIDE_MODULE}`);
53+
}
54+
55+
const sideModuleBytes = readFileSync(SIDE_MODULE);
56+
let wasmModule;
57+
try {
58+
wasmModule = new WebAssembly.Module(sideModuleBytes);
59+
} catch (error) {
60+
fail(`Node could not parse the side module as WebAssembly: ${error.message}`);
61+
}
62+
63+
const imports = WebAssembly.Module.imports(wasmModule);
64+
const exports = WebAssembly.Module.exports(wasmModule);
65+
const envImports = imports.filter((entry) => entry.module === 'env');
66+
const exportNames = new Set(exports.map((entry) => entry.name));
67+
68+
if (envImports.length === 0) {
69+
fail('Expected Emscripten side-module imports from the env module.');
70+
}
71+
72+
if (!exportNames.has('get_module')) {
73+
fail('Expected the PHP extension side module to export get_module.');
74+
}
75+
76+
const version = runWasmtime(['--version']);
77+
requireSuccess('wasmtime --version', version);
78+
const versionText = `${version.stdout}${version.stderr}`.trim();
79+
const expectedVersionText = EXPECTED_WASMTIME_VERSION.replace(/^v/i, '');
80+
if (
81+
EXPECTED_WASMTIME_VERSION &&
82+
!versionText.includes(EXPECTED_WASMTIME_VERSION) &&
83+
!versionText.includes(expectedVersionText)
84+
) {
85+
fail(
86+
`Expected Wasmtime ${EXPECTED_WASMTIME_VERSION}, got ${JSON.stringify(
87+
versionText
88+
)}`
89+
);
90+
}
91+
console.log(`[wasmtime-cli] ${versionText}`);
92+
console.log(`[wasmtime-cli] wasm flags: ${WASMTIME_WASM_FLAGS.join(' ')}`);
93+
94+
const tempDir = mkdtempSync(join(tmpdir(), 'wp-mysql-parser-wasmtime-'));
95+
try {
96+
const compiledPath = join(tempDir, `${basename(SIDE_MODULE)}.cwasm`);
97+
const compile = runWasmtime([
98+
'compile',
99+
...WASMTIME_WASM_FLAGS,
100+
'--target',
101+
'x86_64-unknown-linux',
102+
'-o',
103+
compiledPath,
104+
SIDE_MODULE,
105+
]);
106+
requireSuccess('wasmtime compile', compile);
107+
if (!existsSync(compiledPath) || statSync(compiledPath).size === 0) {
108+
fail(`wasmtime compile did not produce a non-empty ${compiledPath}`);
109+
}
110+
111+
const objdump = runWasmtime([
112+
'objdump',
113+
'--funcs',
114+
'wasm',
115+
'--filter',
116+
'get_module',
117+
compiledPath,
118+
]);
119+
requireSuccess('wasmtime objdump', objdump);
120+
121+
const run = runWasmtime(['run', ...WASMTIME_WASM_FLAGS, SIDE_MODULE]);
122+
if (run.status === 0) {
123+
fail(
124+
'Expected wasmtime run to reject this Emscripten PHP side module as a standalone WASI command.'
125+
);
126+
}
127+
128+
const runOutput = `${run.stdout}\n${run.stderr}`;
129+
if (
130+
!/import|instantiate|link|unknown|wasi|start|command/i.test(runOutput)
131+
) {
132+
fail(
133+
`wasmtime run failed for an unexpected reason.\n` +
134+
`stdout:\n${run.stdout || '<empty>'}\n` +
135+
`stderr:\n${run.stderr || '<empty>'}`
136+
);
137+
}
138+
139+
console.log(`[wasmtime-cli] side module: ${SIDE_MODULE}`);
140+
console.log(`[wasmtime-cli] imports: ${imports.length} (${envImports.length} from env)`);
141+
console.log(`[wasmtime-cli] exports: ${exports.map((entry) => entry.name).join(', ')}`);
142+
console.log('[wasmtime-cli] compile, objdump, and expected non-standalone run rejection passed.');
143+
} finally {
144+
rmSync(tempDir, { recursive: true, force: true });
145+
}

0 commit comments

Comments
 (0)