Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CompareObjectHandles check to run if syscall is available #5749

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/common/peertracker/npipe_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func isCompareObjectHandlesFound() bool {
// compareObjectHandles compares two object handles to determine if they
// refer to the same underlying kernel object
func compareObjectHandles(firstHandle, secondHandle windows.Handle) error {
if isCompareObjectHandlesFound() {
if procCompareObjectHandlesErr != nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see an easy way to unit test compareObjectHandles without refactoring, I just included the one line change in this PR. To make it minimally testable could look something like in the direction of below - changing compareObjectHandles into a method on some type so that anything used by it e.g. syscall.SyscallN could be overridden on the struct used in testing.

// syscaller wraps anything used by `compareObjectHandles` calls
type syscaller struct {
	procCompareObjectHandlesAddr *uintptr // from procCompareObjectHandles.Addr()
	procCompareObjectHandlesErr  error
	syscallN func(trap uintptr, args ...uintptr) (uintptr, uintptr, syscall.Errno)
}

// windowsSyscaller is the only syscaller that should be used at runtime
var windowsSyscaller syscaller

// init sets up the real syscaller
func init() {
	kernelbase := windows.NewLazyDLL("kernelbase.dll")
	procCompareObjectHandles := kernelbase.NewProc("CompareObjectHandles")
	if err := procCompareObjectHandles.Find(); err != nil {
		windowsSyscaller = syscaller{
			procCompareObjectHandlesErr: err,
			syscallN:                    syscall.SyscallN,
		}
	} else {
		syscallAddr := procCompareObjectHandles.Addr()
		windowsSyscaller = syscaller{
			procCompareObjectHandlesAddr: &syscallAddr,
			syscallN:                     syscall.SyscallN,
		}
	}
}

func (s syscaller) isCompareObjectHandlesFound() bool {
	return s.procCompareObjectHandlesAddr == nil
}

// compareObjectHandles compares two object handles to determine if they
// refer to the same underlying kernel object
func (s syscaller) compareObjectHandles(firstHandle, secondHandle windows.Handle) error {
	if s.procCompareObjectHandlesErr != nil {
		return s.procCompareObjectHandlesErr
	}
	r1, _, e1 := s.syscallN(*s.procCompareObjectHandlesAddr, uintptr(firstHandle), uintptr(secondHandle))
	if r1 == 0 {
		return e1
	}
	return nil
}

return procCompareObjectHandlesErr
}
r1, _, e1 := syscall.SyscallN(procCompareObjectHandles.Addr(), uintptr(firstHandle), uintptr(secondHandle))
Expand Down