From fd587ce6cc086b1ef9e17fc91d6d9af0c02bac26 Mon Sep 17 00:00:00 2001
From: midipix <writeonce@midipix.org>
Date: Tue, 7 Feb 2023 18:21:19 +0000
Subject: __ntapi_tt_spawn_native_process(): propagate the w32_envp array as
 needed.

---
 src/argv/ntapi_tt_argv_envp.c               |  7 ++++
 src/process/ntapi_tt_spawn_native_process.c | 50 +++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/src/argv/ntapi_tt_argv_envp.c b/src/argv/ntapi_tt_argv_envp.c
index 72f1f13..11a9f67 100644
--- a/src/argv/ntapi_tt_argv_envp.c
+++ b/src/argv/ntapi_tt_argv_envp.c
@@ -548,6 +548,13 @@ int32_t __stdcall __ntapi_tt_get_argv_envp_utf16(
 
 			rtdata->envc = (int32_t)(ch_p - rtdata->envp);
 		};
+
+		if (rtdata->w32_envp) {
+			rtdata->w32_envp += (uintptr_t)rtdata / sizeof(char *);
+
+			for (ch_p=rtdata->w32_envp; *ch_p; ch_p++)
+				*ch_p += (uintptr_t)rtdata;
+		};
 	}
 
 	/* we're good */
diff --git a/src/process/ntapi_tt_spawn_native_process.c b/src/process/ntapi_tt_spawn_native_process.c
index 8e0623a..3417e0d 100644
--- a/src/process/ntapi_tt_spawn_native_process.c
+++ b/src/process/ntapi_tt_spawn_native_process.c
@@ -63,6 +63,11 @@ int32_t __stdcall __ntapi_tt_spawn_native_process(nt_spawn_process_params * spar
 	char *				patharg;
 	void *				hat;
 	void *				hfile;
+	char *				src;
+	char *				dst;
+	int				envc;
+	char **				penv;
+	char **				pref;
 	char **				parg;
 	char **				rargv;
 	char **				renvp;
@@ -74,6 +79,8 @@ int32_t __stdcall __ntapi_tt_spawn_native_process(nt_spawn_process_params * spar
 	uint32_t			fsuspended;
 	size_t				buflen;
 	size_t				written;
+	size_t				needed;
+	uintptr_t			addr;
 	char *				raddr;
 	size_t				rsize;
 
@@ -230,6 +237,49 @@ int32_t __stdcall __ntapi_tt_spawn_native_process(nt_spawn_process_params * spar
 	wch    += written/sizeof(wchar16_t);
 	buflen -= written;
 
+	/* w32 environment */
+	if (rtctx->w32_envp) {
+		addr  = (uintptr_t)wch;
+		addr += sizeof(uintptr_t) - 1;
+		addr /= sizeof(uintptr_t);
+		addr *= sizeof(uintptr_t);
+
+		penv   = rtctx->w32_envp;
+		needed = sizeof(char *);
+
+		for (envc=0; *penv; envc++) {
+			needed += sizeof(char *);
+			needed += __ntapi->tt_string_null_offset_multibyte(*penv);
+			penv++;
+		}
+
+		needed += sizeof(uintptr_t) - 1;
+		needed /= sizeof(uintptr_t);
+		needed *= sizeof(uintptr_t);
+
+		if (buflen < needed)
+			return __tt_spawn_return(
+				&rtblock,0,0,NT_STATUS_BUFFER_TOO_SMALL);
+
+		rdata->w32_envp = (char **)(addr - (uintptr_t)rtblock.addr);
+
+		pref = (char **)addr;
+		dst  = (char *)&pref[++envc];
+
+
+		for (penv=rtctx->w32_envp; *penv; penv++) {
+			*pref++ = dst - (uintptr_t)rtblock.addr;
+
+			for (src=*penv; *src; src++,dst++)
+				*dst = *src;
+
+			*dst++ = 0;
+		}
+
+		wch    += needed / sizeof(wchar16_t);
+		buflen -= needed;
+	}
+
 	if (buflen < 0x10000)
 		return __tt_spawn_return(
 			&rtblock,0,0,NT_STATUS_BUFFER_TOO_SMALL);
-- 
cgit v1.2.3