Skip to content

Commit

Permalink
get rid of macro CSMITH_BITFIELD
Browse files Browse the repository at this point in the history
instead collect some platform specific information such as integer size before the random generation. Users can control these information through file "platform.info" in the working directory.
  • Loading branch information
Xuejun Yang committed Aug 19, 2011
1 parent 9a88a2f commit 19e0cab
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 30 deletions.
10 changes: 10 additions & 0 deletions src/Bookkeeper.cpp
Expand Up @@ -86,6 +86,8 @@ int Bookkeeper::forward_jump_cnt = 0;
int Bookkeeper::backward_jump_cnt = 0;
int Bookkeeper::use_new_var_cnt = 0;
int Bookkeeper::use_old_var_cnt = 0;
bool Bookkeeper::rely_on_int_size = false;
bool Bookkeeper::rely_on_ptr_size = false;

/*
*
Expand Down Expand Up @@ -190,6 +192,14 @@ Bookkeeper::output_statistics(std::ostream &out)
output_stmts_statistics(out);
out << endl;
output_var_freshness(out);
if (rely_on_int_size) {
out << "FYI: the random generator makes assumptions about the integer size. See ";
out << PLATFORM_CONFIG_FILE << " for more details." << endl;
}
if (rely_on_ptr_size) {
out << "FYI: the random generator makes assumptions about the pointer size. See ";
out << PLATFORM_CONFIG_FILE << " for more details." << endl;
}
}

void
Expand Down
3 changes: 3 additions & 0 deletions src/Bookkeeper.h
Expand Up @@ -140,6 +140,9 @@ class Bookkeeper

static int use_new_var_cnt;
static int use_old_var_cnt;

static bool rely_on_int_size;
static bool rely_on_ptr_size;
};

void incr_counter(std::vector<int>& counters, int index);
Expand Down
73 changes: 60 additions & 13 deletions src/CGOptions.cpp
Expand Up @@ -34,6 +34,7 @@
#include <assert.h>
#include "Fact.h"
#include "DefaultOutputMgr.h"
#include "Bookkeeper.h"
#include "CompatibleChecker.h"
#include "PartialExpander.h"
#include "DeltaMonitor.h"
Expand All @@ -44,6 +45,8 @@
using namespace std;
Reducer* CGOptions::reducer_ = NULL;
vector<int> CGOptions::safe_math_wrapper_ids_;
int CGOptions::int_size_ = 0;
int CGOptions::pointer_size_ = 0;

/*
*
Expand Down Expand Up @@ -114,7 +117,6 @@ DEFINE_GETTER_SETTER_BOOL(coverage_test)
DEFINE_GETTER_SETTER_INT(coverage_test_size)
DEFINE_GETTER_SETTER_BOOL(packed_struct)
DEFINE_GETTER_SETTER_BOOL(bitfields)
DEFINE_GETTER_SETTER_INT(bitfields_length)
DEFINE_GETTER_SETTER_BOOL(prefix_name)
DEFINE_GETTER_SETTER_BOOL(sequence_name_prefix)
DEFINE_GETTER_SETTER_BOOL(compatible_check)
Expand Down Expand Up @@ -167,12 +169,11 @@ DEFINE_GETTER_SETTER_BOOL(union_read_type_sensitive);
DEFINE_GETTER_SETTER_BOOL(use_incr_decr_opers);
DEFINE_GETTER_SETTER_BOOL(use_embedded_assigns);
DEFINE_GETTER_SETTER_BOOL(use_comma_exprs);
DEFINE_GETTER_SETTER_INT(int_bytes);
DEFINE_GETTER_SETTER_INT(pointer_bytes);

void
CGOptions::set_default_settings(void)
{
set_platform_specific_options();
compute_hash(true);
max_funcs(CGOPTIONS_DEFAULT_MAX_SPLIT_FILES);
max_funcs(CGOPTIONS_DEFAULT_MAX_FUNCS);
Expand Down Expand Up @@ -211,7 +212,6 @@ CGOptions::set_default_settings(void)
coverage_test(false);
coverage_test_size(CGOPTIONS_DEFAULT_COVERAGE_TEST_SIZE);
packed_struct(true);
resolve_bitfields_length();
bitfields(true);
prefix_name(false);
sequence_name_prefix(false);
Expand Down Expand Up @@ -254,19 +254,66 @@ CGOptions::set_default_settings(void)
use_incr_decr_opers(true);
use_embedded_assigns(true);
use_comma_exprs(true);
// these are defaults for x86-64 machine, our most used platform.
// configure them to generate *correct* random programs for other platforms.
int_bytes(4);
pointer_bytes(8);
}

/*
looking for the platform info file in the working directory
and load platform specific information. If not found, use
info from the platform that Csmith is running, and output them
to the file
*/
void
CGOptions::set_platform_specific_options(void)
{
const char* int_str = "integer size = ";
const char* ptr_str = "pointer size = ";
ifstream conf(PLATFORM_CONFIG_FILE);
if (conf.fail()) {
ofstream conf(PLATFORM_CONFIG_FILE);
conf << int_str << sizeof(int) << endl;
conf << ptr_str << sizeof(int*) << endl;
int_size(sizeof(int));
pointer_size(sizeof(int*));
conf.close();
}
else {
string line;
while(!conf.eof()) {
getline(conf, line);
if (line.substr(0, strlen(int_str)) == int_str) {
string s = line.substr(strlen(int_str));
StringUtils::chop(s);
int_size(StringUtils::str2int(s));
}
if (line.substr(0, strlen(ptr_str)) == ptr_str) {
string s = line.substr(strlen(ptr_str));
StringUtils::chop(s);
pointer_size(StringUtils::str2int(s));
}
}
if (!int_size_) {
cout << "please specify integer size in " << PLATFORM_CONFIG_FILE << endl;
exit(-1);
}
if (!pointer_size_) {
cout << "please specify pointer size in " << PLATFORM_CONFIG_FILE << endl;
exit(-1);
}
conf.close();
}
}

#define MAX_INTEGER_LENGTH 64
int
CGOptions::int_size(void)
{
Bookkeeper::rely_on_int_size = true;
return int_size_;
}

bool
CGOptions::resolve_bitfields_length(void)
int
CGOptions::pointer_size(void)
{
CGOptions::bitfields_length(MAX_INTEGER_LENGTH - 1);
return true;
return pointer_size_;
}

bool
Expand Down
20 changes: 9 additions & 11 deletions src/CGOptions.h
Expand Up @@ -63,6 +63,7 @@ using namespace std;
#define CGOPTIONS_DEFAULT_SPLIT_FILES_DIR ("./output")
#define CGOPTIONS_DEFAULT_OUTPUT_FILE ("")
#define CGOPTIONS_DEFAULT_VOL_ADDR_FILE ("vol_addr.txt")
#define PLATFORM_CONFIG_FILE ("platform.info")

/*
*
Expand Down Expand Up @@ -216,9 +217,6 @@ class CGOptions {
static bool bitfields(void);
static bool bitfields(bool p);

static int bitfields_length(void);
static int bitfields_length(int p);

static std::string partial_expand(void);
static std::string partial_expand(std::string p);

Expand Down Expand Up @@ -340,7 +338,6 @@ class CGOptions {
static const std::string& conflict_msg(void);

static bool is_random(void);
static bool resolve_bitfields_length();

static bool has_extension_support();

Expand Down Expand Up @@ -390,11 +387,13 @@ class CGOptions {
static bool use_comma_exprs(void);
static bool use_comma_exprs(bool p);

static int int_bytes(void);
static int int_bytes(int p);
static int int_size(void);
static void int_size(int p) { int_size_ = p;}

static int pointer_size(void);
static void pointer_size(int p) { pointer_size_ = p;}

static int pointer_bytes(void);
static int pointer_bytes(int p);
static void set_platform_specific_options(void);

private:
static bool resolve_exhaustive_options();
Expand Down Expand Up @@ -456,7 +455,6 @@ class CGOptions {
static int coverage_test_size_;
static bool packed_struct_;
static bool bitfields_;
static int bitfields_length_;
static bool prefix_name_;
static bool sequence_name_prefix_;
static bool compatible_check_;
Expand Down Expand Up @@ -510,8 +508,8 @@ class CGOptions {
static bool use_incr_decr_opers_;
static bool use_embedded_assigns_;
static bool use_comma_exprs_;
static int int_bytes_;
static int pointer_bytes_;
static int int_size_;
static int pointer_size_;
static Reducer* reducer_;

private:
Expand Down
1 change: 0 additions & 1 deletion src/RandomProgramGenerator.cpp
Expand Up @@ -461,7 +461,6 @@ main(int argc, char **argv)
}

if (strcmp (argv[i], "--bitfields") == 0) {
CGOptions::resolve_bitfields_length();
CGOptions::bitfields(true);
continue;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Type.cpp
Expand Up @@ -454,7 +454,7 @@ Type::has_int_field() const
bool
Type::signed_overflow_possible() const
{
return eType == eSimple && is_signed() && ((int)SizeInBytes()) >= CGOptions::int_bytes();
return eType == eSimple && is_signed() && ((int)SizeInBytes()) >= CGOptions::int_size();
}

void
Expand Down Expand Up @@ -580,7 +580,7 @@ Type::choose_random_nonvoid_simple(void)
void
Type::make_one_bitfield(vector<const Type*> &random_fields, vector<CVQualifiers> &qualifiers, vector<int> &fields_length)
{
int max_length = CGOptions::bitfields_length();
int max_length = CGOptions::int_size() * 8;
bool sign = rnd_flipcoin(BitFieldsSignedProb);
ERROR_RETURN();

Expand Down Expand Up @@ -757,7 +757,7 @@ Type::init_fields_enumerator(Enumerator<string> &enumerator,
int
Type::get_bitfield_length(int length_flag)
{
int max_length = CGOptions::bitfields_length();
int max_length = CGOptions::int_size() * 8;
assert(max_length > 0);
int length;
switch (length_flag) {
Expand Down Expand Up @@ -1451,7 +1451,7 @@ Type::SizeInBytes(void) const
return total_size;
}
case ePointer:
CGOptions::pointer_bytes();
CGOptions::pointer_size();
break;
}
return 0;
Expand Down Expand Up @@ -1606,7 +1606,7 @@ void OutputStructUnion(Type* type, std::ostream &out)
out << " : ";
else
out << " f" << j++ << " : ";
out << "_CSMITH_BITFIELD(" << length << ");";
out << length << ";";
}
else {
type->qfers_[i].output_qualified_type(field, out);
Expand Down

0 comments on commit 19e0cab

Please sign in to comment.