Commit b0370355 authored by Lars T Hansen's avatar Lars T Hansen
Browse files

Bug 1599659 - Predicate to test context for live SABs. r=jonco

SABs become foreground-finalizable so that we can access the runtime
during finalization.  Then a simple counter on the runtime will track
live SABs for the runtime, and the predicate on the context can get
its information from the runtime.

Fallout: SABs are now enabled on the globals used for jsapi-tests.

Differential Revision: https://phabricator.services.mozilla.com/D55783

--HG--
extra : moz-landing-system : lando
parent d6200622
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ UNIFIED_SOURCES += [
    'testRegExp.cpp',
    'testResolveRecursion.cpp',
    'tests.cpp',
    'testSABAccounting.cpp',
    'testSameValue.cpp',
    'testSavedStacks.cpp',
    'testScriptInfo.cpp',
+34 −0
Original line number Diff line number Diff line
#include "jsapi.h"
#include "jsfriendapi.h"

#include "builtin/TestingFunctions.h"
#include "js/SharedArrayBuffer.h"
#include "jsapi-tests/tests.h"

BEGIN_TEST(testSABAccounting) {
  // Purge what we can
  JS::PrepareForFullGC(cx);
  NonIncrementalGC(cx, GC_SHRINK, GCReason::API);

  // Self-hosting and chrome code should not use SABs, or the point of this
  // predicate is completely lost.
  CHECK(!JS_ContainsSharedArrayBuffer(cx));

  JS::RootedObject obj(cx), obj2(cx);
  CHECK(obj = JS::NewSharedArrayBuffer(cx, 4096));
  CHECK(JS_ContainsSharedArrayBuffer(cx));
  CHECK(obj2 = JS::NewSharedArrayBuffer(cx, 4096));
  CHECK(JS_ContainsSharedArrayBuffer(cx));

  // Discard those objects again.
  obj = nullptr;
  obj2 = nullptr;
  JS::PrepareForFullGC(cx);
  NonIncrementalGC(cx, GC_SHRINK, GCReason::API);

  // Should be back to base state.
  CHECK(!JS_ContainsSharedArrayBuffer(cx));

  return true;
}
END_TEST(testSABAccounting)
+2 −1
Original line number Diff line number Diff line
@@ -92,7 +92,8 @@ JSObject* JSAPITest::createGlobal(JSPrincipals* principals) {
      .setStreamsEnabled(true)
      .setFieldsEnabled(true)
      .setAwaitFixEnabled(true)
      .setWeakRefsEnabled(true);
      .setWeakRefsEnabled(true)
      .setSharedMemoryAndAtomicsEnabled(true);
  newGlobal = JS_NewGlobalObject(cx, getGlobalClass(), principals,
                                 JS::FireOnNewGlobalHook, options);
  if (!newGlobal) {
+4 −0
Original line number Diff line number Diff line
@@ -390,6 +390,10 @@ JS_PUBLIC_API void JS_SetFutexCanWait(JSContext* cx) {
  cx->fx.setCanWait(true);
}

JS_PUBLIC_API bool JS_ContainsSharedArrayBuffer(JSContext* cx) {
  return cx->runtime()->hasLiveSABs();
}

JS_PUBLIC_API JSRuntime* JS_GetParentRuntime(JSContext* cx) {
  return cx->runtime()->parentRuntime ? cx->runtime()->parentRuntime
                                      : cx->runtime();
+6 −0
Original line number Diff line number Diff line
@@ -344,6 +344,12 @@ extern JS_PUBLIC_API JSRuntime* JS_GetRuntime(JSContext* cx);

extern JS_PUBLIC_API void JS_SetFutexCanWait(JSContext* cx);

// Returns true if there are any live SharedArrayBuffer objects, including those
// for wasm memories, associated with the context.  This is conservative,
// because it does not run GC.  Some dead objects may not have been collected
// yet and thus will be thought live.
extern JS_PUBLIC_API bool JS_ContainsSharedArrayBuffer(JSContext* cx);

namespace js {

void AssertHeapIsIdle();
Loading