From b29ecfd82233424c9d2c9d487e795ceaf9725511 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Fri, 17 Apr 2020 18:41:14 -0700 Subject: [PATCH] bifcl: Deprecate BifEvent::generate_*, add BifEvent::enqueue_* --- tools/bifcl/bif_arg.cc | 18 ++++--- tools/bifcl/builtin-func.y | 81 ++++++++++++++++++++++---------- tools/bifcl/include/bif_arg.h | 16 ++----- tools/bifcl/include/bif_type.def | 39 +++++++-------- 4 files changed, 89 insertions(+), 65 deletions(-) diff --git a/tools/bifcl/bif_arg.cc b/tools/bifcl/bif_arg.cc index fc70628b8f..cc9fc27cac 100644 --- a/tools/bifcl/bif_arg.cc +++ b/tools/bifcl/bif_arg.cc @@ -11,11 +11,13 @@ static struct { const char* bif_type; const char* bro_type; const char* c_type; + const char* c_type_smart; const char* accessor; const char* constructor; + const char* ctor_smart; } builtin_func_arg_type[] = { -#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \ - {bif_type, bro_type, c_type, accessor, constructor}, +#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \ + {bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart}, #include "bif_type.def" #undef DEFINE_BIF_TYPE }; @@ -68,15 +70,19 @@ void BuiltinFuncArg::PrintCDef(FILE* fp, int n) fprintf(fp, ");\n"); } -void BuiltinFuncArg::PrintCArg(FILE* fp, int n) +void BuiltinFuncArg::PrintCArg(FILE* fp, int n, bool smart) { - const char* ctype = builtin_func_arg_type[type].c_type; + const char* ctype = smart ? builtin_func_arg_type[type].c_type_smart + : builtin_func_arg_type[type].c_type; char buf[1024]; fprintf(fp, "%s %s", ctype, name); } -void BuiltinFuncArg::PrintBroValConstructor(FILE* fp) +void BuiltinFuncArg::PrintBroValConstructor(FILE* fp, bool smart) { - fprintf(fp, builtin_func_arg_type[type].constructor, name); + if ( smart ) + fprintf(fp, builtin_func_arg_type[type].ctor_smart, name); + else + fprintf(fp, builtin_func_arg_type[type].constructor, name); } diff --git a/tools/bifcl/builtin-func.y b/tools/bifcl/builtin-func.y index 4cc4183ae1..c17a98359b 100644 --- a/tools/bifcl/builtin-func.y +++ b/tools/bifcl/builtin-func.y @@ -63,6 +63,8 @@ struct decl_struct { string generate_c_fullname; string generate_c_namespace_start; string generate_c_namespace_end; + string enqueue_c_barename; + string enqueue_c_fullname; } decl; void set_definition_type(int type, const char *arg_type_name) @@ -90,7 +92,9 @@ void set_decl_name(const char *name) decl.bro_name = ""; decl.generate_c_fullname = ""; + decl.enqueue_c_fullname = ""; decl.generate_bare_name = string("generate_") + decl.bare_name; + decl.enqueue_c_barename = string("enqueue_") + decl.bare_name; decl.generate_c_namespace_start = ""; decl.generate_c_namespace_end = ""; @@ -120,6 +124,7 @@ void set_decl_name(const char *name) decl.generate_c_namespace_start = "namespace BifEvent { "; decl.generate_c_namespace_end = " } "; decl.generate_c_fullname = "BifEvent::"; + decl.enqueue_c_fullname = "BifEvent::"; break; default: @@ -136,6 +141,7 @@ void set_decl_name(const char *name) decl.generate_c_namespace_start += "namespace " + decl.module_name + " { "; decl.generate_c_namespace_end += " } "; decl.generate_c_fullname += decl.module_name + "::"; + decl.enqueue_c_fullname += decl.module_name + "::"; } decl.bro_fullname += decl.bare_name; @@ -145,7 +151,7 @@ void set_decl_name(const char *name) decl.c_fullname += decl.bare_name; decl.bro_name += name; decl.generate_c_fullname += decl.generate_bare_name; - + decl.enqueue_c_fullname += decl.enqueue_c_barename; } const char* arg_list_name = "BiF_ARGS"; @@ -157,11 +163,13 @@ static struct { const char* bif_type; const char* bro_type; const char* c_type; + const char* c_type_smart; const char* accessor; const char* constructor; + const char* ctor_smatr; } builtin_types[] = { -#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \ - {bif_type, bro_type, c_type, accessor, constructor}, +#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \ + {bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart}, #include "bif_type.def" #undef DEFINE_BIF_TYPE }; @@ -199,32 +207,54 @@ char* concat(const char* str1, const char* str2) return s; } -// Print the bro_event_* function prototype in C++, without the ending ';' -void print_event_c_prototype(FILE *fp, bool is_header) +static void print_event_c_prototype_args(FILE* fp, bool smart) { - if ( is_header ) + for ( auto i = 0u; i < args.size(); ++i ) + { + if ( i > 0 ) + fprintf(fp, ", "); + + args[i]->PrintCArg(fp, i, smart); + } + } + +static void print_event_c_prototype_header(FILE* fp, bool smart) + { + if ( smart ) fprintf(fp, "%s void %s(analyzer::Analyzer* analyzer%s", - decl.generate_c_namespace_start.c_str(), decl.generate_bare_name.c_str(), + decl.generate_c_namespace_start.c_str(), + decl.enqueue_c_barename.c_str(), + args.size() ? ", " : "" ); + else + fprintf(fp, "%s [[deprecated(\"Remove in 4.1. Use %s instead.\")]] void %s(analyzer::Analyzer* analyzer%s", + decl.generate_c_namespace_start.c_str(), + decl.enqueue_c_fullname.c_str(), + decl.generate_bare_name.c_str(), + args.size() ? ", " : "" ); + + + print_event_c_prototype_args(fp, smart); + fprintf(fp, ")"); + fprintf(fp, "; %s\n", decl.generate_c_namespace_end.c_str()); + } + +static void print_event_c_prototype_impl(FILE* fp, bool smart) + { + if ( smart ) + fprintf(fp, "void %s(analyzer::Analyzer* analyzer%s", + decl.enqueue_c_fullname.c_str(), args.size() ? ", " : "" ); else fprintf(fp, "void %s(analyzer::Analyzer* analyzer%s", decl.generate_c_fullname.c_str(), args.size() ? ", " : "" ); - for ( int i = 0; i < (int) args.size(); ++i ) - { - if ( i > 0 ) - fprintf(fp, ", "); - args[i]->PrintCArg(fp, i); - } + + print_event_c_prototype_args(fp, smart); fprintf(fp, ")"); - if ( is_header ) - fprintf(fp, "; %s\n", decl.generate_c_namespace_end.c_str()); - else - fprintf(fp, "\n"); + fprintf(fp, "\n"); } -// Print the bro_event_* function body in C++. -void print_event_c_body(FILE *fp) +static void print_event_c_body(FILE* fp, bool smart) { fprintf(fp, "\t{\n"); fprintf(fp, "\t// Note that it is intentional that here we do not\n"); @@ -242,7 +272,7 @@ void print_event_c_body(FILE *fp) for ( int i = 0; i < (int) args.size(); ++i ) { fprintf(fp, "\t "); - args[i]->PrintBroValConstructor(fp); + args[i]->PrintBroValConstructor(fp, smart); fprintf(fp, ",\n"); if ( args[i]->Type() == TYPE_CONNECTION ) @@ -266,7 +296,7 @@ void print_event_c_body(FILE *fp) fprintf(fp, ", %s", connection_arg->Name()); fprintf(fp, ");\n"); - fprintf(fp, "\t} // event generation\n"); + fprintf(fp, "\t}\n\n"); //fprintf(fp, "%s // end namespace\n", decl.generate_c_namespace_end.c_str()); } @@ -386,9 +416,12 @@ event_def: event_prefix opt_ws plain_head opt_func_attrs { if ( events.find(decl.bro_fullname) == events.end() ) { - print_event_c_prototype(fp_func_h, true); - print_event_c_prototype(fp_func_def, false); - print_event_c_body(fp_func_def); + print_event_c_prototype_header(fp_func_h, false); + print_event_c_prototype_impl(fp_func_def, false); + print_event_c_body(fp_func_def, false); + print_event_c_prototype_header(fp_func_h, true); + print_event_c_prototype_impl(fp_func_def, true); + print_event_c_body(fp_func_def, true); events.insert(decl.bro_fullname); } } diff --git a/tools/bifcl/include/bif_arg.h b/tools/bifcl/include/bif_arg.h index 4edf23344a..c1dd5064b6 100644 --- a/tools/bifcl/include/bif_arg.h +++ b/tools/bifcl/include/bif_arg.h @@ -3,20 +3,10 @@ #include enum builtin_func_arg_type { -#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) \ +#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \ id, #include "bif_type.def" #undef DEFINE_BIF_TYPE -/* - TYPE_ANY, - TYPE_BOOL, - TYPE_COUNT, - TYPE_INT, - TYPE_STRING, - TYPE_PATTERN, - TYPE_PORT, - TYPE_OTHER, -*/ }; extern const char* builtin_func_arg_type_bro_name[]; @@ -37,8 +27,8 @@ public: void PrintBro(FILE* fp); void PrintCDef(FILE* fp, int n); - void PrintCArg(FILE* fp, int n); - void PrintBroValConstructor(FILE* fp); + void PrintCArg(FILE* fp, int n, bool smart); + void PrintBroValConstructor(FILE* fp, bool smart); protected: const char* name; diff --git a/tools/bifcl/include/bif_type.def b/tools/bifcl/include/bif_type.def index 23e9c40da3..974e8c6db5 100644 --- a/tools/bifcl/include/bif_type.def +++ b/tools/bifcl/include/bif_type.def @@ -1,22 +1,17 @@ -// DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, accessor, constructor) - -DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "%s->AsAddrVal()", "IntrusivePtr{AdoptRef{}, %s}") -DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "%s", "IntrusivePtr{AdoptRef{}, %s}") -DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "%s->AsBool()", "val_mgr->Bool(%s)") -DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "%s", "IntrusivePtr{AdoptRef{}, %s}") -DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->ConnVal()") -DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "%s->AsCount()", "val_mgr->Count(%s)") -DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "%s->AsDouble()", "make_intrusive(%s, TYPE_DOUBLE)") -DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "%s->AsFile()", "make_intrusive(%s)") -DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "%s->AsInt()", "val_mgr->Int(%s)") -DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "%s->AsInterval()", "make_intrusive(%s, Seconds)") -DEFINE_BIF_TYPE(TYPE_PACKET, "packet", "packet", "TCP_TracePacket*", "%s->AsRecordVal()->GetOrigin()", "IntrusivePtr{AdoptRef{}, %s->PacketVal()}") -DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "%s->AsPattern()", "make_intrusive(%s)") -// DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "uint32", "%s->AsPortVal()->Port()", "incomplete data") -DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}") -DEFINE_BIF_TYPE(TYPE_PORTVAL, "portval", "port", "PortVal*", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}") -DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "%s->AsStringVal()", "IntrusivePtr{AdoptRef{}, %s}") -// DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "BroString*", "%s->AsString()", "make_intrusive(%s)") -DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "%s->AsSubNetVal()", "IntrusivePtr{AdoptRef{}, %s}") -DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "%s->AsTime()", "make_intrusive(%s, TYPE_TIME)") -DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "%s", "IntrusivePtr{AdoptRef{}, %s}") +// (id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) +DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "IntrusivePtr", "%s->AsAddrVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "IntrusivePtr", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "int", "%s->AsBool()", "val_mgr->Bool(%s)", "val_mgr->Bool(%s)") +DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "IntrusivePtr", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->ConnVal()", "%s->ConnVal()") +DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "bro_uint_t", "%s->AsCount()", "val_mgr->Count(%s)", "val_mgr->Count(%s)") +DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "double", "%s->AsDouble()", "make_intrusive(%s, TYPE_DOUBLE)", "make_intrusive(%s, TYPE_DOUBLE)") +DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "IntrusivePtr", "%s->AsFile()", "make_intrusive(%s)", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "bro_int_t", "%s->AsInt()", "val_mgr->Int(%s)", "val_mgr->Int(%s)") +DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "double", "%s->AsInterval()", "make_intrusive(%s, Seconds)", "make_intrusive(%s, Seconds)") +DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "IntrusivePtr", "%s->AsPattern()", "make_intrusive(%s)", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "IntrusivePtr", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "IntrusivePtr", "%s->AsStringVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "IntrusivePtr", "%s->AsSubNetVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "double", "%s->AsTime()", "make_intrusive(%s, TYPE_TIME)", "make_intrusive(%s, TYPE_TIME)") +DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "IntrusivePtr", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")