Changeset 150381 in webkit


Ignore:
Timestamp:
May 20, 2013 2:10:19 PM (11 years ago)
Author:
oliver@apple.com
Message:

Make C API more robust against null contexts
https://bugs.webkit.org/show_bug.cgi?id=116462

Reviewed by Anders Carlsson.

Handle null contexts in a non-crashy way. It's a bug to ever call the
API with a null context, and the absence of a context means we can't
produce a meaningful result, so we still assert in debug builds.

Now where possible we detect and early return, returning null for any
pointer type, NaN for doubles, and false for any boolean result.

  • API/JSBase.cpp:

(JSEvaluateScript):
(JSCheckScriptSyntax):
(JSReportExtraMemoryCost):

  • API/JSContextRef.cpp:

(JSContextGetGlobalObject):
(JSContextGetGroup):
(JSContextGetGlobalContext):
(JSContextCreateBacktrace):

  • API/JSObjectRef.cpp:

(JSObjectMake):
(JSObjectMakeFunctionWithCallback):
(JSObjectMakeConstructor):
(JSObjectMakeFunction):
(JSObjectMakeArray):
(JSObjectMakeDate):
(JSObjectMakeError):
(JSObjectMakeRegExp):
(JSObjectGetPrototype):
(JSObjectSetPrototype):
(JSObjectHasProperty):
(JSObjectGetProperty):
(JSObjectSetProperty):
(JSObjectGetPropertyAtIndex):
(JSObjectSetPropertyAtIndex):
(JSObjectDeleteProperty):
(JSObjectCopyPropertyNames):

  • API/JSValueRef.cpp:

(JSValueGetType):
(JSValueIsUndefined):
(JSValueIsNull):
(JSValueIsBoolean):
(JSValueIsNumber):
(JSValueIsString):
(JSValueIsObject):
(JSValueIsObjectOfClass):
(JSValueIsEqual):
(JSValueIsStrictEqual):
(JSValueIsInstanceOfConstructor):
(JSValueMakeUndefined):
(JSValueMakeNull):
(JSValueMakeBoolean):
(JSValueMakeNumber):
(JSValueMakeString):
(JSValueMakeFromJSONString):
(JSValueCreateJSONString):
(JSValueToBoolean):
(JSValueToNumber):
(JSValueToStringCopy):
(JSValueToObject):
(JSValueProtect):

  • API/JSWeakObjectMapRefPrivate.cpp:
Location:
trunk/Source/JavaScriptCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/JSBase.cpp

    r148696 r150381  
    4545JSValueRef JSEvaluateScript(JSContextRef ctx, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
    4646{
     47    if (!ctx) {
     48        ASSERT_NOT_REACHED();
     49        return 0;
     50    }
    4751    ExecState* exec = toJS(ctx);
    4852    APIEntryShim entryShim(exec);
     
    7276bool JSCheckScriptSyntax(JSContextRef ctx, JSStringRef script, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
    7377{
     78    if (!ctx) {
     79        ASSERT_NOT_REACHED();
     80        return false;
     81    }
    7482    ExecState* exec = toJS(ctx);
    7583    APIEntryShim entryShim(exec);
     
    107115void JSReportExtraMemoryCost(JSContextRef ctx, size_t size)
    108116{
     117    if (!ctx) {
     118        ASSERT_NOT_REACHED();
     119        return;
     120    }
    109121    ExecState* exec = toJS(ctx);
    110122    APIEntryShim entryShim(exec);
  • trunk/Source/JavaScriptCore/API/JSContextRef.cpp

    r149420 r150381  
    180180JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
    181181{
     182    if (!ctx) {
     183        ASSERT_NOT_REACHED();
     184        return 0;
     185    }
    182186    ExecState* exec = toJS(ctx);
    183187    APIEntryShim entryShim(exec);
     
    189193JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
    190194{
     195    if (!ctx) {
     196        ASSERT_NOT_REACHED();
     197        return 0;
     198    }
    191199    ExecState* exec = toJS(ctx);
    192200    return toRef(&exec->vm());
     
    195203JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx)
    196204{
     205    if (!ctx) {
     206        ASSERT_NOT_REACHED();
     207        return 0;
     208    }
    197209    ExecState* exec = toJS(ctx);
    198210    APIEntryShim entryShim(exec);
     
    203215JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize)
    204216{
     217    if (!ctx) {
     218        ASSERT_NOT_REACHED();
     219        return 0;
     220    }
    205221    ExecState* exec = toJS(ctx);
    206222    JSLockHolder lock(exec);
  • trunk/Source/JavaScriptCore/API/JSObjectRef.cpp

    r148696 r150381  
    8181JSObjectRef JSObjectMake(JSContextRef ctx, JSClassRef jsClass, void* data)
    8282{
     83    if (!ctx) {
     84        ASSERT_NOT_REACHED();
     85        return 0;
     86    }
    8387    ExecState* exec = toJS(ctx);
    8488    APIEntryShim entryShim(exec);
     
    96100JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
    97101{
     102    if (!ctx) {
     103        ASSERT_NOT_REACHED();
     104        return 0;
     105    }
    98106    ExecState* exec = toJS(ctx);
    99107    APIEntryShim entryShim(exec);
     
    103111JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSClassRef jsClass, JSObjectCallAsConstructorCallback callAsConstructor)
    104112{
     113    if (!ctx) {
     114        ASSERT_NOT_REACHED();
     115        return 0;
     116    }
    105117    ExecState* exec = toJS(ctx);
    106118    APIEntryShim entryShim(exec);
     
    117129JSObjectRef JSObjectMakeFunction(JSContextRef ctx, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception)
    118130{
     131    if (!ctx) {
     132        ASSERT_NOT_REACHED();
     133        return 0;
     134    }
    119135    ExecState* exec = toJS(ctx);
    120136    APIEntryShim entryShim(exec);
     
    139155JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
    140156{
     157    if (!ctx) {
     158        ASSERT_NOT_REACHED();
     159        return 0;
     160    }
    141161    ExecState* exec = toJS(ctx);
    142162    APIEntryShim entryShim(exec);
     
    164184JSObjectRef JSObjectMakeDate(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
    165185{
     186    if (!ctx) {
     187        ASSERT_NOT_REACHED();
     188        return 0;
     189    }
    166190    ExecState* exec = toJS(ctx);
    167191    APIEntryShim entryShim(exec);
     
    184208JSObjectRef JSObjectMakeError(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
    185209{
     210    if (!ctx) {
     211        ASSERT_NOT_REACHED();
     212        return 0;
     213    }
    186214    ExecState* exec = toJS(ctx);
    187215    APIEntryShim entryShim(exec);
     
    203231JSObjectRef JSObjectMakeRegExp(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[],  JSValueRef* exception)
    204232{
     233    if (!ctx) {
     234        ASSERT_NOT_REACHED();
     235        return 0;
     236    }
    205237    ExecState* exec = toJS(ctx);
    206238    APIEntryShim entryShim(exec);
     
    223255JSValueRef JSObjectGetPrototype(JSContextRef ctx, JSObjectRef object)
    224256{
     257    if (!ctx) {
     258        ASSERT_NOT_REACHED();
     259        return 0;
     260    }
    225261    ExecState* exec = toJS(ctx);
    226262    APIEntryShim entryShim(exec);
     
    232268void JSObjectSetPrototype(JSContextRef ctx, JSObjectRef object, JSValueRef value)
    233269{
     270    if (!ctx) {
     271        ASSERT_NOT_REACHED();
     272        return;
     273    }
    234274    ExecState* exec = toJS(ctx);
    235275    APIEntryShim entryShim(exec);
     
    243283bool JSObjectHasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
    244284{
     285    if (!ctx) {
     286        ASSERT_NOT_REACHED();
     287        return false;
     288    }
    245289    ExecState* exec = toJS(ctx);
    246290    APIEntryShim entryShim(exec);
     
    253297JSValueRef JSObjectGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
    254298{
     299    if (!ctx) {
     300        ASSERT_NOT_REACHED();
     301        return 0;
     302    }
    255303    ExecState* exec = toJS(ctx);
    256304    APIEntryShim entryShim(exec);
     
    269317void JSObjectSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception)
    270318{
     319    if (!ctx) {
     320        ASSERT_NOT_REACHED();
     321        return;
     322    }
    271323    ExecState* exec = toJS(ctx);
    272324    APIEntryShim entryShim(exec);
     
    292344JSValueRef JSObjectGetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception)
    293345{
     346    if (!ctx) {
     347        ASSERT_NOT_REACHED();
     348        return 0;
     349    }
    294350    ExecState* exec = toJS(ctx);
    295351    APIEntryShim entryShim(exec);
     
    309365void JSObjectSetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception)
    310366{
     367    if (!ctx) {
     368        ASSERT_NOT_REACHED();
     369        return;
     370    }
    311371    ExecState* exec = toJS(ctx);
    312372    APIEntryShim entryShim(exec);
     
    325385bool JSObjectDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
    326386{
     387    if (!ctx) {
     388        ASSERT_NOT_REACHED();
     389        return false;
     390    }
    327391    ExecState* exec = toJS(ctx);
    328392    APIEntryShim entryShim(exec);
     
    539603JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef ctx, JSObjectRef object)
    540604{
     605    if (!ctx) {
     606        ASSERT_NOT_REACHED();
     607        return 0;
     608    }
    541609    JSObject* jsObject = toJS(object);
    542610    ExecState* exec = toJS(ctx);
  • trunk/Source/JavaScriptCore/API/JSValueRef.cpp

    r146494 r150381  
    6565::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
    6666{
     67    if (!ctx) {
     68        ASSERT_NOT_REACHED();
     69        return kJSTypeUndefined;
     70    }
    6771    ExecState* exec = toJS(ctx);
    6872    APIEntryShim entryShim(exec);
     
    8690bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
    8791{
     92    if (!ctx) {
     93        ASSERT_NOT_REACHED();
     94        return false;
     95    }
    8896    ExecState* exec = toJS(ctx);
    8997    APIEntryShim entryShim(exec);
     
    95103bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
    96104{
     105    if (!ctx) {
     106        ASSERT_NOT_REACHED();
     107        return false;
     108    }
    97109    ExecState* exec = toJS(ctx);
    98110    APIEntryShim entryShim(exec);
     
    104116bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
    105117{
     118    if (!ctx) {
     119        ASSERT_NOT_REACHED();
     120        return false;
     121    }
    106122    ExecState* exec = toJS(ctx);
    107123    APIEntryShim entryShim(exec);
     
    113129bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
    114130{
     131    if (!ctx) {
     132        ASSERT_NOT_REACHED();
     133        return false;
     134    }
    115135    ExecState* exec = toJS(ctx);
    116136    APIEntryShim entryShim(exec);
     
    122142bool JSValueIsString(JSContextRef ctx, JSValueRef value)
    123143{
     144    if (!ctx) {
     145        ASSERT_NOT_REACHED();
     146        return false;
     147    }
    124148    ExecState* exec = toJS(ctx);
    125149    APIEntryShim entryShim(exec);
     
    131155bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
    132156{
     157    if (!ctx) {
     158        ASSERT_NOT_REACHED();
     159        return false;
     160    }
    133161    ExecState* exec = toJS(ctx);
    134162    APIEntryShim entryShim(exec);
     
    140168bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
    141169{
     170    if (!ctx || !jsClass) {
     171        ASSERT_NOT_REACHED();
     172        return false;
     173    }
    142174    ExecState* exec = toJS(ctx);
    143175    APIEntryShim entryShim(exec);
     
    160192bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
    161193{
     194    if (!ctx) {
     195        ASSERT_NOT_REACHED();
     196        return false;
     197    }
    162198    ExecState* exec = toJS(ctx);
    163199    APIEntryShim entryShim(exec);
     
    177213bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
    178214{
     215    if (!ctx) {
     216        ASSERT_NOT_REACHED();
     217        return false;
     218    }
    179219    ExecState* exec = toJS(ctx);
    180220    APIEntryShim entryShim(exec);
     
    188228bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
    189229{
     230    if (!ctx) {
     231        ASSERT_NOT_REACHED();
     232        return false;
     233    }
    190234    ExecState* exec = toJS(ctx);
    191235    APIEntryShim entryShim(exec);
     
    207251JSValueRef JSValueMakeUndefined(JSContextRef ctx)
    208252{
     253    if (!ctx) {
     254        ASSERT_NOT_REACHED();
     255        return 0;
     256    }
    209257    ExecState* exec = toJS(ctx);
    210258    APIEntryShim entryShim(exec);
     
    215263JSValueRef JSValueMakeNull(JSContextRef ctx)
    216264{
     265    if (!ctx) {
     266        ASSERT_NOT_REACHED();
     267        return 0;
     268    }
    217269    ExecState* exec = toJS(ctx);
    218270    APIEntryShim entryShim(exec);
     
    223275JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
    224276{
     277    if (!ctx) {
     278        ASSERT_NOT_REACHED();
     279        return 0;
     280    }
    225281    ExecState* exec = toJS(ctx);
    226282    APIEntryShim entryShim(exec);
     
    231287JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
    232288{
     289    if (!ctx) {
     290        ASSERT_NOT_REACHED();
     291        return 0;
     292    }
    233293    ExecState* exec = toJS(ctx);
    234294    APIEntryShim entryShim(exec);
     
    245305JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
    246306{
     307    if (!ctx) {
     308        ASSERT_NOT_REACHED();
     309        return 0;
     310    }
    247311    ExecState* exec = toJS(ctx);
    248312    APIEntryShim entryShim(exec);
     
    253317JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string)
    254318{
     319    if (!ctx) {
     320        ASSERT_NOT_REACHED();
     321        return 0;
     322    }
    255323    ExecState* exec = toJS(ctx);
    256324    APIEntryShim entryShim(exec);
     
    267335JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
    268336{
     337    if (!ctx) {
     338        ASSERT_NOT_REACHED();
     339        return 0;
     340    }
    269341    ExecState* exec = toJS(ctx);
    270342    APIEntryShim entryShim(exec);
     
    284356bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
    285357{
     358    if (!ctx) {
     359        ASSERT_NOT_REACHED();
     360        return false;
     361    }
    286362    ExecState* exec = toJS(ctx);
    287363    APIEntryShim entryShim(exec);
     
    293369double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
    294370{
     371    if (!ctx) {
     372        ASSERT_NOT_REACHED();
     373        return QNaN;
     374    }
    295375    ExecState* exec = toJS(ctx);
    296376    APIEntryShim entryShim(exec);
     
    310390JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
    311391{
     392    if (!ctx) {
     393        ASSERT_NOT_REACHED();
     394        return 0;
     395    }
    312396    ExecState* exec = toJS(ctx);
    313397    APIEntryShim entryShim(exec);
     
    327411JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
    328412{
     413    if (!ctx) {
     414        ASSERT_NOT_REACHED();
     415        return 0;
     416    }
    329417    ExecState* exec = toJS(ctx);
    330418    APIEntryShim entryShim(exec);
     
    344432void JSValueProtect(JSContextRef ctx, JSValueRef value)
    345433{
     434    if (!ctx) {
     435        ASSERT_NOT_REACHED();
     436        return;
     437    }
    346438    ExecState* exec = toJS(ctx);
    347439    APIEntryShim entryShim(exec);
  • trunk/Source/JavaScriptCore/API/JSWeakObjectMapRefPrivate.cpp

    r147962 r150381  
    5555void JSWeakObjectMapSet(JSContextRef ctx, JSWeakObjectMapRef map, void* key, JSObjectRef object)
    5656{
     57    if (!ctx) {
     58        ASSERT_NOT_REACHED();
     59        return;
     60    }
    5761    ExecState* exec = toJS(ctx);
    5862    APIEntryShim entryShim(exec);
     
    6670JSObjectRef JSWeakObjectMapGet(JSContextRef ctx, JSWeakObjectMapRef map, void* key)
    6771{
     72    if (!ctx) {
     73        ASSERT_NOT_REACHED();
     74        return 0;
     75    }
    6876    ExecState* exec = toJS(ctx);
    6977    APIEntryShim entryShim(exec);
     
    7381void JSWeakObjectMapRemove(JSContextRef ctx, JSWeakObjectMapRef map, void* key)
    7482{
     83    if (!ctx) {
     84        ASSERT_NOT_REACHED();
     85        return;
     86    }
    7587    ExecState* exec = toJS(ctx);
    7688    APIEntryShim entryShim(exec);
  • trunk/Source/JavaScriptCore/ChangeLog

    r150367 r150381  
     12013-05-20  Oliver Hunt  <oliver@apple.com>
     2
     3        Make C API more robust against null contexts
     4        https://bugs.webkit.org/show_bug.cgi?id=116462
     5
     6        Reviewed by Anders Carlsson.
     7
     8        Handle null contexts in a non-crashy way.  It's a bug to ever call the
     9        API with a null context, and the absence of a context means we can't
     10        produce a meaningful result, so we still assert in debug builds.
     11
     12        Now where possible we detect and early return, returning null for any
     13        pointer type, NaN for doubles, and false for any boolean result.
     14
     15        * API/JSBase.cpp:
     16        (JSEvaluateScript):
     17        (JSCheckScriptSyntax):
     18        (JSReportExtraMemoryCost):
     19        * API/JSContextRef.cpp:
     20        (JSContextGetGlobalObject):
     21        (JSContextGetGroup):
     22        (JSContextGetGlobalContext):
     23        (JSContextCreateBacktrace):
     24        * API/JSObjectRef.cpp:
     25        (JSObjectMake):
     26        (JSObjectMakeFunctionWithCallback):
     27        (JSObjectMakeConstructor):
     28        (JSObjectMakeFunction):
     29        (JSObjectMakeArray):
     30        (JSObjectMakeDate):
     31        (JSObjectMakeError):
     32        (JSObjectMakeRegExp):
     33        (JSObjectGetPrototype):
     34        (JSObjectSetPrototype):
     35        (JSObjectHasProperty):
     36        (JSObjectGetProperty):
     37        (JSObjectSetProperty):
     38        (JSObjectGetPropertyAtIndex):
     39        (JSObjectSetPropertyAtIndex):
     40        (JSObjectDeleteProperty):
     41        (JSObjectCopyPropertyNames):
     42        * API/JSValueRef.cpp:
     43        (JSValueGetType):
     44        (JSValueIsUndefined):
     45        (JSValueIsNull):
     46        (JSValueIsBoolean):
     47        (JSValueIsNumber):
     48        (JSValueIsString):
     49        (JSValueIsObject):
     50        (JSValueIsObjectOfClass):
     51        (JSValueIsEqual):
     52        (JSValueIsStrictEqual):
     53        (JSValueIsInstanceOfConstructor):
     54        (JSValueMakeUndefined):
     55        (JSValueMakeNull):
     56        (JSValueMakeBoolean):
     57        (JSValueMakeNumber):
     58        (JSValueMakeString):
     59        (JSValueMakeFromJSONString):
     60        (JSValueCreateJSONString):
     61        (JSValueToBoolean):
     62        (JSValueToNumber):
     63        (JSValueToStringCopy):
     64        (JSValueToObject):
     65        (JSValueProtect):
     66        * API/JSWeakObjectMapRefPrivate.cpp:
     67
    1682013-05-20  David Kilzer  <ddkilzer@apple.com>
    269
Note: See TracChangeset for help on using the changeset viewer.