about summary refs log tree commit diff homepage
path: root/include/klee/KDAlloc/suballocators/sized_regions.h
blob: 7adeea45edca418d512676711e6868f4736ac4b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//===-- sized_regions.h -----------------------------------------*- C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef KDALLOC_UTIL_SIZED_REGIONS_H
#define KDALLOC_UTIL_SIZED_REGIONS_H

#include "../define.h"
#include "../location_info.h"
#include "cow_ptr.h"

#include <cassert>
#include <cstddef>
#include <limits>
#include <string>
#include <utility>

namespace klee::kdalloc::suballocators {
class SizedRegions;

class Node final {
  friend class SizedRegions;

  CoWPtr<Node> lhs;
  CoWPtr<Node> rhs;

  // std::size_t _precomputed_hash;
  char *baseAddress;
  std::size_t size;

  inline std::uintptr_t hash() const noexcept {
    // This hash function implements a very simple hash based only on the base
    // address. The hash is (only) used to break ties w.r.t. the heap property.
    // Note that multiplication with an odd number (as done here) modulo a power
    // of two (the domain of std::size_t) is a permutation, meaning that the
    // hash is guaranteed to break any ties (as tree-keys are unique in the
    // treap). The constant is chosen in such a way that the high bits of the
    // result depend each byte of input while still enabling a fast computation
    // due to the low number of 1-bits involved. It is also chosen odd to ensure
    // co-primality with powers of two.

    static_assert(
        std::numeric_limits<std::uintptr_t>::digits <= 64,
        "Please choose a more appropriate constant in the line below.");
    return reinterpret_cast<std::uintptr_t>(baseAddress) *
           static_cast<std::uintptr_t>(0x8080'8080'8080'8081u);
  }

public:
  char *const &getBaseAddress() const noexcept { return baseAddress; }
  void setBaseAddress(char *const newBaseAddress) noexcept {
    baseAddress = newBaseAddress;
  }

  std::size_t const &getSize() const noexcept { return size; }
  void setSize(std::size_t const newSize) noexcept { size = newSize; }

  Node(char *const baseAddress, std::size_t const size) noexcept
      : baseAddress(baseAddress), size(size) {}
};

class SizedRegions {
  CoWPtr<Node> root;

  static void insertRec(CoWPtr<Node> &treap, CoWPtr<Node> &&region) {
    assert(treap && "target subtreap must exist (insertion at the call "
                    "site is trivial otherwise)");
    assert(region && "region to be inserted must exist");
    assert(treap->getBaseAddress() != region->getBaseAddress() &&
           "multiple insertions of the same tree-key are not supported");

    auto &node = treap.acquire();
    if (region->getBaseAddress() < node.getBaseAddress()) {
      if (node.lhs) {
        insertRec(node.lhs, std::move(region));
      } else {
        node.lhs = std::move(region);
      }
      if (node.lhs->getSize() > node.getSize() ||
          (node.lhs->getSize() == node.getSize() &&
           node.lhs->hash() > node.hash())) {
        auto temp = std::move(node.lhs);
        auto &nodeTemp = temp.acquire();
        node.lhs = std::move(nodeTemp.rhs);
        nodeTemp.rhs = std::move(treap);
        treap = std::move(temp);
      }
    } else {
      if (node.rhs) {
        insertRec(node.rhs, std::move(region));
      } else {
        node.rhs = std::move(region);
      }
      if (node.rhs->getSize() > node.getSize() ||
          (node.rhs->getSize() == node.getSize() &&
           node.rhs->hash() > node.hash())) {
        auto temp = std::move(node.rhs);
        auto &nodeTemp = temp.acquire();
        node.rhs = std::move(nodeTemp.lhs);
        nodeTemp.lhs = std::move(treap);
        treap = std::move(temp);
      }
    }
  }

  static void mergeTreaps(CoWPtr<Node> *target, CoWPtr<Node> lhs,
                          CoWPtr<Node> rhs) {
    assert(!*target && "empty the target first");
    assert(
        (lhs || rhs) &&
        "merging two empty subtreaps should be handled by hand, as it does not "
        "require "
        "acquisition of the object holding `*target` (usually a parent node)");
    for (;;) {
      assert(!*target);
      if (!lhs) {
        *target = std::move(rhs);
        break;
      }
      if (!rhs) {
        *target = std::move(lhs);
        break;
      }
      assert(lhs->getBaseAddress() < rhs->getBaseAddress() &&
             "tree property violated");
      if (lhs->getSize() > rhs->getSize() ||
          (lhs->getSize() == rhs->getSize() && lhs->hash() > rhs->hash())) {
        // Ties w.r.t. the heap property (size) are explicitly broken using a
        // hash derived from the tree key. At the time of writing, the "hash"
        // function is a permutation (if `std::size_t` and `std::uintptr_t` are
        // of the same size), which means that hash collisions are not just
        // unlikely, but rather impossible.
        Node &lhsNode = lhs.acquire();
        *target = std::move(lhs);
        lhs = std::move(lhsNode.rhs);
        target = &lhsNode.rhs;
      } else {
        Node &rhsNode = rhs.acquire();
        *target = std::move(rhs);
        rhs = std::move(rhsNode.lhs);
        target = &rhsNode.lhs;
      }
    }
    assert(!lhs && "lhs must have been consumed");
    assert(!rhs && "rhs must have been consumed");
  }

public:
  constexpr SizedRegions() = default;
  SizedRegions(SizedRegions const &other) = default;
  SizedRegions &operator=(SizedRegions const &other) = default;
  SizedRegions(SizedRegions &&other) = default;
  SizedRegions &operator=(SizedRegions &&other) = default;

  [[nodiscard]] bool isEmpty() const noexcept { return !root; }

  /// Computes the LocationInfo. This functionality really belongs to the
  /// `LargeObjectAllocator`, as it assumes that this treap contains free
  /// regions in between allocations. It also knows that there is a redzone at
  /// the beginning and end and in between any two allocations.
  inline LocationInfo getLocationInfo(char const *const address,
                                      std::size_t const size) const noexcept {
    assert(root && "Cannot compute location_info for an empty treap");

    Node const *currentNode = &*root;
    Node const *closestPredecessor = nullptr;
    for (;;) {
      if (currentNode->getBaseAddress() <= address) {
        if (address < currentNode->getBaseAddress() + currentNode->getSize()) {
          if (address + size <=
              currentNode->getBaseAddress() + currentNode->getSize()) {
            return LocationInfo::LI_Unallocated;
          } else {
            return LocationInfo::LI_Unaligned;
          }
        } else {
          closestPredecessor = currentNode;
          if (!currentNode->rhs) {
            return {LocationInfo::LI_AllocatedOrQuarantined,
                    closestPredecessor->getBaseAddress() +
                        closestPredecessor->getSize()};
          }
          currentNode = &*currentNode->rhs;
        }
      } else {
        assert(closestPredecessor &&
               "If there is no closest predecessor, there must not be a "
               "predecessor at all. Since regions are only ever split, "
               "this would mean that the lookup is outside the valid "
               "range, which has to be handled by the caller.");
        if (currentNode->getBaseAddress() < address + size) {
          return LocationInfo::LI_Unaligned;
        }
        if (!currentNode->lhs) {
          return {LocationInfo::LI_AllocatedOrQuarantined,
                  closestPredecessor->getBaseAddress() +
                      closestPredecessor->getSize()};
        }
        currentNode = &*currentNode->lhs;
      }
    }
  }

  void insert(char *const baseAddress, std::size_t const size) {
    assert(size > 0 && "region to be inserted must contain at least one byte");
    insert(CoWPtr<Node>(CoWPtr<Node>::in_place_t{}, baseAddress, size));
  }

  void insert(CoWPtr<Node> region) {
    assert(region && "region to be inserted must exist");
    assert(region->getSize() > 0 &&
           "region to be inserted must contain at least one byte");
    if (region->lhs || region->rhs) {
      if (region.isOwned()) {
        auto &node = region.acquire();
        node.lhs.release();
        node.rhs.release();
      } else {
        // If region is not owned, then acquiring it would copy the `lhs` and
        // `rhs` members, thereby incrementing and decrementing the refcounts in
        // unrelated objects. To avoid this, we simply recreate the region.
        region = CoWPtr<Node>(CoWPtr<Node>::in_place_t{},
                              region->getBaseAddress(), region->getSize());
      }
    }
    assert(!region->lhs);
    assert(!region->rhs);

    auto *target = &root;
    while (*target && ((*target)->getSize() > region->getSize() ||
                       ((*target)->getSize() == region->getSize() &&
                        (*target)->hash() > region->hash()))) {
      auto &node = target->acquire();
      assert(node.getBaseAddress() != region->getBaseAddress() &&
             "multiple insertions of the same tree-key are not supported");
      target = region->getBaseAddress() < node.getBaseAddress() ? &node.lhs
                                                                : &node.rhs;
    }
    if (!*target) {
      *target = std::move(region);
    } else {
      insertRec(*target, std::move(region));
    }
  }

  [[nodiscard]] CoWPtr<Node> const &getLargestRegion() const noexcept {
    assert(root && "querying the largest region only makes sense if the "
                   "treap is not empty");
    return root;
  }

  CoWPtr<Node> extractLargestRegion() {
    assert(root && "cannot extract the largest region of an empty treap");

    auto result = std::move(root);
    if (result->lhs || result->rhs) {
      auto &node = result.acquire();
      mergeTreaps(&root, std::move(node.lhs), std::move(node.rhs));
    }
    return result;
  }

private:
  static void pushDownRightHeap(CoWPtr<Node> *target, CoWPtr<Node> update,
                                CoWPtr<Node> rhs, std::size_t const newSize,
                                std::uintptr_t const newHash) {
    while (rhs && (newSize < rhs->getSize() ||
                   (newSize == rhs->getSize() && newHash < rhs->hash()))) {
      // optimization opportunity: once lhs does not exist anymore, it will
      // never exist in the future
      *target = std::move(rhs);
      auto &targetNode = target->acquire();
      rhs = std::move(targetNode.lhs);
      target = &targetNode.lhs;
    }

    update.getOwned().rhs = std::move(rhs);
    *target = std::move(update);
  }

  static void pushDownLeftHeap(CoWPtr<Node> *target, CoWPtr<Node> update,
                               CoWPtr<Node> lhs, std::size_t const newSize,
                               std::uintptr_t const newHash) {
    while (lhs && (newSize < lhs->getSize() ||
                   (newSize == lhs->getSize() && newHash < lhs->hash()))) {
      // optimization opportunity: once lhs does not exist anymore, it will
      // never exist in the future
      *target = std::move(lhs);
      auto &targetNode = target->acquire();
      lhs = std::move(targetNode.rhs);
      target = &targetNode.rhs;
    }

    update.getOwned().lhs = std::move(lhs);
    *target = std::move(update);
  }

public:
  void resizeLargestRegion(std::size_t const newSize) {
    assert(root && "updating the largest region only makes sense if the "
                   "treap is not empty");

    auto update = std::move(root);
    auto &node = update.acquire();
    node.setSize(newSize);
    auto const newHash = node.hash();
    auto lhs = std::move(node.lhs);
    auto rhs = std::move(node.rhs);

    auto *target = &root;

    if (!lhs || !(newSize < lhs->getSize() ||
                  (newSize == lhs->getSize() && newHash < lhs->hash()))) {
      node.lhs = std::move(lhs);
      pushDownRightHeap(target, std::move(update), std::move(rhs), newSize,
                        newHash);
    } else if (!rhs ||
               !(newSize < rhs->getSize() ||
                 (newSize == rhs->getSize() && newHash < rhs->hash()))) {
      node.rhs = std::move(rhs);
      pushDownLeftHeap(target, std::move(update), std::move(lhs), newSize,
                       newHash);
    } else {
      for (;;) {
        assert(lhs && (newSize < lhs->getSize() ||
                       (newSize == lhs->getSize() && newHash < lhs->hash())));
        assert(rhs && (newSize < rhs->getSize() ||
                       (newSize == rhs->getSize() && newHash < rhs->hash())));

        if (lhs->getSize() < rhs->getSize() ||
            (lhs->getSize() == rhs->getSize() && lhs->hash() < rhs->hash())) {
          *target = std::move(rhs);
          auto &targetNode = target->acquire();
          rhs = std::move(targetNode.lhs);
          target = &targetNode.lhs;

          if (!rhs || !(newSize < rhs->getSize() ||
                        (newSize == rhs->getSize() && newHash < rhs->hash()))) {
            node.rhs = std::move(rhs);
            pushDownLeftHeap(target, std::move(update), std::move(lhs), newSize,
                             newHash);
            return;
          }
        } else {
          *target = std::move(lhs);
          auto &targetNode = target->acquire();
          lhs = std::move(targetNode.rhs);
          target = &targetNode.rhs;

          if (!lhs || !(newSize < lhs->getSize() ||
                        (newSize == lhs->getSize() && newHash < lhs->hash()))) {
            node.lhs = std::move(lhs);
            pushDownRightHeap(target, std::move(update), std::move(rhs),
                              newSize, newHash);
            return;
          }
        }
      }
    }
  }

  CoWPtr<Node> extractRegion(char const *const baseAddress) {
    CoWPtr<Node> *target = &root;
    for (;;) {
      assert(*target && "cannot extract region that is not part of the treap");
      if ((*target)->getBaseAddress() < baseAddress) {
        target = &target->acquire().rhs;
      } else if ((*target)->getBaseAddress() > baseAddress) {
        target = &target->acquire().lhs;
      } else {
        assert((*target)->getBaseAddress() == baseAddress);
        auto result = std::move(*target);
        if (result->lhs || result->rhs) {
          auto &node = result.acquire();
          mergeTreaps(target, std::move(node.lhs), std::move(node.rhs));
        }
        return result;
      }
    }
  }

private:
  static CoWPtr<Node> mergeRegionWithPreviousRec(CoWPtr<Node> &treap,
                                                 char *const baseAddress) {
    assert(treap && "cannot extract region that is not part of the treap");
    if (baseAddress < treap->getBaseAddress()) {
      auto &node = treap.acquire();
      if (auto greater = mergeRegionWithPreviousRec(node.lhs, baseAddress)) {
        if (node.getBaseAddress() < greater->getBaseAddress()) {
          auto newSize = static_cast<std::size_t>(greater->getBaseAddress() -
                                                  node.getBaseAddress() +
                                                  greater->getSize());
          assert(
              newSize > node.getSize() &&
              "Sizes only get greater by adding `greater - smaller` to them");
          node.setSize(newSize);
          return {};
        } else {
          return greater;
        }
      } else {
        if (node.lhs->getSize() > node.getSize() ||
            (node.lhs->getSize() == node.getSize() &&
             node.lhs->hash() > node.hash())) {
          auto temp = std::move(node.lhs);
          auto &nodeTemp = temp.getOwned();
          node.lhs = std::move(nodeTemp.rhs);
          nodeTemp.rhs = std::move(treap);
          treap = std::move(temp);
        }
        return {};
      }
    } else if (treap->getBaseAddress() < baseAddress) {
      auto &node = treap.acquire();
      if (auto greater = mergeRegionWithPreviousRec(node.rhs, baseAddress)) {
        if (node.getBaseAddress() < greater->getBaseAddress()) {
          auto newSize = static_cast<std::size_t>(greater->getBaseAddress() -
                                                  node.getBaseAddress() +
                                                  greater->getSize());
          assert(
              newSize > node.getSize() &&
              "Sizes only get greater by adding `greater - smaller` to them");
          node.setSize(newSize);
          return {};
        } else {
          return greater;
        }
      } else {
        if (node.rhs->getSize() > node.getSize() ||
            (node.rhs->getSize() == node.getSize() &&
             node.rhs->hash() > node.hash())) {
          auto temp = std::move(node.rhs);
          auto &nodeTemp = temp.getOwned();
          node.rhs = std::move(nodeTemp.lhs);
          nodeTemp.lhs = std::move(treap);
          treap = std::move(temp);
        }
        return greater;
      }
    } else {
      assert(treap->getBaseAddress() == baseAddress);
      // target is now the greater (w.r.t. the tree key) of the two regions we
      // are looking for
      if (treap->lhs) {
        CoWPtr<Node> lhs, rhs;
        if (treap.isOwned()) {
          auto &node = treap.acquire();
          lhs = std::move(node.lhs);
          rhs = std::move(node.rhs);
        } else {
          lhs = treap->lhs;
          rhs = treap->rhs;
        }
        auto const greaterBaseAddress = treap->getBaseAddress();
        auto const greaterSize = treap->getSize();

        auto *target = &lhs;
        while ((*target)->rhs) {
          target = &target->acquire().rhs;
        }
        treap = std::move(*target);
        auto &node = treap.acquire();
        *target = std::move(node.lhs);

        assert(greaterBaseAddress > node.getBaseAddress());
        auto newSize = static_cast<std::size_t>(
            greaterBaseAddress - node.getBaseAddress() + greaterSize);
        assert(newSize > node.getSize() &&
               "Sizes only get greater by adding `greater - smaller` to them");
        node.setSize(newSize);

        assert(!node.rhs && "We looked for a node that has no rhs");
        assert((!rhs || node.getBaseAddress() < rhs->getBaseAddress()) &&
               "`lesser` comes from the left subtree of `greater`, while "
               "rhs is the right subtree of `greater`");
        assert((!rhs || node.getSize() > rhs->getSize()) &&
               "The old size of `greater` was >= that of its right subtree, "
               "so the new size (of `lesser`) is strictly greater");
        node.rhs = std::move(rhs);

        assert(!node.lhs && "The lhs of this node has already been removed");
        assert((!lhs || lhs->getBaseAddress() < node.getBaseAddress()) &&
               "`lesser` is the greatest child of the `lhs` subtree");
        assert((!lhs || node.getSize() > lhs->getSize()) &&
               "The old size of `greater` was >= that of its left subtree, "
               "so the new size (of `lesser`) is strictly greater");
        node.lhs = std::move(lhs);
        return {};
      } else {
        auto greater = std::move(treap);
        if (greater->rhs) {
          if (greater.isOwned()) {
            treap = std::move(greater.acquire().rhs);
          } else {
            treap = greater->rhs;
          }
        }
        return greater; // no move to enable copy elision
      }
    }
  }

public:
  /// This function merges the region starting at the given address, with the
  /// region immediately preceding it. Both regions must exist. The resulting
  /// region has the same base address as the lesser region with its size large
  /// enough to cover the space to the end of the greater region (filling any
  /// holes in the process).
  inline void mergeRegionWithPrevious(char *const baseAddress) {
    assert(root && "An empty treap holds no regions to merge");
    mergeRegionWithPreviousRec(root, baseAddress);
  }

private:
  template <typename OutStream>
  static void dumpRec(OutStream &out, std::string &prefix,
                      CoWPtr<Node> const &treap) {
    if (treap) {
      out << "[" << static_cast<void *>(treap->getBaseAddress()) << "; "
          << static_cast<void *>(treap->getBaseAddress() + treap->getSize())
          << ") of size " << treap->getSize() << "\n";

      auto oldPrefix = prefix.size();

      out << prefix << "├";
      prefix += "│";
      dumpRec(out, prefix, treap->lhs);

      prefix.resize(oldPrefix);

      out << prefix << "╰";
      prefix += " ";
      dumpRec(out, prefix, treap->rhs);
    } else {
      out << "<nil>\n";
    }
  }

public:
  template <typename OutStream> OutStream &dump(OutStream &out) {
    std::string prefix;
    dumpRec(out, prefix, root);
    return out;
  }

private:
  static void checkInvariants(std::pair<bool, bool> &result,
                              CoWPtr<Node> const &treap) {
    if (!result.first && !result.second) {
      return;
    }

    if (treap->lhs) {
      if (treap->lhs->getBaseAddress() >= treap->getBaseAddress()) {
        result.first = false;
      }
      if (treap->lhs->getSize() > treap->getSize()) {
        result.second = false;
      }
      checkInvariants(result, treap->lhs);
    }
    if (treap->rhs) {
      if (treap->rhs->getBaseAddress() <= treap->getBaseAddress()) {
        result.first = false;
      }
      if (treap->rhs->getSize() > treap->getSize()) {
        result.second = false;
      }
      checkInvariants(result, treap->rhs);
    }
  }

public:
  std::pair<bool, bool> checkInvariants() const {
    std::pair<bool, bool> result = {true, true};
    if (root) {
      checkInvariants(result, root);
    }
    return result;
  }
};
} // namespace klee::kdalloc::suballocators

#endif