From 8b3ce501d6cac286f3fbaf246d48596b001fda26 Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Wed, 12 Jun 2019 15:54:51 +0530 Subject: [PATCH 1/4] Added support for Section Function Attribute --- src/Attribute.cpp | 17 +++++++++++++++++ src/Attribute.h | 8 ++++++++ src/Function.cpp | 1 + 3 files changed, 26 insertions(+) diff --git a/src/Attribute.cpp b/src/Attribute.cpp index 342108b7b..0628b9e8d 100644 --- a/src/Attribute.cpp +++ b/src/Attribute.cpp @@ -80,3 +80,20 @@ AlignedAttribute::make_random() else return ""; } + +SectionAttribute::SectionAttribute(string name, int prob) + : Attribute(name, prob) +{ +} + +string +SectionAttribute::make_random() +{ + if(rnd_flipcoin(prob)){ + ostringstream oss; + oss << rnd_upto(10); + return name + "(\"usersection" + oss.str() + "\")"; + } + else + return ""; +} diff --git a/src/Attribute.h b/src/Attribute.h index 804c9a4bb..0b24c6049 100644 --- a/src/Attribute.h +++ b/src/Attribute.h @@ -49,6 +49,14 @@ class AlignedAttribute : public Attribute string make_random(); }; +//Generates usersections to put functions in different sections +class SectionAttribute : public Attribute +{ +public: + SectionAttribute(string, int); + string make_random(); +}; + //Generates function and types attributes class AttributeGenerator { diff --git a/src/Function.cpp b/src/Function.cpp index cb150f33f..d5a10423e 100644 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -136,6 +136,7 @@ Function::InitializeAttributes() func_attr_generator.attributes.push_back(new MultiChoiceAttribute("visibility", FuncAttrProb, visibility_choices)); func_attr_generator.attributes.push_back(new MultiChoiceAttribute("no_sanitize", FuncAttrProb, sanitize_choices)); func_attr_generator.attributes.push_back(new AlignedAttribute("aligned", FuncAttrProb, 16)); + func_attr_generator.attributes.push_back(new SectionAttribute("section", FuncAttrProb)); } } From d35bac20239d97ee2c931cadfcebc04ed408aca6 Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Thu, 13 Jun 2019 12:29:14 +0530 Subject: [PATCH 2/4] Added support for Alias Function Attribute --- src/Function.cpp | 37 ++++++++++++++++++++++++++++++++++ src/Function.h | 8 ++++++++ src/FunctionInvocationUser.cpp | 5 ++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Function.cpp b/src/Function.cpp index d5a10423e..55bee2106 100644 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -486,6 +486,7 @@ Function::make_random_signature(const CGContext& cg_context, const Type* type, c // dummy variable representing return variable, we don't care about the type, so use 0 string rvname = f->name + "_" + "rv"; + f->alias_name = f->name + "_alias"; CVQualifiers ret_qfer = qfer==0 ? CVQualifiers::random_qualifiers(type, Effect::READ, cg_context, true) : qfer->random_qualifiers(true, Effect::READ, cg_context); ERROR_GUARD(NULL); @@ -522,6 +523,7 @@ Function::make_first(void) Function *f = new Function(RandomFunctionName(), ty); // dummy variable representing return variable, we don't care about the type, so use 0 string rvname = f->name + "_" + "rv"; + f->alias_name = f->name + "_alias"; CVQualifiers ret_qfer = CVQualifiers::random_qualifiers(ty); ERROR_GUARD(NULL); f->rv = Variable::CreateVariable(rvname, ty, NULL, &ret_qfer); @@ -608,6 +610,18 @@ Function::OutputHeader(std::ostream &out) out << ")"; } +void +Function::OutputHeaderAlias(std::ostream &out) +{ + if (CGOptions::force_globals_static()) { + out << "static "; + } + rv->qfer.output_qualified_type(return_type, out); + out << " " << get_prefixed_name(alias_name) << "("; + OutputFormalParamList( out ); + out << ") __attribute__((alias(\"" << get_prefixed_name(name) << "\")))"; +} + /* * */ @@ -622,6 +636,14 @@ Function::OutputForwardDecl(std::ostream &out) outputln(out); } +void +Function::OutputForwardDeclAlias(std::ostream &out) +{ + OutputHeaderAlias(out); + out << ";"; + outputln(out); +} + /* * */ @@ -898,6 +920,13 @@ OutputForwardDecl(Function *func, std::ostream *pOut) return 0; } +static int +OutputForwardDeclAlias(Function *func, std::ostream *pOut) +{ + func->OutputForwardDeclAlias(*pOut); + return 0; +} + /* * */ @@ -919,6 +948,14 @@ OutputForwardDeclarations(std::ostream &out) output_comment_line(out, "--- FORWARD DECLARATIONS ---"); for_each(FuncList.begin(), FuncList.end(), std::bind2nd(std::ptr_fun(OutputForwardDecl), &out)); + + if(CGOptions::func_attr_flag()){ + outputln(out); + outputln(out); + output_comment_line(out, "--- FORWARD ALIAS DECLARATIONS ---"); + for_each(FuncList.begin(), FuncList.end(), + std::bind2nd(std::ptr_fun(OutputForwardDeclAlias), &out)); + } } /* diff --git a/src/Function.h b/src/Function.h index e28c46cf3..d08ba9840 100644 --- a/src/Function.h +++ b/src/Function.h @@ -123,12 +123,20 @@ class Function int visited_cnt; Effect accum_eff_context; void InitializeAttributes(); + + //GCC C Extensions + bool func_attr_inline; + void GenerateAttributes(); + std::string alias_name; + void OutputForwardDeclAlias(std::ostream &); + private: static int deleteFunction(Function* func); Function(const std::string &name, const Type *return_type); Function(const std::string &name, const Type *return_type, bool is_builtin); void OutputHeader(std::ostream &); + void OutputHeaderAlias(std::ostream &); void OutputFormalParamList(std::ostream &); void GenerateBody(const CGContext& prev_context); void make_return_const(); diff --git a/src/FunctionInvocationUser.cpp b/src/FunctionInvocationUser.cpp index d52b8bd3e..0b417595e 100644 --- a/src/FunctionInvocationUser.cpp +++ b/src/FunctionInvocationUser.cpp @@ -441,7 +441,10 @@ OutputExpressionVector(const vector &var, std::ostream &out) void FunctionInvocationUser::Output(std::ostream &out) const { - out << func->name << "("; + if(CGOptions::func_attr_flag() && rnd_flipcoin(FuncAttrProb)) + out << func->alias_name << "("; + else + out << func->name << "("; OutputExpressionVector(param_value, out); out << ")"; } From ad225122c268784e99030f2c08d49f1b0de19af7 Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Mon, 17 Jun 2019 14:18:36 +0530 Subject: [PATCH 3/4] Added support for Noclone and Optimize Function Attribute --- src/Function.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Function.cpp b/src/Function.cpp index 55bee2106..788e38b4b 100644 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -82,6 +82,7 @@ static int builtin_functions_cnt; static std::vector common_func_attributes; static std::vector visibility_choices; static std::vector sanitize_choices; +static std::vector optimize_options; void InitializeAttributesChoices() @@ -107,6 +108,7 @@ InitializeAttributesChoices() common_func_attributes.push_back("noinline"); common_func_attributes.push_back("noplt"); common_func_attributes.push_back("stack_protect"); + common_func_attributes.push_back("noclone"); //Pushing visibility choices visibility_choices.push_back("default"); @@ -122,6 +124,15 @@ InitializeAttributesChoices() sanitize_choices.push_back("pointer-compare"); sanitize_choices.push_back("pointer-subtract"); sanitize_choices.push_back("leak"); + + //pushing optimization options + optimize_options.push_back("-O0"); + optimize_options.push_back("-O1"); + optimize_options.push_back("-O2"); + optimize_options.push_back("-O3"); + optimize_options.push_back("-Os"); + optimize_options.push_back("-Ofast"); + optimize_options.push_back("-Og"); } void @@ -135,6 +146,7 @@ Function::InitializeAttributes() func_attr_generator.attributes.push_back(new MultiChoiceAttribute("visibility", FuncAttrProb, visibility_choices)); func_attr_generator.attributes.push_back(new MultiChoiceAttribute("no_sanitize", FuncAttrProb, sanitize_choices)); + func_attr_generator.attributes.push_back(new MultiChoiceAttribute("optimize", FuncAttrProb, optimize_options)); func_attr_generator.attributes.push_back(new AlignedAttribute("aligned", FuncAttrProb, 16)); func_attr_generator.attributes.push_back(new SectionAttribute("section", FuncAttrProb)); } From 814348750d44c61c7b6fa64edd005726e7ae2aa8 Mon Sep 17 00:00:00 2001 From: shubhamnarlawar77 Date: Mon, 6 Jan 2020 20:28:59 +0530 Subject: [PATCH 4/4] Initialized function attributes with string initializers --- src/Function.cpp | 65 ++++++------------------------------------------ 1 file changed, 7 insertions(+), 58 deletions(-) diff --git a/src/Function.cpp b/src/Function.cpp index 788e38b4b..9905ef03a 100644 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -80,73 +80,22 @@ static bool param_first=true; // Flag to track output of commas static int builtin_functions_cnt; static std::vector common_func_attributes; -static std::vector visibility_choices; -static std::vector sanitize_choices; -static std::vector optimize_options; - -void -InitializeAttributesChoices() -{ - //Pushing common function attributes - common_func_attributes.push_back("artificial"); - common_func_attributes.push_back("flatten"); - common_func_attributes.push_back("no_reorder"); - common_func_attributes.push_back("hot"); - common_func_attributes.push_back("cold"); - common_func_attributes.push_back("noipa"); - common_func_attributes.push_back("used"); - common_func_attributes.push_back("unused"); - common_func_attributes.push_back("nothrow"); - common_func_attributes.push_back("deprecated"); - common_func_attributes.push_back("no_icf"); - common_func_attributes.push_back("no_profile_instrument_function"); - common_func_attributes.push_back("no_instrument_function"); - common_func_attributes.push_back("no_sanitize_address"); - common_func_attributes.push_back("no_sanitize_thread"); - common_func_attributes.push_back("no_sanitize_undefined"); - common_func_attributes.push_back("no_split_stack"); - common_func_attributes.push_back("noinline"); - common_func_attributes.push_back("noplt"); - common_func_attributes.push_back("stack_protect"); - common_func_attributes.push_back("noclone"); - - //Pushing visibility choices - visibility_choices.push_back("default"); - visibility_choices.push_back("hidden"); - visibility_choices.push_back("protected"); - visibility_choices.push_back("internal"); - - //Pushing sanitize choices - sanitize_choices.push_back("address"); - sanitize_choices.push_back("thread"); - sanitize_choices.push_back("undefined"); - sanitize_choices.push_back("kernel-address"); - sanitize_choices.push_back("pointer-compare"); - sanitize_choices.push_back("pointer-subtract"); - sanitize_choices.push_back("leak"); - - //pushing optimization options - optimize_options.push_back("-O0"); - optimize_options.push_back("-O1"); - optimize_options.push_back("-O2"); - optimize_options.push_back("-O3"); - optimize_options.push_back("-Os"); - optimize_options.push_back("-Ofast"); - optimize_options.push_back("-Og"); -} void Function::InitializeAttributes() { if(CGOptions::func_attr_flag()){ - InitializeAttributesChoices(); + vector common_func_attributes = {"artificial", "flatten", "no_reorder", "hot", "cold", "noipa", "used", "unused", \ + "nothrow", "deprecated", "no_icf", "no_profile_instrument_function", "noclone", \ + "no_instrument_function", "no_sanitize_address", "no_sanitize_thread", \ + "no_sanitize_undefined", "no_split_stack", "noinline", "noplt", "stack_protect"}; vector::iterator itr; for(itr = common_func_attributes.begin(); itr < common_func_attributes.end(); itr++) func_attr_generator.attributes.push_back(new BooleanAttribute(*itr, FuncAttrProb)); - func_attr_generator.attributes.push_back(new MultiChoiceAttribute("visibility", FuncAttrProb, visibility_choices)); - func_attr_generator.attributes.push_back(new MultiChoiceAttribute("no_sanitize", FuncAttrProb, sanitize_choices)); - func_attr_generator.attributes.push_back(new MultiChoiceAttribute("optimize", FuncAttrProb, optimize_options)); + func_attr_generator.attributes.push_back(new MultiChoiceAttribute("visibility", FuncAttrProb, {"default", "hidden", "protected", "internal"})); + func_attr_generator.attributes.push_back(new MultiChoiceAttribute("no_sanitize", FuncAttrProb, {"address", "thread", "undefined", "kernel-address", "pointer-compare", "pointer-subtract", "leak"})); + func_attr_generator.attributes.push_back(new MultiChoiceAttribute("optimize", FuncAttrProb, {"-O0", "-O1", "-O2", "-O3", "-Os", "-Ofast", "-Og"})); func_attr_generator.attributes.push_back(new AlignedAttribute("aligned", FuncAttrProb, 16)); func_attr_generator.attributes.push_back(new SectionAttribute("section", FuncAttrProb)); }