From b226f7c27144902b136a7153b217405fa52788cf Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 4 Feb 2022 01:19:48 -0800 Subject: [PATCH] Added line logging for all lua errors --- src/pc/lua/smlua_cobject.c | 12 ++++++++++++ src/pc/lua/smlua_hooks.c | 18 ++++++++++++++++++ src/pc/lua/smlua_sync_table.c | 14 ++++++++++++++ src/pc/lua/smlua_utils.c | 4 ++++ 4 files changed, 48 insertions(+) diff --git a/src/pc/lua/smlua_cobject.c b/src/pc/lua/smlua_cobject.c index 5fe6a60fc..9a5d04cae 100644 --- a/src/pc/lua/smlua_cobject.c +++ b/src/pc/lua/smlua_cobject.c @@ -61,22 +61,26 @@ static int smlua__get_field(lua_State* L) { if (pointer == 0) { LOG_LUA("_get_field on null pointer"); + smlua_logline(); return 0; } if (!smlua_valid_lot(lot)) { LOG_LUA("_get_field on invalid LOT '%u'", lot); + smlua_logline(); return 0; } if (!smlua_cobject_allowlist_contains(lot, pointer)) { LOG_LUA("_get_field received a pointer not in allow list. '%u', '%llu", lot, (u64)pointer); + smlua_logline(); return 0; } struct LuaObjectField* data = smlua_get_object_field(lot, key); if (data == NULL) { LOG_LUA("_get_field on invalid key '%s', lot '%d'", key, lot); + smlua_logline(); return 0; } @@ -96,6 +100,7 @@ static int smlua__get_field(lua_State* L) { case LVT_STRING_P: lua_pushstring(L, *(char**)p); break; default: LOG_LUA("_get_field on unimplemented type '%d', key '%s'", data->valueType, key); + smlua_logline(); return 0; } @@ -116,27 +121,32 @@ static int smlua__set_field(lua_State* L) { if (pointer == 0) { LOG_LUA("_set_field on null pointer"); + smlua_logline(); return 0; } if (!smlua_valid_lot(lot)) { LOG_LUA("_set_field on invalid LOT '%u'", lot); + smlua_logline(); return 0; } if (!smlua_cobject_allowlist_contains(lot, pointer)) { LOG_LUA("_set_field received a pointer not in allow list. '%u', '%llu", lot, (u64)pointer); + smlua_logline(); return 0; } struct LuaObjectField* data = smlua_get_object_field(lot, key); if (data == NULL) { LOG_LUA("_set_field on invalid key '%s'", key); + smlua_logline(); return 0; } if (data->immutable) { LOG_LUA("_set_field on immutable key '%s'", key); + smlua_logline(); return 0; } @@ -152,10 +162,12 @@ static int smlua__set_field(lua_State* L) { case LVT_F32: *(f32*)p = smlua_to_number(L, -1); break; default: LOG_LUA("_set_field on unimplemented type '%d', key '%s'", data->valueType, key); + smlua_logline(); return 0; } if (!gSmLuaConvertSuccess) { LOG_LUA("_set_field failed to retrieve value type '%d', key '%s'", data->valueType, key); + smlua_logline(); return 0; } diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 5b29c690a..411a5a70a 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -17,18 +17,21 @@ int smlua_hook_event(lua_State* L) { if (hookType >= HOOK_MAX) { LOG_LUA("Hook Type: %d exceeds max!", hookType); + smlua_logline(); return 0; } struct LuaHookedEvent* hook = &sHookedEvents[hookType]; if (hook->count >= MAX_HOOKED_REFERENCES) { LOG_LUA("Hook Type: %s exceeded maximum references!", LuaHookedEventTypeName[hookType]); + smlua_logline(); return 0; } int ref = luaL_ref(L, LUA_REGISTRYINDEX); if (ref == -1) { LOG_LUA("tried to hook undefined function to '%s'", LuaHookedEventTypeName[hookType]); + smlua_logline(); return 0; } @@ -49,6 +52,7 @@ void smlua_call_event_hooks(enum LuaHookedEventType hookType) { // call the callback if (0 != lua_pcall(L, 0, 0, 0)) { LOG_LUA("Failed to call the event_hook callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); continue; } } @@ -71,6 +75,7 @@ void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct // call the callback if (0 != lua_pcall(L, 1, 0, 0)) { LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); continue; } } @@ -99,6 +104,7 @@ void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struc // call the callback if (0 != lua_pcall(L, 2, 0, 0)) { LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); continue; } } @@ -121,6 +127,7 @@ void smlua_call_event_hooks_network_player_param(enum LuaHookedEventType hookTyp // call the callback if (0 != lua_pcall(L, 1, 0, 0)) { LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); continue; } } @@ -144,12 +151,14 @@ int smlua_hook_mario_action(lua_State* L) { if (L == NULL) { return 0; } if (sHookedMarioActionsCount >= MAX_HOOKED_ACTIONS) { LOG_LUA("Hooked mario actions exceeded maximum references!"); + smlua_logline(); return 0; } lua_Integer action = smlua_to_integer(L, -2); if (action == 0 || gSmLuaConvertSuccess) { LOG_LUA("Hook Action: tried to hook invalid action"); + smlua_logline(); return 0; } @@ -157,6 +166,7 @@ int smlua_hook_mario_action(lua_State* L) { if (ref == -1) { LOG_LUA("Hook Action: %lld tried to hook undefined function", action); + smlua_logline(); return 0; } @@ -186,6 +196,7 @@ bool smlua_call_action_hook(struct MarioState* m, s32* returnValue) { // call the callback if (0 != lua_pcall(L, 1, 1, 0)) { LOG_LUA("Failed to call the action callback: %u, %s", m->action, lua_tostring(L, -1)); + smlua_logline(); continue; } @@ -221,24 +232,28 @@ int smlua_hook_chat_command(lua_State* L) { if (L == NULL) { return 0; } if (sHookedChatCommandsCount >= MAX_HOOKED_CHAT_COMMANDS) { LOG_LUA("Hooked chat command exceeded maximum references!"); + smlua_logline(); return 0; } const char* command = smlua_to_string(L, 1); if (command == NULL || strlen(command) == 0 || !gSmLuaConvertSuccess) { LOG_LUA("Hook chat command: tried to hook invalid command"); + smlua_logline(); return 0; } const char* description = smlua_to_string(L, 2); if (description == NULL || strlen(description) == 0 || !gSmLuaConvertSuccess) { LOG_LUA("Hook chat command: tried to hook invalid description"); + smlua_logline(); return 0; } int ref = luaL_ref(L, LUA_REGISTRYINDEX); if (ref == -1) { LOG_LUA("Hook chat command: tried to hook undefined function '%s'", command); + smlua_logline(); return 0; } @@ -281,6 +296,7 @@ bool smlua_call_chat_command_hook(char* command) { // call the callback if (0 != lua_pcall(L, 1, 1, 0)) { LOG_LUA("Failed to call the chat command callback: %s, %s", command, lua_tostring(L, -1)); + smlua_logline(); continue; } @@ -324,11 +340,13 @@ int smlua_hook_on_sync_table_change(lua_State* L) { if (lua_type(L, syncTableIndex) != LUA_TTABLE) { LOG_LUA("Tried to attach a non-table to hook_on_sync_table_change: %d", lua_type(L, syncTableIndex)); + smlua_logline(); return 0; } if (lua_type(L, funcIndex) != LUA_TFUNCTION) { LOG_LUA("Tried to attach a non-function to hook_on_sync_table_change: %d", lua_type(L, funcIndex)); + smlua_logline(); return 0; } diff --git a/src/pc/lua/smlua_sync_table.c b/src/pc/lua/smlua_sync_table.c index 9dda39f79..1497e02a6 100644 --- a/src/pc/lua/smlua_sync_table.c +++ b/src/pc/lua/smlua_sync_table.c @@ -44,6 +44,7 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) { sUnwoundLnts[sUnwoundLntsCount++] = smlua_to_lnt(L, keyIndex); if (!gSmLuaConvertSuccess) { LOG_LUA("attempted to unwind sync table with invalid key type"); + smlua_logline(); return false; } @@ -55,6 +56,7 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) { // make sure we remain within limits if (sUnwoundLntsCount >= MAX_UNWOUND_LNT) { LOG_LUA("attempted to unwind sync table past its limit"); + smlua_logline(); return false; } @@ -77,6 +79,7 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) { if (!gSmLuaConvertSuccess) { LOG_LUA("attempted to unwind sync table with invalid parent"); lua_pop(L, 1); // pop iterative _parent + smlua_logline(); return false; } @@ -90,6 +93,7 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) { if (parentType != LUA_TTABLE) { if (parentType != LUA_TNIL) { LOG_LUA("attempted to unwind sync table into an invalid parent"); + smlua_logline(); return false; } break; @@ -106,6 +110,7 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) { if (unwoundSize >= MAX_UNWOUND_SIZE) { LOG_LUA("attempted to unwind sync table with too long of a key/parent length"); + smlua_logline(); return false; } @@ -135,6 +140,7 @@ static void smlua_sync_table_call_hook(int syncTableIndex, int keyIndex, int pre // call hook if (0 != lua_pcall(L, 3, 0, 0)) { LOG_LUA("Failed to call the hook_on_changed callback: %s", lua_tostring(L, -1)); + smlua_logline(); } } @@ -156,6 +162,7 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al u16 modRemoteIndex = smlua_get_integer_field(syncTableIndex, "_remoteIndex"); if (!gSmLuaConvertSuccess) { LOG_LUA("Error: tried to alter sync table with an invalid modRemoteIndex: %u", modRemoteIndex); + smlua_logline(); return false; } @@ -163,6 +170,7 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al struct LSTNetworkType lntKey = smlua_to_lnt(L, keyIndex); if (!gSmLuaConvertSuccess) { LOG_LUA("Error: tried to alter sync table with an invalid key"); + smlua_logline(); return false; } lntKey = lntKey; @@ -181,6 +189,7 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al if (prevValueType == LUA_TTABLE) { LOG_LUA("Error: tried to assign on top of sync table"); + smlua_logline(); goto CLEANUP_STACK; } @@ -193,11 +202,13 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al if (valueType == LUA_TTABLE) { if (prevValueType != LUA_TNIL) { LOG_LUA("Error: tried to set a sync table field to a different sync table"); + smlua_logline(); goto CLEANUP_STACK; } if (!smlua_is_table_empty(valueIndex)) { LOG_LUA("Error: tried to generate a sync table with a non-empty table"); + smlua_logline(); goto CLEANUP_STACK; } @@ -217,6 +228,7 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al struct LSTNetworkType lntValue = smlua_to_lnt(L, valueIndex); if (!gSmLuaConvertSuccess) { LOG_LUA("Error: tried to alter sync table with an invalid value"); + smlua_logline(); goto CLEANUP_STACK; } @@ -256,6 +268,7 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al // unwind key + parent tables if (!smlua_sync_table_unwind(syncTableIndex, keyIndex)) { LOG_LUA("Error: failed to unwind sync table for sending over the network"); + smlua_logline(); goto CLEANUP_STACK; } @@ -432,6 +445,7 @@ static void smlua_exec_str(char* str) { if (luaL_dostring(L, str) != LUA_OK) { LOG_LUA("Failed to load lua string."); puts(smlua_to_string(L, lua_gettop(L))); + smlua_logline(); } LUA_STACK_CHECK_END(); } diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index a0ac3fe53..35be95cb1 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -166,6 +166,7 @@ struct LSTNetworkType smlua_to_lnt(lua_State* L, int index) { lnt.value.string = (char*)lua_tostring(L, index); if (lnt.value.string == NULL || strlen(lnt.value.string) > 256) { LOG_LUA("smlua_to_lnt on invalid string value: '%s'", (lnt.value.string == NULL) ? "" : lnt.value.string); + smlua_logline(); gSmLuaConvertSuccess = false; return lnt; } @@ -182,6 +183,7 @@ struct LSTNetworkType smlua_to_lnt(lua_State* L, int index) { } LOG_LUA("smlua_to_lnt on invalid type: '%d'", valueType); + smlua_logline(); gSmLuaConvertSuccess = false; return lnt; } @@ -260,6 +262,7 @@ void smlua_push_lnt(struct LSTNetworkType* lnt) { lua_Integer smlua_get_integer_field(int index, char* name) { if (lua_type(gLuaState, index) != LUA_TTABLE) { LOG_LUA("smlua_get_integer_field received improper type '%d'", lua_type(gLuaState, index)); + smlua_logline(); gSmLuaConvertSuccess = false; return 0; } @@ -272,6 +275,7 @@ lua_Integer smlua_get_integer_field(int index, char* name) { lua_Number smlua_get_number_field(int index, char* name) { if (lua_type(gLuaState, index) != LUA_TTABLE) { LOG_LUA("smlua_get_number_field received improper type '%d'", lua_type(gLuaState, index)); + smlua_logline(); gSmLuaConvertSuccess = false; return 0; }