From 11cc8a422767653b681c9a85b55c90c198ac901b Mon Sep 17 00:00:00 2001 From: midipix Date: Wed, 24 Jan 2024 02:53:20 +0000 Subject: __ntapi_tt_spawn_native_process(): optimize transient buffer alloc size. --- src/process/ntapi_tt_spawn_native_process.c | 32 ++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/process/ntapi_tt_spawn_native_process.c b/src/process/ntapi_tt_spawn_native_process.c index 56007e1..06a0191 100644 --- a/src/process/ntapi_tt_spawn_native_process.c +++ b/src/process/ntapi_tt_spawn_native_process.c @@ -18,8 +18,9 @@ (NT_PROCESS_SPAWN_FLAG_DEBUG_EXECUTION \ | NT_PROCESS_SPAWN_FLAG_DEBUG_SUSPENDED) -#define __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE (0x800000) #define __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_IMGBUF_SIZE (0x10000) +#define __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_DEF (0x80000) +#define __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_MAX (0x800000) static int32_t __stdcall __tt_spawn_return( nt_runtime_data_block * rtblock, @@ -67,6 +68,7 @@ int32_t __stdcall __ntapi_tt_spawn_native_process(nt_spawn_process_params * spar nt_runtime_data * rdata; nt_unicode_string * imgname; nt_peb * peb; + size_t asize; char * patharg; void * hat; void * hfile; @@ -128,9 +130,33 @@ int32_t __stdcall __ntapi_tt_spawn_native_process(nt_spawn_process_params * spar : &sparams->patharg[0] : 0; + /* quickly determine whether a large buffer is needed */ + for (asize=0,parg=sparams->argv; parg && *parg; asize++) { + asize += __ntapi_tt_string_null_offset_multibyte(*parg++); + + if (asize > (__SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_DEF >> 4)) + parg = 0; + } + + for (penv=sparams->envp; penv && *penv; asize++) { + asize += __ntapi_tt_string_null_offset_multibyte(*penv++); + + if (asize > (__SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_DEF >> 4)) + penv = 0; + } + + if (parg && penv) { + asize += (parg - sparams->argv) * sizeof(char *); + asize += (penv - sparams->envp) * sizeof(char *); + } + + asize = (asize <= (__SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_DEF >> 4)) + ? __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_DEF + : __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE_MAX; + /* rtblock, rdata */ rtblock.addr = 0; - rtblock.size = __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE; + rtblock.size = asize; rtblock.remote_addr = 0; rtblock.remote_size = 0; rtblock.flags = 0; @@ -159,7 +185,7 @@ int32_t __stdcall __ntapi_tt_spawn_native_process(nt_spawn_process_params * spar /* imgbuf */ imgbuf = (wchar16_t *)rtblock.addr; - imgbuf += __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_ALLOC_SIZE / sizeof(*imgbuf); + imgbuf += rtblock.size / sizeof(*imgbuf); imgbuf -= __SPAWN_NATIVE_PROCESS_RUNTIME_BLOCK_IMGBUF_SIZE / sizeof(*imgbuf); /* hfile */ -- cgit v1.2.3