Wenzel / libmicrovmi

A cross-platform unified Virtual Machine Introspection API library
https://wenzel.github.io/libmicrovmi/
GNU General Public License v3.0
160 stars 16 forks source link

Refactor driver initialization parameters API #201

Closed Wenzel closed 2 years ago

Wenzel commented 3 years ago

This PR implements the idea discussed in #197

TODO

Adapt examples

To make driver argument parsing less tedius when maintaing the examples, I created a local subcrate utilities, in order to share common code.

This crate exposes a trait Clappable which defines 2 functions to convert a struct to Clap command line arguments, and from Clap matches.

/// This trait allows to convert a struct to Clap's command line arguments
/// and to parse back the matches into the struct
pub trait Clappable {
    /// produces an equivalent of the struct as vector of Clap arguments
    fn to_clap_args<'a, 'b>() -> Vec<Arg<'a, 'b>>;
    /// builds a new struct from Clap matches
    fn from_matches(matches: &ArgMatches) -> Self;
}

And implementing it for DriverInitParams.

Then it's only a matter of importing the trait in the examples:

use utilities::Clappable;

    App::new(file!())
        .version("0.3")
        .author("Mathieu Tarral")
        .about("Watches control register VMI events")
        .args(DriverInitParams::to_clap_args().as_ref())

// parsing the matches
let init_params = DriverInitParams::from_matches(&matches);
// init
microvmi::init(None, Some(init_params))

Adapt C API

    // set vm name and pass init params
    void* vm_name = argv[1];
    DriverInitParamsFFI init_params = {
        .common = {
            .vm_name = vm_name
        }
    };
    void* driver = microvmi_init(NULL, &init_params, &init_error);

Adapt Python API

            from microvmi import Microvmi, DriverInitParamsPy, CommonInitParamsPy, KVMInitParamsPy
            # setup common params
            common = CommonInitParamsPy()
            common.vm_name = "windows10"
            # setup kvm params
            kvm = KVMInitParamsPy()
            kvm.unix_socket = "/tmp/introspector"
            # setup main init_params
            init_params = DriverInitParamsPy()
            init_params.common = common
            init_params.kvm = kvm
            # init
            m = Microvmi(None, init_params)
codecov-commenter commented 3 years ago

Codecov Report

Merging #201 (91e92ca) into master (b257afb) will increase coverage by 1.08%. The diff coverage is 11.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #201      +/-   ##
==========================================
+ Coverage   16.52%   17.61%   +1.08%     
==========================================
  Files           5        9       +4     
  Lines         472      528      +56     
  Branches       75       81       +6     
==========================================
+ Hits           78       93      +15     
- Misses        377      417      +40     
- Partials       17       18       +1     
Flag Coverage Δ
unittests 17.61% <11.92%> (+1.08%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/api/events.rs 0.00% <0.00%> (ø)
src/api/mod.rs 0.00% <0.00%> (ø)
src/api/registers.rs 0.00% <0.00%> (ø)
src/capi.rs 0.00% <0.00%> (ø)
src/lib.rs 4.54% <0.00%> (+0.19%) :arrow_up:
utilities/src/lib.rs 0.00% <0.00%> (ø)
src/api/params.rs 18.18% <18.18%> (ø)
src/driver/kvm.rs 29.31% <84.21%> (+3.03%) :arrow_up:
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update b257afb...91e92ca. Read the comment docs.

Wenzel commented 3 years ago

The CI is green :green_circle: , @rageagainsthepc if you want to take a look ? (I'm on Slack if you questions)