Skip to content

Commit

Permalink
windows,windows\svc,windows\svc\mgr: use unsafe.Slice instead of unsa…
Browse files Browse the repository at this point in the history
…feheader.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 <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
  • Loading branch information
qmuntal committed Sep 8, 2023
1 parent 0e97d69 commit 4848eb0
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 189 deletions.
30 changes: 0 additions & 30 deletions internal/unsafeheader/unsafeheader.go

This file was deleted.

101 changes: 0 additions & 101 deletions internal/unsafeheader/unsafeheader_test.go

This file was deleted.

21 changes: 6 additions & 15 deletions windows/security_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package windows
import (
"syscall"
"unsafe"

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

const (
Expand Down Expand Up @@ -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]))
}
Expand Down
8 changes: 1 addition & 7 deletions windows/svc/mgr/mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"unicode/utf16"
"unsafe"

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

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions windows/svc/mgr/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"
"unsafe"

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

Expand Down Expand Up @@ -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})
Expand Down
7 changes: 1 addition & 6 deletions windows/svc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"
"unsafe"

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

Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 5 additions & 18 deletions windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"time"
"unicode/utf16"
"unsafe"

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

type Handle uintptr
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 1 addition & 5 deletions windows/syscall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"
"unsafe"

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

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4848eb0

Please sign in to comment.