-
Notifications
You must be signed in to change notification settings - Fork 139
/
Probabilities.h
476 lines (336 loc) · 11 KB
/
Probabilities.h
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2015 The University of Utah
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#ifndef PROBABILITIES_H
#define PROBABILITIES_H
#include <string>
#include <map>
#include <vector>
#include <ostream>
#include "Filter.h"
template <class Key, class Value>
class ProbabilityTable;
using namespace std;
enum ProbName {
pMoreStructUnionProb,
pBitFieldsCreationProb,
pBitFieldsSignedProb,
pBitFieldInNormalStructProb,
pScalarFieldInFullBitFieldsProb,
pExhaustiveBitFieldsProb,
pSafeOpsSignedProb,
pSelectDerefPointerProb,
pRegularVolatileProb,
pRegularConstProb,
pStricterConstProb,
pLooserConstProb,
pFieldVolatileProb,
pFieldConstProb,
pStdUnaryFuncProb,
pShiftByNonConstantProb,
pPointerAsLTypeProb,
pStructAsLTypeProb,
pUnionAsLTypeProb,
pFloatAsLTypeProb,
pNewArrayVariableProb,
pAccessOnceVariableProb,
pInlineFunctionProb,
pBuiltinFunctionProb,
// group for statement
pStatementProb,
pAssignProb,
pBlockProb,
pForProb,
pIfElseProb,
pInvokeProb,
pReturnProb,
pContinueProb,
pBreakProb,
pGotoProb,
pArrayOpProb,
// group for assignment ops
pAssignOpsProb,
pSimpleAssignProb,
pMulAssignProb,
pDivAssignProb,
pRemAssignProb,
pAddAssignProb,
pSubAssignProb,
pLShiftAssignProb,
pRShiftAssignProb,
pBitAndAssignProb,
pBitXorAssignProb,
pBitOrAssignProb,
pPreIncrProb,
pPreDecrProb,
pPostIncrProb,
pPostDecrProb,
// for unary ops
pUnaryOpsProb,
pPlusProb,
pMinusProb,
pNotProb,
pBitNotProb,
// for binary ops
pBinaryOpsProb,
pAddProb,
pSubProb,
pMulProb,
pDivProb,
pModProb,
pCmpGtProb,
pCmpLtProb,
pCmpGeProb,
pCmpLeProb,
pCmpEqProb,
pCmpNeProb,
pAndProb,
pOrProb,
pBitXorProb,
pBitAndProb,
pBitOrProb,
pRShiftProb,
pLShiftProb,
// group for simple types
pSimpleTypesProb,
pVoidProb,
pCharProb,
pIntProb,
pShortProb,
pLongProb,
pLongLongProb,
pUCharProb,
pUIntProb,
pUShortProb,
pULongProb,
pULongLongProb,
pFloatProb,
// for safe math ops
pSafeOpsSizeProb,
pInt8Prob,
pInt16Prob,
pInt32Prob,
pInt64Prob,
//for function attributes
pFuncAttrProb,
pTypeAttrProb,
pLabelAttrProb,
pVarAttrProb,
};
#define MAX_PROB_NAME ((ProbName)(pStatementProb+1))
#define MoreStructUnionTypeProb \
Probabilities::get_prob(pMoreStructUnionProb)
#define BitFieldsCreationProb \
Probabilities::get_prob(pBitFieldsCreationProb)
#define BitFieldInNormalStructProb \
Probabilities::get_prob(pBitFieldInNormalStructProb)
#define ScalarFieldInFullBitFieldsProb \
Probabilities::get_prob(pScalarFieldInFullBitFieldsProb)
#define ExhaustiveBitFieldsProb \
Probabilities::get_prob(pExhaustiveBitFieldsProb)
#define BitFieldsSignedProb \
Probabilities::get_prob(pBitFieldsSignedProb)
#define SafeOpsSignedProb \
Probabilities::get_prob(pSafeOpsSignedProb)
#define SelectDerefPointerProb \
Probabilities::get_prob(pSelectDerefPointerProb)
#define RegularVolatileProb \
Probabilities::get_prob(pRegularVolatileProb)
#define RegularConstProb \
Probabilities::get_prob(pRegularConstProb)
#define StricterConstProb \
Probabilities::get_prob(pStricterConstProb)
#define LooserConstProb \
Probabilities::get_prob(pLooserConstProb)
#define FieldVolatileProb \
Probabilities::get_prob(pFieldVolatileProb)
#define FieldConstProb \
Probabilities::get_prob(pFieldConstProb)
#define StdUnaryFuncProb \
Probabilities::get_prob(pStdUnaryFuncProb)
#define ShiftByNonConstantProb \
Probabilities::get_prob(pShiftByNonConstantProb)
#define PointerAsLTypeProb \
Probabilities::get_prob(pPointerAsLTypeProb)
#define StructAsLTypeProb \
Probabilities::get_prob(pStructAsLTypeProb)
#define UnionAsLTypeProb \
Probabilities::get_prob(pUnionAsLTypeProb)
#define FloatAsLTypeProb \
Probabilities::get_prob(pFloatAsLTypeProb)
#define NewArrayVariableProb \
Probabilities::get_prob(pNewArrayVariableProb)
#define AccessOnceVariableProb \
Probabilities::get_prob(pAccessOnceVariableProb)
#define InlineFunctionProb \
Probabilities::get_prob(pInlineFunctionProb)
#define BuiltinFunctionProb \
Probabilities::get_prob(pBuiltinFunctionProb)
#define FuncAttrProb \
Probabilities::get_prob(pFuncAttrProb)
#define TypeAttrProb \
Probabilities::get_prob(pTypeAttrProb)
#define LabelAttrProb \
Probabilities::get_prob(pLabelAttrProb)
#define VarAttrProb \
Probabilities::get_prob(pVarAttrProb)
//////////////////////////////////////////////////
#define UNARY_OPS_PROB_FILTER \
Probabilities::get_prob_filter(pUnaryOpsProb)
#define BINARY_OPS_PROB_FILTER \
Probabilities::get_prob_filter(pBinaryOpsProb)
#define SIMPLE_TYPES_PROB_FILTER \
Probabilities::get_prob_filter(pSimpleTypesProb)
#define SAFE_OPS_SIZE_PROB_FILTER \
Probabilities::get_prob_filter(pSafeOpsSizeProb)
class ProbabilityFilter : public Filter
{
public:
ProbabilityFilter(ProbName pname);
virtual ~ProbabilityFilter(void);
virtual bool filter(int v) const;
private:
const ProbName pname_;
};
class ProbElem {
public:
ProbElem() {};
virtual ~ProbElem() = 0;
virtual int get_prob(ProbName pname) = 0;
virtual void set_prob(ProbName pname, int val) = 0;
virtual void dump_default(std::ostream &out) = 0;
virtual void dump_val(std::ostream &out) = 0;
virtual void set_prob_table(ProbabilityTable<unsigned int, ProbName> * const table) = 0;
virtual bool is_equal() = 0;
};
class SingleProbElem : public ProbElem {
public:
SingleProbElem(const std::string &sname, ProbName pname, int default_val, int val);
virtual ~SingleProbElem();
int get_prob_direct();
virtual int get_prob(ProbName pname);
virtual void set_prob(ProbName pname, int val);
void set_prob(int val) { val_ = val; };
void get_all_values(std::vector<int> &values);
virtual void dump_default(std::ostream &out);
virtual void dump_val(std::ostream &out);
virtual void set_prob_table(ProbabilityTable<unsigned int, ProbName> * const table);
virtual bool is_equal() { return false; }
static const char single_elem_sep_char;
private:
const std::string sname_;
// the name used by csmith internally
const ProbName pname_;
// default probability
const int default_val_;
// actual probability
int val_;
};
class Probabilities;
class GroupProbElem : public ProbElem {
friend class ProbabilityFilter;
public:
GroupProbElem(bool is_equal, const std::string &sname);
virtual ~GroupProbElem();
virtual int get_prob(ProbName pname);
virtual void set_prob(ProbName pname, int val);
virtual void set_prob_table(ProbabilityTable<unsigned int, ProbName> * const table);
virtual void dump_default(std::ostream &out);
virtual void dump_val(std::ostream &out);
virtual bool is_equal() { return is_equal_; }
void initialize(Probabilities *impl, const std::map<ProbName, int> pairs);
static const char group_open_delim;
static const char group_close_delim;
static const char equal_open_delim;
static const char equal_close_delim;
static const char group_sep_char;
private:
int get_random_single_prob(int orig_val, std::vector<unsigned int> &invalid_vals);
bool elem_exist(ProbName pname);
void get_all_values(std::vector<SingleProbElem*> &values);
// represents equal probabilities
const bool is_equal_;
const std::string sname_;
// the name used by csmith internally
// const ProbName pname_;
std::map<ProbName, SingleProbElem*> probs_;
};
class Probabilities {
friend class ProbabilityFilter;
public:
static Probabilities *GetInstance();
static void DestroyInstance();
static unsigned int pname_to_type(ProbName pname);
static int get_random_single_prob(int orig_val);
void set_prob_table(ProbabilityTable<unsigned int, ProbName> * const table, ProbName pname);
// Only get single prob value
// Play with prob table if you need any group probs
static unsigned int get_prob(ProbName pname);
static Filter *get_prob_filter(ProbName pname);
static void register_extra_filter(ProbName pname, Filter *filter);
static void unregister_extra_filter(ProbName pname, Filter *filter);
ProbName get_pname(const std::string &sname);
std::string get_sname(ProbName pname);
bool parse_configuration(std::string &error_msg, const std::string &fname);
void dump_default_probabilities(const std::string &fname);
void dump_actual_probabilities(const std::string &fname, unsigned long seed);
Filter *get_binary_ops_prob_filter();
private:
void set_group_prob(bool is_equal, ProbName pname, const std::map<ProbName, int> &m);
bool setup_group_probabilities(bool is_equal, const std::vector<string> &elems);
bool parse_group_probabilities(bool is_equal, std::string &error_msg, const std::string &line);
int parse_single_elem(bool is_equal, ProbElem *elem, const std::string &line);
bool parse_single_probability(std::string &err_msg, const std::string &line);
bool parse_line(std::string &error_msg, std::string &line);
void set_single_name_maps();
void set_single_name(const char *sname, ProbName pname);
void set_single_name(const char *sname, ProbName pname, unsigned int type);
void set_prob_filter(ProbName pname);
bool check_extra_filter(ProbName pname, int v);
void initialize_single_probs();
void clear_filter(std::map<ProbName, Filter*> &filter);
void initialize_group_probs();
void set_default_statement_prob();
void set_default_simple_types_prob();
void set_default_binary_ops_prob();
void set_default_unary_ops_prob();
void set_default_safe_ops_size_prob();
void initialize();
static Probabilities *instance_;
static const char comment_line_prefix;
std::map<std::string, ProbName> sname_to_pname_;
std::map<ProbName, std::string> pname_to_sname_;
std::map<ProbName, unsigned int> pname_to_type_;
std::map<ProbName, ProbElem *> probabilities_;
std::map<ProbName, Filter*> prob_filters_;
std::map<ProbName, Filter*> extra_filters_;
//const static ProbabilityFilter *binary_ops_prob_filter_;
Probabilities();
~Probabilities();
};
#endif //PROBABILITIES_H