P4C
The P4 Compiler
Loading...
Searching...
No Matches
symbolic_executor.h
1/*
2 * SPDX-FileCopyrightText: 2023 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_SYMBOLIC_EXECUTOR_SYMBOLIC_EXECUTOR_H_
8#define BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_SYMBOLIC_EXECUTOR_SYMBOLIC_EXECUTOR_H_
9
10#include <functional>
11#include <iosfwd>
12#include <vector>
13
14#include "ir/solver.h"
15#include "midend/coverage.h"
16
17#include "backends/p4tools/modules/testgen/core/program_info.h"
18#include "backends/p4tools/modules/testgen/core/small_step/small_step.h"
19#include "backends/p4tools/modules/testgen/lib/execution_state.h"
20#include "backends/p4tools/modules/testgen/lib/final_state.h"
21
22namespace P4::P4Tools::P4Testgen {
23
27class SymbolicExecutor {
28 public:
29 virtual ~SymbolicExecutor() = default;
30
31 SymbolicExecutor(const SymbolicExecutor &) = default;
32
33 SymbolicExecutor(SymbolicExecutor &&) = delete;
34
35 SymbolicExecutor &operator=(const SymbolicExecutor &) = delete;
36
37 SymbolicExecutor &operator=(SymbolicExecutor &&) = delete;
38
42 using Callback = std::function<bool(const FinalState &)>;
43
44 using Branch = SmallStepEvaluator::Branch;
45
46 using StepResult = SmallStepEvaluator::Result;
47
51 virtual void run(const Callback &callBack);
52
53 virtual void runImpl(const Callback &callBack, ExecutionStateReference executionState) = 0;
54
55 explicit SymbolicExecutor(AbstractSolver &solver, const ProgramInfo &programInfo);
56
58 void printCurrentTraceAndBranches(std::ostream &out, const ExecutionState &executionState);
59
62
64 [[nodiscard]] bool updateVisitedNodes(const P4::Coverage::CoverageSet &newNodes);
65
66 protected:
69
72
75
78
83 bool handleTerminalState(const Callback &callback, const ExecutionState &terminalState);
84
86 StepResult step(ExecutionState &state);
87
91 static bool evaluateBranch(const SymbolicExecutor::Branch &branch, AbstractSolver &solver);
92
94 // Remove the branch from the container.
95 static SymbolicExecutor::Branch popRandomBranch(
96 std::vector<SymbolicExecutor::Branch> &candidateBranches);
97
98 private:
99 SmallStepEvaluator evaluator;
100};
101
102} // namespace P4::P4Tools::P4Testgen
103
104#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_SYMBOLIC_EXECUTOR_SYMBOLIC_EXECUTOR_H_ */
Provides a higher-level interface for an SMT solver.
Definition solver.h:30
Represents state of execution after having reached a program point.
Definition execution_state.h:40
Represents the final state after execution.
Definition final_state.h:25
Stores target-specific information about a P4 program.
Definition core/program_info.h:27
static SymbolicExecutor::Branch popRandomBranch(std::vector< SymbolicExecutor::Branch > &candidateBranches)
Select a branch at random from the input.
Definition symbolic_executor.cpp:86
static bool evaluateBranch(const SymbolicExecutor::Branch &branch, AbstractSolver &solver)
Definition symbolic_executor.cpp:70
bool updateVisitedNodes(const P4::Coverage::CoverageSet &newNodes)
Update the set of visited nodes. Returns true if there was an update.
Definition symbolic_executor.cpp:107
StepResult step(ExecutionState &state)
Take one step in the program and return list of possible branches.
Definition symbolic_executor.cpp:29
const P4::Coverage::CoverageSet & getVisitedNodes()
Getter to access visitedNodes.
Definition symbolic_executor.cpp:115
const P4::Coverage::CoverageSet & coverableNodes
Set of all nodes, to be retrieved from programInfo.
Definition symbolic_executor.h:74
const ProgramInfo & programInfo
Target-specific information about the P4 program.
Definition symbolic_executor.h:68
bool handleTerminalState(const Callback &callback, const ExecutionState &terminalState)
Definition symbolic_executor.cpp:48
std::function< bool(const FinalState &)> Callback
Definition symbolic_executor.h:42
virtual void run(const Callback &callBack)
Definition symbolic_executor.cpp:44
P4::Coverage::CoverageSet visitedNodes
Set of all nodes executed in any testcase that has been outputted.
Definition symbolic_executor.h:77
void printCurrentTraceAndBranches(std::ostream &out, const ExecutionState &executionState)
Writes a list of the selected branches into.
Definition symbolic_executor.cpp:117
std::set< const IR::Node *, SourceIdCmp > CoverageSet
Definition coverage.h:47
Definition phv/solver/action_constraint_solver.cpp:33