Giter VIP home page Giter VIP logo

cargo-valgrind's Introduction

cargo-valgrind

A cargo subcommand, that runs valgrind and collects its output in a helpful manner.

Latest version Documentation

This command extends cargo with the capability to directly run valgrind on any crate executable. The output of valgrind is then used to mark the binary as pass/fail.

This command should not be necessary for ordinary Rust programs, especially if you are only using safe Rust code. But if you do FFI-related stuff (either by simply using a FFI-binding crate or because you are developing a safe wrapper for such FFI bindings) it may be really helpful to check, whether the memory usages across the FFI borders are correct.

Usage

A typical mistake would be:

use std::ffi::CString;
use std::os::raw::c_char;

extern "C" {
    fn puts(s: *const c_char);
}

fn main() {
    let string = CString::new("Test").unwrap();

    let ptr = string.into_raw();
    unsafe { puts(ptr) };

    // unsafe { CString::from_raw(ptr) };
}

The memory of the variable string will never be freed. If you run cargo valgrind run in your shell, it detects the leak:

$ cargo valgrind run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/cstring`
Test
       Error Leaked 5 bytes
        Info at realloc (vg_replace_malloc.c:826)
             at realloc (alloc.rs:125)
             at realloc (alloc.rs:184)
             at reserve_internal<u8,alloc::alloc::Global> (raw_vec.rs:666)
             at reserve_exact<u8,alloc::alloc::Global> (raw_vec.rs:411)
             at reserve_exact<u8> (vec.rs:482)
             at std::ffi::c_str::CString::from_vec_unchecked (c_str.rs:355)
             at std::ffi::c_str::CString::_new (c_str.rs:330)
             at std::ffi::c_str::CString::new (c_str.rs:324)
             at cstring::main (main.rs:9)
             at std::rt::lang_start::{{closure}} (rt.rs:64)
             at {{closure}} (rt.rs:49)
             at std::panicking::try::do_call (panicking.rs:293)
             at __rust_maybe_catch_panic (lib.rs:85)
             at try<i32,closure> (panicking.rs:272)
             at catch_unwind<closure,i32> (panic.rs:394)
             at std::rt::lang_start_internal (rt.rs:48)
             at std::rt::lang_start (rt.rs:64)
             at main
     Summary Leaked 5 B total

Un-commenting the unsafe { CString::from_raw(ptr) }; re-takes the memory and frees it correctly. cargo valgrind run will compile the binary for you and won't detect a leak, since there is no leak anymore.

If you would like to pass flags to valgrind (for example to run an alternate subtool), you can set the VALGRINDFLAGS environment variable to a space-delimited list of valid Valgrind options.

Note: users of cargo-valgrind version 1.x should mind the changed command line. Previously there was a cargo valgrind subcommand, that replaced the cargo run or cargo test commands. Now the command line is cargo valgrind <command>, where <command> can be any normal cargo subcommand.

Installation

Requirements

You need to have valgrind installed and in the PATH (you can test this by running valgrind --help in your shell).

You'll also need to have cargo installed and in the PATH, but since this is a cargo subcommand, you will almost certainly have it already installed.

Install the binary

Run the following command to install from crates.io:

$ cargo install cargo-valgrind

This will install the latest official released version.

If you want to use the latest changes, that were not yet published to crates.io, you can install the binary from the git-repository like this:

$ cargo install --git https://github.com/jfrimmel/cargo-valgrind

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in cargo-valgrind by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

cargo-valgrind's People

Contributors

jfrimmel avatar avuxo avatar luni-4 avatar thewastl avatar web-flow avatar krystian-wojtas avatar wiktor-k avatar ki7dk avatar vhqtvn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.