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 "lib/ordered_map.h"
16#include "midend/coverage.h"
17
18#include "backends/p4tools/modules/testgen/core/program_info.h"
19#include "backends/p4tools/modules/testgen/core/small_step/small_step.h"
20#include "backends/p4tools/modules/testgen/lib/exceptions.h"
21#include "backends/p4tools/modules/testgen/lib/execution_state.h"
22#include "backends/p4tools/modules/testgen/lib/final_state.h"
23
24namespace P4::P4Tools::P4Testgen {
25
29class SymbolicExecutor {
30 public:
31 virtual ~SymbolicExecutor() = default;
32
33 SymbolicExecutor(const SymbolicExecutor &) = default;
34
35 SymbolicExecutor(SymbolicExecutor &&) = delete;
36
37 SymbolicExecutor &operator=(const SymbolicExecutor &) = delete;
38
39 SymbolicExecutor &operator=(SymbolicExecutor &&) = delete;
40
44 using Callback = std::function<bool(const FinalState &)>;
45
46 using Branch = SmallStepEvaluator::Branch;
47
48 using StepResult = SmallStepEvaluator::Result;
49
53 virtual void run(const Callback &callBack);
54
55 virtual void runImpl(const Callback &callBack, ExecutionStateReference executionState) = 0;
56
57 explicit SymbolicExecutor(AbstractSolver &solver, const ProgramInfo &programInfo);
58
60 void printCurrentTraceAndBranches(std::ostream &out, const ExecutionState &executionState);
61
64
66 [[nodiscard]] bool updateVisitedNodes(const P4::Coverage::CoverageSet &newNodes);
67
69 [[nodiscard]] const ordered_map<cstring, size_t> &getSkippedPaths() const;
70
71 protected:
76
79
82
85
88
91
96 bool handleTerminalState(const Callback &callback, const ExecutionState &terminalState);
97
99 StepResult step(ExecutionState &state);
100
104 static bool evaluateBranch(const SymbolicExecutor::Branch &branch, AbstractSolver &solver);
105
107 // Remove the branch from the container.
108 static SymbolicExecutor::Branch popRandomBranch(
109 std::vector<SymbolicExecutor::Branch> &candidateBranches);
110
111 private:
112 SmallStepEvaluator evaluator;
113};
114
115} // namespace P4::P4Tools::P4Testgen
116
117#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
const ordered_map< cstring, size_t > & getSkippedPaths() const
Definition symbolic_executor.cpp:127
ordered_map< cstring, size_t > skippedPaths
The paths the executor has skipped, one entry per exception.
Definition symbolic_executor.h:90
static SymbolicExecutor::Branch popRandomBranch(std::vector< SymbolicExecutor::Branch > &candidateBranches)
Select a branch at random from the input.
Definition symbolic_executor.cpp:88
static bool evaluateBranch(const SymbolicExecutor::Branch &branch, AbstractSolver &solver)
Definition symbolic_executor.cpp:72
bool updateVisitedNodes(const P4::Coverage::CoverageSet &newNodes)
Update the set of visited nodes. Returns true if there was an update.
Definition symbolic_executor.cpp:109
StepResult step(ExecutionState &state)
Take one step in the program and return list of possible branches.
Definition symbolic_executor.cpp:31
const P4::Coverage::CoverageSet & getVisitedNodes()
Getter to access visitedNodes.
Definition symbolic_executor.cpp:117
const P4::Coverage::CoverageSet & coverableNodes
Set of all nodes, to be retrieved from programInfo.
Definition symbolic_executor.h:84
const ProgramInfo & programInfo
Target-specific information about the P4 program.
Definition symbolic_executor.h:78
void handleUnimplemented(const TestgenUnimplemented &e)
Definition symbolic_executor.cpp:119
bool handleTerminalState(const Callback &callback, const ExecutionState &terminalState)
Definition symbolic_executor.cpp:50
std::function< bool(const FinalState &)> Callback
Definition symbolic_executor.h:44
virtual void run(const Callback &callBack)
Definition symbolic_executor.cpp:46
P4::Coverage::CoverageSet visitedNodes
Set of all nodes executed in any testcase that has been outputted.
Definition symbolic_executor.h:87
void printCurrentTraceAndBranches(std::ostream &out, const ExecutionState &executionState)
Writes a list of the selected branches into.
Definition symbolic_executor.cpp:131
Definition backends/p4tools/modules/testgen/lib/exceptions.h:17
Definition ordered_map.h:23
std::set< const IR::Node *, SourceIdCmp > CoverageSet
Definition coverage.h:47
Definition phv/solver/action_constraint_solver.cpp:34