tor-browser

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

Transcoding.h (2964B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * Structures and functions for transcoding compiled scripts and functions to
      8 * and from memory.
      9 */
     10 
     11 #ifndef js_Transcoding_h
     12 #define js_Transcoding_h
     13 
     14 #include "mozilla/Range.h"   // mozilla::Range
     15 #include "mozilla/Vector.h"  // mozilla::Vector
     16 
     17 #include <stddef.h>  // size_t
     18 #include <stdint.h>  // uint8_t, uint32_t
     19 
     20 #include "js/TypeDecls.h"
     21 
     22 // Underlying opaque type.
     23 namespace js::frontend {
     24 struct InitialStencilAndDelazifications;
     25 }  // namespace js::frontend
     26 
     27 namespace JS {
     28 
     29 using Stencil = js::frontend::InitialStencilAndDelazifications;
     30 
     31 class JS_PUBLIC_API ReadOnlyCompileOptions;
     32 
     33 using TranscodeBuffer = mozilla::Vector<uint8_t>;
     34 using TranscodeRange = mozilla::Range<const uint8_t>;
     35 
     36 struct TranscodeSource final {
     37  TranscodeSource(const TranscodeRange& range_, const char* file, uint32_t line)
     38      : range(range_), filename(file), lineno(line) {}
     39 
     40  const TranscodeRange range;
     41  const char* filename;
     42  const uint32_t lineno;
     43 };
     44 
     45 enum class TranscodeResult : uint8_t {
     46  // Successful encoding / decoding.
     47  Ok = 0,
     48 
     49  // A warning message, is set to the message out-param.
     50  Failure = 0x10,
     51  Failure_BadBuildId = Failure | 0x1,
     52  Failure_AsmJSNotSupported = Failure | 0x2,
     53  Failure_BadDecode = Failure | 0x3,
     54 
     55  // There is a pending exception on the context.
     56  Throw = 0x20
     57 };
     58 
     59 inline bool IsTranscodeFailureResult(const TranscodeResult result) {
     60  uint8_t raw_result = static_cast<uint8_t>(result);
     61  uint8_t raw_failure = static_cast<uint8_t>(TranscodeResult::Failure);
     62  TranscodeResult masked =
     63      static_cast<TranscodeResult>(raw_result & raw_failure);
     64  return masked == TranscodeResult::Failure;
     65 }
     66 
     67 static constexpr size_t BytecodeOffsetAlignment = 4;
     68 static_assert(BytecodeOffsetAlignment <= alignof(std::max_align_t),
     69              "Alignment condition requires a custom allocator.");
     70 
     71 // Align the bytecode offset for transcoding for the requirement.
     72 inline size_t AlignTranscodingBytecodeOffset(size_t offset) {
     73  size_t extra = offset % BytecodeOffsetAlignment;
     74  if (extra == 0) {
     75    return offset;
     76  }
     77  size_t padding = BytecodeOffsetAlignment - extra;
     78  return offset + padding;
     79 }
     80 
     81 inline bool IsTranscodingBytecodeOffsetAligned(size_t offset) {
     82  return offset % BytecodeOffsetAlignment == 0;
     83 }
     84 
     85 inline bool IsTranscodingBytecodeAligned(const void* offset) {
     86  return IsTranscodingBytecodeOffsetAligned(size_t(offset));
     87 }
     88 
     89 // Check if the compile options and script's flag matches.
     90 //
     91 // JS::DecodeScript* and JS::DecodeOffThreadScript internally check this.
     92 extern JS_PUBLIC_API bool CheckCompileOptionsMatch(
     93    const ReadOnlyCompileOptions& options, JSScript* script);
     94 
     95 }  // namespace JS
     96 
     97 #endif /* js_Transcoding_h */