1 // Copyright 2024 The Abseil Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ABSL_DEBUGGING_INTERNAL_BOUNDED_UTF8_LENGTH_SEQUENCE_H_ 16 #define ABSL_DEBUGGING_INTERNAL_BOUNDED_UTF8_LENGTH_SEQUENCE_H_ 17 18 #include <cstdint> 19 20 #include "absl/base/config.h" 21 #include "absl/numeric/bits.h" 22 23 namespace absl { 24 ABSL_NAMESPACE_BEGIN 25 namespace debugging_internal { 26 27 // A sequence of up to max_elements integers between 1 and 4 inclusive, whose 28 // insertion operation computes the sum of all the elements before the insertion 29 // point. This is useful in decoding Punycode, where one needs to know where in 30 // a UTF-8 byte stream the n-th code point begins. 31 // 32 // BoundedUtf8LengthSequence is async-signal-safe and suitable for use in 33 // symbolizing stack traces in a signal handler, provided max_elements is not 34 // improvidently large. For inputs of lengths accepted by the Rust demangler, 35 // up to a couple hundred code points, InsertAndReturnSumOfPredecessors should 36 // run in a few dozen clock cycles, on par with the other arithmetic required 37 // for Punycode decoding. 38 template <uint32_t max_elements> 39 class BoundedUtf8LengthSequence { 40 public: 41 // Constructs an empty sequence. 42 BoundedUtf8LengthSequence() = default; 43 44 // Inserts `utf_length` at position `index`, shifting any existing elements at 45 // or beyond `index` one position to the right. If the sequence is already 46 // full, the rightmost element is discarded. 47 // 48 // Returns the sum of the elements at positions 0 to `index - 1` inclusive. 49 // If `index` is greater than the number of elements already inserted, the 50 // excess positions in the range count 1 apiece. 51 // 52 // REQUIRES: index < max_elements and 1 <= utf8_length <= 4. InsertAndReturnSumOfPredecessors(uint32_t index,uint32_t utf8_length)53 uint32_t InsertAndReturnSumOfPredecessors( 54 uint32_t index, uint32_t utf8_length) { 55 // The caller shouldn't pass out-of-bounds inputs, but if it does happen, 56 // clamp the values and try to continue. If we're being called from a 57 // signal handler, the last thing we want to do is crash. Emitting 58 // malformed UTF-8 is a lesser evil. 59 if (index >= max_elements) index = max_elements - 1; 60 if (utf8_length == 0 || utf8_length > 4) utf8_length = 1; 61 62 const uint32_t word_index = index/32; 63 const uint32_t bit_index = 2 * (index % 32); 64 const uint64_t ones_bit = uint64_t{1} << bit_index; 65 66 // Compute the sum of predecessors. 67 // - Each value from 1 to 4 is represented by a bit field with value from 68 // 0 to 3, so the desired sum is index plus the sum of the 69 // representations actually stored. 70 // - For each bit field, a set low bit should contribute 1 to the sum, and 71 // a set high bit should contribute 2. 72 // - Another way to say the same thing is that each set bit contributes 1, 73 // and each set high bit contributes an additional 1. 74 // - So the sum we want is index + popcount(everything) + popcount(bits in 75 // odd positions). 76 const uint64_t odd_bits_mask = 0xaaaaaaaaaaaaaaaa; 77 const uint64_t lower_seminibbles_mask = ones_bit - 1; 78 const uint64_t higher_seminibbles_mask = ~lower_seminibbles_mask; 79 const uint64_t same_word_bits_below_insertion = 80 rep_[word_index] & lower_seminibbles_mask; 81 int full_popcount = absl::popcount(same_word_bits_below_insertion); 82 int odd_popcount = 83 absl::popcount(same_word_bits_below_insertion & odd_bits_mask); 84 for (uint32_t j = word_index; j > 0; --j) { 85 const uint64_t word_below_insertion = rep_[j - 1]; 86 full_popcount += absl::popcount(word_below_insertion); 87 odd_popcount += absl::popcount(word_below_insertion & odd_bits_mask); 88 } 89 const uint32_t sum_of_predecessors = 90 index + static_cast<uint32_t>(full_popcount + odd_popcount); 91 92 // Now insert utf8_length's representation, shifting successors up one 93 // place. 94 for (uint32_t j = max_elements/32 - 1; j > word_index; --j) { 95 rep_[j] = (rep_[j] << 2) | (rep_[j - 1] >> 62); 96 } 97 rep_[word_index] = 98 (rep_[word_index] & lower_seminibbles_mask) | 99 (uint64_t{utf8_length - 1} << bit_index) | 100 ((rep_[word_index] & higher_seminibbles_mask) << 2); 101 102 return sum_of_predecessors; 103 } 104 105 private: 106 // If the (32 * i + j)-th element of the represented sequence has the value k 107 // (0 <= j < 32, 1 <= k <= 4), then bits 2 * j and 2 * j + 1 of rep_[i] 108 // contain the seminibble (k - 1). 109 // 110 // In particular, the zero-initialization of rep_ makes positions not holding 111 // any inserted element count as 1 in InsertAndReturnSumOfPredecessors. 112 // 113 // Example: rep_ = {0xb1, ... the rest zeroes ...} represents the sequence 114 // (2, 1, 4, 3, ... the rest 1's ...). Constructing the sequence of Unicode 115 // code points "Àa中" = {U+00C0, U+0061, U+1F0BB, U+4E2D} (among many 116 // other examples) would yield this value of rep_. 117 static_assert(max_elements > 0 && max_elements % 32 == 0, 118 "max_elements must be a positive multiple of 32"); 119 uint64_t rep_[max_elements/32] = {}; 120 }; 121 122 } // namespace debugging_internal 123 ABSL_NAMESPACE_END 124 } // namespace absl 125 126 #endif // ABSL_DEBUGGING_INTERNAL_BOUNDED_UTF8_LENGTH_SEQUENCE_H_ 127