More PNaCL fixes (without GYP/Makefile tweaks)
authorsvenpanne@chromium.org <svenpanne@chromium.org>
Fri, 29 Aug 2014 09:39:28 +0000 (09:39 +0000)
committersvenpanne@chromium.org <svenpanne@chromium.org>
Fri, 29 Aug 2014 09:39:28 +0000 (09:39 +0000)
This is basically https://codereview.chromium.org/513923005/ with a
few changes:

   * Makefile.nacl and v8.gyp are untouched.

   * MAP_NORESERVE-handling is more defensive.

   * Added ugly busy-wait emulation of sem_timedwait.

R=bmeurer@chromium.org

Review URL: https://codereview.chromium.org/521473003

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23513 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/base/platform/platform-linux.cc
src/base/platform/platform-posix.cc
src/base/platform/semaphore.cc
src/base/sys-info.cc
src/d8-posix.cc
src/sampler.cc
test/cctest/test-api.cc

index fca1709..41f3315 100644 (file)
@@ -9,9 +9,7 @@
 #include <semaphore.h>
 #include <signal.h>
 #include <stdlib.h>
-#include <sys/prctl.h>
 #include <sys/resource.h>
-#include <sys/syscall.h>
 #include <sys/time.h>
 #include <sys/types.h>
 
 #include "src/base/macros.h"
 #include "src/base/platform/platform.h"
 
+#if V8_OS_NACL
+#if !defined(MAP_NORESERVE)
+// PNaCL doesn't have this, so we always grab all of the memory, which is bad.
+#define MAP_NORESERVE 0
+#endif
+#else
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#endif
 
 namespace v8 {
 namespace base {
@@ -95,20 +102,30 @@ bool OS::ArmUsingHardFloat() {
 
 
 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
+#if V8_OS_NACL
+  // Missing support for tm_zone field.
+  return "";
+#else
   if (std::isnan(time)) return "";
   time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
   struct tm* t = localtime(&tv);
   if (NULL == t) return "";
   return t->tm_zone;
+#endif
 }
 
 
 double OS::LocalTimeOffset(TimezoneCache* cache) {
+#if V8_OS_NACL
+  // Missing support for tm_zone field.
+  return 0;
+#else
   time_t tv = time(NULL);
   struct tm* t = localtime(&tv);
   // tm_gmtoff includes any daylight savings offset, so subtract it.
   return static_cast<double>(t->tm_gmtoff * msPerSecond -
                              (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
+#endif
 }
 
 
@@ -260,18 +277,15 @@ void OS::SignalCodeMovingGC() {
     OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
     OS::Abort();
   }
-  void* addr = mmap(OS::GetRandomMmapAddr(),
-                    size,
-#if defined(__native_client__)
+  void* addr = mmap(OS::GetRandomMmapAddr(), size,
+#if V8_OS_NACL
                     // The Native Client port of V8 uses an interpreter,
                     // so code pages don't need PROT_EXEC.
                     PROT_READ,
 #else
                     PROT_READ | PROT_EXEC,
 #endif
-                    MAP_PRIVATE,
-                    fileno(f),
-                    0);
+                    MAP_PRIVATE, fileno(f), 0);
   DCHECK(addr != MAP_FAILED);
   OS::Free(addr, size);
   fclose(f);
@@ -387,7 +401,7 @@ void* VirtualMemory::ReserveRegion(size_t size) {
 
 
 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
-#if defined(__native_client__)
+#if V8_OS_NACL
   // The Native Client port of V8 uses an interpreter,
   // so code pages don't need PROT_EXEC.
   int prot = PROT_READ | PROT_WRITE;
index ea06ff5..0fc04fc 100644 (file)
 #include <sys/mman.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
-#if !defined(__pnacl__)
-#include <sys/syscall.h>
-#endif
 #include <sys/time.h>
 #include <sys/types.h>
-#if defined(__linux__) && !defined(__pnacl__)
-#include <sys/prctl.h>  // NOLINT, for prctl
-#endif
 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
     defined(__NetBSD__) || defined(__OpenBSD__)
 #include <sys/sysctl.h>  // NOLINT, for sysctl
 #include <dlfcn.h>
 #endif
 
+#if V8_OS_LINUX
+#include <sys/prctl.h>  // NOLINT, for prctl
+#endif
+
+#if !V8_OS_NACL
+#include <sys/syscall.h>
+#endif
+
 namespace v8 {
 namespace base {
 
@@ -223,11 +225,11 @@ void OS::DebugBreak() {
 #elif V8_HOST_ARCH_MIPS64
   asm("break");
 #elif V8_HOST_ARCH_IA32
-#if defined(__native_client__)
+#if V8_OS_NACL
   asm("hlt");
 #else
   asm("int $3");
-#endif  // __native_client__
+#endif  // V8_OS_NACL
 #elif V8_HOST_ARCH_X64
   asm("int $3");
 #else
@@ -268,12 +270,17 @@ int OS::GetCurrentThreadId() {
 //
 
 int OS::GetUserTime(uint32_t* secs,  uint32_t* usecs) {
+#if V8_OS_NACL
+  // Optionally used in Logger::ResourceEvent.
+  return -1;
+#else
   struct rusage usage;
 
   if (getrusage(RUSAGE_SELF, &usage) < 0) return -1;
   *secs = usage.ru_utime.tv_sec;
   *usecs = usage.ru_utime.tv_usec;
   return 0;
+#endif
 }
 
 
index e11338f..669240e 100644 (file)
@@ -12,6 +12,7 @@
 #include <errno.h>
 
 #include "src/base/logging.h"
+#include "src/base/platform/elapsed-timer.h"
 #include "src/base/platform/time.h"
 
 namespace v8 {
@@ -106,6 +107,17 @@ void Semaphore::Wait() {
 
 
 bool Semaphore::WaitFor(const TimeDelta& rel_time) {
+#if V8_OS_NACL
+  // PNaCL doesn't support sem_timedwait, do ugly busy waiting.
+  ElapsedTimer timer;
+  timer.Start();
+  do {
+    int result = sem_trywait(&native_handle_);
+    if (result == 0) return true;
+    DCHECK(errno == EAGAIN || error == EINTR);
+  } while (!timer.HasExpired(rel_time));
+  return false;
+#else
   // Compute the time for end of timeout.
   const Time time = Time::NowFromSystemTime() + rel_time;
   const struct timespec ts = time.ToTimespec();
@@ -129,6 +141,7 @@ bool Semaphore::WaitFor(const TimeDelta& rel_time) {
     DCHECK_EQ(-1, result);
     DCHECK_EQ(EINTR, errno);
   }
+#endif
 }
 
 #elif V8_OS_WIN
index 203b777..06c4f24 100644 (file)
@@ -91,6 +91,9 @@ int64_t SysInfo::AmountOfPhysicalMemory() {
     return 0;
   }
   return static_cast<int64_t>(stat_buf.st_size);
+#elif V8_OS_NACL
+  // No support for _SC_PHYS_PAGES, assume 2GB.
+  return static_cast<int64_t>(1) << 31;
 #elif V8_OS_POSIX
   long pages = sysconf(_SC_PHYS_PAGES);    // NOLINT(runtime/int)
   long page_size = sysconf(_SC_PAGESIZE);  // NOLINT(runtime/int)
index 59c50b4..9a20b06 100644 (file)
@@ -7,7 +7,6 @@
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/select.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
@@ -16,6 +15,9 @@
 
 #include "src/d8.h"
 
+#if !V8_OS_NACL
+#include <sys/select.h>
+#endif
 
 namespace v8 {
 
@@ -102,11 +104,16 @@ static bool WaitOnFD(int fd,
   }
   timeout.tv_usec = (read_timeout % 1000) * 1000;
   timeout.tv_sec = read_timeout / 1000;
+#if V8_OS_NACL
+  // PNaCL has no support for select.
+  int number_of_fds_ready = -1;
+#else
   int number_of_fds_ready = select(fd + 1,
                                    &readfds,
                                    &writefds,
                                    &exceptfds,
                                    read_timeout != -1 ? &timeout : NULL);
+#endif
   return number_of_fds_ready == 1;
 }
 
@@ -547,8 +554,12 @@ void Shell::SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args) {
     return;
   }
   if (args[0]->IsNumber()) {
-    mode_t mask = args[0]->Int32Value();
-    int previous = umask(mask);
+#if V8_OS_NACL
+    // PNaCL has no support for umask.
+    int previous = 0;
+#else
+    int previous = umask(args[0]->Int32Value());
+#endif
     args.GetReturnValue().Set(previous);
     return;
   } else {
index 29fe27f..394efeb 100644 (file)
@@ -13,7 +13,7 @@
 #include <signal.h>
 #include <sys/time.h>
 
-#if !V8_OS_QNX
+#if !V8_OS_QNX && !V8_OS_NACL
 #include <sys/syscall.h>  // NOLINT
 #endif
 
@@ -21,8 +21,8 @@
 #include <mach/mach.h>
 // OpenBSD doesn't have <ucontext.h>. ucontext_t lives in <signal.h>
 // and is a typedef for struct sigcontext. There is no uc_mcontext.
-#elif(!V8_OS_ANDROID || defined(__BIONIC_HAVE_UCONTEXT_T)) \
-    && !V8_OS_OPENBSD
+#elif(!V8_OS_ANDROID || defined(__BIONIC_HAVE_UCONTEXT_T)) && \
+    !V8_OS_OPENBSD && !V8_OS_NACL
 #include <ucontext.h>
 #endif
 
@@ -294,6 +294,7 @@ class SignalHandler : public AllStatic {
 
  private:
   static void Install() {
+#if !V8_OS_NACL
     struct sigaction sa;
     sa.sa_sigaction = &HandleProfilerSignal;
     sigemptyset(&sa.sa_mask);
@@ -304,16 +305,21 @@ class SignalHandler : public AllStatic {
 #endif
     signal_handler_installed_ =
         (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
+#endif
   }
 
   static void Restore() {
+#if !V8_OS_NACL
     if (signal_handler_installed_) {
       sigaction(SIGPROF, &old_signal_handler_, 0);
       signal_handler_installed_ = false;
     }
+#endif
   }
 
+#if !V8_OS_NACL
   static void HandleProfilerSignal(int signal, siginfo_t* info, void* context);
+#endif
   // Protects the process wide state below.
   static base::Mutex* mutex_;
   static int client_count_;
@@ -328,13 +334,10 @@ struct sigaction SignalHandler::old_signal_handler_;
 bool SignalHandler::signal_handler_installed_ = false;
 
 
+// As Native Client does not support signal handling, profiling is disabled.
+#if !V8_OS_NACL
 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
                                          void* context) {
-#if V8_OS_NACL
-  // As Native Client does not support signal handling, profiling
-  // is disabled.
-  return;
-#else
   USE(info);
   if (signal != SIGPROF) return;
   Isolate* isolate = Isolate::UnsafeCurrent();
@@ -477,8 +480,8 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
 #endif  // V8_OS_QNX
 #endif  // USE_SIMULATOR
   sampler->SampleStack(state);
-#endif  // V8_OS_NACL
 }
+#endif  // V8_OS_NACL
 
 #endif
 
index f74c1a2..db0831c 100644 (file)
@@ -21621,7 +21621,7 @@ THREADED_TEST(JSONParseNumber) {
 }
 
 
-#if V8_OS_POSIX
+#if V8_OS_POSIX && !V8_OS_NACL
 class ThreadInterruptTest {
  public:
   ThreadInterruptTest() : sem_(0), sem_value_(0) { }