Skip to content

setMemory fails for large data after update to v112 #5595

@mnater

Description

@mnater

Hi

I'm using binaryen.js to prepopulate memory with a larger amount of data. Since the update to v112 this doesn't work anymore.
Reduced testcase:

import binaryen from "binaryen";

const dataLength = 100_000;
const mockData = new Uint8Array(dataLength);
for (let i = mockData.length - 1; i >= 0; i--) {
  mockData[i] = i & 255;
}

// Create a module and prepopulate memory
const myModule = new binaryen.Module();

myModule.setMemory(2, -1, "mem", [{
  "data": mockData,
  "offset": myModule.i32.const(0),
  "passive": false
}]);


const binary = myModule.emitBinary();
console.log(binary.length); // emits 100034 on v111

In v112 I get Bus error: 10
(Node v19.6.0 on macOS 12.6.3 )

The maximum dataLength where the code above still works in v112 is 67_312.

I also tried to split data in several chunks but I hit the same issues:

import binaryen from "binaryen";

const dataLength = 100_000;
const mockData = new Uint8Array(dataLength);
for (let i = mockData.length - 1; i >= 0; i--) {
  mockData[i] = i & 255;
}

// Create a module and prepopulate memory
const myModule = new binaryen.Module();

const memSegments = [];
const segmentSize = 50_000
const segmentCount = Math.ceil(dataLength / segmentSize);
for (let i = 0; i < segmentCount; i += 1) {
    memSegments.push({
        "data": mockData.subarray(i * segmentSize, ((i + 1) * segmentSize)),
        "offset": myModule.i32.const(i * segmentSize),
        "passive": false
    });
}

console.log(memSegments);

myModule.setMemory(
    2,
    -1,
    "mem",
    memSegments
);

const binary = myModule.emitBinary();
console.log(binary.length);

I think this is due the change from allocate() to stackAlloc() in https://github.com/WebAssembly/binaryen/blame/main/src/js/binaryen.js-post.js#L2572

How can I use setMemory with a larger amount of data?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions