xref: /openbsd-src/gnu/llvm/compiler-rt/lib/gwp_asan/stack_trace_compressor.h (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1*3cab2bb3Spatrick //===-- stack_trace_compressor.h --------------------------------*- C++ -*-===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick 
9*3cab2bb3Spatrick #ifndef GWP_ASAN_STACK_TRACE_COMPRESSOR_
10*3cab2bb3Spatrick #define GWP_ASAN_STACK_TRACE_COMPRESSOR_
11*3cab2bb3Spatrick 
12*3cab2bb3Spatrick #include <stddef.h>
13*3cab2bb3Spatrick #include <stdint.h>
14*3cab2bb3Spatrick 
15*3cab2bb3Spatrick // These functions implement stack frame compression and decompression. We store
16*3cab2bb3Spatrick // the zig-zag encoded pointer difference between frame[i] and frame[i - 1] as
17*3cab2bb3Spatrick // a variable-length integer. This can reduce the memory overhead of stack
18*3cab2bb3Spatrick // traces by 50%.
19*3cab2bb3Spatrick 
20*3cab2bb3Spatrick namespace gwp_asan {
21*3cab2bb3Spatrick namespace compression {
22*3cab2bb3Spatrick 
23*3cab2bb3Spatrick // For the stack trace in `Unpacked` with length `UnpackedSize`, pack it into
24*3cab2bb3Spatrick // the buffer `Packed` maximum length `PackedMaxSize`. The return value is the
25*3cab2bb3Spatrick // number of bytes that were written to the output buffer.
26*3cab2bb3Spatrick size_t pack(const uintptr_t *Unpacked, size_t UnpackedSize, uint8_t *Packed,
27*3cab2bb3Spatrick             size_t PackedMaxSize);
28*3cab2bb3Spatrick 
29*3cab2bb3Spatrick // From the packed stack trace in `Packed` of length `PackedSize`, write the
30*3cab2bb3Spatrick // unpacked stack trace of maximum length `UnpackedMaxSize` into `Unpacked`.
31*3cab2bb3Spatrick // Returns the number of full entries unpacked, or zero on error.
32*3cab2bb3Spatrick size_t unpack(const uint8_t *Packed, size_t PackedSize, uintptr_t *Unpacked,
33*3cab2bb3Spatrick               size_t UnpackedMaxSize);
34*3cab2bb3Spatrick 
35*3cab2bb3Spatrick } // namespace compression
36*3cab2bb3Spatrick } // namespace gwp_asan
37*3cab2bb3Spatrick 
38*3cab2bb3Spatrick #endif // GWP_ASAN_STACK_TRACE_COMPRESSOR_
39