-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[asan] SEGV when you forget to close a File #5
Comments
A possible fix that doesn't rely on destructors running (you cannot rely on that because Here's the repro case using static lifetimes: use core::mem;
use littlefs2::{
consts, driver,
fs::{File, Filesystem},
io::{Result, Write},
ram_storage,
};
ram_storage!(
name=RamStorage,
backend=Ram,
trait=driver::Storage,
erase_value=0xff,
read_size=20*5,
write_size=20*7,
cache_size_ty=consts::U700,
block_size=20*35,
block_count=32,
lookaheadwords_size_ty=consts::U1,
filename_max_plus_one_ty=consts::U256,
path_max_plus_one_ty=consts::U256,
result=Result,
);
fn main() {
let mut ram: &'static mut _ = Box::leak(Box::new(Ram::default()));
let mut storage = RamStorage::new(ram);
let mut alloc = Filesystem::allocate();
Filesystem::format(&mut storage).unwrap();
let mut fs = Filesystem::mount(&mut alloc, &mut storage).unwrap();
foo(&mut fs, &mut storage);
bar(&mut fs, &mut storage);
println!("OK");
}
#[inline(never)]
fn foo(fs: &mut Filesystem<RamStorage<'static>>, storage: &mut RamStorage<'static>) {
let mut fa: &'static mut _ = Box::leak(Box::new(File::allocate()));
let f = File::create("a.txt", &mut fa, fs, storage).unwrap();
mem::forget(f); // this is now OK
}
#[inline(never)]
fn bar<'r>(fs: &mut Filesystem<RamStorage<'r>>, storage: &mut RamStorage<'r>) {
let mut fa = File::allocate();
let mut f = File::create("b.txt", &mut fa, fs, storage).unwrap();
f.write(fs, storage, b"Hello").unwrap();
f.close(fs, storage).unwrap();
} With this asan errors with a memory leak, which is OK in safe Rust. $ RUSTFLAGS='-Z sanitizer=address' cargo r --target x86_64-unknown-linux-gnu
OK
=================================================================
==31737==ERROR: LeakSanitizer: detected memory leaks
(..)
SUMMARY: AddressSanitizer: 23232 byte(s) leaked in 2 allocation(s). Instead of |
Same comment as for https://github.com/nickray/littlefs2/issues/3:
|
Originally reported in #3. This ticket includes an example that can be run on x86_64 and instrumented with LLVM AddressSanitizer.
STR
The text was updated successfully, but these errors were encountered: