Skip to content

Commit

Permalink
feat: support go1.22&go1.23
Browse files Browse the repository at this point in the history
Change-Id: I58178d6d6c832f7dcf9a41b9821a6acff9f0a149
  • Loading branch information
Sychorius committed Aug 16, 2024
1 parent a60edcc commit 1843d5f
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 170 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
"1.19",
"1.20",
"1.21",
"1.22",
"1.23",
]
os: [linux] # should be [ macOS, linux, windows ], but currently we don't have macOS and windows runners
arch: [X64, ARM64]
Expand Down
46 changes: 0 additions & 46 deletions internal/monkey/common/mem_above_1_19.go

This file was deleted.

21 changes: 15 additions & 6 deletions internal/monkey/common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package common

import (
"syscall"
"unsafe"

"github.com/bytedance/mockey/internal/tool"
)

var pageSize = uintptr(syscall.Getpagesize())
Expand All @@ -32,12 +33,20 @@ func PageSize() int {
}

func AllocatePage() []byte {
var memStats uint64 = 0
addr := sysAlloc(pageSize, &memStats)
return BytesOf(uintptr(addr), int(pageSize))
page, err := allocate(int(pageSize))
tool.Assert(err == nil, "allocate page failed: %v", err)
return page
}

func ReleasePage(mem []byte) {
memStats := uint64(cap(mem))
sysFree(unsafe.Pointer(PtrOf(mem)), uintptr(cap(mem)), &memStats)
err := free(mem)
tool.Assert(err == nil, "free page failed: %v", err)
}

func allocate(n int) ([]byte, error) {
return syscall.Mmap(-1, 0, int(n), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
}

func free(b []byte) error {
return syscall.Munmap(b)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.21
// +build go1.21
//go:build go1.21 && !go1.22
// +build go1.21,!go1.22

/*
* Copyright 2022 ByteDance Inc.
Expand All @@ -17,19 +17,31 @@
* limitations under the License.
*/

package common
package stw

import (
_ "unsafe"
)

func StopTheWorld() {
const stwForTestResetDebugLog = 16
func newSTWCtx() ctx {
return &stwCtx{}
}

type stwCtx struct {
}

const stwForTestResetDebugLog = 16

func (ctx *stwCtx) StopTheWorld() {
stopTheWorld(stwForTestResetDebugLog)
}

func (ctx *stwCtx) StartTheWorld() {
startTheWorld()
}

//go:linkname stopTheWorld runtime.stopTheWorld
func stopTheWorld(reason uint8)

//go:linkname StartTheWorld runtime.startTheWorld
func StartTheWorld()
//go:linkname startTheWorld runtime.startTheWorld
func startTheWorld()
57 changes: 57 additions & 0 deletions internal/monkey/common/runtime_link/stw/stw_1_22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//go:build go1.22 && !go1.23
// +build go1.22,!go1.23

/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package stw

import (
_ "unsafe"
)

func newSTWCtx() ctx {
return &stwCtx{}
}

type stwCtx struct {
w worldStop
}

const stwForTestResetDebugLog = 16

func (ctx *stwCtx) StopTheWorld() {
ctx.w = stopTheWorld(stwForTestResetDebugLog)
}
func (ctx *stwCtx) StartTheWorld() {
startTheWorld(ctx.w)
}

// stwReason is an enumeration of reasons the world is stopping.
type stwReason uint8

// worldStop provides context from the stop-the-world required by the
// start-the-world.
type worldStop struct {
reason stwReason
start int64
}

//go:linkname stopTheWorld runtime.stopTheWorld
func stopTheWorld(reason stwReason) worldStop

//go:linkname startTheWorld runtime.startTheWorld
func startTheWorld(w worldStop)
59 changes: 59 additions & 0 deletions internal/monkey/common/runtime_link/stw/stw_above_1_22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build go1.23 && mockey_stw
// +build go1.23,mockey_stw

/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package stw

import (
_ "unsafe"
)

func newSTWCtx() ctx {
return &stwCtx{}
}

type stwCtx struct {
w worldStop
}

const stwForTestResetDebugLog = 16

func (ctx *stwCtx) StopTheWorld() {
ctx.w = stopTheWorld(stwForTestResetDebugLog)
}
func (ctx *stwCtx) StartTheWorld() {
startTheWorld(ctx.w)
}

// stwReason is an enumeration of reasons the world is stopping.
type stwReason uint8

// worldStop provides context from the stop-the-world required by the
// start-the-world.
type worldStop struct {
reason stwReason
startedStopping int64
finishedStopping int64
stoppingCPUTime int64
}

//go:linkname stopTheWorld runtime.stopTheWorld
func stopTheWorld(reason stwReason) worldStop

//go:linkname startTheWorld runtime.startTheWorld
func startTheWorld(w worldStop)
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@
* limitations under the License.
*/

package common
package stw

import (
_ "unsafe"
)

func StopTheWorld() {
func newSTWCtx() ctx {
return &stwCtx{}
}

type stwCtx struct {
}

func (ctx *stwCtx) StopTheWorld() {
stopTheWorld("mockey")
}

func (ctx *stwCtx) StartTheWorld() {
startTheWorld()
}

//go:linkname stopTheWorld runtime.stopTheWorld
func stopTheWorld(reason string)

//go:linkname StartTheWorld runtime.startTheWorld
func StartTheWorld()
//go:linkname startTheWorld runtime.startTheWorld
func startTheWorld()
26 changes: 26 additions & 0 deletions internal/monkey/common/runtime_link/stw/stw_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package stw

type ctx interface {
StopTheWorld()
StartTheWorld()
}

func NewSTWCtx() ctx {
return newSTWCtx()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !go1.19
// +build !go1.19
//go:build go1.23 && !mockey_stw
// +build go1.23,!mockey_stw

/*
* Copyright 2022 ByteDance Inc.
Expand All @@ -17,17 +17,21 @@
* limitations under the License.
*/

package common
package stw

import "unsafe"
import (
_ "unsafe"
)

/*
* This may lead to runtime.ReadMemStats inaccurate
* See https://github.com/bytedance/mockey/issues/13
*/
func newSTWCtx() ctx {
return &stwCtx{}
}

type stwCtx struct {
}

//go:linkname sysAlloc runtime.sysAlloc
func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer
func (ctx *stwCtx) StopTheWorld() {
}

//go:linkname sysFree runtime.sysFree
func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64)
func (ctx *stwCtx) StartTheWorld() {
}
6 changes: 4 additions & 2 deletions internal/monkey/mem/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package mem

import (
"github.com/bytedance/mockey/internal/monkey/common"
"github.com/bytedance/mockey/internal/monkey/common/runtime_link/stw"
"github.com/bytedance/mockey/internal/tool"
)

// WriteWithSTW copies data bytes to the target address and replaces the original bytes, during which it will stop the
// world (only the current goroutine's P is running).
func WriteWithSTW(target uintptr, data []byte) {
common.StopTheWorld()
ctx := stw.NewSTWCtx()
ctx.StopTheWorld()

begin := target
end := target + uintptr(len(data))
Expand All @@ -43,5 +45,5 @@ func WriteWithSTW(target uintptr, data []byte) {
break
}

common.StartTheWorld()
ctx.StartTheWorld()
}
Loading

0 comments on commit 1843d5f

Please sign in to comment.