From f0055644dc2e4ff99243dd2e765a265b76826c32 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 01:04:41 +0000 Subject: [PATCH 1/2] fix(codegen): reject the #746 Crypto externs on core WASM by name (#822) The 6 Crypto externs from #746 compiled to host imports that no core-WASM host supplies, so the module failed at load with a LinkError. A use is now a named compile error that points to --bun-esm. An unused import of one is dropped. Closes #822 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZjyGGiPhiEUxNGPwsr --- lib/codegen.ml | 14 ++++++++++++++ test/test_e2e.ml | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/codegen.ml b/lib/codegen.ml index b21d7b5c..e0545d0a 100644 --- a/lib/codegen.ml +++ b/lib/codegen.ml @@ -4053,6 +4053,12 @@ let program_free_names (prog : program) : (string, unit) Hashtbl.t = ) prog.prog_decls; h +(* #822: Crypto externs from #746 with a JS lowering only. No core-WASM + host supplies them, so an import would fail at load with a LinkError. *) +let wasm_unsupported_crypto = + [ "sha256"; "hmac_sha256"; "base64url_encode"; "base64url_decode"; + "rsa_sign_pkcs1_sha256"; "rsa_verify_pkcs1_sha256" ] + let gen_imports ?(used : (string, unit) Hashtbl.t option) ?(local_names : string list = []) (loader : Module_loader.t) (imports : import_decl list) (ctx : context) @@ -4075,6 +4081,14 @@ let gen_imports ?(used : (string, unit) Hashtbl.t option) ) loaded.mod_program.prog_decls in match item with | None -> Ok ctx + | Some (`Fn _) when mod_path = ["Crypto"] + && List.mem orig_name wasm_unsupported_crypto -> + (match used with + | Some u when not (Hashtbl.mem u local_name) -> Ok ctx + | _ -> + Error (UnsupportedFeature (Printf.sprintf + "Crypto.%s is not supported on the core-WASM backend; compile \ + with --bun-esm (#822)" orig_name))) | Some (`Fn fd) -> let ft = func_type_of_fn_decl fd in let (type_idx, types_after) = intern_func_type ctx.types ft in diff --git a/test/test_e2e.ml b/test/test_e2e.ml index 9a7d8654..a2737fad 100644 --- a/test/test_e2e.ml +++ b/test/test_e2e.ml @@ -4801,7 +4801,32 @@ let test_modmut561_imported_float_const () = (List.exists (fun f -> List.exists (fun l -> l.l_type = F64) f.f_locals) m.funcs) +(* #822: the #746 Crypto externs have no core-WASM host; a use is a + named compile error, not a LinkError at load. An unused import is dropped. *) +let wasm822 (src : string) : (unit, string) result = + let prog = Parse_driver.parse_string ~file:"" src in + let loader = Module_loader.create { + Module_loader.stdlib_path = "stdlib"; search_paths = []; current_dir = "." } in + match Codegen.generate_module ~loader prog with + | Ok _ -> Ok () + | Error e -> Error (Codegen.show_codegen_error e) + +let test_822_wasm_crypto_use_rejected () = + match wasm822 "use Crypto::{ sha256 };\n\ + pub fn main() -> Int { let d = sha256([1, 2]); len(d) }" with + | Ok () -> Alcotest.fail "Crypto.sha256 must not compile on core WASM" + | Error msg -> + Alcotest.(check bool) "names the function" true + (string_contains msg "Crypto.sha256") + +let test_822_wasm_crypto_unused_import_ok () = + match wasm822 "use Crypto::{ sha256 };\npub fn main() -> Int { 1 }" with + | Error msg -> Alcotest.failf "an unused Crypto import should compile: %s" msg + | Ok () -> () + let modmut561_tests = [ + Alcotest.test_case "#822: a Crypto extern use on core WASM is rejected by name" `Quick test_822_wasm_crypto_use_rejected; + Alcotest.test_case "#822: an unused Crypto import still compiles" `Quick test_822_wasm_crypto_unused_import_ok; Alcotest.test_case "wasm: same-name let of a Float const is f64" `Quick test_modmut561_float_global_same_name_let; Alcotest.test_case "wasm: imported Float const is an f64 global" `Quick test_modmut561_imported_float_const; Alcotest.test_case "wasm: record const field loads at its offset" `Quick test_modmut561_record_const_field_offset; From 00f9ae49b6984a8ee258659a43327bc82014daa8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 01:17:57 +0000 Subject: [PATCH 2/2] test(#822): find stdlib from the dune runtest directory Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZjyGGiPhiEUxNGPwsr --- test/test_e2e.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_e2e.ml b/test/test_e2e.ml index a2737fad..cf3741b3 100644 --- a/test/test_e2e.ml +++ b/test/test_e2e.ml @@ -4805,8 +4805,9 @@ let test_modmut561_imported_float_const () = named compile error, not a LinkError at load. An unused import is dropped. *) let wasm822 (src : string) : (unit, string) result = let prog = Parse_driver.parse_string ~file:"" src in + let sd = List.find Sys.file_exists [ "stdlib"; "../../../stdlib"; "../../../../stdlib" ] in let loader = Module_loader.create { - Module_loader.stdlib_path = "stdlib"; search_paths = []; current_dir = "." } in + Module_loader.stdlib_path = sd; search_paths = []; current_dir = sd } in match Codegen.generate_module ~loader prog with | Ok _ -> Ok () | Error e -> Error (Codegen.show_codegen_error e)