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

Refactor Host and AdviceProvider #1572

Merged
merged 12 commits into from
Nov 22, 2024
Merged

Conversation

plafer
Copy link
Contributor

@plafer plafer commented Nov 7, 2024

Builds on #1571

A round of refactoring to make the PR that addresses #1457 more self contained.

@plafer plafer requested a review from bobbinth November 7, 2024 20:23
@plafer plafer force-pushed the plafer-refactor-process-state branch from d27cb3d to c21c8ba Compare November 7, 2024 20:23
@plafer plafer force-pushed the plafer-1457-refactor-host-advice-provider branch from 164b6c3 to 8af6358 Compare November 8, 2024 15:19
@plafer plafer force-pushed the plafer-refactor-process-state branch from c21c8ba to 90619ec Compare November 19, 2024 10:04
@plafer plafer force-pushed the plafer-1457-refactor-host-advice-provider branch from 8af6358 to 2f9669a Compare November 19, 2024 10:17
@plafer plafer force-pushed the plafer-refactor-process-state branch 2 times, most recently from 2b49880 to b7e9d44 Compare November 19, 2024 10:26
@plafer plafer force-pushed the plafer-1457-refactor-host-advice-provider branch from 2f9669a to 592414f Compare November 19, 2024 10:32
@plafer plafer force-pushed the plafer-1457-refactor-host-advice-provider branch 2 times, most recently from 3afd7d0 to cb51a16 Compare November 19, 2024 16:33
Base automatically changed from plafer-refactor-process-state to next November 19, 2024 19:31
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Thank you! Not a full review, but I left a couple of small comments inline.

More generally, I'd like to understand what is the overall plan for the refactoring. Depending on how we go, it seems to me that a subsequent PR may need to reverse many of the changes in this PR.

For example, I'm imagining that Host::on_event() method will handle the actual dispatch of advice injectors. It could look something like:

fn on_event(
    &mut self,
    process: ProcessState,
    event_source: u32,
    event_id: u32
) -> Result<(), ExecutionError> {
    let handler = self.get_event_handler(event_source_id)?;
    handler.process(event_id, process, self.advice_provider_mut())
}

If we go this way, instead of removing individual advice injector functions, we'd remove the corresponding functions from the AdviceProvider trait. This would make the AdviceProvider trait pretty general (i.e., all injector functionality would be defined separately, while the trait will contain mostly "required" methods).

Comment on lines 58 to +63
fn on_debug(
&mut self,
_process: ProcessState,
_options: &DebugOptions,
) -> Result<HostResponse, ExecutionError> {
) -> Result<(), ExecutionError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we change &mut self to just &self here? The thought is that debug decorators should not modify the state of the host. But maybe there is a good reason not to do that.

Also nit: I'd probably name the parameters process and options and add a clippy exception to this method (this way it will look nicer in docs).

Comment on lines 68 to +70
/// Handles the trace emitted from the VM.
fn on_trace(
&mut self,
_process: ProcessState,
_trace_id: u32,
) -> Result<HostResponse, ExecutionError> {
fn on_trace(&mut self, _process: ProcessState, _trace_id: u32) -> Result<(), ExecutionError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above.

@plafer
Copy link
Contributor Author

plafer commented Nov 20, 2024

More generally, I'd like to understand what is the overall plan for the refactoring.

I saw this PR as a cleanup that we'd want no matter ultimate direction of the subsequent PR. The main contribution is to remove the AdviceProvider::set_advice() and AdviceProvider::get_advice(). For example, set_advice() shoehorned all possible advice injector functionality into a single method call, and then aggregated all possible return types into this HostResponse (with From implementations that would panic if the wrong type is returned). Basically I changed it so that the caller calls the appropriate AdviceProvider method instead, and get the appropriate return type directly instead of through this HostResponse, removing all that complexity. Similarly for AdviceProvider::get_advice().

Then the Host re-exposed those set_advice() and get_advice() methods, blurring the line between Host and AdviceProvider. In the current approach, the Host exposes the advice provider, and the caller can the interact with it directly. It makes for a cleaner separation where the Host manages the advice provider, rather than "being one". It's also easier to refactor - if we change the AdviceProvider, we don't have to duplicate the same changes on the Host.

Finally I inlined all the AdviceProvider default methods to remove a level of indirection - most of these were just a few lines.

Depending on how we go, it seems to me that a subsequent PR may need to reverse many of the changes in this PR.

For example, I'm imagining that Host::on_event() method will handle the actual dispatch of advice injectors. It could look something like:

That's fine, I don't think this undoes any of the changes here - just more moving things around.

If we go this way, instead of removing individual advice injector functions, we'd remove the corresponding functions from the AdviceProvider trait.

I agree with this direction - I'd like the AdviceProvider trait to look as closely as our high-level description of it as possible. I thought about making the AdviceProvider trait have only 3 methods: return a stack, map, and store. But then this would be a lot of changes and this PR was already decently big.

@plafer plafer force-pushed the plafer-1457-refactor-host-advice-provider branch from cb51a16 to f40dffc Compare November 21, 2024 12:12
Also renames the variant to `FalconSigToStack`
…eToStack`

In preparation for converting advice injectors to instructions. This parameter was never used, and is not crucial - the caller can always put the key on top of the stack before calling this injector.
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! Let's merge this together with #1581.

@plafer plafer force-pushed the plafer-1457-refactor-host-advice-provider branch from 19cc0e7 to cfd166e Compare November 22, 2024 15:32
@plafer plafer merged commit 811f89f into next Nov 22, 2024
9 checks passed
@plafer plafer deleted the plafer-1457-refactor-host-advice-provider branch November 22, 2024 15:36
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