Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAssembly Link Error: import object field 'DYNAMICTOP_PTR' is not a Number

The following C file is compiled to wasm with emscripten:

int counter = 100;

int count() {
    counter += 1;
    return counter;
}
$ emcc counter.c -o counter.wasm -s WASM=1 -s SIDE_MODULE=1

With no issues. I then have webpack loading the wasm file (with wasm-loader) as an UInt8Array:

var buffer = new ArrayBuffer(648);
var uint8 = new Uint8Array(buffer);
uint8.set([0,97,115,109,1,0,0,0,0,12,6,100,121,108,105,110,107,144,128,192,2,0,1,150,128,128,128,0,5,96,1,127,0,96,1,127,1,127,96,0,1,127,96,2,127,127,0,96,0,0,2,179,129,128,128,0,10,3,101,110,118,14,68,89,78,65,77,73,67,84,79,80,95,80,84,82,3,127,0,3,101,110,118,13,116,101,109,112,68,111,117,98,108,101,80,116,114,3,127,0,3,101,110,118,5,65,66,79,82,84,3,127,0,3,101,110,118,10,109,101,109,111,114,121,66,97,115,101,3,127,0,3,101,110,118,9,116,97,98,108,101,66,97,115,101,3,127,0,6,103,108,111,98,97,108,3,78,97,78,3,124,0,6,103,108,111,98,97,108,8,73,110,102,105,110,105,116,121,3,124,0,3,101,110,118,18,97,98,111,114,116,83,116,97,99,107,79,118,101,114,102,108,111,119,0,0,3,101,110,118,6,109,101,109,111,114,121,2,0,128,2,3,101,110,118,5,116,97,98,108,101,1,112,0,0,3,137,128,128,128,0,8,1,2,0,3,3,2,4,4,6,242,128,128,128,0,20,127,1,35,0,11,127,1,35,1,11,127,1,35,2,11,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,124,1,35,5,11,124,1,35,6,11,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,127,1,65,0,11,124,1,68,0,0,0,0,0,0,0,0,11,127,1,65,0,11,125,1,67,0,0,0,0,11,125,1,67,0,0,0,0,11,127,0,65,0,11,7,184,128,128,128,0,4,6,95,99,111,117,110,116,0,6,18,95,95,112,111,115,116,95,105,110,115,116,97,110,116,105,97,116,101,0,8,11,114,117,110,80,111,115,116,83,101,116,115,0,7,8,95,99,111,117,110,116,101,114,3,26,9,129,128,128,128,0,0,10,190,129,128,128,0,8,173,128,128,128,0,1,1,127,2,64,35,10,33,1,35,10,32,0,106,36,10,35,10,65,15,106,65,112,113,36,10,35,10,35,11,78,4,64,32,0,16,0,11,32,1,15,0,11,0,11,133,128,128,128,0,0,35,10,15,11,134,128,128,128,0,0,32,0,36,10,11,141,128,128,128,0,0,2,64,32,0,36,10,32,1,36,11,11,11,146,128,128,128,0,0,35,12,65,0,70,4,64,32,0,36,12,32,1,36,13,11,11,161,128,128,128,0,1,4,127,2,64,35,10,33,3,35,3,65,0,106,40,2,0,33,0,32,0,65,1,106,33,1,32,1,15,0,11,0,11,133,128,128,128,0,1,1,127,1,11,152,128,128,128,0,0,2,64,35,3,65,16,106,36,10,35,10,65,128,128,192,2,106,36,11,16,7,11,11,11,135,128,128,128,0,1,0,35,3,11,1,100,]);

My JS code then call the following:

import Counter from './wasm/counter'
const counter = new Counter();

And fails on the instantiation with the following Error:

LinkError: import object field 'DYNAMICTOP_PTR' is not a Number

wasm-loader uses the following default options (importObject) for WebAssembly.Instance(module, importObject):

{
  'global': {},
  'env': {
    'memory': new Memory({initial: 10, limit: 100}),
    'table': new Table({initial: 0, element: 'anyfunc'})
  }
}

Am I doing something wrong ? How can I successfully load the wasm code ?

Edit: Following @Ghillie advice I tried to compile the C code with different optimization flags:

-O1, -02, -O3, -Os and -Oz throw a LinkError: import object field 'memoryBase' is not a Number which is a different error but doesn't solve the issue.

Edit 2:

Solely adding the -O1 optimization flag didn't work but having a different error message allowed to debug the code so both @Ghillie and my answer offer the complete solution to my question.

like image 419
Maxime R. Avatar asked Jun 03 '17 17:06

Maxime R.


2 Answers

The solution appeared to be to tweak the importObject used in WebAssembly.Instance(module, importObject):

{
  'env': {
    'memoryBase': 0,
    'tableBase': 0,
    'memory': new WebAssembly.Memory({initial: 256}),
    'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
  }
}

memoryBase and tableBase were needed and the initial memory value was too low (throwed a LinkedError: Memory of incompatible size).

Also I had to call counter.exports._count() and not counter.exports.count() once the instantiation was done.

Also thanks to @Ghillie as the -O1 optimization flag was indeed needed.

Edit: if you are reading this and just getting started with WebAssembly, I made a summary of my first experience, in case it helps: WebAssembly 101: a developer's first steps.

like image 72
Maxime R. Avatar answered Oct 23 '22 02:10

Maxime R.


Compile with any optimization flag other than the default -O0. For example:

emcc counter.c -O1 -o counter.wasm -s WASM=1 -s SIDE_MODULE=1

The various optimization flags are listed here:

https://kripken.github.io/emscripten-site/docs/optimizing/Optimizing-Code.html

like image 25
Ghillie Avatar answered Oct 23 '22 01:10

Ghillie