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 (b6d82b1a6b02)

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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */
/* 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/. */

#if defined(XP_UNIX)

#  include <dirent.h>
#  include <fcntl.h>
#  include <sys/stat.h>
#  include <unistd.h>

#elif defined(XP_WIN)

#  include <windows.h>

#endif

#include "mozilla/Maybe.h"

#include "jsapi.h"

#include "frontend/BinASTParser.h"
#include "frontend/FullParseHandler.h"
#include "frontend/ParseContext.h"
#include "frontend/Parser.h"
#include "js/Vector.h"

#include "jsapi-tests/tests.h"

#include "vm/Interpreter.h"

using UsedNameTracker = js::frontend::UsedNameTracker;
using namespace JS;
using namespace js;

// Hack: These tests need access to resources, which are present in the source
// dir but not copied by our build system. To simplify things, we chdir to the
// source dir at the start of each test and return to the previous directory
// afterwards.

#if defined(XP_UNIX)

#  include <sys/param.h>

static int gJsDirectory(0);
void enterJsDirectory() {
  // Save current directory.
  MOZ_ASSERT(gJsDirectory == 0);
  gJsDirectory = open(".", O_RDONLY);
  MOZ_ASSERT(gJsDirectory != 0, "Could not open directory '.'");
  // Go to the directory provided by the test harness, if any.
  const char* destination = getenv("CPP_UNIT_TESTS_DIR_JS_SRC");
  if (destination) {
    if (chdir(destination) == -1) {
      MOZ_CRASH_UNSAFE_PRINTF("Could not chdir to %s", destination);
    }
  }
}

void exitJsDirectory() {
  MOZ_ASSERT(gJsDirectory);
  if (fchdir(gJsDirectory) == -1) {
    MOZ_CRASH("Could not return to original directory");
  }
  if (close(gJsDirectory) != 0) {
    MOZ_CRASH("Could not close js directory");
  }
  gJsDirectory = 0;
}

#else

char gJsDirectory[MAX_PATH] = {0};

void enterJsDirectory() {
  // Save current directory.
  MOZ_ASSERT(strlen(gJsDirectory) == 0);
  auto result = GetCurrentDirectory(MAX_PATH, gJsDirectory);
  if (result <= 0) {
    MOZ_CRASH("Could not get current directory");
  }
  if (result > MAX_PATH) {
    MOZ_CRASH_UNSAFE_PRINTF(
        "Could not get current directory: needed %ld bytes, got %ld\n", result,
        MAX_PATH);
  }

  // Find destination directory, if any.
  char destination[MAX_PATH];
  result = GetEnvironmentVariable("CPP_UNIT_TESTS_DIR_JS_SRC", destination,
                                  MAX_PATH);
  if (result == 0) {
    if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
      return;  // No need to chdir
    } else {
      MOZ_CRASH("Could not get CPP_UNIT_TESTS_DIR_JS_SRC");
    }
  }
  if (result > MAX_PATH) {
    MOZ_CRASH_UNSAFE_PRINTF(
        "Could not get CPP_UNIT_TESTS_DIR_JS_SRC: needed %ld bytes, got %ld\n",
        result, MAX_PATH);
  }

  // Go to the directory.
  if (SetCurrentDirectory(destination) == 0) {
    MOZ_CRASH_UNSAFE_PRINTF("Could not chdir to %s", destination);
  }
}

void exitJsDirectory() {
  MOZ_ASSERT(strlen(gJsDirectory) > 0);
  if (SetCurrentDirectory(gJsDirectory) == 0) {
    MOZ_CRASH("Could not return to original directory");
  }
  gJsDirectory[0] = 0;
}

#endif  // defined(XP_UNIX) || defined(XP_WIN)

void readFull(const char* path, js::Vector<uint8_t>& buf) {
  enterJsDirectory();
  buf.shrinkTo(0);
  FILE* in = fopen(path, "rb");
  if (!in) {
    MOZ_CRASH_UNSAFE_PRINTF("Could not open %s: %s", path, strerror(errno));
  }

  struct stat info;
  if (stat(path, &info) < 0) {
    MOZ_CRASH_UNSAFE_PRINTF("Could not get stat on %s", path);
  }

  if (!buf.growBy(info.st_size)) {
    MOZ_CRASH("OOM");
  }

  int result = fread(buf.begin(), 1, info.st_size, in);
  if (fclose(in) != 0) {
    MOZ_CRASH("Could not close input file");
  }
  if (result != info.st_size) {
    MOZ_CRASH_UNSAFE_PRINTF(
        "Read error while reading %s: expected %llu bytes, got %llu", path,
        (unsigned long long)info.st_size, (unsigned long long)result);
  }
  exitJsDirectory();
}

void readFull(JSContext* cx, const char* path, js::Vector<char16_t>& buf) {
  buf.shrinkTo(0);

  js::Vector<uint8_t> intermediate(cx);
  readFull(path, intermediate);

  if (!buf.appendAll(intermediate)) {
    MOZ_CRASH("Couldn't read data");
  }
}

// Invariant: `path` must end with directory separator.
template <typename Tok>
void runTestFromPath(JSContext* cx, const char* path) {
  const char BIN_SUFFIX[] = ".binjs";
  const char TXT_SUFFIX[] = ".js";
  fprintf(stderr, "runTestFromPath: entering directory '%s'\n", path);
  const size_t pathlen = strlen(path);

#if defined(XP_UNIX)
  MOZ_ASSERT(path[pathlen - 1] == '/');

  // Read the list of files in the directory.
  enterJsDirectory();
  DIR* dir = opendir(path);
  exitJsDirectory();
  if (!dir) {
    MOZ_CRASH("Couldn't open directory");
  }

  while (auto entry = readdir(dir)) {
    const char* d_name = entry->d_name;
    const bool isDirectory = entry->d_type == DT_DIR;

#elif defined(XP_WIN)
  MOZ_ASSERT(path[pathlen - 1] == '\\');

  Vector<char> pattern(cx);
  if (!pattern.append(path, pathlen)) {
    MOZ_CRASH();
  }
  if (!pattern.append('*')) {
    MOZ_CRASH();
  }
  if (!pattern.append('\0')) {
    MOZ_CRASH();
  }

  WIN32_FIND_DATA FindFileData;
  enterJsDirectory();
  HANDLE hFind = FindFirstFile(pattern.begin(), &FindFileData);
  exitJsDirectory();
  for (bool found = (hFind != INVALID_HANDLE_VALUE); found;
       found = FindNextFile(hFind, &FindFileData)) {
    const char* d_name = FindFileData.cFileName;
    const bool isDirectory =
        FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;

#endif  // defined(XP_UNIX) || defined(XP_WIN)

    const size_t namlen = strlen(d_name);

    // Recurse through subdirectories.
    if (isDirectory) {
      if (strcmp(d_name, ".") == 0) {
        continue;
      }
      if (strcmp(d_name, "..") == 0) {
        continue;
      }

      Vector<char> subPath(cx);
      // Start with `path` (including directory separator).
      if (!subPath.append(path, pathlen)) {
        MOZ_CRASH();
      }
      if (!subPath.append(d_name, namlen)) {
        MOZ_CRASH();
      }
      // Append same directory separator.
      if (!subPath.append(path[pathlen - 1])) {
        MOZ_CRASH();
      }
      if (!subPath.append(0)) {
        MOZ_CRASH();
      }
      runTestFromPath<Tok>(cx, subPath.begin());
      continue;
    }

    // Make sure that we run GC between two tests. Otherwise, since we're
    // running everything from the same cx and without returning to JS, there is
    // nothing to deallocate the ASTs.
    JS_GC(cx);

    LifoAllocScope allocScope(&cx->tempLifoAlloc());

    // Find files whose name ends with ".binjs".
    fprintf(stderr, "Considering %s\n", d_name);
    if (namlen < sizeof(BIN_SUFFIX)) {
      continue;
    }
    if (strncmp(d_name + namlen - (sizeof(BIN_SUFFIX) - 1), BIN_SUFFIX,
                sizeof(BIN_SUFFIX)) != 0)
      continue;

    // Find text file.
    Vector<char> txtPath(cx);
    if (!txtPath.append(path, pathlen)) {
      MOZ_CRASH();
    }
    if (!txtPath.append(d_name, namlen)) {
      MOZ_CRASH();
    }
    txtPath.shrinkBy(sizeof(BIN_SUFFIX) - 1);
    if (!txtPath.append(TXT_SUFFIX, sizeof(TXT_SUFFIX))) {
      MOZ_CRASH();
    }
    fprintf(stderr, "Testing %s\n", txtPath.begin());

    // Read text file.
    js::Vector<char16_t> txtSource(cx);
    readFull(cx, txtPath.begin(), txtSource);

    // Parse text file.
    CompileOptions txtOptions(cx);
    txtOptions.setFileAndLine(txtPath.begin(), 0);

    UsedNameTracker txtUsedNames(cx);

    RootedScriptSourceObject sourceObject(
        cx,
        frontend::CreateScriptSourceObject(cx, txtOptions, mozilla::Nothing()));
    if (!sourceObject) {
      MOZ_CRASH("Couldn't initialize ScriptSourceObject");
    }

    js::frontend::Parser<js::frontend::FullParseHandler, char16_t> txtParser(
        cx, allocScope.alloc(), txtOptions, txtSource.begin(),
        txtSource.length(),
        /* foldConstants = */ false, txtUsedNames, nullptr, nullptr,
        sourceObject, frontend::ParseGoal::Script);
    if (!txtParser.checkOptions()) {
      MOZ_CRASH("Bad options");
    }

    auto txtParsed =
        txtParser
            .parse();  // Will be deallocated once `parser` goes out of scope.
    RootedValue txtExn(cx);
    if (!txtParsed) {
      // Save exception for more detailed error message, if necessary.
      if (!js::GetAndClearException(cx, &txtExn)) {
        MOZ_CRASH("Couldn't clear exception");
      }
    }

    // Read binary file.
    Vector<char> binPath(cx);
    if (!binPath.append(path, pathlen)) {
      MOZ_CRASH();
    }
    if (!binPath.append(d_name, namlen)) {
      MOZ_CRASH();
    }
    if (!binPath.append(0)) {
      MOZ_CRASH();
    }

    js::Vector<uint8_t> binSource(cx);
    readFull(binPath.begin(), binSource);

    // Parse binary file.
    CompileOptions binOptions(cx);
    binOptions.setFileAndLine(binPath.begin(), 0);

    frontend::UsedNameTracker binUsedNames(cx);

    frontend::Directives directives(false);
    frontend::GlobalSharedContext globalsc(cx, ScopeKind::Global, directives,
                                           false);

    RootedScriptSourceObject sourceObj(
        cx,
        frontend::CreateScriptSourceObject(cx, binOptions, mozilla::Nothing()));
    if (!sourceObj) {
      MOZ_CRASH();
    }

    frontend::BinASTParser<Tok> binParser(cx, allocScope.alloc(), binUsedNames,
                                          binOptions, sourceObj);

    auto binParsed = binParser.parse(
        &globalsc,
        binSource);  // Will be deallocated once `reader` goes out of scope.
    RootedValue binExn(cx);
    if (binParsed.isErr()) {
      // Save exception for more detailed error message, if necessary.
      if (!js::GetAndClearException(cx, &binExn)) {
        MOZ_CRASH("Couldn't clear binExn");
      }
    }

    // The binary parser should accept the file iff the text parser has.
    if (binParsed.isOk() && !txtParsed) {
      fprintf(stderr, "Text file parsing failed: ");

      js::ErrorReport report(cx);
      if (!report.init(cx, txtExn, js::ErrorReport::WithSideEffects)) {
        MOZ_CRASH("Couldn't report txtExn");
      }

      PrintError(cx, stderr, report.toStringResult(), report.report(),
                 /* reportWarnings */ true);
      MOZ_CRASH("Binary parser accepted a file that text parser rejected");
    }

    if (binParsed.isErr() && txtParsed) {
      fprintf(stderr, "Binary file parsing failed: ");

      js::ErrorReport report(cx);
      if (!report.init(cx, binExn, js::ErrorReport::WithSideEffects)) {
        MOZ_CRASH("Couldn't report binExn");
      }

      PrintError(cx, stderr, report.toStringResult(), report.report(),
                 /* reportWarnings */ true);
      MOZ_CRASH("Binary parser rejected a file that text parser accepted");
    }

    if (binParsed.isErr()) {
      fprintf(stderr,
              "Binary parser and text parser agree that %s is invalid\n",
              txtPath.begin());
      continue;
    }

#if defined(DEBUG)  // Dumping an AST is only defined in DEBUG builds
    // Compare ASTs.
    Sprinter binPrinter(cx);
    if (!binPrinter.init()) {
      MOZ_CRASH("Couldn't display binParsed");
    }
    DumpParseTree(binParsed.unwrap(), binPrinter);

    Sprinter txtPrinter(cx);
    if (!txtPrinter.init()) {
      MOZ_CRASH("Couldn't display txtParsed");
    }
    DumpParseTree(txtParsed, txtPrinter);

    if (strcmp(binPrinter.string(), txtPrinter.string()) != 0) {
      fprintf(stderr,
              "Got distinct ASTs when parsing %s "
              "(%p/%p):\n\tBINARY\n%s\n\n\tTEXT\n%s\n",
              txtPath.begin(), (void*)binPrinter.getOffset(),
              (void*)txtPrinter.getOffset(), binPrinter.string(),
              txtPrinter.string());
#  if 0   // Not for release, but useful for debugging.
          // In case of error, this dumps files to /tmp, so they may
          // easily be diffed.
            auto fd = open("/tmp/bin.ast", O_CREAT | O_TRUNC | O_WRONLY, 0666);
            if (!fd) {
                MOZ_CRASH("Could not open bin.ast");
            }
            auto result = write(fd, binPrinter.string(), binPrinter.stringEnd() - binPrinter.string());
            if (result <= 0) {
                MOZ_CRASH("Could not write to bin.ast");
            }
            result = close(fd);
            if (result != 0) {
                MOZ_CRASH("Could not close bin.ast");
            }

            fd = open("/tmp/txt.ast", O_CREAT | O_TRUNC | O_WRONLY, 0666);
            if (!fd) {
                MOZ_CRASH("Could not open txt.ast");
            }
            result = write(fd, txtPrinter.string(), txtPrinter.stringEnd() - txtPrinter.string());
            if (result <= 0) {
                MOZ_CRASH("Could not write to txt.ast");
            }
            result = close(fd);
            if (result != 0) {
                MOZ_CRASH("Could not close txt.ast");
            }
#  endif  // 0
      MOZ_CRASH("Got distinct ASTs");
    }

    fprintf(stderr, "Got the same AST when parsing %s\n", txtPath.begin());
#endif  // defined(DEBUG)
  }

#if defined(XP_WIN)
  if (!FindClose(hFind)) {
    MOZ_CRASH("Could not close Find");
  }
#elif defined(XP_UNIX)
  if (closedir(dir) != 0) {
    MOZ_CRASH("Could not close dir");
  }
#endif  // defined(XP_WIN)
}

BEGIN_TEST(testBinASTReaderMultipartECMAScript2) {
#if defined(XP_WIN)
  runTestFromPath<js::frontend::BinASTTokenReaderMultipart>(
      cx, "jsapi-tests\\binast\\parser\\multipart\\");
#else
  runTestFromPath<js::frontend::BinASTTokenReaderMultipart>(
      cx, "jsapi-tests/binast/parser/multipart/");
#endif  // defined(XP_XIN)
  return true;
}
END_TEST(testBinASTReaderMultipartECMAScript2)