DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jsapi-tests/tests.h"

/*
 * Test that resolve hook recursion for the same object and property is
 * prevented.
 */
BEGIN_TEST(testResolveRecursion)
{
    static const JSClass my_resolve_class = {
        "MyResolve",
        JSCLASS_HAS_PRIVATE,
        nullptr, // add
        nullptr, // delete
        nullptr, // get
        nullptr, // set
        nullptr, // enumerate
        my_resolve
    };

    obj1.init(cx, JS_NewObject(cx, &my_resolve_class));
    CHECK(obj1);
    obj2.init(cx, JS_NewObject(cx, &my_resolve_class));
    CHECK(obj2);
    JS_SetPrivate(obj1, this);
    JS_SetPrivate(obj2, this);

    JS::RootedValue obj1Val(cx, JS::ObjectValue(*obj1));
    JS::RootedValue obj2Val(cx, JS::ObjectValue(*obj2));
    CHECK(JS_DefineProperty(cx, global, "obj1", obj1Val, 0));
    CHECK(JS_DefineProperty(cx, global, "obj2", obj2Val, 0));

    resolveEntryCount = 0;
    resolveExitCount = 0;

    /* Start the essence of the test via invoking the first resolve hook. */
    JS::RootedValue v(cx);
    EVAL("obj1.x", &v);
    CHECK(v.isFalse());
    CHECK_EQUAL(resolveEntryCount, 4);
    CHECK_EQUAL(resolveExitCount, 4);

    obj1 = nullptr;
    obj2 = nullptr;
    return true;
}

JS::PersistentRootedObject obj1;
JS::PersistentRootedObject obj2;
int resolveEntryCount;
int resolveExitCount;

struct AutoIncrCounters {

    explicit AutoIncrCounters(cls_testResolveRecursion* t) : t(t) {
        t->resolveEntryCount++;
    }

    ~AutoIncrCounters() {
        t->resolveExitCount++;
    }

    cls_testResolveRecursion* t;
};

bool
doResolve(JS::HandleObject obj, JS::HandleId id, bool* resolvedp)
{
    CHECK_EQUAL(resolveExitCount, 0);
    AutoIncrCounters incr(this);
    CHECK(obj == obj1 || obj == obj2);

    CHECK(JSID_IS_STRING(id));

    JSFlatString* str = JS_FlattenString(cx, JSID_TO_STRING(id));
    CHECK(str);
    JS::RootedValue v(cx);
    if (JS_FlatStringEqualsAscii(str, "x")) {
        if (obj == obj1) {
            /* First resolve hook invocation. */
            CHECK_EQUAL(resolveEntryCount, 1);
            EVAL("obj2.y = true", &v);
            CHECK(v.isTrue());
            CHECK(JS_DefinePropertyById(cx, obj, id, JS::FalseHandleValue, JSPROP_RESOLVING));
            *resolvedp = true;
            return true;
        }
        if (obj == obj2) {
            CHECK_EQUAL(resolveEntryCount, 4);
            *resolvedp = false;
            return true;
        }
    } else if (JS_FlatStringEqualsAscii(str, "y")) {
        if (obj == obj2) {
            CHECK_EQUAL(resolveEntryCount, 2);
            CHECK(JS_DefinePropertyById(cx, obj, id, JS::NullHandleValue, JSPROP_RESOLVING));
            EVAL("obj1.x", &v);
            CHECK(v.isUndefined());
            EVAL("obj1.y", &v);
            CHECK(v.isInt32(0));
            *resolvedp = true;
            return true;
        }
        if (obj == obj1) {
            CHECK_EQUAL(resolveEntryCount, 3);
            EVAL("obj1.x", &v);
            CHECK(v.isUndefined());
            EVAL("obj1.y", &v);
            CHECK(v.isUndefined());
            EVAL("obj2.y", &v);
            CHECK(v.isNull());
            EVAL("obj2.x", &v);
            CHECK(v.isUndefined());
            EVAL("obj1.y = 0", &v);
            CHECK(v.isInt32(0));
            *resolvedp = true;
            return true;
        }
    }
    CHECK(false);
    return false;
}

static bool
my_resolve(JSContext* cx, JS::HandleObject obj, JS::HandleId id, bool* resolvedp)
{
    return static_cast<cls_testResolveRecursion*>(JS_GetPrivate(obj))->
           doResolve(obj, id, resolvedp);
}
END_TEST(testResolveRecursion)

/*
 * Test that JS_InitStandardClasses does not cause resolve hooks to be called.
 *
 * (XPConnect apparently does have global classes, such as the one created by
 * nsMessageManagerScriptExecutor::InitChildGlobalInternal(), that have resolve
 * hooks which can call back into JS, and on which JS_InitStandardClasses is
 * called. Calling back into JS in the middle of resolving `undefined` is bad.)
 */
BEGIN_TEST(testResolveRecursion_InitStandardClasses)
{
    CHECK(JS_InitStandardClasses(cx, global));
    return true;
}

const JSClass* getGlobalClass() override {
    static const JSClass myGlobalClass = {
        "testResolveRecursion_InitStandardClasses_myGlobalClass",
        JSCLASS_GLOBAL_FLAGS,
        nullptr, // add
        nullptr, // delete
        nullptr, // get
        nullptr, // set
        nullptr, // enumerate
        my_resolve,
        nullptr, // mayResolve
        nullptr, // finalize
        nullptr, // call
        nullptr, // hasInstance
        nullptr, // construct
        JS_GlobalObjectTraceHook
    };

    return &myGlobalClass;
}

static bool
my_resolve(JSContext* cx, JS::HandleObject obj, JS::HandleId id, bool* resolvedp)
{
    MOZ_ASSERT_UNREACHABLE("resolve hook should not be called from InitStandardClasses");
    JS_ReportError(cx, "FAIL");
    return false;
}
END_TEST(testResolveRecursion_InitStandardClasses)