github.com/gofiber/fiber/v2@v2.47.0/internal/gopsutil/process/process_windows_386.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package process
     5  
     6  import (
     7  	"syscall"
     8  	"unsafe"
     9  
    10  	"github.com/gofiber/fiber/v2/internal/gopsutil/common"
    11  )
    12  
    13  type PROCESS_MEMORY_COUNTERS struct {
    14  	CB                         uint32
    15  	PageFaultCount             uint32
    16  	PeakWorkingSetSize         uint32
    17  	WorkingSetSize             uint32
    18  	QuotaPeakPagedPoolUsage    uint32
    19  	QuotaPagedPoolUsage        uint32
    20  	QuotaPeakNonPagedPoolUsage uint32
    21  	QuotaNonPagedPoolUsage     uint32
    22  	PagefileUsage              uint32
    23  	PeakPagefileUsage          uint32
    24  }
    25  
    26  func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) uint64 {
    27  	if is32BitProcess {
    28  		//we are on a 32-bit process reading an external 32-bit process
    29  		var info processBasicInformation32
    30  
    31  		ret, _, _ := common.ProcNtQueryInformationProcess.Call(
    32  			uintptr(procHandle),
    33  			uintptr(common.ProcessBasicInformation),
    34  			uintptr(unsafe.Pointer(&info)),
    35  			uintptr(unsafe.Sizeof(info)),
    36  			uintptr(0),
    37  		)
    38  		if int(ret) >= 0 {
    39  			return uint64(info.PebBaseAddress)
    40  		}
    41  	} else {
    42  		//we are on a 32-bit process reading an external 64-bit process
    43  		if common.ProcNtWow64QueryInformationProcess64.Find() == nil { //avoid panic
    44  			var info processBasicInformation64
    45  
    46  			ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call(
    47  				uintptr(procHandle),
    48  				uintptr(common.ProcessBasicInformation),
    49  				uintptr(unsafe.Pointer(&info)),
    50  				uintptr(unsafe.Sizeof(info)),
    51  				uintptr(0),
    52  			)
    53  			if int(ret) >= 0 {
    54  				return info.PebBaseAddress
    55  			}
    56  		}
    57  	}
    58  
    59  	//return 0 on error
    60  	return 0
    61  }
    62  
    63  func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte {
    64  	if is32BitProcess {
    65  		var read uint
    66  
    67  		buffer := make([]byte, size)
    68  
    69  		ret, _, _ := common.ProcNtReadVirtualMemory.Call(
    70  			uintptr(h),
    71  			uintptr(address),
    72  			uintptr(unsafe.Pointer(&buffer[0])),
    73  			uintptr(size),
    74  			uintptr(unsafe.Pointer(&read)),
    75  		)
    76  		if int(ret) >= 0 && read > 0 {
    77  			return buffer[:read]
    78  		}
    79  	} else {
    80  		//reading a 64-bit process from a 32-bit one
    81  		if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { //avoid panic
    82  			var read uint64
    83  
    84  			buffer := make([]byte, size)
    85  
    86  			ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call(
    87  				uintptr(h),
    88  				uintptr(address&0xFFFFFFFF), //the call expects a 64-bit value
    89  				uintptr(address>>32),
    90  				uintptr(unsafe.Pointer(&buffer[0])),
    91  				uintptr(size), //the call expects a 64-bit value
    92  				uintptr(0),    //but size is 32-bit so pass zero as the high dword
    93  				uintptr(unsafe.Pointer(&read)),
    94  			)
    95  			if int(ret) >= 0 && read > 0 {
    96  				return buffer[:uint(read)]
    97  			}
    98  		}
    99  	}
   100  
   101  	//if we reach here, an error happened
   102  	return nil
   103  }