Skip to content
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

cacache-11.5.2 crashes with SIGBUS when disk full and data size <= 512KB #48

Closed
komar007 opened this issue May 17, 2023 · 0 comments · Fixed by #50
Closed

cacache-11.5.2 crashes with SIGBUS when disk full and data size <= 512KB #48

komar007 opened this issue May 17, 2023 · 0 comments · Fixed by #50

Comments

@komar007
Copy link
Contributor

komar007 commented May 17, 2023

cacache-11.5.2 crashes a program with SIGBUS when working under disk-full conditions on linux.
Running ubuntu-19.10 with linux-5.3.0-64-generic.

$ rustc --version
rustc 1.69.0 (84c898d65 2023-04-16)

Reproduction:

  • create a filesystem with limited space, for example mkdir /tmp/ram && mount -ttmpfs -osize=5m tmpfs /tmp/ram
  • run the following program:
    fn main() {
        for i in 0..12 {
            println!("{}", i);
            let data: Vec<_> = (0..512*1024).map(|_| rand::random::<u8>()).collect();
            println!("{:?}", cacache::write_hash_sync("/tmp/ram/cache", &data));
        }
    }
    
    // in Cargo.toml
    // ...
    // [dependencies]
    // cacache = "11.5.2"
    // rand = "0.8"
  • result:
    0
    Writer::new size=Some(524288)
    Ok(Integrity { hashes: [Hash { algorithm: Sha256, digest: "QQ9CVmHX6CzNPkuGFAhp/k8wSkmEVexMp6ARULmLdMM=" }] })
    1
    Writer::new size=Some(524288)
    Ok(Integrity { hashes: [Hash { algorithm: Sha256, digest: "JAI/yZ2LjUfpKko8L4RFV7g7DzNxHvq7jfYhX/9mQ4o=" }] })
    [...]
    9
    Writer::new size=Some(524288)
    Bus error (core dumped)
    

The problem is caused by the optimization where if the binary blob to cache is no more than 512KB, then it is written via mmap. Since the file obtained from tempfile may be sparse, writing to it may result in allocation of more blocks on the fs and failure with SIGBUS (I'm not sure this is standard/defined behavior or not, it seems legit though - what else can the OS do?). The call to std::fs::File::set_len only results in calling the truncate syscall, which does not guarantee file allocation and does not return an error on not enough space on device.

Calling posix_fallocate on the fd of the file obtained from tempfile fixes the issue for me, but I am not sure posix_fallocate guarantees file allocation or it just happens to work ok.

@zkat zkat linked a pull request May 19, 2023 that will close this issue
@zkat zkat closed this as completed in #50 May 19, 2023
zkat pushed a commit that referenced this issue May 19, 2023
Fixes: #48

This avoids SIGBUS on memory write in case the temp file is sparse.
Implemented for linux only; other target_os cfg values unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant