return result;
}
+#ifdef REG_TEST
+int lua_remove_registry_table_test(lua_State* L)
+{
+ const char* table_entry = lua_tostring(L, 2);
+ if (!table_entry)
+ {
+ return 0;
+ }
+ lua_getfield(L, LUA_REGISTRYINDEX, table_entry);
+ if (lua_isnil(L, -1))
+ {
+ APPID_LOG(nullptr, TRACE_ERROR_LEVEL, "appid: Registry table %s not found \n", table_entry);
+ return 0;
+ }
+ lua_pushnil(L);
+ lua_setfield(L, LUA_REGISTRYINDEX, table_entry);
+ return 1;
+}
+#endif
+
static const luaL_Reg detector_methods[] =
{
/* Obsolete API names. No longer use these! They are here for backward
{"addCipService", detector_add_cip_service},
{"addEnipCommand", detector_add_enip_command},
+#ifdef REG_TEST
+ {"luaRemoveRegistryTableTest", lua_remove_registry_table_test},
+#endif
+
{ nullptr, nullptr }
};
if (lua_pcall(my_lua_state, 0, 1, 0))
{
// Runtime Lua errors are suppressed in production code since detectors are written for
- // efficiency and with defensive minimum checks. Errors are dealt as exceptions
+ // efficiency and with minimum defensive checks. Errors are dealt as exceptions
// that don't impact processing by other detectors or future packets by the same detector.
APPID_LOG(args.pkt, TRACE_ERROR_LEVEL, "lua detector %s: error validating %s\n",
package_info.name.c_str(), lua_tostring(my_lua_state, -1));
if (L)
{
if (lua_gettop(L))
+ {
APPID_LOG(nullptr, TRACE_WARNING_LEVEL, "appid: leak of %d lua stack elements before detector unload\n",
lua_gettop(L));
+ lua_settop(L, 0);
+ }
- for ( auto& lua_object : allocated_objects )
+ for (auto& lua_object : allocated_objects)
{
LuaStateDescriptor* lsd = lua_object->validate_lua_state(false);
- lua_getfield(L, LUA_REGISTRYINDEX, lsd->package_info.name.c_str());
- lua_getfield(L, -1, lsd->package_info.cleanFunctionName.c_str());
- if ( lua_isfunction(L, -1) )
+ if (!lsd->package_info.cleanFunctionName.empty())
{
- string name = lsd->package_info.name + "_";
- lua_getglobal(L, name.c_str());
-
- if ( lua_pcall(L, 1, 1, 0) )
+ lua_getfield(L, LUA_REGISTRYINDEX, lsd->package_info.name.c_str());
+ if (lua_istable(L, -1))
{
- APPID_LOG(nullptr, TRACE_ERROR_LEVEL, "Could not cleanup the %s client app element: %s\n",
- lsd->package_info.name.c_str(), lua_tostring(L, -1));
+ lua_getfield(L, -1, lsd->package_info.cleanFunctionName.c_str());
+ if (lua_isfunction(L, -1))
+ {
+ string name = lsd->package_info.name + "_";
+ lua_getglobal(L, name.c_str());
+
+ if ( lua_pcall(L, 1, 1, 0) )
+ {
+ APPID_LOG(nullptr, TRACE_ERROR_LEVEL, "Error - appid: Could not cleanup the %s client app element: %s\n",
+ lsd->package_info.name.c_str(), lua_tostring(L, -1));
+ }
+ }
}
+ else
+ {
+ APPID_LOG(nullptr, TRACE_ERROR_LEVEL, "Error - appid: Could not find the %s detector table for cleanup\n",
+ lsd->package_info.name.c_str());
+ }
+
+ lua_settop(L, 0);
}
- lua_settop(L, 0);
+
delete lua_object;
}
lua_close(L);
Lua::ManageStack mgr(L);
lua_getfield(L, LUA_REGISTRYINDEX, detector_name);
+ if (!lua_istable(L, -1))
+ {
+ if (init(L))
+ APPID_LOG(nullptr, TRACE_ERROR_LEVEL, "Error - appid: Could not find detector table %s for initialization\n",
+ detector_name);
+ lua_pop(L, 1);
+ return nullptr;
+ }
lua_getfield(L, -1, "DetectorPackageInfo");
if (!lua_istable(L, -1))
void LuaDetectorManager::activate_lua_detectors(const SnortConfig* sc)
{
if (lua_gettop(L))
+ {
APPID_LOG(nullptr, TRACE_WARNING_LEVEL, "appid: leak of %d lua stack elements before detector activate\n",
lua_gettop(L));
+ lua_settop(L, 0);
+ }
uint32_t lua_tracker_size = compute_lua_tracker_size(MAX_MEMORY_FOR_LUA_DETECTORS, allocated_objects.size());
list<LuaObject*>::iterator lo = allocated_objects.begin();
{
LuaStateDescriptor* lsd = (*lo)->validate_lua_state(false);
lua_getfield(L, LUA_REGISTRYINDEX, lsd->package_info.name.c_str());
+ if (!lua_istable(L, -1))
+ {
+ if (init(L))
+ APPID_LOG(nullptr, TRACE_ERROR_LEVEL, "Error - appid: Could not find detector table %s for initialization\n",
+ (*lo)->get_detector()->get_name().c_str());
+ if (!(*lo)->get_detector()->is_custom_detector())
+ num_odp_detectors--;
+ lua_settop(L, 0);
+ delete *lo;
+ lo = allocated_objects.erase(lo);
+ continue;
+ }
lua_getfield(L, -1, lsd->package_info.initFunctionName.c_str());
if (!lua_isfunction(L, -1))
{
if (rval == 0 )
{
if (lua_gettop(L))
+ {
APPID_LOG(nullptr, TRACE_WARNING_LEVEL, "appid: leak of %d lua stack elements before detector load\n",
lua_gettop(L));
+ lua_settop(L, 0);
+ }
for (unsigned n = 0; n < globs.gl_pathc; n++)
{