c5292c293058b7641fdb0e6a0f8aa429f82f80b7
[sbcl.git] / src / runtime / win32-os.c
1 /*
2  * the Win32 incarnation of OS-dependent routines.  See also
3  * $(sbcl_arch)-win32-os.c
4  *
5  * This file (along with os.h) exports an OS-independent interface to
6  * the operating system VM facilities. Surprise surprise, this
7  * interface looks a lot like the Mach interface (but simpler in some
8  * places). For some operating systems, a subset of these functions
9  * will have to be emulated.
10  */
11
12 /*
13  * This software is part of the SBCL system. See the README file for
14  * more information.
15  *
16  * This software is derived from the CMU CL system, which was
17  * written at Carnegie Mellon University and released into the
18  * public domain. The software is in the public domain and is
19  * provided with absolutely no warranty. See the COPYING and CREDITS
20  * files for more information.
21  */
22
23 /*
24  * This file was copied from the Linux version of the same, and
25  * likely still has some linuxisms in it have haven't been elimiated
26  * yet.
27  */
28
29 #include <malloc.h>
30 #include <stdio.h>
31 #include <sys/param.h>
32 #include <sys/file.h>
33 #include <io.h>
34 #include "sbcl.h"
35 #include "./signal.h"
36 #include "os.h"
37 #include "arch.h"
38 #include "globals.h"
39 #include "sbcl.h"
40 #include "interrupt.h"
41 #include "interr.h"
42 #include "lispregs.h"
43 #include "runtime.h"
44 #include "alloc.h"
45 #include "genesis/primitive-objects.h"
46
47 #include <sys/types.h>
48 #include <signal.h>
49 #include <sys/time.h>
50 #include <sys/stat.h>
51 #include <unistd.h>
52
53 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
54  * shlobj.h.
55  *
56  * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
57  * or get rid of our own boolean type.
58  */
59 #define boolean rpcndr_boolean
60 #include <shlobj.h>
61 #undef boolean
62
63 #include <math.h>
64
65 #include <excpt.h>
66
67 #include "validate.h"
68 #include "thread.h"
69 size_t os_vm_page_size;
70
71 #include "gc.h"
72 #include "gencgc-internal.h"
73
74 #if 0
75 int linux_sparc_siginfo_bug = 0;
76 int linux_supports_futex=0;
77 #endif
78
79 /* The exception handling function looks like this: */
80 EXCEPTION_DISPOSITION handle_exception(EXCEPTION_RECORD *,
81                                        struct lisp_exception_frame *,
82                                        CONTEXT *,
83                                        void *);
84
85 void *base_seh_frame;
86
87 static void *get_seh_frame(void)
88 {
89     void* retval;
90     asm volatile ("movl %%fs:0,%0": "=r" (retval));
91     return retval;
92 }
93
94 static void set_seh_frame(void *frame)
95 {
96     asm volatile ("movl %0,%%fs:0": : "r" (frame));
97 }
98
99 static struct lisp_exception_frame *find_our_seh_frame(void)
100 {
101     struct lisp_exception_frame *frame = get_seh_frame();
102
103     while (frame->handler != handle_exception)
104         frame = frame->next_frame;
105
106     return frame;
107 }
108
109 #if 0
110 inline static void *get_stack_frame(void)
111 {
112     void* retval;
113     asm volatile ("movl %%ebp,%0": "=r" (retval));
114     return retval;
115 }
116 #endif
117
118 void os_init(char *argv[], char *envp[])
119 {
120     SYSTEM_INFO system_info;
121
122     GetSystemInfo(&system_info);
123     os_vm_page_size = system_info.dwPageSize;
124
125     base_seh_frame = get_seh_frame();
126 }
127
128
129 /*
130  * So we have three fun scenarios here.
131  *
132  * First, we could be being called to reserve the memory areas
133  * during initialization (prior to loading the core file).
134  *
135  * Second, we could be being called by the GC to commit a page
136  * that has just been decommitted (for easy zero-fill).
137  *
138  * Third, we could be being called by create_thread_struct()
139  * in order to create the sundry and various stacks.
140  *
141  * The third case is easy to pick out because it passes an
142  * addr of 0.
143  *
144  * The second case is easy to pick out because it will be for
145  * a range of memory that is MEM_RESERVE rather than MEM_FREE.
146  *
147  * The second case is also an easy implement, because we leave
148  * the memory as reserved (since we do lazy commits).
149  */
150
151 os_vm_address_t
152 os_validate(os_vm_address_t addr, os_vm_size_t len)
153 {
154     MEMORY_BASIC_INFORMATION mem_info;
155
156     if (!addr) {
157         /* the simple case first */
158         os_vm_address_t real_addr;
159         if (!(real_addr = VirtualAlloc(addr, len, MEM_COMMIT, PAGE_EXECUTE_READWRITE))) {
160             fprintf(stderr, "VirtualAlloc: 0x%lx.\n", GetLastError());
161             return 0;
162         }
163
164         return real_addr;
165     }
166
167     if (!VirtualQuery(addr, &mem_info, sizeof mem_info)) {
168         fprintf(stderr, "VirtualQuery: 0x%lx.\n", GetLastError());
169         return 0;
170     }
171
172     if ((mem_info.State == MEM_RESERVE) && (mem_info.RegionSize >=len)) return addr;
173
174     if (mem_info.State == MEM_RESERVE) {
175         fprintf(stderr, "validation of reserved space too short.\n");
176         fflush(stderr);
177     }
178
179     if (!VirtualAlloc(addr, len, (mem_info.State == MEM_RESERVE)? MEM_COMMIT: MEM_RESERVE, PAGE_EXECUTE_READWRITE)) {
180         fprintf(stderr, "VirtualAlloc: 0x%lx.\n", GetLastError());
181         return 0;
182     }
183
184     return addr;
185 }
186
187 /*
188  * For os_invalidate(), we merely decommit the memory rather than
189  * freeing the address space. This loses when freeing per-thread
190  * data and related memory since it leaks address space. It's not
191  * too lossy, however, since the two scenarios I'm aware of are
192  * fd-stream buffers, which are pooled rather than torched, and
193  * thread information, which I hope to pool (since windows creates
194  * threads at its own whim, and we probably want to be able to
195  * have them callback without funky magic on the part of the user,
196  * and full-on thread allocation is fairly heavyweight). Someone
197  * will probably shoot me down on this with some pithy comment on
198  * the use of (setf symbol-value) on a special variable. I'm happy
199  * for them.
200  */
201
202 void
203 os_invalidate(os_vm_address_t addr, os_vm_size_t len)
204 {
205     if (!VirtualFree(addr, len, MEM_DECOMMIT)) {
206         fprintf(stderr, "VirtualFree: 0x%lx.\n", GetLastError());
207     }
208 }
209
210 /*
211  * os_map() is called to map a chunk of the core file into memory.
212  *
213  * Unfortunately, Windows semantics completely screws this up, so
214  * we just add backing store from the swapfile to where the chunk
215  * goes and read it up like a normal file. We could consider using
216  * a lazy read (demand page) setup, but that would mean keeping an
217  * open file pointer for the core indefinately (and be one more
218  * thing to maintain).
219  */
220
221 os_vm_address_t
222 os_map(int fd, int offset, os_vm_address_t addr, os_vm_size_t len)
223 {
224     os_vm_size_t count;
225
226 #if 0
227     fprintf(stderr, "os_map: %d, 0x%x, %p, 0x%x.\n", fd, offset, addr, len);
228     fflush(stderr);
229 #endif
230
231     if (!VirtualAlloc(addr, len, MEM_COMMIT, PAGE_EXECUTE_READWRITE)) {
232         fprintf(stderr, "VirtualAlloc: 0x%lx.\n", GetLastError());
233         lose("os_map: VirtualAlloc failure");
234     }
235
236     if (lseek(fd, offset, SEEK_SET) == -1) {
237         lose("os_map: Seek failure.");
238     }
239
240     count = read(fd, addr, len);
241     if (count != len) {
242         fprintf(stderr, "expected 0x%x, read 0x%x.\n", len, count);
243         lose("os_map: Failed to read enough bytes.");
244     }
245
246     return addr;
247 }
248
249 static DWORD os_protect_modes[8] = {
250     PAGE_NOACCESS,
251     PAGE_READONLY,
252     PAGE_READWRITE,
253     PAGE_READWRITE,
254     PAGE_EXECUTE,
255     PAGE_EXECUTE_READ,
256     PAGE_EXECUTE_READWRITE,
257     PAGE_EXECUTE_READWRITE,
258 };
259
260 void
261 os_protect(os_vm_address_t address, os_vm_size_t length, os_vm_prot_t prot)
262 {
263     DWORD old_prot;
264
265     if (!VirtualProtect(address, length, os_protect_modes[prot], &old_prot)) {
266         fprintf(stderr, "VirtualProtect failed, code 0x%lx.\n", GetLastError());
267         fflush(stderr);
268     }
269 }
270
271 /* FIXME: Now that FOO_END, rather than FOO_SIZE, is the fundamental
272  * description of a space, we could probably punt this and just do
273  * (FOO_START <= x && x < FOO_END) everywhere it's called. */
274 static boolean
275 in_range_p(os_vm_address_t a, lispobj sbeg, size_t slen)
276 {
277     char* beg = (char*)((long)sbeg);
278     char* end = (char*)((long)sbeg) + slen;
279     char* adr = (char*)a;
280     return (adr >= beg && adr < end);
281 }
282
283 boolean
284 is_linkage_table_addr(os_vm_address_t addr)
285 {
286     return in_range_p(addr, LINKAGE_TABLE_SPACE_START, LINKAGE_TABLE_SPACE_END);
287 }
288
289 boolean
290 is_valid_lisp_addr(os_vm_address_t addr)
291 {
292     struct thread *th;
293     if(in_range_p(addr, READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE) ||
294        in_range_p(addr, STATIC_SPACE_START   , STATIC_SPACE_SIZE) ||
295        in_range_p(addr, DYNAMIC_SPACE_START  , dynamic_space_size))
296         return 1;
297     for_each_thread(th) {
298         if(((os_vm_address_t)th->control_stack_start <= addr) && (addr < (os_vm_address_t)th->control_stack_end))
299             return 1;
300         if(in_range_p(addr, (unsigned long)th->binding_stack_start, BINDING_STACK_SIZE))
301             return 1;
302     }
303     return 0;
304 }
305
306 /*
307  * any OS-dependent special low-level handling for signals
308  */
309
310 /* A tiny bit of interrupt.c state we want our paws on. */
311 extern boolean internal_errors_enabled;
312
313 /*
314  * FIXME: There is a potential problem with foreign code here.
315  * If we are running foreign code instead of lisp code and an
316  * exception occurs we arrange a call into Lisp. If the
317  * foreign code has installed an exception handler, we run the
318  * very great risk of throwing through their exception handler
319  * without asking it to unwind. This is more a problem with
320  * non-sigtrap (EXCEPTION_BREAKPOINT) exceptions, as they could
321  * reasonably be expected to happen in foreign code. We need to
322  * figure out the exception handler unwind semantics and adhere
323  * to them (probably by abusing the Lisp unwind-protect system)
324  * if we are going to handle this scenario correctly.
325  *
326  * A good explanation of the exception handling semantics is
327  * http://win32assembly.online.fr/Exceptionhandling.html .
328  * We will also need to handle this ourselves when foreign
329  * code tries to unwind -us-.
330  *
331  * When unwinding through foreign code we should unwind the
332  * Lisp stack to the entry from foreign code, then unwind the
333  * foreign code stack to the entry from Lisp, then resume
334  * unwinding in Lisp.
335  */
336
337 EXCEPTION_DISPOSITION sigtrap_emulator(CONTEXT *context,
338                                        struct lisp_exception_frame *exception_frame)
339 {
340     if (*((char *)context->Eip + 1) == trap_ContextRestore) {
341         /* This is the cleanup for what is immediately below, and
342          * for the generic exception handling further below. We
343          * have to memcpy() the original context (emulated sigtrap
344          * or normal exception) over our context and resume it. */
345         memcpy(context, &exception_frame->context, sizeof(CONTEXT));
346         return ExceptionContinueExecution;
347
348     } else {
349         /* Not a trap_ContextRestore, must be a sigtrap.
350          * sigtrap_trampoline is defined in x86-assem.S. */
351         extern void sigtrap_trampoline;
352
353         /*
354          * Unlike some other operating systems, Win32 leaves EIP
355          * pointing to the breakpoint instruction.
356          */
357         context->Eip++;
358
359         /* We're not on an alternate stack like we would be in some
360          * other operating systems, and we don't want to risk leaking
361          * any important resources if we throw out of the sigtrap
362          * handler, so we need to copy off our context to a "safe"
363          * place and then monkey with the return EIP to point to a
364          * trampoline which calls another function which copies the
365          * context out to a really-safe place and then calls the real
366          * sigtrap handler. When the real sigtrap handler returns, the
367          * trampoline then contains another breakpoint with a code of
368          * trap_ContextRestore (see above). Essentially the same
369          * mechanism is used by the generic exception path. There is
370          * a small window of opportunity between us copying the
371          * context to the "safe" place and the sigtrap wrapper copying
372          * it to the really-safe place (allocated in its stack frame)
373          * during which the context can be smashed. The only scenario
374          * I can come up with for this, however, involves a stack
375          * overflow occuring at just the wrong time (which makes one
376          * wonder how stack overflow exceptions even happen, given
377          * that we don't switch stacks for exception processing...) */
378         memcpy(&exception_frame->context, context, sizeof(CONTEXT));
379
380         /* FIXME: Why do we save the old EIP in EAX? The sigtrap_trampoline
381          * pushes it into stack, but the sigtrap_wrapper where the trampoline
382          * goes ignores it, and after the wrapper we hit the trap_ContextRestore,
383          * which nukes the whole context with the original one?
384          *
385          * Am I misreading this, or is the EAX here and in the
386          * trampoline superfluous? --NS 20061024 */
387         context->Eax = context->Eip;
388         context->Eip = (unsigned long)&sigtrap_trampoline;
389
390         /* and return */
391         return ExceptionContinueExecution;
392     }
393 }
394
395 void sigtrap_wrapper(void)
396 {
397     /*
398      * This is the wrapper around the sigtrap handler called from
399      * the trampoline returned to from the function above.
400      *
401      * There actually is a point to some of the commented-out code
402      * in this function, although it really belongs to the callback
403      * wrappers. Once it is installed there, it can probably be
404      * removed from here.
405      */
406     extern void sigtrap_handler(int signal, siginfo_t *info, void *context);
407
408 /*     volatile struct { */
409 /*      void *handler[2]; */
410     CONTEXT context;
411 /*     } handler; */
412
413     struct lisp_exception_frame *frame = find_our_seh_frame();
414
415 /*     wos_install_interrupt_handlers(handler); */
416 /*     handler.handler[0] = get_seh_frame(); */
417 /*     handler.handler[1] = &handle_exception; */
418 /*     set_seh_frame(&handler); */
419
420     memcpy(&context, &frame->context, sizeof(CONTEXT));
421     sigtrap_handler(0, NULL, &context);
422     memcpy(&frame->context, &context, sizeof(CONTEXT));
423
424 /*     set_seh_frame(handler.handler[0]); */
425 }
426
427 EXCEPTION_DISPOSITION handle_exception(EXCEPTION_RECORD *exception_record,
428                                        struct lisp_exception_frame *exception_frame,
429                                        CONTEXT *context,
430                                        void *dc) /* FIXME: What's dc again? */
431 {
432
433     /* For EXCEPTION_ACCESS_VIOLATION only. */
434     void *fault_address = (void *)exception_record->ExceptionInformation[1];
435
436     if (exception_record->ExceptionCode == EXCEPTION_BREAKPOINT) {
437         /* Pick off sigtrap case first. */
438         return sigtrap_emulator(context, exception_frame);
439
440     }
441     else if (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
442              (is_valid_lisp_addr(fault_address) ||
443               is_linkage_table_addr(fault_address))) {
444         /* Pick off GC-related memory fault next. */
445         MEMORY_BASIC_INFORMATION mem_info;
446
447         if (!VirtualQuery(fault_address, &mem_info, sizeof mem_info)) {
448             fprintf(stderr, "VirtualQuery: 0x%lx.\n", GetLastError());
449             lose("handle_exception: VirtualQuery failure");
450         }
451
452         if (mem_info.State == MEM_RESERVE) {
453             /* First use new page, lets get some memory for it. */
454             if (!VirtualAlloc(mem_info.BaseAddress, os_vm_page_size,
455                               MEM_COMMIT, PAGE_EXECUTE_READWRITE)) {
456                 fprintf(stderr, "VirtualAlloc: 0x%lx.\n", GetLastError());
457                 lose("handle_exception: VirtualAlloc failure");
458
459             } else {
460                 /*
461                  * Now, if the page is supposedly write-protected and this
462                  * is a write, tell the gc that it's been hit.
463                  *
464                  * FIXME: Are we supposed to fall-through to the Lisp
465                  * exception handler if the gc doesn't take the wp violation?
466                  */
467                 if (exception_record->ExceptionInformation[0]) {
468                     int index = find_page_index(fault_address);
469                     if ((index != -1) && (page_table[index].write_protected)) {
470                         gencgc_handle_wp_violation(fault_address);
471                     }
472                 }
473                 return ExceptionContinueExecution;
474             }
475
476         } else if (gencgc_handle_wp_violation(fault_address)) {
477             /* gc accepts the wp violation, so resume where we left off. */
478             return ExceptionContinueExecution;
479         }
480
481         /* All else failed, drop through to the lisp-side exception handler. */
482     }
483
484     /*
485      * If we fall through to here then we need to either forward
486      * the exception to the lisp-side exception handler if it's
487      * set up, or drop to LDB.
488      */
489
490     if (internal_errors_enabled) {
491         /* exception_trampoline is defined in x86-assem.S. */
492         extern void exception_trampoline;
493
494         /* We're making the somewhat arbitrary decision that having
495          * internal errors enabled means that lisp has sufficient
496          * marbles to be able to handle exceptions, but xceptions
497          * aren't supposed to happen during cold init or reinit
498          * anyway.
499          *
500          * We use the same mechanism as the sigtrap emulator above
501          * with just a couple changes. We obviously use a different
502          * trampoline and wrapper function, we kill out any live
503          * floating point exceptions, and we save off the exception
504          * record as well as the context. */
505
506         /* Save off context and exception information */
507         memcpy(&exception_frame->context, context, sizeof(CONTEXT));
508         memcpy(&exception_frame->exception, exception_record, sizeof(EXCEPTION_RECORD));
509
510         /* Set up to activate trampoline when we return
511          *
512          * FIXME: Why do we save the old EIP in EAX? The
513          * exception_trampoline pushes it into stack, but the wrapper
514          * where the trampoline goes ignores it, and then the wrapper
515          * unwinds from Lisp... WTF?
516          *
517          * Am I misreading this, or is the EAX here and in the
518          * trampoline superfluous? --NS 20061024 */
519         context->Eax = context->Eip;
520         context->Eip = (unsigned long)&exception_trampoline;
521
522         /* Make sure a floating-point trap doesn't kill us */
523         context->FloatSave.StatusWord &= ~0x3f;
524
525         /* And return. */
526         return ExceptionContinueExecution;
527     }
528
529     fprintf(stderr, "Exception Code: 0x%lx.\n", exception_record->ExceptionCode);
530     fprintf(stderr, "Faulting IP: 0x%lx.\n", (DWORD)exception_record->ExceptionAddress);
531     if (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
532         MEMORY_BASIC_INFORMATION mem_info;
533
534         if (VirtualQuery(fault_address, &mem_info, sizeof mem_info)) {
535             fprintf(stderr, "page status: 0x%lx.\n", mem_info.State);
536         }
537
538         fprintf(stderr, "Was writing: %ld, where: 0x%lx.\n",
539                 exception_record->ExceptionInformation[0],
540                 (DWORD)fault_address);
541     }
542
543     fflush(stderr);
544
545     fake_foreign_function_call(context);
546     lose("fake_foreign_function_call fell through");
547
548     /* FIXME: WTF? How are we supposed to end up here? */
549     return ExceptionContinueSearch;
550 }
551
552 void handle_win32_exception_wrapper(void)
553 {
554     struct lisp_exception_frame *frame = find_our_seh_frame();
555     CONTEXT context;
556     EXCEPTION_RECORD exception_record;
557     lispobj context_sap;
558     lispobj exception_record_sap;
559
560     memcpy(&context, &frame->context, sizeof(CONTEXT));
561     memcpy(&exception_record, &frame->exception, sizeof(EXCEPTION_RECORD));
562
563     fake_foreign_function_call(&context);
564
565     /* Allocate the SAP objects while the "interrupts" are still
566      * disabled. */
567     context_sap = alloc_sap(&context);
568     exception_record_sap = alloc_sap(&exception_record);
569
570     funcall2(SymbolFunction(HANDLE_WIN32_EXCEPTION), context_sap,
571              exception_record_sap);
572
573     /* FIXME: These never happen, as the Lisp-side call is
574      * to an ERROR, which means we must do a non-local exit
575      */
576     undo_fake_foreign_function_call(&context);
577     memcpy(&frame->context, &context, sizeof(CONTEXT));
578 }
579
580 void
581 wos_install_interrupt_handlers(struct lisp_exception_frame *handler)
582 {
583     handler->next_frame = get_seh_frame();
584     handler->handler = &handle_exception;
585     set_seh_frame(handler);
586 }
587
588 void bcopy(const void *src, void *dest, size_t n)
589 {
590     MoveMemory(dest, src, n);
591 }
592
593 /*
594  * The stubs below are replacements for the windows versions,
595  * which can -fail- when used in our memory spaces because they
596  * validate the memory spaces they are passed in a way that
597  * denies our exception handler a chance to run.
598  */
599
600 void *memmove(void *dest, const void *src, size_t n)
601 {
602     if (dest < src) {
603         int i;
604         for (i = 0; i < n; i++) *(((char *)dest)+i) = *(((char *)src)+i);
605     } else {
606         while (n--) *(((char *)dest)+n) = *(((char *)src)+n);
607     }
608     return dest;
609 }
610
611 void *memcpy(void *dest, const void *src, size_t n)
612 {
613     while (n--) *(((char *)dest)+n) = *(((char *)src)+n);
614     return dest;
615 }
616
617 char *dirname(char *path)
618 {
619     static char buf[PATH_MAX + 1];
620     size_t pathlen = strlen(path);
621     int i;
622
623     if (pathlen >= sizeof(buf)) {
624         lose("Pathname too long in dirname.\n");
625         return NULL;
626     }
627
628     strcpy(buf, path);
629     for (i = pathlen; i >= 0; --i) {
630         if (buf[i] == '/' || buf[i] == '\\') {
631             buf[i] = '\0';
632             break;
633         }
634     }
635
636     return buf;
637 }
638
639 /* This is a manually-maintained version of ldso_stubs.S. */
640
641 void scratch(void)
642 {
643     CloseHandle(0);
644     FlushConsoleInputBuffer(0);
645     FormatMessageA(0, 0, 0, 0, 0, 0, 0);
646     FreeLibrary(0);
647     GetACP();
648     GetConsoleCP();
649     GetConsoleOutputCP();
650     GetCurrentProcess();
651     GetExitCodeProcess(0, 0);
652     GetLastError();
653     GetOEMCP();
654     GetProcAddress(0, 0);
655     GetProcessTimes(0, 0, 0, 0, 0);
656     GetSystemTimeAsFileTime(0);
657     LoadLibrary(0);
658     LocalFree(0);
659     PeekConsoleInput(0, 0, 0, 0);
660     PeekNamedPipe(0, 0, 0, 0, 0, 0);
661     ReadFile(0, 0, 0, 0, 0);
662     Sleep(0);
663     WriteFile(0, 0, 0, 0, 0);
664     _get_osfhandle(0);
665     _pipe(0,0,0);
666     access(0,0);
667     acos(0);
668     asin(0);
669     close(0);
670     cosh(0);
671     dup(0);
672     hypot(0, 0);
673     isatty(0);
674     sinh(0);
675     strerror(42);
676     write(0, 0, 0);
677     #ifndef LISP_FEATURE_SB_UNICODE
678       CreateDirectoryA(0,0);
679       GetComputerNameA(0, 0);
680       GetCurrentDirectoryA(0,0);
681       GetEnvironmentVariableA(0, 0, 0);
682       GetVersionExA(0);
683       MoveFileA(0,0);
684       SHGetFolderPathA(0, 0, 0, 0, 0);
685       SetCurrentDirectoryA(0);
686       SetEnvironmentVariableA(0, 0);
687     #else
688       CreateDirectoryW(0,0);
689       FormatMessageW(0, 0, 0, 0, 0, 0, 0);
690       GetComputerNameW(0, 0);
691       GetCurrentDirectoryW(0,0);
692       GetEnvironmentVariableW(0, 0, 0);
693       GetVersionExW(0);
694       MoveFileW(0,0);
695       SHGetFolderPathW(0, 0, 0, 0, 0);
696       SetCurrentDirectoryW(0);
697       SetEnvironmentVariableW(0, 0);
698     #endif
699 }
700
701 char *
702 os_get_runtime_executable_path()
703 {
704     char path[MAX_PATH + 1];
705     DWORD bufsize = sizeof(path);
706     DWORD size;
707
708     if ((size = GetModuleFileNameA(NULL, path, bufsize)) == 0)
709         return NULL;
710     else if (size == bufsize && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
711         return NULL;
712
713     return copied_string(path);
714 }
715
716 /* EOF */