author Mozilla Releng Treescript <release+treescript@mozilla.org>
Fri, 28 Jan 2022 19:05:43 +0000
changeset 605836 32a2d1ce4bab2979c3be01244f100f000ca77d8e
parent 589115 245e0bc541f95eb137e75f4cbe216707b56335b4
permissions -rw-r--r--
no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD cy -> 0eecc3c5396a7ce0ae207243a6879f559761b7a3 de -> 9220d3a92bcdc0287d9fedf94dfff7053058ccf1 fr -> 43ef41efa9e6ff87d6b4d0672d44c6d0d02f078c hsb -> 5d446c2cb85ab7fc253ec1b4fb6ed55438b0ef0f hye -> c5edf78a6f636234bb8bba68726a237d4ff9c397 it -> aa7f02187d8cecb017436efd84bf96faf8fbf63c nl -> ce81040d4aa33f478a2b20a5762f720c94b34ae6 sl -> dc3ce6db84b4e461483b2f3f25b0c627b69e446c uk -> 3deee0c202358c3bec34a0d3975ef764abf223a4

/* -*- 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/. */

#include <string.h>

#include "gc/GC.h"

#include "js/RootingAPI.h"
#include "js/StableStringChars.h"
#include "js/String.h"  // JS::StringToLinearString

#include "jsapi-tests/tests.h"

#include "vm/JSContext.h"
#include "vm/StringType.h"

#include "vm/JSContext-inl.h"

static bool SameChars(JSContext* cx, JSString* str1, JSString* str2,
                      size_t offset) {
  JS::AutoCheckCannotGC nogc(cx);

  const JS::Latin1Char* chars1 =
      JS::StringToLinearString(cx, str1)->latin1Chars(nogc);
  const JS::Latin1Char* chars2 =
      JS::StringToLinearString(cx, str2)->latin1Chars(nogc);

  return chars1 == chars2 + offset;

BEGIN_TEST(testDeduplication_ASSC) {
  // Test with a long enough string to avoid inline chars allocation.
  const char text[] =

  // Create a string to deduplicate later strings to.
  JS::RootedString original(cx);
  JS::RootedString str(cx);
  JS::RootedString dep(cx);
  JS::RootedString depdep(cx);
  JS::RootedString str2(cx);
  JS::RootedString dep2(cx);
  JS::RootedString depdep2(cx);

    // This test checks the behavior when GC is performed after allocating
    // all the following strings.
    // GC shouldn't happen in between them, even in compacting jobs.
    js::gc::AutoSuppressGC suppress(cx);

    original = JS_NewStringCopyZ(cx, text);

    // Create a chain of dependent strings, with a base string whose contents
    // match `original`'s.
    str = JS_NewStringCopyZ(cx, text);

    dep = JS_NewDependentString(cx, str, 10, 100);

    depdep = JS_NewDependentString(cx, dep, 10, 80);

    // Repeat. This one will not be prevented from deduplication.
    str2 = JS_NewStringCopyZ(cx, text);

    dep2 = JS_NewDependentString(cx, str2, 10, 100);

    depdep2 = JS_NewDependentString(cx, dep2, 10, 80);

  // Initializing an AutoStableStringChars with `depdep` should prevent the
  // owner of its chars (`str`) from deduplication.
  JS::AutoStableStringChars stable(cx);
  CHECK(stable.init(cx, depdep));

  const JS::Latin1Char* chars = stable.latin1Chars();
  CHECK(memcmp(chars, text + 20, 80 * sizeof(JS::Latin1Char)) == 0);

  // `depdep` should share chars with `str` but not with `original`.
  CHECK(SameChars(cx, depdep, str, 20));
  CHECK(!SameChars(cx, depdep, original, 20));

  // Same for `depdep2`.
  CHECK(SameChars(cx, depdep2, str2, 20));
  CHECK(!SameChars(cx, depdep2, original, 20));

  // Do a minor GC that will deduplicate `str2` to `original`, and would have
  // deduplicated `str` as well if it weren't prevented by the
  // AutoStableStringChars.

  // `depdep` should still share chars with `str` but not with `original`.
  CHECK(SameChars(cx, depdep, str, 20));
  CHECK(!SameChars(cx, depdep, original, 20));

  // `depdep2` should now share chars with both `str` and `original`.
  CHECK(SameChars(cx, depdep2, str2, 20));
  CHECK(SameChars(cx, depdep2, original, 20));

  return true;