Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server crash on removal callback called on unloaded plugin #3

Open
FortyTwoFortyTwo opened this issue Jun 3, 2020 · 0 comments
Open

Comments

@FortyTwoFortyTwo
Copy link

FortyTwoFortyTwo commented Jun 3, 2020

Tested in TF2 windows and linux with 2.2.0-detours7, i couldn't get to test whenever non-detour version would still crash but im pretty sure it does as i haven't seen any related changes between it.

I'm getting a lot of these crashes, but this one caught my eyes on how it happened, which it seems like it crashed here.

I have a plugin with several hook removal callback, and another plugin that automatically unloads plugin on map end. From what i can see it crashes when dhook tries to call a removal callback while plugin is being unloaded.

Smallest reproduce i can make:

  • Load below plugin named disabled/42test.smx
  • Join server
  • Change map
    This will unload plugin and dhook attempting to call OnHookRemoved, leading to a crash.
#include <sdktools>
#include <dhooks>

Handle g_hHookGetMaxHealth;

public void OnPluginStart()
{
	GameData hGameData = new GameData("sdkhooks.games");
	if (hGameData == null)
		SetFailState("Could not find sdkhooks.games gamedata");
	
	int iOffset = hGameData.GetOffset("GetMaxHealth");
	g_hHookGetMaxHealth = DHookCreate(iOffset, HookType_Entity, ReturnType_Int, ThisPointer_CBaseEntity);
	if (g_hHookGetMaxHealth == null)
		LogMessage("Failed to create hook: CTFPlayer::GetMaxHealth");
	
	delete hGameData;
	
	//Lateload
	for (int iClient = 1; iClient <= MaxClients; iClient++)
		if (IsClientInGame(iClient))
			OnClientPutInServer(iClient);
}

public void OnMapEnd()
{
	ServerCommand("sm plugins unload disabled/42test");
}

public void OnClientPutInServer(int iClient)
{
	DHookEntity(g_hHookGetMaxHealth, true, iClient, OnHookRemoved, GetMaxHealth);
}

public void OnHookRemoved(int iHookId)
{
}

public MRESReturn GetMaxHealth(int iClient, Handle hReturn)
{
	return MRES_Ignored;
}
FortyTwoFortyTwo added a commit to FortyTwoFortyTwo/Randomizer that referenced this issue Jun 3, 2020
Was having some crashes because of it, more info here Drifter321/DHooks2#3
peace-maker added a commit to peace-maker/DHooks2 that referenced this issue Jun 7, 2020
When a plugin which registered an entity vtable hook with a remove callback is unloaded OnMapEnd, dhooks still tried to call the removal callback on the next frame after the map ended and all entities were destroyed.

Remove the plugin's hooks from the removal list when a plugin is unloaded. The hooks are free'd anyways.

Drifter321#3
peace-maker added a commit to peace-maker/DHooks2 that referenced this issue Jun 17, 2020
The previous fix in ac44af9 did not remove the hook itself instead of only not calling the removal callback. So yes, the removal callback wouldn't be called anymore, but the hook wouldn't be removed causing the normal hook callback to fire after the plugin unloaded.

Drifter321#3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant