V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
platform-win32.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Platform-specific code for Win32.
6 
7 // Secure API functions are not available using MinGW with msvcrt.dll
8 // on Windows XP. Make sure MINGW_HAS_SECURE_API is not defined to
9 // disable definition of secure API functions in standard headers that
10 // would conflict with our own implementation.
11 #ifdef __MINGW32__
12 #include <_mingw.h>
13 #ifdef MINGW_HAS_SECURE_API
14 #undef MINGW_HAS_SECURE_API
15 #endif // MINGW_HAS_SECURE_API
16 #endif // __MINGW32__
17 
18 #include <limits>
19 
20 #include "src/base/win32-headers.h"
21 
22 #include "src/base/bits.h"
23 #include "src/base/lazy-instance.h"
24 #include "src/base/macros.h"
25 #include "src/base/platform/platform.h"
26 #include "src/base/platform/time.h"
27 #include "src/base/timezone-cache.h"
28 #include "src/base/utils/random-number-generator.h"
29 
30 #include <VersionHelpers.h>
31 
32 #if defined(_MSC_VER)
33 #include <crtdbg.h> // NOLINT
34 #endif // defined(_MSC_VER)
35 
36 // Extra functions for MinGW. Most of these are the _s functions which are in
37 // the Microsoft Visual Studio C++ CRT.
38 #ifdef __MINGW32__
39 
40 
41 #ifndef __MINGW64_VERSION_MAJOR
42 
43 #define _TRUNCATE 0
44 #define STRUNCATE 80
45 
46 inline void MemoryFence() {
47  int barrier = 0;
48  __asm__ __volatile__("xchgl %%eax,%0 ":"=r" (barrier));
49 }
50 
51 #endif // __MINGW64_VERSION_MAJOR
52 
53 
54 int localtime_s(tm* out_tm, const time_t* time) {
55  tm* posix_local_time_struct = localtime_r(time, out_tm);
56  if (posix_local_time_struct == nullptr) return 1;
57  return 0;
58 }
59 
60 
61 int fopen_s(FILE** pFile, const char* filename, const char* mode) {
62  *pFile = fopen(filename, mode);
63  return *pFile != nullptr ? 0 : 1;
64 }
65 
66 int _vsnprintf_s(char* buffer, size_t sizeOfBuffer, size_t count,
67  const char* format, va_list argptr) {
68  DCHECK(count == _TRUNCATE);
69  return _vsnprintf(buffer, sizeOfBuffer, format, argptr);
70 }
71 
72 
73 int strncpy_s(char* dest, size_t dest_size, const char* source, size_t count) {
74  CHECK(source != nullptr);
75  CHECK(dest != nullptr);
76  CHECK_GT(dest_size, 0);
77 
78  if (count == _TRUNCATE) {
79  while (dest_size > 0 && *source != 0) {
80  *(dest++) = *(source++);
81  --dest_size;
82  }
83  if (dest_size == 0) {
84  *(dest - 1) = 0;
85  return STRUNCATE;
86  }
87  } else {
88  while (dest_size > 0 && count > 0 && *source != 0) {
89  *(dest++) = *(source++);
90  --dest_size;
91  --count;
92  }
93  }
94  CHECK_GT(dest_size, 0);
95  *dest = 0;
96  return 0;
97 }
98 
99 #endif // __MINGW32__
100 
101 namespace v8 {
102 namespace base {
103 
104 namespace {
105 
106 bool g_hard_abort = false;
107 
108 } // namespace
109 
111  public:
112  WindowsTimezoneCache() : initialized_(false) {}
113 
114  ~WindowsTimezoneCache() override {}
115 
116  void Clear() override { initialized_ = false; }
117 
118  const char* LocalTimezone(double time) override;
119 
120  double LocalTimeOffset(double time, bool is_utc) override;
121 
122  double DaylightSavingsOffset(double time) override;
123 
124  // Initialize timezone information. The timezone information is obtained from
125  // windows. If we cannot get the timezone information we fall back to CET.
126  void InitializeIfNeeded() {
127  // Just return if timezone information has already been initialized.
128  if (initialized_) return;
129 
130  // Initialize POSIX time zone data.
131  _tzset();
132  // Obtain timezone information from operating system.
133  memset(&tzinfo_, 0, sizeof(tzinfo_));
134  if (GetTimeZoneInformation(&tzinfo_) == TIME_ZONE_ID_INVALID) {
135  // If we cannot get timezone information we fall back to CET.
136  tzinfo_.Bias = -60;
137  tzinfo_.StandardDate.wMonth = 10;
138  tzinfo_.StandardDate.wDay = 5;
139  tzinfo_.StandardDate.wHour = 3;
140  tzinfo_.StandardBias = 0;
141  tzinfo_.DaylightDate.wMonth = 3;
142  tzinfo_.DaylightDate.wDay = 5;
143  tzinfo_.DaylightDate.wHour = 2;
144  tzinfo_.DaylightBias = -60;
145  }
146 
147  // Make standard and DST timezone names.
148  WideCharToMultiByte(CP_UTF8, 0, tzinfo_.StandardName, -1, std_tz_name_,
149  kTzNameSize, nullptr, nullptr);
150  std_tz_name_[kTzNameSize - 1] = '\0';
151  WideCharToMultiByte(CP_UTF8, 0, tzinfo_.DaylightName, -1, dst_tz_name_,
152  kTzNameSize, nullptr, nullptr);
153  dst_tz_name_[kTzNameSize - 1] = '\0';
154 
155  // If OS returned empty string or resource id (like "@tzres.dll,-211")
156  // simply guess the name from the UTC bias of the timezone.
157  // To properly resolve the resource identifier requires a library load,
158  // which is not possible in a sandbox.
159  if (std_tz_name_[0] == '\0' || std_tz_name_[0] == '@') {
160  OS::SNPrintF(std_tz_name_, kTzNameSize - 1,
161  "%s Standard Time",
162  GuessTimezoneNameFromBias(tzinfo_.Bias));
163  }
164  if (dst_tz_name_[0] == '\0' || dst_tz_name_[0] == '@') {
165  OS::SNPrintF(dst_tz_name_, kTzNameSize - 1,
166  "%s Daylight Time",
167  GuessTimezoneNameFromBias(tzinfo_.Bias));
168  }
169  // Timezone information initialized.
170  initialized_ = true;
171  }
172 
173  // Guess the name of the timezone from the bias.
174  // The guess is very biased towards the northern hemisphere.
175  const char* GuessTimezoneNameFromBias(int bias) {
176  static const int kHour = 60;
177  switch (-bias) {
178  case -9*kHour: return "Alaska";
179  case -8*kHour: return "Pacific";
180  case -7*kHour: return "Mountain";
181  case -6*kHour: return "Central";
182  case -5*kHour: return "Eastern";
183  case -4*kHour: return "Atlantic";
184  case 0*kHour: return "GMT";
185  case +1*kHour: return "Central Europe";
186  case +2*kHour: return "Eastern Europe";
187  case +3*kHour: return "Russia";
188  case +5*kHour + 30: return "India";
189  case +8*kHour: return "China";
190  case +9*kHour: return "Japan";
191  case +12*kHour: return "New Zealand";
192  default: return "Local";
193  }
194  }
195 
196 
197  private:
198  static const int kTzNameSize = 128;
199  bool initialized_;
200  char std_tz_name_[kTzNameSize];
201  char dst_tz_name_[kTzNameSize];
202  TIME_ZONE_INFORMATION tzinfo_;
203  friend class Win32Time;
204 };
205 
206 
207 // ----------------------------------------------------------------------------
208 // The Time class represents time on win32. A timestamp is represented as
209 // a 64-bit integer in 100 nanoseconds since January 1, 1601 (UTC). JavaScript
210 // timestamps are represented as a doubles in milliseconds since 00:00:00 UTC,
211 // January 1, 1970.
212 
213 class Win32Time {
214  public:
215  // Constructors.
216  Win32Time();
217  explicit Win32Time(double jstime);
218  Win32Time(int year, int mon, int day, int hour, int min, int sec);
219 
220  // Convert timestamp to JavaScript representation.
221  double ToJSTime();
222 
223  // Set timestamp to current time.
224  void SetToCurrentTime();
225 
226  // Returns the local timezone offset in milliseconds east of UTC. This is
227  // the number of milliseconds you must add to UTC to get local time, i.e.
228  // LocalOffset(CET) = 3600000 and LocalOffset(PST) = -28800000. This
229  // routine also takes into account whether daylight saving is effect
230  // at the time.
231  int64_t LocalOffset(WindowsTimezoneCache* cache);
232 
233  // Returns the daylight savings time offset for the time in milliseconds.
234  int64_t DaylightSavingsOffset(WindowsTimezoneCache* cache);
235 
236  // Returns a string identifying the current timezone for the
237  // timestamp taking into account daylight saving.
238  char* LocalTimezone(WindowsTimezoneCache* cache);
239 
240  private:
241  // Constants for time conversion.
242  static const int64_t kTimeEpoc = 116444736000000000LL;
243  static const int64_t kTimeScaler = 10000;
244  static const int64_t kMsPerMinute = 60000;
245 
246  // Constants for timezone information.
247  static const bool kShortTzNames = false;
248 
249  // Return whether or not daylight savings time is in effect at this time.
250  bool InDST(WindowsTimezoneCache* cache);
251 
252  // Accessor for FILETIME representation.
253  FILETIME& ft() { return time_.ft_; }
254 
255  // Accessor for integer representation.
256  int64_t& t() { return time_.t_; }
257 
258  // Although win32 uses 64-bit integers for representing timestamps,
259  // these are packed into a FILETIME structure. The FILETIME structure
260  // is just a struct representing a 64-bit integer. The TimeStamp union
261  // allows access to both a FILETIME and an integer representation of
262  // the timestamp.
263  union TimeStamp {
264  FILETIME ft_;
265  int64_t t_;
266  };
267 
268  TimeStamp time_;
269 };
270 
271 
272 // Initialize timestamp to start of epoc.
273 Win32Time::Win32Time() {
274  t() = 0;
275 }
276 
277 
278 // Initialize timestamp from a JavaScript timestamp.
279 Win32Time::Win32Time(double jstime) {
280  t() = static_cast<int64_t>(jstime) * kTimeScaler + kTimeEpoc;
281 }
282 
283 
284 // Initialize timestamp from date/time components.
285 Win32Time::Win32Time(int year, int mon, int day, int hour, int min, int sec) {
286  SYSTEMTIME st;
287  st.wYear = year;
288  st.wMonth = mon;
289  st.wDay = day;
290  st.wHour = hour;
291  st.wMinute = min;
292  st.wSecond = sec;
293  st.wMilliseconds = 0;
294  SystemTimeToFileTime(&st, &ft());
295 }
296 
297 
298 // Convert timestamp to JavaScript timestamp.
299 double Win32Time::ToJSTime() {
300  return static_cast<double>((t() - kTimeEpoc) / kTimeScaler);
301 }
302 
303 
304 // Set timestamp to current time.
305 void Win32Time::SetToCurrentTime() {
306  // The default GetSystemTimeAsFileTime has a ~15.5ms resolution.
307  // Because we're fast, we like fast timers which have at least a
308  // 1ms resolution.
309  //
310  // timeGetTime() provides 1ms granularity when combined with
311  // timeBeginPeriod(). If the host application for v8 wants fast
312  // timers, it can use timeBeginPeriod to increase the resolution.
313  //
314  // Using timeGetTime() has a drawback because it is a 32bit value
315  // and hence rolls-over every ~49days.
316  //
317  // To use the clock, we use GetSystemTimeAsFileTime as our base;
318  // and then use timeGetTime to extrapolate current time from the
319  // start time. To deal with rollovers, we resync the clock
320  // any time when more than kMaxClockElapsedTime has passed or
321  // whenever timeGetTime creates a rollover.
322 
323  static bool initialized = false;
324  static TimeStamp init_time;
325  static DWORD init_ticks;
326  static const int64_t kHundredNanosecondsPerSecond = 10000000;
327  static const int64_t kMaxClockElapsedTime =
328  60*kHundredNanosecondsPerSecond; // 1 minute
329 
330  // If we are uninitialized, we need to resync the clock.
331  bool needs_resync = !initialized;
332 
333  // Get the current time.
334  TimeStamp time_now;
335  GetSystemTimeAsFileTime(&time_now.ft_);
336  DWORD ticks_now = timeGetTime();
337 
338  // Check if we need to resync due to clock rollover.
339  needs_resync |= ticks_now < init_ticks;
340 
341  // Check if we need to resync due to elapsed time.
342  needs_resync |= (time_now.t_ - init_time.t_) > kMaxClockElapsedTime;
343 
344  // Check if we need to resync due to backwards time change.
345  needs_resync |= time_now.t_ < init_time.t_;
346 
347  // Resync the clock if necessary.
348  if (needs_resync) {
349  GetSystemTimeAsFileTime(&init_time.ft_);
350  init_ticks = ticks_now = timeGetTime();
351  initialized = true;
352  }
353 
354  // Finally, compute the actual time. Why is this so hard.
355  DWORD elapsed = ticks_now - init_ticks;
356  this->time_.t_ = init_time.t_ + (static_cast<int64_t>(elapsed) * 10000);
357 }
358 
359 
360 // Return the local timezone offset in milliseconds east of UTC. This
361 // takes into account whether daylight saving is in effect at the time.
362 // Only times in the 32-bit Unix range may be passed to this function.
363 // Also, adding the time-zone offset to the input must not overflow.
364 // The function EquivalentTime() in date.js guarantees this.
365 int64_t Win32Time::LocalOffset(WindowsTimezoneCache* cache) {
366  cache->InitializeIfNeeded();
367 
368  Win32Time rounded_to_second(*this);
369  rounded_to_second.t() =
370  rounded_to_second.t() / 1000 / kTimeScaler * 1000 * kTimeScaler;
371  // Convert to local time using POSIX localtime function.
372  // Windows XP Service Pack 3 made SystemTimeToTzSpecificLocalTime()
373  // very slow. Other browsers use localtime().
374 
375  // Convert from JavaScript milliseconds past 1/1/1970 0:00:00 to
376  // POSIX seconds past 1/1/1970 0:00:00.
377  double unchecked_posix_time = rounded_to_second.ToJSTime() / 1000;
378  if (unchecked_posix_time > INT_MAX || unchecked_posix_time < 0) {
379  return 0;
380  }
381  // Because _USE_32BIT_TIME_T is defined, time_t is a 32-bit int.
382  time_t posix_time = static_cast<time_t>(unchecked_posix_time);
383 
384  // Convert to local time, as struct with fields for day, hour, year, etc.
385  tm posix_local_time_struct;
386  if (localtime_s(&posix_local_time_struct, &posix_time)) return 0;
387 
388  if (posix_local_time_struct.tm_isdst > 0) {
389  return (cache->tzinfo_.Bias + cache->tzinfo_.DaylightBias) * -kMsPerMinute;
390  } else if (posix_local_time_struct.tm_isdst == 0) {
391  return (cache->tzinfo_.Bias + cache->tzinfo_.StandardBias) * -kMsPerMinute;
392  } else {
393  return cache->tzinfo_.Bias * -kMsPerMinute;
394  }
395 }
396 
397 
398 // Return whether or not daylight savings time is in effect at this time.
399 bool Win32Time::InDST(WindowsTimezoneCache* cache) {
400  cache->InitializeIfNeeded();
401 
402  // Determine if DST is in effect at the specified time.
403  bool in_dst = false;
404  if (cache->tzinfo_.StandardDate.wMonth != 0 ||
405  cache->tzinfo_.DaylightDate.wMonth != 0) {
406  // Get the local timezone offset for the timestamp in milliseconds.
407  int64_t offset = LocalOffset(cache);
408 
409  // Compute the offset for DST. The bias parameters in the timezone info
410  // are specified in minutes. These must be converted to milliseconds.
411  int64_t dstofs =
412  -(cache->tzinfo_.Bias + cache->tzinfo_.DaylightBias) * kMsPerMinute;
413 
414  // If the local time offset equals the timezone bias plus the daylight
415  // bias then DST is in effect.
416  in_dst = offset == dstofs;
417  }
418 
419  return in_dst;
420 }
421 
422 
423 // Return the daylight savings time offset for this time.
424 int64_t Win32Time::DaylightSavingsOffset(WindowsTimezoneCache* cache) {
425  return InDST(cache) ? 60 * kMsPerMinute : 0;
426 }
427 
428 
429 // Returns a string identifying the current timezone for the
430 // timestamp taking into account daylight saving.
431 char* Win32Time::LocalTimezone(WindowsTimezoneCache* cache) {
432  // Return the standard or DST time zone name based on whether daylight
433  // saving is in effect at the given time.
434  return InDST(cache) ? cache->dst_tz_name_ : cache->std_tz_name_;
435 }
436 
437 
438 // Returns the accumulated user time for thread.
439 int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) {
440  FILETIME dummy;
441  uint64_t usertime;
442 
443  // Get the amount of time that the thread has executed in user mode.
444  if (!GetThreadTimes(GetCurrentThread(), &dummy, &dummy, &dummy,
445  reinterpret_cast<FILETIME*>(&usertime))) return -1;
446 
447  // Adjust the resolution to micro-seconds.
448  usertime /= 10;
449 
450  // Convert to seconds and microseconds
451  *secs = static_cast<uint32_t>(usertime / 1000000);
452  *usecs = static_cast<uint32_t>(usertime % 1000000);
453  return 0;
454 }
455 
456 
457 // Returns current time as the number of milliseconds since
458 // 00:00:00 UTC, January 1, 1970.
459 double OS::TimeCurrentMillis() {
460  return Time::Now().ToJsTime();
461 }
462 
463 // Returns a string identifying the current timezone taking into
464 // account daylight saving.
465 const char* WindowsTimezoneCache::LocalTimezone(double time) {
466  return Win32Time(time).LocalTimezone(this);
467 }
468 
469 // Returns the local time offset in milliseconds east of UTC without
470 // taking daylight savings time into account.
471 double WindowsTimezoneCache::LocalTimeOffset(double time_ms, bool is_utc) {
472  // Ignore is_utc and time_ms for now. That way, the behavior wouldn't
473  // change with icu_timezone_data disabled.
474  // Use current time, rounded to the millisecond.
475  Win32Time t(OS::TimeCurrentMillis());
476  // Time::LocalOffset inlcudes any daylight savings offset, so subtract it.
477  return static_cast<double>(t.LocalOffset(this) -
478  t.DaylightSavingsOffset(this));
479 }
480 
481 // Returns the daylight savings offset in milliseconds for the given
482 // time.
483 double WindowsTimezoneCache::DaylightSavingsOffset(double time) {
484  int64_t offset = Win32Time(time).DaylightSavingsOffset(this);
485  return static_cast<double>(offset);
486 }
487 
488 TimezoneCache* OS::CreateTimezoneCache() { return new WindowsTimezoneCache(); }
489 
490 int OS::GetLastError() {
491  return ::GetLastError();
492 }
493 
494 
495 int OS::GetCurrentProcessId() {
496  return static_cast<int>(::GetCurrentProcessId());
497 }
498 
499 
500 int OS::GetCurrentThreadId() {
501  return static_cast<int>(::GetCurrentThreadId());
502 }
503 
504 void OS::ExitProcess(int exit_code) {
505  // Use TerminateProcess avoid races between isolate threads and
506  // static destructors.
507  fflush(stdout);
508  fflush(stderr);
509  TerminateProcess(GetCurrentProcess(), exit_code);
510 }
511 
512 // ----------------------------------------------------------------------------
513 // Win32 console output.
514 //
515 // If a Win32 application is linked as a console application it has a normal
516 // standard output and standard error. In this case normal printf works fine
517 // for output. However, if the application is linked as a GUI application,
518 // the process doesn't have a console, and therefore (debugging) output is lost.
519 // This is the case if we are embedded in a windows program (like a browser).
520 // In order to be able to get debug output in this case the the debugging
521 // facility using OutputDebugString. This output goes to the active debugger
522 // for the process (if any). Else the output can be monitored using DBMON.EXE.
523 
524 enum OutputMode {
525  UNKNOWN, // Output method has not yet been determined.
526  CONSOLE, // Output is written to stdout.
527  ODS // Output is written to debug facility.
528 };
529 
530 static OutputMode output_mode = UNKNOWN; // Current output mode.
531 
532 
533 // Determine if the process has a console for output.
534 static bool HasConsole() {
535  // Only check the first time. Eventual race conditions are not a problem,
536  // because all threads will eventually determine the same mode.
537  if (output_mode == UNKNOWN) {
538  // We cannot just check that the standard output is attached to a console
539  // because this would fail if output is redirected to a file. Therefore we
540  // say that a process does not have an output console if either the
541  // standard output handle is invalid or its file type is unknown.
542  if (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE &&
543  GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)) != FILE_TYPE_UNKNOWN)
544  output_mode = CONSOLE;
545  else
546  output_mode = ODS;
547  }
548  return output_mode == CONSOLE;
549 }
550 
551 
552 static void VPrintHelper(FILE* stream, const char* format, va_list args) {
553  if ((stream == stdout || stream == stderr) && !HasConsole()) {
554  // It is important to use safe print here in order to avoid
555  // overflowing the buffer. We might truncate the output, but this
556  // does not crash.
557  char buffer[4096];
558  OS::VSNPrintF(buffer, sizeof(buffer), format, args);
559  OutputDebugStringA(buffer);
560  } else {
561  vfprintf(stream, format, args);
562  }
563 }
564 
565 
566 FILE* OS::FOpen(const char* path, const char* mode) {
567  FILE* result;
568  if (fopen_s(&result, path, mode) == 0) {
569  return result;
570  } else {
571  return nullptr;
572  }
573 }
574 
575 
576 bool OS::Remove(const char* path) {
577  return (DeleteFileA(path) != 0);
578 }
579 
580 char OS::DirectorySeparator() { return '\\'; }
581 
582 bool OS::isDirectorySeparator(const char ch) {
583  return ch == '/' || ch == '\\';
584 }
585 
586 
587 FILE* OS::OpenTemporaryFile() {
588  // tmpfile_s tries to use the root dir, don't use it.
589  char tempPathBuffer[MAX_PATH];
590  DWORD path_result = 0;
591  path_result = GetTempPathA(MAX_PATH, tempPathBuffer);
592  if (path_result > MAX_PATH || path_result == 0) return nullptr;
593  UINT name_result = 0;
594  char tempNameBuffer[MAX_PATH];
595  name_result = GetTempFileNameA(tempPathBuffer, "", 0, tempNameBuffer);
596  if (name_result == 0) return nullptr;
597  FILE* result = FOpen(tempNameBuffer, "w+"); // Same mode as tmpfile uses.
598  if (result != nullptr) {
599  Remove(tempNameBuffer); // Delete on close.
600  }
601  return result;
602 }
603 
604 
605 // Open log file in binary mode to avoid /n -> /r/n conversion.
606 const char* const OS::LogFileOpenMode = "wb";
607 
608 
609 // Print (debug) message to console.
610 void OS::Print(const char* format, ...) {
611  va_list args;
612  va_start(args, format);
613  VPrint(format, args);
614  va_end(args);
615 }
616 
617 
618 void OS::VPrint(const char* format, va_list args) {
619  VPrintHelper(stdout, format, args);
620 }
621 
622 
623 void OS::FPrint(FILE* out, const char* format, ...) {
624  va_list args;
625  va_start(args, format);
626  VFPrint(out, format, args);
627  va_end(args);
628 }
629 
630 
631 void OS::VFPrint(FILE* out, const char* format, va_list args) {
632  VPrintHelper(out, format, args);
633 }
634 
635 
636 // Print error message to console.
637 void OS::PrintError(const char* format, ...) {
638  va_list args;
639  va_start(args, format);
640  VPrintError(format, args);
641  va_end(args);
642 }
643 
644 
645 void OS::VPrintError(const char* format, va_list args) {
646  VPrintHelper(stderr, format, args);
647 }
648 
649 
650 int OS::SNPrintF(char* str, int length, const char* format, ...) {
651  va_list args;
652  va_start(args, format);
653  int result = VSNPrintF(str, length, format, args);
654  va_end(args);
655  return result;
656 }
657 
658 
659 int OS::VSNPrintF(char* str, int length, const char* format, va_list args) {
660  int n = _vsnprintf_s(str, length, _TRUNCATE, format, args);
661  // Make sure to zero-terminate the string if the output was
662  // truncated or if there was an error.
663  if (n < 0 || n >= length) {
664  if (length > 0)
665  str[length - 1] = '\0';
666  return -1;
667  } else {
668  return n;
669  }
670 }
671 
672 
673 char* OS::StrChr(char* str, int c) {
674  return const_cast<char*>(strchr(str, c));
675 }
676 
677 
678 void OS::StrNCpy(char* dest, int length, const char* src, size_t n) {
679  // Use _TRUNCATE or strncpy_s crashes (by design) if buffer is too small.
680  size_t buffer_size = static_cast<size_t>(length);
681  if (n + 1 > buffer_size) // count for trailing '\0'
682  n = _TRUNCATE;
683  int result = strncpy_s(dest, length, src, n);
684  USE(result);
685  DCHECK(result == 0 || (n == _TRUNCATE && result == STRUNCATE));
686 }
687 
688 
689 #undef _TRUNCATE
690 #undef STRUNCATE
691 
692 static LazyInstance<RandomNumberGenerator>::type
693  platform_random_number_generator = LAZY_INSTANCE_INITIALIZER;
694 static LazyMutex rng_mutex = LAZY_MUTEX_INITIALIZER;
695 
696 void OS::Initialize(bool hard_abort, const char* const gc_fake_mmap) {
697  g_hard_abort = hard_abort;
698 }
699 
700 // static
701 size_t OS::AllocatePageSize() {
702  static size_t allocate_alignment = 0;
703  if (allocate_alignment == 0) {
704  SYSTEM_INFO info;
705  GetSystemInfo(&info);
706  allocate_alignment = info.dwAllocationGranularity;
707  }
708  return allocate_alignment;
709 }
710 
711 // static
712 size_t OS::CommitPageSize() {
713  static size_t page_size = 0;
714  if (page_size == 0) {
715  SYSTEM_INFO info;
716  GetSystemInfo(&info);
717  page_size = info.dwPageSize;
718  DCHECK_EQ(4096, page_size);
719  }
720  return page_size;
721 }
722 
723 // static
724 void OS::SetRandomMmapSeed(int64_t seed) {
725  if (seed) {
726  MutexGuard guard(rng_mutex.Pointer());
727  platform_random_number_generator.Pointer()->SetSeed(seed);
728  }
729 }
730 
731 // static
732 void* OS::GetRandomMmapAddr() {
733 // The address range used to randomize RWX allocations in OS::Allocate
734 // Try not to map pages into the default range that windows loads DLLs
735 // Use a multiple of 64k to prevent committing unused memory.
736 // Note: This does not guarantee RWX regions will be within the
737 // range kAllocationRandomAddressMin to kAllocationRandomAddressMax
738 #ifdef V8_HOST_ARCH_64_BIT
739  static const uintptr_t kAllocationRandomAddressMin = 0x0000000080000000;
740  static const uintptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
741 #else
742  static const uintptr_t kAllocationRandomAddressMin = 0x04000000;
743  static const uintptr_t kAllocationRandomAddressMax = 0x3FFF0000;
744 #endif
745  uintptr_t address;
746  {
747  MutexGuard guard(rng_mutex.Pointer());
748  platform_random_number_generator.Pointer()->NextBytes(&address,
749  sizeof(address));
750  }
751  address <<= kPageSizeBits;
752  address += kAllocationRandomAddressMin;
753  address &= kAllocationRandomAddressMax;
754  return reinterpret_cast<void*>(address);
755 }
756 
757 namespace {
758 
759 DWORD GetProtectionFromMemoryPermission(OS::MemoryPermission access) {
760  switch (access) {
761  case OS::MemoryPermission::kNoAccess:
762  return PAGE_NOACCESS;
763  case OS::MemoryPermission::kRead:
764  return PAGE_READONLY;
765  case OS::MemoryPermission::kReadWrite:
766  return PAGE_READWRITE;
767  case OS::MemoryPermission::kReadWriteExecute:
768  if (IsWindows10OrGreater())
769  return PAGE_EXECUTE_READWRITE | PAGE_TARGETS_INVALID;
770  return PAGE_EXECUTE_READWRITE;
771  case OS::MemoryPermission::kReadExecute:
772  if (IsWindows10OrGreater())
773  return PAGE_EXECUTE_READ | PAGE_TARGETS_INVALID;
774  return PAGE_EXECUTE_READ;
775  }
776  UNREACHABLE();
777 }
778 
779 uint8_t* RandomizedVirtualAlloc(size_t size, DWORD flags, DWORD protect,
780  void* hint) {
781  LPVOID base = nullptr;
782  static BOOL use_aslr = -1;
783 #ifdef V8_HOST_ARCH_32_BIT
784  // Don't bother randomizing on 32-bit hosts, because they lack the room and
785  // don't have viable ASLR anyway.
786  if (use_aslr == -1 && !IsWow64Process(GetCurrentProcess(), &use_aslr))
787  use_aslr = FALSE;
788 #else
789  use_aslr = TRUE;
790 #endif
791 
792  if (use_aslr && protect != PAGE_READWRITE) {
793  // For executable or reserved pages try to randomize the allocation address.
794  base = VirtualAlloc(hint, size, flags, protect);
795  }
796 
797  // On failure, let the OS find an address to use.
798  if (base == nullptr) {
799  base = VirtualAlloc(nullptr, size, flags, protect);
800  }
801  return reinterpret_cast<uint8_t*>(base);
802 }
803 
804 } // namespace
805 
806 // static
807 void* OS::Allocate(void* address, size_t size, size_t alignment,
808  MemoryPermission access) {
809  size_t page_size = AllocatePageSize();
810  DCHECK_EQ(0, size % page_size);
811  DCHECK_EQ(0, alignment % page_size);
812  DCHECK_LE(page_size, alignment);
813  address = AlignedAddress(address, alignment);
814 
815  DWORD flags = (access == OS::MemoryPermission::kNoAccess)
816  ? MEM_RESERVE
817  : MEM_RESERVE | MEM_COMMIT;
818  DWORD protect = GetProtectionFromMemoryPermission(access);
819 
820  // First, try an exact size aligned allocation.
821  uint8_t* base = RandomizedVirtualAlloc(size, flags, protect, address);
822  if (base == nullptr) return nullptr; // Can't allocate, we're OOM.
823 
824  // If address is suitably aligned, we're done.
825  uint8_t* aligned_base = reinterpret_cast<uint8_t*>(
826  RoundUp(reinterpret_cast<uintptr_t>(base), alignment));
827  if (base == aligned_base) return reinterpret_cast<void*>(base);
828 
829  // Otherwise, free it and try a larger allocation.
830  CHECK(Free(base, size));
831 
832  // Clear the hint. It's unlikely we can allocate at this address.
833  address = nullptr;
834 
835  // Add the maximum misalignment so we are guaranteed an aligned base address
836  // in the allocated region.
837  size_t padded_size = size + (alignment - page_size);
838  const int kMaxAttempts = 3;
839  aligned_base = nullptr;
840  for (int i = 0; i < kMaxAttempts; ++i) {
841  base = RandomizedVirtualAlloc(padded_size, flags, protect, address);
842  if (base == nullptr) return nullptr; // Can't allocate, we're OOM.
843 
844  // Try to trim the allocation by freeing the padded allocation and then
845  // calling VirtualAlloc at the aligned base.
846  CHECK(Free(base, padded_size));
847  aligned_base = reinterpret_cast<uint8_t*>(
848  RoundUp(reinterpret_cast<uintptr_t>(base), alignment));
849  base = reinterpret_cast<uint8_t*>(
850  VirtualAlloc(aligned_base, size, flags, protect));
851  // We might not get the reduced allocation due to a race. In that case,
852  // base will be nullptr.
853  if (base != nullptr) break;
854  }
855  DCHECK_IMPLIES(base, base == aligned_base);
856  return reinterpret_cast<void*>(base);
857 }
858 
859 // static
860 bool OS::Free(void* address, const size_t size) {
861  DCHECK_EQ(0, reinterpret_cast<uintptr_t>(address) % AllocatePageSize());
862  DCHECK_EQ(0, size % AllocatePageSize());
863  USE(size);
864  return VirtualFree(address, 0, MEM_RELEASE) != 0;
865 }
866 
867 // static
868 bool OS::Release(void* address, size_t size) {
869  DCHECK_EQ(0, reinterpret_cast<uintptr_t>(address) % CommitPageSize());
870  DCHECK_EQ(0, size % CommitPageSize());
871  return VirtualFree(address, size, MEM_DECOMMIT) != 0;
872 }
873 
874 // static
875 bool OS::SetPermissions(void* address, size_t size, MemoryPermission access) {
876  DCHECK_EQ(0, reinterpret_cast<uintptr_t>(address) % CommitPageSize());
877  DCHECK_EQ(0, size % CommitPageSize());
878  if (access == MemoryPermission::kNoAccess) {
879  return VirtualFree(address, size, MEM_DECOMMIT) != 0;
880  }
881  DWORD protect = GetProtectionFromMemoryPermission(access);
882  return VirtualAlloc(address, size, MEM_COMMIT, protect) != nullptr;
883 }
884 
885 // static
886 bool OS::DiscardSystemPages(void* address, size_t size) {
887  // On Windows, discarded pages are not returned to the system immediately and
888  // not guaranteed to be zeroed when returned to the application.
889  using DiscardVirtualMemoryFunction =
890  DWORD(WINAPI*)(PVOID virtualAddress, SIZE_T size);
891  static std::atomic<DiscardVirtualMemoryFunction> discard_virtual_memory(
892  reinterpret_cast<DiscardVirtualMemoryFunction>(-1));
893  if (discard_virtual_memory ==
894  reinterpret_cast<DiscardVirtualMemoryFunction>(-1))
895  discard_virtual_memory =
896  reinterpret_cast<DiscardVirtualMemoryFunction>(GetProcAddress(
897  GetModuleHandle(L"Kernel32.dll"), "DiscardVirtualMemory"));
898  // Use DiscardVirtualMemory when available because it releases faster than
899  // MEM_RESET.
900  DiscardVirtualMemoryFunction discard_function = discard_virtual_memory.load();
901  if (discard_function) {
902  DWORD ret = discard_function(address, size);
903  if (!ret) return true;
904  }
905  // DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
906  // failure.
907  void* ptr = VirtualAlloc(address, size, MEM_RESET, PAGE_READWRITE);
908  CHECK(ptr);
909  return ptr;
910 }
911 
912 // static
913 bool OS::HasLazyCommits() {
914  // TODO(alph): implement for the platform.
915  return false;
916 }
917 
918 void OS::Sleep(TimeDelta interval) {
919  ::Sleep(static_cast<DWORD>(interval.InMilliseconds()));
920 }
921 
922 
923 void OS::Abort() {
924  // Before aborting, make sure to flush output buffers.
925  fflush(stdout);
926  fflush(stderr);
927 
928  if (g_hard_abort) {
929  V8_IMMEDIATE_CRASH();
930  }
931  // Make the MSVCRT do a silent abort.
932  raise(SIGABRT);
933 
934  // Make sure function doesn't return.
935  abort();
936 }
937 
938 
939 void OS::DebugBreak() {
940 #if V8_CC_MSVC
941  // To avoid Visual Studio runtime support the following code can be used
942  // instead
943  // __asm { int 3 }
944  __debugbreak();
945 #else
946  ::DebugBreak();
947 #endif
948 }
949 
950 
952  public:
953  Win32MemoryMappedFile(HANDLE file, HANDLE file_mapping, void* memory,
954  size_t size)
955  : file_(file),
956  file_mapping_(file_mapping),
957  memory_(memory),
958  size_(size) {}
959  ~Win32MemoryMappedFile() final;
960  void* memory() const final { return memory_; }
961  size_t size() const final { return size_; }
962 
963  private:
964  HANDLE const file_;
965  HANDLE const file_mapping_;
966  void* const memory_;
967  size_t const size_;
968 };
969 
970 
971 // static
972 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
973  // Open a physical file
974  HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
975  FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
976  OPEN_EXISTING, 0, nullptr);
977  if (file == INVALID_HANDLE_VALUE) return nullptr;
978 
979  DWORD size = GetFileSize(file, nullptr);
980 
981  // Create a file mapping for the physical file
982  HANDLE file_mapping =
983  CreateFileMapping(file, nullptr, PAGE_READWRITE, 0, size, nullptr);
984  if (file_mapping == nullptr) return nullptr;
985 
986  // Map a view of the file into memory
987  void* memory = MapViewOfFile(file_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size);
988  return new Win32MemoryMappedFile(file, file_mapping, memory, size);
989 }
990 
991 
992 // static
993 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name,
994  size_t size, void* initial) {
995  // Open a physical file
996  HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
997  FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
998  OPEN_ALWAYS, 0, nullptr);
999  if (file == nullptr) return nullptr;
1000  // Create a file mapping for the physical file
1001  HANDLE file_mapping = CreateFileMapping(file, nullptr, PAGE_READWRITE, 0,
1002  static_cast<DWORD>(size), nullptr);
1003  if (file_mapping == nullptr) return nullptr;
1004  // Map a view of the file into memory
1005  void* memory = MapViewOfFile(file_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size);
1006  if (memory) memmove(memory, initial, size);
1007  return new Win32MemoryMappedFile(file, file_mapping, memory, size);
1008 }
1009 
1010 
1011 Win32MemoryMappedFile::~Win32MemoryMappedFile() {
1012  if (memory_) UnmapViewOfFile(memory_);
1013  CloseHandle(file_mapping_);
1014  CloseHandle(file_);
1015 }
1016 
1017 
1018 // The following code loads functions defined in DbhHelp.h and TlHelp32.h
1019 // dynamically. This is to avoid being depending on dbghelp.dll and
1020 // tlhelp32.dll when running (the functions in tlhelp32.dll have been moved to
1021 // kernel32.dll at some point so loading functions defines in TlHelp32.h
1022 // dynamically might not be necessary any more - for some versions of Windows?).
1023 
1024 // Function pointers to functions dynamically loaded from dbghelp.dll.
1025 #define DBGHELP_FUNCTION_LIST(V) \
1026  V(SymInitialize) \
1027  V(SymGetOptions) \
1028  V(SymSetOptions) \
1029  V(SymGetSearchPath) \
1030  V(SymLoadModule64) \
1031  V(StackWalk64) \
1032  V(SymGetSymFromAddr64) \
1033  V(SymGetLineFromAddr64) \
1034  V(SymFunctionTableAccess64) \
1035  V(SymGetModuleBase64)
1036 
1037 // Function pointers to functions dynamically loaded from dbghelp.dll.
1038 #define TLHELP32_FUNCTION_LIST(V) \
1039  V(CreateToolhelp32Snapshot) \
1040  V(Module32FirstW) \
1041  V(Module32NextW)
1042 
1043 // Define the decoration to use for the type and variable name used for
1044 // dynamically loaded DLL function..
1045 #define DLL_FUNC_TYPE(name) _##name##_
1046 #define DLL_FUNC_VAR(name) _##name
1047 
1048 // Define the type for each dynamically loaded DLL function. The function
1049 // definitions are copied from DbgHelp.h and TlHelp32.h. The IN and VOID macros
1050 // from the Windows include files are redefined here to have the function
1051 // definitions to be as close to the ones in the original .h files as possible.
1052 #ifndef IN
1053 #define IN
1054 #endif
1055 #ifndef VOID
1056 #define VOID void
1057 #endif
1058 
1059 // DbgHelp isn't supported on MinGW yet
1060 #ifndef __MINGW32__
1061 // DbgHelp.h functions.
1062 typedef BOOL (__stdcall *DLL_FUNC_TYPE(SymInitialize))(IN HANDLE hProcess,
1063  IN PSTR UserSearchPath,
1064  IN BOOL fInvadeProcess);
1065 typedef DWORD (__stdcall *DLL_FUNC_TYPE(SymGetOptions))(VOID);
1066 typedef DWORD (__stdcall *DLL_FUNC_TYPE(SymSetOptions))(IN DWORD SymOptions);
1067 typedef BOOL (__stdcall *DLL_FUNC_TYPE(SymGetSearchPath))(
1068  IN HANDLE hProcess,
1069  OUT PSTR SearchPath,
1070  IN DWORD SearchPathLength);
1071 typedef DWORD64 (__stdcall *DLL_FUNC_TYPE(SymLoadModule64))(
1072  IN HANDLE hProcess,
1073  IN HANDLE hFile,
1074  IN PSTR ImageName,
1075  IN PSTR ModuleName,
1076  IN DWORD64 BaseOfDll,
1077  IN DWORD SizeOfDll);
1078 typedef BOOL (__stdcall *DLL_FUNC_TYPE(StackWalk64))(
1079  DWORD MachineType,
1080  HANDLE hProcess,
1081  HANDLE hThread,
1082  LPSTACKFRAME64 StackFrame,
1083  PVOID ContextRecord,
1084  PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
1085  PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
1086  PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
1087  PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress);
1088 typedef BOOL (__stdcall *DLL_FUNC_TYPE(SymGetSymFromAddr64))(
1089  IN HANDLE hProcess,
1090  IN DWORD64 qwAddr,
1091  OUT PDWORD64 pdwDisplacement,
1092  OUT PIMAGEHLP_SYMBOL64 Symbol);
1093 typedef BOOL (__stdcall *DLL_FUNC_TYPE(SymGetLineFromAddr64))(
1094  IN HANDLE hProcess,
1095  IN DWORD64 qwAddr,
1096  OUT PDWORD pdwDisplacement,
1097  OUT PIMAGEHLP_LINE64 Line64);
1098 // DbgHelp.h typedefs. Implementation found in dbghelp.dll.
1099 typedef PVOID (__stdcall *DLL_FUNC_TYPE(SymFunctionTableAccess64))(
1100  HANDLE hProcess,
1101  DWORD64 AddrBase); // DbgHelp.h typedef PFUNCTION_TABLE_ACCESS_ROUTINE64
1102 typedef DWORD64 (__stdcall *DLL_FUNC_TYPE(SymGetModuleBase64))(
1103  HANDLE hProcess,
1104  DWORD64 AddrBase); // DbgHelp.h typedef PGET_MODULE_BASE_ROUTINE64
1105 
1106 // TlHelp32.h functions.
1107 typedef HANDLE (__stdcall *DLL_FUNC_TYPE(CreateToolhelp32Snapshot))(
1108  DWORD dwFlags,
1109  DWORD th32ProcessID);
1110 typedef BOOL (__stdcall *DLL_FUNC_TYPE(Module32FirstW))(HANDLE hSnapshot,
1111  LPMODULEENTRY32W lpme);
1112 typedef BOOL (__stdcall *DLL_FUNC_TYPE(Module32NextW))(HANDLE hSnapshot,
1113  LPMODULEENTRY32W lpme);
1114 
1115 #undef IN
1116 #undef VOID
1117 
1118 // Declare a variable for each dynamically loaded DLL function.
1119 #define DEF_DLL_FUNCTION(name) DLL_FUNC_TYPE(name) DLL_FUNC_VAR(name) = nullptr;
1120 DBGHELP_FUNCTION_LIST(DEF_DLL_FUNCTION)
1121 TLHELP32_FUNCTION_LIST(DEF_DLL_FUNCTION)
1122 #undef DEF_DLL_FUNCTION
1123 
1124 // Load the functions. This function has a lot of "ugly" macros in order to
1125 // keep down code duplication.
1126 
1127 static bool LoadDbgHelpAndTlHelp32() {
1128  static bool dbghelp_loaded = false;
1129 
1130  if (dbghelp_loaded) return true;
1131 
1132  HMODULE module;
1133 
1134  // Load functions from the dbghelp.dll module.
1135  module = LoadLibrary(TEXT("dbghelp.dll"));
1136  if (module == nullptr) {
1137  return false;
1138  }
1139 
1140 #define LOAD_DLL_FUNC(name) \
1141  DLL_FUNC_VAR(name) = \
1142  reinterpret_cast<DLL_FUNC_TYPE(name)>(GetProcAddress(module, #name));
1143 
1144 DBGHELP_FUNCTION_LIST(LOAD_DLL_FUNC)
1145 
1146 #undef LOAD_DLL_FUNC
1147 
1148  // Load functions from the kernel32.dll module (the TlHelp32.h function used
1149  // to be in tlhelp32.dll but are now moved to kernel32.dll).
1150  module = LoadLibrary(TEXT("kernel32.dll"));
1151  if (module == nullptr) {
1152  return false;
1153  }
1154 
1155 #define LOAD_DLL_FUNC(name) \
1156  DLL_FUNC_VAR(name) = \
1157  reinterpret_cast<DLL_FUNC_TYPE(name)>(GetProcAddress(module, #name));
1158 
1159 TLHELP32_FUNCTION_LIST(LOAD_DLL_FUNC)
1160 
1161 #undef LOAD_DLL_FUNC
1162 
1163  // Check that all functions where loaded.
1164 bool result =
1165 #define DLL_FUNC_LOADED(name) (DLL_FUNC_VAR(name) != nullptr)&&
1166 
1167  DBGHELP_FUNCTION_LIST(DLL_FUNC_LOADED)
1168  TLHELP32_FUNCTION_LIST(DLL_FUNC_LOADED)
1169 
1170 #undef DLL_FUNC_LOADED
1171  true;
1172 
1173  dbghelp_loaded = result;
1174  return result;
1175  // NOTE: The modules are never unloaded and will stay around until the
1176  // application is closed.
1177 }
1178 
1179 #undef DBGHELP_FUNCTION_LIST
1180 #undef TLHELP32_FUNCTION_LIST
1181 #undef DLL_FUNC_VAR
1182 #undef DLL_FUNC_TYPE
1183 
1184 
1185 // Load the symbols for generating stack traces.
1186 static std::vector<OS::SharedLibraryAddress> LoadSymbols(
1187  HANDLE process_handle) {
1188  static std::vector<OS::SharedLibraryAddress> result;
1189 
1190  static bool symbols_loaded = false;
1191 
1192  if (symbols_loaded) return result;
1193 
1194  BOOL ok;
1195 
1196  // Initialize the symbol engine.
1197  ok = _SymInitialize(process_handle, // hProcess
1198  nullptr, // UserSearchPath
1199  false); // fInvadeProcess
1200  if (!ok) return result;
1201 
1202  DWORD options = _SymGetOptions();
1203  options |= SYMOPT_LOAD_LINES;
1204  options |= SYMOPT_FAIL_CRITICAL_ERRORS;
1205  options = _SymSetOptions(options);
1206 
1207  char buf[OS::kStackWalkMaxNameLen] = {0};
1208  ok = _SymGetSearchPath(process_handle, buf, OS::kStackWalkMaxNameLen);
1209  if (!ok) {
1210  int err = GetLastError();
1211  OS::Print("%d\n", err);
1212  return result;
1213  }
1214 
1215  HANDLE snapshot = _CreateToolhelp32Snapshot(
1216  TH32CS_SNAPMODULE, // dwFlags
1217  GetCurrentProcessId()); // th32ProcessId
1218  if (snapshot == INVALID_HANDLE_VALUE) return result;
1219  MODULEENTRY32W module_entry;
1220  module_entry.dwSize = sizeof(module_entry); // Set the size of the structure.
1221  BOOL cont = _Module32FirstW(snapshot, &module_entry);
1222  while (cont) {
1223  DWORD64 base;
1224  // NOTE the SymLoadModule64 function has the peculiarity of accepting a
1225  // both unicode and ASCII strings even though the parameter is PSTR.
1226  base = _SymLoadModule64(
1227  process_handle, // hProcess
1228  0, // hFile
1229  reinterpret_cast<PSTR>(module_entry.szExePath), // ImageName
1230  reinterpret_cast<PSTR>(module_entry.szModule), // ModuleName
1231  reinterpret_cast<DWORD64>(module_entry.modBaseAddr), // BaseOfDll
1232  module_entry.modBaseSize); // SizeOfDll
1233  if (base == 0) {
1234  int err = GetLastError();
1235  if (err != ERROR_MOD_NOT_FOUND &&
1236  err != ERROR_INVALID_HANDLE) {
1237  result.clear();
1238  return result;
1239  }
1240  }
1241  int lib_name_length = WideCharToMultiByte(
1242  CP_UTF8, 0, module_entry.szExePath, -1, nullptr, 0, nullptr, nullptr);
1243  std::string lib_name(lib_name_length, 0);
1244  WideCharToMultiByte(CP_UTF8, 0, module_entry.szExePath, -1, &lib_name[0],
1245  lib_name_length, nullptr, nullptr);
1246  result.push_back(OS::SharedLibraryAddress(
1247  lib_name, reinterpret_cast<uintptr_t>(module_entry.modBaseAddr),
1248  reinterpret_cast<uintptr_t>(module_entry.modBaseAddr +
1249  module_entry.modBaseSize)));
1250  cont = _Module32NextW(snapshot, &module_entry);
1251  }
1252  CloseHandle(snapshot);
1253 
1254  symbols_loaded = true;
1255  return result;
1256 }
1257 
1258 
1259 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
1260  // SharedLibraryEvents are logged when loading symbol information.
1261  // Only the shared libraries loaded at the time of the call to
1262  // GetSharedLibraryAddresses are logged. DLLs loaded after
1263  // initialization are not accounted for.
1264  if (!LoadDbgHelpAndTlHelp32()) return std::vector<OS::SharedLibraryAddress>();
1265  HANDLE process_handle = GetCurrentProcess();
1266  return LoadSymbols(process_handle);
1267 }
1268 
1269 void OS::SignalCodeMovingGC() {}
1270 
1271 #else // __MINGW32__
1272 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
1273  return std::vector<OS::SharedLibraryAddress>();
1274 }
1275 
1276 void OS::SignalCodeMovingGC() {}
1277 #endif // __MINGW32__
1278 
1279 
1280 int OS::ActivationFrameAlignment() {
1281 #ifdef _WIN64
1282  return 16; // Windows 64-bit ABI requires the stack to be 16-byte aligned.
1283 #elif defined(__MINGW32__)
1284  // With gcc 4.4 the tree vectorization optimizer can generate code
1285  // that requires 16 byte alignment such as movdqa on x86.
1286  return 16;
1287 #else
1288  return 8; // Floating-point math runs faster with 8-byte alignment.
1289 #endif
1290 }
1291 
1292 #if (defined(_WIN32) || defined(_WIN64))
1293 void EnsureConsoleOutputWin32() {
1294  UINT new_flags =
1295  SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
1296  UINT existing_flags = SetErrorMode(new_flags);
1297  SetErrorMode(existing_flags | new_flags);
1298 #if defined(_MSC_VER)
1299  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1300  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
1301  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1302  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1303  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1304  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1305  _set_error_mode(_OUT_TO_STDERR);
1306 #endif // defined(_MSC_VER)
1307 }
1308 #endif // (defined(_WIN32) || defined(_WIN64))
1309 
1310 // ----------------------------------------------------------------------------
1311 // Win32 thread support.
1312 
1313 // Definition of invalid thread handle and id.
1314 static const HANDLE kNoThread = INVALID_HANDLE_VALUE;
1315 
1316 // Entry point for threads. The supplied argument is a pointer to the thread
1317 // object. The entry function dispatches to the run method in the thread
1318 // object. It is important that this function has __stdcall calling
1319 // convention.
1320 static unsigned int __stdcall ThreadEntry(void* arg) {
1321  Thread* thread = reinterpret_cast<Thread*>(arg);
1322  thread->NotifyStartedAndRun();
1323  return 0;
1324 }
1325 
1326 
1327 class Thread::PlatformData {
1328  public:
1329  explicit PlatformData(HANDLE thread) : thread_(thread) {}
1330  HANDLE thread_;
1331  unsigned thread_id_;
1332 };
1333 
1334 
1335 // Initialize a Win32 thread object. The thread has an invalid thread
1336 // handle until it is started.
1337 
1338 Thread::Thread(const Options& options)
1339  : stack_size_(options.stack_size()), start_semaphore_(nullptr) {
1340  data_ = new PlatformData(kNoThread);
1341  set_name(options.name());
1342 }
1343 
1344 
1345 void Thread::set_name(const char* name) {
1346  OS::StrNCpy(name_, sizeof(name_), name, strlen(name));
1347  name_[sizeof(name_) - 1] = '\0';
1348 }
1349 
1350 
1351 // Close our own handle for the thread.
1352 Thread::~Thread() {
1353  if (data_->thread_ != kNoThread) CloseHandle(data_->thread_);
1354  delete data_;
1355 }
1356 
1357 
1358 // Create a new thread. It is important to use _beginthreadex() instead of
1359 // the Win32 function CreateThread(), because the CreateThread() does not
1360 // initialize thread specific structures in the C runtime library.
1361 void Thread::Start() {
1362  data_->thread_ = reinterpret_cast<HANDLE>(
1363  _beginthreadex(nullptr, static_cast<unsigned>(stack_size_), ThreadEntry,
1364  this, 0, &data_->thread_id_));
1365 }
1366 
1367 
1368 // Wait for thread to terminate.
1369 void Thread::Join() {
1370  if (data_->thread_id_ != GetCurrentThreadId()) {
1371  WaitForSingleObject(data_->thread_, INFINITE);
1372  }
1373 }
1374 
1375 
1376 Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
1377  DWORD result = TlsAlloc();
1378  DCHECK(result != TLS_OUT_OF_INDEXES);
1379  return static_cast<LocalStorageKey>(result);
1380 }
1381 
1382 
1383 void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
1384  BOOL result = TlsFree(static_cast<DWORD>(key));
1385  USE(result);
1386  DCHECK(result);
1387 }
1388 
1389 
1390 void* Thread::GetThreadLocal(LocalStorageKey key) {
1391  return TlsGetValue(static_cast<DWORD>(key));
1392 }
1393 
1394 
1395 void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
1396  BOOL result = TlsSetValue(static_cast<DWORD>(key), value);
1397  USE(result);
1398  DCHECK(result);
1399 }
1400 
1401 } // namespace base
1402 } // namespace v8
Definition: libplatform.h:13