Skip to content

Commit 1ddbc21

Browse files
author
Wenkang Huang
committed
chore: replace magic value with enum
1 parent 814f719 commit 1ddbc21

File tree

2 files changed

+79
-48
lines changed

2 files changed

+79
-48
lines changed

libs2eplugins/src/s2e/Plugins/Searchers/ValidPathSearcher.cpp

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void ValidPathSearcher::initialize() {
2121
s2e()->getExecutor()->setSearcher(this);
2222
m_onStateForkConn =
2323
s2e()->getCorePlugin()->onStateFork.connect(sigc::mem_fun(*this, &ValidPathSearcher::onStateFork));
24+
m_onTranslateBlockEndConn = s2e()->getCorePlugin()->onTranslateBlockEnd.connect(
25+
sigc::mem_fun(*this, &ValidPathSearcher::onTranslateBlockEnd));
2426

2527
onARMFunctionConn = s2e()->getPlugin<ARMFunctionMonitor>();
2628
onARMFunctionConn->onARMFunctionCallEvent.connect(sigc::mem_fun(*this, &ValidPathSearcher::onARMFunctionCall));
@@ -31,67 +33,37 @@ void ValidPathSearcher::initialize() {
3133
m_searcherActive = false;
3234
}
3335

34-
bool ValidPathSearcher::insertToForkStates(uint32_t callerPC, uint32_t forkPC, S2EExecutionState *state) {
35-
const auto &foundSameCallerPC = m_forkStateItems.find(callerPC);
36-
if (foundSameCallerPC != m_forkStateItems.end()) {
37-
const auto &foundSameForkPC = foundSameCallerPC->second.find(forkPC);
38-
if (foundSameForkPC != foundSameCallerPC->second.end()) {
39-
if (foundSameForkPC->second.size() >= 2) {
40-
return false;
41-
}
42-
}
43-
}
44-
m_forkStateItems[callerPC][forkPC].push_back(state);
45-
m_idStateMap[state->getID()] = {state, forkPC, 0};
46-
return true;
47-
}
48-
49-
void ValidPathSearcher::onStateFork(S2EExecutionState *state, const std::vector<S2EExecutionState *> &newStates,
50-
const std::vector<klee::ref<klee::Expr>> &newConditions) {
51-
getDebugStream() << "onStateFork" << '\n';
52-
53-
uint32_t forkPC = state->regs()->getPc();
54-
getDebugStream() << "[forkPC: " << hexval(forkPC) << "] "
55-
<< "[stateID: " << hexval(state->getID()) << "] "
56-
<< "[new stateID: " << hexval(newStates[0]->getID()) << "] "
57-
<< "[new stateID: " << hexval(newStates[1]->getID()) << "] "
58-
<< "\n";
59-
insertToForkStates(m_callerPC, forkPC, newStates[0]);
60-
insertToForkStates(m_callerPC, forkPC, newStates[1]);
61-
m_searcherActive = true;
62-
}
63-
6436
klee::ExecutionState &ValidPathSearcher::selectState() {
6537
getDebugStream() << "selectState"
6638
<< "\n";
6739

6840
if (m_selfSwitch) {
6941
m_selfSwitch = false;
70-
m_idStateMap[m_curState->getID()].flag = 2;
42+
m_idStateMap[m_curState->getID()].flag = VALID;
7143
getDebugStream() << "[self switch, stateID: " << m_curState->getID() << "] "
7244
<< "\n";
7345
return *m_curState;
7446
}
7547

7648
// flag 0 firstly
7749
for (auto it = m_idStateMap.rbegin(); it != m_idStateMap.rend(); ++it) {
78-
if (it->second.flag == 0) {
79-
it->second.flag = 2;
50+
if (it->second.flag == UNVISITED) {
51+
it->second.flag = VALID;
8052
m_curState = it->second.state;
81-
getDebugStream() << "[flag 0: selected stateID: " << m_curState->getID() << "] "
53+
getDebugStream() << "[selected unvisited stateID: " << m_curState->getID() << "] "
8254
<< "\n";
8355
return *m_curState;
8456
}
8557
}
86-
// flag 2 secondly
87-
// for (auto it = m_idStateMap.rbegin(); it != m_idStateMap.rend(); ++it) {
88-
// if (it->second.flag == 2) {
89-
// m_curState = it->second.state;
90-
// getDebugStream() << "[flag 2: selected stateID: " << m_curState->getID() << "] "
91-
// << "\n";
92-
// return *m_curState;
93-
// }
94-
// }
58+
// flag 2 secondly
59+
for (auto it = m_idStateMap.rbegin(); it != m_idStateMap.rend(); ++it) {
60+
if (it->second.flag == VALID) {
61+
m_curState = it->second.state;
62+
getDebugStream() << "[selected valid stateID: " << m_curState->getID() << "] "
63+
<< "\n";
64+
return *m_curState;
65+
}
66+
}
9567

9668
getDebugStream() << "[no option, stateID: " << m_curState->getID() << "] "
9769
<< "\n";
@@ -104,22 +76,62 @@ void ValidPathSearcher::update(klee::ExecutionState *current, const klee::StateS
10476
S2EExecutionState *removedState = static_cast<S2EExecutionState *>(it);
10577
auto found = m_idStateMap.find(removedState->getID());
10678
if (found != m_idStateMap.end()) {
107-
found->second.flag = 1;
79+
found->second.flag = INVALID;
10880
}
10981
}
11082
}
11183

11284
bool ValidPathSearcher::empty() {
113-
getDebugStream() << "empty"
114-
<< "\n";
11585
for (const auto &it : m_idStateMap) {
116-
if (it.second.flag == 0 || it.second.flag == 2) {
86+
if (it.second.flag == UNVISITED || it.second.flag == VALID) {
87+
getDebugStream() << "empty"
88+
<< "\n";
11789
return false;
11890
}
11991
}
12092
return true;
12193
}
12294

95+
bool ValidPathSearcher::insertToForkStates(uint32_t callerPC, uint32_t forkPC, S2EExecutionState *state) {
96+
const auto &foundSameCallerPC = m_forkStateItems.find(callerPC);
97+
if (foundSameCallerPC != m_forkStateItems.end()) {
98+
const auto &foundSameForkPC = foundSameCallerPC->second.find(forkPC);
99+
if (foundSameForkPC != foundSameCallerPC->second.end()) {
100+
if (foundSameForkPC->second.size() >= 2) {
101+
return false;
102+
}
103+
}
104+
}
105+
m_forkStateItems[callerPC][forkPC].push_back(state);
106+
m_idStateMap[state->getID()] = {state, forkPC, UNVISITED};
107+
return true;
108+
}
109+
110+
void ValidPathSearcher::onStateFork(S2EExecutionState *state, const std::vector<S2EExecutionState *> &newStates,
111+
const std::vector<klee::ref<klee::Expr>> &newConditions) {
112+
getDebugStream() << "onStateFork" << '\n';
113+
114+
uint32_t forkPC = state->regs()->getPc();
115+
getDebugStream() << "[forkPC: " << hexval(forkPC) << "] "
116+
<< "[stateID: " << hexval(state->getID()) << "] "
117+
<< "[new stateID: " << hexval(newStates[0]->getID()) << "] "
118+
<< "[new stateID: " << hexval(newStates[1]->getID()) << "] "
119+
<< "\n";
120+
insertToForkStates(m_callerPC, forkPC, newStates[0]);
121+
insertToForkStates(m_callerPC, forkPC, newStates[1]);
122+
m_idStateMap[state->getID()].flag = VALID;
123+
m_searcherActive = true;
124+
}
125+
126+
void ValidPathSearcher::onTranslateBlockEnd(ExecutionSignal *signal, S2EExecutionState *state, TranslationBlock *tb,
127+
uint64_t pc, bool isStatic, uint64_t staticTargetPc) {
128+
getDebugStream() << "onTranslateBlockEnd" << '\n';
129+
130+
m_tbNum++;
131+
getDebugStream() << "[the number of blocks: " << m_tbNum << "]"
132+
<< "\n";
133+
}
134+
123135
void ValidPathSearcher::onARMFunctionCall(S2EExecutionState *state, uint32_t pcCaller, uint64_t pcCtxHashVal,
124136
uint32_t pcReturn) {
125137
getDebugStream() << "onARMFunctionCall"
@@ -147,9 +159,11 @@ void ValidPathSearcher::onARMFunctionReturn(S2EExecutionState *state, uint32_t p
147159
uint32_t forkPC = m_idStateMap[state->getID()].forkPC;
148160

149161
for (auto &it : m_forkStateItems[callerPC][forkPC]) {
150-
if (m_idStateMap[it->getID()].flag == 0) {
162+
if (m_idStateMap[it->getID()].flag == INVALID) {
151163
m_selfSwitch = true;
152164
m_curState = m_idStateMap[it->getID()].state;
165+
getDebugStream() << "self switch"
166+
<< "\n";
153167
void *cp = g_s2e->getPlugin("CorePlugin");
154168
s2e()->getExecutor()->validPathSearcherStateSwitchCallback(cp);
155169
s2e()->getExecutor()->setCpuExitRequest();

libs2eplugins/src/s2e/Plugins/Searchers/ValidPathSearcher.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ struct StateItem {
3737
uint32_t flag;
3838
};
3939

40+
enum StatePhase { UNVISITED, INVALID, VALID };
41+
4042
class ValidPathSearcher : public Plugin, public klee::Searcher {
4143
S2E_PLUGIN
4244

@@ -48,8 +50,10 @@ class ValidPathSearcher : public Plugin, public klee::Searcher {
4850

4951
private:
5052
sigc::connection m_onStateForkConn;
53+
sigc::connection m_onTranslateBlockEndConn;
5154
ARMFunctionMonitor *onARMFunctionConn;
5255

56+
uint32_t m_tbNum;
5357
uint32_t m_callerPC;
5458
uint32_t m_returnPC;
5559
S2EExecutionState *m_curState;
@@ -69,6 +73,19 @@ class ValidPathSearcher : public Plugin, public klee::Searcher {
6973
*/
7074
void onStateFork(S2EExecutionState *state, const std::vector<S2EExecutionState *> &newStates,
7175
const std::vector<klee::ref<klee::Expr>> &newConditions);
76+
77+
/**
78+
*
79+
* @param signal
80+
* @param state
81+
* @param tb
82+
* @param pc
83+
* @param isStatic
84+
* @param staticTargetPc
85+
*/
86+
void onTranslateBlockEnd(ExecutionSignal *signal, S2EExecutionState *state, TranslationBlock *tb, uint64_t pc,
87+
bool isStatic, uint64_t staticTargetPc);
88+
7289
/**
7390
* callback when function call.
7491
*

0 commit comments

Comments
 (0)