authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-18 16:22:19-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-18 16:22:19-05:00
logd93edadead45e447e6bc16c0934a3031a06d0fd8
tree1d76a57f0f5f866ccf6640f137712b7e105ee759
parent6ed0910d6d974750fe65990983d5347ceacc0186
parent0cbc59f2274663b9d956170fff3bf7f477b2b3cf
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13993 from squeek502/windows-childprocess-perf

`spawnWindows`: Improve worst-case performance considerably + tests

8 files changed, 589 insertions(+), 95 deletions(-)

lib/std/child_process.zig+365-82
...@@ -946,109 +946,105 @@ pub const ChildProcess = struct {...@@ -946,109 +946,105 @@ pub const ChildProcess = struct {
946 defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf);946 defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf);
947 const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null;947 const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null;
948948
949 const app_name_utf8 = self.argv[0];
950 const app_name_is_absolute = fs.path.isAbsolute(app_name_utf8);
951
949 // the cwd set in ChildProcess is in effect when choosing the executable path952 // the cwd set in ChildProcess is in effect when choosing the executable path
950 // to match posix semantics953 // to match posix semantics
951 const app_path = x: {954 var cwd_path_w_needs_free = false;
952 if (self.cwd) |cwd| {955 const cwd_path_w = x: {
953 const resolved = try fs.path.resolve(self.allocator, &[_][]const u8{ cwd, self.argv[0] });956 // If the app name is absolute, then we need to use its dirname as the cwd
954 defer self.allocator.free(resolved);957 if (app_name_is_absolute) {
955 break :x try cstr.addNullByte(self.allocator, resolved);958 cwd_path_w_needs_free = true;
959 const dir = fs.path.dirname(app_name_utf8).?;
960 break :x try unicode.utf8ToUtf16LeWithNull(self.allocator, dir);
961 } else if (self.cwd) |cwd| {
962 cwd_path_w_needs_free = true;
963 break :x try unicode.utf8ToUtf16LeWithNull(self.allocator, cwd);
956 } else {964 } else {
957 break :x try cstr.addNullByte(self.allocator, self.argv[0]);965 break :x &[_:0]u16{}; // empty for cwd
966 }
967 };
968 defer if (cwd_path_w_needs_free) self.allocator.free(cwd_path_w);
969
970 // If the app name has more than just a filename, then we need to separate that
971 // into the basename and dirname and use the dirname as an addition to the cwd
972 // path. This is because NtQueryDirectoryFile cannot accept FileName params with
973 // path separators.
974 const app_basename_utf8 = fs.path.basename(app_name_utf8);
975 // If the app name is absolute, then the cwd will already have the app's dirname in it,
976 // so only populate app_dirname if app name is a relative path with > 0 path separators.
977 const maybe_app_dirname_utf8 = if (!app_name_is_absolute) fs.path.dirname(app_name_utf8) else null;
978 const app_dirname_w: ?[:0]u16 = x: {
979 if (maybe_app_dirname_utf8) |app_dirname_utf8| {
980 break :x try unicode.utf8ToUtf16LeWithNull(self.allocator, app_dirname_utf8);
958 }981 }
982 break :x null;
959 };983 };
960 defer self.allocator.free(app_path);984 defer if (app_dirname_w != null) self.allocator.free(app_dirname_w.?);
961985
962 const app_path_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, app_path);986 const app_name_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, app_basename_utf8);
963 defer self.allocator.free(app_path_w);987 defer self.allocator.free(app_name_w);
964988
965 const cmd_line_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, cmd_line);989 const cmd_line_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, cmd_line);
966 defer self.allocator.free(cmd_line_w);990 defer self.allocator.free(cmd_line_w);
967991
968 exec: {992 exec: {
969 windowsCreateProcess(app_path_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| {993 const PATH: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATH")) orelse &[_:0]u16{};
970 switch (no_path_err) {994 const PATHEXT: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATHEXT")) orelse &[_:0]u16{};
971 error.FileNotFound, error.InvalidExe => {},
972 else => |e| return e,
973 }
974995
975 const PATH: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATH")) orelse &[_:0]u16{};996 var app_buf = std.ArrayListUnmanaged(u16){};
976 const PATHEXT: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATHEXT")) orelse &[_:0]u16{};997 defer app_buf.deinit(self.allocator);
977
978 var path_buf = std.ArrayListUnmanaged(u16){};
979 defer path_buf.deinit(self.allocator);
980
981 // Try again with PATHEXT's extensions appended
982 {
983 try path_buf.appendSlice(self.allocator, app_path_w);
984 var ext_it = mem.tokenize(u16, PATHEXT, &[_]u16{';'});
985 while (ext_it.next()) |ext| {
986 path_buf.shrinkRetainingCapacity(app_path_w.len);
987 try path_buf.appendSlice(self.allocator, ext);
988 try path_buf.append(self.allocator, 0);
989 const path_with_ext = path_buf.items[0 .. path_buf.items.len - 1 :0];
990
991 if (windowsCreateProcess(path_with_ext.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| {
992 break :exec;
993 } else |err| switch (err) {
994 error.FileNotFound, error.AccessDenied, error.InvalidExe => {},
995 else => return err,
996 }
997 }
998 }
999998
1000 // No need to search the PATH if the app path is absolute999 try app_buf.appendSlice(self.allocator, app_name_w);
1001 if (fs.path.isAbsoluteWindowsWTF16(app_path_w)) return no_path_err;1000
10021001 var dir_buf = std.ArrayListUnmanaged(u16){};
1003 // app_path_w has the cwd prepended to it if cwd is non-null, so when1002 defer dir_buf.deinit(self.allocator);
1004 // searching the PATH we should make sure we use the app_name verbatim.1003
1005 var app_name_w_needs_free = false;1004 if (cwd_path_w.len > 0) {
1006 const app_name_w = x: {1005 try dir_buf.appendSlice(self.allocator, cwd_path_w);
1007 if (self.cwd) |_| {1006 }
1008 app_name_w_needs_free = true;1007 if (app_dirname_w) |app_dir| {
1009 break :x try unicode.utf8ToUtf16LeWithNull(self.allocator, self.argv[0]);1008 if (dir_buf.items.len > 0) try dir_buf.append(self.allocator, fs.path.sep);
1010 } else {1009 try dir_buf.appendSlice(self.allocator, app_dir);
1011 break :x app_path_w;1010 }
1012 }1011 if (dir_buf.items.len > 0) {
1012 // Need to normalize the path, openDirW can't handle things like double backslashes
1013 const normalized_len = windows.normalizePath(u16, dir_buf.items) catch return error.BadPathName;
1014 dir_buf.shrinkRetainingCapacity(normalized_len);
1015 }
1016
1017 windowsCreateProcessPathExt(self.allocator, &dir_buf, &app_buf, PATHEXT, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| {
1018 var original_err = switch (no_path_err) {
1019 error.FileNotFound, error.InvalidExe, error.AccessDenied => |e| e,
1020 error.UnrecoverableInvalidExe => return error.InvalidExe,
1021 else => |e| return e,
1013 };1022 };
1014 defer if (app_name_w_needs_free) self.allocator.free(app_name_w);1023
1024 // If the app name had path separators, that disallows PATH searching,
1025 // and there's no need to search the PATH if the cwd path is absolute.
1026 if (app_dirname_w != null or fs.path.isAbsoluteWindowsWTF16(cwd_path_w)) {
1027 return original_err;
1028 }
10151029
1016 var it = mem.tokenize(u16, PATH, &[_]u16{';'});1030 var it = mem.tokenize(u16, PATH, &[_]u16{';'});
1017 while (it.next()) |search_path| {1031 while (it.next()) |search_path| {
1018 path_buf.clearRetainingCapacity();1032 dir_buf.clearRetainingCapacity();
1019 const search_path_trimmed = mem.trimRight(u16, search_path, &[_]u16{ '\\', '/' });1033 try dir_buf.appendSlice(self.allocator, search_path);
1020 try path_buf.appendSlice(self.allocator, search_path_trimmed);1034 // Need to normalize the path, some PATH values can contain things like double
1021 try path_buf.append(self.allocator, fs.path.sep);1035 // backslashes which openDirW can't handle
1022 const app_name_trimmed = mem.trimLeft(u16, app_name_w, &[_]u16{ '\\', '/' });1036 const normalized_len = windows.normalizePath(u16, dir_buf.items) catch continue;
1023 try path_buf.appendSlice(self.allocator, app_name_trimmed);1037 dir_buf.shrinkRetainingCapacity(normalized_len);
1024 try path_buf.append(self.allocator, 0);1038
1025 const path_no_ext = path_buf.items[0 .. path_buf.items.len - 1 :0];1039 if (windowsCreateProcessPathExt(self.allocator, &dir_buf, &app_buf, PATHEXT, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) {
1026
1027 if (windowsCreateProcess(path_no_ext.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| {
1028 break :exec;1040 break :exec;
1029 } else |err| switch (err) {1041 } else |err| switch (err) {
1030 error.FileNotFound, error.AccessDenied, error.InvalidExe => {},1042 error.FileNotFound, error.AccessDenied, error.InvalidExe => continue,
1031 else => return err,1043 error.UnrecoverableInvalidExe => return error.InvalidExe,
1032 }1044 else => |e| return e,
1033
1034 var ext_it = mem.tokenize(u16, PATHEXT, &[_]u16{';'});
1035 while (ext_it.next()) |ext| {
1036 path_buf.shrinkRetainingCapacity(path_no_ext.len);
1037 try path_buf.appendSlice(self.allocator, ext);
1038 try path_buf.append(self.allocator, 0);
1039 const joined_path = path_buf.items[0 .. path_buf.items.len - 1 :0];
1040
1041 if (windowsCreateProcess(joined_path.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| {
1042 break :exec;
1043 } else |err| switch (err) {
1044 error.FileNotFound => continue,
1045 error.AccessDenied => continue,
1046 error.InvalidExe => continue,
1047 else => return err,
1048 }
1049 }1045 }
1050 } else {1046 } else {
1051 return no_path_err; // return the original error1047 return original_err;
1052 }1048 }
1053 };1049 };
1054 }1050 }
...@@ -1094,6 +1090,235 @@ pub const ChildProcess = struct {...@@ -1094,6 +1090,235 @@ pub const ChildProcess = struct {
1094 }1090 }
1095};1091};
10961092
1093/// Expects `app_buf` to contain exactly the app name, and `dir_buf` to contain exactly the dir path.
1094/// After return, `app_buf` will always contain exactly the app name and `dir_buf` will always contain exactly the dir path.
1095/// Note: `app_buf` should not contain any leading path separators.
1096/// Note: If the dir is the cwd, dir_buf should be empty (len = 0).
1097fn windowsCreateProcessPathExt(
1098 allocator: mem.Allocator,
1099 dir_buf: *std.ArrayListUnmanaged(u16),
1100 app_buf: *std.ArrayListUnmanaged(u16),
1101 pathext: [:0]const u16,
1102 cmd_line: [*:0]u16,
1103 envp_ptr: ?[*]u16,
1104 cwd_ptr: ?[*:0]u16,
1105 lpStartupInfo: *windows.STARTUPINFOW,
1106 lpProcessInformation: *windows.PROCESS_INFORMATION,
1107) !void {
1108 const app_name_len = app_buf.items.len;
1109 const dir_path_len = dir_buf.items.len;
1110
1111 if (app_name_len == 0) return error.FileNotFound;
1112
1113 defer app_buf.shrinkRetainingCapacity(app_name_len);
1114 defer dir_buf.shrinkRetainingCapacity(dir_path_len);
1115
1116 // The name of the game here is to avoid CreateProcessW calls at all costs,
1117 // and only ever try calling it when we have a real candidate for execution.
1118 // Secondarily, we want to minimize the number of syscalls used when checking
1119 // for each PATHEXT-appended version of the app name.
1120 //
1121 // An overview of the technique used:
1122 // - Open the search directory for iteration (either cwd or a path from PATH)
1123 // - Use NtQueryDirectoryFile with a wildcard filename of `<app name>*` to
1124 // check if anything that could possibly match either the unappended version
1125 // of the app name or any of the versions with a PATHEXT value appended exists.
1126 // - If the wildcard NtQueryDirectoryFile call found nothing, we can exit early
1127 // without needing to use PATHEXT at all.
1128 //
1129 // This allows us to use a <open dir, NtQueryDirectoryFile, close dir> sequence
1130 // for any directory that doesn't contain any possible matches, instead of having
1131 // to use a separate look up for each individual filename combination (unappended +
1132 // each PATHEXT appended). For directories where the wildcard *does* match something,
1133 // we only need to do a maximum of <number of supported PATHEXT extensions> more
1134 // NtQueryDirectoryFile calls.
1135
1136 var dir = dir: {
1137 if (fs.path.isAbsoluteWindowsWTF16(dir_buf.items[0..dir_path_len])) {
1138 const prefixed_path = try windows.wToPrefixedFileW(dir_buf.items[0..dir_path_len]);
1139 break :dir fs.cwd().openDirW(prefixed_path.span().ptr, .{}, true) catch return error.FileNotFound;
1140 }
1141 // needs to be null-terminated
1142 try dir_buf.append(allocator, 0);
1143 defer dir_buf.shrinkRetainingCapacity(dir_buf.items[0..dir_path_len].len);
1144 const dir_path_z = dir_buf.items[0 .. dir_buf.items.len - 1 :0];
1145 break :dir std.fs.cwd().openDirW(dir_path_z.ptr, .{}, true) catch return error.FileNotFound;
1146 };
1147 defer dir.close();
1148
1149 // Add wildcard and null-terminator
1150 try app_buf.append(allocator, '*');
1151 try app_buf.append(allocator, 0);
1152 const app_name_wildcard = app_buf.items[0 .. app_buf.items.len - 1 :0];
1153
1154 // Enough for the FILE_DIRECTORY_INFORMATION + (NAME_MAX UTF-16 code units [2 bytes each]).
1155 const file_info_buf_size = @sizeOf(windows.FILE_DIRECTORY_INFORMATION) + (windows.NAME_MAX * 2);
1156 var file_information_buf: [file_info_buf_size]u8 align(@alignOf(os.windows.FILE_DIRECTORY_INFORMATION)) = undefined;
1157 var io_status: windows.IO_STATUS_BLOCK = undefined;
1158 const found_name: ?[]const u16 = found_name: {
1159 const app_name_len_bytes = math.cast(u16, app_name_wildcard.len * 2) orelse return error.NameTooLong;
1160 var app_name_unicode_string = windows.UNICODE_STRING{
1161 .Length = app_name_len_bytes,
1162 .MaximumLength = app_name_len_bytes,
1163 .Buffer = @intToPtr([*]u16, @ptrToInt(app_name_wildcard.ptr)),
1164 };
1165 const rc = windows.ntdll.NtQueryDirectoryFile(
1166 dir.fd,
1167 null,
1168 null,
1169 null,
1170 &io_status,
1171 &file_information_buf,
1172 file_information_buf.len,
1173 .FileDirectoryInformation,
1174 // TODO: It might be better to iterate over all wildcard matches and
1175 // only pick the ones that match an appended PATHEXT instead of only
1176 // using the wildcard as a lookup and then restarting iteration
1177 // on future NtQueryDirectoryFile calls.
1178 //
1179 // However, note that this could lead to worse outcomes in the
1180 // case of a very generic command name (e.g. "a"), so it might
1181 // be better to only use the wildcard to determine if it's worth
1182 // checking with PATHEXT (this is the current behavior).
1183 windows.TRUE, // single result
1184 &app_name_unicode_string,
1185 windows.TRUE, // restart iteration
1186 );
1187
1188 // If we get nothing with the wildcard, then we can just bail out
1189 // as we know appending PATHEXT will not yield anything.
1190 switch (rc) {
1191 .SUCCESS => {},
1192 .NO_SUCH_FILE => return error.FileNotFound,
1193 .NO_MORE_FILES => return error.FileNotFound,
1194 .ACCESS_DENIED => return error.AccessDenied,
1195 else => return windows.unexpectedStatus(rc),
1196 }
1197
1198 const dir_info = @ptrCast(*windows.FILE_DIRECTORY_INFORMATION, &file_information_buf);
1199 if (dir_info.FileAttributes & windows.FILE_ATTRIBUTE_DIRECTORY != 0) {
1200 break :found_name null;
1201 }
1202 break :found_name @ptrCast([*]u16, &dir_info.FileName)[0 .. dir_info.FileNameLength / 2];
1203 };
1204
1205 const unappended_err = unappended: {
1206 // NtQueryDirectoryFile returns results in order by filename, so the first result of
1207 // the wildcard call will always be the unappended version if it exists. So, if found_name
1208 // is not the unappended version, we can skip straight to trying versions with PATHEXT appended.
1209 // TODO: This might depend on the filesystem, though; need to somehow verify that it always
1210 // works this way.
1211 if (found_name != null and windows.eqlIgnoreCaseWTF16(found_name.?, app_buf.items[0..app_name_len])) {
1212 if (dir_path_len != 0) switch (dir_buf.items[dir_buf.items.len - 1]) {
1213 '/', '\\' => {},
1214 else => try dir_buf.append(allocator, fs.path.sep),
1215 };
1216 try dir_buf.appendSlice(allocator, app_buf.items[0..app_name_len]);
1217 try dir_buf.append(allocator, 0);
1218 const full_app_name = dir_buf.items[0 .. dir_buf.items.len - 1 :0];
1219
1220 if (windowsCreateProcess(full_app_name.ptr, cmd_line, envp_ptr, cwd_ptr, lpStartupInfo, lpProcessInformation)) |_| {
1221 return;
1222 } else |err| switch (err) {
1223 error.FileNotFound,
1224 error.AccessDenied,
1225 => break :unappended err,
1226 error.InvalidExe => {
1227 // On InvalidExe, if the extension of the app name is .exe then
1228 // it's treated as an unrecoverable error. Otherwise, it'll be
1229 // skipped as normal.
1230 const app_name = app_buf.items[0..app_name_len];
1231 const ext_start = std.mem.lastIndexOfScalar(u16, app_name, '.') orelse break :unappended err;
1232 const ext = app_name[ext_start..];
1233 if (windows.eqlIgnoreCaseWTF16(ext, unicode.utf8ToUtf16LeStringLiteral(".EXE"))) {
1234 return error.UnrecoverableInvalidExe;
1235 }
1236 break :unappended err;
1237 },
1238 else => return err,
1239 }
1240 }
1241 break :unappended error.FileNotFound;
1242 };
1243
1244 // Now we know that at least *a* file matching the wildcard exists, we can loop
1245 // through PATHEXT in order and exec any that exist
1246
1247 var ext_it = mem.tokenize(u16, pathext, &[_]u16{';'});
1248 while (ext_it.next()) |ext| {
1249 if (!windowsCreateProcessSupportsExtension(ext)) continue;
1250
1251 app_buf.shrinkRetainingCapacity(app_name_len);
1252 try app_buf.appendSlice(allocator, ext);
1253 try app_buf.append(allocator, 0);
1254 const app_name_appended = app_buf.items[0 .. app_buf.items.len - 1 :0];
1255
1256 const app_name_len_bytes = math.cast(u16, app_name_appended.len * 2) orelse return error.NameTooLong;
1257 var app_name_unicode_string = windows.UNICODE_STRING{
1258 .Length = app_name_len_bytes,
1259 .MaximumLength = app_name_len_bytes,
1260 .Buffer = @intToPtr([*]u16, @ptrToInt(app_name_appended.ptr)),
1261 };
1262
1263 // Re-use the directory handle but this time we call with the appended app name
1264 // with no wildcard.
1265 const rc = windows.ntdll.NtQueryDirectoryFile(
1266 dir.fd,
1267 null,
1268 null,
1269 null,
1270 &io_status,
1271 &file_information_buf,
1272 file_information_buf.len,
1273 .FileDirectoryInformation,
1274 windows.TRUE, // single result
1275 &app_name_unicode_string,
1276 windows.TRUE, // restart iteration
1277 );
1278
1279 switch (rc) {
1280 .SUCCESS => {},
1281 .NO_SUCH_FILE => continue,
1282 .NO_MORE_FILES => continue,
1283 .ACCESS_DENIED => continue,
1284 else => return windows.unexpectedStatus(rc),
1285 }
1286
1287 const dir_info = @ptrCast(*windows.FILE_DIRECTORY_INFORMATION, &file_information_buf);
1288 // Skip directories
1289 if (dir_info.FileAttributes & windows.FILE_ATTRIBUTE_DIRECTORY != 0) continue;
1290
1291 dir_buf.shrinkRetainingCapacity(dir_path_len);
1292 if (dir_path_len != 0) switch (dir_buf.items[dir_buf.items.len - 1]) {
1293 '/', '\\' => {},
1294 else => try dir_buf.append(allocator, fs.path.sep),
1295 };
1296 try dir_buf.appendSlice(allocator, app_buf.items[0..app_name_len]);
1297 try dir_buf.appendSlice(allocator, ext);
1298 try dir_buf.append(allocator, 0);
1299 const full_app_name = dir_buf.items[0 .. dir_buf.items.len - 1 :0];
1300
1301 if (windowsCreateProcess(full_app_name.ptr, cmd_line, envp_ptr, cwd_ptr, lpStartupInfo, lpProcessInformation)) |_| {
1302 return;
1303 } else |err| switch (err) {
1304 error.FileNotFound => continue,
1305 error.AccessDenied => continue,
1306 error.InvalidExe => {
1307 // On InvalidExe, if the extension of the app name is .exe then
1308 // it's treated as an unrecoverable error. Otherwise, it'll be
1309 // skipped as normal.
1310 if (windows.eqlIgnoreCaseWTF16(ext, unicode.utf8ToUtf16LeStringLiteral(".EXE"))) {
1311 return error.UnrecoverableInvalidExe;
1312 }
1313 continue;
1314 },
1315 else => return err,
1316 }
1317 }
1318
1319 return unappended_err;
1320}
1321
1097fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u16, cwd_ptr: ?[*:0]u16, lpStartupInfo: *windows.STARTUPINFOW, lpProcessInformation: *windows.PROCESS_INFORMATION) !void {1322fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u16, cwd_ptr: ?[*:0]u16, lpStartupInfo: *windows.STARTUPINFOW, lpProcessInformation: *windows.PROCESS_INFORMATION) !void {
1098 // TODO the docs for environment pointer say:1323 // TODO the docs for environment pointer say:
1099 // > A pointer to the environment block for the new process. If this parameter1324 // > A pointer to the environment block for the new process. If this parameter
...@@ -1126,6 +1351,64 @@ fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u1...@@ -1126,6 +1351,64 @@ fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u1
1126 );1351 );
1127}1352}
11281353
1354/// Case-insenstive UTF-16 lookup
1355fn windowsCreateProcessSupportsExtension(ext: []const u16) bool {
1356 const State = enum {
1357 start,
1358 dot,
1359 b,
1360 ba,
1361 c,
1362 cm,
1363 co,
1364 e,
1365 ex,
1366 };
1367 var state: State = .start;
1368 for (ext) |c| switch (state) {
1369 .start => switch (c) {
1370 '.' => state = .dot,
1371 else => return false,
1372 },
1373 .dot => switch (c) {
1374 'b', 'B' => state = .b,
1375 'c', 'C' => state = .c,
1376 'e', 'E' => state = .e,
1377 else => return false,
1378 },
1379 .b => switch (c) {
1380 'a', 'A' => state = .ba,
1381 else => return false,
1382 },
1383 .c => switch (c) {
1384 'm', 'M' => state = .cm,
1385 'o', 'O' => state = .co,
1386 else => return false,
1387 },
1388 .e => switch (c) {
1389 'x', 'X' => state = .ex,
1390 else => return false,
1391 },
1392 .ba => switch (c) {
1393 't', 'T' => return true, // .BAT
1394 else => return false,
1395 },
1396 .cm => switch (c) {
1397 'd', 'D' => return true, // .CMD
1398 else => return false,
1399 },
1400 .co => switch (c) {
1401 'm', 'M' => return true, // .COM
1402 else => return false,
1403 },
1404 .ex => switch (c) {
1405 'e', 'E' => return true, // .EXE
1406 else => return false,
1407 },
1408 };
1409 return false;
1410}
1411
1129/// Caller must dealloc.1412/// Caller must dealloc.
1130fn windowsCreateCommandLine(allocator: mem.Allocator, argv: []const []const u8) ![:0]u8 {1413fn windowsCreateCommandLine(allocator: mem.Allocator, argv: []const []const u8) ![:0]u8 {
1131 var buf = std.ArrayList(u8).init(allocator);1414 var buf = std.ArrayList(u8).init(allocator);
lib/std/os.zig+1-13
...@@ -1944,19 +1944,7 @@ pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {...@@ -1944,19 +1944,7 @@ pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {
1944 while (ptr[i] != 0) : (i += 1) {}1944 while (ptr[i] != 0) : (i += 1) {}
1945 const this_value = ptr[value_start..i :0];1945 const this_value = ptr[value_start..i :0];
19461946
1947 const key_string_bytes = @intCast(u16, key_slice.len * 2);1947 if (windows.eqlIgnoreCaseWTF16(key_slice, this_key)) {
1948 const key_string = windows.UNICODE_STRING{
1949 .Length = key_string_bytes,
1950 .MaximumLength = key_string_bytes,
1951 .Buffer = @intToPtr([*]u16, @ptrToInt(key)),
1952 };
1953 const this_key_string_bytes = @intCast(u16, this_key.len * 2);
1954 const this_key_string = windows.UNICODE_STRING{
1955 .Length = this_key_string_bytes,
1956 .MaximumLength = this_key_string_bytes,
1957 .Buffer = this_key.ptr,
1958 };
1959 if (windows.ntdll.RtlEqualUnicodeString(&key_string, &this_key_string, windows.TRUE) == windows.TRUE) {
1960 return this_value;1948 return this_value;
1961 }1949 }
19621950
lib/std/os/windows.zig+34
...@@ -1624,6 +1624,9 @@ pub fn CreateProcessW(...@@ -1624,6 +1624,9 @@ pub fn CreateProcessW(
1624 .RING2SEG_MUST_BE_MOVABLE,1624 .RING2SEG_MUST_BE_MOVABLE,
1625 .RELOC_CHAIN_XEEDS_SEGLIM,1625 .RELOC_CHAIN_XEEDS_SEGLIM,
1626 .INFLOOP_IN_RELOC_CHAIN, // MAX_EXEC_ERROR in errno.cpp1626 .INFLOOP_IN_RELOC_CHAIN, // MAX_EXEC_ERROR in errno.cpp
1627 // This one is not mapped to ENOEXEC but it is possible, for example
1628 // when calling CreateProcessW on a plain text file with a .exe extension
1629 .EXE_MACHINE_TYPE_MISMATCH,
1627 => return error.InvalidExe,1630 => return error.InvalidExe,
1628 else => |err| return unexpectedError(err),1631 else => |err| return unexpectedError(err),
1629 }1632 }
...@@ -1824,6 +1827,23 @@ pub fn nanoSecondsToFileTime(ns: i128) FILETIME {...@@ -1824,6 +1827,23 @@ pub fn nanoSecondsToFileTime(ns: i128) FILETIME {
1824 };1827 };
1825}1828}
18261829
1830/// Compares two WTF16 strings using RtlEqualUnicodeString
1831pub fn eqlIgnoreCaseWTF16(a: []const u16, b: []const u16) bool {
1832 const a_bytes = @intCast(u16, a.len * 2);
1833 const a_string = UNICODE_STRING{
1834 .Length = a_bytes,
1835 .MaximumLength = a_bytes,
1836 .Buffer = @intToPtr([*]u16, @ptrToInt(a.ptr)),
1837 };
1838 const b_bytes = @intCast(u16, b.len * 2);
1839 const b_string = UNICODE_STRING{
1840 .Length = b_bytes,
1841 .MaximumLength = b_bytes,
1842 .Buffer = @intToPtr([*]u16, @ptrToInt(b.ptr)),
1843 };
1844 return ntdll.RtlEqualUnicodeString(&a_string, &b_string, TRUE) == TRUE;
1845}
1846
1827pub const PathSpace = struct {1847pub const PathSpace = struct {
1828 data: [PATH_MAX_WIDE:0]u16,1848 data: [PATH_MAX_WIDE:0]u16,
1829 len: usize,1849 len: usize,
...@@ -3682,6 +3702,20 @@ pub const RTL_DRIVE_LETTER_CURDIR = extern struct {...@@ -3682,6 +3702,20 @@ pub const RTL_DRIVE_LETTER_CURDIR = extern struct {
36823702
3683pub const PPS_POST_PROCESS_INIT_ROUTINE = ?*const fn () callconv(.C) void;3703pub const PPS_POST_PROCESS_INIT_ROUTINE = ?*const fn () callconv(.C) void;
36843704
3705pub const FILE_DIRECTORY_INFORMATION = extern struct {
3706 NextEntryOffset: ULONG,
3707 FileIndex: ULONG,
3708 CreationTime: LARGE_INTEGER,
3709 LastAccessTime: LARGE_INTEGER,
3710 LastWriteTime: LARGE_INTEGER,
3711 ChangeTime: LARGE_INTEGER,
3712 EndOfFile: LARGE_INTEGER,
3713 AllocationSize: LARGE_INTEGER,
3714 FileAttributes: ULONG,
3715 FileNameLength: ULONG,
3716 FileName: [1]WCHAR,
3717};
3718
3685pub const FILE_BOTH_DIR_INFORMATION = extern struct {3719pub const FILE_BOTH_DIR_INFORMATION = extern struct {
3686 NextEntryOffset: ULONG,3720 NextEntryOffset: ULONG,
3687 FileIndex: ULONG,3721 FileIndex: ULONG,
lib/std/os/windows/kernel32.zig+2
...@@ -177,6 +177,8 @@ pub extern "kernel32" fn GetEnvironmentStringsW() callconv(WINAPI) ?[*:0]u16;...@@ -177,6 +177,8 @@ pub extern "kernel32" fn GetEnvironmentStringsW() callconv(WINAPI) ?[*:0]u16;
177177
178pub extern "kernel32" fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: [*]u16, nSize: DWORD) callconv(WINAPI) DWORD;178pub extern "kernel32" fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: [*]u16, nSize: DWORD) callconv(WINAPI) DWORD;
179179
180pub extern "kernel32" fn SetEnvironmentVariableW(lpName: LPCWSTR, lpValue: ?LPCWSTR) callconv(WINAPI) BOOL;
181
180pub extern "kernel32" fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: *DWORD) callconv(WINAPI) BOOL;182pub extern "kernel32" fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: *DWORD) callconv(WINAPI) BOOL;
181183
182pub extern "kernel32" fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LARGE_INTEGER) callconv(WINAPI) BOOL;184pub extern "kernel32" fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LARGE_INTEGER) callconv(WINAPI) BOOL;
test/standalone.zig+4
...@@ -63,6 +63,10 @@ pub fn addCases(cases: *tests.StandaloneContext) void {...@@ -63,6 +63,10 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
63 cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{});63 cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{});
64 }64 }
6565
66 if (builtin.os.tag == .windows) {
67 cases.addBuildFile("test/standalone/windows_spawn/build.zig", .{});
68 }
69
66 cases.addBuildFile("test/standalone/c_compiler/build.zig", .{70 cases.addBuildFile("test/standalone/c_compiler/build.zig", .{
67 .build_modes = true,71 .build_modes = true,
68 .cross_targets = true,72 .cross_targets = true,
test/standalone/windows_spawn/build.zig created+16
...@@ -0,0 +1,16 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const hello = b.addExecutable("hello", "hello.zig");
7 hello.setBuildMode(mode);
8
9 const main = b.addExecutable("main", "main.zig");
10 main.setBuildMode(mode);
11 const run = main.run();
12 run.addArtifactArg(hello);
13
14 const test_step = b.step("test", "Test it");
15 test_step.dependOn(&run.step);
16}
test/standalone/windows_spawn/hello.zig created+6
...@@ -0,0 +1,6 @@
1const std = @import("std");
2
3pub fn main() !void {
4 const stdout = std.io.getStdOut().writer();
5 try stdout.writeAll("hello from exe\n");
6}
test/standalone/windows_spawn/main.zig created+161
...@@ -0,0 +1,161 @@
1const std = @import("std");
2const windows = std.os.windows;
3const utf16Literal = std.unicode.utf8ToUtf16LeStringLiteral;
4
5pub fn main() anyerror!void {
6 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7 defer if (gpa.deinit()) @panic("found memory leaks");
8 const allocator = gpa.allocator();
9
10 var it = try std.process.argsWithAllocator(allocator);
11 defer it.deinit();
12 _ = it.next() orelse unreachable; // skip binary name
13 const hello_exe_cache_path = it.next() orelse unreachable;
14
15 var tmp = std.testing.tmpDir(.{});
16 defer tmp.cleanup();
17
18 const tmp_absolute_path = try tmp.dir.realpathAlloc(allocator, ".");
19 defer allocator.free(tmp_absolute_path);
20 const tmp_absolute_path_w = try std.unicode.utf8ToUtf16LeWithNull(allocator, tmp_absolute_path);
21 defer allocator.free(tmp_absolute_path_w);
22 const cwd_absolute_path = try std.fs.cwd().realpathAlloc(allocator, ".");
23 defer allocator.free(cwd_absolute_path);
24 const tmp_relative_path = try std.fs.path.relative(allocator, cwd_absolute_path, tmp_absolute_path);
25 defer allocator.free(tmp_relative_path);
26
27 // Clear PATH
28 std.debug.assert(std.os.windows.kernel32.SetEnvironmentVariableW(
29 utf16Literal("PATH"),
30 null,
31 ) == windows.TRUE);
32
33 // Set PATHEXT to something predictable
34 std.debug.assert(std.os.windows.kernel32.SetEnvironmentVariableW(
35 utf16Literal("PATHEXT"),
36 utf16Literal(".COM;.EXE;.BAT;.CMD;.JS"),
37 ) == windows.TRUE);
38
39 // No PATH, so it should fail to find anything not in the cwd
40 try testExecError(error.FileNotFound, allocator, "something_missing");
41
42 std.debug.assert(std.os.windows.kernel32.SetEnvironmentVariableW(
43 utf16Literal("PATH"),
44 tmp_absolute_path_w,
45 ) == windows.TRUE);
46
47 // Move hello.exe into the tmp dir which is now added to the path
48 try std.fs.cwd().copyFile(hello_exe_cache_path, tmp.dir, "hello.exe", .{});
49
50 // with extension should find the .exe (case insensitive)
51 try testExec(allocator, "HeLLo.exe", "hello from exe\n");
52 // without extension should find the .exe (case insensitive)
53 try testExec(allocator, "heLLo", "hello from exe\n");
54
55 // now add a .bat
56 try tmp.dir.writeFile("hello.bat", "@echo hello from bat");
57 // and a .cmd
58 try tmp.dir.writeFile("hello.cmd", "@echo hello from cmd");
59
60 // with extension should find the .bat (case insensitive)
61 try testExec(allocator, "heLLo.bat", "hello from bat\r\n");
62 // with extension should find the .cmd (case insensitive)
63 try testExec(allocator, "heLLo.cmd", "hello from cmd\r\n");
64 // without extension should find the .exe (since its first in PATHEXT)
65 try testExec(allocator, "heLLo", "hello from exe\n");
66
67 // now rename the exe to not have an extension
68 try tmp.dir.rename("hello.exe", "hello");
69
70 // with extension should now fail
71 try testExecError(error.FileNotFound, allocator, "hello.exe");
72 // without extension should succeed (case insensitive)
73 try testExec(allocator, "heLLo", "hello from exe\n");
74
75 try tmp.dir.makeDir("something");
76 try tmp.dir.rename("hello", "something/hello.exe");
77
78 const relative_path_no_ext = try std.fs.path.join(allocator, &.{ tmp_relative_path, "something/hello" });
79 defer allocator.free(relative_path_no_ext);
80
81 // Giving a full relative path to something/hello should work
82 try testExec(allocator, relative_path_no_ext, "hello from exe\n");
83 // But commands with path separators get excluded from PATH searching, so this will fail
84 try testExecError(error.FileNotFound, allocator, "something/hello");
85
86 // Now that .BAT is the first PATHEXT that should be found, this should succeed
87 try testExec(allocator, "heLLo", "hello from bat\r\n");
88
89 // Add a hello.exe that is not a valid executable
90 try tmp.dir.writeFile("hello.exe", "invalid");
91
92 // Trying to execute it with extension will give InvalidExe. This is a special
93 // case for .EXE extensions, where if they ever try to get executed but they are
94 // invalid, that gets treated as a fatal error wherever they are found and InvalidExe
95 // is returned immediately.
96 try testExecError(error.InvalidExe, allocator, "hello.exe");
97 // Same thing applies to the command with no extension--even though there is a
98 // hello.bat that could be executed, it should stop after it tries executing
99 // hello.exe and getting InvalidExe.
100 try testExecError(error.InvalidExe, allocator, "hello");
101
102 // If we now rename hello.exe to have no extension, it will behave differently
103 try tmp.dir.rename("hello.exe", "hello");
104
105 // Now, trying to execute it without an extension should treat InvalidExe as recoverable
106 // and skip over it and find hello.bat and execute that
107 try testExec(allocator, "hello", "hello from bat\r\n");
108
109 // If we rename the invalid exe to something else
110 try tmp.dir.rename("hello", "goodbye");
111 // Then we should now get FileNotFound when trying to execute 'goodbye',
112 // since that is what the original error will be after searching for 'goodbye'
113 // in the cwd. It will try to execute 'goodbye' from the PATH but the InvalidExe error
114 // should be ignored in this case.
115 try testExecError(error.FileNotFound, allocator, "goodbye");
116
117 // Now let's set the tmp dir as the cwd and set the path only include the "something" sub dir
118 try tmp.dir.setAsCwd();
119 const something_subdir_abs_path = try std.mem.concatWithSentinel(allocator, u16, &.{ tmp_absolute_path_w, utf16Literal("\\something") }, 0);
120 defer allocator.free(something_subdir_abs_path);
121
122 std.debug.assert(std.os.windows.kernel32.SetEnvironmentVariableW(
123 utf16Literal("PATH"),
124 something_subdir_abs_path,
125 ) == windows.TRUE);
126
127 // Now trying to execute goodbye should give error.InvalidExe since it's the original
128 // error that we got when trying within the cwd
129 try testExecError(error.InvalidExe, allocator, "goodbye");
130
131 // hello should still find the .bat
132 try testExec(allocator, "hello", "hello from bat\r\n");
133
134 // If we rename something/hello.exe to something/goodbye.exe
135 try tmp.dir.rename("something/hello.exe", "something/goodbye.exe");
136 // And try to execute goodbye, then the one in something should be found
137 // since the one in cwd is an invalid executable
138 try testExec(allocator, "goodbye", "hello from exe\n");
139
140 // If we use an absolute path to execute the invalid goodbye
141 const goodbye_abs_path = try std.mem.join(allocator, "\\", &.{ tmp_absolute_path, "goodbye" });
142 defer allocator.free(goodbye_abs_path);
143 // then the PATH should not be searched and we should get InvalidExe
144 try testExecError(error.InvalidExe, allocator, goodbye_abs_path);
145}
146
147fn testExecError(err: anyerror, allocator: std.mem.Allocator, command: []const u8) !void {
148 return std.testing.expectError(err, testExec(allocator, command, ""));
149}
150
151fn testExec(allocator: std.mem.Allocator, command: []const u8, expected_stdout: []const u8) !void {
152 var result = try std.ChildProcess.exec(.{
153 .allocator = allocator,
154 .argv = &[_][]const u8{command},
155 });
156 defer allocator.free(result.stdout);
157 defer allocator.free(result.stderr);
158
159 try std.testing.expectEqualStrings("", result.stderr);
160 try std.testing.expectEqualStrings(expected_stdout, result.stdout);
161}