Skip to content

Commit

Permalink
Merge pull request #82 from shubhamnarlawar77/csmith-gcc-c-extension
Browse files Browse the repository at this point in the history
Added support for Section Function Attribute and Alias Function Attribute
  • Loading branch information
jxyang committed Jan 9, 2020
2 parents 900729a + 8143487 commit 9797ed2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 47 deletions.
17 changes: 17 additions & 0 deletions src/Attribute.cpp
Expand Up @@ -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 "";
}
8 changes: 8 additions & 0 deletions src/Attribute.h
Expand Up @@ -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
{
Expand Down
91 changes: 45 additions & 46 deletions src/Function.cpp
Expand Up @@ -80,62 +80,24 @@ static bool param_first=true; // Flag to track output of commas
static int builtin_functions_cnt;

static std::vector<string> common_func_attributes;
static std::vector<string> visibility_choices;
static std::vector<string> sanitize_choices;

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");

//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");
}

void
Function::InitializeAttributes()
{
if(CGOptions::func_attr_flag()){
InitializeAttributesChoices();
vector<string> 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<string>::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("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));
}
}

Expand Down Expand Up @@ -485,6 +447,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);
Expand Down Expand Up @@ -521,6 +484,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);
Expand Down Expand Up @@ -607,6 +571,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) << "\")))";
}

/*
*
*/
Expand All @@ -621,6 +597,14 @@ Function::OutputForwardDecl(std::ostream &out)
outputln(out);
}

void
Function::OutputForwardDeclAlias(std::ostream &out)
{
OutputHeaderAlias(out);
out << ";";
outputln(out);
}

/*
*
*/
Expand Down Expand Up @@ -897,6 +881,13 @@ OutputForwardDecl(Function *func, std::ostream *pOut)
return 0;
}

static int
OutputForwardDeclAlias(Function *func, std::ostream *pOut)
{
func->OutputForwardDeclAlias(*pOut);
return 0;
}

/*
*
*/
Expand All @@ -918,6 +909,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));
}
}

/*
Expand Down
8 changes: 8 additions & 0 deletions src/Function.h
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/FunctionInvocationUser.cpp
Expand Up @@ -441,7 +441,10 @@ OutputExpressionVector(const vector<const Expression*> &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 << ")";
}
Expand Down

0 comments on commit 9797ed2

Please sign in to comment.