diff --git a/src/script_opt/CPP/Compile.h b/src/script_opt/CPP/Compile.h index 7c00a6ed0b..5d818c1756 100644 --- a/src/script_opt/CPP/Compile.h +++ b/src/script_opt/CPP/Compile.h @@ -187,6 +187,11 @@ private: // Maps functions (not hooks or events) to upstream compiled names. std::unordered_map hashed_funcs; + // Tracks all of the module names used in activate_bodies__CPP() + // calls, to ensure all of the global names of compiled-to-standalone + // functions are available to subsequent scripts. + std::unordered_set module_names; + // If non-zero, provides a tag used for auxiliary/additional // compilation units. int addl_tag = 0; @@ -1007,6 +1012,16 @@ private: NL(); } + void Emit(const std::string& fmt, const std::string& arg1, + const std::string& arg2, const std::string& arg3, + const std::string& arg4, const std::string& arg5) const + { + Indent(); + fprintf(write_file, fmt.c_str(), arg1.c_str(), arg2.c_str(), + arg3.c_str(), arg4.c_str(), arg5.c_str()); + NL(); + } + // Returns an expression for constructing a Zeek String object // corresponding to the given byte array. std::string GenString(const char* b, int len) const; diff --git a/src/script_opt/CPP/Inits.cc b/src/script_opt/CPP/Inits.cc index 6996f42057..1f055d82f8 100644 --- a/src/script_opt/CPP/Inits.cc +++ b/src/script_opt/CPP/Inits.cc @@ -4,6 +4,7 @@ #include #include +#include "zeek/module_util.h" #include "zeek/script_opt/ProfileFunc.h" #include "zeek/script_opt/CPP/Compile.h" @@ -516,10 +517,6 @@ void CPPCompile::GenStandaloneActivation() for ( auto& fb : func_bodies ) { - auto f = fb.first; - const auto fn = f->Name(); - const auto& ft = f->GetType(); - string hashes; for ( auto h : fb.second ) { @@ -531,8 +528,23 @@ void CPPCompile::GenStandaloneActivation() hashes = "{" + hashes + "}"; - Emit("activate_bodies__CPP(\"%s\", %s, %s);", - fn, GenTypeName(ft), hashes); + auto f = fb.first; + auto fn = f->Name(); + const auto& ft = f->GetType(); + + auto var = extract_var_name(fn); + auto mod = extract_module_name(fn); + module_names.insert(mod); + + auto fid = lookup_ID(var.c_str(), mod.c_str(), + false, true, false); + if ( ! fid ) + reporter->InternalError("can't find identifier %s", fn); + + auto exported = fid->IsExport() ? "true" : "false"; + + Emit("activate_bodies__CPP(\"%s\", \"%s\", %s, %s, %s);", + var, mod, exported, GenTypeName(ft), hashes); } NL(); @@ -552,7 +564,15 @@ void CPPCompile::GenLoad() Emit("register_scripts__CPP(%s, standalone_init__CPP);", Fmt(total_hash)); - // Spit out the placeholder script. + // Spit out the placeholder script, and any associated module + // definitions. + for ( auto& m : module_names ) + if ( m != "GLOBAL" ) + printf("module %s;\n", m.c_str()); + + if ( module_names.size() > 0 ) + printf("module GLOBAL;\n\n"); + printf("global init_CPP_%llu = load_CPP(%llu);\n", total_hash, total_hash); } diff --git a/src/script_opt/CPP/RuntimeInit.cc b/src/script_opt/CPP/RuntimeInit.cc index c03f1ce3d3..003d082cd8 100644 --- a/src/script_opt/CPP/RuntimeInit.cc +++ b/src/script_opt/CPP/RuntimeInit.cc @@ -98,14 +98,15 @@ void register_scripts__CPP(p_hash_type h, void (*callback)()) standalone_callbacks[h] = callback; } -void activate_bodies__CPP(const char* fn, TypePtr t, vector hashes) +void activate_bodies__CPP(const char* fn, const char* module, bool exported, + TypePtr t, vector hashes) { auto ft = cast_intrusive(t); - auto fg = lookup_ID(fn, GLOBAL_MODULE_NAME, false, false, false); + auto fg = lookup_ID(fn, module, false, false, false); if ( ! fg ) { - fg = install_ID(fn, GLOBAL_MODULE_NAME, true, false); + fg = install_ID(fn, module, true, exported); fg->SetType(ft); } @@ -170,13 +171,13 @@ void activate_bodies__CPP(const char* fn, TypePtr t, vector hashes) } } -IDPtr lookup_global__CPP(const char* g, const TypePtr& t) +IDPtr lookup_global__CPP(const char* g, const TypePtr& t, bool exported) { auto gl = lookup_ID(g, GLOBAL_MODULE_NAME, false, false, false); if ( ! gl ) { - gl = install_ID(g, GLOBAL_MODULE_NAME, true, false); + gl = install_ID(g, GLOBAL_MODULE_NAME, true, exported); gl->SetType(t); } diff --git a/src/script_opt/CPP/RuntimeInit.h b/src/script_opt/CPP/RuntimeInit.h index e0e5de0c6d..0e58e9e42a 100644 --- a/src/script_opt/CPP/RuntimeInit.h +++ b/src/script_opt/CPP/RuntimeInit.h @@ -47,15 +47,17 @@ extern void register_lambda__CPP(CPPStmtPtr body, p_hash_type hash, // the given hash. extern void register_scripts__CPP(p_hash_type h, void (*callback)()); -// Activates the event handler/hook with the given name (which is created -// if it doesn't exist) and type, using (at least) the bodies associated -// with the given hashes. -extern void activate_bodies__CPP(const char* fn, TypePtr t, +// Activates the function/event handler/hook with the given name and in +// the given module, using (at least) the bodies associated with the +// given hashes. Creates the identifier using the given module and +// export setting if it doesn't already exist. +extern void activate_bodies__CPP(const char* fn, const char* module, + bool exported, TypePtr t, std::vector hashes); // Looks for a global with the given name. If not present, creates it -// with the given type. -extern IDPtr lookup_global__CPP(const char* g, const TypePtr& t); +// with the given type and export setting. +extern IDPtr lookup_global__CPP(const char* g, const TypePtr& t, bool exported); // Looks for a BiF with the given name. Returns nil if not present. extern Func* lookup_bif__CPP(const char* bif); diff --git a/src/script_opt/CPP/Vars.cc b/src/script_opt/CPP/Vars.cc index d1582f51a5..6e1795123b 100644 --- a/src/script_opt/CPP/Vars.cc +++ b/src/script_opt/CPP/Vars.cc @@ -109,9 +109,11 @@ void CPPCompile::CreateGlobal(const ID* g) const auto& t = g->GetType(); NoteInitDependency(g, TypeRep(t)); + auto exported = g->IsExport() ? "true" : "false"; + AddInit(g, globals[gn], string("lookup_global__CPP(\"") + gn + "\", " + - GenTypeName(t) + ")"); + GenTypeName(t) + ", " + exported + ")"); } if ( is_bif )