hlua
hlua copied to clipboard
Crash when accepting HashMap as function argument
Paste following test to hlua/src/functions_write.rs:
// Next line is line 578 so that backtrace matches source
#[test]
fn functions_accept_hashmap() {
use AnyHashableLuaValue;
use AnyLuaValue;
use std::collections::{BTreeMap, HashMap};
let mut lua = Lua::new();
let orig: HashMap<i32, f64> = [1., 2., 3.].into_iter().enumerate().map(|(k, v)| (k as i32, *v as f64)).collect();
let orig_copy = orig.clone();
// Collect to BTreeMap so that iterator yields values in order
let orig_btree: BTreeMap<_, _> = orig_copy.into_iter().collect();
lua.set("v", orig);
let foo = {
move |read: HashMap<_, _>| {
// Same as above
let read_btree: BTreeMap<_, _> = read.into_iter().collect();
for (o, r) in orig_btree.iter().zip(read_btree.iter()) {
if let (&AnyHashableLuaValue::LuaNumber(i), &AnyLuaValue::LuaNumber(n)) = r {
let (&o_i, &o_n) = o;
assert_eq!(o_i, i);
assert_eq!(o_n, n);
} else {
panic!("Unexpected variant");
}
}
}
};
lua.set("foo", function1(foo));
let () = lua.execute("foo()").unwrap();
}
Run:
± % ➜ cargo test -- functions_accept_hashmap
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running /home/mkpankov/workspace/projects/hlua/target/debug/deps/hlua-c599ad8b2ea80e00
running 1 test
error: An unknown error occurred
Then run with debugger attached:
../target/debug/deps/hlua-c599ad8b2ea80e00 functions_accept_hashmap
It reports SIGSEGV.
Backtrace:
#0 0x000055555564b9f7 in findindex (L=0x7ffff640d000, t=0x7ffff640c060, key=0x7ffff6410040) at lua/src/ltable.c:154
#1 0x000055555564bae8 in luaH_next (L=0x7ffff640d000, t=0x7ffff640c060, key=0x7ffff6410040) at lua/src/ltable.c:170
#2 0x000055555563dccd in lua_next (L=0x7ffff640d000, idx=-1) at lua/src/lapi.c:1120
#3 0x0000555555600b6d in hlua::rust_tables::{{impl}}::lua_read_at_position<&mut hlua::functions_write::InsideCallback> (lua=0x7ffff69fd488, index=0) at src/rust_tables.rs:205
#4 0x000055555563b1db in hlua::tuples::{{impl}}::lua_read_at_position<&mut hlua::functions_write::InsideCallback,std::collections::hash::map::HashMap<hlua::any::AnyHashableLuaValue, hlua::any::AnyLuaValue, std::collections::hash::map::RandomState>> (lua=0x7ffff69fd488, index=0) at src/tuples.rs:27
#5 0x00005555555e7af4 in hlua::functions_write::wrapper<hlua::functions_write::Function<closure, (std::collections::hash::map::HashMap<hlua::any::AnyHashableLuaValue, hlua::any::AnyLuaValue, std::collections::hash::map::RandomState>), ()>,(std::collections::hash::map::HashMap<hlua::any::AnyHashableLuaValue, hlua::any::AnyLuaValue, std::collections::hash::map::RandomState>),()> (lua=0x7ffff640d000) at src/functions_write.rs:398
#6 0x00005555556406e6 in luaD_precall (L=0x7ffff640d000, func=0x7ffff6410030, nresults=0) at lua/src/ldo.c:319
#7 0x000055555565150f in luaV_execute (L=0x7ffff640d000) at lua/src/lvm.c:709
#8 0x0000555555640b4f in luaD_call (L=0x7ffff640d000, func=0x7ffff6410020, nResults=1, allowyield=0) at lua/src/ldo.c:402
#9 0x000055555563d6db in f_call (L=0x7ffff640d000, ud=0x7ffff69fdd20) at lua/src/lapi.c:923
#10 0x000055555563fcbe in luaD_rawrunprotected (L=0x7ffff640d000, f=0x55555563d6a4 <f_call>, ud=0x7ffff69fdd20) at lua/src/ldo.c:131
#11 0x00005555556412c4 in luaD_pcall (L=0x7ffff640d000, func=0x55555563d6a4 <f_call>, u=0x7ffff69fdd20, old_top=32, ef=0) at lua/src/ldo.c:603
#12 0x000055555563d7a8 in lua_pcallk (L=0x7ffff640d000, nargs=0, nresults=1, errfunc=0, ctx=0, k=0x0) at lua/src/lapi.c:949
#13 0x00005555555ee0af in lua52_sys::lua_pcall (L=<optimized out>, nargs=<optimized out>, nresults=<optimized out>, errfunc=<optimized out>) at /home/mkpankov/workspace/projects/hlua/lua52-sys/src/lib.rs:252
#14 hlua::lua_functions::LuaFunction<hlua::PushGuard<&mut hlua::Lua>>::call_with_args<hlua::PushGuard<&mut hlua::Lua>,(),(),hlua::Void> (self=0x7ffff69fe210, args=()) at src/lua_functions.rs:270
#15 0x00005555555ec027 in hlua::lua_functions::LuaFunction<hlua::PushGuard<&mut hlua::Lua>>::call<hlua::PushGuard<&mut hlua::Lua>,()> (self=0x7ffff69fe210) at src/lua_functions.rs:227
#16 0x0000555555615e69 in hlua::Lua::execute<()> (self=0x7ffff69fe320, code=...) at src/lib.rs:624
#17 0x00005555555e997d in hlua::functions_write::tests::functions_accept_hashmap () at src/functions_write.rs:608
#18 0x0000555555672502 in test::run_test::{{closure}} () at /checkout/src/libtest/lib.rs:1451
#19 core::ops::FnOnce::call_once<closure,(())> () at /checkout/src/libcore/ops.rs:2683
#20 test::{{impl}}::call_box<(),closure> () at /checkout/src/libtest/lib.rs:140
#21 0x00005555556ae01b in panic_unwind::__rust_maybe_catch_panic () at /checkout/src/libpanic_unwind/lib.rs:98
#22 0x00005555556669c4 in std::panicking::try<(),std::panic::AssertUnwindSafe<closure>> () at /checkout/src/libstd/panicking.rs:433
#23 std::panic::catch_unwind<std::panic::AssertUnwindSafe<closure>,()> () at /checkout/src/libstd/panic.rs:361
#24 test::run_test::run_test_inner::{{closure}} () at /checkout/src/libtest/lib.rs:1390
#25 std::sys_common::backtrace::__rust_begin_short_backtrace<closure,()> () at /checkout/src/libstd/sys_common/backtrace.rs:136
#26 0x0000555555667763 in std::thread::{{impl}}::spawn::{{closure}}::{{closure}}<closure,()> () at /checkout/src/libstd/thread/mod.rs:364
#27 std::panic::{{impl}}::call_once<(),closure> () at /checkout/src/libstd/panic.rs:296
#28 std::panicking::try::do_call<std::panic::AssertUnwindSafe<closure>,()> () at /checkout/src/libstd/panicking.rs:454
#29 0x00005555556ae01b in panic_unwind::__rust_maybe_catch_panic () at /checkout/src/libpanic_unwind/lib.rs:98
#30 0x000055555566d6ed in std::panicking::try<(),std::panic::AssertUnwindSafe<closure>> () at /checkout/src/libstd/panicking.rs:433
#31 std::panic::catch_unwind<std::panic::AssertUnwindSafe<closure>,()> () at /checkout/src/libstd/panic.rs:361
#32 std::thread::{{impl}}::spawn::{{closure}}<closure,()> () at /checkout/src/libstd/thread/mod.rs:363
#33 alloc::boxed::{{impl}}::call_box<(),closure> () at /checkout/src/liballoc/boxed.rs:648
#34 0x00005555556a5ec6 in alloc::boxed::{{impl}}::call_once<(),()> () at /checkout/src/liballoc/boxed.rs:658
#35 std::sys_common::thread::start_thread () at /checkout/src/libstd/sys_common/thread.rs:21
#36 std::sys::imp::thread::{{impl}}::new::thread_start () at /checkout/src/libstd/sys/unix/thread.rs:84
#37 0x00007ffff77b8184 in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0
#38 0x00007ffff72cdffd in clone () from /lib/x86_64-linux-gnu/libc.so.6
Looks similar to #149
Doesn't make a lot of sense to me. I probably won't be able to debug this as it will take me a lot of time understanding the bowels of hlua and Lua C FFI.
For the record it's because you're not passing any parameter when calling foo.
Obviously it's still not supposed to crash.