1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBARTBASE_BASE_BIT_MEMORY_REGION_H_
18 #define ART_LIBARTBASE_BASE_BIT_MEMORY_REGION_H_
19 
20 #include "memory_region.h"
21 
22 #include "bit_utils.h"
23 #include "memory_tool.h"
24 
25 #include <array>
26 
27 namespace art {
28 
29 // Bit memory region is a bit offset subregion of a normal memoryregion. This is useful for
30 // abstracting away the bit start offset to avoid needing passing as an argument everywhere.
31 class BitMemoryRegion final : public ValueObject {
32  public:
33   struct Less {
operatorLess34     bool operator()(const BitMemoryRegion& lhs, const BitMemoryRegion& rhs) const {
35       return Compare(lhs, rhs) < 0;
36     }
37   };
38 
39   BitMemoryRegion() = default;
BitMemoryRegion(uint8_t * data,ssize_t bit_start,size_t bit_size)40   ALWAYS_INLINE BitMemoryRegion(uint8_t* data, ssize_t bit_start, size_t bit_size) {
41     // Normalize the data pointer. Note that bit_start may be negative.
42     data_ = AlignDown(data + (bit_start >> kBitsPerByteLog2), kPageSize);
43     bit_start_ = bit_start + kBitsPerByte * (data - data_);
44     bit_size_ = bit_size;
45   }
BitMemoryRegion(MemoryRegion region)46   ALWAYS_INLINE explicit BitMemoryRegion(MemoryRegion region)
47     : BitMemoryRegion(region.begin(), /* bit_start */ 0, region.size_in_bits()) {
48   }
BitMemoryRegion(MemoryRegion region,size_t bit_offset,size_t bit_length)49   ALWAYS_INLINE BitMemoryRegion(MemoryRegion region, size_t bit_offset, size_t bit_length)
50     : BitMemoryRegion(region) {
51     *this = Subregion(bit_offset, bit_length);
52   }
53 
IsValid()54   ALWAYS_INLINE bool IsValid() const { return data_ != nullptr; }
55 
data()56   const uint8_t* data() const {
57     DCHECK_ALIGNED(bit_start_, kBitsPerByte);
58     return data_ + bit_start_ / kBitsPerByte;
59   }
60 
size_in_bits()61   size_t size_in_bits() const {
62     return bit_size_;
63   }
64 
Resize(size_t bit_size)65   void Resize(size_t bit_size) {
66     bit_size_ = bit_size;
67   }
68 
Subregion(size_t bit_offset,size_t bit_length)69   ALWAYS_INLINE BitMemoryRegion Subregion(size_t bit_offset, size_t bit_length) const {
70     DCHECK_LE(bit_offset, bit_size_);
71     DCHECK_LE(bit_length, bit_size_ - bit_offset);
72     BitMemoryRegion result = *this;
73     result.bit_start_ += bit_offset;
74     result.bit_size_ = bit_length;
75     return result;
76   }
77 
Subregion(size_t bit_offset)78   ALWAYS_INLINE BitMemoryRegion Subregion(size_t bit_offset) const {
79     DCHECK_LE(bit_offset, bit_size_);
80     BitMemoryRegion result = *this;
81     result.bit_start_ += bit_offset;
82     result.bit_size_ -= bit_offset;
83     return result;
84   }
85 
86   // Load a single bit in the region. The bit at offset 0 is the least
87   // significant bit in the first byte.
LoadBit(size_t bit_offset)88   ALWAYS_INLINE bool LoadBit(size_t bit_offset) const {
89     DCHECK_LT(bit_offset, bit_size_);
90     size_t index = (bit_start_ + bit_offset) / kBitsPerByte;
91     size_t shift = (bit_start_ + bit_offset) % kBitsPerByte;
92     return ((data_[index] >> shift) & 1) != 0;
93   }
94 
StoreBit(size_t bit_offset,bool value)95   ALWAYS_INLINE void StoreBit(size_t bit_offset, bool value) {
96     DCHECK_LT(bit_offset, bit_size_);
97     size_t index = (bit_start_ + bit_offset) / kBitsPerByte;
98     size_t shift = (bit_start_ + bit_offset) % kBitsPerByte;
99     data_[index] &= ~(1 << shift);  // Clear bit.
100     data_[index] |= (value ? 1 : 0) << shift;  // Set bit.
101     DCHECK_EQ(value, LoadBit(bit_offset));
102   }
103 
104   // Load `bit_length` bits from `data` starting at given `bit_offset`.
105   // The least significant bit is stored in the smallest memory offset.
106   template<typename Result = size_t>
107   ATTRIBUTE_NO_SANITIZE_ADDRESS  // We might touch extra bytes due to the alignment.
108   ATTRIBUTE_NO_SANITIZE_HWADDRESS  // The hwasan uses different attribute.
LoadBits(size_t bit_offset,size_t bit_length)109   ALWAYS_INLINE Result LoadBits(size_t bit_offset, size_t bit_length) const {
110     static_assert(std::is_integral<Result>::value, "Result must be integral");
111     static_assert(std::is_unsigned<Result>::value, "Result must be unsigned");
112     DCHECK(IsAligned<sizeof(Result)>(data_));
113     DCHECK_LE(bit_offset, bit_size_);
114     DCHECK_LE(bit_length, bit_size_ - bit_offset);
115     DCHECK_LE(bit_length, BitSizeOf<Result>());
116     if (bit_length == 0) {
117       return 0;
118     }
119     // Load naturally-aligned value which contains the least significant bit.
120     Result* data = reinterpret_cast<Result*>(data_);
121     size_t width = BitSizeOf<Result>();
122     size_t index = (bit_start_ + bit_offset) / width;
123     size_t shift = (bit_start_ + bit_offset) % width;
124     Result value = data[index] >> shift;
125     // Load extra value containing the most significant bit (it might be the same one).
126     // We can not just load the following value as that could potentially cause SIGSEGV.
127     Result extra = data[index + (shift + (bit_length - 1)) / width];
128     // Mask to clear unwanted bits (the 1s are needed to avoid avoid undefined shift).
129     Result clear = (std::numeric_limits<Result>::max() << 1) << (bit_length - 1);
130     // Prepend the extra value.  We add explicit '& (width - 1)' so that the shift is defined.
131     // It is a no-op for `shift != 0` and if `shift == 0` then `value == extra` because of
132     // bit_length <= width causing the `value` and `extra` to be read from the same location.
133     // The '& (width - 1)' is implied by the shift instruction on ARM and removed by compiler.
134     return (value | (extra << ((width - shift) & (width - 1)))) & ~clear;
135   }
136 
137   // Store `bit_length` bits in `data` starting at given `bit_offset`.
138   // The least significant bit is stored in the smallest memory offset.
StoreBits(size_t bit_offset,uint32_t value,size_t bit_length)139   ALWAYS_INLINE void StoreBits(size_t bit_offset, uint32_t value, size_t bit_length) {
140     DCHECK_LE(bit_offset, bit_size_);
141     DCHECK_LE(bit_length, bit_size_ - bit_offset);
142     DCHECK_LE(bit_length, BitSizeOf<uint32_t>());
143     DCHECK_LE(value, MaxInt<uint32_t>(bit_length));
144     if (bit_length == 0) {
145       return;
146     }
147     // Write data byte by byte to avoid races with other threads
148     // on bytes that do not overlap with this region.
149     uint32_t mask = std::numeric_limits<uint32_t>::max() >> (BitSizeOf<uint32_t>() - bit_length);
150     size_t index = (bit_start_ + bit_offset) / kBitsPerByte;
151     size_t shift = (bit_start_ + bit_offset) % kBitsPerByte;
152     data_[index] &= ~(mask << shift);  // Clear bits.
153     data_[index] |= (value << shift);  // Set bits.
154     size_t finished_bits = kBitsPerByte - shift;
155     for (int i = 1; finished_bits < bit_length; i++, finished_bits += kBitsPerByte) {
156       data_[index + i] &= ~(mask >> finished_bits);  // Clear bits.
157       data_[index + i] |= (value >> finished_bits);  // Set bits.
158     }
159     DCHECK_EQ(value, LoadBits(bit_offset, bit_length));
160   }
161 
162   // Store bits from other bit region.
StoreBits(size_t bit_offset,const BitMemoryRegion & src,size_t bit_length)163   ALWAYS_INLINE void StoreBits(size_t bit_offset, const BitMemoryRegion& src, size_t bit_length) {
164     DCHECK_LE(bit_offset, bit_size_);
165     DCHECK_LE(bit_length, bit_size_ - bit_offset);
166     size_t bit = 0;
167     constexpr size_t kNumBits = BitSizeOf<uint32_t>();
168     for (; bit + kNumBits <= bit_length; bit += kNumBits) {
169       StoreBits(bit_offset + bit, src.LoadBits(bit, kNumBits), kNumBits);
170     }
171     size_t num_bits = bit_length - bit;
172     StoreBits(bit_offset + bit, src.LoadBits(bit, num_bits), num_bits);
173   }
174 
175   // Count the number of set bits within the given bit range.
PopCount(size_t bit_offset,size_t bit_length)176   ALWAYS_INLINE size_t PopCount(size_t bit_offset, size_t bit_length) const {
177     DCHECK_LE(bit_offset, bit_size_);
178     DCHECK_LE(bit_length, bit_size_ - bit_offset);
179     size_t count = 0;
180     size_t bit = 0;
181     constexpr size_t kNumBits = BitSizeOf<uint32_t>();
182     for (; bit + kNumBits <= bit_length; bit += kNumBits) {
183       count += POPCOUNT(LoadBits(bit_offset + bit, kNumBits));
184     }
185     count += POPCOUNT(LoadBits(bit_offset + bit, bit_length - bit));
186     return count;
187   }
188 
Compare(const BitMemoryRegion & lhs,const BitMemoryRegion & rhs)189   static int Compare(const BitMemoryRegion& lhs, const BitMemoryRegion& rhs) {
190     if (lhs.size_in_bits() != rhs.size_in_bits()) {
191       return (lhs.size_in_bits() < rhs.size_in_bits()) ? -1 : 1;
192     }
193     size_t bit = 0;
194     constexpr size_t kNumBits = BitSizeOf<uint32_t>();
195     for (; bit + kNumBits <= lhs.size_in_bits(); bit += kNumBits) {
196       uint32_t lhs_bits = lhs.LoadBits(bit, kNumBits);
197       uint32_t rhs_bits = rhs.LoadBits(bit, kNumBits);
198       if (lhs_bits != rhs_bits) {
199         return (lhs_bits < rhs_bits) ? -1 : 1;
200       }
201     }
202     size_t num_bits = lhs.size_in_bits() - bit;
203     uint32_t lhs_bits = lhs.LoadBits(bit, num_bits);
204     uint32_t rhs_bits = rhs.LoadBits(bit, num_bits);
205     if (lhs_bits != rhs_bits) {
206       return (lhs_bits < rhs_bits) ? -1 : 1;
207     }
208     return 0;
209   }
210 
211  private:
212   uint8_t* data_ = nullptr;  // The pointer is page aligned.
213   size_t bit_start_ = 0;
214   size_t bit_size_ = 0;
215 };
216 
217 constexpr uint32_t kVarintBits = 4;  // Minimum number of bits used for varint.
218 constexpr uint32_t kVarintMax = 11;  // Maximum value which is stored "inline".
219 
220 class BitMemoryReader {
221  public:
222   BitMemoryReader(BitMemoryReader&&) = default;
BitMemoryReader(BitMemoryRegion data)223   explicit BitMemoryReader(BitMemoryRegion data)
224       : finished_region_(data.Subregion(0, 0) /* set the length to zero */ ) {
225   }
226   explicit BitMemoryReader(const uint8_t* data, ssize_t bit_offset = 0)
227       : finished_region_(const_cast<uint8_t*>(data), bit_offset, /* bit_length */ 0) {
228   }
229 
data()230   const uint8_t* data() const { return finished_region_.data(); }
231 
GetReadRegion()232   BitMemoryRegion GetReadRegion() const { return finished_region_; }
233 
NumberOfReadBits()234   size_t NumberOfReadBits() const { return finished_region_.size_in_bits(); }
235 
ReadRegion(size_t bit_length)236   ALWAYS_INLINE BitMemoryRegion ReadRegion(size_t bit_length) {
237     size_t bit_offset = finished_region_.size_in_bits();
238     finished_region_.Resize(bit_offset + bit_length);
239     return finished_region_.Subregion(bit_offset, bit_length);
240   }
241 
242   template<typename Result = size_t>
ReadBits(size_t bit_length)243   ALWAYS_INLINE Result ReadBits(size_t bit_length) {
244     return ReadRegion(bit_length).LoadBits<Result>(/* bit_offset */ 0, bit_length);
245   }
246 
ReadBit()247   ALWAYS_INLINE bool ReadBit() {
248     return ReadRegion(/* bit_length */ 1).LoadBit(/* bit_offset */ 0);
249   }
250 
251   // Read variable-length bit-packed integer.
252   // The first four bits determine the variable length of the encoded integer:
253   //   Values 0..11 represent the result as-is, with no further following bits.
254   //   Values 12..15 mean the result is in the next 8/16/24/32-bits respectively.
ReadVarint()255   ALWAYS_INLINE uint32_t ReadVarint() {
256     uint32_t x = ReadBits(kVarintBits);
257     return (x <= kVarintMax) ? x : ReadBits((x - kVarintMax) * kBitsPerByte);
258   }
259 
260   // Read N 'interleaved' varints (different to just reading consecutive varints).
261   // All small values are stored first and the large values are stored after them.
262   // This requires fewer bit-reads compared to indidually storing the varints.
263   template<size_t N>
ReadInterleavedVarints()264   ALWAYS_INLINE std::array<uint32_t, N> ReadInterleavedVarints() {
265     static_assert(N * kVarintBits <= sizeof(uint64_t) * kBitsPerByte, "N too big");
266     std::array<uint32_t, N> values;
267     // StackMap BitTable uses over 8 varints in the header, so we need uint64_t.
268     uint64_t data = ReadBits<uint64_t>(N * kVarintBits);
269     for (size_t i = 0; i < N; i++) {
270       values[i] = BitFieldExtract(data, i * kVarintBits, kVarintBits);
271     }
272     // Do the second part in its own loop as that seems to produce better code in clang.
273     for (size_t i = 0; i < N; i++) {
274       if (UNLIKELY(values[i] > kVarintMax)) {
275         values[i] = ReadBits((values[i] - kVarintMax) * kBitsPerByte);
276       }
277     }
278     return values;
279   }
280 
281  private:
282   // Represents all of the bits which were read so far. There is no upper bound.
283   // Therefore, by definition, the "cursor" is always at the end of the region.
284   BitMemoryRegion finished_region_;
285 
286   DISALLOW_COPY_AND_ASSIGN(BitMemoryReader);
287 };
288 
289 template<typename Vector>
290 class BitMemoryWriter {
291  public:
292   explicit BitMemoryWriter(Vector* out, size_t bit_offset = 0)
out_(out)293       : out_(out), bit_start_(bit_offset), bit_offset_(bit_offset) {
294     DCHECK_EQ(NumberOfWrittenBits(), 0u);
295   }
296 
GetWrittenRegion()297   BitMemoryRegion GetWrittenRegion() const {
298     return BitMemoryRegion(out_->data(), bit_start_, bit_offset_ - bit_start_);
299   }
300 
data()301   const uint8_t* data() const { return out_->data(); }
302 
NumberOfWrittenBits()303   size_t NumberOfWrittenBits() const { return bit_offset_ - bit_start_; }
304 
Allocate(size_t bit_length)305   ALWAYS_INLINE BitMemoryRegion Allocate(size_t bit_length) {
306     out_->resize(BitsToBytesRoundUp(bit_offset_ + bit_length));
307     BitMemoryRegion region(out_->data(), bit_offset_, bit_length);
308     DCHECK_LE(bit_length, std::numeric_limits<size_t>::max() - bit_offset_) << "Overflow";
309     bit_offset_ += bit_length;
310     return region;
311   }
312 
WriteRegion(const BitMemoryRegion & region)313   ALWAYS_INLINE void WriteRegion(const BitMemoryRegion& region) {
314     Allocate(region.size_in_bits()).StoreBits(/* bit_offset */ 0, region, region.size_in_bits());
315   }
316 
WriteBits(uint32_t value,size_t bit_length)317   ALWAYS_INLINE void WriteBits(uint32_t value, size_t bit_length) {
318     Allocate(bit_length).StoreBits(/* bit_offset */ 0, value, bit_length);
319   }
320 
WriteBit(bool value)321   ALWAYS_INLINE void WriteBit(bool value) {
322     Allocate(1).StoreBit(/* bit_offset */ 0, value);
323   }
324 
325   template<size_t N>
WriteInterleavedVarints(std::array<uint32_t,N> values)326   ALWAYS_INLINE void WriteInterleavedVarints(std::array<uint32_t, N> values) {
327     // Write small values (or the number of bytes needed for the large values).
328     for (uint32_t value : values) {
329       if (value > kVarintMax) {
330         WriteBits(kVarintMax + BitsToBytesRoundUp(MinimumBitsToStore(value)), kVarintBits);
331       } else {
332         WriteBits(value, kVarintBits);
333       }
334     }
335     // Write large values.
336     for (uint32_t value : values) {
337       if (value > kVarintMax) {
338         WriteBits(value, BitsToBytesRoundUp(MinimumBitsToStore(value)) * kBitsPerByte);
339       }
340     }
341   }
342 
WriteVarint(uint32_t value)343   ALWAYS_INLINE void WriteVarint(uint32_t value) {
344     WriteInterleavedVarints<1>({value});
345   }
346 
ByteAlign()347   ALWAYS_INLINE void ByteAlign() {
348     size_t end = bit_start_ + bit_offset_;
349     bit_offset_ += RoundUp(end, kBitsPerByte) - end;
350   }
351 
352  private:
353   Vector* out_;
354   size_t bit_start_;
355   size_t bit_offset_;
356 
357   DISALLOW_COPY_AND_ASSIGN(BitMemoryWriter);
358 };
359 
360 }  // namespace art
361 
362 #endif  // ART_LIBARTBASE_BASE_BIT_MEMORY_REGION_H_
363