Bug 22360

Summary: WTF current time code uses unprotected static variables
Product: WebKit Reporter: Alexey Proskuryakov <ap>
Component: Web Template FrameworkAssignee: Nobody <webkit-unassigned>
Status: NEW    
Severity: Normal CC: barraclough, darin, kevinwatters, sfalken
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   

Alexey Proskuryakov
Reported 2008-11-19 11:31:17 PST
Recently, a number of static variables were added to JSC::getCurrentUTCTimeWithMicroseconds(). These include doubles, which are not written to memory atomically on common platforms. Likely, this also created race conditions in this function, although I don't have any to pinpoint. I'm fairly sure that this will make JavaScriptCore date/time functions misbehave when used from multiple concurrent threads.
Attachments
Darin Adler
Comment 1 2008-11-19 11:52:33 PST
Since this is Windows-specific code, we can just use Windows thread-specific globals for this. Should be very quick to fix that way.
Darin Adler
Comment 2 2008-11-19 11:53:36 PST
I mean __declspec(thread).
Darin Adler
Comment 3 2008-11-19 12:02:02 PST
Steve mentioned that __declspec(thread) doesn't work for delay-loaded libraries (at least on XP) so we probably need to stay away from it.
Darin Adler
Comment 4 2008-11-19 12:02:55 PST
Generally speaking, for our uses it seems fine for each thread to have its own separate "drift-free" mechanism, so we don't need to introduce locking here -- per-thread data should suffice. I'm contemplating adding JSGlobalData* arguments into these calls and moving the data there.
Alexey Proskuryakov
Comment 5 2008-11-19 12:20:31 PST
Someone just mentioned on IRC that they wanted to move this function to WTF, in order to replace gettimeofday in ThreadingPthreads (I don't know why exactly).
Kevin Watters
Comment 6 2008-11-19 12:47:28 PST
(In reply to comment #5) > Someone just mentioned on IRC that they wanted to move this function to WTF, in > order to replace gettimeofday in ThreadingPthreads (I don't know why exactly). > The reason is that the wx port is switching from ThreadingNone.cpp to ThreadingPthreads.cpp, in which the ThreadCondition::timedWait function needs a high resolution timer to calculate a target time to give to pthread_cond_timedwait. My temporary solution was to implement gettimeofday in that file using the Win32 function GetSystemTimeAsFileTime--but reusing getCurrentUTCTimeWithMicroseconds would work as well if it makes sense to have some time functionality in WTF.
Gavin Barraclough
Comment 7 2012-01-10 12:03:40 PST
Naturally aligned memory accesses are atomic on X86, so if this is windows specific code, it this an issue? Alexey, I can't find a method called getCurrentUTCTimeWithMicroseconds - do you know if this has been renamed? cheers, G.
Darin Adler
Comment 8 2012-01-10 23:13:17 PST
It’s the currentTime() functions in CurrentTime.cpp.
Alexey Proskuryakov
Comment 9 2012-01-11 10:27:36 PST
> Naturally aligned memory accesses are atomic on X86 I didn't know that double accesses were also atomic. I believe x86 also has some guarantees for in-order writes, but code like below still makes me nervous: static bool init = false; static double lastTime; static DWORD lastTickCount; if (!init) { // .. } On most platforms, monotonicallyIncreasingTime() has a whole static mach_timebase_info_data_t variable, e.g. static mach_timebase_info_data_t timebaseInfo; if (!timebaseInfo.denom) {
Alexey Proskuryakov
Comment 10 2012-01-11 10:28:16 PST
> On most platforms s/most platforms/the Mac/
Note You need to log in before you can comment on or make changes to this bug.