Debian Codegraph @

js/src/vm/Compression.cpp

raw
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 #include "vm/Compression.h"
8
9 #include "mozilla/DebugOnly.h"
10 #include "mozilla/MemoryChecking.h"
11 #include "mozilla/PodOperations.h"
12 #include "mozilla/ScopeExit.h"
13
14 #include "js/Utility.h"
15 #include "util/Memory.h"
16
17 using namespace js;
18
19 static void* zlib_alloc(void* cx, uInt items, uInt size) {
20 return js_calloc(items, size);
21 }
22
23 static void zlib_free(void* cx, void* addr) { js_free(addr); }
24
25 Compressor::Compressor(const unsigned char* inp, size_t inplen)
26 : inp(inp),
27 inplen(inplen),
28 initialized(false),
29 finished(false),
30 currentChunkSize(0) {
31 MOZ_ASSERT(inplen > 0, "data to compress can't be empty");
32
33 zs.opaque = nullptr;
34 zs.next_in = (Bytef*)inp;
35 zs.avail_in = 0;
36 zs.next_out = nullptr;
37 zs.avail_out = 0;
38 zs.zalloc = zlib_alloc;
39 zs.zfree = zlib_free;
40 zs.total_in = 0;
41 zs.total_out = 0;
42 zs.msg = nullptr;
43 zs.state = nullptr;
44 zs.data_type = 0;
45 zs.adler = 0;
46 zs.reserved = 0;
47
48 // Reserve space for the CompressedDataHeader.
49 outbytes = sizeof(CompressedDataHeader);
50 }
51
52 Compressor::~Compressor() {
53 if (initialized) {
54 int ret = deflateEnd(&zs);
55 if (ret != Z_OK) {
56 // If we finished early, we can get a Z_DATA_ERROR.
57 MOZ_ASSERT(ret == Z_DATA_ERROR);
58 MOZ_ASSERT(!finished);
59 }
60 }
61 }
62
63 // According to the zlib docs, the default value for windowBits is 15. Passing
64 // -15 is treated the same, but it also forces 'raw deflate' (no zlib header or
65 // trailer). Raw deflate is necessary for chunked decompression.
66 static const int WindowBits = -15;
67
68 bool Compressor::init() {
69 if (inplen >= UINT32_MAX) {
70 return false;
71 }
72 // zlib is slow and we'd rather be done compression sooner
73 // even if it means decompression is slower which penalizes
74 // Function.toString()
75 // zlib-ng/libz-rs is faster for compression, and its compression level 2,
76 // while still faster than zlib's level 1, is on par for compressed size.
77 #ifdef USE_LIBZ_RS
78 # define COMPRESSION_LEVEL 2
79 #else
80 # define COMPRESSION_LEVEL Z_BEST_SPEED
81 #endif
82 int ret = deflateInit2(&zs, COMPRESSION_LEVEL, Z_DEFLATED, WindowBits, 8,
83 Z_DEFAULT_STRATEGY);
84 if (ret != Z_OK) {
85 MOZ_ASSERT(ret == Z_MEM_ERROR);
86 return false;
87 }
88 initialized = true;
89 return true;
90 }
91
92 void Compressor::setOutput(unsigned char* out, size_t outlen) {
93 MOZ_ASSERT(outlen > outbytes);
94 zs.next_out = out + outbytes;
95 zs.avail_out = outlen - outbytes;
96 }
97
98 Compressor::Status Compressor::compressMore() {
99 MOZ_ASSERT(zs.next_out);
100 uInt left = inplen - (zs.next_in - inp);
101 if (left <= MAX_INPUT_SIZE) {
102 zs.avail_in = left;
103 } else if (zs.avail_in == 0) {
104 zs.avail_in = MAX_INPUT_SIZE;
105 }
106
107 // Finish the current chunk if needed.
108 bool flush = false;
109 MOZ_ASSERT(currentChunkSize <= CHUNK_SIZE);
110 if (currentChunkSize + zs.avail_in >= CHUNK_SIZE) {
111 // Adjust avail_in, so we don't get chunks that are larger than
112 // CHUNK_SIZE.
113 zs.avail_in = CHUNK_SIZE - currentChunkSize;
114 MOZ_ASSERT(currentChunkSize + zs.avail_in == CHUNK_SIZE);
115 flush = true;
116 }
117
118 MOZ_ASSERT(zs.avail_in <= left);
119 bool done = zs.avail_in == left;
120
121 Bytef* oldin = zs.next_in;
122 Bytef* oldout = zs.next_out;
123 int ret = deflate(&zs, done ? Z_FINISH : (flush ? Z_FULL_FLUSH : Z_NO_FLUSH));
124 outbytes += zs.next_out - oldout;
125 currentChunkSize += zs.next_in - oldin;
126 MOZ_ASSERT(currentChunkSize <= CHUNK_SIZE);
127
128 if (ret == Z_MEM_ERROR) {
129 zs.avail_out = 0;
130 return OOM;
131 }
132 if (ret == Z_BUF_ERROR || (ret == Z_OK && zs.avail_out == 0)) {
133 // We have to resize the output buffer. Note that we're not done yet
134 // because ret != Z_STREAM_END.
135 MOZ_ASSERT(zs.avail_out == 0);
136 return MOREOUTPUT;
137 }
138
139 if (done || currentChunkSize == CHUNK_SIZE) {
140 MOZ_ASSERT_IF(!done, flush);
141 MOZ_ASSERT(chunkSize(inplen, chunkOffsets.length()) == currentChunkSize);
142 if (!chunkOffsets.append(outbytes)) {
143 return OOM;
144 }
145 currentChunkSize = 0;
146 MOZ_ASSERT_IF(done, chunkOffsets.length() == (inplen - 1) / CHUNK_SIZE + 1);
147 }
148
149 MOZ_ASSERT_IF(!done, ret == Z_OK);
150 MOZ_ASSERT_IF(done, ret == Z_STREAM_END);
151 return done ? DONE : CONTINUE;
152 }
153
154 size_t Compressor::totalBytesNeeded() const {
155 return AlignBytes(outbytes, sizeof(uint32_t)) + sizeOfChunkOffsets();
156 }
157
158 void Compressor::finish(char* dest, size_t destBytes) {
159 MOZ_ASSERT(!chunkOffsets.empty());
160
161 CompressedDataHeader* compressedHeader =
162 reinterpret_cast<CompressedDataHeader*>(dest);
163 compressedHeader->compressedBytes = outbytes;
164
165 size_t outbytesAligned = AlignBytes(outbytes, sizeof(uint32_t));
166
167 // Zero the padding bytes, the ImmutableStringsCache will hash them.
168 mozilla::PodZero(dest + outbytes, outbytesAligned - outbytes);
169
170 uint32_t* destArr = reinterpret_cast<uint32_t*>(dest + outbytesAligned);
171
172 MOZ_ASSERT(uintptr_t(dest + destBytes) ==
173 uintptr_t(destArr + chunkOffsets.length()));
174 mozilla::PodCopy(destArr, chunkOffsets.begin(), chunkOffsets.length());
175
176 finished = true;
177 }
178
179 bool js::DecompressString(const unsigned char* inp, size_t inplen,
180 unsigned char* out, size_t outlen) {
181 MOZ_ASSERT(inplen <= UINT32_MAX);
182
183 // Mark the memory we pass to zlib as initialized for MSan.
184 MOZ_MAKE_MEM_DEFINED(out, outlen);
185
186 z_stream zs;
187 zs.zalloc = zlib_alloc;
188 zs.zfree = zlib_free;
189 zs.opaque = nullptr;
190 zs.next_in = (Bytef*)inp;
191 zs.avail_in = inplen;
192 zs.next_out = out;
193 MOZ_ASSERT(outlen);
194 zs.avail_out = outlen;
195 int ret = inflateInit(&zs);
196 if (ret != Z_OK) {
197 MOZ_ASSERT(ret == Z_MEM_ERROR);
198 return false;
199 }
200 ret = inflate(&zs, Z_FINISH);
201 MOZ_ASSERT(ret == Z_STREAM_END);
202 ret = inflateEnd(&zs);
203 MOZ_ASSERT(ret == Z_OK);
204 return true;
205 }
206
207 bool js::DecompressStringChunk(const unsigned char* inp, size_t chunk,
208 unsigned char* out, size_t outlen) {
209 MOZ_ASSERT(outlen <= Compressor::CHUNK_SIZE);
210
211 const CompressedDataHeader* header =
212 reinterpret_cast<const CompressedDataHeader*>(inp);
213
214 size_t compressedBytes = header->compressedBytes;
215 size_t compressedBytesAligned = AlignBytes(compressedBytes, sizeof(uint32_t));
216
217 const unsigned char* offsetBytes = inp + compressedBytesAligned;
218 const uint32_t* offsets = reinterpret_cast<const uint32_t*>(offsetBytes);
219
220 uint32_t compressedStart =
221 chunk > 0 ? offsets[chunk - 1] : sizeof(CompressedDataHeader);
222 uint32_t compressedEnd = offsets[chunk];
223
224 MOZ_ASSERT(compressedStart < compressedEnd);
225 MOZ_ASSERT(compressedEnd <= compressedBytes);
226
227 bool lastChunk = compressedEnd == compressedBytes;
228
229 // Mark the memory we pass to zlib as initialized for MSan.
230 MOZ_MAKE_MEM_DEFINED(out, outlen);
231
232 z_stream zs;
233 zs.zalloc = zlib_alloc;
234 zs.zfree = zlib_free;
235 zs.opaque = nullptr;
236 zs.next_in = (Bytef*)(inp + compressedStart);
237 zs.avail_in = compressedEnd - compressedStart;
238 zs.next_out = out;
239 MOZ_ASSERT(outlen);
240 zs.avail_out = outlen;
241
242 // Bug 1505857 - Use 'volatile' so variable is preserved in crashdump
243 // when release-asserts below are tripped.
244 volatile int ret = inflateInit2(&zs, WindowBits);
245 if (ret != Z_OK) {
246 MOZ_ASSERT(ret == Z_MEM_ERROR);
247 return false;
248 }
249
250 auto autoCleanup = mozilla::MakeScopeExit([&] {
251 mozilla::DebugOnly<int> ret = inflateEnd(&zs);
252 MOZ_ASSERT(ret == Z_OK);
253 });
254
255 if (lastChunk) {
256 ret = inflate(&zs, Z_FINISH);
257 MOZ_RELEASE_ASSERT(ret == Z_STREAM_END);
258 } else {
259 ret = inflate(&zs, Z_NO_FLUSH);
260 if (ret == Z_MEM_ERROR) {
261 return false;
262 }
263 MOZ_RELEASE_ASSERT(ret == Z_OK);
264 }
265 MOZ_ASSERT(zs.avail_in == 0);
266 MOZ_ASSERT(zs.avail_out == 0);
267 return true;
268 }