diff --git a/src/posix-compat.c b/src/posix-compat.c index 636c8a5..7a5d809 100644 --- a/src/posix-compat.c +++ b/src/posix-compat.c @@ -54,13 +54,13 @@ struct pipe_handle { #ifdef _WIN32 # define CONTEXT_INIT { .ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS | CONTEXT_FLOATING_POINT } -struct stack_info { +struct region_info { char *ptr; char *base; size_t size; }; -static inline void get_stack_info(struct stack_info *stack) { +static inline void get_memory_info(struct region_info *stack, struct region_info *heap) { CONTEXT context = CONTEXT_INIT; MEMORY_BASIC_INFORMATION mbi; @@ -70,6 +70,13 @@ static inline void get_stack_info(struct stack_info *stack) { VirtualQuery(stack->ptr, &mbi, sizeof (mbi)); stack->base = mbi.BaseAddress; stack->size = mbi.RegionSize; + + void *ptr = malloc(1); + VirtualQuery(ptr, &mbi, sizeof (mbi)); + heap->ptr = NULL; + heap->base = mbi.BaseAddress; + heap->size = mbi.RegionSize; + free(ptr); } static jmp_buf g_jmp; @@ -104,12 +111,19 @@ s_proc_handle *fork_process() { Register(ip, context) = (intptr_t) resume_child; SetThreadContext(info.hThread, &context); - // Copy stack - struct stack_info stack; - get_stack_info(&stack); + // Copy stack, heap + struct region_info stack; + struct region_info heap; + get_memory_info(&stack, &heap); VirtualAllocEx(info.hProcess, stack.base, stack.size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); - WriteProcessMemory(info.hProcess, stack.ptr, stack.ptr, stack.base + stack.size - stack.ptr, NULL); + WriteProcessMemory(info.hProcess, stack.base, stack.base, stack.size, NULL); + + VirtualAllocEx(info.hProcess, heap.base, heap.size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); + WriteProcessMemory(info.hProcess, heap.base, heap.base, heap.size, NULL); + + // Copy jmp_buf + WriteProcessMemory(info.hProcess, &g_jmp, &g_jmp, sizeof (jmp_buf), NULL); ResumeThread(info.hThread); CloseHandle(info.hThread);