tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testSourcePolicy.cpp (1791B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "mozilla/Utf8.h"  // mozilla::Utf8Unit
      6 
      7 #include "js/CompilationAndEvaluation.h"  // JS::CompileFunction, JS::Evaluate
      8 #include "js/EnvironmentChain.h"          // JS::EnvironmentChain
      9 #include "js/GlobalObject.h"              // JS_NewGlobalObject
     10 #include "js/MemoryFunctions.h"
     11 #include "js/SourceText.h"  // JS::Source{Ownership,Text}
     12 #include "jsapi-tests/tests.h"
     13 #include "vm/JSScript.h"
     14 
     15 BEGIN_TEST(testBug795104) {
     16  JS::RealmOptions options;
     17  options.behaviors().setDiscardSource(true);
     18 
     19  JS::RootedObject g(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr,
     20                                            JS::FireOnNewGlobalHook, options));
     21  CHECK(g);
     22 
     23  JSAutoRealm ar(cx, g);
     24 
     25  const size_t strLen = 60002;
     26  char* s = static_cast<char*>(JS_malloc(cx, strLen));
     27  CHECK(s);
     28 
     29  s[0] = '"';
     30  memset(s + 1, 'x', strLen - 2);
     31  s[strLen - 1] = '"';
     32 
     33  JS::SourceText<mozilla::Utf8Unit> srcBuf;
     34  CHECK(srcBuf.init(cx, s, strLen, JS::SourceOwnership::Borrowed));
     35 
     36  JS::CompileOptions opts(cx);
     37 
     38  // We don't want an rval for our JS::Evaluate call
     39  opts.setNoScriptRval(true);
     40 
     41  JS::RootedValue unused(cx);
     42  CHECK(JS::Evaluate(cx, opts, srcBuf, &unused));
     43 
     44  JS::RootedFunction fun(cx);
     45  JS::EnvironmentChain emptyEnvChain(cx, JS::SupportUnscopables::No);
     46 
     47  // But when compiling a function we don't want to use no-rval
     48  // mode, since it's not supported for functions.
     49  opts.setNoScriptRval(false);
     50 
     51  fun = JS::CompileFunction(cx, emptyEnvChain, opts, "f", 0, nullptr, srcBuf);
     52  CHECK(fun);
     53 
     54  JS_free(cx, s);
     55 
     56  return true;
     57 }
     58 END_TEST(testBug795104)