mmap_unix.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //go:build darwin || linux
  2. // +build darwin linux
  3. /**
  4. * Copyright 2023 ByteDance Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package loader
  19. import (
  20. `syscall`
  21. )
  22. const (
  23. _AP = syscall.MAP_ANON | syscall.MAP_PRIVATE
  24. _RX = syscall.PROT_READ | syscall.PROT_EXEC
  25. _RW = syscall.PROT_READ | syscall.PROT_WRITE
  26. )
  27. func mmap(nb int) uintptr {
  28. if m, _, e := syscall.RawSyscall6(syscall.SYS_MMAP, 0, uintptr(nb), _RW, _AP, 0, 0); e != 0 {
  29. panic(e)
  30. } else {
  31. return m
  32. }
  33. }
  34. func mprotect(p uintptr, nb int) {
  35. if _, _, err := syscall.RawSyscall(syscall.SYS_MPROTECT, p, uintptr(nb), _RX); err != 0 {
  36. panic(err)
  37. }
  38. }