Skip to content

Commit b7ed25e

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

4 files changed

Lines changed: 239 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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ 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, and invokes the pinned `wasmtime` CLI with
12+
the same proposal gates used by CI. When Wasmtime can precompile the module,
13+
the check verifies `wasmtime compile`, checks the compiled output with
14+
`wasmtime objdump`, and asserts `wasmtime run` rejects it as an Emscripten PHP
15+
side module rather than a standalone WASI command. Current Playground JSPI PHP
16+
side modules use legacy-encoded exception instructions that Wasmtime 46 reports
17+
as `legacy_exceptions feature required for try instruction`, so CI also accepts
18+
that exact validator diagnostic from both `wasmtime compile` and `wasmtime run`
19+
as the expected Wasmtime compatibility result. PHP 7.4 is
20+
outside this Rust path because `ext-php-rs` 0.15
1021
depends on PHP 8 Zend APIs and does not compile against PHP 7.4 headers.
1122

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

118130
## Reproduce
@@ -123,6 +135,9 @@ cd packages/php-ext-wp-mysql-parser/wasm-spike
123135
PLAYGROUND_REPO=/abs/path/to/wordpress-playground \
124136
bash build-in-docker-rust.sh
125137

138+
PLAYGROUND_REPO=/abs/path/to/wordpress-playground PHP_VERSION=8.4 \
139+
node check-wasmtime-cli.mjs
140+
126141
PLAYGROUND_REPO=/abs/path/to/wordpress-playground PHP_VERSION=8.4 \
127142
node run-spike.mjs
128143
```
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
const LEGACY_EXCEPTIONS_DIAGNOSTIC =
27+
/legacy_exceptions feature required for try instruction/i;
28+
29+
function fail(message) {
30+
console.error(`[wasmtime-cli] ${message}`);
31+
process.exit(1);
32+
}
33+
34+
function runWasmtime(args, options = {}) {
35+
return spawnSync(WASMTIME, args, {
36+
encoding: 'utf8',
37+
...options,
38+
});
39+
}
40+
41+
function requireSuccess(label, result) {
42+
if (result.status === 0) {
43+
return;
44+
}
45+
46+
failWithCommandOutput(label, result);
47+
}
48+
49+
function failWithCommandOutput(label, result) {
50+
fail(
51+
`${label} failed with exit ${result.status ?? 'unknown'}\n` +
52+
`stdout:\n${result.stdout || '<empty>'}\n` +
53+
`stderr:\n${result.stderr || '<empty>'}`
54+
);
55+
}
56+
57+
function commandOutput(result) {
58+
return `${result.stdout || ''}\n${result.stderr || ''}`;
59+
}
60+
61+
function isLegacyExceptionsRejection(result) {
62+
return result.status !== 0 && LEGACY_EXCEPTIONS_DIAGNOSTIC.test(commandOutput(result));
63+
}
64+
65+
if (!existsSync(SIDE_MODULE)) {
66+
fail(`Missing side module: ${SIDE_MODULE}`);
67+
}
68+
69+
const sideModuleBytes = readFileSync(SIDE_MODULE);
70+
let wasmModule;
71+
try {
72+
wasmModule = new WebAssembly.Module(sideModuleBytes);
73+
} catch (error) {
74+
fail(`Node could not parse the side module as WebAssembly: ${error.message}`);
75+
}
76+
77+
const imports = WebAssembly.Module.imports(wasmModule);
78+
const exports = WebAssembly.Module.exports(wasmModule);
79+
const envImports = imports.filter((entry) => entry.module === 'env');
80+
const exportNames = new Set(exports.map((entry) => entry.name));
81+
82+
if (envImports.length === 0) {
83+
fail('Expected Emscripten side-module imports from the env module.');
84+
}
85+
86+
if (!exportNames.has('get_module')) {
87+
fail('Expected the PHP extension side module to export get_module.');
88+
}
89+
90+
const version = runWasmtime(['--version']);
91+
requireSuccess('wasmtime --version', version);
92+
const versionText = `${version.stdout}${version.stderr}`.trim();
93+
const expectedVersionText = EXPECTED_WASMTIME_VERSION.replace(/^v/i, '');
94+
if (
95+
EXPECTED_WASMTIME_VERSION &&
96+
!versionText.includes(EXPECTED_WASMTIME_VERSION) &&
97+
!versionText.includes(expectedVersionText)
98+
) {
99+
fail(
100+
`Expected Wasmtime ${EXPECTED_WASMTIME_VERSION}, got ${JSON.stringify(
101+
versionText
102+
)}`
103+
);
104+
}
105+
console.log(`[wasmtime-cli] ${versionText}`);
106+
console.log(`[wasmtime-cli] wasm flags: ${WASMTIME_WASM_FLAGS.join(' ')}`);
107+
108+
const tempDir = mkdtempSync(join(tmpdir(), 'wp-mysql-parser-wasmtime-'));
109+
try {
110+
const compiledPath = join(tempDir, `${basename(SIDE_MODULE)}.cwasm`);
111+
const compile = runWasmtime([
112+
'compile',
113+
...WASMTIME_WASM_FLAGS,
114+
'--target',
115+
'x86_64-unknown-linux',
116+
'-o',
117+
compiledPath,
118+
SIDE_MODULE,
119+
]);
120+
const run = runWasmtime(['run', ...WASMTIME_WASM_FLAGS, SIDE_MODULE]);
121+
122+
if (compile.status !== 0) {
123+
if (!isLegacyExceptionsRejection(compile)) {
124+
failWithCommandOutput('wasmtime compile', compile);
125+
}
126+
if (!isLegacyExceptionsRejection(run)) {
127+
failWithCommandOutput('wasmtime run legacy-exceptions rejection check', run);
128+
}
129+
130+
console.log(`[wasmtime-cli] side module: ${SIDE_MODULE}`);
131+
console.log(`[wasmtime-cli] imports: ${imports.length} (${envImports.length} from env)`);
132+
console.log(`[wasmtime-cli] exports: ${exports.map((entry) => entry.name).join(', ')}`);
133+
console.log(
134+
'[wasmtime-cli] compile and run reached Wasmtime validation and rejected legacy exception instructions as expected for current Playground JSPI output.'
135+
);
136+
} else {
137+
if (!existsSync(compiledPath) || statSync(compiledPath).size === 0) {
138+
fail(`wasmtime compile did not produce a non-empty ${compiledPath}`);
139+
}
140+
141+
const objdump = runWasmtime([
142+
'objdump',
143+
'--funcs',
144+
'wasm',
145+
'--filter',
146+
'get_module',
147+
compiledPath,
148+
]);
149+
requireSuccess('wasmtime objdump', objdump);
150+
151+
if (run.status === 0) {
152+
fail(
153+
'Expected wasmtime run to reject this Emscripten PHP side module as a standalone WASI command.'
154+
);
155+
}
156+
157+
const runOutput = commandOutput(run);
158+
if (
159+
!/import|instantiate|link|unknown|wasi|start|command/i.test(runOutput)
160+
) {
161+
fail(
162+
`wasmtime run failed for an unexpected reason.\n` +
163+
`stdout:\n${run.stdout || '<empty>'}\n` +
164+
`stderr:\n${run.stderr || '<empty>'}`
165+
);
166+
}
167+
168+
console.log(`[wasmtime-cli] side module: ${SIDE_MODULE}`);
169+
console.log(`[wasmtime-cli] imports: ${imports.length} (${envImports.length} from env)`);
170+
console.log(`[wasmtime-cli] exports: ${exports.map((entry) => entry.name).join(', ')}`);
171+
console.log('[wasmtime-cli] compile, objdump, and expected non-standalone run rejection passed.');
172+
}
173+
} finally {
174+
rmSync(tempDir, { recursive: true, force: true });
175+
}

0 commit comments

Comments
 (0)