0

I am building a WebAssembly module in Rust to be used in Cloudflare Workers, via wasm-bindgen. The module is overall very basic; it has a single function named process, which takes as inputs two binaries (represented by two Uint8BitArrays), and a json_value (to be interpreted by serde), and yields either None or a string, typically something like this.

#[wasm_bindgen]
pub fn process(
    binary_a: &[u8],
    binary_b: &[u8],
    json_value: &JsValue,
) -> Option<String> {
   Some(String::new())
}

The glue code to instantiate the WebAssembly module is nearly identical from the wasm-bindgen --no-modules command, I've only changed the init condition at line 93 to true:

self = {};

(function() {
    var wasm;
    const __exports = {};


    let cachegetUint8Memory = null;
    function getUint8Memory() {
        if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
            cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
        }
        return cachegetUint8Memory;
    }

    let WASM_VECTOR_LEN = 0;

    function passArray8ToWasm(arg) {
        const ptr = wasm.__wbindgen_malloc(arg.length * 1);
        getUint8Memory().set(arg, ptr / 1);
        WASM_VECTOR_LEN = arg.length;
        return ptr;
    }

    const heap = new Array(32);

    heap.fill(undefined);

    heap.push(undefined, null, true, false);

    let stack_pointer = 32;

    function addBorrowedObject(obj) {
        if (stack_pointer == 1) throw new Error('out of js stack');
        heap[--stack_pointer] = obj;
        return stack_pointer;
    }

    let cachedTextDecoder = new TextDecoder('utf-8');

    function getStringFromWasm(ptr, len) {
        return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
    }

    let cachedGlobalArgumentPtr = null;
    function globalArgumentPtr() {
        if (cachedGlobalArgumentPtr === null) {
            cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
        }
        return cachedGlobalArgumentPtr;
    }

    let cachegetUint32Memory = null;
    function getUint32Memory() {
        if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
            cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
        }
        return cachegetUint32Memory;
    }
    /**
    * @param {Uint8Array} arg0
    * @param {Uint8Array} arg1
    * @param {any} arg2
    * @returns {string}
    */
    __exports.process = function(arg0, arg1, arg2) {
        const ptr0 = passArray8ToWasm(arg0);
        const len0 = WASM_VECTOR_LEN;
        const ptr1 = passArray8ToWasm(arg1);
        const len1 = WASM_VECTOR_LEN;
        const retptr = globalArgumentPtr();
        try {
            wasm.process(retptr, ptr0, len0, ptr1, len1, addBorrowedObject(arg2));
            const mem = getUint32Memory();
            const rustptr = mem[retptr / 4];
            const rustlen = mem[retptr / 4 + 1];
            if (rustptr === 0) return;
            const realRet = getStringFromWasm(rustptr, rustlen).slice();
            wasm.__wbindgen_free(rustptr, rustlen * 1);
            return realRet;


        } finally {
            wasm.__wbindgen_free(ptr0, len0 * 1);
            wasm.__wbindgen_free(ptr1, len1 * 1);
            heap[stack_pointer++] = undefined;

        }

    };

    function init(path_or_module) {
        let instantiation;
        const imports = { './phototaxon_worker_utils': __exports };
        if (true) {
            instantiation = WebAssembly.instantiate(path_or_module, imports)
            .then(instance => {
            return { instance, module: path_or_module }
        });
    } else {
        const data = fetch(path_or_module);
        if (typeof WebAssembly.instantiateStreaming === 'function') {
            instantiation = WebAssembly.instantiateStreaming(data, imports);
        } else {
            instantiation = data
            .then(response => response.arrayBuffer())
            .then(buffer => WebAssembly.instantiate(buffer, imports));
        }
    }
    return instantiation.then(({instance}) => {
        wasm = init.wasm = instance.exports;

    });
};
self.wasm_bindgen = Object.assign(init, __exports);
})();

self.wasm_bindgen(MODULE).then(() => { XXX }).catch(error => console.log(error));

I've used cloudworker to try out the overall script and it operated without issues. I've then tried the same script with the Preview Service API and it did work fine for a few attempts, until it started to throw errors:

RangeError: WebAssembly Instantiation: Out of memory: wasm memory
    at init (worker.js:1200:35)
    at Module.<anonymous> (worker.js:1878:15)
    at __webpack_require__ (worker.js:20:30)
    at worker.js:84:18
    at worker.js:87:10

This happens at the instantiation, regardless of the request sent (what happens after the initialisation is not .

I've been trying to slim down my Webassembly script, but even a hello-world-type function was rejected. I don't know how to debug, is this related to the glue-code, the rust code, or the Cloudflare's Preview Service?

Edouard
  • 149
  • 1
  • 11
  • Are you using the default allocator? IIRC, you can use `wee_alloc` to cut the binary size down significantly. – w.brian Feb 01 '19 at 20:26

1 Answers1

0

The problem was the absence of the String capacity (in the process function), so that the WebAssembly module could not provision the required memory to run (likely over-provisions). By setting a limit with String::with_capacity, the module runs properly without any memory issue.

Edouard
  • 149
  • 1
  • 11
  • 1
    Hey @Edouard, actually, after seeing your question, we noticed that the Workers preview service was throwing the error for all `new WebAssembly.Memory(...)` calls. I tried restarting the service and... that fixed it. Clearly there's a deeper bug which we'll be investigating. Just wanted to let you know that when you saw it start working, it may not have been anything you changed, but because I restarted the preview service. (As far as we can tell, this issue only ever affected preview, not workers deployed in production.) – Kenton Varda Feb 01 '19 at 22:21
  • This makes sense. I reverted the changes, and it was working again... until I tried making a few more requests, and it no longer works. I replaced the code again with the capacity option, and it does not work either. I guess my module somehow crashes the whole WebAssembly.Memory thing? As a side question, what are the CPU resource limits of the preview service? (equivalent to the 5ms free tier?) – Edouard Feb 02 '19 at 10:41
  • I haven't been able to reproduce the problem myself. Would you be up for e-mailing me your zone name and details so I can track this down? kenton at cloudflare. To answer your second question, the CPU limit at which we terminate a request varies but is always at least 50ms, regardless of plan level, in both production and preview. – Kenton Varda Feb 04 '19 at 16:41