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: memory allocate in windows #64

Merged
merged 1 commit into from
Sep 3, 2024
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/smartystreets/goconvey v1.6.4
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff
golang.org/x/sys v0.24.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5P
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
8 changes: 0 additions & 8 deletions internal/monkey/common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,3 @@ func ReleasePage(mem []byte) {
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)
}
32 changes: 32 additions & 0 deletions internal/monkey/common/page_alloc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !windows
// +build !windows

/*
* 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 common

import (
"syscall"
)

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)
}
68 changes: 68 additions & 0 deletions internal/monkey/common/page_alloc_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build windows
// +build windows

/*
* 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 common

import (
"fmt"

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

const (
_MEM_COMMIT = 0x1000
_MEM_RESERVE = 0x2000
_MEM_DECOMMIT = 0x4000

_PAGE_READWRITE = 0x0004
)

var virtualAlloc, virtualFree *windows.LazyProc

func init() {
kernel32 := windows.NewLazySystemDLL("kernel32.dll")
virtualAlloc = kernel32.NewProc("VirtualAlloc")
virtualFree = kernel32.NewProc("VirtualFree")
}

func allocate(n int) ([]byte, error) {
ptr, _, _ := virtualAlloc.Call(
0,
uintptr(n),
_MEM_COMMIT|_MEM_RESERVE,
_PAGE_READWRITE,
)
if ptr == 0 {
return nil, fmt.Errorf("VirtualAlloc failed: %d", ptr)
}
return BytesOf(ptr, n), nil
}

func free(b []byte) error {
res, _, err := virtualFree.Call(
PtrOf(b),
uintptr(len(b)),
_MEM_DECOMMIT,
)
if res == 0 {
return fmt.Errorf("VirtualFree failed: (%d)%w", res, err)
}

return nil
}
44 changes: 44 additions & 0 deletions internal/monkey/mem/write_darwin_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build linux || darwin
// +build linux darwin

package mem

// 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 (
"syscall"
"testing"

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

func Test_write(t *testing.T) {
target := make([]byte, 1024)
for i := 0; i < len(target); i++ {
target[i] = 0x00
}
data := make([]byte, 512)
for i := 0; i < len(data); i++ {
data[i] = 0xaa + byte(i)
}

res := write(common.PtrOf(target), common.PtrOf(data), 3, common.PageOf(common.PtrOf(target)), common.PageSize(), syscall.PROT_READ|syscall.PROT_WRITE)
tool.Assert(res == 0, "assert fail")
tool.Assert(target[0] == 0xaa, "assert fail, target0 %x", target[0])
tool.Assert(target[1] == 0xab, "assert fail, target1 %x", target[1])
tool.Assert(target[2] == 0xac, "assert fail, target2 %x", target[2])
tool.Assert(target[3] == 0x00, "assert fail, target3 %x", target[3])
}
19 changes: 0 additions & 19 deletions internal/monkey/mem/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package mem
// limitations under the License.

import (
"syscall"
"testing"

"github.com/bytedance/mockey/internal/monkey/common"
Expand Down Expand Up @@ -60,21 +59,3 @@ func TestWrite(t *testing.T) {
break
}
}

func Test_write(t *testing.T) {
target := make([]byte, 1024)
for i := 0; i < len(target); i++ {
target[i] = 0x00
}
data := make([]byte, 512)
for i := 0; i < len(data); i++ {
data[i] = 0xaa + byte(i)
}

res := write(common.PtrOf(target), common.PtrOf(data), 3, common.PageOf(common.PtrOf(target)), common.PageSize(), syscall.PROT_READ|syscall.PROT_WRITE)
tool.Assert(res == 0, "assert fail")
tool.Assert(target[0] == 0xaa, "assert fail, target0 %x", target[0])
tool.Assert(target[1] == 0xab, "assert fail, target1 %x", target[1])
tool.Assert(target[2] == 0xac, "assert fail, target2 %x", target[2])
tool.Assert(target[3] == 0x00, "assert fail, target3 %x", target[3])
}
Loading