Rust FFI memleak (Even when I don't call any library function)


#1

I’m writing wrappers for some C APIs and I’ve a test.rs

extern crate journald;

#[test]
fn all_ok() {
    println!("Hello world");
}

When I run valgrind, this is the output

valgrind --leak-check=full --show-leak-kinds=all ./target/debug/test-b45d069281b30d87 
==1933== Memcheck, a memory error detector
==1933== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1933== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1933== Command: ./target/debug/test-b45d069281b30d87
==1933== 

running 1 test
test all_ok ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

==1933== 
==1933== HEAP SUMMARY:
==1933==     in use at exit: 32 bytes in 1 blocks
==1933==   total heap usage: 15 allocs, 14 frees, 2,384 bytes allocated
==1933== 
==1933== 32 bytes in 1 blocks are still reachable in loss record 1 of 1
==1933==    at 0x4C2DB95: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1933==    by 0x4E3969E: _dlerror_run (dlerror.c:141)
==1933==    by 0x4E39060: dlopen@@GLIBC_2.2.5 (dlopen.c:87)
==1933==    by 0x152777: dynamic_lib::_$LT$impl$GT$::open::h76b0f4e87cbc85c5a0d (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x158B5F: sys::thread::_$LT$impl$GT$::new::he9a70169ae8f9f72Eyw (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x134E72: run_test::run_test_inner::h955b7b34d1387b0cYYb (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x128CAD: run_test::h0249d6a0c66e284biYb (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x11A8DF: run_tests_console::h8f4136861dce29e9IGb (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x1180A2: test_main::h7e69c7919067bd4000a (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x11E887: test_main_static::h33c1034ce1415856f3a (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x1156A6: __test::main::h17e80fb8a4f2a707Maa (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933==    by 0x15DFF4: sys_common::unwind::try::try_fn::h5008875067004982501 (in /home/user1/Desktop/journald-rs/target/debug/test-b45d069281b30d87)
==1933== 
==1933== LEAK SUMMARY:
==1933==    definitely lost: 0 bytes in 0 blocks
==1933==    indirectly lost: 0 bytes in 0 blocks
==1933==      possibly lost: 0 bytes in 0 blocks
==1933==    still reachable: 32 bytes in 1 blocks
==1933==         suppressed: 0 bytes in 0 blocks
==1933== 
==1933== For counts of detected and suppressed errors, rerun with: -v
==1933== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Why is still reachable 32 bytes even though I’m not using any library function ?


#2

The test harness is doing work in the background, part of which involves calling dlopen. _dlerror_run intentionally allocates memory it does not free and it looks like your valgrind does not have a suppression for it: http://stackoverflow.com/questions/1542457/memory-leak-reported-by-valgrind-in-dlopen


#3

Ohh… Thank you