Skip to content

Commit

Permalink
[release-branch.go1.12] runtime: do not use PowerRegisterSuspendResum…
Browse files Browse the repository at this point in the history
…eNotification on systems with "program time" timer

Systems where PowerRegisterSuspendResumeNotification returns ERROR_
FILE_NOT_FOUND are also systems where nanotime() is on "program time"
rather than "real time".  The chain for this is:

powrprof.dll!PowerRegisterSuspendResumeNotification ->
  umpdc.dll!PdcPortOpen ->
    ntdll.dll!ZwAlpcConnectPort("\\PdcPort") ->
      syscall -> ntoskrnl.exe!AlpcpConnectPort

Opening \\.\PdcPort fails with STATUS_OBJECT_NAME_NOT_FOUND when pdc.sys
hasn't been initialized. Pdc.sys also provides the various hooks for
sleep resumption events, which means if it's not loaded, then our "real
time" timer is actually on "program time". Finally STATUS_OBJECT_NAME_
NOT_FOUND is passed through RtlNtStatusToDosError, which returns ERROR_
FILE_NOT_FOUND. Therefore, in the case where the function returns ERROR_
FILE_NOT_FOUND, we don't mind, since the timer we're using will
correspond fine with the lack of sleep resumption notifications. This
applies, for example, to Docker users.

Updates #35447
Updates #35482
Fixes #36377

Change-Id: I9e1ce5bbc54b9da55ff7a3918b5da28112647eee
Reviewed-on: https://go-review.googlesource.com/c/go/+/208317
Reviewed-by: Jason A. Donenfeld <[email protected]>
Reviewed-by: Austin Clements <[email protected]>
Run-TryBot: Jason A. Donenfeld <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-on: https://go-review.googlesource.com/c/go/+/213198
  • Loading branch information
zx2c4 authored and dmitshur committed Jan 3, 2020
1 parent 93f0599 commit e42221d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/runtime/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ func loadOptionalSyscalls() {
}

func monitorSuspendResume() {
const _DEVICE_NOTIFY_CALLBACK = 2
const (
_DEVICE_NOTIFY_CALLBACK = 2
_ERROR_FILE_NOT_FOUND = 2
)
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct {
callback uintptr
context uintptr
Expand All @@ -287,10 +290,20 @@ func monitorSuspendResume() {
callback: compileCallback(*efaceOf(&fn), true),
}
handle := uintptr(0)
if stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
uintptr(unsafe.Pointer(&params)),
uintptr(unsafe.Pointer(&handle))) != 0 {
throw("PowerRegisterSuspendResumeNotification failure")
ret := stdcall3(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK,
uintptr(unsafe.Pointer(&params)), uintptr(unsafe.Pointer(&handle)))
// This function doesn't use GetLastError(), so we use the return value directly.
switch ret {
case 0:
return // Successful, nothing more to do.
case _ERROR_FILE_NOT_FOUND:
// Systems without access to the suspend/resume notifier
// also have their clock on "program time", and therefore
// don't want or need this anyway.
return
default:
println("runtime: PowerRegisterSuspendResumeNotification failed with errno=", ret)
throw("runtime: PowerRegisterSuspendResumeNotification failure")
}
}

Expand Down

0 comments on commit e42221d

Please sign in to comment.