check-structures-sanity: Allow running without MALLOC_PERTURB_, and adjust bool check accordingly

develop
lethosor 2023-09-28 22:59:35 -04:00
parent e253642660
commit 780fb5483d
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
3 changed files with 20 additions and 14 deletions

@ -82,6 +82,7 @@ public:
bool failfast; bool failfast;
bool noprogress; bool noprogress;
bool maybepointer; bool maybepointer;
uint8_t perturb_byte;
Checker(color_ostream & out); Checker(color_ostream & out);
bool queue_item(const QueueItem & item, CheckedStructure cs); bool queue_item(const QueueItem & item, CheckedStructure cs);

@ -296,7 +296,7 @@ void Checker::dispatch_primitive(const QueueItem & item, const CheckedStructure
else if (cs.identity == df::identity_traits<bool>::get()) else if (cs.identity == df::identity_traits<bool>::get())
{ {
auto val = *reinterpret_cast<const uint8_t *>(item.ptr); auto val = *reinterpret_cast<const uint8_t *>(item.ptr);
if (val > 1 && val != 0xd2) if (val > 1 && perturb_byte && val != perturb_byte && val != (perturb_byte ^ 0xff))
{ {
FAIL("invalid value for bool: " << int(val)); FAIL("invalid value for bool: " << int(val));
} }

@ -33,28 +33,33 @@ DFhackCExport command_result plugin_init(color_ostream &, std::vector<PluginComm
return CR_OK; return CR_OK;
} }
bool check_malloc_perturb() // returns 0 if MALLOC_PERTURB_ is unset, or if set to 0, because 0 is not useful
uint8_t check_malloc_perturb()
{ {
struct T_test { const size_t TEST_DATA_LEN = 5000; // >1 4kb page
uint32_t data[1024]; std::unique_ptr<uint8_t[]> test_data{new uint8_t[TEST_DATA_LEN]};
};
auto test = new T_test; uint8_t expected_perturb = test_data[0];
bool ret = (test->data[0] == 0xd2d2d2d2); if (getenv("MALLOC_PERTURB_"))
delete test; expected_perturb = 0xff ^ static_cast<uint8_t>(atoi(getenv("MALLOC_PERTURB_")));
return ret;
for (size_t i = 0; i < TEST_DATA_LEN; i++)
if (expected_perturb != test_data[i])
return 0;
return expected_perturb;
} }
static command_result command(color_ostream & out, std::vector<std::string> & parameters) static command_result command(color_ostream & out, std::vector<std::string> & parameters)
{ {
if (!check_malloc_perturb()) uint8_t perturb_byte = check_malloc_perturb();
{ if (!perturb_byte)
out.printerr("check-structures-sanity: MALLOC_PERTURB_ not set, cannot continue\n"); out.printerr("check-structures-sanity: MALLOC_PERTURB_ not set. Some checks may be bypassed or fail.\n");
return CR_FAILURE;
}
CoreSuspender suspend; CoreSuspender suspend;
Checker checker(out); Checker checker(out);
checker.perturb_byte = perturb_byte;
// check parameters with values first // check parameters with values first
#define VAL_PARAM(name, expr_using_value) \ #define VAL_PARAM(name, expr_using_value) \