From 6ecf2d235c48edbe57901868ec9a85c73b26234f Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sat, 5 Jan 2013 22:44:30 +0100 Subject: [PATCH] Fix integer overflow in scheduling. --- Core/CoreTiming.cpp | 4 ++-- Core/CoreTiming.h | 32 ++++++++++++++++++-------------- Core/HLE/sceKernelThread.cpp | 3 ++- pspautotests | 2 +- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Core/CoreTiming.cpp b/Core/CoreTiming.cpp index 01f6627a3b..70a9431f5c 100644 --- a/Core/CoreTiming.cpp +++ b/Core/CoreTiming.cpp @@ -194,7 +194,7 @@ u64 GetIdleTicks() // This is to be called when outside threads, such as the graphics thread, wants to // schedule things to be executed on the main thread. -void ScheduleEvent_Threadsafe(int cyclesIntoFuture, int event_type, u64 userdata) +void ScheduleEvent_Threadsafe(s64 cyclesIntoFuture, int event_type, u64 userdata) { std::lock_guard lk(externalEventSection); Event *ne = GetNewTsEvent(); @@ -253,7 +253,7 @@ void AddEventToQueue(Event* ne) // This must be run ONLY from within the cpu thread // cyclesIntoFuture may be VERY inaccurate if called from anything else // than Advance -void ScheduleEvent(int cyclesIntoFuture, int event_type, u64 userdata) +void ScheduleEvent(s64 cyclesIntoFuture, int event_type, u64 userdata) { Event *ne = GetNewEvent(); ne->userdata = userdata; diff --git a/Core/CoreTiming.h b/Core/CoreTiming.h index e5aa8f8772..bed8aa7a46 100644 --- a/Core/CoreTiming.h +++ b/Core/CoreTiming.h @@ -39,31 +39,35 @@ //const int CPU_HZ = 222000000; extern int CPU_HZ; -inline int msToCycles(int ms) { +inline s64 msToCycles(int ms) { return CPU_HZ / 1000 * ms; } -inline int msToCycles(float ms) { - return (int)(CPU_HZ * ms * (0.001f)); +inline s64 msToCycles(float ms) { + return (s64)(CPU_HZ * ms * (0.001f)); } -inline int msToCycles(double ms) { - return (int)(CPU_HZ * ms * (0.001)); +inline s64 msToCycles(double ms) { + return (s64)(CPU_HZ * ms * (0.001)); } -inline int usToCycles(float us) { - return (int)(CPU_HZ * us * (0.000001f)); +inline s64 usToCycles(float us) { + return (s64)(CPU_HZ * us * (0.000001f)); } -inline int usToCycles(int us) { - return (int)(CPU_HZ / 1000000 * us); +inline s64 usToCycles(int us) { + return (CPU_HZ / 1000000 * (s64)us); } -inline u64 usToCycles(u64 us) { - return (u64)(CPU_HZ / 1000000ULL * us); +inline s64 usToCycles(s64 us) { + return (CPU_HZ / 1000000 * us); } -inline u64 cyclesToUs(u64 cycles) { +inline s64 usToCycles(u64 us) { + return (s64)(CPU_HZ / 1000000 * us); +} + +inline s64 cyclesToUs(s64 cycles) { return cycles / (CPU_HZ / 1000000); } @@ -85,8 +89,8 @@ namespace CoreTiming // userdata MAY NOT CONTAIN POINTERS. userdata might get written and reloaded from disk, // when we implement state saves. - void ScheduleEvent(int cyclesIntoFuture, int event_type, u64 userdata=0); - void ScheduleEvent_Threadsafe(int cyclesIntoFuture, int event_type, u64 userdata=0); + void ScheduleEvent(s64 cyclesIntoFuture, int event_type, u64 userdata=0); + void ScheduleEvent_Threadsafe(s64 cyclesIntoFuture, int event_type, u64 userdata=0); void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata=0); u64 UnscheduleEvent(int event_type, u64 userdata); diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 6811048dfd..d75d779269 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -1024,7 +1024,8 @@ void hleScheduledWakeup(u64 userdata, int cyclesLate) void __KernelScheduleWakeup(SceUID threadID, int usFromNow) { - CoreTiming::ScheduleEvent(usToCycles(usFromNow), eventScheduledWakeup, threadID); + s64 cycles = usToCycles(usFromNow); + CoreTiming::ScheduleEvent(cycles, eventScheduledWakeup, threadID); } void __KernelCancelWakeup(SceUID threadID) diff --git a/pspautotests b/pspautotests index 3e41f389df..dddc3db6f1 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit 3e41f389df00ea2aaeb338f8ebad030397386200 +Subproject commit dddc3db6f1b3d18c9aac9bf998225c3bc1b8185a