Commit 44efb1a0 authored by Alexandre Julliard's avatar Alexandre Julliard

ntdll: Allocate environment blocks from the process heap.

parent 911e5084
...@@ -472,12 +472,7 @@ static BOOL build_initial_environment(void) ...@@ -472,12 +472,7 @@ static BOOL build_initial_environment(void)
} }
size *= sizeof(WCHAR); size *= sizeof(WCHAR);
/* Now allocate the environment */ if (!(ptr = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return FALSE;
ptr = NULL;
if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
return FALSE;
NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr; NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr;
endptr = p + size / sizeof(WCHAR); endptr = p + size / sizeof(WCHAR);
......
...@@ -41,6 +41,13 @@ static WCHAR empty[] = {0}; ...@@ -41,6 +41,13 @@ static WCHAR empty[] = {0};
static const UNICODE_STRING empty_str = { 0, sizeof(empty), empty }; static const UNICODE_STRING empty_str = { 0, sizeof(empty), empty };
static const UNICODE_STRING null_str = { 0, 0, NULL }; static const UNICODE_STRING null_str = { 0, 0, NULL };
static inline SIZE_T get_env_length( const WCHAR *env )
{
const WCHAR *end = env;
while (*end) end += strlenW(end) + 1;
return end + 1 - env;
}
/****************************************************************************** /******************************************************************************
* NtQuerySystemEnvironmentValue [NTDLL.@] * NtQuerySystemEnvironmentValue [NTDLL.@]
*/ */
...@@ -68,40 +75,21 @@ NTSYSAPI NTSTATUS WINAPI NtQuerySystemEnvironmentValueEx(PUNICODE_STRING name, L ...@@ -68,40 +75,21 @@ NTSYSAPI NTSTATUS WINAPI NtQuerySystemEnvironmentValueEx(PUNICODE_STRING name, L
*/ */
NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env) NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
{ {
NTSTATUS nts; SIZE_T size;
TRACE("(%u,%p)!\n", inherit, env); TRACE("(%u,%p)!\n", inherit, env);
if (inherit) if (inherit)
{ {
MEMORY_BASIC_INFORMATION mbi;
RtlAcquirePebLock(); RtlAcquirePebLock();
size = get_env_length( NtCurrentTeb()->Peb->ProcessParameters->Environment ) * sizeof(WCHAR);
nts = NtQueryVirtualMemory(NtCurrentProcess(), if ((*env = RtlAllocateHeap( GetProcessHeap(), 0, size )))
NtCurrentTeb()->Peb->ProcessParameters->Environment, memcpy( *env, NtCurrentTeb()->Peb->ProcessParameters->Environment, size );
0, &mbi, sizeof(mbi), NULL);
if (nts == STATUS_SUCCESS)
{
*env = NULL;
nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (nts == STATUS_SUCCESS)
memcpy(*env, NtCurrentTeb()->Peb->ProcessParameters->Environment, mbi.RegionSize);
else *env = NULL;
}
RtlReleasePebLock(); RtlReleasePebLock();
} }
else else *env = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR) );
{
SIZE_T size = 1;
PVOID addr = NULL;
nts = NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (nts == STATUS_SUCCESS) *env = addr;
}
return nts; return *env ? STATUS_SUCCESS : STATUS_NO_MEMORY;
} }
/****************************************************************************** /******************************************************************************
...@@ -109,11 +97,8 @@ NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env) ...@@ -109,11 +97,8 @@ NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
*/ */
NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env) NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
{ {
SIZE_T size = 0; RtlFreeHeap( GetProcessHeap(), 0, env );
return STATUS_SUCCESS;
TRACE("(%p)!\n", env);
return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
} }
static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen) static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
...@@ -205,7 +190,6 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name, ...@@ -205,7 +190,6 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
INT len, old_size; INT len, old_size;
LPWSTR p, env; LPWSTR p, env;
NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND; NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
MEMORY_BASIC_INFORMATION mbi;
TRACE("(%p, %s, %s)\n", penv, debugstr_us(name), debugstr_us(value)); TRACE("(%p, %s, %s)\n", penv, debugstr_us(name), debugstr_us(value));
...@@ -224,9 +208,7 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name, ...@@ -224,9 +208,7 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
env = NtCurrentTeb()->Peb->ProcessParameters->Environment; env = NtCurrentTeb()->Peb->ProcessParameters->Environment;
} else env = *penv; } else env = *penv;
/* compute current size of environment */ old_size = get_env_length( env );
for (p = env; *p; p += strlenW(p) + 1);
old_size = p + 1 - env;
/* Find a place to insert the string */ /* Find a place to insert the string */
for (p = env; *p; p += strlenW(p) + 1) for (p = env; *p; p += strlenW(p) + 1)
...@@ -245,20 +227,16 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name, ...@@ -245,20 +227,16 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR)); memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
} }
nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0, if ((old_size + len) * sizeof(WCHAR) > RtlSizeHeap( GetProcessHeap(), 0, env ))
&mbi, sizeof(mbi), NULL);
if (nts != STATUS_SUCCESS) goto done;
if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
{ {
LPWSTR new_env; SIZE_T new_size = max( old_size * 2, old_size + len ) * sizeof(WCHAR);
SIZE_T new_size = (old_size + len) * sizeof(WCHAR); LPWSTR new_env = RtlAllocateHeap( GetProcessHeap(), 0, new_size );
new_env = NULL;
nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
&new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (nts != STATUS_SUCCESS) goto done;
if (!new_env)
{
nts = STATUS_NO_MEMORY;
goto done;
}
memmove(new_env, env, (p - env) * sizeof(WCHAR)); memmove(new_env, env, (p - env) * sizeof(WCHAR));
assert(len > 0); assert(len > 0);
memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR)); memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
...@@ -282,6 +260,8 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name, ...@@ -282,6 +260,8 @@ NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
memcpy( p, value->Buffer, value->Length ); memcpy( p, value->Buffer, value->Length );
p[value->Length / sizeof(WCHAR)] = 0; p[value->Length / sizeof(WCHAR)] = 0;
} }
nts = STATUS_SUCCESS;
done: done:
if (!penv) RtlReleasePebLock(); if (!penv) RtlReleasePebLock();
...@@ -465,7 +445,6 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu ...@@ -465,7 +445,6 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu
const RTL_USER_PROCESS_PARAMETERS *cur_params; const RTL_USER_PROCESS_PARAMETERS *cur_params;
SIZE_T size, env_size = 0; SIZE_T size, env_size = 0;
void *ptr; void *ptr;
const WCHAR *env;
NTSTATUS status = STATUS_SUCCESS; NTSTATUS status = STATUS_SUCCESS;
RtlAcquirePebLock(); RtlAcquirePebLock();
...@@ -488,13 +467,7 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu ...@@ -488,13 +467,7 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu
if (!ShellInfo) ShellInfo = &empty_str; if (!ShellInfo) ShellInfo = &empty_str;
if (!RuntimeInfo) RuntimeInfo = &null_str; if (!RuntimeInfo) RuntimeInfo = &null_str;
if (Environment) if (Environment) env_size = get_env_length( Environment ) * sizeof(WCHAR);
{
env = Environment;
while (*env) env += strlenW(env) + 1;
env++;
env_size = ROUND_SIZE( (env - Environment) * sizeof(WCHAR) );
}
size = (sizeof(RTL_USER_PROCESS_PARAMETERS) size = (sizeof(RTL_USER_PROCESS_PARAMETERS)
+ ROUND_SIZE( ImagePathName->MaximumLength ) + ROUND_SIZE( ImagePathName->MaximumLength )
...@@ -506,7 +479,7 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu ...@@ -506,7 +479,7 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu
+ ROUND_SIZE( ShellInfo->MaximumLength ) + ROUND_SIZE( ShellInfo->MaximumLength )
+ ROUND_SIZE( RuntimeInfo->MaximumLength )); + ROUND_SIZE( RuntimeInfo->MaximumLength ));
if ((ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, size + env_size ))) if ((ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, size + ROUND_SIZE(env_size) )))
{ {
RTL_USER_PROCESS_PARAMETERS *params = ptr; RTL_USER_PROCESS_PARAMETERS *params = ptr;
params->AllocationSize = size; params->AllocationSize = size;
...@@ -524,8 +497,7 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu ...@@ -524,8 +497,7 @@ NTSTATUS WINAPI RtlCreateProcessParametersEx( RTL_USER_PROCESS_PARAMETERS **resu
append_unicode_string( &ptr, Desktop, &params->Desktop ); append_unicode_string( &ptr, Desktop, &params->Desktop );
append_unicode_string( &ptr, ShellInfo, &params->ShellInfo ); append_unicode_string( &ptr, ShellInfo, &params->ShellInfo );
append_unicode_string( &ptr, RuntimeInfo, &params->RuntimeInfo ); append_unicode_string( &ptr, RuntimeInfo, &params->RuntimeInfo );
if (Environment) if (Environment) params->Environment = memcpy( ptr, Environment, env_size );
params->Environment = memcpy( ptr, Environment, (env - Environment) * sizeof(WCHAR) );
*result = params; *result = params;
if (!(flags & PROCESS_PARAMS_FLAG_NORMALIZED)) RtlDeNormalizeProcessParams( params ); if (!(flags & PROCESS_PARAMS_FLAG_NORMALIZED)) RtlDeNormalizeProcessParams( params );
} }
...@@ -579,9 +551,8 @@ static inline void get_unicode_string( UNICODE_STRING *str, WCHAR **src, UINT le ...@@ -579,9 +551,8 @@ static inline void get_unicode_string( UNICODE_STRING *str, WCHAR **src, UINT le
*/ */
void init_user_process_params( SIZE_T data_size ) void init_user_process_params( SIZE_T data_size )
{ {
void *ptr;
WCHAR *src; WCHAR *src;
SIZE_T info_size, env_size, alloc_size; SIZE_T info_size, env_size;
NTSTATUS status; NTSTATUS status;
startup_info_t *info; startup_info_t *info;
RTL_USER_PROCESS_PARAMETERS *params = NULL; RTL_USER_PROCESS_PARAMETERS *params = NULL;
...@@ -658,13 +629,11 @@ void init_user_process_params( SIZE_T data_size ) ...@@ -658,13 +629,11 @@ void init_user_process_params( SIZE_T data_size )
params->wShowWindow = info->show; params->wShowWindow = info->show;
/* environment needs to be a separate memory block */ /* environment needs to be a separate memory block */
ptr = NULL; if ((params->Environment = RtlAllocateHeap( GetProcessHeap(), 0, max( env_size, sizeof(WCHAR) ))))
alloc_size = max( 1, env_size ); {
status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &alloc_size, if (env_size) memcpy( params->Environment, (char *)info + info_size, env_size );
MEM_COMMIT, PAGE_READWRITE ); else params->Environment[0] = 0;
if (status != STATUS_SUCCESS) goto done; }
memcpy( ptr, (char *)info + info_size, env_size );
params->Environment = ptr;
done: done:
RtlFreeHeap( GetProcessHeap(), 0, info ); RtlFreeHeap( GetProcessHeap(), 0, info );
......
...@@ -2663,6 +2663,7 @@ WINADVAPI BOOL WINAPI SetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR,PS ...@@ -2663,6 +2663,7 @@ WINADVAPI BOOL WINAPI SetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR,PS
WINADVAPI BOOL WINAPI SetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR,PSID,BOOL); WINADVAPI BOOL WINAPI SetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR,PSID,BOOL);
WINADVAPI BOOL WINAPI SetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR,BOOL,PACL,BOOL); WINADVAPI BOOL WINAPI SetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR,BOOL,PACL,BOOL);
WINBASEAPI BOOL WINAPI SetStdHandle(DWORD,HANDLE); WINBASEAPI BOOL WINAPI SetStdHandle(DWORD,HANDLE);
WINBASEAPI BOOL WINAPI SetStdHandleEx(DWORD,HANDLE,HANDLE*);
#define SetSwapAreaSize(w) (w) #define SetSwapAreaSize(w) (w)
WINBASEAPI BOOL WINAPI SetSystemPowerState(BOOL,BOOL); WINBASEAPI BOOL WINAPI SetSystemPowerState(BOOL,BOOL);
WINBASEAPI BOOL WINAPI SetSystemTime(const SYSTEMTIME*); WINBASEAPI BOOL WINAPI SetSystemTime(const SYSTEMTIME*);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment