Skip to content

Commit

Permalink
monitor/sandbox: use Callable
Browse files Browse the repository at this point in the history
Issue #5420
  • Loading branch information
nfeske committed Jan 15, 2025
1 parent 68613c8 commit c5e6d07
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 43 deletions.
8 changes: 5 additions & 3 deletions repos/os/include/sandbox/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _INCLUDE__SANDBOX__SANDBOX_H_

#include <util/xml_node.h>
#include <util/callable.h>
#include <util/noncopyable.h>
#include <base/registry.h>
#include <base/service.h>
Expand Down Expand Up @@ -60,12 +61,13 @@ class Genode::Sandbox : Noncopyable
Region_map &address_space;
};

struct Fn : Interface { virtual void call(Intrinsics &) const = 0; };
using With_intrinsics = Callable<void, Intrinsics &>;

/**
* Call 'Fn' with the 'Intrinsics' that apply for the specified PD
* Call 'fn' with the 'Intrinsics' that apply for the specified PD
*/
virtual void with_intrinsics(Capability<Pd_session>, Pd_session &, Fn const &) = 0;
virtual void with_intrinsics(Capability<Pd_session>, Pd_session &,
With_intrinsics::Ft const &fn) = 0;

/**
* Start the initial thread of new PD at the given instruction pointer
Expand Down
22 changes: 4 additions & 18 deletions repos/os/src/lib/sandbox/child.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,14 @@ class Sandbox::Child : Child_policy, Routed_service::Wakeup

enum class Sample_state_result { CHANGED, UNCHANGED };

/*
* Helper for passing lambda functions as 'Pd_intrinsics::Fn'
*/

using Pd_intrinsics = Genode::Sandbox::Pd_intrinsics;

template <typename PD_SESSION, typename FN>
template <typename PD_SESSION>
static void with_pd_intrinsics(Pd_intrinsics &pd_intrinsics,
Capability<Pd_session> cap, PD_SESSION &pd,
FN const &fn)
auto const &fn)
{
struct Impl : Pd_intrinsics::Fn
{
using Intrinsics = Pd_intrinsics::Intrinsics;

FN const &_fn;
Impl(FN const &fn) : _fn(fn) { }
void call(Intrinsics &intrinsics) const override { _fn(intrinsics); }
};

pd_intrinsics.with_intrinsics(cap, pd, Impl { fn });
pd_intrinsics.with_intrinsics(cap, pd, Pd_intrinsics::With_intrinsics::Fn { fn });
}

private:
Expand Down Expand Up @@ -306,8 +293,7 @@ class Sandbox::Child : Child_policy, Routed_service::Wakeup

Pd_intrinsics &_pd_intrinsics;

template <typename FN>
void _with_pd_intrinsics(FN const &fn)
void _with_pd_intrinsics(auto const &fn)
{
with_pd_intrinsics(_pd_intrinsics, _child.pd_session_cap(), _child.pd(), fn);
}
Expand Down
5 changes: 3 additions & 2 deletions repos/os/src/lib/sandbox/library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,14 @@ struct Genode::Sandbox::Library : ::Sandbox::State_reporter::Producer,
{
Env &_env;

void with_intrinsics(Capability<Pd_session>, Pd_session &pd, Fn const &fn) override
void with_intrinsics(Capability<Pd_session>, Pd_session &pd,
With_intrinsics::Ft const &fn) override
{
Region_map_client region_map(pd.address_space());

Intrinsics intrinsics { _env.pd(), _env.pd_session_cap(),
_env.cpu(), _env.cpu_session_cap(), region_map };
fn.call(intrinsics);
fn(intrinsics);
}

void start_initial_thread(Capability<Cpu_thread> cap, addr_t ip) override
Expand Down
25 changes: 8 additions & 17 deletions repos/os/src/monitor/gdb_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef _GDB_COMMAND_H_
#define _GDB_COMMAND_H_

#include <util/callable.h>
#include <base/registry.h>
#include <monitor/string.h>
#include <types.h>
Expand Down Expand Up @@ -47,23 +48,13 @@ struct Monitor::Gdb::Command : private Commands::Element, Interface
with_skipped_prefix(bytes, name, match_remainder_fn);
}

struct With_args_fn : Interface
{
virtual void call(Const_byte_range_ptr const &args) const = 0;
};
using With_args = Callable<void, Const_byte_range_ptr const &>;

virtual void _with_args(Const_byte_range_ptr const &, With_args_fn const &) const = 0;
virtual void _with_args(Const_byte_range_ptr const &, With_args::Ft const &) const = 0;

void with_args(Const_byte_range_ptr const &command_bytes, auto const &fn) const
{
using Fn = typeof(fn);
struct Impl : With_args_fn
{
Fn const &_fn;
Impl(Fn const &fn) : _fn(fn) { }
void call(Const_byte_range_ptr const &args) const override { _fn(args); }
};
_with_args(command_bytes, Impl(fn));
_with_args(command_bytes, With_args::Fn { fn });
}

virtual void execute(State &, Const_byte_range_ptr const &args, Output &) const = 0;
Expand Down Expand Up @@ -190,11 +181,11 @@ struct Monitor::Gdb::Command_with_separator : Command
}

void _with_args(Const_byte_range_ptr const &bytes,
With_args_fn const &fn) const override
With_args::Ft const &fn) const override
{
_match_name(bytes, [&] (Const_byte_range_ptr const &bytes) {
_match_separator(bytes, [&] (Const_byte_range_ptr const &args) {
fn.call(args); }); });
fn(args); }); });
}
};

Expand All @@ -204,10 +195,10 @@ struct Monitor::Gdb::Command_without_separator : Command
using Command::Command;

void _with_args(Const_byte_range_ptr const &bytes,
With_args_fn const &fn) const override
With_args::Ft const &fn) const override
{
_match_name(bytes, [&] (Const_byte_range_ptr const &args) {
fn.call(args); });
fn(args); });
}
};

Expand Down
7 changes: 4 additions & 3 deletions repos/os/src/monitor/pd_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ struct Monitor::Pd_intrinsics : Sandbox::Pd_intrinsics

} _monitored_ref_cpu { _env.ep(), _env.cpu_session_cap(), Session::Label { } };

void with_intrinsics(Capability<Pd_session> pd_cap, Pd_session &pd, Fn const &fn) override
void with_intrinsics(Capability<Pd_session> pd_cap, Pd_session &pd,
With_intrinsics::Ft const &fn) override
{
/*
* Depending on the presence of the PD session in our local entrypoint,
Expand All @@ -111,7 +112,7 @@ struct Monitor::Pd_intrinsics : Sandbox::Pd_intrinsics
.ref_cpu = _monitored_ref_cpu,
.ref_cpu_cap = _monitored_ref_cpu.cap(),
.address_space = inferior_pd._address_space };
fn.call(intrinsics);
fn(intrinsics);
},
[&] /* PD session not intercepted */ {

Expand All @@ -122,7 +123,7 @@ struct Monitor::Pd_intrinsics : Sandbox::Pd_intrinsics
.ref_cpu = _env.cpu(),
.ref_cpu_cap = _env.cpu_session_cap(),
.address_space = region_map };
fn.call(intrinsics);
fn(intrinsics);
}
);
}
Expand Down

0 comments on commit c5e6d07

Please sign in to comment.