This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnetlistFaultInjector.cpp
171 lines (142 loc) · 3.71 KB
/
netlistFaultInjector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (C) 2022 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License, as published
* by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "common.h"
#include "netlistFaultInjector.hpp"
size_t nfiErrorCnt = 0;
int NetlistFaultInjector::ModuleFiBitsCnt(size_t * bits, size_t moduleIndex)
{
const auto it = FiSignalCntCache_.find(moduleIndex);
if(FiSignalCntCache_.end() != it)
{
*bits = it->second;
return 0;
}
*bits = 0;
// Cnt fi-signals in this module
for(const auto &signal: modules[moduleIndex].FiSignal)
{
*bits += signal.Width;
}
// Cnt fi-signals in instances of this module
for(const auto &inst: modules[moduleIndex].InstanceUuids)
{
size_t instBits = 0;
if(ModuleFiBitsCnt(&instBits, inst.first))
{
nfiError("ModuleFiBitsCnt failed\n");
return -1;
}
*bits += instBits;
}
FiSignalCntCache_[moduleIndex] = *bits;
return 0;
}
static size_t randUL()
{
size_t ret;
uint8_t * retu8 = (uint8_t*) &ret;
for(size_t byte = 0; byte < sizeof(ret); byte++)
{
// coverity[DC.WEAK_CRYPTO]
retu8[byte] = rand();
}
return ret;
}
int NetlistFaultInjector::Init()
{
// Count all bits in all assignments in modules
if(ModuleFiBitsCnt(&FiBitCnt_, modulesTopIndex))
{
nfiError("ModuleFiBitsCnt failed\n");
return -1;
}
if(0 == FiBitCnt_)
{
nfiError("No fi signals\n");
return -1;
}
nfiDebug("Counted %lu fi bits\n", FiBitCnt_);
nfiDebug("Cache:\n");
#if NFI_DEBUG
for(const auto &mod: FiSignalCntCache_)
{
nfiDebug("%s: %lu\n", modules[mod.first].Name.c_str(), mod.second);
}
#endif // NFI_DEBUG
return 0;
}
int NetlistFaultInjector::RandomFiGet(std::vector<uint16_t> * moduleInstanceChain, uint32_t * assignmentUUID, size_t * width)
{
moduleInstanceChain->clear();
if(RandomFiGet(moduleInstanceChain, assignmentUUID, width, modulesTopIndex, modulesTopUUID))
{
nfiError("RandomFiGet failed\n");
return -1;
}
return 0;
}
int NetlistFaultInjector::RandomFiGet(std::vector<uint16_t> * modInst, uint32_t * assignNr, size_t * width, size_t moduleIndex, uint16_t moduleUUID)
{
modInst->push_back(moduleUUID);
if(0 == FiBitCnt_)
{
nfiError("Not initialized\n");
return -1;
}
// How many bits in this module?
size_t bitsTotal = 0;
if(ModuleFiBitsCnt(&bitsTotal, moduleIndex))
{
nfiError("ModuleFiBitsCnt failed\n");
return -1;
}
// Choose random bit
const size_t randomBit = randUL() % bitsTotal;
// In which signal / instance is the random bit?
size_t currentBit = 0;
// Go through signals in this module
// Cnt fi-signals in this module
for(const auto &signal: modules[moduleIndex].FiSignal)
{
currentBit += signal.Width;
if(randomBit < currentBit)
{
*assignNr = signal.UUID;
*width = signal.Width;
return 0; // found a signal
}
}
// Cnt fi-signals in instances of this module
for(const auto &inst: modules[moduleIndex].InstanceUuids)
{
size_t instBits = 0;
if(ModuleFiBitsCnt(&instBits, inst.first))
{
nfiError("ModuleFiBitsCnt failed\n");
return -1;
}
currentBit += instBits;
if(randomBit < currentBit)
{
return RandomFiGet(modInst, assignNr, width, inst.first, inst.second);
}
}
return 0;
}