memory_windows.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // +build windows
  2. package vmem
  3. import (
  4. "errors"
  5. "io"
  6. "reflect"
  7. "sync"
  8. "unsafe"
  9. "golang.org/x/sys/windows"
  10. )
  11. var (
  12. kernel32 = windows.NewLazySystemDLL("kernel32")
  13. kernel32VirtualAlloc = kernel32.NewProc("VirtualAlloc")
  14. kernel32VirtualFree = kernel32.NewProc("VirtualFree")
  15. kernel32VirtualProtect = kernel32.NewProc("VirtualProtect")
  16. kernel32GetNativeSystemInfo = kernel32.NewProc("GetNativeSystemInfo")
  17. kernel32FlushInstructionCache = kernel32.NewProc("FlushInstructionCache")
  18. kernel32GetCurrentProcess = kernel32.NewProc("GetCurrentProcess")
  19. pageSize uint64
  20. pageSizeOnce sync.Once
  21. )
  22. // getCurrentProcess returns the current process handle.
  23. func getCurrentProcess() uintptr {
  24. r, _, _ := kernel32GetCurrentProcess.Call()
  25. return uintptr(r)
  26. }
  27. // GetPageSize returns the size of a memory page.
  28. func GetPageSize() uint64 {
  29. pageSizeOnce.Do(func() {
  30. if kernel32GetNativeSystemInfo.Find() != nil {
  31. pageSize = 0x1000
  32. }
  33. info := systemInfo{}
  34. kernel32GetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&info)))
  35. pageSize = uint64(info.dwPageSize)
  36. })
  37. return pageSize
  38. }
  39. // Memory represents a raw block of memory.
  40. type Memory struct {
  41. data []byte
  42. i int64
  43. }
  44. // Alloc allocates memory at addr of size with allocType and protect.
  45. // It returns nil if it fails.
  46. func Alloc(addr, size uint64, allocType, protect int) *Memory {
  47. r, _, _ := kernel32VirtualAlloc.Call(uintptr(addr), uintptr(size), uintptr(allocType), uintptr(protect))
  48. if r == 0 {
  49. return nil
  50. }
  51. return Get(uint64(r), size)
  52. }
  53. // Get returns a range of existing memory. If the range is not a block of
  54. // allocated memory, the returned memory will pagefault when accessed.
  55. func Get(addr, size uint64) *Memory {
  56. m := &Memory{}
  57. sh := (*reflect.SliceHeader)(unsafe.Pointer(&m.data))
  58. sh.Data = uintptr(addr)
  59. sh.Len = int(size)
  60. sh.Cap = int(size)
  61. return m
  62. }
  63. // Free frees the block of memory.
  64. func (m *Memory) Free() {
  65. sh := (*reflect.SliceHeader)(unsafe.Pointer(&m.data))
  66. kernel32VirtualFree.Call(sh.Data, 0, memRelease)
  67. m.data = nil
  68. }
  69. // Addr returns the actual address of the memory.
  70. func (m *Memory) Addr() uint64 {
  71. return uint64((*reflect.SliceHeader)(unsafe.Pointer(&m.data)).Data)
  72. }
  73. // Read implements the io.Reader interface.
  74. func (m *Memory) Read(b []byte) (n int, err error) {
  75. if m.i >= int64(len(m.data)) {
  76. return 0, io.EOF
  77. }
  78. n = copy(b, m.data[m.i:])
  79. m.i += int64(n)
  80. return n, nil
  81. }
  82. // ReadAt implements the io.ReaderAt interface.
  83. func (m *Memory) ReadAt(b []byte, off int64) (n int, err error) {
  84. if off < 0 {
  85. return 0, errors.New("negative offset")
  86. }
  87. if off >= int64(len(m.data)) {
  88. return 0, io.EOF
  89. }
  90. n = copy(b, m.data[off:])
  91. if n < len(b) {
  92. return n, io.EOF
  93. }
  94. return n, nil
  95. }
  96. // Write implements the io.Writer interface.
  97. func (m *Memory) Write(b []byte) (n int, err error) {
  98. if m.i >= int64(len(m.data)) {
  99. return 0, io.ErrShortWrite
  100. }
  101. n = copy(m.data[m.i:], b)
  102. if kernel32FlushInstructionCache.Find() == nil {
  103. kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[m.i])), uintptr(n))
  104. }
  105. m.i += int64(n)
  106. return n, nil
  107. }
  108. // WriteAt implements the io.WriterAt interface.
  109. func (m *Memory) WriteAt(b []byte, off int64) (n int, err error) {
  110. if off < 0 {
  111. return 0, errors.New("negative offset")
  112. }
  113. if off >= int64(len(m.data)) {
  114. return 0, io.ErrShortWrite
  115. }
  116. n = copy(m.data[off:], b)
  117. if kernel32FlushInstructionCache.Find() == nil {
  118. kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[off])), uintptr(n))
  119. }
  120. if n < len(b) {
  121. return n, io.ErrShortWrite
  122. }
  123. return n, nil
  124. }
  125. // Seek implements the io.Seeker interface.
  126. func (m *Memory) Seek(offset int64, whence int) (int64, error) {
  127. var n int64
  128. switch whence {
  129. case io.SeekStart:
  130. n = offset
  131. case io.SeekCurrent:
  132. n = m.i + offset
  133. case io.SeekEnd:
  134. n = int64(len(m.data)) + offset
  135. default:
  136. return 0, errors.New("invalid whence")
  137. }
  138. if n < 0 {
  139. return 0, errors.New("negative position")
  140. }
  141. m.i = n
  142. return n, nil
  143. }
  144. // Clear sets all bytes in the memory block to zero.
  145. func (m *Memory) Clear() {
  146. for i := range m.data {
  147. m.data[i] = 0
  148. }
  149. if kernel32FlushInstructionCache.Find() == nil {
  150. kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[0])), uintptr(len(m.data)))
  151. }
  152. }
  153. // Protect changes the memory protection for a range of memory.
  154. func (m *Memory) Protect(addr, size uint64, protect int) error {
  155. // TODO: error handling
  156. oldProtect := uint32(0)
  157. kernel32VirtualProtect.Call(uintptr(m.Addr()+addr), uintptr(size), uintptr(protect), uintptr(unsafe.Pointer(&oldProtect)))
  158. return nil
  159. }