stack_trace_compressor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===-- stack_trace_compressor.cpp ------------------------------*- C++ -*-===//
  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. #include "gwp_asan/stack_trace_compressor.h"
  9. namespace gwp_asan {
  10. namespace compression {
  11. namespace {
  12. // Encodes `Value` as a variable-length integer to `Out`. Returns zero if there
  13. // was not enough space in the output buffer to write the complete varInt.
  14. // Otherwise returns the length of the encoded integer.
  15. size_t varIntEncode(uintptr_t Value, uint8_t *Out, size_t OutLen) {
  16. for (size_t i = 0; i < OutLen; ++i) {
  17. Out[i] = Value & 0x7f;
  18. Value >>= 7;
  19. if (!Value)
  20. return i + 1;
  21. Out[i] |= 0x80;
  22. }
  23. return 0;
  24. }
  25. // Decodes a variable-length integer to `Out`. Returns zero if the integer was
  26. // too large to be represented in a uintptr_t, or if the input buffer finished
  27. // before the integer was decoded (either case meaning that the `In` does not
  28. // point to a valid varInt buffer). Otherwise, returns the number of bytes that
  29. // were used to store the decoded integer.
  30. size_t varIntDecode(const uint8_t *In, size_t InLen, uintptr_t *Out) {
  31. *Out = 0;
  32. uint8_t Shift = 0;
  33. for (size_t i = 0; i < InLen; ++i) {
  34. *Out |= (static_cast<uintptr_t>(In[i]) & 0x7f) << Shift;
  35. if (In[i] < 0x80)
  36. return i + 1;
  37. Shift += 7;
  38. // Disallow overflowing the range of the output integer.
  39. if (Shift >= sizeof(uintptr_t) * 8)
  40. return 0;
  41. }
  42. return 0;
  43. }
  44. uintptr_t zigzagEncode(uintptr_t Value) {
  45. uintptr_t Encoded = Value << 1;
  46. if (static_cast<intptr_t>(Value) >= 0)
  47. return Encoded;
  48. return ~Encoded;
  49. }
  50. uintptr_t zigzagDecode(uintptr_t Value) {
  51. uintptr_t Decoded = Value >> 1;
  52. if (!(Value & 1))
  53. return Decoded;
  54. return ~Decoded;
  55. }
  56. } // anonymous namespace
  57. size_t pack(const uintptr_t *Unpacked, size_t UnpackedSize, uint8_t *Packed,
  58. size_t PackedMaxSize) {
  59. size_t Index = 0;
  60. for (size_t CurrentDepth = 0; CurrentDepth < UnpackedSize; CurrentDepth++) {
  61. uintptr_t Diff = Unpacked[CurrentDepth];
  62. if (CurrentDepth > 0)
  63. Diff -= Unpacked[CurrentDepth - 1];
  64. size_t EncodedLength =
  65. varIntEncode(zigzagEncode(Diff), Packed + Index, PackedMaxSize - Index);
  66. if (!EncodedLength)
  67. break;
  68. Index += EncodedLength;
  69. }
  70. return Index;
  71. }
  72. size_t unpack(const uint8_t *Packed, size_t PackedSize, uintptr_t *Unpacked,
  73. size_t UnpackedMaxSize) {
  74. size_t CurrentDepth;
  75. size_t Index = 0;
  76. for (CurrentDepth = 0; CurrentDepth < UnpackedMaxSize; CurrentDepth++) {
  77. uintptr_t EncodedDiff;
  78. size_t DecodedLength =
  79. varIntDecode(Packed + Index, PackedSize - Index, &EncodedDiff);
  80. if (!DecodedLength)
  81. break;
  82. Index += DecodedLength;
  83. Unpacked[CurrentDepth] = zigzagDecode(EncodedDiff);
  84. if (CurrentDepth > 0)
  85. Unpacked[CurrentDepth] += Unpacked[CurrentDepth - 1];
  86. }
  87. if (Index != PackedSize && CurrentDepth != UnpackedMaxSize)
  88. return 0;
  89. return CurrentDepth;
  90. }
  91. } // namespace compression
  92. } // namespace gwp_asan