Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions tests/programs/test_js_asmjs_plain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <string.h>

#include "napi_test_helpers.h"

// V8 translates validated asm.js modules to WebAssembly and compiles them
// with its wasm pipeline, without consulting the embedder's wasm
// code-generation policy. The provider disables that translation, so an asm.js
// module must run as ordinary JavaScript.
//
// The probe distinguishes the two by inspecting the asm.js function's stack
// frame from a foreign function it calls: in sloppy-mode JavaScript,
// CallSite.getFunction() returns the very function object that is running; a
// frame of translated asm.js code is a wasm frame and never yields that
// function.
static const char kScript[] =
"(function () {\n"
" var observed = 'no frame';\n"
" function probe() {\n"
" var saved = Error.prepareStackTrace;\n"
" Error.prepareStackTrace = function (_error, frames) { return frames; };\n"
" var frames = new Error().stack;\n"
" Error.prepareStackTrace = saved;\n"
" for (var i = 0; i < frames.length; i++) {\n"
" if (frames[i].getFunctionName() === 'asmAdd') {\n"
" observed = frames[i].getFunction();\n"
" break;\n"
" }\n"
" }\n"
" }\n"
" function AsmModule(stdlib, foreign) {\n"
" 'use asm';\n"
" var probe = foreign.probe;\n"
" function asmAdd(a, b) {\n"
" a = a | 0;\n"
" b = b | 0;\n"
" probe();\n"
" return (a + b) | 0;\n"
" }\n"
" return { asmAdd: asmAdd };\n"
" }\n"
" var exports = AsmModule(globalThis, { probe: probe });\n"
" var sum = exports.asmAdd(40, 2);\n"
" if (sum !== 42) return 'wrong result ' + sum;\n"
" if (observed === exports.asmAdd) return 'plain';\n"
" return 'translated (frame function: ' + typeof observed + ')';\n"
"})()";

int main(void) {
napi_env env = napi_wasm_init_env();
CHECK_OR_FAIL(env != NULL, "napi_wasm_init_env returned NULL");

napi_value source;
NAPI_CALL(env, napi_create_string_utf8(env, kScript, NAPI_AUTO_LENGTH,
&source));
napi_value result;
NAPI_CALL(env, napi_run_script(env, source, &result));

char text[128] = {0};
size_t length = 0;
NAPI_CALL(env, napi_get_value_string_utf8(env, result, text, sizeof(text),
&length));
if (strcmp(text, "plain") != 0) {
printf("FAIL: asm.js module did not run as plain JavaScript: %s\n", text);
return 1;
}
puts("ASMJS_RUNS_AS_PLAIN_JS_OK");
return 0;
}
14 changes: 14 additions & 0 deletions tests/v8_host_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ fn guest_cannot_allocate_unmetered_v8_webassembly_memory() {
assert!(stdout.contains("JS_WASM_DISABLED_OK"), "{stdout}\n{stderr}");
}

/// asm.js would otherwise be translated to WebAssembly and compiled into
/// V8's wasm code space, bypassing the WebAssembly restriction above.
#[test]
fn asm_js_runs_as_plain_javascript() {
let wasm = build_wasix_test("test_js_asmjs_plain");
let (exit_code, stdout, stderr) =
run_guest(&NapiCtx::default(), &wasm).expect("guest run failed");
assert_eq!(exit_code, 0, "{stdout}\n{stderr}");
assert!(
stdout.contains("ASMJS_RUNS_AS_PLAIN_JS_OK"),
"{stdout}\n{stderr}"
);
}

#[test]
fn guest_cannot_start_unmanaged_v8_workers_or_profiler() {
let wasm = build_wasix_test("test_guest_unmanaged_controls");
Expand Down
8 changes: 7 additions & 1 deletion v8/src/internal/node_v8_default_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
inline constexpr char kNodeJsExplicitResourceManagementFlag[] =
"--js-explicit-resource-management";
inline constexpr char kNodeJsFloat16ArrayFlag[] = "--js-float16array";
// `--no-validate-asm` is not a shipping feature flag. V8 translates validated
// asm.js modules to WebAssembly and compiles them into wasm code space without
// consulting the embedder's wasm code-generation callback, which would bypass
// the provider's WebAssembly policy. Without validation asm.js runs as
// ordinary JavaScript, with the same results for correct programs.
inline constexpr char kNodeDefaultShippingV8Flags[] =
"--js-explicit-resource-management "
"--js-float16array";
"--js-float16array "
"--no-validate-asm";

@syrusakbary syrusakbary Oct 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would prevent to use optimized Wasm when needed for asm.js.

What we need to --no-validate-asm if and only if wasm is not supported. If Wasm is supported, then we shouldn't add this flag


#endif // NAPI_V8_NODE_V8_DEFAULT_FLAGS_H_
Loading