Skip to content

exchange TypedArray or Buffer between host and vm #68

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

Closed
Lyoko-Jeremie opened this issue Jun 23, 2022 · 2 comments
Closed

exchange TypedArray or Buffer between host and vm #68

Lyoko-Jeremie opened this issue Jun 23, 2022 · 2 comments

Comments

@Lyoko-Jeremie
Copy link

can i exchange TypedArray or Buffer between host and vm ?


i want to use libsodium-wrapper in vm, but it need crypto.getRandomValues function,

so when i try to impl the crypto.getRandomValues in vm, i notice that this function is a node impl and the web polyfill use randombytes to wrap browser crypto.randomBytes function to impl crypto.getRandomValues function.

but the randomBytes and getRandomValues all process the Buffer or TypedArray .

    // crypto.randomBytes  define in nodejs crypto
    function randomBytes(size: number): Buffer;
    function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
    function pseudoRandomBytes(size: number): Buffer;
    function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
    // crypto.getRandomValues  define in `lib.dom.d.ts`
    getRandomValues<T extends ArrayBufferView | null>(array: T): T;

so , i think it can impl like :

vm request some random -> tell the host -> host generate crypto random Buffer -> pass random Buffer to vm -> get the crypto random Buffer 

vm.randomBytes() call -> host.randomBytes() call -> host return random buffer -> vm.randomBytes() return

can i pass TypedArray or Buffer from host to vm ?

@justjake
Copy link
Owner

Currently you cannot directly pass a ArrayBuffer or similar to the VM, although it’s an interesting use-case, because we might be able to expose raw data like this directly to the VM without copying in the future.

For now, I suggest converting your random bytes to an Array of number on the host, sending to the vm, and converting from array of number to TypedArray inside the guest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants