Discussion:
Struggling with function pointers
Nolan Darilek
2018-10-02 14:58:57 UTC
Permalink
This is a continuation of my earlier thread, which kind of mutated when
I decided to leave Rust for Nim. So I thought I'd start a new one.


For brief context, I'm trying to use SpatiaLite, an SQLite geospatial
engine, in the browser via Emscripten. Yes I know about spatiasql.js,
but I need large databases that won't fit into memory. But Spatiasql at
least proves that the end goal is achievable.


I have a proc (roughly equivalent to a function in Nim) that initializes
the database and SpatiaLite. It works from the command line when built
natively, and looks like:


```

proc newMap*(path: cstring): ptr Map {.exportc.} =
  echo("Allocating map")
  result = create(Map) # malloc, essentially
  echo("Opening database")
  result.db = db_sqlite.open($path, "", "", "")
  echo("Allocating spatialite connection")
  let cache = spatialite_alloc_connection()
  if cache == nil:
    quit("Could not allocate SpatiaLite connection")
  echo("Initializing spatialite")
  spatialite_init_ex(result.db, cache, 1)
  echo("Done")
```


In Nim, `result` is a variable whose value is implicitly returned. As
such, this should print out a bunch of SpatiaLite initialization logs,
then "Done". And so it does via the command line. When I try loading the
wasm via node.js, I get:


```

Initializing spatialite
Invalid function pointer '0' called with signature 'X'. Perhaps this is
an invalid value (e.g. caused by calling a virtual method on a NULL
pointer)? Or calling a function with an incorrect type, which will fail
? (it is worth building your source files with -Werror (warnings are
errors), as warnings can indicate undefined behavior which can cause this)
This pointer might make sense in another type signature: i: undefined 
v: undefined  di: undefined  ii: undefined  vi: undefined  dii:
undefined  iid: undefined  iii: undefined  vid: undefined  vii: undefined  d
iii: undefined  iidi: undefined  iiid: undefined  iiii: undefined viii:
undefined  diiii: undefined  iidii: undefined  iiiii: undefined  viddi:
undefined  viiid: undefined  viiii: undefined iiiiid: undefined
iiiiii: undefined  viiiii: undefined  iiiiiid: undefined  iiiiiii:
undefined  viiiiii: undefined  iiiiiiii: undefined  iiiiiiiii:
undefined  iiiiiiiiii: undefined
0
0
Thrown: abort(0) at Error
    at jsStackTrace (/home/nolan/Projects/waynav/waynav.js:1181:13)
    at stackTrace (/home/nolan/Projects/waynav/waynav.js:1198:12)
    at abort (/home/nolan/Projects/waynav/waynav.js:11309:44)
    at nullFunc_X (/home/nolan/Projects/waynav/waynav.js:6807:1552)
    at b1 (wasm-function[10433]:3)
    at byn$fpcast-emu$b1 (wasm-function[10467]:1)
    at _mallocWithAlarm (wasm-function[2706]:88)
    at _sqlite3Malloc (wasm-function[3081]:348)
    at _sqlite3DbMallocRaw (wasm-function[4810]:203)
    at _sqlite3DbMallocZero (wasm-function[5014]:122)
```


This is an improvement over yesterday's error, which was the same
stacktrace in triplicate and a crash of node. :)


I tried building/linking with `-s EMULATE_FUNCTION_POINTER_CASTS=1 -g
-O0`, which I thought would eliminate that class of error. I'm aware of
the performance losses, but performance is irrelevant if I'll never get
this working anyway. :)


Are there any more tips on how to debug this in node? I'll eventually be
running it in the browser, but for now I'd rather work on it without
constructing a usable UI or fighting with IDBFS issues with large files.


Thanks.
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alon Zakai
2018-10-03 02:49:01 UTC
Permalink
"Invalid function pointer '0' called" indicates it's calling a null pointer
there, so it doesn't look like a function pointer type issue (the error
message could be improved). To debug it, if you can add debug printouts in
the original source code that's usually easier in my experience.
Specifically here, I'd look into what mallocWithAlarm is trying to do -
perhaps it has a list of function pointers that needs to be initialized
beforehand with some API call.
Post by Nolan Darilek
This is a continuation of my earlier thread, which kind of mutated when
I decided to leave Rust for Nim. So I thought I'd start a new one.
For brief context, I'm trying to use SpatiaLite, an SQLite geospatial
engine, in the browser via Emscripten. Yes I know about spatiasql.js,
but I need large databases that won't fit into memory. But Spatiasql at
least proves that the end goal is achievable.
I have a proc (roughly equivalent to a function in Nim) that initializes
the database and SpatiaLite. It works from the command line when built
```
proc newMap*(path: cstring): ptr Map {.exportc.} =
echo("Allocating map")
result = create(Map) # malloc, essentially
echo("Opening database")
result.db = db_sqlite.open($path, "", "", "")
echo("Allocating spatialite connection")
let cache = spatialite_alloc_connection()
quit("Could not allocate SpatiaLite connection")
echo("Initializing spatialite")
spatialite_init_ex(result.db, cache, 1)
echo("Done")
```
In Nim, `result` is a variable whose value is implicitly returned. As
such, this should print out a bunch of SpatiaLite initialization logs,
then "Done". And so it does via the command line. When I try loading the
```
Initializing spatialite
Invalid function pointer '0' called with signature 'X'. Perhaps this is
an invalid value (e.g. caused by calling a virtual method on a NULL
pointer)? Or calling a function with an incorrect type, which will fail
? (it is worth building your source files with -Werror (warnings are
errors), as warnings can indicate undefined behavior which can cause this)
This pointer might make sense in another type signature: i: undefined
undefined iid: undefined iii: undefined vid: undefined vii: undefined
d
undefined viiid: undefined viiii: undefined iiiiid: undefined
undefined iiiiiiiiii: undefined
0
0
Thrown: abort(0) at Error
at jsStackTrace (/home/nolan/Projects/waynav/waynav.js:1181:13)
at stackTrace (/home/nolan/Projects/waynav/waynav.js:1198:12)
at abort (/home/nolan/Projects/waynav/waynav.js:11309:44)
at nullFunc_X (/home/nolan/Projects/waynav/waynav.js:6807:1552)
at b1 (wasm-function[10433]:3)
at byn$fpcast-emu$b1 (wasm-function[10467]:1)
at _mallocWithAlarm (wasm-function[2706]:88)
at _sqlite3Malloc (wasm-function[3081]:348)
at _sqlite3DbMallocRaw (wasm-function[4810]:203)
at _sqlite3DbMallocZero (wasm-function[5014]:122)
```
This is an improvement over yesterday's error, which was the same
stacktrace in triplicate and a crash of node. :)
I tried building/linking with `-s EMULATE_FUNCTION_POINTER_CASTS=1 -g
-O0`, which I thought would eliminate that class of error. I'm aware of
the performance losses, but performance is irrelevant if I'll never get
this working anyway. :)
Are there any more tips on how to debug this in node? I'll eventually be
running it in the browser, but for now I'd rather work on it without
constructing a usable UI or fighting with IDBFS issues with large files.
Thanks.
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...