Skip to content

Commit

Permalink
Added two options: --[no]global-variables
Browse files Browse the repository at this point in the history
Passing --no-global-variables to csmith will force
the generator to skip generating global variables.

One implication of this change is that all the generated
programs will output the same checksum (i.e. 0), because
checksums are computed using global variables.
  • Loading branch information
chenyang78 committed May 30, 2016
1 parent 5af4618 commit 69963ae
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/Block.cpp
Expand Up @@ -335,7 +335,9 @@ Block*
Block::random_parent_block(void)
{
vector<Block*> blks;
blks.push_back(NULL);
if (CGOptions::global_variables()) {
blks.push_back(NULL);
}
Block* tmp = this;
while (tmp) {
blks.push_back(tmp);
Expand Down
2 changes: 2 additions & 0 deletions src/CGOptions.cpp
Expand Up @@ -148,6 +148,7 @@ DEFINE_GETTER_SETTER_BOOL(arg_unions)
DEFINE_GETTER_SETTER_BOOL(volatiles)
DEFINE_GETTER_SETTER_BOOL(volatile_pointers)
DEFINE_GETTER_SETTER_BOOL(const_pointers)
DEFINE_GETTER_SETTER_BOOL(global_variables)
DEFINE_GETTER_SETTER_BOOL(access_once)
DEFINE_GETTER_SETTER_BOOL(strict_volatile_rule)
DEFINE_GETTER_SETTER_BOOL(addr_taken_of_locals)
Expand Down Expand Up @@ -268,6 +269,7 @@ CGOptions::set_default_settings(void)
volatiles(true);
volatile_pointers(true);
const_pointers(true);
global_variables(true);
consts(true);
dangling_global_ptrs(true);
divs(true);
Expand Down
4 changes: 4 additions & 0 deletions src/CGOptions.h
Expand Up @@ -292,6 +292,9 @@ class CGOptions {
static bool const_pointers(void);
static bool const_pointers(bool p);

static bool global_variables(void);
static bool global_variables(bool p);

static std::string vol_tests_mach(void);
static bool set_vol_tests(const std::string &s);

Expand Down Expand Up @@ -559,6 +562,7 @@ class CGOptions {
static bool volatiles_;
static bool volatile_pointers_;
static bool const_pointers_;
static bool global_variables_;
static std::string vol_tests_mach_;
static bool access_once_;
static bool strict_volatile_rule_;
Expand Down
2 changes: 1 addition & 1 deletion src/CVQualifiers.cpp
Expand Up @@ -187,7 +187,7 @@ CVQualifiers::match_indirect(const CVQualifiers& qfer) const
void
CVQualifiers::make_scalar_volatiles(std::vector<bool> &volatiles)
{
if (!CGOptions::volatile_pointers()) {
if (!CGOptions::volatile_pointers() || !CGOptions::global_variables()) {
for (size_t i=1; i<volatiles.size(); i++)
volatiles[i] = false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Probabilities.cpp
Expand Up @@ -560,7 +560,9 @@ Probabilities::initialize_single_probs()
else
m[pLooserConstProb] = 0;

if (CGOptions::volatiles() && CGOptions::vol_struct_union_fields())
if (CGOptions::volatiles() &&
CGOptions::vol_struct_union_fields() &&
CGOptions::global_variables())
m[pFieldVolatileProb] = 30;
else
m[pFieldVolatileProb] = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/RandomProgramGenerator.cpp
Expand Up @@ -194,6 +194,7 @@ static void print_help()
cout << " --volatiles | --no-volatiles: enable | disable volatiles (enabled by default)." << endl << endl;
cout << " --volatile-pointers | --no-volatile-pointers: enable | disable volatile pointers (enabled by default)." << endl << endl;
cout << " --const-pointers | --no-const-pointers: enable | disable const pointers (enabled by default)." << endl << endl;
cout << " --global-variables | --no-global-variables: enable | disable global variables (enabled by default)." << endl << endl;

cout << " --builtins | --no-builtins: enable | disable to generate builtin functions (disabled by default)." << endl << endl;
cout << " --enable-builtin-kinds k1,k2 | --disable-builtin-kinds k1,k2: enable | disable certain kinds of builtin functions." << endl << endl;
Expand Down Expand Up @@ -920,6 +921,16 @@ main(int argc, char **argv)
continue;
}

if (strcmp (argv[i], "--global-variabless") == 0) {
CGOptions::global_variables(true);
continue;
}

if (strcmp (argv[i], "--no-global-variables") == 0) {
CGOptions::global_variables(false);
continue;
}

if (strcmp (argv[i], "--enable-access-once") == 0) {
CGOptions::access_once(true);
continue;
Expand Down
38 changes: 26 additions & 12 deletions src/VariableSelector.cpp
Expand Up @@ -116,10 +116,17 @@ VariableSelector::InitScopeTable()
{
if (scopeTable_ == NULL) {
scopeTable_ = new ProbabilityTable<unsigned int, eVariableScope>();
scopeTable_->add_elem(35, eGlobal);
scopeTable_->add_elem(65, eParentLocal);
scopeTable_->add_elem(95, eParentParam);
scopeTable_->add_elem(100, eNewValue);
if (CGOptions::global_variables()) {
scopeTable_->add_elem(35, eGlobal);
scopeTable_->add_elem(65, eParentLocal);
scopeTable_->add_elem(95, eParentParam);
scopeTable_->add_elem(100, eNewValue);
}
else {
scopeTable_->add_elem(50, eParentLocal);
scopeTable_->add_elem(95, eParentParam);
scopeTable_->add_elem(100, eNewValue);
}
}
}

Expand All @@ -128,6 +135,7 @@ VariableSelector::InitScopeTable()
static string
RandomGlobalName(void)
{
assert(CGOptions::global_variables() && "no global variables!");
return gensym("g_");
}

Expand Down Expand Up @@ -847,7 +855,7 @@ VariableSelector::make_init_value(Effect::Access access, const CGContext &cg_con
CVQualifiers qfer_deref = qfer.random_loose_qualifiers(no_volatile, access, cg_context);
qfer_deref.remove_qualifiers(1);
qfer_deref.accept_stricter = false;
bool use_local = (b != 0 && type->eType == ePointer && !qfer_deref.is_volatile());
bool use_local = (!CGOptions::global_variables() || (b != 0 && type->eType == ePointer && !qfer_deref.is_volatile()));
const Type* tt = use_local ? Type::random_type_from_type(type, true, true) : Type::random_type_from_type(type, false, true);
ERROR_GUARD(NULL);
// create a local if it's not a volatile, and it's a pointer, and block is specified
Expand Down Expand Up @@ -1034,7 +1042,7 @@ VariableSelectionProbability(eVariableScope upper = MAX_VAR_SCOPE, Filter *filte
static eVariableScope
VariableCreationProbability(void)
{
bool flag = rnd_flipcoin(10);
bool flag = CGOptions::global_variables() && rnd_flipcoin(10);
ERROR_GUARD(MAX_VAR_SCOPE);
if (flag) // 10% chance to create new global var
return eGlobal;
Expand Down Expand Up @@ -1142,7 +1150,13 @@ VariableSelector::SelectLoopCtrlVar(const CGContext &cg_context, const vector<co
Variable* var = choose_var(vars, Effect::WRITE, cg_context, type, 0, eConvert, invalid_vars, true);
ERROR_GUARD(NULL);
if (var == NULL) {
var = GenerateNewGlobal(Effect::WRITE, cg_context, type, 0);
if (CGOptions::global_variables()) {
var = GenerateNewGlobal(Effect::WRITE, cg_context, type, 0);
}
else {
var = GenerateNewParentLocal(*cg_context.get_current_block(),
Effect::WRITE, cg_context, type, 0);
}
}
return var;
}
Expand Down Expand Up @@ -1238,10 +1252,10 @@ VariableSelector::select_deref_pointer(Effect::Access access, const CGContext &c
if (!ptr_type) {
return 0;
}
CVQualifiers ptr_qfer = (!qfer || qfer->wildcard)
? CVQualifiers::random_qualifiers(ptr_type, access, cg_context, true)
//: qfer->indirect_qualifiers(-1);
: qfer->random_add_qualifiers(!cg_context.get_effect_context().is_side_effect_free());
CVQualifiers ptr_qfer = (!qfer || qfer->wildcard || !CGOptions::global_variables())
? CVQualifiers::random_qualifiers(ptr_type, access, cg_context, true)
//: qfer->indirect_qualifiers(-1);
: qfer->random_add_qualifiers(!cg_context.get_effect_context().is_side_effect_free());
ERROR_GUARD(NULL);
ptr_qfer.accept_stricter = false;
if (access == Effect::WRITE) {
Expand Down Expand Up @@ -1300,7 +1314,7 @@ VariableSelector::create_array_and_itemize(Block* blk, string name, const CGCont
ArrayVariable*
VariableSelector::create_random_array(const CGContext& cg_context)
{
bool as_global = rnd_flipcoin(25);
bool as_global = CGOptions::global_variables() && rnd_flipcoin(25);
ERROR_GUARD(NULL);
string name;
Block* blk = 0;
Expand Down

0 comments on commit 69963ae

Please sign in to comment.