Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic when write code cross-page #46

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions internal/monkey/mem/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,23 @@ import (
// world (only the current goroutine's P is running).
func WriteWithSTW(target uintptr, data []byte) {
common.StopTheWorld()
defer common.StartTheWorld()
err := Write(target, data)
tool.Assert(err == nil, err)

begin := target
end := target + uintptr(len(data))
for begin < end {
if common.PageOf(begin) < common.PageOf(end) {
nextPage := common.PageOf(begin) + uintptr(common.PageSize())
buf := data[:nextPage-begin]
data = data[nextPage-begin:]
err := Write(begin, buf)
tool.Assert(err == nil, err)
begin += uintptr(len(buf))
continue
}
err := Write(begin, data)
tool.Assert(err == nil, err)
break
}

common.StartTheWorld()
}
61 changes: 61 additions & 0 deletions internal/monkey/mem/write_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package mem
Sychorius marked this conversation as resolved.
Show resolved Hide resolved

// Copyright 2023 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.

import (
"testing"

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

func TestWrite(t *testing.T) {
data := make([]byte, common.PageSize()*3)
target := uintptr(common.PageSize() / 2)

expected := [][2]uintptr{
{target, uintptr(common.PageSize())},
{uintptr(common.PageSize()), uintptr(common.PageSize()) * 2},
{uintptr(common.PageSize() * 2), uintptr(common.PageSize()) * 3},
{uintptr(common.PageSize() * 3), target + uintptr(len(data))},
}

begin := target
end := target + uintptr(len(data))
index := 0
for begin < end {
if common.PageOf(begin) < common.PageOf(end) {
nextPage := common.PageOf(begin) + uintptr(common.PageSize())
buf := data[:nextPage-begin]
data = data[nextPage-begin:]

// err := Write(begin, buf)
// tool.Assert(err == nil, err)
tool.Assert(begin == expected[index][0], index, begin, expected[index][0])
tool.Assert(begin+uintptr(len(buf)) == expected[index][1], index, begin+uintptr(len(buf)), expected[index][1])

begin += uintptr(len(buf))
index += 1
continue
}

// err := Write(begin, data)
// tool.Assert(err == nil, err)
tool.Assert(begin == expected[index][0], index, begin, expected[index][0])
tool.Assert(begin+uintptr(len(data)) == expected[index][1], index, begin+uintptr(len(data)), expected[index][1])

break
}
}
Loading