windows,windows\svc,windows\svc\mgr: use unsafe.Slice instead of unsafeheader.Slice

unsafe.Slice is available since Go 1.17, which is already the minimum
version supported by this package.

This change removes the dependency on the internal unsafeheader package,
which can be removed from the module.

Change-Id: I6c34cb152f2336ea04c5f9c7e88797ed8914f9cc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/526635
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
qmuntal
2023-09-08 18:06:11 +00:00
committed by Quim Muntal
parent 0e97d69659
commit 4848eb0479
8 changed files with 15 additions and 189 deletions
+6 -15
View File
@@ -7,8 +7,6 @@ package windows
import (
"syscall"
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
)
const (
@@ -1341,21 +1339,14 @@ func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor()
sdLen = min
}
var src []byte
h := (*unsafeheader.Slice)(unsafe.Pointer(&src))
h.Data = unsafe.Pointer(selfRelativeSD)
h.Len = sdLen
h.Cap = sdLen
src := unsafe.Slice((*byte)(unsafe.Pointer(selfRelativeSD)), sdLen)
// SECURITY_DESCRIPTOR has pointers in it, which means checkptr expects for it to
// be aligned properly. When we're copying a Windows-allocated struct to a
// Go-allocated one, make sure that the Go allocation is aligned to the
// pointer size.
const psize = int(unsafe.Sizeof(uintptr(0)))
var dst []byte
h = (*unsafeheader.Slice)(unsafe.Pointer(&dst))
alloc := make([]uintptr, (sdLen+psize-1)/psize)
h.Data = (*unsafeheader.Slice)(unsafe.Pointer(&alloc)).Data
h.Len = sdLen
h.Cap = sdLen
dst := unsafe.Slice((*byte)(unsafe.Pointer(&alloc[0])), sdLen)
copy(dst, src)
return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&dst[0]))
}
+1 -7
View File
@@ -17,7 +17,6 @@ import (
"unicode/utf16"
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)
@@ -199,12 +198,7 @@ func (m *Mgr) ListServices() ([]string, error) {
if servicesReturned == 0 {
return nil, nil
}
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)
services := unsafe.Slice((*windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0])), int(servicesReturned))
var names []string
for _, s := range services {
+1 -7
View File
@@ -13,7 +13,6 @@ import (
"time"
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)
@@ -70,12 +69,7 @@ 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)
actions := unsafe.Slice(p.Actions, int(p.ActionsCount))
var recoveryActions []RecoveryAction
for _, action := range actions {
recoveryActions = append(recoveryActions, RecoveryAction{Type: int(action.Type), Delay: time.Duration(action.Delay) * time.Millisecond})
+1 -6
View File
@@ -13,7 +13,6 @@ import (
"sync"
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)
@@ -222,11 +221,7 @@ func serviceMain(argc uint32, argv **uint16) uintptr {
defer func() {
theService.h = 0
}()
var args16 []*uint16
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&args16))
hdr.Data = unsafe.Pointer(argv)
hdr.Len = int(argc)
hdr.Cap = int(argc)
args16 := unsafe.Slice(argv, int(argc))
args := make([]string, len(args16))
for i, a := range args16 {
+5 -18
View File
@@ -15,8 +15,6 @@ import (
"time"
"unicode/utf16"
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
)
type Handle uintptr
@@ -1667,12 +1665,8 @@ func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
// Slice returns a uint16 slice that aliases the data in the NTUnicodeString.
func (s *NTUnicodeString) Slice() []uint16 {
var slice []uint16
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice))
hdr.Data = unsafe.Pointer(s.Buffer)
hdr.Len = int(s.Length)
hdr.Cap = int(s.MaximumLength)
return slice
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
return slice[:s.Length]
}
func (s *NTUnicodeString) String() string {
@@ -1695,12 +1689,8 @@ func NewNTString(s string) (*NTString, error) {
// Slice returns a byte slice that aliases the data in the NTString.
func (s *NTString) Slice() []byte {
var slice []byte
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice))
hdr.Data = unsafe.Pointer(s.Buffer)
hdr.Len = int(s.Length)
hdr.Cap = int(s.MaximumLength)
return slice
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
return slice[:s.Length]
}
func (s *NTString) String() string {
@@ -1752,10 +1742,7 @@ func LoadResourceData(module, resInfo Handle) (data []byte, err error) {
if err != nil {
return
}
h := (*unsafeheader.Slice)(unsafe.Pointer(&data))
h.Data = unsafe.Pointer(ptr)
h.Len = int(size)
h.Cap = int(size)
data = unsafe.Slice((*byte)(unsafe.Pointer(ptr)), size)
return
}
+1 -5
View File
@@ -22,7 +22,6 @@ import (
"time"
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
"golang.org/x/sys/windows"
)
@@ -865,10 +864,7 @@ func TestSystemModuleVersions(t *testing.T) {
return
}
mods := (*windows.RTL_PROCESS_MODULES)(unsafe.Pointer(&moduleBuffer[0]))
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&modules))
hdr.Data = unsafe.Pointer(&mods.Modules[0])
hdr.Len = int(mods.NumberOfModules)
hdr.Cap = int(mods.NumberOfModules)
modules = unsafe.Slice(&mods.Modules[0], int(mods.NumberOfModules))
break
}
for i := range modules {