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>
This commit is contained in:
Jason A. Donenfeld
2019-09-24 09:22:10 +00:00
parent 2aa67d56cd
commit 98129a5cf4
3 changed files with 22 additions and 23 deletions
+12 -2
View File
@@ -178,8 +178,6 @@ func NewCallbackCDecl(fn interface{}) uintptr {
//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 @@ func NewCallbackCDecl(fn interface{}) uintptr {
// 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) {