mongodb-labs / mongorover

Intern project - MongoDB driver for the Lua programming language - This Repository is NOT a supported MongoDB product
Apache License 2.0
55 stars 10 forks source link

Lua 5.1 compiled with LUA_USE_APICHECK: assertion failed #57

Open neoxic opened 8 years ago

neoxic commented 8 years ago

With Lua 5.1 compiled in development mode (-D LUA_USE_APICHECK) which is encouraged for module development, mongorover fails on opening with the following failed assertion:

lua5.1: lapi.c:449: lua_pushlstring: Assertion `L->top < L->ci->top' failed.
==2784==
==2784== Process terminating with default action of signal 6 (SIGABRT)
==2784==    at 0x55BA04F: raise (in /usr/lib/libc-2.24.so)
==2784==    by 0x55BB479: abort (in /usr/lib/libc-2.24.so)
==2784==    by 0x55B2EA6: __assert_fail_base (in /usr/lib/libc-2.24.so)
==2784==    by 0x55B2F51: __assert_fail (in /usr/lib/libc-2.24.so)
==2784==    by 0x405B57: lua_pushlstring (lapi.c:449)
==2784==    by 0x4141E5: luaL_findtable (lauxlib.c:364)
==2784==    by 0x4142CE: luaL_openlib (lauxlib.c:247)
==2784==    by 0x5F99CAD: newlib_compat (lua-version-compat.c:61)
==2784==    by 0x5F95E8B: luaopen_mongo_module (mongo-module.c:61)
==2784==    by 0x4091C7: luaD_precall (ldo.c:320)
==2784==    by 0x4095F3: luaD_call (ldo.c:377)
==2784==    by 0x41F4EB: ll_require (loadlib.c:484)

Debugging it myself, I found that the stack is uncontrolled in luaopen_mongo_module. Please note the stack size control policy here: http://www.lua.org/manual/5.1/manual.html#3.2 It also seems odd to me that the 20 default slots are not enough for such a non-recursive function as luaopen_mongo_module(). It looks there are too many lua_pushvalue()'s or some other issues. I believe the same applies to higher Lua versions as well.

neoxic commented 8 years ago

I must also note that I observe many repetitive compilation warnings on my system (Archlinux) such as:


In file included from /usr/include/libmongoc-1.0/mongoc-stream.h:25:0,
                 from /usr/include/libmongoc-1.0/mongoc-gridfs.h:26,
                 from /usr/include/libmongoc-1.0/mongoc-client.h:31,
                 from /usr/include/libmongoc-1.0/mongoc.h:27,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongoc-wrapper.h:9,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-bson.h:21,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/mongo-module.h:19,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/mongo-module.c:17:
/usr/include/libmongoc-1.0/mongoc-socket.h:96:51: warning: ‘struct addrinfo’ declared inside parameter list will not be visible outside of this definition or declaration
 void             mongoc_socket_inet_ntop  (struct addrinfo         *rp,
                                                   ^~~~~~~~
/mnt/seny/Downloads/mongorover-master/c_wrapper/lua-bson.c: In function ‘_iterate_and_add_values_document_or_array_to_table’:
/mnt/seny/Downloads/mongorover-master/c_wrapper/lua-bson.c:652:36: warning: passing argument 1 of ‘bson_oid_to_string’ from incompatible pointer type [-Wincompatible-pointer-types]
                 bson_oid_to_string(value->value.v_oid.bytes, oid_str);
                                    ^~~~~
In file included from /usr/include/libbson-1.0/bson.h:42:0,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongoc-wrapper.h:8,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-bson.h:21,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-bson.c:17:
/usr/include/libbson-1.0/bson-oid.h:56:10: note: expected ‘const bson_oid_t * {aka const struct <anonymous> *}’ but argument is of type ‘const uint8_t * {aka const unsigned char *}’
 void     bson_oid_to_string        (const bson_oid_t *oid,
          ^~~~~~~~~~~~~~~~~~
/mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.c: In function ‘lua_mongo_cursor_iterate’:
/mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.c:56:60: warning: passing argument 2 of ‘bson_document_or_array_to_table’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
         throw_error = !(bson_document_or_array_to_table(L, doc, true, absolute_luaBSONObjects_index, &error));
                                                            ^~~
In file included from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.h:21:0,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.c:16:
/mnt/seny/Downloads/mongorover-master/c_wrapper/lua-bson.h:55:6: note: expected ‘bson_t * {aka struct _bson_t *}’ but argument is of type ‘const bson_t * {aka const struct _bson_t *}’
 bool bson_document_or_array_to_table(lua_State *L,
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.c:57:22: warning: passing argument 1 of ‘bson_destroy’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
         bson_destroy(doc);
                      ^~~
In file included from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongoc-wrapper.h:8:0,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.h:20,
                 from /mnt/seny/Downloads/mongorover-master/c_wrapper/lua-mongo-cursor.c:16:
/usr/include/libbson-1.0/bson.h:374:1: note: expected ‘bson_t * {aka struct _bson_t *}’ but argument is of type ‘const bson_t * {aka const struct _bson_t *}’
 bson_destroy (bson_t *bson);
 ^~~~~~~~~~~~

This makes one kind of suspicious of other hidden faults. :-)

christopherjwang commented 8 years ago

I'll try to reproduce your LUA_USE_APICHECK error hopefully soon. I just use pre-compiled binaries for Lua. As to the compile warnings, they should be addressed. Feel free to make a PR. I'm pretty sure it's just sticking a bunch of consts before input parameter types. I could be completely wrong though.

christopherjwang commented 8 years ago

It would expedite the process if you posted a snippet of code that causes the error in your first comment. Thanks.

neoxic commented 8 years ago

No code snippet required. Just compile Lua 5.1 with LUA_USE_APICHECK enabled and then try to run your tests.