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.

Header

Mercurial (409f3966645a)

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/* -*- 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 "frontend/BinTokenReaderTester.h"

#include "mozilla/ArrayUtils.h"
#include "mozilla/Casting.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/PodOperations.h"

#include "frontend/BinSource-macros.h"
#include "js/Result.h"

namespace js {
namespace frontend {

using BinFields = BinTokenReaderTester::BinFields;
using AutoList = BinTokenReaderTester::AutoList;
using AutoTaggedTuple = BinTokenReaderTester::AutoTaggedTuple;
using AutoTuple = BinTokenReaderTester::AutoTuple;

BinTokenReaderTester::BinTokenReaderTester(JSContext* cx, const uint8_t* start, const size_t length)
    : BinTokenReaderBase(cx, start, length)
{ }

BinTokenReaderTester::BinTokenReaderTester(JSContext* cx, const Vector<uint8_t>& buf)
    : BinTokenReaderBase(cx, buf.begin(), buf.length())
{ }

JS::Result<Ok>
BinTokenReaderTester::readHeader()
{
    // This format does not have a header.
    return Ok();
}

JS::Result<bool>
BinTokenReaderTester::readBool()
{
    updateLatestKnownGood();
    BINJS_MOZ_TRY_DECL(byte, readByte());

    switch (byte) {
      case 0:
        return false;
      case 1:
        return true;
      case 2:
        return raiseError("Not implemented: null boolean value");
      default:
        return raiseError("Invalid boolean value");
    }
}


// Nullable doubles (little-endian)
//
// NULL_FLOAT_REPRESENTATION (signaling NaN) => null
// anything other 64 bit sequence => IEEE-764 64-bit floating point number
JS::Result<double>
BinTokenReaderTester::readDouble()
{
    updateLatestKnownGood();

    uint8_t bytes[8];
    MOZ_ASSERT(sizeof(bytes) == sizeof(double));
    MOZ_TRY(readBuf(reinterpret_cast<uint8_t*>(bytes), mozilla::ArrayLength(bytes)));

    // Decode little-endian.
    const uint64_t asInt = mozilla::LittleEndian::readUint64(bytes);

    if (asInt == NULL_FLOAT_REPRESENTATION)
        return raiseError("Not implemented: null double value");

    // Canonicalize NaN, just to make sure another form of signalling NaN
    // doesn't slip past us.
    return JS::CanonicalizeNaN(mozilla::BitwiseCast<double>(asInt));
}

// Internal uint32_t
//
// Encoded as 4 bytes, little-endian.
MOZ_MUST_USE JS::Result<uint32_t>
BinTokenReaderTester::readInternalUint32()
{
    uint8_t bytes[4];
    MOZ_ASSERT(sizeof(bytes) == sizeof(uint32_t));
    MOZ_TRY(readBuf(bytes, 4));

    // Decode little-endian.
    return mozilla::LittleEndian::readUint32(bytes);
}



// Nullable strings:
// - "<string>" (not counted in byte length)
// - byte length (not counted in byte length)
// - bytes (UTF-8)
// - "</string>" (not counted in byte length)
//
// The special sequence of bytes `[255, 0]` (which is an invalid UTF-8 sequence)
// is reserved to `null`.
JS::Result<JSAtom*>
BinTokenReaderTester::readMaybeAtom()
{
    updateLatestKnownGood();

    MOZ_TRY(readConst("<string>"));

    RootedAtom result(cx_);

    // 1. Read byteLength
    BINJS_MOZ_TRY_DECL(byteLen, readInternalUint32());

    // 2. Reject if we can't read
    if (current_ + byteLen < current_) // Check for overflows
        return raiseError("Arithmetics overflow: string is too long");

    if (current_ + byteLen > stop_)
        return raiseError("Not enough bytes to read chars");

    if (byteLen == 2 && *current_ == 255 && *(current_ + 1) == 0) {
        // 3. Special case: null string.
        result = nullptr;
    } else {
        // 4. Other strings (bytes are copied)
        BINJS_TRY_VAR(result, Atomize(cx_, (const char*)current_, byteLen));
    }

    current_ += byteLen;
    MOZ_TRY(readConst("</string>"));
    return result.get();
}


// Nullable strings:
// - "<string>" (not counted in byte length)
// - byte length (not counted in byte length)
// - bytes (UTF-8)
// - "</string>" (not counted in byte length)
//
// The special sequence of bytes `[255, 0]` (which is an invalid UTF-8 sequence)
// is reserved to `null`.
JS::Result<Ok>
BinTokenReaderTester::readChars(Chars& out)
{
    updateLatestKnownGood();

    MOZ_TRY(readConst("<string>"));

    // 1. Read byteLength
    BINJS_MOZ_TRY_DECL(byteLen, readInternalUint32());

    // 2. Reject if we can't read
    if (current_ + byteLen < current_) // Check for overflows
        return raiseError("Arithmetics overflow: string is too long");

    if (current_ + byteLen > stop_)
        return raiseError("Not enough bytes to read chars");

    if (byteLen == 2 && *current_ == 255 && *(current_ + 1) == 0) {
        // 3. Special case: null string.
        return raiseError("Empty string");
    }

    // 4. Other strings (bytes are copied)
    if (!out.resize(byteLen))
        return raiseOOM();

    mozilla::PodCopy(out.begin(), current_, byteLen);

    current_ += byteLen;

    MOZ_TRY(readConst("</string>"));
    return Ok();
}

JS::Result<JSAtom*>
BinTokenReaderTester::readAtom()
{
    RootedAtom atom(cx_);
    MOZ_TRY_VAR(atom, readMaybeAtom());

    if (!atom)
        return raiseError("Empty string");
    return atom.get();
}

JS::Result<BinVariant>
BinTokenReaderTester::readVariant()
{
    MOZ_TRY(readConst("<string>"));
    BINJS_MOZ_TRY_DECL(byteLen, readInternalUint32());

    // 2. Reject if we can't read
    if (current_ + byteLen < current_) // Check for overflows
        return raiseError("Arithmetics overflow: string is too long");

    if (current_ + byteLen > stop_)
        return raiseError("Not enough bytes to read chars");

    if (byteLen == 2 && *current_ == 255 && *(current_ + 1) == 0) {
        // 3. Special case: null string.
        return raiseError("Empty variant");
    }

    BinaryASTSupport::CharSlice slice((const char*)current_, byteLen);
    current_ += byteLen;

    BINJS_MOZ_TRY_DECL(variant, cx_->runtime()->binast().binVariant(cx_, slice));
    if (!variant)
        return raiseError("Not a variant");

    MOZ_TRY(readConst("</string>"));
    return *variant;
}

JS::Result<BinTokenReaderBase::SkippableSubTree>
BinTokenReaderTester::readSkippableSubTree()
{
    updateLatestKnownGood();
    BINJS_MOZ_TRY_DECL(byteLen, readInternalUint32());

    if (current_ + byteLen > stop_ || current_ + byteLen < current_)
        return raiseError("Invalid byte length in readSkippableSubTree");

    const auto start = current_;

    current_ += byteLen;

    return BinTokenReaderBase::SkippableSubTree(start, byteLen);
}

// Untagged tuple:
// - "<tuple>";
// - contents (specified by the higher-level grammar);
// - "</tuple>"
JS::Result<Ok>
BinTokenReaderTester::enterUntaggedTuple(AutoTuple& guard)
{
    MOZ_TRY(readConst("<tuple>"));

    guard.init();
    return Ok();
}

// Tagged tuples:
// - "<tuple>";
// - "<head>";
// - non-null string `name`, followed by \0 (see `readString()`);
// - uint32_t number of fields;
// - array of `number of fields` non-null strings followed each by \0 (see `readString()`);
// - "</head>";
// - content (specified by the higher-level grammar);
// - "</tuple>"
JS::Result<Ok>
BinTokenReaderTester::enterTaggedTuple(BinKind& tag, BinFields& fields, AutoTaggedTuple& guard)
{
    // Header
    MOZ_TRY(readConst("<tuple>"));
    MOZ_TRY(readConst("<head>"));

    // This would probably be much faster with a HashTable, but we don't
    // really care about the speed of BinTokenReaderTester.
    do {

#define FIND_MATCH(CONSTRUCTOR, NAME) \
        if (matchConst(NAME, true)) { \
            tag = BinKind::CONSTRUCTOR; \
            break; \
        } // else

        FOR_EACH_BIN_KIND(FIND_MATCH)
#undef FIND_MATCH

        // else
        return raiseError("Invalid tag");
    } while(false);

    // Now fields.
    BINJS_MOZ_TRY_DECL(fieldNum, readInternalUint32());

    fields.clear();
    if (!fields.reserve(fieldNum))
        return raiseOOM();

    for (uint32_t i = 0; i < fieldNum; ++i) {
        // This would probably be much faster with a HashTable, but we don't
        // really care about the speed of BinTokenReaderTester.
        BinField field;
        do {

#define FIND_MATCH(CONSTRUCTOR, NAME) \
            if (matchConst(NAME, true)) { \
                field = BinField::CONSTRUCTOR; \
                break; \
            } // else

            FOR_EACH_BIN_FIELD(FIND_MATCH)
#undef FIND_MATCH

            // else
            return raiseError("Invalid field");
        } while (false);

        // Make sure that we do not have duplicate fields.
        // Search is linear, but again, we don't really care
        // in this implementation.
        for (uint32_t j = 0; j < i; ++j) {
            if (fields[j] == field) {
                return raiseError("Duplicate field");
            }
        }

        fields.infallibleAppend(field); // Already checked.
    }

    // End of header
    MOZ_TRY(readConst("</head>"));

    // Enter the body.
    guard.init();
    return Ok();
}

// List:
//
// - "<list>" (not counted in byte length);
// - uint32_t byte length (not counted in byte length);
// - uint32_t number of items;
// - contents (specified by higher-level grammar);
// - "</list>" (not counted in byte length)
//
// The total byte length of `number of items` + `contents` must be `byte length`.
JS::Result<Ok>
BinTokenReaderTester::enterList(uint32_t& items, AutoList& guard)
{
    MOZ_TRY(readConst("<list>"));
    guard.init();

    MOZ_TRY_VAR(items, readInternalUint32());
    return Ok();
}

void
BinTokenReaderTester::AutoBase::init()
{
    initialized_ = true;
}

BinTokenReaderTester::AutoBase::AutoBase(BinTokenReaderTester& reader)
    : initialized_(false)
    , reader_(reader)
{ }

BinTokenReaderTester::AutoBase::~AutoBase()
{
    // By now, the `AutoBase` must have been deinitialized by calling `done()`.
    // The only case in which we can accept not calling `done()` is if we have
    // bailed out because of an error.
    MOZ_ASSERT_IF(initialized_, reader_.cx_->isExceptionPending());
}

JS::Result<Ok>
BinTokenReaderTester::AutoBase::checkPosition(const uint8_t* expectedEnd)
{
    if (reader_.current_ != expectedEnd)
        return reader_.raiseError("Caller did not consume the expected set of bytes");

    return Ok();
}

BinTokenReaderTester::AutoList::AutoList(BinTokenReaderTester& reader)
    : AutoBase(reader)
{ }

void
BinTokenReaderTester::AutoList::init()
{
    AutoBase::init();
}

JS::Result<Ok>
BinTokenReaderTester::AutoList::done()
{
    MOZ_ASSERT(initialized_);
    initialized_ = false;
    if (reader_.cx_->isExceptionPending()) {
        // Already errored, no need to check further.
        return reader_.cx_->alreadyReportedError();
    }

    // Check suffix.
    MOZ_TRY(reader_.readConst("</list>"));

    return Ok();
}

BinTokenReaderTester::AutoTaggedTuple::AutoTaggedTuple(BinTokenReaderTester& reader)
    : AutoBase(reader)
{ }

JS::Result<Ok>
BinTokenReaderTester::AutoTaggedTuple::done()
{
    MOZ_ASSERT(initialized_);
    initialized_ = false;
    if (reader_.cx_->isExceptionPending()) {
        // Already errored, no need to check further.
        return reader_.cx_->alreadyReportedError();
    }

    // Check suffix.
    MOZ_TRY(reader_.readConst("</tuple>"));

    return Ok();
}

BinTokenReaderTester::AutoTuple::AutoTuple(BinTokenReaderTester& reader)
    : AutoBase(reader)
{ }

JS::Result<Ok>
BinTokenReaderTester::AutoTuple::done()
{
    MOZ_ASSERT(initialized_);
    initialized_ = false;
    if (reader_.cx_->isExceptionPending()) {
        // Already errored, no need to check further.
        return reader_.cx_->alreadyReportedError();
    }

    // Check suffix.
    MOZ_TRY(reader_.readConst("</tuple>"));

    return Ok();
}

} // namespace frontend
} // namespace js