diff --git a/src/base/macros.h b/src/base/macros.h index 736a656ed9..381107b860 100644 --- a/src/base/macros.h +++ b/src/base/macros.h @@ -6,6 +6,7 @@ #define V8_BASE_MACROS_H_ #include "include/v8stdint.h" +#include "src/base/build_config.h" // The expression OFFSET_OF(type, field) computes the byte-offset @@ -112,6 +113,52 @@ inline void USE(T) { } #define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0)) + +// Define our own macros for writing 64-bit constants. This is less fragile +// than defining __STDC_CONSTANT_MACROS before including , and it +// works on compilers that don't have it (like MSVC). +#if V8_CC_MSVC +# define V8_UINT64_C(x) (x ## UI64) +# define V8_INT64_C(x) (x ## I64) +# if V8_HOST_ARCH_64_BIT +# define V8_INTPTR_C(x) (x ## I64) +# define V8_PTR_PREFIX "ll" +# else +# define V8_INTPTR_C(x) (x) +# define V8_PTR_PREFIX "" +# endif // V8_HOST_ARCH_64_BIT +#elif V8_CC_MINGW64 +# define V8_UINT64_C(x) (x ## ULL) +# define V8_INT64_C(x) (x ## LL) +# define V8_INTPTR_C(x) (x ## LL) +# define V8_PTR_PREFIX "I64" +#elif V8_HOST_ARCH_64_BIT +# if V8_OS_MACOSX +# define V8_UINT64_C(x) (x ## ULL) +# define V8_INT64_C(x) (x ## LL) +# else +# define V8_UINT64_C(x) (x ## UL) +# define V8_INT64_C(x) (x ## L) +# endif +# define V8_INTPTR_C(x) (x ## L) +# define V8_PTR_PREFIX "l" +#else +# define V8_UINT64_C(x) (x ## ULL) +# define V8_INT64_C(x) (x ## LL) +# define V8_INTPTR_C(x) (x) +# define V8_PTR_PREFIX "" +#endif + +#define V8PRIxPTR V8_PTR_PREFIX "x" +#define V8PRIdPTR V8_PTR_PREFIX "d" +#define V8PRIuPTR V8_PTR_PREFIX "u" + +// Fix for Mac OS X defining uintptr_t as "unsigned long": +#if V8_OS_MACOSX +#undef V8PRIxPTR +#define V8PRIxPTR "lx" +#endif + // The following macro works on both 32 and 64-bit platforms. // Usage: instead of writing 0x1234567890123456 // write V8_2PART_UINT64_C(0x12345678,90123456); diff --git a/src/globals.h b/src/globals.h index c76dc85309..970a0f4239 100644 --- a/src/globals.h +++ b/src/globals.h @@ -66,51 +66,6 @@ typedef unsigned int __my_bool__; typedef uint8_t byte; typedef byte* Address; -// Define our own macros for writing 64-bit constants. This is less fragile -// than defining __STDC_CONSTANT_MACROS before including , and it -// works on compilers that don't have it (like MSVC). -#if V8_CC_MSVC -# define V8_UINT64_C(x) (x ## UI64) -# define V8_INT64_C(x) (x ## I64) -# if V8_HOST_ARCH_64_BIT -# define V8_INTPTR_C(x) (x ## I64) -# define V8_PTR_PREFIX "ll" -# else -# define V8_INTPTR_C(x) (x) -# define V8_PTR_PREFIX "" -# endif // V8_HOST_ARCH_64_BIT -#elif V8_CC_MINGW64 -# define V8_UINT64_C(x) (x ## ULL) -# define V8_INT64_C(x) (x ## LL) -# define V8_INTPTR_C(x) (x ## LL) -# define V8_PTR_PREFIX "I64" -#elif V8_HOST_ARCH_64_BIT -# if V8_OS_MACOSX -# define V8_UINT64_C(x) (x ## ULL) -# define V8_INT64_C(x) (x ## LL) -# else -# define V8_UINT64_C(x) (x ## UL) -# define V8_INT64_C(x) (x ## L) -# endif -# define V8_INTPTR_C(x) (x ## L) -# define V8_PTR_PREFIX "l" -#else -# define V8_UINT64_C(x) (x ## ULL) -# define V8_INT64_C(x) (x ## LL) -# define V8_INTPTR_C(x) (x) -# define V8_PTR_PREFIX "" -#endif - -#define V8PRIxPTR V8_PTR_PREFIX "x" -#define V8PRIdPTR V8_PTR_PREFIX "d" -#define V8PRIuPTR V8_PTR_PREFIX "u" - -// Fix for Mac OS X defining uintptr_t as "unsigned long": -#if V8_OS_MACOSX -#undef V8PRIxPTR -#define V8PRIxPTR "lx" -#endif - // ----------------------------------------------------------------------------- // Constants diff --git a/src/platform-cygwin.cc b/src/platform-cygwin.cc index ea72fd11c3..ac313b2a03 100644 --- a/src/platform-cygwin.cc +++ b/src/platform-cygwin.cc @@ -14,12 +14,13 @@ #include #include // sysconf -#undef MAP_TYPE +#include -#include "src/v8.h" +#undef MAP_TYPE #include "src/base/win32-headers.h" #include "src/platform.h" +#include "src/utils.h" namespace v8 { namespace internal { @@ -237,7 +238,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) static_cast(OS::AllocateAlignment())); void* address = ReserveRegion(request_size); if (address == NULL) return; - Address base = RoundUp(static_cast
(address), alignment); + uint8_t* base = RoundUp(static_cast(address), alignment); // Try reducing the size by freeing and then reallocating a specific area. bool result = ReleaseRegion(address, request_size); USE(result); @@ -245,7 +246,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS); if (address != NULL) { request_size = size; - ASSERT(base == static_cast
(address)); + ASSERT(base == static_cast(address)); } else { // Resizing failed, just go with a bigger area. address = ReserveRegion(request_size); diff --git a/src/platform-freebsd.cc b/src/platform-freebsd.cc index d77b30f245..9e668a8ba6 100644 --- a/src/platform-freebsd.cc +++ b/src/platform-freebsd.cc @@ -25,11 +25,12 @@ #include #include // index +#include + #undef MAP_TYPE -#include "src/v8.h" - #include "src/platform.h" +#include "src/utils.h" namespace v8 { @@ -192,8 +193,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) kMmapFdOffset); if (reservation == MAP_FAILED) return; - Address base = static_cast
(reservation); - Address aligned_base = RoundUp(base, alignment); + uint8_t* base = static_cast(reservation); + uint8_t* aligned_base = RoundUp(base, alignment); ASSERT_LE(base, aligned_base); // Unmap extra memory reserved before and after the desired block. diff --git a/src/platform-linux.cc b/src/platform-linux.cc index f92f2f9a9f..9d7c8cd141 100644 --- a/src/platform-linux.cc +++ b/src/platform-linux.cc @@ -39,11 +39,12 @@ #include #endif +#include + #undef MAP_TYPE -#include "src/v8.h" - #include "src/platform.h" +#include "src/utils.h" namespace v8 { @@ -254,9 +255,9 @@ void OS::SignalCodeMovingGC() { // by the kernel and allows us to synchronize V8 code log and the // kernel log. int size = sysconf(_SC_PAGESIZE); - FILE* f = fopen(FLAG_gc_fake_mmap, "w+"); + FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+"); if (f == NULL) { - OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap); + OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile()); OS::Abort(); } void* addr = mmap(OS::GetRandomMmapAddr(), @@ -302,8 +303,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) kMmapFdOffset); if (reservation == MAP_FAILED) return; - Address base = static_cast
(reservation); - Address aligned_base = RoundUp(base, alignment); + uint8_t* base = static_cast(reservation); + uint8_t* aligned_base = RoundUp(base, alignment); ASSERT_LE(base, aligned_base); // Unmap extra memory reserved before and after the desired block. diff --git a/src/platform-macos.cc b/src/platform-macos.cc index dfff0d17ef..7b6609b63f 100644 --- a/src/platform-macos.cc +++ b/src/platform-macos.cc @@ -31,11 +31,12 @@ #include #include +#include + #undef MAP_TYPE -#include "src/v8.h" - #include "src/platform.h" +#include "src/utils.h" namespace v8 { @@ -194,8 +195,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) kMmapFdOffset); if (reservation == MAP_FAILED) return; - Address base = static_cast
(reservation); - Address aligned_base = RoundUp(base, alignment); + uint8_t* base = static_cast(reservation); + uint8_t* aligned_base = RoundUp(base, alignment); ASSERT_LE(base, aligned_base); // Unmap extra memory reserved before and after the desired block. diff --git a/src/platform-openbsd.cc b/src/platform-openbsd.cc index b09d588a3f..c29ebde033 100644 --- a/src/platform-openbsd.cc +++ b/src/platform-openbsd.cc @@ -23,11 +23,12 @@ #include // mmap & munmap #include // sysconf +#include + #undef MAP_TYPE -#include "src/v8.h" - #include "src/platform.h" +#include "src/utils.h" namespace v8 { @@ -184,9 +185,9 @@ void OS::SignalCodeMovingGC() { // by the kernel and allows us to synchronize V8 code log and the // kernel log. int size = sysconf(_SC_PAGESIZE); - FILE* f = fopen(FLAG_gc_fake_mmap, "w+"); + FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+"); if (f == NULL) { - OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap); + OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile()); OS::Abort(); } void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, @@ -223,8 +224,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) kMmapFdOffset); if (reservation == MAP_FAILED) return; - Address base = static_cast
(reservation); - Address aligned_base = RoundUp(base, alignment); + uint8_t* base = static_cast(reservation); + uint8_t* aligned_base = RoundUp(base, alignment); ASSERT_LE(base, aligned_base); // Unmap extra memory reserved before and after the desired block. diff --git a/src/platform-posix.cc b/src/platform-posix.cc index ae47bd4d3f..928edb86a5 100644 --- a/src/platform-posix.cc +++ b/src/platform-posix.cc @@ -8,6 +8,7 @@ #include #include +#include #include #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) #include // for pthread_set_name_np @@ -41,10 +42,13 @@ #include // NOLINT #endif -#include "src/v8.h" +#include +#include #include "src/base/lazy-instance.h" +#include "src/base/macros.h" #include "src/platform.h" +#include "src/platform/time.h" #include "src/utils/random-number-generator.h" #ifdef V8_FAST_TLS_SUPPORTED @@ -54,8 +58,16 @@ namespace v8 { namespace internal { +namespace { + // 0 is never a valid thread id. -static const pthread_t kNoThread = (pthread_t) 0; +const pthread_t kNoThread = (pthread_t) 0; + +bool g_hard_abort = false; + +const char* g_gc_fake_mmap = NULL; + +} // namespace int OS::NumberOfProcessorsOnline() { @@ -191,8 +203,18 @@ static base::LazyInstance::type platform_random_number_generator = LAZY_INSTANCE_INITIALIZER; -void OS::SetRandomSeed(int64_t seed) { - platform_random_number_generator.Pointer()->SetSeed(seed); +void OS::Initialize(int64_t random_seed, bool hard_abort, + const char* const gc_fake_mmap) { + if (random_seed) { + platform_random_number_generator.Pointer()->SetSeed(random_seed); + } + g_hard_abort = hard_abort; + g_gc_fake_mmap = gc_fake_mmap; +} + + +const char* OS::GetGCFakeMMapFile() { + return g_gc_fake_mmap; } @@ -253,7 +275,7 @@ void OS::Sleep(int milliseconds) { void OS::Abort() { - if (FLAG_hard_abort) { + if (g_hard_abort) { V8_IMMEDIATE_CRASH(); } // Redirect to std abort to signal abnormal program termination. diff --git a/src/platform-qnx.cc b/src/platform-qnx.cc index 6d72558da1..bdf07d5280 100644 --- a/src/platform-qnx.cc +++ b/src/platform-qnx.cc @@ -27,11 +27,12 @@ #include // mmap & munmap #include // sysconf +#include + #undef MAP_TYPE -#include "src/v8.h" - #include "src/platform.h" +#include "src/utils.h" namespace v8 { @@ -259,8 +260,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) kMmapFdOffset); if (reservation == MAP_FAILED) return; - Address base = static_cast
(reservation); - Address aligned_base = RoundUp(base, alignment); + uint8_t* base = static_cast(reservation); + uint8_t* aligned_base = RoundUp(base, alignment); ASSERT_LE(base, aligned_base); // Unmap extra memory reserved before and after the desired block. diff --git a/src/platform-solaris.cc b/src/platform-solaris.cc index 931bc1166a..4803531b73 100644 --- a/src/platform-solaris.cc +++ b/src/platform-solaris.cc @@ -23,12 +23,12 @@ #include // walkstack(), getcontext() #include // getpagesize(), usleep() +#include #undef MAP_TYPE -#include "src/v8.h" - #include "src/platform.h" +#include "src/utils.h" // It seems there is a bug in some Solaris distributions (experienced in @@ -165,8 +165,8 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) kMmapFdOffset); if (reservation == MAP_FAILED) return; - Address base = static_cast
(reservation); - Address aligned_base = RoundUp(base, alignment); + uint8_t* base = static_cast(reservation); + uint8_t* aligned_base = RoundUp(base, alignment); ASSERT_LE(base, aligned_base); // Unmap extra memory reserved before and after the desired block. diff --git a/src/platform-win32.cc b/src/platform-win32.cc index 9fe3e14112..1897b75be6 100644 --- a/src/platform-win32.cc +++ b/src/platform-win32.cc @@ -17,10 +17,10 @@ #include "src/base/win32-headers.h" -#include "src/v8.h" - #include "src/base/lazy-instance.h" #include "src/platform.h" +#include "src/platform/time.h" +#include "src/utils.h" #include "src/utils/random-number-generator.h" #ifdef _MSC_VER @@ -103,6 +103,12 @@ int strncpy_s(char* dest, size_t dest_size, const char* source, size_t count) { namespace v8 { namespace internal { +namespace { + +bool g_hard_abort = false; + +} // namespace + intptr_t OS::MaxVirtualMemory() { return 0; } @@ -713,8 +719,12 @@ static base::LazyInstance::type platform_random_number_generator = LAZY_INSTANCE_INITIALIZER; -void OS::SetRandomSeed(int64_t seed) { - platform_random_number_generator.Pointer()->SetSeed(seed); +void OS::Initialize(int64_t random_seed, bool hard_abort, + const char* const gc_fake_mmap) { + if (random_seed) { + platform_random_number_generator.Pointer()->SetSeed(random_seed); + } + g_hard_abort = hard_abort; } @@ -808,7 +818,7 @@ void OS::Sleep(int milliseconds) { void OS::Abort() { - if (FLAG_hard_abort) { + if (g_hard_abort) { V8_IMMEDIATE_CRASH(); } // Make the MSVCRT do a silent abort. @@ -1217,7 +1227,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) static_cast(OS::AllocateAlignment())); void* address = ReserveRegion(request_size); if (address == NULL) return; - Address base = RoundUp(static_cast
(address), alignment); + uint8_t* base = RoundUp(static_cast(address), alignment); // Try reducing the size by freeing and then reallocating a specific area. bool result = ReleaseRegion(address, request_size); USE(result); @@ -1225,7 +1235,7 @@ VirtualMemory::VirtualMemory(size_t size, size_t alignment) address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS); if (address != NULL) { request_size = size; - ASSERT(base == static_cast
(address)); + ASSERT(base == static_cast(address)); } else { // Resizing failed, just go with a bigger area. address = ReserveRegion(request_size); diff --git a/src/platform.h b/src/platform.h index a8500f0ddd..b7aca9da8a 100644 --- a/src/platform.h +++ b/src/platform.h @@ -141,6 +141,14 @@ class TimezoneCache; class OS { public: + // Initialize the OS class. + // - random_seed: Used for the GetRandomMmapAddress() if non-zero. + // - hard_abort: If true, OS::Abort() will crash instead of aborting. + // - gc_fake_mmap: Name of the file for fake gc mmap used in ll_prof. + static void Initialize(int64_t random_seed, + bool hard_abort, + const char* const gc_fake_mmap); + // Returns the accumulated user time for thread. This routine // can be used for profiling. The implementation should // strive for high-precision timer resolution, preferable @@ -212,10 +220,6 @@ class OS { // Assign memory as a guard page so that access will cause an exception. static void Guard(void* address, const size_t size); - // Set a fixed random seed for the random number generator used for - // GetRandomMmapAddr. - static void SetRandomSeed(int64_t seed); - // Generate a random address to be used for hinting mmap(). static void* GetRandomMmapAddr(); @@ -306,6 +310,10 @@ class OS { private: static const int msPerSecond = 1000; +#if V8_OS_POSIX + static const char* GetGCFakeMMapFile(); +#endif + DISALLOW_IMPLICIT_CONSTRUCTORS(OS); }; diff --git a/src/v8.cc b/src/v8.cc index 47392059cf..f8362703b6 100644 --- a/src/v8.cc +++ b/src/v8.cc @@ -90,7 +90,7 @@ void V8::InitializeOncePerProcessImpl() { FLAG_max_semi_space_size = 1; } - if (FLAG_random_seed != 0) OS::SetRandomSeed(FLAG_random_seed); + OS::Initialize(FLAG_random_seed, FLAG_hard_abort, FLAG_gc_fake_mmap); #ifdef V8_USE_DEFAULT_PLATFORM platform_ = new DefaultPlatform;