DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/* -*- 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 "vm/Compression.h"

#include "mozilla/DebugOnly.h"
#include "mozilla/MemoryChecking.h"
#include "mozilla/PodOperations.h"
#include "mozilla/ScopeExit.h"

#include "jsutil.h"

#include "js/Utility.h"

using namespace js;

static void* zlib_alloc(void* cx, uInt items, uInt size) {
  return js_calloc(items, size);
}

static void zlib_free(void* cx, void* addr) { js_free(addr); }

Compressor::Compressor(const unsigned char* inp, size_t inplen)
    : inp(inp),
      inplen(inplen),
      initialized(false),
      finished(false),
      currentChunkSize(0),
      chunkOffsets() {
  MOZ_ASSERT(inplen > 0, "data to compress can't be empty");

  zs.opaque = nullptr;
  zs.next_in = (Bytef*)inp;
  zs.avail_in = 0;
  zs.next_out = nullptr;
  zs.avail_out = 0;
  zs.zalloc = zlib_alloc;
  zs.zfree = zlib_free;
  zs.total_in = 0;
  zs.total_out = 0;
  zs.msg = nullptr;
  zs.state = nullptr;
  zs.data_type = 0;
  zs.adler = 0;
  zs.reserved = 0;

  // Reserve space for the CompressedDataHeader.
  outbytes = sizeof(CompressedDataHeader);
}

Compressor::~Compressor() {
  if (initialized) {
    int ret = deflateEnd(&zs);
    if (ret != Z_OK) {
      // If we finished early, we can get a Z_DATA_ERROR.
      MOZ_ASSERT(ret == Z_DATA_ERROR);
      MOZ_ASSERT(!finished);
    }
  }
}

// According to the zlib docs, the default value for windowBits is 15. Passing
// -15 is treated the same, but it also forces 'raw deflate' (no zlib header or
// trailer). Raw deflate is necessary for chunked decompression.
static const int WindowBits = -15;

bool Compressor::init() {
  if (inplen >= UINT32_MAX) {
    return false;
  }
  // zlib is slow and we'd rather be done compression sooner
  // even if it means decompression is slower which penalizes
  // Function.toString()
  int ret = deflateInit2(&zs, Z_BEST_SPEED, Z_DEFLATED, WindowBits, 8,
                         Z_DEFAULT_STRATEGY);
  if (ret != Z_OK) {
    MOZ_ASSERT(ret == Z_MEM_ERROR);
    return false;
  }
  initialized = true;
  return true;
}

void Compressor::setOutput(unsigned char* out, size_t outlen) {
  MOZ_ASSERT(outlen > outbytes);
  zs.next_out = out + outbytes;
  zs.avail_out = outlen - outbytes;
}

Compressor::Status Compressor::compressMore() {
  MOZ_ASSERT(zs.next_out);
  uInt left = inplen - (zs.next_in - inp);
  if (left <= MAX_INPUT_SIZE) {
    zs.avail_in = left;
  } else if (zs.avail_in == 0) {
    zs.avail_in = MAX_INPUT_SIZE;
  }

  // Finish the current chunk if needed.
  bool flush = false;
  MOZ_ASSERT(currentChunkSize <= CHUNK_SIZE);
  if (currentChunkSize + zs.avail_in >= CHUNK_SIZE) {
    // Adjust avail_in, so we don't get chunks that are larger than
    // CHUNK_SIZE.
    zs.avail_in = CHUNK_SIZE - currentChunkSize;
    MOZ_ASSERT(currentChunkSize + zs.avail_in == CHUNK_SIZE);
    flush = true;
  }

  MOZ_ASSERT(zs.avail_in <= left);
  bool done = zs.avail_in == left;

  Bytef* oldin = zs.next_in;
  Bytef* oldout = zs.next_out;
  int ret = deflate(&zs, done ? Z_FINISH : (flush ? Z_FULL_FLUSH : Z_NO_FLUSH));
  outbytes += zs.next_out - oldout;
  currentChunkSize += zs.next_in - oldin;
  MOZ_ASSERT(currentChunkSize <= CHUNK_SIZE);

  if (ret == Z_MEM_ERROR) {
    zs.avail_out = 0;
    return OOM;
  }
  if (ret == Z_BUF_ERROR || (ret == Z_OK && zs.avail_out == 0)) {
    // We have to resize the output buffer. Note that we're not done yet
    // because ret != Z_STREAM_END.
    MOZ_ASSERT(zs.avail_out == 0);
    return MOREOUTPUT;
  }

  if (done || currentChunkSize == CHUNK_SIZE) {
    MOZ_ASSERT_IF(!done, flush);
    MOZ_ASSERT(chunkSize(inplen, chunkOffsets.length()) == currentChunkSize);
    if (!chunkOffsets.append(outbytes)) {
      return OOM;
    }
    currentChunkSize = 0;
    MOZ_ASSERT_IF(done, chunkOffsets.length() == (inplen - 1) / CHUNK_SIZE + 1);
  }

  MOZ_ASSERT_IF(!done, ret == Z_OK);
  MOZ_ASSERT_IF(done, ret == Z_STREAM_END);
  return done ? DONE : CONTINUE;
}

size_t Compressor::totalBytesNeeded() const {
  return AlignBytes(outbytes, sizeof(uint32_t)) + sizeOfChunkOffsets();
}

void Compressor::finish(char* dest, size_t destBytes) {
  MOZ_ASSERT(!chunkOffsets.empty());

  CompressedDataHeader* compressedHeader =
      reinterpret_cast<CompressedDataHeader*>(dest);
  compressedHeader->compressedBytes = outbytes;

  size_t outbytesAligned = AlignBytes(outbytes, sizeof(uint32_t));

  // Zero the padding bytes, the ImmutableStringsCache will hash them.
  mozilla::PodZero(dest + outbytes, outbytesAligned - outbytes);

  uint32_t* destArr = reinterpret_cast<uint32_t*>(dest + outbytesAligned);

  MOZ_ASSERT(uintptr_t(dest + destBytes) ==
             uintptr_t(destArr + chunkOffsets.length()));
  mozilla::PodCopy(destArr, chunkOffsets.begin(), chunkOffsets.length());

  finished = true;
}

bool js::DecompressString(const unsigned char* inp, size_t inplen,
                          unsigned char* out, size_t outlen) {
  MOZ_ASSERT(inplen <= UINT32_MAX);

  // Mark the memory we pass to zlib as initialized for MSan.
  MOZ_MAKE_MEM_DEFINED(out, outlen);

  z_stream zs;
  zs.zalloc = zlib_alloc;
  zs.zfree = zlib_free;
  zs.opaque = nullptr;
  zs.next_in = (Bytef*)inp;
  zs.avail_in = inplen;
  zs.next_out = out;
  MOZ_ASSERT(outlen);
  zs.avail_out = outlen;
  int ret = inflateInit(&zs);
  if (ret != Z_OK) {
    MOZ_ASSERT(ret == Z_MEM_ERROR);
    return false;
  }
  ret = inflate(&zs, Z_FINISH);
  MOZ_ASSERT(ret == Z_STREAM_END);
  ret = inflateEnd(&zs);
  MOZ_ASSERT(ret == Z_OK);
  return true;
}

bool js::DecompressStringChunk(const unsigned char* inp, size_t chunk,
                               unsigned char* out, size_t outlen) {
  MOZ_ASSERT(outlen <= Compressor::CHUNK_SIZE);

  const CompressedDataHeader* header =
      reinterpret_cast<const CompressedDataHeader*>(inp);

  size_t compressedBytes = header->compressedBytes;
  size_t compressedBytesAligned = AlignBytes(compressedBytes, sizeof(uint32_t));

  const unsigned char* offsetBytes = inp + compressedBytesAligned;
  const uint32_t* offsets = reinterpret_cast<const uint32_t*>(offsetBytes);

  uint32_t compressedStart =
      chunk > 0 ? offsets[chunk - 1] : sizeof(CompressedDataHeader);
  uint32_t compressedEnd = offsets[chunk];

  MOZ_ASSERT(compressedStart < compressedEnd);
  MOZ_ASSERT(compressedEnd <= compressedBytes);

  bool lastChunk = compressedEnd == compressedBytes;

  // Mark the memory we pass to zlib as initialized for MSan.
  MOZ_MAKE_MEM_DEFINED(out, outlen);

  z_stream zs;
  zs.zalloc = zlib_alloc;
  zs.zfree = zlib_free;
  zs.opaque = nullptr;
  zs.next_in = (Bytef*)(inp + compressedStart);
  zs.avail_in = compressedEnd - compressedStart;
  zs.next_out = out;
  MOZ_ASSERT(outlen);
  zs.avail_out = outlen;

  int ret = inflateInit2(&zs, WindowBits);
  if (ret != Z_OK) {
    MOZ_ASSERT(ret == Z_MEM_ERROR);
    return false;
  }

  auto autoCleanup = mozilla::MakeScopeExit([&] {
    mozilla::DebugOnly<int> ret = inflateEnd(&zs);
    MOZ_ASSERT(ret == Z_OK);
  });

  if (lastChunk) {
    ret = inflate(&zs, Z_FINISH);
    MOZ_RELEASE_ASSERT(ret == Z_STREAM_END);
  } else {
    ret = inflate(&zs, Z_NO_FLUSH);
    if (ret == Z_MEM_ERROR) {
      return false;
    }
    MOZ_RELEASE_ASSERT(ret == Z_OK);
  }
  MOZ_ASSERT(zs.avail_in == 0);
  MOZ_ASSERT(zs.avail_out == 0);
  return true;
}