|
| 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