tor-browser

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

ContextOptions.h (6179B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /* JavaScript API. */
      8 
      9 #ifndef js_ContextOptions_h
     10 #define js_ContextOptions_h
     11 
     12 #include "jstypes.h"  // JS_PUBLIC_API
     13 
     14 #include "js/CompileOptions.h"  // PrefableCompileOptions
     15 #include "js/WasmFeatures.h"
     16 
     17 struct JS_PUBLIC_API JSContext;
     18 
     19 namespace JS {
     20 
     21 class JS_PUBLIC_API ContextOptions {
     22 public:
     23  // clang-format off
     24  ContextOptions()
     25      : wasm_(true),
     26        wasmForTrustedPrinciples_(true),
     27        wasmBaseline_(true),
     28        wasmIon_(true),
     29        testWasmAwaitTier2_(false),
     30        disableIon_(false),
     31        disableEvalSecurityChecks_(false),
     32        disableFilenameSecurityChecks_(false),
     33        asyncStack_(true),
     34        asyncStackCaptureDebuggeeOnly_(false),
     35        throwOnDebuggeeWouldRun_(true),
     36        dumpStackOnDebuggeeWouldRun_(false),
     37        fuzzing_(false) {
     38  }
     39  // clang-format on
     40 
     41  bool asmJS() const {
     42    return compileOptions_.asmJSOption() == AsmJSOption::Enabled;
     43  }
     44  AsmJSOption asmJSOption() const { return compileOptions_.asmJSOption(); }
     45  ContextOptions& setAsmJS(bool flag) {
     46    compileOptions_.setAsmJS(flag);
     47    return *this;
     48  }
     49  ContextOptions& setAsmJSOption(AsmJSOption option) {
     50    compileOptions_.setAsmJSOption(option);
     51    return *this;
     52  }
     53 
     54  bool wasm() const { return wasm_; }
     55  ContextOptions& setWasm(bool flag) {
     56    wasm_ = flag;
     57    return *this;
     58  }
     59  ContextOptions& toggleWasm() {
     60    wasm_ = !wasm_;
     61    return *this;
     62  }
     63 
     64  bool wasmForTrustedPrinciples() const { return wasmForTrustedPrinciples_; }
     65  ContextOptions& setWasmForTrustedPrinciples(bool flag) {
     66    wasmForTrustedPrinciples_ = flag;
     67    return *this;
     68  }
     69 
     70  bool wasmBaseline() const { return wasmBaseline_; }
     71  ContextOptions& setWasmBaseline(bool flag) {
     72    wasmBaseline_ = flag;
     73    return *this;
     74  }
     75 
     76  bool wasmIon() const { return wasmIon_; }
     77  ContextOptions& setWasmIon(bool flag) {
     78    wasmIon_ = flag;
     79    return *this;
     80  }
     81 
     82  bool testWasmAwaitTier2() const { return testWasmAwaitTier2_; }
     83  ContextOptions& setTestWasmAwaitTier2(bool flag) {
     84    testWasmAwaitTier2_ = flag;
     85    return *this;
     86  }
     87 
     88  bool throwOnAsmJSValidationFailure() const {
     89    return compileOptions_.throwOnAsmJSValidationFailure();
     90  }
     91  ContextOptions& setThrowOnAsmJSValidationFailure(bool flag) {
     92    compileOptions_.setThrowOnAsmJSValidationFailure(flag);
     93    return *this;
     94  }
     95  ContextOptions& toggleThrowOnAsmJSValidationFailure() {
     96    compileOptions_.toggleThrowOnAsmJSValidationFailure();
     97    return *this;
     98  }
     99 
    100  // Override to allow disabling Ion for this context irrespective of the
    101  // process-wide Ion-enabled setting. This must be set right after creating
    102  // the context.
    103  bool disableIon() const { return disableIon_; }
    104  ContextOptions& setDisableIon() {
    105    disableIon_ = true;
    106    return *this;
    107  }
    108 
    109  // These next two checks are needed for PAC Scripts: we cannot enforce
    110  // restrictions on them because they are user provided.
    111 
    112  // Override to allow disabling the eval restriction security checks for
    113  // this context.
    114  bool disableEvalSecurityChecks() const { return disableEvalSecurityChecks_; }
    115  ContextOptions& setDisableEvalSecurityChecks() {
    116    disableEvalSecurityChecks_ = true;
    117    return *this;
    118  }
    119 
    120  // Override to allow disabling the filename security checks (checks that
    121  // ensure the script doesn't come from the web) for this context. There is a
    122  // lower-level flag for this same check in CompileOptions which will be set by
    123  // this flag; this is needed here to propagate the flag down into eval
    124  // contexts
    125  bool disableFilenameSecurityChecks() const {
    126    return disableFilenameSecurityChecks_;
    127  }
    128  ContextOptions& setDisableFilenameSecurityChecks() {
    129    disableFilenameSecurityChecks_ = true;
    130    return *this;
    131  }
    132 
    133  bool asyncStack() const { return asyncStack_; }
    134  ContextOptions& setAsyncStack(bool flag) {
    135    asyncStack_ = flag;
    136    return *this;
    137  }
    138 
    139  bool asyncStackCaptureDebuggeeOnly() const {
    140    return asyncStackCaptureDebuggeeOnly_;
    141  }
    142  ContextOptions& setAsyncStackCaptureDebuggeeOnly(bool flag) {
    143    asyncStackCaptureDebuggeeOnly_ = flag;
    144    return *this;
    145  }
    146 
    147  // Enable/disable support for parsing '//(#@) source(Mapping)?URL=' pragmas.
    148  bool sourcePragmas() const { return compileOptions_.sourcePragmas(); }
    149  ContextOptions& setSourcePragmas(bool flag) {
    150    compileOptions_.setSourcePragmas(flag);
    151    return *this;
    152  }
    153 
    154  bool throwOnDebuggeeWouldRun() const { return throwOnDebuggeeWouldRun_; }
    155  ContextOptions& setThrowOnDebuggeeWouldRun(bool flag) {
    156    throwOnDebuggeeWouldRun_ = flag;
    157    return *this;
    158  }
    159 
    160  bool dumpStackOnDebuggeeWouldRun() const {
    161    return dumpStackOnDebuggeeWouldRun_;
    162  }
    163  ContextOptions& setDumpStackOnDebuggeeWouldRun(bool flag) {
    164    dumpStackOnDebuggeeWouldRun_ = flag;
    165    return *this;
    166  }
    167 
    168  bool fuzzing() const { return fuzzing_; }
    169  // Defined out-of-line because it depends on a compile-time option
    170  ContextOptions& setFuzzing(bool flag);
    171 
    172  void disableOptionsForSafeMode() {
    173    setAsmJSOption(AsmJSOption::DisabledByAsmJSPref);
    174    setWasmBaseline(false);
    175  }
    176 
    177  PrefableCompileOptions& compileOptions() { return compileOptions_; }
    178  const PrefableCompileOptions& compileOptions() const {
    179    return compileOptions_;
    180  }
    181 
    182 private:
    183  // WASM options.
    184  bool wasm_ : 1;
    185  bool wasmForTrustedPrinciples_ : 1;
    186  bool wasmBaseline_ : 1;
    187  bool wasmIon_ : 1;
    188  bool testWasmAwaitTier2_ : 1;
    189 
    190  // JIT options.
    191  bool disableIon_ : 1;
    192 
    193  // Runtime options.
    194  bool disableEvalSecurityChecks_ : 1;
    195  bool disableFilenameSecurityChecks_ : 1;
    196  bool asyncStack_ : 1;
    197  bool asyncStackCaptureDebuggeeOnly_ : 1;
    198  bool throwOnDebuggeeWouldRun_ : 1;
    199  bool dumpStackOnDebuggeeWouldRun_ : 1;
    200  bool fuzzing_ : 1;
    201 
    202  // Compile options.
    203  PrefableCompileOptions compileOptions_;
    204 };
    205 
    206 JS_PUBLIC_API ContextOptions& ContextOptionsRef(JSContext* cx);
    207 
    208 }  // namespace JS
    209 
    210 #endif  // js_ContextOptions_h