MCPcopy Create free account
hub / github.com/echQoQ/RustSL / alloc_mem

Function alloc_mem

src/alloc_mem/va_syscall.rs:16–50  ·  view source on GitHub ↗
(size: usize)

Source from the content-addressed store, hash-verified

14) -> i32;
15
16pub unsafe fn alloc_mem(size: usize) -> Result<*mut u8, String> {
17 let mut base_address: *mut c_void = ptr::null_mut();
18 let mut region_size: usize = size;
19
20 const MEM_COMMIT: u32 = 0x0000_1000;
21 const MEM_RESERVE: u32 = 0x0000_2000;
22 const PAGE_EXECUTE_READWRITE: u32 = 0x40;
23
24 // Indirect syscall via DInvoke: dynamically build and execute the syscall stub.
25 let function_type: NtAllocateVirtualMemory;
26 #[allow(unused_assignments)]
27 let mut status: Option<i32> = None;
28
29 // NtAllocateVirtualMemory(ProcessHandle = GetCurrentProcess())
30 dinvoke::execute_syscall!(
31 "NtAllocateVirtualMemory",
32 function_type,
33 status,
34 HANDLE(-1isize as isize), // ProcessHandle
35 &mut base_address, // BaseAddress
36 0, // ZeroBits
37 &mut region_size, // RegionSize
38 MEM_COMMIT | MEM_RESERVE, // AllocationType
39 PAGE_EXECUTE_READWRITE, // Protect
40 );
41
42 if status == Some(0) { // STATUS_SUCCESS
43 Ok(base_address as *mut u8)
44 } else {
45 Err(match status {
46 Some(code) => format!("NtAllocateVirtualMemory failed with status: 0x{:x}", code),
47 None => "NtAllocateVirtualMemory syscall execution failed".to_string(),
48 })
49 }
50}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected