generic_win_port.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #define _CRT_RAND_S
  2. #include <generic_win_port.h>
  3. #include <dispatch_test.h>
  4. #include <stdarg.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <wchar.h>
  10. #include <Windows.h>
  11. static bool
  12. expand_wstr(WCHAR **str, size_t *capacity, size_t needed)
  13. {
  14. if (*capacity >= needed) {
  15. return true;
  16. }
  17. if (needed > UNICODE_STRING_MAX_CHARS) {
  18. return false;
  19. }
  20. size_t new_capacity = *capacity ?: needed;
  21. while (new_capacity < needed) {
  22. new_capacity *= 2;
  23. }
  24. WCHAR *new_str = realloc(*str, new_capacity * sizeof(WCHAR));
  25. if (!new_str) {
  26. return false;
  27. }
  28. *str = new_str;
  29. *capacity = new_capacity;
  30. return true;
  31. }
  32. static bool
  33. append_wstr(WCHAR **str, size_t *capacity, size_t *len, WCHAR *suffix)
  34. {
  35. size_t suffix_len = wcslen(suffix);
  36. if (!expand_wstr(str, capacity, *len + suffix_len)) {
  37. return false;
  38. }
  39. memcpy(*str + *len, suffix, suffix_len * sizeof(WCHAR));
  40. *len += suffix_len;
  41. return true;
  42. }
  43. WCHAR *
  44. argv_to_command_line(char **argv)
  45. {
  46. // This is basically the reverse of CommandLineToArgvW(). We want to convert
  47. // an argv array into a command-line compatible with CreateProcessW().
  48. //
  49. // See also:
  50. // <https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-commandlinetoargvw>
  51. // <https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/>
  52. size_t len = 0, capacity = 0;
  53. WCHAR *cmdline = NULL;
  54. if (!expand_wstr(&cmdline, &capacity, 256)) {
  55. goto error;
  56. }
  57. for (size_t i = 0; argv[i]; i++) {
  58. // Separate arguments with spaces.
  59. if (i > 0 && !append_wstr(&cmdline, &capacity, &len, L" ")) {
  60. goto error;
  61. }
  62. // Surround the argument with quotes if it's empty or contains special
  63. // characters.
  64. char *cur = argv[i];
  65. bool quoted = (*cur == '\0' || cur[strcspn(cur, " \t\n\v\"")] != '\0');
  66. if (quoted && !append_wstr(&cmdline, &capacity, &len, L"\"")) {
  67. goto error;
  68. }
  69. while (*cur != '\0') {
  70. if (*cur == '"') {
  71. // Quotes must be escaped with a backslash.
  72. if (!append_wstr(&cmdline, &capacity, &len, L"\\\"")) {
  73. goto error;
  74. }
  75. cur++;
  76. } else if (*cur == '\\') {
  77. // Windows treats backslashes differently depending on whether
  78. // they're followed by a quote. If the backslashes aren't
  79. // followed by a quote, then all slashes are copied into the
  80. // argument string. Otherwise, only n/2 slashes are included.
  81. // Count the number of slashes and double them if they're
  82. // followed by a quote.
  83. size_t backslashes = strspn(cur, "\\");
  84. cur += backslashes;
  85. // If the argument needs to be surrounded with quotes, we must
  86. // also check if the backslashes are at the end of the argument
  87. // because the added quote will follow them.
  88. if (*cur == '"' || (quoted && *cur == '\0')) {
  89. backslashes *= 2;
  90. }
  91. if (!expand_wstr(&cmdline, &capacity, len + backslashes)) {
  92. goto error;
  93. }
  94. wmemset(&cmdline[len], L'\\', backslashes);
  95. len += backslashes;
  96. } else {
  97. // Widen as many characters as possible.
  98. size_t mb_len = strcspn(cur, "\"\\");
  99. int wide_len = MultiByteToWideChar(CP_UTF8, 0, cur, mb_len,
  100. NULL, 0);
  101. if (wide_len == 0) {
  102. goto error;
  103. }
  104. if (!expand_wstr(&cmdline, &capacity, len + wide_len)) {
  105. goto error;
  106. }
  107. wide_len = MultiByteToWideChar(CP_UTF8, 0, cur, mb_len,
  108. &cmdline[len], wide_len);
  109. if (wide_len == 0) {
  110. goto error;
  111. }
  112. cur += mb_len;
  113. len += wide_len;
  114. }
  115. }
  116. if (quoted && !append_wstr(&cmdline, &capacity, &len, L"\"")) {
  117. goto error;
  118. }
  119. }
  120. if (!expand_wstr(&cmdline, &capacity, len + 1)) {
  121. goto error;
  122. }
  123. cmdline[len] = L'\0';
  124. return cmdline;
  125. error:
  126. free(cmdline);
  127. return NULL;
  128. }
  129. int
  130. asprintf(char **strp, const char *format, ...)
  131. {
  132. va_list arg1;
  133. va_start(arg1, format);
  134. int len = vsnprintf(NULL, 0, format, arg1);
  135. va_end(arg1);
  136. if (len >= 0) {
  137. size_t size = (size_t)len + 1;
  138. *strp = malloc(size);
  139. if (!*strp) {
  140. return -1;
  141. }
  142. va_list arg2;
  143. va_start(arg2, format);
  144. len = vsnprintf(*strp, size, format, arg2);
  145. va_end(arg2);
  146. }
  147. return len;
  148. }
  149. void
  150. filetime_to_timeval(struct timeval *tp, const FILETIME *ft)
  151. {
  152. int64_t ticks = ft->dwLowDateTime | (((int64_t)ft->dwHighDateTime) << 32);
  153. static const int64_t ticks_per_sec = 10LL * 1000LL * 1000LL;
  154. static const int64_t ticks_per_usec = 10LL;
  155. if (ticks >= 0) {
  156. tp->tv_sec = (long)(ticks / ticks_per_sec);
  157. tp->tv_usec = (long)((ticks % ticks_per_sec) / ticks_per_usec);
  158. } else {
  159. tp->tv_sec = (long)((ticks + 1) / ticks_per_sec - 1);
  160. tp->tv_usec = (long)((ticks_per_sec - 1 + (ticks + 1) % ticks_per_sec) / ticks_per_usec);
  161. }
  162. }
  163. pid_t
  164. getpid(void)
  165. {
  166. return (pid_t)GetCurrentProcessId();
  167. }
  168. int
  169. gettimeofday(struct timeval *tp, void *tzp)
  170. {
  171. (void)tzp;
  172. FILETIME ft;
  173. GetSystemTimePreciseAsFileTime(&ft);
  174. int64_t ticks = ft.dwLowDateTime | (((int64_t)ft.dwHighDateTime) << 32);
  175. ticks -= 116444736000000000LL; // Convert to Unix time
  176. FILETIME unix_ft = {.dwLowDateTime = (DWORD)ticks, .dwHighDateTime = ticks >> 32};
  177. filetime_to_timeval(tp, &unix_ft);
  178. return 0;
  179. }
  180. typedef void (WINAPI *QueryUnbiasedInterruptTimePreciseT)(PULONGLONG);
  181. static QueryUnbiasedInterruptTimePreciseT QueryUnbiasedInterruptTimePrecisePtr;
  182. static BOOL WINAPI
  183. mach_absolute_time_init(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContext)
  184. {
  185. // QueryUnbiasedInterruptTimePrecise() is declared in the Windows headers
  186. // but it isn't available in any import libraries. We must manually load it
  187. // from KernelBase.dll.
  188. HMODULE kernelbase = LoadLibraryW(L"KernelBase.dll");
  189. if (!kernelbase) {
  190. print_winapi_error("LoadLibraryW", GetLastError());
  191. abort();
  192. }
  193. QueryUnbiasedInterruptTimePrecisePtr =
  194. (QueryUnbiasedInterruptTimePreciseT)GetProcAddress(kernelbase,
  195. "QueryUnbiasedInterruptTimePrecise");
  196. if (!QueryUnbiasedInterruptTimePrecisePtr) {
  197. fprintf(stderr, "QueryUnbiasedInterruptTimePrecise is not available\n");
  198. abort();
  199. }
  200. return TRUE;
  201. }
  202. uint64_t
  203. mach_absolute_time(void)
  204. {
  205. static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
  206. if (!InitOnceExecuteOnce(&init_once, mach_absolute_time_init, NULL, NULL)) {
  207. print_winapi_error("InitOnceExecuteOnce", GetLastError());
  208. abort();
  209. }
  210. ULONGLONG result = 0;
  211. QueryUnbiasedInterruptTimePrecisePtr(&result);
  212. return result * 100; // Convert from 100ns units
  213. }
  214. static void
  215. randomize_name(char *out)
  216. {
  217. static const char chars[] =
  218. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-";
  219. const size_t num_chars = sizeof(chars) - 1;
  220. unsigned int lo, hi;
  221. rand_s(&lo);
  222. rand_s(&hi);
  223. uint64_t val = ((uint64_t)hi << 32) | lo;
  224. for (int j = 0; j < 6; j++) {
  225. out[j] = chars[val % num_chars];
  226. val /= num_chars;
  227. }
  228. }
  229. #ifndef HAVE_MKSTEMP
  230. dispatch_fd_t
  231. mkstemp(char *tmpl)
  232. {
  233. size_t len = strlen(tmpl);
  234. if (len < 6) {
  235. errno = EINVAL;
  236. return -1;
  237. }
  238. char *replace = &tmpl[len - 6];
  239. for (int i = 0; i < 100; i++) {
  240. randomize_name(replace);
  241. dispatch_fd_t fd = dispatch_test_fd_open(tmpl, O_RDWR | O_CREAT | O_EXCL);
  242. if (fd != -1) {
  243. return fd;
  244. }
  245. }
  246. errno = EEXIST;
  247. return -1;
  248. }
  249. #endif
  250. void
  251. print_winapi_error(const char *function_name, DWORD error)
  252. {
  253. char *message = NULL;
  254. DWORD len = FormatMessageA(
  255. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
  256. error, 0, (LPSTR)&message, 0, NULL);
  257. if (len > 0) {
  258. // Note: FormatMessage includes a newline at the end of the message
  259. fprintf(stderr, "%s: %s", function_name, message);
  260. LocalFree(message);
  261. } else {
  262. fprintf(stderr, "%s: error %lu\n", function_name, error);
  263. }
  264. }
  265. intptr_t
  266. random(void)
  267. {
  268. unsigned int x;
  269. rand_s(&x);
  270. return x & INT_MAX;
  271. }
  272. unsigned int
  273. sleep(unsigned int seconds)
  274. {
  275. Sleep(seconds * 1000);
  276. return 0;
  277. }
  278. int
  279. usleep(unsigned int usec)
  280. {
  281. Sleep((usec + 999) / 1000);
  282. return 0;
  283. }