Fix x86 float formatting precision (fixes #2562) (#2573)

This commit is contained in:
Nicholas Hastings
2026-09-14 22:38:52 +00:00
committed by GitHub
parent 42237ddc03
commit 7b013d5097
14 changed files with 1127 additions and 4 deletions
+319
View File
@@ -0,0 +1,319 @@
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
public Plugin myinfo =
{
name = "Async File API Test",
author = "AlliedModders",
description = "Manual functional test for the asynchronous file API",
version = "1.0",
url = "https://www.sourcemod.net/"
};
enum TestStep
{
TestStep_CreateDirectory,
TestStep_DirExists,
TestStep_Write,
TestStep_FileExists,
TestStep_FileSize,
TestStep_Read,
TestStep_Copy,
TestStep_Rename,
TestStep_OpenDirectory,
TestStep_DeleteRenamed,
TestStep_DeleteSource,
TestStep_RemoveDirectory,
TestStep_Done,
};
static const char g_Directory[] = "addons/sourcemod/data/async_file_api_test";
static const char g_Source[] = "addons/sourcemod/data/async_file_api_test/source.bin";
static const char g_Copy[] = "addons/sourcemod/data/async_file_api_test/copy.bin";
static const char g_Renamed[] = "addons/sourcemod/data/async_file_api_test/renamed.bin";
static const char g_PathID[] = "DEFAULT_WRITE_PATH";
static int g_Bytes[] = {0x00, 0x01, 0x41, 0x00, 0xff, 0x7f};
static const int g_ByteCount = 6;
static bool g_Running;
static bool g_ValveFS;
static int g_Failures;
static TestStep g_Step;
public void OnPluginStart()
{
RegAdminCmd("sm_async_file_test", Command_RunOS, ADMFLAG_ROOT,
"Run the asynchronous file API test against the OS filesystem");
RegAdminCmd("sm_async_file_test_valve", Command_RunValve, ADMFLAG_ROOT,
"Run the asynchronous file API test against the Valve filesystem");
}
public Action Command_RunOS(int client, int args)
{
StartTest(false);
return Plugin_Handled;
}
public Action Command_RunValve(int client, int args)
{
StartTest(true);
return Plugin_Handled;
}
static void StartTest(bool valveFS)
{
if (g_Running)
{
PrintToServer("[async-file-test] A test is already running.");
return;
}
g_Running = true;
g_ValveFS = valveFS;
g_Failures = 0;
g_Step = TestStep_CreateDirectory;
// Cleanup is deliberately synchronous: it only removes this test's stale
// artifacts before the async test begins.
DeleteFile(g_Source, valveFS, g_PathID);
DeleteFile(g_Copy, valveFS, g_PathID);
DeleteFile(g_Renamed, valveFS, g_PathID);
RemoveDir(g_Directory);
PrintToServer("[async-file-test] Starting %s filesystem test.",
valveFS ? "Valve" : "OS");
RunNextStep();
}
static void RunNextStep()
{
switch (g_Step)
{
case TestStep_CreateDirectory:
{
CreateDirectoryAsync(g_Directory,
FPERM_U_READ | FPERM_U_WRITE | FPERM_U_EXEC |
FPERM_G_READ | FPERM_G_EXEC | FPERM_O_READ | FPERM_O_EXEC,
OnResult, g_Step, g_ValveFS, g_PathID);
}
case TestStep_DirExists:
{
DirExistsAsync(g_Directory, OnExists, g_Step, g_ValveFS, g_PathID);
}
case TestStep_Write:
{
char bytes[6];
for (int i = 0; i < g_ByteCount; i++)
{
bytes[i] = g_Bytes[i];
}
WriteFileAsync(g_Source, bytes, g_ByteCount, OnResult, g_Step,
false, g_ValveFS, g_PathID);
}
case TestStep_FileExists:
{
FileExistsAsync(g_Source, OnExists, g_Step, g_ValveFS, g_PathID);
}
case TestStep_FileSize:
{
FileSizeAsync(g_Source, OnFileSize, g_Step, g_ValveFS, g_PathID);
}
case TestStep_Read:
{
ReadFileAsync(g_Source, OnRead, g_Step, g_ValveFS, g_PathID);
}
case TestStep_Copy:
{
CopyFileAsync(g_Copy, g_Source, OnResult, g_Step, g_ValveFS,
g_PathID, g_PathID);
}
case TestStep_Rename:
{
RenameFileAsync(g_Renamed, g_Copy, OnResult, g_Step, g_ValveFS, g_PathID);
}
case TestStep_OpenDirectory:
{
if (g_ValveFS)
{
PrintToServer("[async-file-test] Skipping Valve directory enumeration (not supported asynchronously).");
Advance();
return;
}
OpenDirectoryAsync(g_Directory, OnDirectory, g_Step);
}
case TestStep_DeleteRenamed:
{
DeleteFileAsync(g_Renamed, OnResult, g_Step, g_ValveFS, g_PathID);
}
case TestStep_DeleteSource:
{
DeleteFileAsync(g_Source, OnResult, g_Step, g_ValveFS, g_PathID);
}
case TestStep_RemoveDirectory:
{
// RemoveDirAsync currently operates on the OS filesystem only.
RemoveDirAsync(g_Directory, OnResult, g_Step);
}
case TestStep_Done:
{
FinishTest();
}
}
}
void OnResult(FileOpResult result, const char[] path, any data)
{
CheckCallbackData(data);
Check(result == FileOp_Success, "operation result");
CheckExpectedPath(path);
Advance();
}
void OnExists(bool exists, const char[] path, any data)
{
CheckCallbackData(data);
Check(exists, "entry exists");
CheckExpectedPath(path);
Advance();
}
void OnFileSize(FileOpResult result, const char[] path, int size, any data)
{
CheckCallbackData(data);
Check(result == FileOp_Success, "file-size result");
Check(size == g_ByteCount, "file size");
CheckExpectedPath(path);
Advance();
}
void OnRead(FileOpResult result, const char[] path, const char[] contents, int size, any data)
{
CheckCallbackData(data);
Check(result == FileOp_Success, "read result");
Check(size == g_ByteCount, "read size");
CheckExpectedPath(path);
bool mismatch = false;
for (int i = 0; i < size && i < g_ByteCount; i++)
{
if ((contents[i] & 0xff) != (g_Bytes[i] & 0xff))
{
PrintToServer("[async-file-test] Read mismatch at byte %d: expected 0x%02X, async 0x%02X.",
i, g_Bytes[i] & 0xff, contents[i] & 0xff);
Check(false, "read byte");
mismatch = true;
}
}
if (mismatch && g_ValveFS)
DiagnoseValveRead();
Advance();
}
void DiagnoseValveRead()
{
File file = OpenFile(g_Source, "rb", true, g_PathID);
if (file == null)
{
PrintToServer("[async-file-test] DIAG: synchronous Valve read could not open '%s'.", g_Source);
return;
}
int bytes[6];
int count = file.Read(bytes, sizeof(bytes), 1);
delete file;
PrintToServer("[async-file-test] DIAG: synchronous Valve read returned %d bytes: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X.",
count, bytes[0] & 0xff, bytes[1] & 0xff, bytes[2] & 0xff,
bytes[3] & 0xff, bytes[4] & 0xff, bytes[5] & 0xff);
}
void OnDirectory(FileOpResult result, const char[] path, DirectoryListing listing, any data)
{
char name[PLATFORM_MAX_PATH];
FileType type;
bool foundSource;
bool foundRenamed;
CheckCallbackData(data);
Check(result == FileOp_Success, "directory result");
CheckExpectedPath(path);
Check(listing != null, "directory listing");
if (listing != null)
{
while (listing.GetNext(name, sizeof(name), type))
{
if (StrEqual(name, "source.bin"))
{
Check(type == FileType_File, "source entry type");
foundSource = true;
}
else if (StrEqual(name, "renamed.bin"))
{
Check(type == FileType_File, "renamed entry type");
foundRenamed = true;
}
}
delete listing;
}
Check(foundSource, "source directory entry");
Check(foundRenamed, "renamed directory entry");
Advance();
}
static void CheckCallbackData(any data)
{
Check(data == g_Step, "callback data");
}
static void CheckExpectedPath(const char[] path)
{
char expected[PLATFORM_MAX_PATH];
switch (g_Step)
{
case TestStep_CreateDirectory, TestStep_DirExists, TestStep_OpenDirectory,
TestStep_RemoveDirectory:
{
strcopy(expected, sizeof(expected), g_Directory);
}
case TestStep_Write, TestStep_FileExists, TestStep_FileSize, TestStep_Read,
TestStep_DeleteSource:
{
strcopy(expected, sizeof(expected), g_Source);
}
case TestStep_Copy:
{
strcopy(expected, sizeof(expected), g_Copy);
}
case TestStep_Rename, TestStep_DeleteRenamed:
{
strcopy(expected, sizeof(expected), g_Renamed);
}
}
Check(StrEqual(path, expected), "callback path");
}
static void Check(bool condition, const char[] name)
{
if (condition)
return;
g_Failures++;
PrintToServer("[async-file-test] FAIL: %s (step %d, %s filesystem)", name,
g_Step, g_ValveFS ? "Valve" : "OS");
}
static void Advance()
{
g_Step++;
RunNextStep();
}
static void FinishTest()
{
PrintToServer("[async-file-test] %s filesystem test %s (%d failure%s).",
g_ValveFS ? "Valve" : "OS", g_Failures ? "FAILED" : "PASSED", g_Failures,
g_Failures == 1 ? "" : "s");
g_Running = false;
}
@@ -0,0 +1,36 @@
#pragma semicolon 1
#include <testing>
#pragma newdecls required
public Plugin myinfo =
{
name = "Forward Free During Call Test",
author = "AlliedModders LLC",
description = "Tests that freeing a private forward from within one of its own callbacks does not crash",
version = "1.0.0.0",
url = "https://www.sourcemod.net/"
};
public void OnPluginStart() {
SetTestContext("Forward Free During Call Test");
Test_FreeForwardDuringOwnCall();
PrintToServer("PASS");
}
void Test_FreeForwardDuringOwnCall() {
Handle fwd = CreateForward(ET_Ignore, Param_Cell);
AddToForward(fwd, INVALID_HANDLE, ForwardCall);
Call_StartForward(fwd);
Call_PushCell(fwd);
Call_Finish();
// If we got here without crashing, the forward was safely destroyed.
}
public void ForwardCall(Handle fwd) {
// Mirrors the NativeVotes pattern that triggered issue #1041: freeing the
// forward's own handle from within one of its own callbacks.
delete fwd;
}
@@ -0,0 +1,266 @@
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
public Plugin myinfo =
{
name = "MySQL Connection Stress Test",
author = "AlliedModders",
description = "Exercises one MySQL connection from synchronous and threaded queries",
version = "1.0",
url = "https://www.sourcemod.net/"
};
static const char g_ConfigName[] = "mysql-stress";
static const int g_DefaultRounds = 5000;
static const int g_AsyncQueriesPerRound = 4;
static Database g_Database;
static DBStatement g_Statement;
static bool g_Running;
static int g_TargetRounds;
static int g_RoundsRun;
static int g_QueriesSubmitted;
static int g_QueriesCompleted;
static int g_ExpectedValue;
static int g_Failures;
public void OnPluginStart()
{
RegServerCmd("sm_mysql_stress", Command_Run,
"Stress a shared MySQL connection. Usage: sm_mysql_stress [rounds]");
}
public Action Command_Run(int args)
{
if (g_Running)
{
PrintToServer("[mysql-stress] A test is already running.");
return Plugin_Handled;
}
int rounds = g_DefaultRounds;
if (args >= 1)
{
char argument[16];
GetCmdArg(1, argument, sizeof(argument));
rounds = StringToInt(argument);
}
if (rounds < 1 || rounds > 100000)
{
PrintToServer("[mysql-stress] Rounds must be between 1 and 100000.");
return Plugin_Handled;
}
StartTest(rounds);
return Plugin_Handled;
}
static void StartTest(int rounds)
{
char error[256];
g_Database = SQL_Connect(g_ConfigName, false, error, sizeof(error));
if (g_Database == null)
{
PrintToServer("[mysql-stress] Connection to '%s' failed: %s", g_ConfigName, error);
return;
}
if (!PrepareDatabase())
{
CloseTestHandles();
return;
}
g_Statement = SQL_PrepareQuery(g_Database,
"UPDATE sm_mysql_connection_stress SET value = value + 1 WHERE id = ?",
error, sizeof(error));
if (g_Statement == null)
{
PrintToServer("[mysql-stress] Failed to prepare statement: %s", error);
CloseTestHandles();
return;
}
g_Running = true;
g_TargetRounds = rounds;
g_RoundsRun = 0;
g_QueriesSubmitted = 0;
g_QueriesCompleted = 0;
g_ExpectedValue = 0;
g_Failures = 0;
PrintToServer("[mysql-stress] Starting %d rounds (%d threaded queries per round).",
g_TargetRounds, g_AsyncQueriesPerRound);
CreateTimer(0.01, Timer_RunRound, _, TIMER_REPEAT);
}
static bool PrepareDatabase()
{
return FastQuery("CREATE TABLE IF NOT EXISTS sm_mysql_connection_stress (id INT PRIMARY KEY, value INT NOT NULL)", "create table")
&& FastQuery("INSERT INTO sm_mysql_connection_stress (id, value) VALUES (1, 0) ON DUPLICATE KEY UPDATE value = 0", "reset table");
}
public Action Timer_RunRound(Handle timer)
{
if (g_RoundsRun == g_TargetRounds)
{
FinishIfComplete();
return Plugin_Stop;
}
for (int i = 0; i < g_AsyncQueriesPerRound; i++)
{
g_Database.Query(OnThreadedQuery,
"SELECT SLEEP(0.002) AS delay, value FROM sm_mysql_connection_stress WHERE id = 1");
g_QueriesSubmitted++;
}
RunSynchronousRound();
g_RoundsRun++;
return Plugin_Continue;
}
static void RunSynchronousRound()
{
char escaped[128];
int written;
if (!SQL_EscapeString(g_Database, "quote ' test", escaped, sizeof(escaped), written)
|| written == 0)
{
ReportDatabaseFailure("escape string", g_Database);
}
if (FastQuery("UPDATE sm_mysql_connection_stress SET value = value + 1 WHERE id = 1", "fast query"))
{
g_ExpectedValue++;
}
DBResultSet result = SQL_Query(g_Database,
"SELECT value FROM sm_mysql_connection_stress WHERE id = 1");
if (result == null || !result.FetchRow())
{
ReportDatabaseFailure("synchronous query", g_Database);
}
delete result;
SQL_BindParamInt(g_Statement, 0, 1);
if (!SQL_Execute(g_Statement))
{
ReportDatabaseFailure("prepared statement", g_Statement);
}
else
{
g_ExpectedValue++;
}
if (g_RoundsRun % 64 == 0)
{
if (!SQL_SetCharset(g_Database, "utf8"))
{
ReportDatabaseFailure("set charset", g_Database);
}
// This verifies that the public recursive lock still works when the
// driver also acquires it internally.
SQL_LockDatabase(g_Database);
bool succeeded = FastQuery(
"UPDATE sm_mysql_connection_stress SET value = value + 1 WHERE id = 1",
"explicit lock query");
SQL_UnlockDatabase(g_Database);
if (succeeded)
{
g_ExpectedValue++;
}
}
}
public void OnThreadedQuery(Database db, DBResultSet results, const char[] error, any data)
{
g_QueriesCompleted++;
if (results == null)
{
ReportFailure("threaded query failed: %s", error);
}
else if (!results.FetchRow())
{
ReportFailure("threaded query returned no row");
}
else
{
results.FetchInt(1);
}
FinishIfComplete();
}
static bool FastQuery(const char[] query, const char[] operation)
{
if (SQL_FastQuery(g_Database, query))
{
return true;
}
ReportDatabaseFailure(operation, g_Database);
return false;
}
static void FinishIfComplete()
{
if (!g_Running || g_RoundsRun != g_TargetRounds || g_QueriesCompleted != g_QueriesSubmitted)
{
return;
}
DBResultSet result = SQL_Query(g_Database,
"SELECT value FROM sm_mysql_connection_stress WHERE id = 1");
if (result == null || !result.FetchRow())
{
ReportDatabaseFailure("final value query", g_Database);
}
else
{
int actual = result.FetchInt(0);
if (actual != g_ExpectedValue)
{
ReportFailure("final value mismatch: expected %d, received %d", g_ExpectedValue, actual);
}
}
delete result;
PrintToServer("[mysql-stress] %s: %d rounds, %d threaded queries, %d failure%s.",
g_Failures == 0 ? "PASS" : "FAIL", g_RoundsRun, g_QueriesCompleted, g_Failures,
g_Failures == 1 ? "" : "s");
g_Running = false;
CloseTestHandles();
}
static void ReportDatabaseFailure(const char[] operation, Handle handle)
{
char error[256];
SQL_GetError(handle, error, sizeof(error));
ReportFailure("%s failed: %s", operation, error);
}
static void ReportFailure(const char[] format, any ...)
{
g_Failures++;
if (g_Failures <= 10)
{
char message[256];
VFormat(message, sizeof(message), format, 2);
PrintToServer("[mysql-stress] FAIL: %s", message);
}
}
static void CloseTestHandles()
{
delete g_Statement;
delete g_Database;
}
@@ -0,0 +1,86 @@
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
public Plugin myinfo =
{
name = "MySQL unload stress test",
author = "AlliedModders",
description = "Verifies that threaded-query unload behavior is safe",
version = "1.0",
url = "https://www.sourcemod.net/"
};
#define DATABASE_CONFIG "mysql-stress"
#define RELOAD_COMMAND "sm plugins reload test_mysql_unload_stress"
ConVar g_TestState;
ConVar g_TestCallback;
ConVar g_TestStartedAt;
public void OnPluginStart()
{
g_TestState = CreateConVar("sm_mysql_unload_stress_state", "0", "Internal state for the MySQL unload stress test.");
g_TestCallback = CreateConVar("sm_mysql_unload_stress_callback", "0", "Internal callback marker for the MySQL unload stress test.");
g_TestStartedAt = CreateConVar("sm_mysql_unload_stress_started_at", "0", "Internal start timestamp for the MySQL unload stress test.");
RegConsoleCmd("sm_mysql_unload_stress", Command_StartStress, "Run the MySQL threaded-query unload stress test.");
if (g_TestState.IntValue == 1)
{
float elapsed = GetEngineTime() - g_TestStartedAt.FloatValue;
if (g_TestCallback.IntValue == 0 && elapsed < 2.0)
{
PrintToServer("[mysql-unload-stress] PASS: discard reloaded promptly and did not run the old callback.");
}
else if (g_TestCallback.IntValue == 1 && elapsed >= 4.0)
{
PrintToServer("[mysql-unload-stress] PASS: wait preserved the historical callback-before-unload behavior.");
}
else
{
LogError("[mysql-unload-stress] FAIL: unexpected unload behavior (callback %d, elapsed %.2f seconds).", g_TestCallback.IntValue, elapsed);
}
g_TestState.IntValue = 0;
}
}
public Action Command_StartStress(int client, int args)
{
if (g_TestState.IntValue != 0)
{
ReplyToCommand(client, "[mysql-unload-stress] A test is already in progress.");
return Plugin_Handled;
}
char error[256];
Database db = SQL_Connect(DATABASE_CONFIG, false, error, sizeof(error));
if (db == null)
{
ReplyToCommand(client, "[mysql-unload-stress] Could not connect: %s", error);
return Plugin_Handled;
}
g_TestState.IntValue = 1;
g_TestCallback.IntValue = 0;
g_TestStartedAt.FloatValue = GetEngineTime();
SQL_TQuery(db, OnSlowQueryComplete, "SELECT SLEEP(5)");
delete db;
CreateTimer(0.25, Timer_ReloadPlugin);
ReplyToCommand(client, "[mysql-unload-stress] Started a five-second query; reloading this plugin shortly.");
return Plugin_Handled;
}
public Action Timer_ReloadPlugin(Handle timer)
{
ServerCommand(RELOAD_COMMAND);
return Plugin_Stop;
}
public void OnSlowQueryComplete(Database db, DBResultSet results, const char[] error, any data)
{
g_TestCallback.IntValue = 1;
PrintToServer("[mysql-unload-stress] The old query callback completed before unload.");
}
@@ -0,0 +1,64 @@
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
bool g_SeenMessages[255];
int g_HookedMessages;
int g_InterceptedMessages;
public Plugin myinfo =
{
name = "User Message Stack Alignment Test",
author = "AlliedModders LLC",
description = "Exercises pre user-message interception callbacks",
version = "1.0.0.0",
url = "https://www.sourcemod.net/"
};
public void OnPluginStart()
{
if (GetEngineVersion() != Engine_CSS)
{
SetFailState("This test is intended for Counter-Strike: Source.");
return;
}
for (int id = 0; id < sizeof(g_SeenMessages); id++)
{
UserMsg message = view_as<UserMsg>(id);
char name[64];
if (!GetUserMessageName(message, name, sizeof(name)))
continue;
HookUserMessage(message, OnUserMessage, true);
g_HookedMessages++;
}
RegServerCmd("sm_test_usermsg_stack_alignment_status", Command_Status);
LogMessage("Hooked %d user messages for pre-interception.", g_HookedMessages);
}
public Action Command_Status(int args)
{
LogMessage("Intercepted %d user messages.", g_InterceptedMessages);
return Plugin_Handled;
}
public Action OnUserMessage(UserMsg message, BfRead buffer, const int[] players,
int playersNum, bool reliable, bool init)
{
int id = view_as<int>(message);
g_InterceptedMessages++;
// Log each message type once so normal game traffic does not flood the log.
if (!g_SeenMessages[id])
{
char name[64];
GetUserMessageName(message, name, sizeof(name));
LogMessage("Intercepted %s (%d) for the first time.", name, id);
g_SeenMessages[id] = true;
}
return Plugin_Continue;
}