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