Skip to content
Open
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
76 changes: 0 additions & 76 deletions scripts/clusterfuzz/embed_wasms.py

This file was deleted.

94 changes: 0 additions & 94 deletions scripts/clusterfuzz/extract_wasms.py

This file was deleted.

5 changes: 1 addition & 4 deletions scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,10 +1863,7 @@ def handle_pair(self, input, before_wasm, after_wasm, opts):
if output != IGNORE and INSTANTIATE_ERROR not in output:
# Do the work to find if there were function exports: extract the
# wasm from the JS, and process it.
run([sys.executable,
in_binaryen('scripts', 'clusterfuzz', 'extract_wasms.py'),
fuzz_file,
'extracted'])
run([in_bin('wasm-embed'), '--extract', fuzz_file, 'extracted'])
if get_exports('extracted.0.wasm', ['func']):
assert FUZZ_EXEC_EXPORT_PREFIX in output

Expand Down
1 change: 1 addition & 0 deletions scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def is_exe(fpath):
WASM_CTOR_EVAL = [os.path.join(options.binaryen_bin, 'wasm-ctor-eval')]
WASM_SHELL = [os.path.join(options.binaryen_bin, 'wasm-shell')]
WASM_REDUCE = [os.path.join(options.binaryen_bin, 'wasm-reduce')]
WASM_EMBED = [os.path.join(options.binaryen_bin, 'wasm-embed')]
WASM_METADCE = [os.path.join(options.binaryen_bin, 'wasm-metadce')]
WASM_EMSCRIPTEN_FINALIZE = [os.path.join(options.binaryen_bin,
'wasm-emscripten-finalize')]
Expand Down
3 changes: 2 additions & 1 deletion scripts/update_help_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

TOOLS = ['wasm-opt', 'wasm-as', 'wasm-dis', 'wasm2js', 'wasm-ctor-eval',
'wasm-shell', 'wasm-reduce', 'wasm-metadce', 'wasm-split',
'wasm-fuzz-types', 'wasm-emscripten-finalize', 'wasm-merge']
'wasm-fuzz-types', 'wasm-emscripten-finalize', 'wasm-merge',
'wasm-embed']


def main():
Expand Down
1 change: 1 addition & 0 deletions src/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ if(NOT BUILD_EMSCRIPTEN_TOOLS_ONLY)
binaryen_add_executable(wasm-merge wasm-merge.cpp)
binaryen_add_executable(wasm-fuzz-types "${fuzzing_SOURCES};wasm-fuzz-types.cpp")
binaryen_add_executable(wasm-fuzz-lattices "${fuzzing_SOURCES};wasm-fuzz-lattices.cpp")
binaryen_add_executable(wasm-embed wasm-embed.cpp)
add_subdirectory(wasm-reduce)
endif()

Expand Down
151 changes: 151 additions & 0 deletions src/tools/wasm-embed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2026 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//
// Embed Wasm binaries into a JS file, or extract embedded Wasm binaries from a
// JS file.
//

#include <iostream>
#include <string>
#include <vector>

#include "parsing.h"
#include "pass.h"
#include "support/command-line.h"
#include "support/file.h"
#include "support/js-embedded-module.h"
#include "support/utilities.h"
#include "wasm-io.h"
#include "wasm-validator.h"

using namespace wasm;

int main(int argc, const char* argv[]) {
const std::string WasmEmbedOption = "wasm-embed options";

bool extract = false;
std::string output;
std::vector<std::string> positionals;

Options options(
"wasm-embed",
"Embed Wasm binaries into a JS file, or extract embedded Wasm binaries "
"from a JS file");
options
.add("--extract",
"-e",
"Extract embedded Wasm modules from the input JS file into "
"<output>.0.wasm, <output>.1.wasm, ...",
WasmEmbedOption,
Options::Arguments::Zero,
[&](Options* o, const std::string& argument) { extract = true; })
.add("--output",
"-o",
"Output JS file (or output prefix when --extract is used)",
WasmEmbedOption,
Options::Arguments::One,
[&](Options* o, const std::string& argument) { output = argument; })
.add_positional("INFILE.js [WASM_FILES...] [OUTFILE]",
Options::Arguments::N,
[&](Options* o, const std::string& argument) {
positionals.push_back(argument);
});
options.parse(argc, argv);

if (extract) {
std::string inJsFile;
if (output.empty()) {
if (positionals.size() != 2) {
Fatal() << "Expected input JS file and output prefix";
}
inJsFile = positionals[0];
output = positionals[1];
} else {
if (positionals.size() != 1) {
Fatal() << "Expected a single input JS file when --output is specified";
}
inJsFile = positionals[0];
}

auto js = read_file<std::string>(inJsFile, Flags::Text);
auto modules = findEmbeddedModules(js);
for (size_t i = 0; i < modules.size(); ++i) {
write_file(output + "." + std::to_string(i) + ".wasm", modules[i].bytes);
}
flush_and_quick_exit(0);
}

std::string inJsFile;
std::vector<std::string> wasmFiles;
if (output.empty()) {
if (positionals.size() < 2) {
Fatal() << "Expected input JS file, wasm files, and output JS file";
}
inJsFile = positionals.front();
output = positionals.back();
wasmFiles.assign(positionals.begin() + 1, positionals.end() - 1);
} else {
if (positionals.empty()) {
Fatal() << "Expected input JS file";
}
inJsFile = positionals.front();
wasmFiles.assign(positionals.begin() + 1, positionals.end());
}

auto js = read_file<std::string>(inJsFile, Flags::Text);
auto existing = findEmbeddedModules(js);
if (existing.size() != wasmFiles.size()) {
Fatal() << "Number of embedded wasm modules in " << inJsFile << " ("
<< existing.size()
<< ") does not match number of provided wasm files ("
<< wasmFiles.size() << ")";
}

for (size_t i = existing.size(); i > 0; --i) {
const auto& mod = existing[i - 1];
const auto& wasmFile = wasmFiles[i - 1];
auto bytes = read_file<std::vector<char>>(wasmFile, Flags::Binary);
bool isBinary = bytes.size() >= 4 && bytes[0] == '\0' && bytes[1] == 'a' &&
bytes[2] == 's' && bytes[3] == 'm';

Module wasm;
wasm.features = FeatureSet::All;
try {
ModuleReader().readData(bytes, wasm);
} catch (ParseException& p) {
p.dump(std::cerr);
std::cerr << '\n';
Fatal() << "error parsing wasm (" << wasmFile << ")";
}

if (!WasmValidator().validate(wasm)) {
Fatal() << "error validating module (" << wasmFile << ")";
}

if (!isBinary) {
PassOptions passOptions;
ModuleWriter writer(passOptions);
writer.setBinary(true);
writer.write(wasm, bytes);
}

js.replace(mod.start, mod.end - mod.start, formatByteArray(bytes));
}

write_file(output, js);
flush_and_quick_exit(0);
}
27 changes: 27 additions & 0 deletions test/lit/help/wasm-embed.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
;; RUN: wasm-embed --help | filecheck %s
;; CHECK: ================================================================================
;; CHECK-NEXT: wasm-embed INFILE.js [WASM_FILES...] [OUTFILE]
;; CHECK-EMPTY:
;; CHECK-NEXT: Embed Wasm binaries into a JS file, or extract embedded Wasm binaries from a JS
;; CHECK-NEXT: file
;; CHECK-NEXT: ================================================================================
;; CHECK-EMPTY:
;; CHECK-EMPTY:
;; CHECK-NEXT: wasm-embed options:
;; CHECK-NEXT: -------------------
;; CHECK-EMPTY:
;; CHECK-NEXT: --extract,-e Extract embedded Wasm modules from the input JS file into
;; CHECK-NEXT: <output>.0.wasm, <output>.1.wasm, ...
;; CHECK-EMPTY:
;; CHECK-NEXT: --output,-o Output JS file (or output prefix when --extract is used)
;; CHECK-EMPTY:
;; CHECK-EMPTY:
;; CHECK-NEXT: General options:
;; CHECK-NEXT: ----------------
;; CHECK-EMPTY:
;; CHECK-NEXT: --version Output version information and exit
;; CHECK-EMPTY:
;; CHECK-NEXT: --help,-h Show this help message and exit
;; CHECK-EMPTY:
;; CHECK-NEXT: --debug,-d Print debug information to stderr
;; CHECK-EMPTY:
Loading
Loading