windows: use pseudo handle constants to implement GetCurrentProcess

There's no point in adding a function call to retrieve a constant, or
worse, a syscall to retrieve a constant. These are fixed and baked so
deep into NT they'll never change. So let's benefit from the obvious
optimization and make these constants. Go easily inlines the function
calls as well. We also take the opportunity to sunset
OpenCurrentProcessToken and restore its original behavior, since users
should be invoking this deliberately with the correct access mask.

Change-Id: I92f7de56c0fcf5b69b59f5a79d2828c7ddf3c8f6
Reviewed-on: https://go-review.googlesource.com/c/sys/+/196800
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
diff --git a/windows/security_windows.go b/windows/security_windows.go
index 308d484..f5e725a 100644
--- a/windows/security_windows.go
+++ b/windows/security_windows.go
@@ -647,13 +647,16 @@
 // system-related operations on the local computer.
 type Token Handle
 
-// OpenCurrentProcessToken opens the access token
-// associated with current process. It is a real
-// token that needs to be closed, unlike
-// GetCurrentProcessToken.
-func OpenCurrentProcessToken() (token Token, err error) {
-	err = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &token)
-	return
+// OpenCurrentProcessToken opens an access token associated with current
+// process with TOKEN_QUERY access. It is a real token that needs to be closed.
+//
+// Deprecated: Explicitly call OpenProcessToken(GetCurrentProcess(), ...)
+// with the desired access instead, or use GetCurrentProcessToken for a
+// TOKEN_QUERY token.
+func OpenCurrentProcessToken() (Token, error) {
+	var token Token
+	err := OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)
+	return token, err
 }
 
 // GetCurrentProcessToken returns the access token associated with
diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go
index 47bde87..5734645 100644
--- a/windows/syscall_windows.go
+++ b/windows/syscall_windows.go
@@ -178,8 +178,6 @@
 //sys	TerminateProcess(handle Handle, exitcode uint32) (err error)
 //sys	GetExitCodeProcess(handle Handle, exitcode *uint32) (err error)
 //sys	GetStartupInfo(startupInfo *StartupInfo) (err error) = GetStartupInfoW
-//sys	GetCurrentProcess() (pseudoHandle Handle)
-//sys	GetCurrentThread() (pseudoHandle Handle)
 //sys	GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error)
 //sys	DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error)
 //sys	WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff]
@@ -309,6 +307,18 @@
 
 // syscall interface implementation for other packages
 
+// GetCurrentProcess returns the handle for the current process.
+// It is a pseudo handle that does not need to be closed.
+func GetCurrentProcess() Handle {
+	return Handle(^uintptr(1 - 1))
+}
+
+// GetCurrentThread returns the handle for the current thread.
+// It is a pseudo handle that does not need to be closed.
+func GetCurrentThread() Handle {
+	return Handle(^uintptr(2 - 1))
+}
+
 // GetProcAddressByOrdinal retrieves the address of the exported
 // function from module by ordinal.
 func GetProcAddressByOrdinal(module Handle, ordinal uintptr) (proc uintptr, err error) {
diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go
index d0b74bf..8008c03 100644
--- a/windows/zsyscall_windows.go
+++ b/windows/zsyscall_windows.go
@@ -119,8 +119,6 @@
 	procTerminateProcess                                     = modkernel32.NewProc("TerminateProcess")
 	procGetExitCodeProcess                                   = modkernel32.NewProc("GetExitCodeProcess")
 	procGetStartupInfoW                                      = modkernel32.NewProc("GetStartupInfoW")
-	procGetCurrentProcess                                    = modkernel32.NewProc("GetCurrentProcess")
-	procGetCurrentThread                                     = modkernel32.NewProc("GetCurrentThread")
 	procGetProcessTimes                                      = modkernel32.NewProc("GetProcessTimes")
 	procDuplicateHandle                                      = modkernel32.NewProc("DuplicateHandle")
 	procWaitForSingleObject                                  = modkernel32.NewProc("WaitForSingleObject")
@@ -1209,18 +1207,6 @@
 	return
 }
 
-func GetCurrentProcess() (pseudoHandle Handle) {
-	r0, _, _ := syscall.Syscall(procGetCurrentProcess.Addr(), 0, 0, 0, 0)
-	pseudoHandle = Handle(r0)
-	return
-}
-
-func GetCurrentThread() (pseudoHandle Handle) {
-	r0, _, _ := syscall.Syscall(procGetCurrentThread.Addr(), 0, 0, 0, 0)
-	pseudoHandle = Handle(r0)
-	return
-}
-
 func GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error) {
 	r1, _, e1 := syscall.Syscall6(procGetProcessTimes.Addr(), 5, uintptr(handle), uintptr(unsafe.Pointer(creationTime)), uintptr(unsafe.Pointer(exitTime)), uintptr(unsafe.Pointer(kernelTime)), uintptr(unsafe.Pointer(userTime)), 0)
 	if r1 == 0 {