Skip to content

Commit

Permalink
fix: memory allocate in windows
Browse files Browse the repository at this point in the history
Change-Id: I48df2c97f43e74b164fc724ee572a8aa0b691473
  • Loading branch information
Sychorius committed Aug 22, 2024
1 parent 69f3314 commit 8f2b4e7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
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) {
r1, _, err := virtualAlloc.Call(
0,
uintptr(n),
_MEM_COMMIT|_MEM_RESERVE,
_PAGE_READWRITE,
)
if r1 == 0 || err != nil {
return nil, fmt.Errorf("VirtualAlloc failed: (%d)%w", r1, err)
}
return BytesOf(r1, n), nil
}

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

return nil
}

0 comments on commit 8f2b4e7

Please sign in to comment.