2020-03-10 17:53:56 -06:00
|
|
|
#include "check-structures-sanity.h"
|
|
|
|
|
|
|
|
#include "LuaTools.h"
|
|
|
|
#include "LuaWrapper.h"
|
|
|
|
|
|
|
|
DFHACK_PLUGIN("check-structures-sanity");
|
|
|
|
|
|
|
|
static command_result command(color_ostream &, std::vector<std::string> &);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init(color_ostream &, std::vector<PluginCommand> & commands)
|
|
|
|
{
|
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"check-structures-sanity",
|
|
|
|
"performs a sanity check on df-structures",
|
|
|
|
command,
|
|
|
|
false,
|
|
|
|
"check-structures-sanity [-enums] [-sizes] [-lowmem] [-maxerrors n] [-failfast] [starting_point]\n"
|
|
|
|
"\n"
|
|
|
|
"-enums: report unexpected or unnamed enum or bitfield values.\n"
|
|
|
|
"-sizes: report struct and class sizes that don't match structures. (requires sizecheck)\n"
|
|
|
|
"-unnamed: report unnamed enum/bitfield values, not just undefined ones.\n"
|
|
|
|
"-maxerrors n: set the maximum number of errors before bailing out.\n"
|
|
|
|
"-failfast: crash if any error is encountered. useful only for debugging.\n"
|
2020-03-12 10:35:52 -06:00
|
|
|
"-maybepointer: report integers that might actually be pointers.\n"
|
2020-03-10 17:53:56 -06:00
|
|
|
"starting_point: a lua expression or a word like 'screen', 'item', or 'building'. (defaults to df.global)\n"
|
|
|
|
"\n"
|
|
|
|
"by default, check-structures-sanity reports invalid pointers, vectors, strings, and vtables."
|
|
|
|
));
|
|
|
|
|
2020-03-10 22:05:59 -06:00
|
|
|
known_types_by_size.clear();
|
|
|
|
build_size_table();
|
|
|
|
|
2020-03-10 17:53:56 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:48:21 -06:00
|
|
|
bool check_malloc_perturb()
|
|
|
|
{
|
|
|
|
struct T_test {
|
|
|
|
int32_t data[1024];
|
|
|
|
};
|
|
|
|
auto test = new T_test;
|
|
|
|
bool ret = (test->data[0] == 0xd2d2d2d2);
|
|
|
|
delete test;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-10 17:53:56 -06:00
|
|
|
static command_result command(color_ostream & out, std::vector<std::string> & parameters)
|
|
|
|
{
|
2020-04-01 23:48:21 -06:00
|
|
|
if (!check_malloc_perturb())
|
|
|
|
{
|
|
|
|
out.printerr("check-structures-sanity: MALLOC_PERTURB_ not set, cannot continue\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-03-10 17:53:56 -06:00
|
|
|
CoreSuspender suspend;
|
|
|
|
|
|
|
|
Checker checker(out);
|
2020-03-11 10:20:10 -06:00
|
|
|
|
2020-03-10 17:53:56 -06:00
|
|
|
// check parameters with values first
|
|
|
|
#define VAL_PARAM(name, expr_using_value) \
|
|
|
|
auto name ## _idx = std::find(parameters.begin(), parameters.end(), "-" #name); \
|
|
|
|
if (name ## _idx != parameters.end()) \
|
|
|
|
{ \
|
|
|
|
if (name ## _idx + 1 == parameters.end()) \
|
|
|
|
{ \
|
|
|
|
return CR_WRONG_USAGE; \
|
|
|
|
} \
|
|
|
|
try \
|
|
|
|
{ \
|
|
|
|
auto value = std::move(*(name ## _idx + 1)); \
|
|
|
|
parameters.erase((name ## _idx + 1)); \
|
|
|
|
parameters.erase(name ## _idx); \
|
|
|
|
checker.name = (expr_using_value); \
|
|
|
|
} \
|
|
|
|
catch (std::exception & ex) \
|
|
|
|
{ \
|
|
|
|
out.printerr("check-structures-sanity: argument to -%s: %s\n", #name, ex.what()); \
|
|
|
|
return CR_WRONG_USAGE; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
VAL_PARAM(maxerrors, std::stoul(value));
|
|
|
|
#undef VAL_PARAM
|
|
|
|
|
|
|
|
#define BOOL_PARAM(name) \
|
|
|
|
auto name ## _idx = std::find(parameters.begin(), parameters.end(), "-" #name); \
|
|
|
|
if (name ## _idx != parameters.end()) \
|
|
|
|
{ \
|
|
|
|
checker.name = true; \
|
|
|
|
parameters.erase(name ## _idx); \
|
|
|
|
}
|
|
|
|
BOOL_PARAM(enums);
|
|
|
|
BOOL_PARAM(sizes);
|
|
|
|
BOOL_PARAM(unnamed);
|
|
|
|
BOOL_PARAM(failfast);
|
2020-03-11 10:20:10 -06:00
|
|
|
BOOL_PARAM(noprogress);
|
2020-03-12 10:35:52 -06:00
|
|
|
BOOL_PARAM(maybepointer);
|
2020-03-10 17:53:56 -06:00
|
|
|
#undef BOOL_PARAM
|
|
|
|
|
|
|
|
if (parameters.size() > 1)
|
|
|
|
{
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.empty())
|
|
|
|
{
|
|
|
|
checker.queue_globals();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using namespace DFHack::Lua;
|
|
|
|
using namespace DFHack::Lua::Core;
|
|
|
|
using namespace DFHack::LuaWrapper;
|
|
|
|
|
|
|
|
StackUnwinder unwinder(State);
|
|
|
|
PushModulePublic(out, "utils", "df_expr_to_ref");
|
|
|
|
Push(parameters.at(0));
|
|
|
|
if (!SafeCall(out, 1, 1))
|
|
|
|
{
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
if (!lua_touserdata(State, -1))
|
|
|
|
{
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueueItem item(parameters.at(0), get_object_ref(State, -1));
|
|
|
|
lua_getfield(State, -1, "_type");
|
|
|
|
lua_getfield(State, -1, "_identity");
|
|
|
|
auto identity = reinterpret_cast<type_identity *>(lua_touserdata(State, -1));
|
|
|
|
if (!identity)
|
|
|
|
{
|
|
|
|
out.printerr("could not determine type identity\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
checker.queue_item(item, CheckedStructure(identity));
|
|
|
|
}
|
|
|
|
|
|
|
|
while (checker.process_queue())
|
|
|
|
{
|
2020-03-11 10:20:10 -06:00
|
|
|
if (!checker.noprogress)
|
2020-03-10 17:53:56 -06:00
|
|
|
{
|
|
|
|
out << "checked " << checker.checked_count << " fields\r" << std::flush;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "checked " << checker.checked_count << " fields" << std::endl;
|
|
|
|
|
|
|
|
return checker.error_count ? CR_FAILURE : CR_OK;
|
|
|
|
}
|