From c13e9580cad542e6f85ca8ce1a554da58d07a3b5 Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Fri, 21 Jun 2019 20:13:22 +0530 Subject: [PATCH 1/3] Added support for Common Type Attributes --- src/CGOptions.cpp | 1 + src/CGOptions.h | 4 ++++ src/Probabilities.cpp | 4 ++++ src/Probabilities.h | 4 ++++ src/RandomProgramGenerator.cpp | 11 +++++++++++ src/Type.cpp | 23 ++++++++++++++++++++++- 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/CGOptions.cpp b/src/CGOptions.cpp index 5cb459b56..6dbe02a8a 100644 --- a/src/CGOptions.cpp +++ b/src/CGOptions.cpp @@ -197,6 +197,7 @@ DEFINE_GETTER_SETTER_BOOL(fast_execution); //GCC C Extensions DEFINE_GETTER_SETTER_BOOL(func_attr_flag); +DEFINE_GETTER_SETTER_BOOL(type_attr_flag); void CGOptions::set_default_builtin_kinds() diff --git a/src/CGOptions.h b/src/CGOptions.h index 43465d55b..924823e6f 100644 --- a/src/CGOptions.h +++ b/src/CGOptions.h @@ -460,6 +460,9 @@ class CGOptions { //GCC C Extensions static bool func_attr_flag(void); static bool func_attr_flag(bool p); + + static bool type_attr_flag(void); + static bool type_attr_flag(bool p); private: static bool enabled_builtin_kind(const string &kind); @@ -604,6 +607,7 @@ class CGOptions { //GCC C Extensions static bool func_attr_flag_; + static bool type_attr_flag_; private: CGOptions(void); CGOptions(CGOptions &cgo); diff --git a/src/Probabilities.cpp b/src/Probabilities.cpp index 85116bed8..c2ca221b8 100644 --- a/src/Probabilities.cpp +++ b/src/Probabilities.cpp @@ -504,6 +504,9 @@ Probabilities::set_single_name_maps() //for choosing function attributes set_single_name("func_attr_flag", pFuncAttrProb); + + //for choosing type attributes + set_single_name("type_attr_flag", pTypeAttrProb); } void @@ -546,6 +549,7 @@ Probabilities::initialize_single_probs() //GCC C Extensions m[pFuncAttrProb] = 30; + m[pTypeAttrProb] = 50; if (CGOptions::volatiles()) m[pRegularVolatileProb] = 50; diff --git a/src/Probabilities.h b/src/Probabilities.h index 86014ecca..b9f694eb3 100644 --- a/src/Probabilities.h +++ b/src/Probabilities.h @@ -149,6 +149,7 @@ enum ProbName { //for function attributes pFuncAttrProb, + pTypeAttrProb, }; #define MAX_PROB_NAME ((ProbName)(pStatementProb+1)) @@ -228,6 +229,9 @@ enum ProbName { #define FuncAttrProb \ Probabilities::get_prob(pFuncAttrProb) +#define TypeAttrProb \ + Probabilities::get_prob(pTypeAttrProb) + ////////////////////////////////////////////////// #define UNARY_OPS_PROB_FILTER \ Probabilities::get_prob_filter(pUnaryOpsProb) diff --git a/src/RandomProgramGenerator.cpp b/src/RandomProgramGenerator.cpp index 8f1ccadcb..aac234060 100644 --- a/src/RandomProgramGenerator.cpp +++ b/src/RandomProgramGenerator.cpp @@ -211,6 +211,7 @@ static void print_help() //--------------------------GCC C Extensions-------------------------- cout<< "------------------------------GCC C Extensions------------------------------" << endl << endl; cout << " --function-attributes | --no-func-attributes: enable | disable generate common function attributes (disabled by default)." << endl << endl; + cout << " --type-attributes | --no-type-attributes: enable | disable generate common type attributes (disabled by default)." << endl << endl; } @@ -809,6 +810,16 @@ main(int argc, char **argv) continue; } + if (strcmp (argv[i], "--type-attributes") == 0) { + CGOptions::type_attr_flag(true); + continue; + } + + if (strcmp (argv[i], "--no-type-attributes") == 0) { + CGOptions::type_attr_flag(false); + continue; + } + if (strcmp (argv[i], "--max-array-dim") ==0 ) { unsigned long dim; i++; diff --git a/src/Type.cpp b/src/Type.cpp index 73f7858ad..7b6f97e20 100644 --- a/src/Type.cpp +++ b/src/Type.cpp @@ -54,6 +54,7 @@ #include "Probabilities.h" #include "DepthSpec.h" #include "Enumerator.h" +#include "Attribute.h" using namespace std; @@ -71,7 +72,22 @@ Type *Type::void_type = NULL; static vector AllTypes; static vector derived_types; +AttributeGenerator type_attr_generator; + ////////////////////////////////////////////////////////////////////// + +void +InitializeTypeAttributes() +{ + if(CGOptions::type_attr_flag()){ + type_attr_generator.attributes.push_back(new AlignedAttribute("aligned", TypeAttrProb, 8)); + type_attr_generator.attributes.push_back(new AlignedAttribute("warn_if_not_aligned", TypeAttrProb, 8)); + type_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", TypeAttrProb)); + type_attr_generator.attributes.push_back(new BooleanAttribute("unused", TypeAttrProb)); + type_attr_generator.attributes.push_back(new BooleanAttribute("transparent_union", TypeAttrProb)); + } +} + class NonVoidTypeFilter : public Filter { public: @@ -1218,6 +1234,7 @@ Type::GenerateSimpleTypes(void) void GenerateAllTypes(void) { + InitializeTypeAttributes(); // In the exhaustive mode, we want to generate all type first. // We don't support struct for now if (CGOptions::dfs_exhaustive()) { @@ -1911,7 +1928,11 @@ void OutputStructUnion(Type* type, std::ostream &out) OutputUnionAssignOps(type, out, true); } - out << "};"; + out << "}"; + if(type->eType == eStruct || type->eType == eUnion){ + type_attr_generator.Output(out); + } + out << ";"; really_outputln(out); if (type->packed_) { if (CGOptions::ccomp()) { From 300b4ccfb9ffd47e7f83d854fbb2f0327a4ece52 Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Mon, 12 Aug 2019 19:37:25 +0530 Subject: [PATCH 2/3] Added support for Label Attributes --- src/CGOptions.cpp | 1 + src/CGOptions.h | 4 ++++ src/Probabilities.cpp | 4 ++++ src/Probabilities.h | 5 +++++ src/RandomProgramGenerator.cpp | 11 +++++++++++ src/Statement.cpp | 26 ++++++++++++++++++++++++-- src/Statement.h | 3 +++ 7 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/CGOptions.cpp b/src/CGOptions.cpp index 6dbe02a8a..71a9f8eab 100644 --- a/src/CGOptions.cpp +++ b/src/CGOptions.cpp @@ -198,6 +198,7 @@ DEFINE_GETTER_SETTER_BOOL(fast_execution); //GCC C Extensions DEFINE_GETTER_SETTER_BOOL(func_attr_flag); DEFINE_GETTER_SETTER_BOOL(type_attr_flag); +DEFINE_GETTER_SETTER_BOOL(label_attr_flag); void CGOptions::set_default_builtin_kinds() diff --git a/src/CGOptions.h b/src/CGOptions.h index 924823e6f..4a874fba9 100644 --- a/src/CGOptions.h +++ b/src/CGOptions.h @@ -463,6 +463,9 @@ class CGOptions { static bool type_attr_flag(void); static bool type_attr_flag(bool p); + + static bool label_attr_flag(void); + static bool label_attr_flag(bool p); private: static bool enabled_builtin_kind(const string &kind); @@ -608,6 +611,7 @@ class CGOptions { //GCC C Extensions static bool func_attr_flag_; static bool type_attr_flag_; + static bool label_attr_flag_; private: CGOptions(void); CGOptions(CGOptions &cgo); diff --git a/src/Probabilities.cpp b/src/Probabilities.cpp index c2ca221b8..d9e6661b6 100644 --- a/src/Probabilities.cpp +++ b/src/Probabilities.cpp @@ -507,6 +507,9 @@ Probabilities::set_single_name_maps() //for choosing type attributes set_single_name("type_attr_flag", pTypeAttrProb); + + //for choosing label attributes + set_single_name("label_attr_flag", pLabelAttrProb); } void @@ -550,6 +553,7 @@ Probabilities::initialize_single_probs() //GCC C Extensions m[pFuncAttrProb] = 30; m[pTypeAttrProb] = 50; + m[pLabelAttrProb] = 30; if (CGOptions::volatiles()) m[pRegularVolatileProb] = 50; diff --git a/src/Probabilities.h b/src/Probabilities.h index b9f694eb3..d9e3dc2cb 100644 --- a/src/Probabilities.h +++ b/src/Probabilities.h @@ -150,6 +150,8 @@ enum ProbName { //for function attributes pFuncAttrProb, pTypeAttrProb, + pLabelAttrProb, + }; #define MAX_PROB_NAME ((ProbName)(pStatementProb+1)) @@ -232,6 +234,9 @@ enum ProbName { #define TypeAttrProb \ Probabilities::get_prob(pTypeAttrProb) +#define LabelAttrProb \ + Probabilities::get_prob(pLabelAttrProb) + ////////////////////////////////////////////////// #define UNARY_OPS_PROB_FILTER \ Probabilities::get_prob_filter(pUnaryOpsProb) diff --git a/src/RandomProgramGenerator.cpp b/src/RandomProgramGenerator.cpp index aac234060..37603eee5 100644 --- a/src/RandomProgramGenerator.cpp +++ b/src/RandomProgramGenerator.cpp @@ -212,6 +212,7 @@ static void print_help() cout<< "------------------------------GCC C Extensions------------------------------" << endl << endl; cout << " --function-attributes | --no-func-attributes: enable | disable generate common function attributes (disabled by default)." << endl << endl; cout << " --type-attributes | --no-type-attributes: enable | disable generate common type attributes (disabled by default)." << endl << endl; + cout << " --label-attributes | --no-label-attributes: enable | disable generate common label attributes (disabled by default)." << endl << endl; } @@ -820,6 +821,16 @@ main(int argc, char **argv) continue; } + if (strcmp (argv[i], "--label-attributes") == 0) { + CGOptions::label_attr_flag(true); + continue; + } + + if (strcmp (argv[i], "--no-label-attributes") == 0) { + CGOptions::label_attr_flag(false); + continue; + } + if (strcmp (argv[i], "--max-array-dim") ==0 ) { unsigned long dim; i++; diff --git a/src/Statement.cpp b/src/Statement.cpp index 3afa456ef..e747e5841 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -84,11 +84,24 @@ #include "util.h" #include "StringUtils.h" #include "VariableSelector.h" +#include "Attribute.h" using namespace std; const Statement* Statement::failed_stm; +AttributeGenerator Statement::label_attr_generator; + /////////////////////////////////////////////////////////////////////////////// + +void +InitializeLabelAttributes() +{ + if(CGOptions::label_attr_flag()){ + Statement::label_attr_generator.attributes.push_back(new BooleanAttribute("hot", TypeAttrProb)); + Statement::label_attr_generator.attributes.push_back(new BooleanAttribute("cold", TypeAttrProb)); + } +} + class StatementFilter : public Filter { public: @@ -97,6 +110,8 @@ class StatementFilter : public Filter virtual ~StatementFilter(void); virtual bool filter(int v) const; + + static bool label_attr_generate; private: const CGContext &cg_context_; }; @@ -104,7 +119,10 @@ class StatementFilter : public Filter StatementFilter::StatementFilter(const CGContext &cg_context) : cg_context_(cg_context) { - + if(!label_attr_generate){ + InitializeLabelAttributes(); + label_attr_generate = true; + } } StatementFilter::~StatementFilter(void) @@ -112,6 +130,8 @@ StatementFilter::~StatementFilter(void) } +bool StatementFilter::label_attr_generate = false; + // use a table to define probabilities of different kinds of statements // Must initialize it before use ProbabilityTable *Statement::stmtTable_ = NULL; @@ -951,7 +971,9 @@ Statement::pre_output(std::ostream &out, FactMgr* /* fm */, int indent) const vector gotos; if (find_jump_sources(gotos)) { assert(gotos.size() > 0); - out << gotos[0]->label << ":" << endl; + out << gotos[0]->label << ":"; + label_attr_generator.Output(out); + out << endl; return 1; //for (j=0; joutput_skipped_var_inits(out, indent); diff --git a/src/Statement.h b/src/Statement.h index c5a812aa0..166fb4629 100644 --- a/src/Statement.h +++ b/src/Statement.h @@ -66,6 +66,7 @@ class ProbabilityTable; class StatementGoto; class Variable; class Expression; +class AttributeGenerator; enum eStatementType { @@ -178,6 +179,8 @@ class Statement int get_blk_depth(void) const; + static AttributeGenerator label_attr_generator; + // unique id for each statement int stm_id; Function* func; From 48f4da9eae53907a0ec679f5b49824ed21f7bf4b Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Tue, 13 Aug 2019 19:29:24 +0530 Subject: [PATCH 3/3] Added support for Variable Attributes --- src/CGOptions.cpp | 1 + src/CGOptions.h | 4 ++++ src/Probabilities.cpp | 4 ++++ src/Probabilities.h | 4 ++++ src/RandomProgramGenerator.cpp | 28 ++++++++++++++++++++++++++++ src/Type.cpp | 20 +++++++++++++------- src/Variable.cpp | 26 +++++++++++++++++++++++++- src/Variable.h | 4 ++++ 8 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/CGOptions.cpp b/src/CGOptions.cpp index 71a9f8eab..1c8f9efad 100644 --- a/src/CGOptions.cpp +++ b/src/CGOptions.cpp @@ -199,6 +199,7 @@ DEFINE_GETTER_SETTER_BOOL(fast_execution); DEFINE_GETTER_SETTER_BOOL(func_attr_flag); DEFINE_GETTER_SETTER_BOOL(type_attr_flag); DEFINE_GETTER_SETTER_BOOL(label_attr_flag); +DEFINE_GETTER_SETTER_BOOL(var_attr_flag); void CGOptions::set_default_builtin_kinds() diff --git a/src/CGOptions.h b/src/CGOptions.h index 4a874fba9..f402dd2a6 100644 --- a/src/CGOptions.h +++ b/src/CGOptions.h @@ -466,6 +466,9 @@ class CGOptions { static bool label_attr_flag(void); static bool label_attr_flag(bool p); + + static bool var_attr_flag(void); + static bool var_attr_flag(bool p); private: static bool enabled_builtin_kind(const string &kind); @@ -612,6 +615,7 @@ class CGOptions { static bool func_attr_flag_; static bool type_attr_flag_; static bool label_attr_flag_; + static bool var_attr_flag_; private: CGOptions(void); CGOptions(CGOptions &cgo); diff --git a/src/Probabilities.cpp b/src/Probabilities.cpp index d9e6661b6..866f566a9 100644 --- a/src/Probabilities.cpp +++ b/src/Probabilities.cpp @@ -510,6 +510,9 @@ Probabilities::set_single_name_maps() //for choosing label attributes set_single_name("label_attr_flag", pLabelAttrProb); + + //for choosing variable attributes + set_single_name("var_attr_flag", pVarAttrProb); } void @@ -554,6 +557,7 @@ Probabilities::initialize_single_probs() m[pFuncAttrProb] = 30; m[pTypeAttrProb] = 50; m[pLabelAttrProb] = 30; + m[pVarAttrProb] = 30; if (CGOptions::volatiles()) m[pRegularVolatileProb] = 50; diff --git a/src/Probabilities.h b/src/Probabilities.h index d9e3dc2cb..ec280b29c 100644 --- a/src/Probabilities.h +++ b/src/Probabilities.h @@ -151,6 +151,7 @@ enum ProbName { pFuncAttrProb, pTypeAttrProb, pLabelAttrProb, + pVarAttrProb, }; @@ -237,6 +238,9 @@ enum ProbName { #define LabelAttrProb \ Probabilities::get_prob(pLabelAttrProb) +#define VarAttrProb \ + Probabilities::get_prob(pVarAttrProb) + ////////////////////////////////////////////////// #define UNARY_OPS_PROB_FILTER \ Probabilities::get_prob_filter(pUnaryOpsProb) diff --git a/src/RandomProgramGenerator.cpp b/src/RandomProgramGenerator.cpp index 37603eee5..fac0e9be2 100644 --- a/src/RandomProgramGenerator.cpp +++ b/src/RandomProgramGenerator.cpp @@ -213,6 +213,8 @@ static void print_help() cout << " --function-attributes | --no-func-attributes: enable | disable generate common function attributes (disabled by default)." << endl << endl; cout << " --type-attributes | --no-type-attributes: enable | disable generate common type attributes (disabled by default)." << endl << endl; cout << " --label-attributes | --no-label-attributes: enable | disable generate common label attributes (disabled by default)." << endl << endl; + cout << " --variable-attributes | --no-variable-attributes: enable | disable generate common variable attributes (disabled by default)." << endl << endl; + cout << " --compiler-attributes | --no-compiler-attributes: enable | disable generate function, type, label and variable attributes (disabled by default)." << endl << endl; } @@ -831,6 +833,32 @@ main(int argc, char **argv) continue; } + if (strcmp (argv[i], "--variable-attributes") == 0) { + CGOptions::var_attr_flag(true); + continue; + } + + if (strcmp (argv[i], "--no-variable-attributes") == 0) { + CGOptions::var_attr_flag(false); + continue; + } + + if (strcmp (argv[i], "--compiler-attributes") == 0) { + CGOptions::func_attr_flag(true); + CGOptions::type_attr_flag(true); + CGOptions::label_attr_flag(true); + CGOptions::var_attr_flag(true); + continue; + } + + if (strcmp (argv[i], "--no-compiler-attributes") == 0) { + CGOptions::func_attr_flag(false); + CGOptions::type_attr_flag(false); + CGOptions::label_attr_flag(false); + CGOptions::var_attr_flag(false); + continue; + } + if (strcmp (argv[i], "--max-array-dim") ==0 ) { unsigned long dim; i++; diff --git a/src/Type.cpp b/src/Type.cpp index 7b6f97e20..b5174474d 100644 --- a/src/Type.cpp +++ b/src/Type.cpp @@ -72,7 +72,8 @@ Type *Type::void_type = NULL; static vector AllTypes; static vector derived_types; -AttributeGenerator type_attr_generator; +AttributeGenerator struct_type_attr_generator; +AttributeGenerator union_type_attr_generator; ////////////////////////////////////////////////////////////////////// @@ -80,11 +81,15 @@ void InitializeTypeAttributes() { if(CGOptions::type_attr_flag()){ - type_attr_generator.attributes.push_back(new AlignedAttribute("aligned", TypeAttrProb, 8)); - type_attr_generator.attributes.push_back(new AlignedAttribute("warn_if_not_aligned", TypeAttrProb, 8)); - type_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", TypeAttrProb)); - type_attr_generator.attributes.push_back(new BooleanAttribute("unused", TypeAttrProb)); - type_attr_generator.attributes.push_back(new BooleanAttribute("transparent_union", TypeAttrProb)); + struct_type_attr_generator.attributes.push_back(new AlignedAttribute("aligned", TypeAttrProb, 8)); + struct_type_attr_generator.attributes.push_back(new AlignedAttribute("warn_if_not_aligned", TypeAttrProb, 8)); + struct_type_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", TypeAttrProb)); + struct_type_attr_generator.attributes.push_back(new BooleanAttribute("unused", TypeAttrProb)); + union_type_attr_generator.attributes.push_back(new AlignedAttribute("aligned", TypeAttrProb, 8)); + union_type_attr_generator.attributes.push_back(new AlignedAttribute("warn_if_not_aligned", TypeAttrProb, 8)); + union_type_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", TypeAttrProb)); + union_type_attr_generator.attributes.push_back(new BooleanAttribute("unused", TypeAttrProb)); + union_type_attr_generator.attributes.push_back(new BooleanAttribute("transparent_union", TypeAttrProb)); } } @@ -1930,7 +1935,8 @@ void OutputStructUnion(Type* type, std::ostream &out) out << "}"; if(type->eType == eStruct || type->eType == eUnion){ - type_attr_generator.Output(out); + struct_type_attr_generator.Output(out); + union_type_attr_generator.Output(out); } out << ";"; really_outputln(out); diff --git a/src/Variable.cpp b/src/Variable.cpp index 0295b27ac..49108a6bf 100644 --- a/src/Variable.cpp +++ b/src/Variable.cpp @@ -69,6 +69,7 @@ #include "Error.h" #include "ArrayVariable.h" #include "StringUtils.h" +#include "Attribute.h" using namespace std; @@ -77,8 +78,25 @@ unsigned long Variable::ctrl_vars_count; const char Variable::sink_var_name[] = "csmith_sink_"; +bool Variable::var_attr_generate = false; +AttributeGenerator Variable::var_attr_generator; + ////////////////////////////////////////////////////////////////////////////// +void +InitializeVariableAttributes() +{ + if(CGOptions::var_attr_flag()){ + Variable::var_attr_generator.attributes.push_back(new MultiChoiceAttribute("visibility", VarAttrProb, {"default", "hidden", "protected", "internal"})); + Variable::var_attr_generator.attributes.push_back(new AlignedAttribute("aligned", VarAttrProb, 8)); + Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("common", VarAttrProb)); + Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("uncommon", VarAttrProb)); + Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", VarAttrProb)); + Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("unused", VarAttrProb)); + Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("used", VarAttrProb)); + } +} + int find_variable_in_set(const vector& set, const Variable* v) { size_t i; @@ -409,6 +427,10 @@ Variable::CreateVariable(const std::string &name, const Type *type, Variable * Variable::CreateVariable(const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer) { + if(!Variable::var_attr_generate){ + InitializeVariableAttributes(); + Variable::var_attr_generate = true; + } assert(type); if (type->eType == eSimple) assert(type->simple_type != eVoid); @@ -672,7 +694,9 @@ Variable::OutputDef(std::ostream &out, int indent) const out << "static "; } output_qualified_type(out); - out << get_actual_name() << " = "; + out << get_actual_name(); + var_attr_generator.Output(out); + out << " = "; assert(init); init->Output(out); out << ";"; diff --git a/src/Variable.h b/src/Variable.h index 40a68d5dc..185b154b8 100644 --- a/src/Variable.h +++ b/src/Variable.h @@ -58,6 +58,7 @@ class Function; class Block; class Lhs; class ArrayVariable; +class AttributeGenerator; class Variable { @@ -160,6 +161,9 @@ class Variable static const char sink_var_name[]; + static bool var_attr_generate; + static AttributeGenerator var_attr_generator; + private: Variable(const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer); Variable(const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer, const Variable* isFieldVarOf, bool isArray);