LLVM 24.0.0git
Compression.cpp
Go to the documentation of this file.
1//===--- Compression.cpp - Compression implementation ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements compression functions.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Config/config.h"
19#include "llvm/Support/Error.h"
21#include <limits>
22#include <optional>
23#if LLVM_ENABLE_ZLIB
24#include <zlib.h>
25#endif
26#if LLVM_ENABLE_ZSTD
27#include <zstd.h>
28#endif
29#if LLVM_ENABLE_LZMA
30#include <lzma.h>
31#endif
32
33using namespace llvm;
34using namespace llvm::compression;
35
36// RFC 1950 section 2.2 zlib wrapper. Two-byte header CMF then FLG:
37// CMF: CM (bits 0-3) must be 8 (deflate). CINFO (bits 4-7) is
38// log2(windowSize)-8 and must be <= 7.
39// FLG: FCHECK (bits 0-4) is chosen so CMF*256+FLG is a multiple of 31;
40// FDICT (bit 5) marks a preset dictionary; FLEVEL (bits 6-7) is a
41// compressor hint. This only identifies the wrapper.
43 if (Input.size() < 2)
44 return false;
45 unsigned CMF = Input[0];
46 unsigned FLG = Input[1];
47 if ((CMF & 0x0f) != 8 || (CMF >> 4) > 7)
48 return false;
49 return (CMF * 256 + FLG) % 31 == 0;
50}
51
52// RFC 8878 section 3.1.1: Zstandard frame magic 0xFD2FB528, little-endian.
54 static constexpr uint8_t Magic[] = {0x28, 0xb5, 0x2f, 0xfd};
55 return Input.take_front(4) == ArrayRef(Magic);
56}
57
58// Check zstd first: 0x28 is a valid zlib CMF (CINFO=2, 1KiB window).
59static std::optional<Format> identifyFormat(ArrayRef<uint8_t> Input) {
60 if (isZstdMagic(Input))
61 return Format::Zstd;
63 return Format::Zlib;
64 return std::nullopt;
65}
66
68 switch (F) {
71 return nullptr;
72 return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
73 "build time";
76 return nullptr;
77 return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
78 "build time";
79 }
81}
82
84 if (std::optional<Format> F = identifyFormat(Input))
86 return "unknown compression format";
87}
88
91 switch (P.format) {
93 zlib::compress(Input, Output, P.level);
94 break;
96 zstd::compress(Input, Output, P.level, P.zstdEnableLdm);
97 break;
98 }
99}
100
102 uint8_t *Output, size_t UncompressedSize) {
103 switch (formatFor(T)) {
105 return zlib::decompress(Input, Output, UncompressedSize);
107 return zstd::decompress(Input, Output, UncompressedSize);
108 }
110}
111
114 size_t UncompressedSize) {
115 switch (F) {
117 return zlib::decompress(Input, Output, UncompressedSize);
119 return zstd::decompress(Input, Output, UncompressedSize);
120 }
122}
123
126 size_t UncompressedSize) {
127 return decompress(formatFor(T), Input, Output, UncompressedSize);
128}
129
132 size_t UncompressedSize) {
133 std::optional<Format> F = identifyFormat(Input);
134 if (const char *Reason =
135 F ? getReasonIfUnsupported(*F) : "unknown compression format")
136 return createStringError(Reason);
137 return decompress(*F, Input, Output, UncompressedSize);
138}
139
140#if LLVM_ENABLE_ZLIB
141
142static StringRef convertZlibCodeToString(int Code) {
143 switch (Code) {
144 case Z_MEM_ERROR:
145 return "zlib error: Z_MEM_ERROR";
146 case Z_BUF_ERROR:
147 return "zlib error: Z_BUF_ERROR";
148 case Z_STREAM_ERROR:
149 return "zlib error: Z_STREAM_ERROR";
150 case Z_DATA_ERROR:
151 return "zlib error: Z_DATA_ERROR";
152 case Z_OK:
153 default:
154 llvm_unreachable("unknown or unexpected zlib status code");
155 }
156}
157
158bool zlib::isAvailable() { return true; }
159
161 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
162 unsigned long CompressedSize = ::compressBound(Input.size());
163 CompressedBuffer.resize_for_overwrite(CompressedSize);
164 int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
165 (const Bytef *)Input.data(), Input.size(), Level);
166 if (Res == Z_MEM_ERROR)
167 report_bad_alloc_error("Allocation failed");
168 assert(Res == Z_OK);
169 // Tell MemorySanitizer that zlib output buffer is fully initialized.
170 // This avoids a false report when running LLVM with uninstrumented ZLib.
171 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
172 if (CompressedSize < CompressedBuffer.size())
173 CompressedBuffer.truncate(CompressedSize);
174}
175
177 size_t &UncompressedSize) {
178 int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
179 (const Bytef *)Input.data(), Input.size());
180 // Tell MemorySanitizer that zlib output buffer is fully initialized.
181 // This avoids a false report when running LLVM with uninstrumented ZLib.
182 __msan_unpoison(Output, UncompressedSize);
183 return Res ? make_error<StringError>(convertZlibCodeToString(Res),
185 : Error::success();
186}
187
190 size_t UncompressedSize) {
191 Output.resize_for_overwrite(UncompressedSize);
192 Error E = zlib::decompress(Input, Output.data(), UncompressedSize);
193 if (UncompressedSize < Output.size())
194 Output.truncate(UncompressedSize);
195 return E;
196}
197
198#else
199bool zlib::isAvailable() { return false; }
201 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
202 llvm_unreachable("zlib::compress is unavailable");
203}
205 size_t &UncompressedSize) {
206 llvm_unreachable("zlib::decompress is unavailable");
207}
209 SmallVectorImpl<uint8_t> &UncompressedBuffer,
210 size_t UncompressedSize) {
211 llvm_unreachable("zlib::decompress is unavailable");
212}
213#endif
214
215#if LLVM_ENABLE_ZSTD
216
217bool zstd::isAvailable() { return true; }
218
219#include <zstd.h> // Ensure ZSTD library is included
220
222 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level,
223 bool EnableLdm) {
224 ZSTD_CCtx *Cctx = ZSTD_createCCtx();
225 if (!Cctx)
226 report_bad_alloc_error("Failed to create ZSTD_CCtx");
227
228 if (ZSTD_isError(ZSTD_CCtx_setParameter(
229 Cctx, ZSTD_c_enableLongDistanceMatching, EnableLdm ? 1 : 0))) {
230 ZSTD_freeCCtx(Cctx);
231 report_bad_alloc_error("Failed to set ZSTD_c_enableLongDistanceMatching");
232 }
233
234 if (ZSTD_isError(
235 ZSTD_CCtx_setParameter(Cctx, ZSTD_c_compressionLevel, Level))) {
236 ZSTD_freeCCtx(Cctx);
237 report_bad_alloc_error("Failed to set ZSTD_c_compressionLevel");
238 }
239
240 unsigned long CompressedBufferSize = ZSTD_compressBound(Input.size());
241 CompressedBuffer.resize_for_overwrite(CompressedBufferSize);
242
243 size_t const CompressedSize =
244 ZSTD_compress2(Cctx, CompressedBuffer.data(), CompressedBufferSize,
245 Input.data(), Input.size());
246
247 ZSTD_freeCCtx(Cctx);
248
249 if (ZSTD_isError(CompressedSize))
250 report_bad_alloc_error("Compression failed");
251
252 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
253 if (CompressedSize < CompressedBuffer.size())
254 CompressedBuffer.truncate(CompressedSize);
255}
256
258 size_t &UncompressedSize) {
259 const size_t Res = ::ZSTD_decompress(
260 Output, UncompressedSize, (const uint8_t *)Input.data(), Input.size());
261 UncompressedSize = Res;
262 if (ZSTD_isError(Res))
263 return make_error<StringError>(ZSTD_getErrorName(Res),
265 // Tell MemorySanitizer that zstd output buffer is fully initialized.
266 // This avoids a false report when running LLVM with uninstrumented ZLib.
267 __msan_unpoison(Output, UncompressedSize);
268 return Error::success();
269}
270
273 size_t UncompressedSize) {
274 Output.resize_for_overwrite(UncompressedSize);
275 Error E = zstd::decompress(Input, Output.data(), UncompressedSize);
276 if (UncompressedSize < Output.size())
277 Output.truncate(UncompressedSize);
278 return E;
279}
280
281#else
282bool zstd::isAvailable() { return false; }
284 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level,
285 bool EnableLdm) {
286 llvm_unreachable("zstd::compress is unavailable");
287}
289 size_t &UncompressedSize) {
290 llvm_unreachable("zstd::decompress is unavailable");
291}
294 size_t UncompressedSize) {
295 llvm_unreachable("zstd::decompress is unavailable");
296}
297#endif
298
299#if LLVM_ENABLE_LZMA
300
301bool xz::isAvailable() { return true; }
302
303// Returns a C string rather than a StringRef because every caller feeds the
304// result to a printf-style "%s", which requires NUL termination.
305static const char *convertLZMACodeToString(lzma_ret Code) {
306 switch (Code) {
307 case LZMA_STREAM_END:
308 return "lzma error: LZMA_STREAM_END";
309 case LZMA_NO_CHECK:
310 return "lzma error: LZMA_NO_CHECK";
311 case LZMA_UNSUPPORTED_CHECK:
312 return "lzma error: LZMA_UNSUPPORTED_CHECK";
313 case LZMA_GET_CHECK:
314 return "lzma error: LZMA_GET_CHECK";
315 case LZMA_MEM_ERROR:
316 return "lzma error: LZMA_MEM_ERROR";
317 case LZMA_MEMLIMIT_ERROR:
318 return "lzma error: LZMA_MEMLIMIT_ERROR";
319 case LZMA_FORMAT_ERROR:
320 return "lzma error: LZMA_FORMAT_ERROR";
321 case LZMA_OPTIONS_ERROR:
322 return "lzma error: LZMA_OPTIONS_ERROR";
323 case LZMA_DATA_ERROR:
324 return "lzma error: LZMA_DATA_ERROR";
325 case LZMA_BUF_ERROR:
326 return "lzma error: LZMA_BUF_ERROR";
327 case LZMA_PROG_ERROR:
328 return "lzma error: LZMA_PROG_ERROR";
329 default:
330 llvm_unreachable("unknown or unexpected lzma status code");
331 }
332}
333
334/// Read the uncompressed size recorded in the xz stream's index.
335static Expected<uint64_t> getUncompressedSize(ArrayRef<uint8_t> Input) {
336 if (Input.size() < LZMA_STREAM_HEADER_SIZE)
337 return createStringError(
338 "size of xz-compressed blob (%zu bytes) is smaller than the "
339 "LZMA_STREAM_HEADER_SIZE (%zu bytes)",
340 Input.size(), size_t(LZMA_STREAM_HEADER_SIZE));
341
342 // Decode the xz footer.
343 lzma_stream_flags FooterFlags{};
344 lzma_ret Ret = lzma_stream_footer_decode(
345 &FooterFlags, Input.take_back(LZMA_STREAM_HEADER_SIZE).data());
346 if (Ret != LZMA_OK)
347 return createStringError("lzma_stream_footer_decode()=%s",
348 convertLZMACodeToString(Ret));
349
350 // A stream is the header, block data, index and stream footer
351 uint64_t MinSize = FooterFlags.backward_size + 2 * LZMA_STREAM_HEADER_SIZE;
352 if (Input.size() < MinSize)
353 return createStringError(
354 "xz-compressed buffer size (%zu bytes) too small (required at "
355 "least %" PRIu64 " bytes)",
356 Input.size(), MinSize);
357
358 // Decode xz index.
359 // liblzma stores null on failure, and lzma_index_end() ignores null.
360 lzma_index *Index = nullptr;
361 llvm::scope_exit FreeIndex([&] { lzma_index_end(Index, nullptr); });
362 uint64_t MemLimit = UINT64_MAX;
363 size_t InPos = 0;
364 Ret = lzma_index_buffer_decode(
365 &Index, &MemLimit, nullptr,
366 Input.take_back(LZMA_STREAM_HEADER_SIZE + FooterFlags.backward_size)
367 .data(),
368 &InPos, Input.size());
369 if (Ret != LZMA_OK)
370 return createStringError("lzma_index_buffer_decode()=%s",
371 convertLZMACodeToString(Ret));
372
373 return lzma_index_uncompressed_size(Index);
374}
375
377 SmallVectorImpl<uint8_t> &Output) {
378 // Hand back nothing unless the whole stream decodes.
379 Output.clear();
380
381 Expected<uint64_t> UncompressedSize = getUncompressedSize(Input);
382 if (!UncompressedSize)
383 return UncompressedSize.takeError();
384
385 if (*UncompressedSize > std::numeric_limits<size_t>::max())
386 return createStringError("xz uncompressed size (%" PRIu64
387 " bytes) exceeds addressable memory",
388 *UncompressedSize);
389
390 // Concatenated streams are unsupported: liblzma decodes only the first and
391 // still reports LZMA_OK, leaving the rest of Output zero-filled.
392 Output.resize(static_cast<size_t>(*UncompressedSize));
393 uint64_t MemLimit = UINT64_MAX;
394 size_t InPos = 0;
395 size_t OutPos = 0;
396 lzma_ret Ret = lzma_stream_buffer_decode(
397 &MemLimit, /*flags=*/0, nullptr, Input.data(), &InPos, Input.size(),
398 Output.data(), &OutPos, Output.size());
399 if (Ret != LZMA_OK) {
400 Output.clear();
401 return createStringError("lzma_stream_buffer_decode()=%s",
402 convertLZMACodeToString(Ret));
403 }
404
405 return Error::success();
406}
407
408#else
409
410bool xz::isAvailable() { return false; }
412 SmallVectorImpl<uint8_t> &Output) {
413 llvm_unreachable("xz::decompress is unavailable");
414}
415
416#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define __msan_unpoison(p, size)
Definition Compiler.h:592
static bool isZstdMagic(ArrayRef< uint8_t > Input)
static bool isZlibHeader(ArrayRef< uint8_t > Input)
static std::optional< Format > identifyFormat(ArrayRef< uint8_t > Input)
#define F(x, y, z)
Definition MD5.cpp:54
#define T
#define P(N)
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the SmallVector class.
The Input class is used to parse a yaml document into in-memory structs and vectors.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
void truncate(size_type N)
Like resize, but requires that N is less than size().
void resize(size_type N)
pointer data()
Return a pointer to the vector's buffer, even if empty().
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI bool isAvailable()
Return true if LLVM was built with LZMA support (LLVM_ENABLE_LZMA).
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
Decompress an xz stream.
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
LLVM_ABI bool isAvailable()
LLVM_ABI Error decompress(ArrayRef< uint8_t > Input, uint8_t *Output, size_t &UncompressedSize)
LLVM_ABI bool isAvailable()
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression, bool EnableLdm=false)
LLVM_ABI const char * getReasonIfUnsupported(Format F)
LLVM_ABI Error decompress(DebugCompressionType T, ArrayRef< uint8_t > Input, uint8_t *Output, size_t UncompressedSize)
Format formatFor(DebugCompressionType Type)
LLVM_ABI void compress(Params P, ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &Output)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
DebugCompressionType
Definition Compression.h:28
ArrayRef(const T &OneElt) -> ArrayRef< T >
LogicalResult success(bool IsSuccess=true)
Utility function to generate a LogicalResult.
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.