Skip to content

Commit

Permalink
windows: use unsafeheader.Slice
Browse files Browse the repository at this point in the history
  • Loading branch information
rikysya committed May 5, 2020
1 parent 3ce0480 commit 43e62db
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
21 changes: 17 additions & 4 deletions windows/security_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package windows
import (
"syscall"
"unsafe"

"golang.org/x/sys/internal/unsafeheader"
)

const (
Expand Down Expand Up @@ -1230,7 +1232,13 @@ func (sd *SECURITY_DESCRIPTOR) String() string {
return ""
}
defer LocalFree(Handle(unsafe.Pointer(sddl)))
return UTF16PtrToString(sddl)

var utf16s []uint16
h := (*unsafeheader.Slice)(unsafe.Pointer(&utf16s))
h.Data = unsafe.Pointer(sddl)
h.Len = int(strLen)
h.Cap = int(strLen)
return UTF16ToString(utf16s)
}

// ToAbsolute converts a self-relative security descriptor into an absolute one.
Expand Down Expand Up @@ -1308,9 +1316,14 @@ func (absoluteSD *SECURITY_DESCRIPTOR) ToSelfRelative() (selfRelativeSD *SECURIT
}

func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor() *SECURITY_DESCRIPTOR {
sdBytes := make([]byte, selfRelativeSD.Length())
copy(sdBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(selfRelativeSD))[:len(sdBytes):len(sdBytes)])
return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&sdBytes[0]))
var dst, src []byte
dst = make([]byte, selfRelativeSD.Length())
h := (*unsafeheader.Slice)(unsafe.Pointer(&src))
h.Data = unsafe.Pointer(selfRelativeSD)
h.Len = len(dst)
h.Cap = len(dst)
copy(dst, src)
return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&dst[0]))
}

// SecurityDescriptorFromString converts an SDDL string describing a security descriptor into a
Expand Down
9 changes: 8 additions & 1 deletion windows/svc/mgr/mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"unicode/utf16"
"unsafe"

"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -201,7 +202,13 @@ func (m *Mgr) ListServices() ([]string, error) {
if servicesReturned == 0 {
return nil, nil
}
services := (*[1 << 20]windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0]))[:servicesReturned:servicesReturned]

var services []windows.ENUM_SERVICE_STATUS_PROCESS
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&services))
hdr.Data = unsafe.Pointer(&buf[0])
hdr.Len = int(servicesReturned)
hdr.Cap = int(servicesReturned)

var names []string
for _, s := range services {
name := windows.UTF16PtrToString(s.ServiceName)
Expand Down
8 changes: 7 additions & 1 deletion windows/svc/mgr/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
"unsafe"

"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -68,8 +69,13 @@ func (s *Service) RecoveryActions() ([]RecoveryAction, error) {
return nil, err
}

var actions []windows.SC_ACTION
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&actions))
hdr.Data = unsafe.Pointer(p.Actions)
hdr.Len = int(p.ActionsCount)
hdr.Cap = int(p.ActionsCount)

var recoveryActions []RecoveryAction
actions := (*[1024]windows.SC_ACTION)(unsafe.Pointer(p.Actions))[:p.ActionsCount:p.ActionsCount]
for _, action := range actions {
recoveryActions = append(recoveryActions, RecoveryAction{Type: int(action.Type), Delay: time.Duration(action.Delay) * time.Millisecond})
}
Expand Down
9 changes: 8 additions & 1 deletion windows/svc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"syscall"
"unsafe"

"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -224,7 +225,13 @@ const (
func (s *service) run() {
s.goWaits.Wait()
s.h = windows.Handle(ssHandle)
argv := (*[100]*uint16)(unsafe.Pointer(sArgv))[:sArgc:sArgc]

var argv []*uint16
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&argv))
hdr.Data = unsafe.Pointer(sArgv)
hdr.Len = int(sArgc)
hdr.Cap = int(sArgc)

args := make([]string, len(argv))
for i, a := range argv {
args[i] = windows.UTF16PtrToString(a)
Expand Down
31 changes: 13 additions & 18 deletions windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ package windows

import (
errorspkg "errors"
"strings"
"sync"
"syscall"
"time"
"unicode/utf16"
"unsafe"

"golang.org/x/sys/internal/unsafeheader"
)

type Handle uintptr
Expand Down Expand Up @@ -125,25 +126,19 @@ func UTF16PtrToString(p *uint16) string {
if p == nil {
return ""
}

var sb strings.Builder

for r := rune(*p); r != 0; r = rune(*p) {
if !utf16.IsSurrogate(r) {
sb.WriteRune(r)
} else {
p = (*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p)))
sb.WriteRune(utf16.DecodeRune(r, rune(*p)))

if *p == 0 {
break
}
}

p = (*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p)))
// Find NUL terminator.
n := 0
for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ {
ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p))
}

return sb.String()
var s []uint16
h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
h.Data = unsafe.Pointer(p)
h.Len = n
h.Cap = n

return string(utf16.Decode(s))
}

func Getpagesize() int { return 4096 }
Expand Down

0 comments on commit 43e62db

Please sign in to comment.