Changeset 92694 in webkit


Ignore:
Timestamp:
Aug 9, 2011 12:01:05 PM (13 years ago)
Author:
dslomov@google.com
Message:

Source/WebCore: https://bugs.webkit.org/show_bug.cgi?id=65778
[WebWorkers][chromium] Make statics thread-safe and make sure V8 API accesses correct isolates.

Reviewed by Dmitry Titov.

Covered by existing tests.

  • bindings/v8/V8Binding.cpp:

(WebCore::V8BindingPerIsolateData::V8BindingPerIsolateData):

  • bindings/v8/V8Binding.h:

(WebCore::V8BindingPerIsolateData::lazyEventListenerToStringTemplate):
(WebCore::V8BindingPerIsolateData::hiddenPropertyName):
(WebCore::V8BindingPerIsolateData::globalHandleMap):
(WebCore::AllowAllocation::AllowAllocation):Moving to V8Binding.h from V8Utilities.h to resolve header dependency.
(WebCore::AllowAllocation::~AllowAllocation):
(WebCore::AllowAllocation::current):
(WebCore::SafeAllocation::newInstance):

  • bindings/v8/V8GCController.cpp:

(WebCore::currentGlobalHandleMap):
(WebCore::enumerateGlobalHandles):
(WebCore::V8GCController::registerGlobalHandle):
(WebCore::V8GCController::unregisterGlobalHandle):

  • bindings/v8/V8HiddenPropertyName.cpp:

(WebCore::V8HiddenPropertyName::createString):

  • bindings/v8/V8HiddenPropertyName.h:

(WebCore::V8HiddenPropertyName::V8HiddenPropertyName):

  • bindings/v8/V8LazyEventListener.cpp:

(WebCore::V8LazyEventListener::prepareListenerObject):

  • bindings/v8/V8NPObject.cpp:
  • bindings/v8/V8Proxy.cpp:

(WebCore::V8Proxy::checkNewLegal):

  • bindings/v8/V8Utilities.h:
  • bindings/v8/WorkerContextExecutionProxy.cpp:

(WebCore::WorkerContextExecutionProxy::WorkerContextExecutionProxy):
(WebCore::WorkerContextExecutionProxy::initIsolate):

  • bindings/v8/WorkerContextExecutionProxy.h:
  • bindings/v8/WorkerScriptController.cpp:

(WebCore::WorkerScriptController::scheduleExecutionTermination):

Source/WebKit/chromium: https://bugs.webkit.org/show_bug.cgi?id=65778
[WebWorkers][chromium] Make statics thread-safe and make sure V8 API accesses correct isolates

Reviewed by Dmitry Titov.

  • src/BoundObject.cpp:
Location:
trunk/Source
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r92693 r92694  
     12011-08-09  Dmitry Lomov  <dslomov@google.com>
     2
     3        https://bugs.webkit.org/show_bug.cgi?id=65778
     4        [WebWorkers][chromium] Make statics thread-safe and make sure V8 API accesses correct isolates.
     5
     6        Reviewed by Dmitry Titov.
     7
     8        Covered by existing tests.
     9
     10        * bindings/v8/V8Binding.cpp:
     11        (WebCore::V8BindingPerIsolateData::V8BindingPerIsolateData):
     12        * bindings/v8/V8Binding.h:
     13        (WebCore::V8BindingPerIsolateData::lazyEventListenerToStringTemplate):
     14        (WebCore::V8BindingPerIsolateData::hiddenPropertyName):
     15        (WebCore::V8BindingPerIsolateData::globalHandleMap):
     16        (WebCore::AllowAllocation::AllowAllocation):Moving to V8Binding.h from V8Utilities.h to resolve header dependency.
     17        (WebCore::AllowAllocation::~AllowAllocation):
     18        (WebCore::AllowAllocation::current):
     19        (WebCore::SafeAllocation::newInstance):
     20        * bindings/v8/V8GCController.cpp:
     21        (WebCore::currentGlobalHandleMap):
     22        (WebCore::enumerateGlobalHandles):
     23        (WebCore::V8GCController::registerGlobalHandle):
     24        (WebCore::V8GCController::unregisterGlobalHandle):
     25        * bindings/v8/V8HiddenPropertyName.cpp:
     26        (WebCore::V8HiddenPropertyName::createString):
     27        * bindings/v8/V8HiddenPropertyName.h:
     28        (WebCore::V8HiddenPropertyName::V8HiddenPropertyName):
     29        * bindings/v8/V8LazyEventListener.cpp:
     30        (WebCore::V8LazyEventListener::prepareListenerObject):
     31        * bindings/v8/V8NPObject.cpp:
     32        * bindings/v8/V8Proxy.cpp:
     33        (WebCore::V8Proxy::checkNewLegal):
     34        * bindings/v8/V8Utilities.h:
     35        * bindings/v8/WorkerContextExecutionProxy.cpp:
     36        (WebCore::WorkerContextExecutionProxy::WorkerContextExecutionProxy):
     37        (WebCore::WorkerContextExecutionProxy::initIsolate):
     38        * bindings/v8/WorkerContextExecutionProxy.h:
     39        * bindings/v8/WorkerScriptController.cpp:
     40        (WebCore::WorkerScriptController::scheduleExecutionTermination):
     41
    1422011-08-09  Steve Block  <steveblock@google.com>
    243
  • trunk/Source/WebCore/bindings/v8/V8Binding.cpp

    r92068 r92694  
    5252V8BindingPerIsolateData::V8BindingPerIsolateData(v8::Isolate* isolate)
    5353    : m_domDataStore(0)
     54    , m_currentAllocationsAllowed(false)
    5455{
    5556}
  • trunk/Source/WebCore/bindings/v8/V8Binding.h

    r92643 r92694  
    3737#include "PlatformString.h"
    3838#include "V8DOMWrapper.h"
     39#include "V8GCController.h"
     40#include "V8HiddenPropertyName.h"
    3941#include <wtf/text/AtomicString.h>
    4042
     
    8688    };
    8789
     90    class AllowAllocation;
     91
     92#ifndef NDEBUG
     93    typedef HashMap<v8::Value*, GlobalHandleInfo*> GlobalHandleMap;
     94#endif
     95
    8896    class V8BindingPerIsolateData {
    8997    public:
     
    108116        v8::Persistent<v8::String>& toStringName() { return m_toStringName; }
    109117        v8::Persistent<v8::FunctionTemplate>& toStringTemplate() { return m_toStringTemplate; }
     118
     119        v8::Persistent<v8::FunctionTemplate>& lazyEventListenerToStringTemplate()
     120        {
     121            return m_lazyEventListenerToStringTemplate;
     122        }
     123
    110124        StringCache* stringCache() { return &m_stringCache; }
    111125
    112126        DOMDataList& allStores() { return m_domDataList; }
     127
     128        V8HiddenPropertyName* hiddenPropertyName() { return &m_hiddenPropertyName; }
    113129
    114130        void registerDOMDataStore(DOMDataStore* domDataStore)
     
    127143        // DOMDataStore is owned outside V8BindingPerIsolateData.
    128144        void setDOMDataStore(DOMDataStore* store) { m_domDataStore = store; }
     145
     146#ifndef NDEBUG
     147        GlobalHandleMap& globalHandleMap() { return m_globalHandleMap; }
     148#endif
    129149
    130150    private:
     
    136156        v8::Persistent<v8::String> m_toStringName;
    137157        v8::Persistent<v8::FunctionTemplate> m_toStringTemplate;
     158        v8::Persistent<v8::FunctionTemplate> m_lazyEventListenerToStringTemplate;
    138159        StringCache m_stringCache;
    139160
    140161        DOMDataList m_domDataList;
    141162        DOMDataStore* m_domDataStore;
    142     };
     163
     164        V8HiddenPropertyName m_hiddenPropertyName;
     165
     166        bool m_currentAllocationsAllowed;
     167        friend class AllowAllocation;
     168
     169#ifndef NDEBUG
     170        GlobalHandleMap m_globalHandleMap;
     171#endif
     172    };
     173
     174    class AllowAllocation {
     175    public:
     176        AllowAllocation()
     177        {
     178            V8BindingPerIsolateData* data = V8BindingPerIsolateData::current();
     179            m_previous = data->m_currentAllocationsAllowed;
     180            data->m_currentAllocationsAllowed = true;
     181        }
     182
     183        ~AllowAllocation()
     184        {
     185            V8BindingPerIsolateData* data = V8BindingPerIsolateData::current();
     186            data->m_currentAllocationsAllowed = m_previous;
     187        }
     188
     189        static bool current() { return V8BindingPerIsolateData::current()->m_currentAllocationsAllowed; }
     190
     191    private:
     192        bool m_previous;
     193    };
     194
     195    class SafeAllocation {
     196    public:
     197        static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>);
     198        static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::ObjectTemplate>);
     199        static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]);
     200    };
     201
     202    v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function)
     203    {
     204        if (function.IsEmpty())
     205            return v8::Local<v8::Object>();
     206        AllowAllocation allow;
     207        return function->NewInstance();
     208    }
     209
     210    v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::ObjectTemplate> objectTemplate)
     211    {
     212        if (objectTemplate.IsEmpty())
     213            return v8::Local<v8::Object>();
     214        AllowAllocation allow;
     215        return objectTemplate->NewInstance();
     216    }
     217
     218    v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
     219    {
     220        if (function.IsEmpty())
     221            return v8::Local<v8::Object>();
     222        AllowAllocation allow;
     223        return function->NewInstance(argc, argv);
     224    }
     225
    143226
    144227
  • trunk/Source/WebCore/bindings/v8/V8GCController.cpp

    r92643 r92694  
    8282// #endif
    8383//
    84 typedef HashMap<v8::Value*, GlobalHandleInfo*> GlobalHandleMap;
    85 
    86 static GlobalHandleMap& globalHandleMap()
    87 {
    88     DEFINE_STATIC_LOCAL(GlobalHandleMap, staticGlobalHandleMap, ());
    89     return staticGlobalHandleMap;
     84
     85static GlobalHandleMap& currentGlobalHandleMap()
     86{
     87    return V8BindingPerIsolateData::current()->globalHandleMap();
    9088}
    9189
     
    9492static void enumerateGlobalHandles()
    9593{
    96     for (GlobalHandleMap::iterator it = globalHandleMap().begin(), end = globalHandleMap().end(); it != end; ++it) {
     94    GlobalHandleMap& globalHandleMap = currentGlobalHandleMap();
     95    for (GlobalHandleMap::iterator it = globalHandleMap.begin(), end = globalHandleMap.end(); it != end; ++it) {
    9796        GlobalHandleInfo* info = it->second;
    9897        UNUSED_PARAM(info);
     
    104103void V8GCController::registerGlobalHandle(GlobalHandleType type, void* host, v8::Persistent<v8::Value> handle)
    105104{
    106     ASSERT(!globalHandleMap().contains(*handle));
    107     globalHandleMap().set(*handle, new GlobalHandleInfo(host, type));
     105    GlobalHandleMap& globalHandleMap = currentGlobalHandleMap();
     106    ASSERT(!globalHandleMap.contains(*handle));
     107    globalHandleMap.set(*handle, new GlobalHandleInfo(host, type));
    108108}
    109109
    110110void V8GCController::unregisterGlobalHandle(void* host, v8::Persistent<v8::Value> handle)
    111111{
    112     ASSERT(globalHandleMap().contains(*handle));
    113     GlobalHandleInfo* info = globalHandleMap().take(*handle);
     112    GlobalHandleMap& globalHandleMap = currentGlobalHandleMap();
     113    ASSERT(globalHandleMap.contains(*handle));
     114    GlobalHandleInfo* info = globalHandleMap.take(*handle);
    114115    ASSERT(info->m_host == host);
    115116    delete info;
  • trunk/Source/WebCore/bindings/v8/V8HiddenPropertyName.cpp

    r92643 r92694  
    3232#include "V8HiddenPropertyName.h"
    3333
     34#include "V8Binding.h"
    3435#include <string.h>
    3536#include <wtf/Vector.h>
     
    4344v8::Handle<v8::String> V8HiddenPropertyName::name() \
    4445{ \
    45     static v8::Persistent<v8::String>* string = createString("WebCore::HiddenProperty::" V8_AS_STRING(name)); \
    46     return *string; \
     46    V8HiddenPropertyName* hiddenPropertyName = V8BindingPerIsolateData::current()->hiddenPropertyName(); \
     47    if (hiddenPropertyName->m_##name.IsEmpty()) { \
     48        hiddenPropertyName->m_##name = createString("WebCore::HiddenProperty::" V8_AS_STRING(name)); \
     49    } \
     50    return hiddenPropertyName->m_##name; \
    4751}
    4852
     
    6064}
    6165
    62 v8::Persistent<v8::String>* V8HiddenPropertyName::createString(const char* key)
     66v8::Persistent<v8::String> V8HiddenPropertyName::createString(const char* key)
    6367{
    6468    v8::HandleScope scope;
    65     return new v8::Persistent<v8::String>(v8::Persistent<v8::String>::New(v8::String::NewSymbol(key)));
     69    return v8::Persistent<v8::String>::New(v8::String::NewSymbol(key));
    6670}
    6771
  • trunk/Source/WebCore/bindings/v8/V8HiddenPropertyName.h

    r92643 r92694  
    4949    class V8HiddenPropertyName {
    5050    public:
     51        V8HiddenPropertyName() { }
    5152#define V8_DECLARE_PROPERTY(name) static v8::Handle<v8::String> name();
    5253        V8_HIDDEN_PROPERTIES(V8_DECLARE_PROPERTY);
     
    5657
    5758    private:
    58         static v8::Persistent<v8::String>* createString(const char* key);
     59        static v8::Persistent<v8::String> createString(const char* key);
     60#define V8_DECLARE_FIELD(name) v8::Persistent<v8::String> m_##name;
     61        V8_HIDDEN_PROPERTIES(V8_DECLARE_FIELD);
     62#undef V8_DECLARE_FIELD
    5963    };
    6064
  • trunk/Source/WebCore/bindings/v8/V8LazyEventListener.cpp

    r92643 r92694  
    138138            // other use. That fails miserably if the actual wrapper source is
    139139            // returned.
    140             DEFINE_STATIC_LOCAL(v8::Persistent<v8::FunctionTemplate>, toStringTemplate, ());
     140            v8::Persistent<v8::FunctionTemplate>& toStringTemplate =
     141                V8BindingPerIsolateData::current()->lazyEventListenerToStringTemplate();
    141142            if (toStringTemplate.IsEmpty())
    142143                toStringTemplate = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New(V8LazyEventListenerToString));
  • trunk/Source/WebCore/bindings/v8/V8NPObject.cpp

    r92643 r92694  
    3535#include "HTMLPlugInElement.h"
    3636#include "NPV8Object.h"
     37#include "V8Binding.h"
    3738#include "V8DOMMap.h"
    3839#include "V8HTMLAppletElement.h"
  • trunk/Source/WebCore/bindings/v8/V8Proxy.cpp

    r92643 r92694  
    133133typedef HashMap<int, v8::FunctionTemplate*> FunctionTemplateMap;
    134134
    135 bool AllowAllocation::m_current = false;
    136 
    137135static void addMessageToConsole(Page* page, const String& message, const String& sourceID, unsigned lineNumber)
    138136{
     
    784782v8::Handle<v8::Value> V8Proxy::checkNewLegal(const v8::Arguments& args)
    785783{
    786     if (!AllowAllocation::m_current)
     784    if (!AllowAllocation::current())
    787785        return throwError(TypeError, "Illegal constructor");
    788786
  • trunk/Source/WebCore/bindings/v8/V8Utilities.h

    r92643 r92694  
    9696    }
    9797
    98     class AllowAllocation {
    99     public:
    100         inline AllowAllocation()
    101         {
    102             m_previous = m_current;
    103             m_current = true;
    104         }
    105 
    106         inline ~AllowAllocation()
    107         {
    108             m_current = m_previous;
    109         }
    110 
    111         static bool m_current;
    112 
    113     private:
    114         bool m_previous;
    115     };
    116 
    117     class SafeAllocation {
    118      public:
    119       static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>);
    120       static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::ObjectTemplate>);
    121       static inline v8::Local<v8::Object> newInstance(v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]);
    122     };
    123 
    124     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function)
    125     {
    126         if (function.IsEmpty())
    127             return v8::Local<v8::Object>();
    128         AllowAllocation allow;
    129         return function->NewInstance();
    130     }
    131 
    132     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::ObjectTemplate> objectTemplate)
    133     {
    134         if (objectTemplate.IsEmpty())
    135             return v8::Local<v8::Object>();
    136         AllowAllocation allow;
    137         return objectTemplate->NewInstance();
    138     }
    139 
    140     v8::Local<v8::Object> SafeAllocation::newInstance(v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
    141     {
    142         if (function.IsEmpty())
    143             return v8::Local<v8::Object>();
    144         AllowAllocation allow;
    145         return function->NewInstance(argc, argv);
    146     }
    147 
    14898} // namespace WebCore
    14999
  • trunk/Source/WebCore/bindings/v8/WorkerContextExecutionProxy.cpp

    r92643 r92694  
    8484    , m_recursion(0)
    8585{
    86     initV8();
     86    initIsolate();
    8787}
    8888
     
    109109}
    110110
    111 void WorkerContextExecutionProxy::initV8()
     111void WorkerContextExecutionProxy::initIsolate()
    112112{
    113113    // Tell V8 not to call the default OOM handler, binding code will handle it.
    114114    v8::V8::IgnoreOutOfMemoryException();
    115115    v8::V8::SetFatalErrorHandler(reportFatalErrorInV8);
     116
     117    v8::V8::SetGlobalGCPrologueCallback(&V8GCController::gcPrologue);
     118    v8::V8::SetGlobalGCEpilogueCallback(&V8GCController::gcEpilogue);
    116119
    117120    v8::ResourceConstraints resource_constraints;
  • trunk/Source/WebCore/bindings/v8/WorkerContextExecutionProxy.h

    r92643 r92694  
    7474
    7575    private:
    76         void initV8();
     76        void initIsolate();
    7777        bool initContextIfNeeded();
    7878        void dispose();
  • trunk/Source/WebCore/bindings/v8/WorkerScriptController.cpp

    r92643 r92694  
    9595void WorkerScriptController::scheduleExecutionTermination()
    9696{
    97     v8::V8::TerminateExecution();
     97    v8::V8::TerminateExecution(m_isolate);
    9898}
    9999
  • trunk/Source/WebKit/chromium/ChangeLog

    r92679 r92694  
     12011-08-09  Dmitry Lomov  <dslomov@google.com>
     2
     3        https://bugs.webkit.org/show_bug.cgi?id=65778
     4        [WebWorkers][chromium] Make statics thread-safe and make sure V8 API accesses correct isolates
     5
     6        Reviewed by Dmitry Titov.
     7
     8        * src/BoundObject.cpp:
     9
    1102011-08-09  Jochen Eisinger  <jochen@chromium.org>
    211
  • trunk/Source/WebKit/chromium/src/BoundObject.cpp

    r92643 r92694  
    3232#include "BoundObject.h"
    3333
     34#include "V8Binding.h"
    3435#include "V8Proxy.h"
    3536
Note: See TracChangeset for help on using the changeset viewer.