0

When I tried to compile the following snippet into WebAssembly binary, I kept hitting the unresolved symbol: llvm_trap warning, which makes the wasm code not consumable from JS.

emcc test.c -s WASM=1 -s ONLY_MY_CODE=1 -s "EXPORTED_FUNCTIONS=['_test']" -O2 -g -o test.js

test.c (This is a test code to reproduce the issue without doing meaningful jobs.)

int test(int *buf) {
  int C = 1;
  // Assuming WebAssembly.Memory buffer has been preloaed with data. 
  // *T represents the preloaded data here. And We know *T and *buf 
  // won't overlap in memory.
  int *T = 0; 

  int index = C ^ buf[5];
  int right = T[index];
  int left = (unsigned)C >> 8;

  // warning disappears if this is commented out. But why?
  C = left ^ right; 

  return C;
}

I didn't write any llvm_trap related code. Does someone have ideas what does it mean?

hackjutsu
  • 8,336
  • 13
  • 47
  • 87

1 Answers1

0

The variable T must be initialised. If it represents an array that 'maps' to the WebAssembly linear memory, you can define it as a global as follows:

int T[1000];

int test(int *buf) {
  int C = 1;

  int index = C ^ buf[5];
  int right = T[index];
  int left = (unsigned)C >> 8;

  // warning disappears if this is commented out. But why?
  C = left ^ right;

  return C;
}

This compiles without the llvm_trap warnings.

For more detail on how to pass data to a WASM function using linear memory, see the following question:

How to access WebAssembly linear memory from C/C++

ColinE
  • 68,894
  • 15
  • 164
  • 232