Skip to content

WASM memory view invalidation (not a problem) #55

@noname0310

Description

@noname0310

Context: https://x.com/noname20310/status/1862416380278317407

I've check the potential WASM memory view invalidation bug.
The conclusion is that there are no bugs in the code.

https://github.com/drumath2237/spz-loader/blob/main/packages/core/lib/spz-wasm/cppBufferUtil.ts#L18

The reason is that as soon as the ArrayBufferView is created, the contents are copied to the ArrayBuffer on the js side and the View is no longer used.

export const floatVectorToFloatArray = (
  wasmModule: MainModule,
  vec: VectorFloat32,
  enhancementFunc: (n: number) => number = (n) => n,
): Float32Array => {
  const pointer = wasmModule.vf32_ptr(vec);
  const size = vec.size();

  // buffer is ArrayBufferView of WASM Memory
  const buffer = new Float32Array(wasmModule.HEAPF32.buffer, pointer, size);
  // copiedBuffer is referencing js side new ArrayBuffer instance
  const copiedBuffer = buffer.map(enhancementFunc);

  return copiedBuffer;
};

The problem starts when you save the ArrayBufferView as a member of the class as is or as a member of an object, and you need to check for invalidation, but I don't see any such cases in the code.

I apologize for the confusion. You can close this issue once you've read it.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    👀 In review

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions