Home | History | Annotate | Download | only in src
      1 // Copyright 2014 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 #ifndef V8_EXECUTION_H_
      6 #define V8_EXECUTION_H_
      7 
      8 #include "src/allocation.h"
      9 #include "src/base/atomicops.h"
     10 #include "src/handles.h"
     11 #include "src/utils.h"
     12 
     13 namespace v8 {
     14 namespace internal {
     15 
     16 class Execution final : public AllStatic {
     17  public:
     18   // Call a function, the caller supplies a receiver and an array
     19   // of arguments.
     20   //
     21   // When the function called is not in strict mode, receiver is
     22   // converted to an object.
     23   //
     24   V8_EXPORT_PRIVATE MUST_USE_RESULT static MaybeHandle<Object> Call(
     25       Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
     26       int argc, Handle<Object> argv[]);
     27 
     28   // Construct object from function, the caller supplies an array of
     29   // arguments.
     30   MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> constructor,
     31                                                  int argc,
     32                                                  Handle<Object> argv[]);
     33   MUST_USE_RESULT static MaybeHandle<Object> New(Isolate* isolate,
     34                                                  Handle<Object> constructor,
     35                                                  Handle<Object> new_target,
     36                                                  int argc,
     37                                                  Handle<Object> argv[]);
     38 
     39   // Call a function, just like Call(), but make sure to silently catch
     40   // any thrown exceptions. The return value is either the result of
     41   // calling the function (if caught exception is false) or the exception
     42   // that occurred (if caught exception is true).
     43   // In the exception case, exception_out holds the caught exceptions, unless
     44   // it is a termination exception.
     45   static MaybeHandle<Object> TryCall(Isolate* isolate, Handle<Object> callable,
     46                                      Handle<Object> receiver, int argc,
     47                                      Handle<Object> argv[],
     48                                      MaybeHandle<Object>* exception_out = NULL);
     49 };
     50 
     51 
     52 class ExecutionAccess;
     53 class PostponeInterruptsScope;
     54 
     55 
     56 // StackGuard contains the handling of the limits that are used to limit the
     57 // number of nested invocations of JavaScript and the stack size used in each
     58 // invocation.
     59 class StackGuard final {
     60  public:
     61   // Pass the address beyond which the stack should not grow.  The stack
     62   // is assumed to grow downwards.
     63   void SetStackLimit(uintptr_t limit);
     64 
     65   // The simulator uses a separate JS stack. Limits on the JS stack might have
     66   // to be adjusted in order to reflect overflows of the C stack, because we
     67   // cannot rely on the interleaving of frames on the simulator.
     68   void AdjustStackLimitForSimulator();
     69 
     70   // Threading support.
     71   char* ArchiveStackGuard(char* to);
     72   char* RestoreStackGuard(char* from);
     73   static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
     74   void FreeThreadResources();
     75   // Sets up the default stack guard for this thread if it has not
     76   // already been set up.
     77   void InitThread(const ExecutionAccess& lock);
     78   // Clears the stack guard for this thread so it does not look as if
     79   // it has been set up.
     80   void ClearThread(const ExecutionAccess& lock);
     81 
     82 #define INTERRUPT_LIST(V)                                          \
     83   V(DEBUGBREAK, DebugBreak, 0)                                     \
     84   V(DEBUGCOMMAND, DebugCommand, 1)                                 \
     85   V(TERMINATE_EXECUTION, TerminateExecution, 2)                    \
     86   V(GC_REQUEST, GC, 3)                                             \
     87   V(INSTALL_CODE, InstallCode, 4)                                  \
     88   V(API_INTERRUPT, ApiInterrupt, 5)                                \
     89   V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)
     90 
     91 #define V(NAME, Name, id)                                          \
     92   inline bool Check##Name() { return CheckInterrupt(NAME); }  \
     93   inline void Request##Name() { RequestInterrupt(NAME); }     \
     94   inline void Clear##Name() { ClearInterrupt(NAME); }
     95   INTERRUPT_LIST(V)
     96 #undef V
     97 
     98   // Flag used to set the interrupt causes.
     99   enum InterruptFlag {
    100   #define V(NAME, Name, id) NAME = (1 << id),
    101     INTERRUPT_LIST(V)
    102   #undef V
    103   #define V(NAME, Name, id) NAME |
    104     ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
    105   #undef V
    106   };
    107 
    108   uintptr_t climit() { return thread_local_.climit(); }
    109   uintptr_t jslimit() { return thread_local_.jslimit(); }
    110   // This provides an asynchronous read of the stack limits for the current
    111   // thread.  There are no locks protecting this, but it is assumed that you
    112   // have the global V8 lock if you are using multiple V8 threads.
    113   uintptr_t real_climit() {
    114     return thread_local_.real_climit_;
    115   }
    116   uintptr_t real_jslimit() {
    117     return thread_local_.real_jslimit_;
    118   }
    119   Address address_of_jslimit() {
    120     return reinterpret_cast<Address>(&thread_local_.jslimit_);
    121   }
    122   Address address_of_real_jslimit() {
    123     return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
    124   }
    125 
    126   // If the stack guard is triggered, but it is not an actual
    127   // stack overflow, then handle the interruption accordingly.
    128   Object* HandleInterrupts();
    129   void HandleGCInterrupt();
    130 
    131  private:
    132   StackGuard();
    133 
    134   bool CheckInterrupt(InterruptFlag flag);
    135   void RequestInterrupt(InterruptFlag flag);
    136   void ClearInterrupt(InterruptFlag flag);
    137   bool CheckAndClearInterrupt(InterruptFlag flag);
    138 
    139   // You should hold the ExecutionAccess lock when calling this method.
    140   bool has_pending_interrupts(const ExecutionAccess& lock) {
    141     return thread_local_.interrupt_flags_ != 0;
    142   }
    143 
    144   // You should hold the ExecutionAccess lock when calling this method.
    145   inline void set_interrupt_limits(const ExecutionAccess& lock);
    146 
    147   // Reset limits to actual values. For example after handling interrupt.
    148   // You should hold the ExecutionAccess lock when calling this method.
    149   inline void reset_limits(const ExecutionAccess& lock);
    150 
    151   // Enable or disable interrupts.
    152   void EnableInterrupts();
    153   void DisableInterrupts();
    154 
    155 #if V8_TARGET_ARCH_64_BIT
    156   static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
    157   static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
    158 #else
    159   static const uintptr_t kInterruptLimit = 0xfffffffe;
    160   static const uintptr_t kIllegalLimit = 0xfffffff8;
    161 #endif
    162 
    163   void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
    164   void PopPostponeInterruptsScope();
    165 
    166   class ThreadLocal final {
    167    public:
    168     ThreadLocal() { Clear(); }
    169     // You should hold the ExecutionAccess lock when you call Initialize or
    170     // Clear.
    171     void Clear();
    172 
    173     // Returns true if the heap's stack limits should be set, false if not.
    174     bool Initialize(Isolate* isolate);
    175 
    176     // The stack limit is split into a JavaScript and a C++ stack limit. These
    177     // two are the same except when running on a simulator where the C++ and
    178     // JavaScript stacks are separate. Each of the two stack limits have two
    179     // values. The one eith the real_ prefix is the actual stack limit
    180     // set for the VM. The one without the real_ prefix has the same value as
    181     // the actual stack limit except when there is an interruption (e.g. debug
    182     // break or preemption) in which case it is lowered to make stack checks
    183     // fail. Both the generated code and the runtime system check against the
    184     // one without the real_ prefix.
    185     uintptr_t real_jslimit_;  // Actual JavaScript stack limit set for the VM.
    186     uintptr_t real_climit_;  // Actual C++ stack limit set for the VM.
    187 
    188     // jslimit_ and climit_ can be read without any lock.
    189     // Writing requires the ExecutionAccess lock.
    190     base::AtomicWord jslimit_;
    191     base::AtomicWord climit_;
    192 
    193     uintptr_t jslimit() {
    194       return bit_cast<uintptr_t>(base::NoBarrier_Load(&jslimit_));
    195     }
    196     void set_jslimit(uintptr_t limit) {
    197       return base::NoBarrier_Store(&jslimit_,
    198                                    static_cast<base::AtomicWord>(limit));
    199     }
    200     uintptr_t climit() {
    201       return bit_cast<uintptr_t>(base::NoBarrier_Load(&climit_));
    202     }
    203     void set_climit(uintptr_t limit) {
    204       return base::NoBarrier_Store(&climit_,
    205                                    static_cast<base::AtomicWord>(limit));
    206     }
    207 
    208     PostponeInterruptsScope* postpone_interrupts_;
    209     int interrupt_flags_;
    210   };
    211 
    212   // TODO(isolates): Technically this could be calculated directly from a
    213   //                 pointer to StackGuard.
    214   Isolate* isolate_;
    215   ThreadLocal thread_local_;
    216 
    217   friend class Isolate;
    218   friend class StackLimitCheck;
    219   friend class PostponeInterruptsScope;
    220 
    221   DISALLOW_COPY_AND_ASSIGN(StackGuard);
    222 };
    223 
    224 }  // namespace internal
    225 }  // namespace v8
    226 
    227 #endif  // V8_EXECUTION_H_
    228