Socket
Socket
Sign inDemoInstall

@webassemblyjs/wasm-edit

Package Overview
Dependencies
16
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

@webassemblyjs/wasm-edit

> Rewrite a WASM binary

    1.11.6latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
23,280,089
increased by5.64%

Weekly downloads

Package description

What is @webassemblyjs/wasm-edit?

The @webassemblyjs/wasm-edit package is a toolchain for WebAssembly that allows you to manipulate WASM binaries. It provides functionalities to parse, edit, and generate WASM binaries, making it useful for tasks such as optimizing, instrumenting, or simply understanding the structure of a WebAssembly module.

What are @webassemblyjs/wasm-edit's main functionalities?

Parsing WebAssembly binaries

This feature allows you to parse a WebAssembly binary and turn it into an Abstract Syntax Tree (AST) that can be manipulated or analyzed.

const { decode } = require('@webassemblyjs/wasm-parser');
const { readFileSync } = require('fs');

const wasmBuffer = readFileSync('module.wasm');
const ast = decode(wasmBuffer);

console.log(ast);

Modifying the AST

After parsing a WASM binary into an AST, you can traverse and modify the AST. This example shows how to change the module imports.

const { traverse } = require('@webassemblyjs/ast');

traverse(ast, {
  ModuleImport(path) {
    // Modify the AST by changing module imports
    path.node.module = 'new_module';
  }
});

Generating WebAssembly binaries

Once you have an AST—either from parsing a binary or constructing it manually—you can generate a new WebAssembly binary.

const { encode } = require('@webassemblyjs/wasm-gen');

const newWasmBuffer = encode(ast);

writeFileSync('modified.wasm', newWasmBuffer);

Other packages similar to @webassemblyjs/wasm-edit

Readme

Source

@webassemblyjs/wasm-edit

Rewrite a WASM binary

Replace in-place an AST node in the binary.

Installation

yarn add @webassemblyjs/wasm-edit

Usage

Update:

import { edit } from "@webassemblyjs/wasm-edit";

const binary = [/*...*/];

const visitors = {
  ModuleImport({ node }) {
    node.module = "foo";
    node.name = "bar";
  }
};

const newBinary = edit(binary, visitors);

Replace:

import { edit } from "@webassemblyjs/wasm-edit";

const binary = [/*...*/];

const visitors = {
  Instr(path) {
    const newNode = t.callInstruction(t.indexLiteral(0));
    path.replaceWith(newNode);
  }
};

const newBinary = edit(binary, visitors);

Remove:

import { edit } from "@webassemblyjs/wasm-edit";

const binary = [/*...*/];

const visitors = {
  ModuleExport({ node }) {
    path.remove()
  }
};

const newBinary = edit(binary, visitors);

Insert:

import { add } from "@webassemblyjs/wasm-edit";

const binary = [/*...*/];

const newBinary = add(actualBinary, [
  t.moduleImport("env", "mem", t.memory(t.limit(1)))
]);

Providing the AST

Providing an AST allows you to handle the decoding yourself, here is the API:

addWithAST(Program, ArrayBuffer, Array<Node>): ArrayBuffer;
editWithAST(Program, ArrayBuffer, visitors): ArrayBuffer;

Note that the AST will be updated in-place.

FAQs

Last updated on 09 May 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc