mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
bifcl: Render runtime type checks for var_args bifs
Currently, Zeek disables any static type checking for var_arg bifs. However, the generated preamble for var_args bifs assume that typed positional arguments are correctly typed and blindly calls the type converters on them. This easily triggers abort()s at runtime currently when a script mistakenly uses the wrong types for var_arg bifs. For example, calling publish_rr() with a port instead of a string causes a hard-abort with Zeek 5.0.8. $ zeek -e 'Cluster::publish_rr(Cluster::Pool(), 80/tcp)' fatal error in <no location>: Val::CONVERTER (port/string) (80/tcp) Aborted (core dumped) Extend bifcl so that for var_arg functions and the types that bifcl understands, we render a runtime type check and explicit early return to avoid the abort(). For any/other types, the implementer of the bif continuous to be responsible for type checking. This isn't solving the var_args situation generally, but avoids some ad-hoc fixes trickling in current bif implementations. Some references: https://github.com/zeek/zeek/issues/1523 https://github.com/zeek/zeek/issues/2425 https://github.com/zeek/zeek/issues/2935 https://github.com/zeek/zeek/pull/2950
This commit is contained in:
parent
efb32d31fc
commit
e7cce57f2b
3 changed files with 26 additions and 9 deletions
|
@ -9,6 +9,7 @@ using namespace std;
|
||||||
|
|
||||||
static struct
|
static struct
|
||||||
{
|
{
|
||||||
|
const char* type_enum;
|
||||||
const char* bif_type;
|
const char* bif_type;
|
||||||
const char* zeek_type;
|
const char* zeek_type;
|
||||||
const char* c_type;
|
const char* c_type;
|
||||||
|
@ -21,8 +22,8 @@ static struct
|
||||||
} builtin_func_arg_type[] = {
|
} builtin_func_arg_type[] = {
|
||||||
#define DEFINE_BIF_TYPE(id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, \
|
#define DEFINE_BIF_TYPE(id, bif_type, zeek_type, c_type, c_type_smart, accessor, accessor_smart, \
|
||||||
cast_smart, constructor, ctor_smart) \
|
cast_smart, constructor, ctor_smart) \
|
||||||
{bif_type, zeek_type, c_type, c_type_smart, accessor, \
|
{#id, bif_type, zeek_type, c_type, c_type_smart, \
|
||||||
accessor_smart, cast_smart, constructor, ctor_smart},
|
accessor, accessor_smart, cast_smart, constructor, ctor_smart},
|
||||||
#include "bif_type.def"
|
#include "bif_type.def"
|
||||||
#undef DEFINE_BIF_TYPE
|
#undef DEFINE_BIF_TYPE
|
||||||
};
|
};
|
||||||
|
@ -58,8 +59,24 @@ void BuiltinFuncArg::PrintZeek(FILE* fp)
|
||||||
fprintf(fp, "%s: %s%s %s", name, builtin_func_arg_type[type].zeek_type, type_str, attr_str);
|
fprintf(fp, "%s: %s%s %s", name, builtin_func_arg_type[type].zeek_type, type_str, attr_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuiltinFuncArg::PrintCDef(FILE* fp, int n)
|
void BuiltinFuncArg::PrintCDef(FILE* fp, int n, bool runtime_type_check)
|
||||||
{
|
{
|
||||||
|
// Generate a runtime type-check pre-amble for types we understand
|
||||||
|
if ( runtime_type_check && type != TYPE_OTHER && type != TYPE_ANY )
|
||||||
|
{
|
||||||
|
fprintf(fp, "\t\t{\n");
|
||||||
|
fprintf(fp, "\t\t// Runtime type check for %s argument\n", name);
|
||||||
|
fprintf(fp, "\t\tzeek::TypeTag __tag = (*%s)[%d]->GetType()->Tag();\n", arg_list_name, n);
|
||||||
|
fprintf(fp, "\t\tif ( __tag != %s )\n", builtin_func_arg_type[type].type_enum);
|
||||||
|
fprintf(fp, "\t\t\t{\n");
|
||||||
|
fprintf(fp,
|
||||||
|
"\t\t\tzeek::emit_builtin_error(zeek::util::fmt(\"expected type %s for %s, got "
|
||||||
|
"%%s\", zeek::type_name(__tag)));\n",
|
||||||
|
builtin_func_arg_type[type].zeek_type, name);
|
||||||
|
fprintf(fp, "\t\t\treturn nullptr;\n");
|
||||||
|
fprintf(fp, "\t\t\t}\n");
|
||||||
|
fprintf(fp, "\t\t}\n");
|
||||||
|
}
|
||||||
fprintf(fp, "\t%s %s = (%s) (", builtin_func_arg_type[type].c_type, name,
|
fprintf(fp, "\t%s %s = (%s) (", builtin_func_arg_type[type].c_type, name,
|
||||||
builtin_func_arg_type[type].c_type);
|
builtin_func_arg_type[type].c_type);
|
||||||
|
|
||||||
|
|
|
@ -708,8 +708,8 @@ body_start: TOK_LPB c_code_begin
|
||||||
fprintf(fp_func_def, "\tif ( %s->size() != %d )\n", arg_list_name, argc);
|
fprintf(fp_func_def, "\tif ( %s->size() != %d )\n", arg_list_name, argc);
|
||||||
fprintf(fp_func_def, "\t\t{\n");
|
fprintf(fp_func_def, "\t\t{\n");
|
||||||
fprintf(fp_func_def,
|
fprintf(fp_func_def,
|
||||||
"\t\treporter->Error(\"%s() takes exactly %d argument(s)\");\n",
|
"\t\tzeek::emit_builtin_error(zeek::util::fmt(\"%s() takes exactly %d argument(s), got %%lu\", %s->size()));\n",
|
||||||
decl.zeek_fullname.c_str(), argc);
|
decl.zeek_fullname.c_str(), argc, arg_list_name);
|
||||||
fprintf(fp_func_def, "\t\treturn nullptr;\n");
|
fprintf(fp_func_def, "\t\treturn nullptr;\n");
|
||||||
fprintf(fp_func_def, "\t\t}\n");
|
fprintf(fp_func_def, "\t\t}\n");
|
||||||
}
|
}
|
||||||
|
@ -718,14 +718,14 @@ body_start: TOK_LPB c_code_begin
|
||||||
fprintf(fp_func_def, "\tif ( %s->size() < %d )\n", arg_list_name, argc);
|
fprintf(fp_func_def, "\tif ( %s->size() < %d )\n", arg_list_name, argc);
|
||||||
fprintf(fp_func_def, "\t\t{\n");
|
fprintf(fp_func_def, "\t\t{\n");
|
||||||
fprintf(fp_func_def,
|
fprintf(fp_func_def,
|
||||||
"\t\treporter->Error(\"%s() takes at least %d argument(s)\");\n",
|
"\t\tzeek::emit_builtin_error(zeek::util::fmt(\"%s() takes at least %d argument(s), got %%lu\", %s->size()));\n",
|
||||||
decl.zeek_fullname.c_str(), argc);
|
decl.zeek_fullname.c_str(), argc, arg_list_name);
|
||||||
fprintf(fp_func_def, "\t\treturn nullptr;\n");
|
fprintf(fp_func_def, "\t\treturn nullptr;\n");
|
||||||
fprintf(fp_func_def, "\t\t}\n");
|
fprintf(fp_func_def, "\t\t}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int i = 0; i < (int) args.size(); ++i )
|
for ( int i = 0; i < (int) args.size(); ++i )
|
||||||
args[i]->PrintCDef(fp_func_def, i + implicit_arg);
|
args[i]->PrintCDef(fp_func_def, i + implicit_arg, var_arg);
|
||||||
print_line_directive(fp_func_def);
|
print_line_directive(fp_func_def);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
|
@ -25,7 +25,7 @@ public:
|
||||||
int Type() const { return type; }
|
int Type() const { return type; }
|
||||||
|
|
||||||
void PrintZeek(FILE* fp);
|
void PrintZeek(FILE* fp);
|
||||||
void PrintCDef(FILE* fp, int n);
|
void PrintCDef(FILE* fp, int n, bool runtime_type_check = false);
|
||||||
void PrintCArg(FILE* fp, int n);
|
void PrintCArg(FILE* fp, int n);
|
||||||
void PrintValConstructor(FILE* fp);
|
void PrintValConstructor(FILE* fp);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue