Skip to content

Commit

Permalink
Add type restrictions to csmith
Browse files Browse the repository at this point in the history
According to the type config setting, add the type using
restrictions to methods, such as: choose_var, match etc.

Signed-off-by: ZhuJunhua1104 tom.Zhu@huawei.com
  • Loading branch information
ZhuJunhua1104 committed Sep 8, 2015
1 parent 62386d5 commit a6b565d
Show file tree
Hide file tree
Showing 22 changed files with 245 additions and 196 deletions.
43 changes: 25 additions & 18 deletions src/AggregateType.cpp
Expand Up @@ -50,6 +50,7 @@
#include "util.h"
#include "Bookkeeper.h"
#include "Probabilities.h"
#include "TypeConfig.h"

using namespace std;

Expand Down Expand Up @@ -156,7 +157,7 @@ AggregateType::make_full_bitfields_struct_fields(size_t field_cnt, vector<const
for (size_t i=0; i<field_cnt; i++) {
bool is_non_bitfield = rnd_flipcoin(ScalarFieldInFullBitFieldsProb);
if (is_non_bitfield) {
make_one_struct_field(random_fields, qualifiers, fields_length);
make_one_struct_field(random_fields, qualifiers, fields_length, TypeConfig::get_filter_for_request(asStructMember));
}
else {
make_one_bitfield(random_fields, qualifiers, fields_length);
Expand All @@ -167,27 +168,32 @@ AggregateType::make_full_bitfields_struct_fields(size_t field_cnt, vector<const
void
AggregateType::make_one_struct_field(vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length)
vector<int> &fields_length,
Filter * additional_filter)
{
VectorFilter f;
VectorFilter * f = NULL;
if (additional_filter)
f = dynamic_cast<VectorFilter *>(additional_filter);
if (!f)
f = new VectorFilter();
unsigned int type_index = 0;
Type *typ = 0;
for( type_index = 0; type_index < AllTypes.size(); type_index++) {
typ = AllTypes[type_index];
assert(typ);
if (typ->eType == eSimple) {
vector<Type *>::iterator iter;
for(iter = AllTypes.begin(); iter != AllTypes.end(); ++iter)
{
if ((*iter)->eType == eSimple) {
Filter *filter = SIMPLE_TYPES_PROB_FILTER;
if(filter->filter(typ->simple_type))
f.add(type_index);
if(filter->filter((*iter)->simple_type))
f->add((*iter)->type_index);
}
else if ((typ->eType == eStruct) && (!CGOptions::return_structs())) {
f.add(type_index);
else if (((*iter)->eType == eStruct) && (!CGOptions::return_structs())) {
f->add((*iter)->type_index);
}
if (typ->get_struct_depth() >= CGOptions::max_nested_struct_level()) {
f.add(type_index);
if ((*iter)->get_struct_depth() >= CGOptions::max_nested_struct_level()) {
f->add((*iter)->type_index);
}
}
type_index = rnd_upto(AllTypes.size(), &f);
type_index = rnd_upto(AllTypes.size(), f);
delete f;
const Type* type = AllTypes[type_index];
random_fields.push_back(type);
CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb);
Expand All @@ -208,7 +214,7 @@ AggregateType::make_one_union_field(vector<const Type*> &fields, vector<CVQualif
// filter out struct types containing bit-fields. Their layout is implementation
// defined, we don't want to mess with them in unions for now
for (i=0; i<AllTypes.size(); i++) {
if (!AllTypes[i]->has_bitfields()) {
if (!AllTypes[i]->has_bitfields() && ! TypeConfig::check_exclude_by_request( AllTypes[i], asUnionMember)) {
ok_types.push_back(AllTypes[i]);
}
}
Expand Down Expand Up @@ -262,7 +268,7 @@ AggregateType::make_normal_struct_fields(size_t field_cnt, vector<const Type*> &
make_one_bitfield(random_fields, qualifiers, fields_length);
}
else {
make_one_struct_field(random_fields, qualifiers, fields_length);
make_one_struct_field(random_fields, qualifiers, fields_length, TypeConfig::get_filter_for_request(asStructMember));
}
}
}
Expand Down Expand Up @@ -511,7 +517,8 @@ AggregateType::get_int_subfield_names(string prefix, vector<string>& names, cons
for (i=0; i<fields.size(); i++) {
if (is_unamed_padding(i)) continue; // skip 0 length bitfields
// skip excluded fields
if (std::find(excluded_fields.begin(), excluded_fields.end(), j) != excluded_fields.end()) {
if (std::find(excluded_fields.begin(), excluded_fields.end(), j) != excluded_fields.end() ||
is_bitfield(i)) {
j++;
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/AggregateType.h
Expand Up @@ -113,7 +113,8 @@ class AggregateType : public Type

static void make_one_struct_field(vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length);
vector<int> &fields_length,
Filter * additional_filter = NULL);

static void make_one_union_field(vector<const Type*> &fields, vector<CVQualifiers> &qfers, vector<int> &lens);

Expand Down
3 changes: 2 additions & 1 deletion src/ArrayVariable.cpp 100644 → 100755
Expand Up @@ -57,6 +57,7 @@
#include "SafeOpFlags.h"
#include "OutputMgr.h"
#include "StringUtils.h"
#include "TypeConfig.h"

using namespace std;

Expand Down Expand Up @@ -504,7 +505,7 @@ ArrayVariable::OutputDef(std::ostream &out, int indent) const
{
if (collective == 0) {
output_tab(out, indent);
if (!no_loop_initializer() ) {
if (!no_loop_initializer() || TypeConfig::check_exclude_by_request(type, asGlobalInit) && is_global() ) {
// don't print definition for array, rather use a loop initializer
OutputDecl(out);
out << ";";
Expand Down
79 changes: 10 additions & 69 deletions src/Constant.cpp
Expand Up @@ -96,65 +96,19 @@ Constant::clone() const

// --------------------------------------------------------------
static string
GenerateRandomCharConstant(void)
GenerateRandomConstant(int size)
{
string ch;
string suffix = "L";
if (size > 4)
suffix = "LL";
if (CGOptions::ccomp() || !CGOptions::longlong())
ch = string("0x") + RandomHexDigits(2);
ch = string("0x") + RandomHexDigits(2 * size);
else
ch = string("0x") + RandomHexDigits(2) + "L";
ch = string("0x") + RandomHexDigits(2 * size) + suffix;
return ch;
}

// --------------------------------------------------------------
static string
GenerateRandomIntConstant(void)
{
string val;
// Int constant - Max 8 Hex digits on 32-bit platforms
if (CGOptions::ccomp() || !CGOptions::longlong())
val = "0x" + RandomHexDigits( 8 );
else
val = "0x" + RandomHexDigits( 8 ) + "L";

return val;
}

// --------------------------------------------------------------
static string
GenerateRandomShortConstant(void)
{
string val;
// Short constant - Max 4 Hex digits on 32-bit platforms
if (CGOptions::ccomp() || !CGOptions::longlong())
val = "0x" + RandomHexDigits( 4 );
else
val = "0x" + RandomHexDigits( 4 ) + "L";

return val;
}

// --------------------------------------------------------------
static string
GenerateRandomLongConstant(void)
{
string val;
// Long constant - Max 8 Hex digits on 32-bit platforms
if (!CGOptions::longlong())
val = "0x" + RandomHexDigits( 8 );
else
val = "0x" + RandomHexDigits( 8 ) + "L";
return val;
}

// --------------------------------------------------------------
static string
GenerateRandomLongLongConstant(void)
{
// Long constant - Max 8 Hex digits on 32-bit platforms
string val = "0x" + RandomHexDigits( 16 ) + "LL";
return val;
}

// --------------------------------------------------------------
#if 0
Expand Down Expand Up @@ -358,23 +312,10 @@ GenerateRandomConstant(const Type* type)
v = oss.str() + (type->is_signed() ? "L" : "UL");
}
} else {
switch (st) {
case eVoid: v = "/* void */"; break;
case eChar: v = GenerateRandomCharConstant(); break;
case eInt: v = GenerateRandomIntConstant(); break;
case eShort: v = GenerateRandomShortConstant(); break;
case eLong: v = GenerateRandomLongConstant(); break;
case eLongLong: v = GenerateRandomLongLongConstant(); break;
case eUChar: v = GenerateRandomCharConstant(); break;
case eUInt: v = GenerateRandomIntConstant(); break;
case eUShort: v = GenerateRandomShortConstant(); break;
case eULong: v = GenerateRandomLongConstant(); break;
case eULongLong: v = GenerateRandomLongLongConstant(); break;
case eFloat: v = GenerateRandomFloatHexConstant(); break;
// case eDouble: v = GenerateRandomFloatConstant(); break;
default:
assert(0 && "Unsupported type!");
}
if(st == eVoid)
v = "/* void */";
else
v = GenerateRandomConstant(Type::get_simple_type(st).SizeInBytes());
}
} else {
assert(0); // no support for types other than integers and structs for now
Expand Down
8 changes: 6 additions & 2 deletions src/Expression.cpp
Expand Up @@ -59,6 +59,7 @@
#include "random.h"
#include "CVQualifiers.h"
#include "Parameter.h"
#include "TypeConfig.h"

int eid = 0;

Expand Down Expand Up @@ -171,7 +172,9 @@ Expression::make_random(CGContext &cg_context, const Type* type, const CVQualifi
VectorFilter filter(&Expression::exprTable_);
if (no_func ||
(!CGOptions::return_structs() && type->eType == eStruct) ||
(!CGOptions::return_unions() && type->eType == eUnion)) {
(!CGOptions::return_unions() && type->eType == eUnion) ||
TypeConfig::check_exclude_by_request(type, asReturn))
{
filter.add(eFunction);
}
// struct constants can't be subexpressions (union constant can't either?)
Expand Down Expand Up @@ -266,7 +269,8 @@ Expression::make_random_param_value(CGContext &cg_context, const Parameter* para
VectorFilter filter(&Expression::paramTable_);
filter.add(eConstant); // don't call functions with constant parameters because it is not interesting
if ((!CGOptions::return_structs() && type->eType == eStruct) ||
(!CGOptions::return_unions() && type->eType == eUnion)) {
(!CGOptions::return_unions() && type->eType == eUnion) ||
TypeConfig::check_exclude_by_request(type, asReturn)) {
filter.add(eFunction);
}
if (type->is_const_struct_union()) {
Expand Down
5 changes: 4 additions & 1 deletion src/ExpressionFuncall.cpp 100644 → 100755
Expand Up @@ -44,7 +44,7 @@
#include "StringUtils.h"
#include "Block.h"
#include "random.h"

#include "TypeConfig.h"
///////////////////////////////////////////////////////////////////////////////

/*
Expand All @@ -70,6 +70,9 @@ ExpressionFuncall::make_random(CGContext &cg_context, const Type* type, const CV
// unary/binary "functions" produce scalar types only
if (type && (type->eType != eSimple || type->simple_type == eVoid))
std_func = false;
if (type && (TypeConfig::check_exclude_by_request(type, asBinaryExprRv) ||
TypeConfig::check_exclude_by_request(type, asUnaryExprRv)))
std_func = false;

Effect effect_accum = cg_context.get_accum_effect();
Effect effect_stm = cg_context.get_effect_stm();
Expand Down
5 changes: 3 additions & 2 deletions src/Function.cpp
Expand Up @@ -65,6 +65,7 @@
#include "OutputMgr.h"
#include "Parameter.h"
#include "BuiltinConfig.h"
#include "TypeConfig.h"

using namespace std;

Expand Down Expand Up @@ -243,7 +244,7 @@ static const Type*
RandomReturnType(void)
{
const Type* t = 0;
t = Type::choose_random();
t = Type::choose_random(TypeConfig::get_filter_for_request(asReturn));
return t;
}

Expand Down Expand Up @@ -770,7 +771,7 @@ GenerateFunctions(void)
FactMgr::add_interested_facts(CGOptions::interested_facts());
if (CGOptions::builtins()) {
Function::initialize_builtin_functions();
BuiltinConfig::build_builtin_list_from_xml(CGOptions::init_builtin_config_filepath()); // just for test
BuiltinConfig::build_builtin_list_from_xml(CGOptions::init_config_filepath()); // just for test
}
// -----------------
// Create a basic first function, then generate a random graph from there.
Expand Down
8 changes: 6 additions & 2 deletions src/FunctionInvocation.cpp
Expand Up @@ -66,6 +66,7 @@
#include "CompatibleChecker.h"
#include "Constant.h"
#include "CGOptions.h"
#include "TypeConfig.h"

using namespace std;

Expand Down Expand Up @@ -142,7 +143,7 @@ FunctionInvocation::make_random_unary(CGContext &cg_context, const Type* type)
eUnaryOps op;
do {
op = (eUnaryOps)(rnd_upto(MAX_UNARY_OP, UNARY_OPS_PROB_FILTER));
} while (type->is_float() && !UnaryOpWorksForFloat(op));
} while (TypeConfig::check_exclude_by_unaryop(type, op));

SafeOpFlags *flags = NULL;
flags = SafeOpFlags::make_random_unary(type, NULL, op);
Expand All @@ -152,6 +153,7 @@ FunctionInvocation::make_random_unary(CGContext &cg_context, const Type* type)
FunctionInvocation *fi = FunctionInvocationUnary::CreateFunctionInvocationUnary(cg_context, op, flags);

Expression *operand = Expression::make_random(cg_context, type);
operand->check_and_set_cast(type);

fi->param_value.push_back(operand);
return fi;
Expand All @@ -170,7 +172,7 @@ FunctionInvocation::make_random_binary(CGContext &cg_context, const Type* type)
eBinaryOps op;
do {
op = (eBinaryOps)(rnd_upto(MAX_BINARY_OP, BINARY_OPS_PROB_FILTER));
} while (type->is_float() && !BinaryOpWorksForFloat(op));
} while (TypeConfig::check_exclude_by_binaryop(type, op));
assert(type);
SafeOpFlags *flags = SafeOpFlags::make_random_binary(type, NULL, NULL, sOpBinary, op);
assert(flags);
Expand Down Expand Up @@ -258,6 +260,8 @@ FunctionInvocation::make_random_binary(CGContext &cg_context, const Type* type)
}
// TODO: fix `rhs' for eLShift and eRShift and ...
// Currently, the "fix" is handled in `FunctionInvocationBinary::Output'.
lhs->check_and_set_cast(lhs_type);
rhs->check_and_set_cast(rhs_type);
fi->param_value.push_back(lhs);
fi->param_value.push_back(rhs);
return fi;
Expand Down

0 comments on commit a6b565d

Please sign in to comment.