Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM memory view invalidation (not a problem) #55

Open
noname0310 opened this issue Nov 29, 2024 · 2 comments
Open

WASM memory view invalidation (not a problem) #55

noname0310 opened this issue Nov 29, 2024 · 2 comments

Comments

@noname0310
Copy link

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.

@drumath2237
Copy link
Owner

@noname0310

Thank you so much for your investigation!

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 understand.

I'm planning to provide lower level API from core package, because there are some overhead of buffer copies in babylon.js integration (and will other package too).
At that time, it is important to check wheather the ArrayBuffer's pointer is correct or not, I think.

@noname0310
Copy link
Author

I was going to suggest copy optimization like you said, but it would be quite complicated to do, so I only described the problem.

For copy optimization, it is necessary to care about ArrayBufferView invalidation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 👀 In review
Development

No branches or pull requests

2 participants