roflmuffin / CounterStrikeSharp

CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2
https://docs.cssharp.dev
Other
769 stars 119 forks source link

Invoking a native while inside a native callback will result in a preemptive cleanup. #501

Closed Poggicek closed 1 month ago

Poggicek commented 3 months ago

As discussed in https://discord.com/channels/1160907911501991946/1215025384752681081/1215026288151240704 calling a native in any callback called by native c++ will cleanup the global context resources which may be used by the callback itself.

In the case that InvokeNativeInternal happens to loop back to Invoke (calling a native in a native [callbacks]) it shouldn't call the GlobalCleanUp as it will be called as soon as that InvokeNativeInternal finishes, doing so would cleanup any resources (e.g. strings) before the original native finishes.

For example if we have a hook that has a string param and we call any native inside the hook, the string will become corrupted after that native call.

hook(string str)
{
// str is valid

KickPlayer();

// str is free'd [BAD]
}

https://github.com/roflmuffin/CounterStrikeSharp/blob/2eaf7c2d8c7ab0810ed87756ac5f6c1cf6d756e4/managed/CounterStrikeSharp.API/Core/ScriptContext.cs#L100-L105