Skip to content

Commit 4848eb0

Browse files
committed
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 <[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]>
1 parent 0e97d69 commit 4848eb0

File tree

8 files changed

+15
-189
lines changed

8 files changed

+15
-189
lines changed

internal/unsafeheader/unsafeheader.go

-30
This file was deleted.

internal/unsafeheader/unsafeheader_test.go

-101
This file was deleted.

windows/security_windows.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package windows
77
import (
88
"syscall"
99
"unsafe"
10-
11-
"golang.org/x/sys/internal/unsafeheader"
1210
)
1311

1412
const (
@@ -1341,21 +1339,14 @@ func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor()
13411339
sdLen = min
13421340
}
13431341

1344-
var src []byte
1345-
h := (*unsafeheader.Slice)(unsafe.Pointer(&src))
1346-
h.Data = unsafe.Pointer(selfRelativeSD)
1347-
h.Len = sdLen
1348-
h.Cap = sdLen
1349-
1342+
src := unsafe.Slice((*byte)(unsafe.Pointer(selfRelativeSD)), sdLen)
1343+
// SECURITY_DESCRIPTOR has pointers in it, which means checkptr expects for it to
1344+
// be aligned properly. When we're copying a Windows-allocated struct to a
1345+
// Go-allocated one, make sure that the Go allocation is aligned to the
1346+
// pointer size.
13501347
const psize = int(unsafe.Sizeof(uintptr(0)))
1351-
1352-
var dst []byte
1353-
h = (*unsafeheader.Slice)(unsafe.Pointer(&dst))
13541348
alloc := make([]uintptr, (sdLen+psize-1)/psize)
1355-
h.Data = (*unsafeheader.Slice)(unsafe.Pointer(&alloc)).Data
1356-
h.Len = sdLen
1357-
h.Cap = sdLen
1358-
1349+
dst := unsafe.Slice((*byte)(unsafe.Pointer(&alloc[0])), sdLen)
13591350
copy(dst, src)
13601351
return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&dst[0]))
13611352
}

windows/svc/mgr/mgr.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"unicode/utf16"
1818
"unsafe"
1919

20-
"golang.org/x/sys/internal/unsafeheader"
2120
"golang.org/x/sys/windows"
2221
)
2322

@@ -199,12 +198,7 @@ func (m *Mgr) ListServices() ([]string, error) {
199198
if servicesReturned == 0 {
200199
return nil, nil
201200
}
202-
203-
var services []windows.ENUM_SERVICE_STATUS_PROCESS
204-
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&services))
205-
hdr.Data = unsafe.Pointer(&buf[0])
206-
hdr.Len = int(servicesReturned)
207-
hdr.Cap = int(servicesReturned)
201+
services := unsafe.Slice((*windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0])), int(servicesReturned))
208202

209203
var names []string
210204
for _, s := range services {

windows/svc/mgr/recovery.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414
"unsafe"
1515

16-
"golang.org/x/sys/internal/unsafeheader"
1716
"golang.org/x/sys/windows"
1817
)
1918

@@ -70,12 +69,7 @@ func (s *Service) RecoveryActions() ([]RecoveryAction, error) {
7069
return nil, err
7170
}
7271

73-
var actions []windows.SC_ACTION
74-
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&actions))
75-
hdr.Data = unsafe.Pointer(p.Actions)
76-
hdr.Len = int(p.ActionsCount)
77-
hdr.Cap = int(p.ActionsCount)
78-
72+
actions := unsafe.Slice(p.Actions, int(p.ActionsCount))
7973
var recoveryActions []RecoveryAction
8074
for _, action := range actions {
8175
recoveryActions = append(recoveryActions, RecoveryAction{Type: int(action.Type), Delay: time.Duration(action.Delay) * time.Millisecond})

windows/svc/service.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sync"
1414
"unsafe"
1515

16-
"golang.org/x/sys/internal/unsafeheader"
1716
"golang.org/x/sys/windows"
1817
)
1918

@@ -222,11 +221,7 @@ func serviceMain(argc uint32, argv **uint16) uintptr {
222221
defer func() {
223222
theService.h = 0
224223
}()
225-
var args16 []*uint16
226-
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&args16))
227-
hdr.Data = unsafe.Pointer(argv)
228-
hdr.Len = int(argc)
229-
hdr.Cap = int(argc)
224+
args16 := unsafe.Slice(argv, int(argc))
230225

231226
args := make([]string, len(args16))
232227
for i, a := range args16 {

windows/syscall_windows.go

+5-18
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"time"
1616
"unicode/utf16"
1717
"unsafe"
18-
19-
"golang.org/x/sys/internal/unsafeheader"
2018
)
2119

2220
type Handle uintptr
@@ -1667,12 +1665,8 @@ func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
16671665

16681666
// Slice returns a uint16 slice that aliases the data in the NTUnicodeString.
16691667
func (s *NTUnicodeString) Slice() []uint16 {
1670-
var slice []uint16
1671-
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice))
1672-
hdr.Data = unsafe.Pointer(s.Buffer)
1673-
hdr.Len = int(s.Length)
1674-
hdr.Cap = int(s.MaximumLength)
1675-
return slice
1668+
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
1669+
return slice[:s.Length]
16761670
}
16771671

16781672
func (s *NTUnicodeString) String() string {
@@ -1695,12 +1689,8 @@ func NewNTString(s string) (*NTString, error) {
16951689

16961690
// Slice returns a byte slice that aliases the data in the NTString.
16971691
func (s *NTString) Slice() []byte {
1698-
var slice []byte
1699-
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice))
1700-
hdr.Data = unsafe.Pointer(s.Buffer)
1701-
hdr.Len = int(s.Length)
1702-
hdr.Cap = int(s.MaximumLength)
1703-
return slice
1692+
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
1693+
return slice[:s.Length]
17041694
}
17051695

17061696
func (s *NTString) String() string {
@@ -1752,10 +1742,7 @@ func LoadResourceData(module, resInfo Handle) (data []byte, err error) {
17521742
if err != nil {
17531743
return
17541744
}
1755-
h := (*unsafeheader.Slice)(unsafe.Pointer(&data))
1756-
h.Data = unsafe.Pointer(ptr)
1757-
h.Len = int(size)
1758-
h.Cap = int(size)
1745+
data = unsafe.Slice((*byte)(unsafe.Pointer(ptr)), size)
17591746
return
17601747
}
17611748

windows/syscall_windows_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"time"
2323
"unsafe"
2424

25-
"golang.org/x/sys/internal/unsafeheader"
2625
"golang.org/x/sys/windows"
2726
)
2827

@@ -865,10 +864,7 @@ func TestSystemModuleVersions(t *testing.T) {
865864
return
866865
}
867866
mods := (*windows.RTL_PROCESS_MODULES)(unsafe.Pointer(&moduleBuffer[0]))
868-
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&modules))
869-
hdr.Data = unsafe.Pointer(&mods.Modules[0])
870-
hdr.Len = int(mods.NumberOfModules)
871-
hdr.Cap = int(mods.NumberOfModules)
867+
modules = unsafe.Slice(&mods.Modules[0], int(mods.NumberOfModules))
872868
break
873869
}
874870
for i := range modules {

0 commit comments

Comments
 (0)