Skip to content

Added 'add_fuel' command line option#3792

Merged
fitzgen merged 10 commits into
bytecodealliance:mainfrom
ilikepi63:feature/add-fuel-amount-to-configuration-set
Feb 15, 2022
Merged

Added 'add_fuel' command line option#3792
fitzgen merged 10 commits into
bytecodealliance:mainfrom
ilikepi63:feature/add-fuel-amount-to-configuration-set

Conversation

@ilikepi63
Copy link
Copy Markdown
Contributor

Relatively new to both Rust and Wasmtime, so trying to just pick up a small first issue here.

PR to try a solution to Issue #3717

Currently just adds a "add_fuel" config option to the wasmtime API. I was taking a look and I think I might need to update this default value when the interpreter is instantiated:

    pub fn new(state: InterpreterState<'a>) -> Self {
        Self { state, fuel: None }
    }

to

    pub fn new(state: InterpreterState<'a>, fuel: Option<u64> = None) -> Self {
        Self { state, fuel: fuel }
    }

Don't currently have any test cases for the specific command line option just yet. Just wanted to check if I was on the right track.

CC: @fitzgen

@ilikepi63 ilikepi63 marked this pull request as draft February 11, 2022 13:04
@ilikepi63 ilikepi63 marked this pull request as ready for review February 11, 2022 13:04
@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Feb 11, 2022

You don't need to modify the clif interpreter at all here.

You want to add a call to https://docs.rs/wasmtime/latest/wasmtime/struct.Store.html#method.add_fuel somewhere around here: https://github.com/bytecodealliance/wasmtime/blob/main/src/commands/run.rs#L165

@ilikepi63
Copy link
Copy Markdown
Contributor Author

@fitzgen Thank you so much! That specific instantiation eluded me!

Thank you so much for being so patient and understanding, as well as holding my hand through this!

Copy link
Copy Markdown
Contributor

@a1phyr a1phyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could warn the user if --add-fuel is used without --consume-fuel ?

Comment thread src/commands/run.rs Outdated
// if consume fuel has been flagged, we want to instantiate the
// store with the set amount of fuel from the 'add_fuel' option
if self.common.consume_fuel {
store.add_fuel(self.common.add_fuel).unwrap();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to panic here, even though this should never fail.

Suggested change
store.add_fuel(self.common.add_fuel).unwrap();
store.add_fuel(self.common.add_fuel)?;

Copy link
Copy Markdown
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

We can use clap's requires functionality to require that both --consume-fuel and --add-fuel are given if either one is given. This avoids having to check and warn (or at least pushes that into clap rather than here).


But backing up a bit, the fact that users always have to path both is a hint that maybe this is one knob, not two different knobs.

What if instead we removed --consume-fuel and --add-fuel <N> and instead had just --fuel <N>.

#[structopt(long)]
fuel: Option<u64>,

and then when configuring the config and store we would do this:

// When configuring the config...
if self.common.fuel.is_some() {
    config.consume_fuel(true);
}

// ...

// When configuring the store...
if let Some(fuel) = self.common.fuel {
    store.add_fuel(fuel)?;
}

Does that make sense?

Comment thread src/lib.rs Outdated
Comment on lines +242 to +243
#[structopt(long, default_value = "0")]
add_fuel: u64,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[structopt(long, default_value = "0")]
add_fuel: u64,
#[structopt(long, requires = "consume_fuel")]
add_fuel: Option<u64>,

Comment thread src/commands/run.rs Outdated
Comment on lines +167 to +179
// if consume fuel has been flagged, we want to instantiate the
// store with the set amount of fuel from the 'add_fuel' option
if self.common.consume_fuel {
store.add_fuel(self.common.add_fuel)?;
}

// warn the user if 'add_fuel' is set - is not 0 - and 'consume_fuel' is not
if self.common.add_fuel != 0 && !self.common.consume_fuel {
eprintln!(
"warning: using `--add-fuel` without '--consume-fuel' flag\
being set. This currently won't do anything."
);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// if consume fuel has been flagged, we want to instantiate the
// store with the set amount of fuel from the 'add_fuel' option
if self.common.consume_fuel {
store.add_fuel(self.common.add_fuel)?;
}
// warn the user if 'add_fuel' is set - is not 0 - and 'consume_fuel' is not
if self.common.add_fuel != 0 && !self.common.consume_fuel {
eprintln!(
"warning: using `--add-fuel` without '--consume-fuel' flag\
being set. This currently won't do anything."
);
}
if self.common.consume_fuel {
let fuel = self.common.add_fuel.expect("both add_fuel and consume_fuel must be given");
store.add_fuel(fuel)?;
}

Comment thread src/lib.rs Outdated
Comment on lines 238 to 239
#[structopt(long)]
consume_fuel: bool,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[structopt(long)]
consume_fuel: bool,
#[structopt(long, requires = "add_fuel")]
consume_fuel: bool,

@ilikepi63
Copy link
Copy Markdown
Contributor Author

ilikepi63 commented Feb 15, 2022

The only concern I could see from --fuel is the point where someone could call without --add-fuel to indicate that they want to --consume-fuel with 0 fuel.

I am not sure if there is a use case, but just thought I would mention it.

@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Feb 15, 2022

The only concern I could see from --fuel is the point where someone could call without --add-fuel to indicate that they want to --consume-fuel with 0 fuel.

In that case they would be able to do --fuel 0.

Copy link
Copy Markdown
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks great, I just have a couple tiny nitpicks on the wording of the comments. In general, we try to use capitalization and punctuation in our code comments.

Once you commit these suggestions, I'll hit the merge button.

Thanks again!

Comment thread src/lib.rs Outdated
Comment thread src/commands/run.rs Outdated
@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Feb 15, 2022

Once you commit these suggestions, I'll hit the merge button.

Or I guess github lets me add those suggestions as commits to your branch :)

@ilikepi63
Copy link
Copy Markdown
Contributor Author

Thanks @fitzgen!

Learnt a lot from this. Eager to try some more once I get a better understanding of compiler architecture. Thanks again for all the input, you are the best!

@fitzgen fitzgen merged commit 85cf4b0 into bytecodealliance:main Feb 15, 2022
mpardesh pushed a commit to avanhatt/wasmtime that referenced this pull request Mar 17, 2022
* Added 'add_fuel' command line option

* Added default value to 'add_fuel' config option

* Added 'add_fuel' to run command Store instantiation

* Added comment

* Added warning for add-fuel without consume-fuel

* Formatting

* Changed out --add-fuel and --consume-fuel to --fuel

* Formatting

* Update src/lib.rs

* Update src/commands/run.rs

Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
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 this pull request may close these issues.

3 participants