using System.Security.Cryptography; using System.Text; using Mono.Cecil; using Mono.Cecil.Cil; /// /// Removes only the pinned scheduler's periodic debug-print block from the /// interrupt path. The tick counter, runtime accounting and scheduling stay intact. /// internal static class PeriodicSchedulerLogPatch { internal const string SchedulerType = "Cosmos.Kernel.Core.Scheduler.Stride.StrideScheduler"; internal const int ChangedInstructionIndex = 63; internal const int ResumeInstructionIndex = 82; internal const string OriginalMethodSha256 = "e1a70ae8c5e8c5bbd17e0255a98f0d40fd1529a3d32cf33cd0fa475dea3d396c"; public static bool Apply(AssemblyDefinition assembly) { if (assembly.Name.Name != "Cosmos.Kernel.Core" || assembly.Name.Version != new Version(3, 0, 94, 0)) throw new InvalidOperationException("Periodic scheduler logging patch requires Cosmos.Kernel.Core 3.0.94."); var type = assembly.MainModule.Types.Single(candidate => candidate.FullName == SchedulerType); var method = type.Methods.Single(candidate => candidate.Name == "OnTick"); var counter = type.Fields.Single(candidate => candidate.Name == "s_onTickLogCount"); if (method.IsStatic || method.ReturnType.MetadataType != MetadataType.Boolean || method.Parameters.Count != 3 || method.Parameters[0].ParameterType.FullName != "Cosmos.Kernel.Core.Scheduler.PerCpuState" || method.Parameters[1].ParameterType.FullName != "Cosmos.Kernel.Core.Scheduler.SchedulerThread" || method.Parameters[2].ParameterType.MetadataType != MetadataType.UInt64 || !method.HasBody || !counter.IsPrivate || !counter.IsStatic || counter.FieldType.MetadataType != MetadataType.UInt32 || method.Body.CodeSize != 269 || method.Body.Instructions.Count != 108) throw new InvalidOperationException("The pinned scheduler timer callback contract changed."); var instructions = method.Body.Instructions; var change = instructions[ChangedInstructionIndex]; var resume = instructions[ResumeInstructionIndex]; bool alreadyPatched = change.OpCode == OpCodes.Br && ReferenceEquals(change.Operand, resume); if (!alreadyPatched && (change.OpCode != OpCodes.Ldsfld || change.Operand is not FieldReference field || field.FullName != counter.FullName)) throw new InvalidOperationException("Unknown periodic scheduler log entry; refusing to patch it."); // Bind the whole method, including every original branch destination and // local type. Normalization accepts exactly the one five-byte replacement. if (HashCanonical(method, counter) != OriginalMethodSha256) throw new InvalidOperationException("The pinned scheduler OnTick IL differs outside the periodic logging patch."); if (alreadyPatched) return true; // ldsfld and br both occupy five bytes. Keep every other instruction, // branch target, local, exception region and metadata entry untouched. change.OpCode = OpCodes.Br; change.Operand = resume; return false; } private static string HashCanonical(MethodDefinition method, FieldReference counter) { var body = method.Body; string canonical = body.CodeSize + "|" + body.MaxStackSize + "|" + body.InitLocals + "|" + body.ExceptionHandlers.Count + "|" + string.Join(";", body.Variables.Select(variable => variable.VariableType.FullName)) + "|" + string.Join("\n", body.Instructions.Select((instruction, index) => index == ChangedInstructionIndex ? Code.Ldsfld + ":" + counter.FullName : instruction.OpCode.Code + ":" + (instruction.Operand switch { Instruction target => "#" + body.Instructions.IndexOf(target), VariableDefinition local => "$" + local.Index, MemberReference member => member.FullName, null => "", var operand => operand.ToString() }))); return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(canonical))); } }