summaryrefslogtreecommitdiffstats
path: root/dom/script/ScriptCompression.cpp
blob: 7aa8f08de251005acdc587b55a326bc87216bd98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- 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 "zlib.h"
#include "ScriptLoadRequest.h"
#include "ScriptLoader.h"
#include "mozilla/PerfStats.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/Vector.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_browser.h"

using namespace mozilla;

namespace JS::loader {

#undef LOG
#define LOG(args)                                       \
  MOZ_LOG(mozilla::dom::ScriptLoader::gScriptLoaderLog, \
          mozilla::LogLevel::Debug, args)

/*
 * ScriptBytecodeDataLayout
 *
 * ScriptBytecodeDataLayout provides accessors to maintain the correct data
 * layout of the ScriptLoadRequest's script bytecode buffer.
 */
class ScriptBytecodeDataLayout {
 public:
  explicit ScriptBytecodeDataLayout(mozilla::Vector<uint8_t>& aBytecode,
                                    size_t aBytecodeOffset)
      : mBytecode(aBytecode), mBytecodeOffset(aBytecodeOffset) {}

  uint8_t* prelude() const { return mBytecode.begin(); }
  size_t preludeLength() const { return mBytecodeOffset; }

  uint8_t* bytecode() const { return prelude() + mBytecodeOffset; }
  size_t bytecodeLength() const { return mBytecode.length() - preludeLength(); }

  mozilla::Vector<uint8_t>& mBytecode;
  size_t mBytecodeOffset;
};

/*
 * ScriptBytecodeCompressedDataLayout
 *
 * ScriptBytecodeCompressedDataLayout provides accessors to maintain the correct
 * data layout of a compressed script bytecode buffer.
 */
class ScriptBytecodeCompressedDataLayout {
 public:
  using UncompressedLengthType = uint32_t;

  explicit ScriptBytecodeCompressedDataLayout(
      mozilla::Vector<uint8_t>& aBytecode, size_t aBytecodeOffset)
      : mBytecode(aBytecode), mBytecodeOffset(aBytecodeOffset) {}

  uint8_t* prelude() const { return mBytecode.begin(); }
  size_t preludeLength() const { return mBytecodeOffset; }

  uint8_t* uncompressedLength() const { return prelude() + mBytecodeOffset; }
  size_t uncompressedLengthLength() const {
    return sizeof(UncompressedLengthType);
  }

  uint8_t* bytecode() const {
    return uncompressedLength() + uncompressedLengthLength();
  }
  size_t bytecodeLength() const {
    return mBytecode.length() - uncompressedLengthLength() - preludeLength();
  }

  mozilla::Vector<uint8_t>& mBytecode;
  size_t mBytecodeOffset;
};

bool ScriptBytecodeCompress(Vector<uint8_t>& aBytecodeBuf,
                            size_t aBytecodeOffset,
                            Vector<uint8_t>& aCompressedBytecodeBufOut) {
  // TODO probably need to move this to a helper thread

  AUTO_PROFILER_MARKER_TEXT("ScriptBytecodeCompress", JS, {}, ""_ns);
  PerfStats::AutoMetricRecording<PerfStats::Metric::JSBC_Compression>
      autoRecording;

  ScriptBytecodeDataLayout uncompressedLayout(aBytecodeBuf, aBytecodeOffset);
  ScriptBytecodeCompressedDataLayout compressedLayout(
      aCompressedBytecodeBufOut, uncompressedLayout.preludeLength());
  ScriptBytecodeCompressedDataLayout::UncompressedLengthType
      uncompressedLength = uncompressedLayout.bytecodeLength();
  z_stream zstream{.next_in = uncompressedLayout.bytecode(),
                   .avail_in = uncompressedLength};
  auto compressedLength = deflateBound(&zstream, uncompressedLength);
  if (!aCompressedBytecodeBufOut.resizeUninitialized(
          compressedLength + compressedLayout.preludeLength() +
          compressedLayout.uncompressedLengthLength())) {
    return false;
  }
  memcpy(compressedLayout.prelude(), uncompressedLayout.prelude(),
         uncompressedLayout.preludeLength());
  memcpy(compressedLayout.uncompressedLength(), &uncompressedLength,
         sizeof(uncompressedLength));
  zstream.next_out = compressedLayout.bytecode();
  zstream.avail_out = compressedLength;

  const uint32_t compressionLevel =
      StaticPrefs::browser_cache_jsbc_compression_level();
  if (deflateInit(&zstream, compressionLevel) != Z_OK) {
    LOG(
        ("ScriptLoadRequest: Unable to initialize bytecode cache "
         "compression."));
    return false;
  }
  auto autoDestroy = MakeScopeExit([&]() { deflateEnd(&zstream); });

  int ret = deflate(&zstream, Z_FINISH);
  if (ret == Z_MEM_ERROR) {
    return false;
  }
  MOZ_RELEASE_ASSERT(ret == Z_STREAM_END);

  aCompressedBytecodeBufOut.shrinkTo(zstream.next_out -
                                     aCompressedBytecodeBufOut.begin());
  return true;
}

bool ScriptBytecodeDecompress(Vector<uint8_t>& aCompressedBytecodeBuf,
                              size_t aBytecodeOffset,
                              Vector<uint8_t>& aBytecodeBufOut) {
  AUTO_PROFILER_MARKER_TEXT("ScriptBytecodeDecompress", JS, {}, ""_ns);
  PerfStats::AutoMetricRecording<PerfStats::Metric::JSBC_Decompression>
      autoRecording;

  ScriptBytecodeDataLayout uncompressedLayout(aBytecodeBufOut, aBytecodeOffset);
  ScriptBytecodeCompressedDataLayout compressedLayout(
      aCompressedBytecodeBuf, uncompressedLayout.preludeLength());
  ScriptBytecodeCompressedDataLayout::UncompressedLengthType uncompressedLength;
  memcpy(&uncompressedLength, compressedLayout.uncompressedLength(),
         compressedLayout.uncompressedLengthLength());
  if (!aBytecodeBufOut.resizeUninitialized(uncompressedLayout.preludeLength() +
                                           uncompressedLength)) {
    return false;
  }
  memcpy(uncompressedLayout.prelude(), compressedLayout.prelude(),
         compressedLayout.preludeLength());

  z_stream zstream{nullptr};
  zstream.next_in = compressedLayout.bytecode();
  zstream.avail_in = static_cast<uint32_t>(compressedLayout.bytecodeLength());
  zstream.next_out = uncompressedLayout.bytecode();
  zstream.avail_out = uncompressedLength;
  if (inflateInit(&zstream) != Z_OK) {
    LOG(("ScriptLoadRequest: inflateInit FAILED (%s)", zstream.msg));
    return false;
  }
  auto autoDestroy = MakeScopeExit([&]() { inflateEnd(&zstream); });

  int ret = inflate(&zstream, Z_NO_FLUSH);
  bool ok = (ret == Z_OK || ret == Z_STREAM_END) && zstream.avail_in == 0;
  if (!ok) {
    LOG(("ScriptLoadReques: inflate FAILED (%s)", zstream.msg));
    return false;
  }

  return true;
}

#undef LOG

}  // namespace JS::loader