paralegal_policy

Struct RootContext

Source
pub struct RootContext {
    marker_to_ids: HashMap<Marker, MarkerTargets>,
    desc: ProgramDescription,
    flows_to: Option<HashMap<Endpoint, CtrlFlowsTo>>,
    pub(crate) diagnostics: DiagnosticsRecorder,
    name_map: HashMap<Identifier, Vec<DefId>>,
    pub(crate) config: Arc<Config>,
    pub(crate) stats: ContextStats,
}
Expand description

Interface for defining policies.

Holds a PDG (Self::desc) and defines basic queries like Self::all_nodes_for_ctrl and combinators such as Self::always_happens_before. These should be composed into more complex policies.

To communicate the results of your policies with the user you can emit diagnostic messages. To communicate a policy failure use error or the crate::assert_error macro. To communicate suspicious circumstances that are not outright cause for failure use warning or assert_warning. For all types of errors, including those with span information for a particular node, see the crate::Diagnostics trait.

Note that these methods just queue the diagnostics messages. To emit them (and potentially terminate the program if the policy does not hold) use Self::emit_diagnostics. If you used super::GraphLocation::with_context this will be done automatically for you.

Fields§

§marker_to_ids: HashMap<Marker, MarkerTargets>§desc: ProgramDescription§flows_to: Option<HashMap<Endpoint, CtrlFlowsTo>>§diagnostics: DiagnosticsRecorder§name_map: HashMap<Identifier, Vec<DefId>>§config: Arc<Config>§stats: ContextStats

Implementations§

Source§

impl RootContext

Source

pub fn always_happens_before( &self, starting_points: impl IntoIterator<Item = GlobalNode>, is_checkpoint: impl FnMut(GlobalNode) -> bool, is_terminal: impl FnMut(GlobalNode) -> bool, ) -> Result<AlwaysHappensBefore>

Enforce that on every data flow path from the starting_points to is_terminal a node satisfying is_checkpoint is passed.

Fails if ctrl_id on a provided starting point is not found.

The return value contains some statistics information about the traversal. The property holds if AlwaysHappensBefore::holds is true.

Note that is_checkpoint and is_terminal will be called many times and should thus be efficient computations. In addition they should always return the same result for the same input.

Source§

impl RootContext

Source

pub fn new(desc: ProgramDescription, config: Config) -> Self

Construct a Context from a ProgramDescription.

This also precomputes some data structures like an index over markers.

Source

pub fn associated_call_site(&self, node: GlobalNode) -> CallString

👎Deprecated: Use NodeExt::associated_call_site instead

Find the call string for the statement or function that produced this node.

Source

pub fn consuming_call_sites( &self, node: GlobalNode, ) -> impl Iterator<Item = CallString> + '_

👎Deprecated: Use NodeQueries::consuming_call_sites instead

Call sites that consume this node directly. E.g. the outgoing edges.

Source

pub fn controllers_by_name( &self, name: Identifier, ) -> impl Iterator<Item = Endpoint> + '_

Find all controllers that bare this name.

This function is intended for use in writing test cases. Actual policies should generally refrain from working with controller names, other than printing them in error messages or for debugging. Policies contingent on controller names are likely unsound.

Source

pub fn controller_by_name(&self, name: Identifier) -> Result<Endpoint>

Find a singular controller with this name.

This function should only be used in tests as the same caveats apply as in Self::controllers_by_name.

§Returns Err

If there is not exactly one controller of this name.

Source

pub fn find_by_name(&self, name: impl AsRef<str>) -> Result<DefId>

Find a type, controller or function id by its name.

Since many often share the same name this can fail with too many candidates. To handle such cases use Self::find_by_path or Self::find_all_by_name.

Source

pub fn find_all_by_name(&self, name: impl AsRef<str>) -> Result<&[DefId]>

Find all types, controllers and functions with this name.

Source

pub fn find_by_path(&self, path: impl AsRef<[Identifier]>) -> Result<DefId>

Find a type, controller or function with this path.

Source

pub fn emit_diagnostics_may_exit(&self, w: impl Write) -> Result<()>

Dispatch and drain all queued diagnostics, aborts the program if any of them demand failure.

Source

pub fn emit_diagnostics(&self, w: impl Write) -> Result<bool>

Dispatch and drain all queued diagnostics without aborting the program.

Source

pub fn all_nodes(&self) -> impl Iterator<Item = GlobalNode> + '_

Returns all nodes that are in any of the PDGs

Source

pub fn roots_where<'a>( &'a self, f: impl Fn(GlobalNode) -> bool + 'a, ) -> impl Iterator<Item = GlobalNode> + 'a

Return nodes that satisfy the predicate and which have no ancestors that satisfy the same predicate.

Source

pub fn report_marker_if_absent(&self, marker: Marker)

Emit a warning if this marker was not found in the source code.

Source

fn build_index_on_markers( desc: &ProgramDescription, ) -> HashMap<Marker, MarkerTargets>

Source

fn build_flows_to(desc: &ProgramDescription) -> HashMap<Endpoint, CtrlFlowsTo>

Source

pub fn flows_to( &self, src: impl IntoIterGlobalNodes, sink: impl IntoIterGlobalNodes, edge_type: EdgeSelection, ) -> bool

👎Deprecated: Use NodeQueries::flows_to instead

Returns whether a node flows to a node through the configured edge type.

Nodes do not flow to themselves. CallArgument nodes do flow to their respective CallSites.

If you use flows_to with EdgeSelection::Control, you might want to consider using RootContext::has_ctrl_influence, which additionally considers intermediate nodes which the src node has data flow to and has ctrl influence on the sink.

Source

pub fn controller_argument( &self, ctrl_id: Endpoint, index: u32, ) -> Option<GlobalNode>

Find the node that represents the indexth argument of the controller ctrl_id.

§Returns None

If the controller with this id does not exist or the controller has fewer than index arguments.

Source

pub fn has_ctrl_influence( &self, influencer: impl IntoIterGlobalNodes, target: impl IntoIterGlobalNodes, ) -> bool

Returns whether there is direct control flow influence from influencer to sink, or there is some node which is data-flow influenced by influencer and has direct control flow influence on target. Or as expressed in code:

some n where self.flows_to(influencer, n, EdgeSelection::Data) && self.flows_to(n, target, EdgeSelection::Control).

Source

pub fn influencers( &self, sink: impl IntoIterGlobalNodes, edge_type: EdgeSelection, ) -> impl Iterator<Item = GlobalNode> + '_

Returns iterator over all Nodes that influence the given sink Node.

Does not return the input node. A CallSite sink will return all of the associated CallArgument nodes.

Source

pub fn influencees( &self, src: impl IntoIterGlobalNodes, edge_type: EdgeSelection, ) -> impl Iterator<Item = GlobalNode> + '_

Returns iterator over all Nodes that are influenced by the given src Node.

Does not return the input node. A CallArgument src will return the associated CallSite.

Source

pub fn get_node_types(&self, node: GlobalNode) -> &[DefId]

👎Deprecated: Use NodeExt::types instead

Get the type(s) of a Node.

Source

pub fn has_marker(&self, marker: Marker, node: GlobalNode) -> bool

👎Deprecated: Use NodeExt::has_marker instead

Returns whether the given Node has the marker applied to it directly or via its type.

Source

pub fn all_nodes_for_ctrl( &self, ctrl_id: Endpoint, ) -> impl Iterator<Item = GlobalNode> + '_

Returns all DataSources, DataSinks, and CallSites for a Controller as Nodes.

Source

pub fn srcs_with_type( &self, ctrl_id: Endpoint, t: DefId, ) -> impl Iterator<Item = GlobalNode> + '_

Returns an iterator over the data sources within controller c that have type t.

Source

pub fn roots( &self, ctrl_id: Endpoint, edge_type: EdgeSelection, ) -> impl Iterator<Item = GlobalNode> + '_

Returns an iterator over all nodes that do not have any influencers of the given edge_type.

Source

pub fn desc(&self) -> &ProgramDescription

Returns the input ProgramDescription.

Source

pub fn otypes(&self, id: TypeId) -> &[TypeId]

Returns all the type alias annotation for a given type

Source

pub fn marked_type(&self, marker: Marker) -> &[DefId]

Return all types that are marked with marker

Source

pub fn any_flows( &self, from: &[GlobalNode], to: &[GlobalNode], edge_type: EdgeSelection, ) -> Option<(GlobalNode, GlobalNode)>

Return an example pair for a flow from an source from from to a sink in to if any exist.

Source

pub fn all_controllers(&self) -> impl Iterator<Item = (Endpoint, &SPDG)>

Iterate over all defined controllers

Source

pub fn describe_def(&self, def_id: DefId) -> DisplayDef<'_>

Returns a DisplayDef for the given def_id

Source

pub fn describe_node(&self, node: GlobalNode) -> DisplayNode<'_>

👎Deprecated: Use NodeExt::describe instead

Returns a DisplayNode for the given Node

Source

pub fn inputs_of(&self, call_string: CallString) -> NodeCluster

Return which data is being read from for the modification performed at this location

Source

pub fn outputs_of(&self, call_string: CallString) -> NodeCluster

Return which data is being written to at this location

Source

pub fn node_info(&self, node: GlobalNode) -> &NodeInfo

👎Deprecated: Use NodeExt::info instead

Retrieve metadata about a node.

Source

pub fn instruction_at_node(&self, node: GlobalNode) -> &InstructionInfo

Retrieve metadata about the instruction executed by a specific node.

Source

pub fn successors( &self, node: GlobalNode, ) -> impl Iterator<Item = GlobalNode> + '_

👎Deprecated: Use NodeExt::successors instead

Return the immediate successors of this node

Source

pub fn predecessors( &self, node: GlobalNode, ) -> impl Iterator<Item = GlobalNode> + '_

👎Deprecated: Use NodeExt::predecessors instead

Return the immediate predecessors of this node

Source

pub fn get_location(&self, node: GlobalNode) -> &Span

👎Deprecated: Use NodeExt::get_location instead

Get the span of a node

Source§

impl RootContext

Source

pub fn named_policy<A>( self: Arc<Self>, name: impl Into<Identifier>, policy: impl FnOnce(Arc<PolicyContext>) -> A, ) -> A

Add a policy to the diagnostic context.

See the module level documentation for more information on diagnostic context management.

Source

pub fn named_controller<A>( self: Arc<Self>, id: Endpoint, policy: impl FnOnce(Arc<ControllerContext>) -> A, ) -> A

Run the computation in the diagnostic context of this controller

See the module level documentation for more information on diagnostic context management.

Source

pub fn named_combinator<A>( self: Arc<Self>, name: impl Into<Identifier>, computation: impl FnOnce(Arc<CombinatorContext>) -> A, ) -> A

Nest another named combinator into the diagnostic context.

See the module level documentation for more information on diagnostic context management.

Source

pub fn controller_contexts( self: &Arc<Self>, ) -> impl Iterator<Item = Arc<ControllerContext>>

Iterate over all defined controllers as contexts

Trait Implementations§

Source§

impl Context for RootContext

Source§

fn root(&self) -> &RootContext

Get the root context object
Source§

fn nodes_marked_any_way( &self, marker: Marker, ) -> impl Iterator<Item = GlobalNode> + '_

All nodes with this marker, be that via type or directly
Source§

fn nodes_marked_via_type( &self, marker: Marker, ) -> impl Iterator<Item = GlobalNode> + '_

All nodes that have this marker through a type
Source§

fn marked_nodes(&self, marker: Marker) -> impl Iterator<Item = GlobalNode> + '_

Returns an iterator over all objects marked with marker.
Source§

impl Debug for RootContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl HasDiagnosticsBase for RootContext

Source§

fn record(&self, diagnostic: Diagnostic)

Record a diagnostic message.

Source§

fn as_ctx(&self) -> &RootContext

Access to Context, usually also available via std::ops::Deref.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> Diagnostics for T

Source§

fn struct_error(&self, msg: impl Into<String>) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for an error. Read more
Source§

fn struct_span_error( &self, span: impl Into<HighlightedSpan>, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for an error with a source code span. Read more
Source§

fn struct_warning(&self, msg: impl Into<String>) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for a warning. Read more
Source§

fn struct_span_warning( &self, span: impl Into<HighlightedSpan>, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for a warning with a source code span Read more
Source§

fn struct_help(&self, msg: impl Into<String>) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for a help message.
Source§

fn struct_span_help( &self, span: impl Into<HighlightedSpan>, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for a help message with a source code span
Source§

fn struct_note(&self, msg: impl Into<String>) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for a note
Source§

fn struct_span_note( &self, span: impl Into<HighlightedSpan>, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for a note with a source code span
Source§

fn error(&self, msg: impl Into<String>)

Emit a message that is severe enough that it causes the policy to fail.
Source§

fn warning(&self, msg: impl Into<String>)

Emit a message that indicates to the user that the policy might be fraudulent but could be correct.
Source§

fn note(&self, msg: impl Into<String>)

Emit a message that provides additional information to the user.
Source§

fn help(&self, msg: impl Into<String>)

Emit a message that suggests something to the user.
Source§

fn span_error(&self, msg: impl Into<String>, span: Span)

Emit a message that is severe enough that it causes the policy to fail with a source code span.
Source§

fn span_warning(&self, msg: impl Into<String>, span: Span)

Emit a message that indicates to the user that the policy might be fraudulent but could be correct. Includes a source code span.
Source§

fn span_note(&self, msg: impl Into<String>, span: Span)

Emit a message that provides additional information to the user.
Source§

fn span_help(&self, msg: impl Into<String>, span: Span)

Emit a message that suggests something to the user.
Source§

fn struct_node_error( &self, node: GlobalNode, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for an error with the span of a graph node. Read more
Source§

fn struct_node_warning( &self, node: GlobalNode, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for an error with the span of a graph node. Read more
Source§

fn struct_node_note( &self, node: GlobalNode, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for an note with the span of a graph node.
Source§

fn struct_node_help( &self, node: GlobalNode, msg: impl Into<String>, ) -> DiagnosticBuilder<'_, Self>

Initialize a diagnostic builder for an help message with the span of a graph node.
Source§

fn node_error(&self, node: GlobalNode, msg: impl Into<String>)

Emit an error, failing the policy, with the span of a graph node.
Source§

fn node_warning(&self, node: GlobalNode, msg: impl Into<String>)

Emit an warning, that does not fail the policy, with the span of a graph node.
Source§

fn node_note(&self, node: GlobalNode, msg: impl Into<String>)

Emit a note with the span of a graph node.
Source§

fn node_help(&self, node: GlobalNode, msg: impl Into<String>)

Emit a help message with the span of a graph node.
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.