Changeset 171564 in webkit


Ignore:
Timestamp:
Jul 24, 2014, 5:59:10 PM (11 years ago)
Author:
mark.lam@apple.com
Message:

JSWrapperMap's jsWrapperForObject() needs to keep weak prototype and constructors from being GCed.
<https://webkit.org/b/135258>

Reviewed by Mark Hahnenberg.

Where needed, we cache the prototype object pointer in a stack local var.
This allows it to be scanned by the GC, and hence be kept alive until
we use it. The constructor object will in turn be kept alive by the
prototype object.

Also added some comments to warn against future code additions that could
regress this issue.

  • API/JSWrapperMap.mm:

(-[JSObjCClassInfo allocateConstructorAndPrototypeWithSuperClassInfo:]):
(-[JSObjCClassInfo reallocateConstructorAndOrPrototype]):
(-[JSObjCClassInfo wrapperForObject:]):
(-[JSObjCClassInfo constructor]):

Location:
trunk/Source/JavaScriptCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/JSWrapperMap.mm

    r171553 r171564  
    465465        }
    466466    } else {
     467        // We need to hold a reference to the superclass prototype here on the stack
     468        // to that it won't get GC'ed while we do allocations between now and when we
     469        // set it in this class' prototype below.
     470        JSC::JSObject* superClassPrototype = superClassInfo->m_prototype.get();
     471
    467472        const char* className = class_getName(m_class);
    468473
     
    494499
    495500        // Set [Prototype].
    496         JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(m_prototype.get()), toRef(superClassInfo->m_prototype.get()));
     501        JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(m_prototype.get()), toRef(superClassPrototype));
    497502    }
    498503}
     
    501506{
    502507    [self allocateConstructorAndPrototypeWithSuperClassInfo:[m_context.wrapperMap classInfoForClass:class_getSuperclass(m_class)]];
     508    // We should not add any code here that can trigger a GC or the prototype and
     509    // constructor that we just created may be collected before they can be used.
    503510}
    504511
     
    520527        [self reallocateConstructorAndOrPrototype];
    521528    ASSERT(!!m_prototype);
     529    // We need to hold a reference to the prototype here on the stack to that it won't
     530    // get GC'ed while we create the wrapper below.
     531    JSC::JSObject* prototype = m_prototype.get();
    522532
    523533    JSObjectRef wrapper = makeWrapper([m_context JSGlobalContextRef], m_classRef, object);
    524     JSObjectSetPrototype([m_context JSGlobalContextRef], wrapper, toRef(m_prototype.get()));
     534    JSObjectSetPrototype([m_context JSGlobalContextRef], wrapper, toRef(prototype));
    525535    return [JSValue valueWithJSValueRef:wrapper inContext:m_context];
    526536}
     
    531541        [self reallocateConstructorAndOrPrototype];
    532542    ASSERT(!!m_constructor);
     543    // If we need to add any code here in the future that can trigger a GC, we should
     544    // cache the constructor pointer in a stack local var first so that it is protected
     545    // from the GC until it gets used below.
    533546    return [JSValue valueWithJSValueRef:toRef(m_constructor.get()) inContext:m_context];
    534547}
  • trunk/Source/JavaScriptCore/ChangeLog

    r171558 r171564  
     12014-07-24  Mark Lam  <mark.lam@apple.com>
     2
     3        JSWrapperMap's jsWrapperForObject() needs to keep weak prototype and constructors from being GCed.
     4        <https://webkit.org/b/135258>
     5
     6        Reviewed by Mark Hahnenberg.
     7
     8        Where needed, we cache the prototype object pointer in a stack local var.
     9        This allows it to be scanned by the GC, and hence be kept alive until
     10        we use it.  The constructor object will in turn be kept alive by the
     11        prototype object.
     12
     13        Also added some comments to warn against future code additions that could
     14        regress this issue.
     15
     16        * API/JSWrapperMap.mm:
     17        (-[JSObjCClassInfo allocateConstructorAndPrototypeWithSuperClassInfo:]):
     18        (-[JSObjCClassInfo reallocateConstructorAndOrPrototype]):
     19        (-[JSObjCClassInfo wrapperForObject:]):
     20        (-[JSObjCClassInfo constructor]):
     21
    1222014-07-24  Joseph Pecoraro  <pecoraro@apple.com>
    223
Note: See TracChangeset for help on using the changeset viewer.