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
|
//===-- reuse.cpp ---------------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "klee/KDAlloc/kdalloc.h"
#if defined(USE_GTEST_INSTEAD_OF_MAIN)
#include "gtest/gtest.h"
#endif
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <unordered_set>
#if defined(USE_GTEST_INSTEAD_OF_MAIN)
int reuse_test() {
#else
int main() {
#endif
{
static const std::size_t size = 8;
static const std::uint32_t quarantine = 0;
std::cout << "size = " << size << " quarantine = " << quarantine << "\n";
auto allocator =
static_cast<klee::kdalloc::Allocator>(klee::kdalloc::AllocatorFactory(
static_cast<std::size_t>(1) << 42, quarantine));
auto a = allocator.allocate(size);
allocator.free(a, size);
auto b = allocator.allocate(size);
allocator.free(b, size);
auto c = allocator.allocate(size);
allocator.free(c, size);
auto d = allocator.allocate(size);
allocator.free(d, size);
std::cout << "a: " << a << "\nb: " << b << "\nc: " << c << "\nd: " << d
<< "\n";
assert(a == b);
assert(a == c);
assert(a == d);
}
std::cout << "\n\n";
{
static const std::size_t size = 8;
static const std::uint32_t quarantine = 1;
std::cout << "size = " << size << " quarantine = " << quarantine << "\n";
auto allocator =
static_cast<klee::kdalloc::Allocator>(klee::kdalloc::AllocatorFactory(
static_cast<std::size_t>(1) << 42, quarantine));
auto a = allocator.allocate(size);
allocator.free(a, size);
auto b = allocator.allocate(size);
allocator.free(b, size);
auto c = allocator.allocate(size);
allocator.free(c, size);
auto d = allocator.allocate(size);
allocator.free(d, size);
std::cout << "a: " << a << "\nb: " << b << "\nc: " << c << "\nd: " << d
<< "\n";
assert(a != b);
assert(a == c);
assert(b == d);
}
std::cout << "\n\n";
{
static const std::size_t size = 8;
static const std::uint32_t quarantine = 2;
std::cout << "size = " << size << " quarantine = " << quarantine << "\n";
auto allocator =
static_cast<klee::kdalloc::Allocator>(klee::kdalloc::AllocatorFactory(
static_cast<std::size_t>(1) << 42, quarantine));
auto a = allocator.allocate(size);
allocator.free(a, size);
auto b = allocator.allocate(size);
allocator.free(b, size);
auto c = allocator.allocate(size);
allocator.free(c, size);
auto d = allocator.allocate(size);
allocator.free(d, size);
std::cout << "a: " << a << "\nb: " << b << "\nc: " << c << "\nd: " << d
<< "\n";
assert(a != b);
assert(a != c);
assert(b != c);
assert(a == d);
}
std::cout << "\n\n";
{
static const std::size_t size = 8;
static const std::uint32_t quarantine =
klee::kdalloc::AllocatorFactory::unlimitedQuarantine;
std::cout << "size = " << size << " quarantine unlimited\n";
auto allocator =
static_cast<klee::kdalloc::Allocator>(klee::kdalloc::AllocatorFactory(
static_cast<std::size_t>(1) << 42, quarantine));
#if KDALLOC_TRACE >= 2
static const std::size_t iterations = 10;
#else
static const std::size_t iterations = 10'000;
#endif
std::unordered_set<void *> allocations;
allocations.reserve(iterations);
for (std::size_t i = 0; i < iterations; ++i) {
auto *ptr = allocator.allocate(size);
allocator.free(ptr, 8);
auto emplaced = allocations.emplace(ptr);
assert(emplaced.second);
}
}
std::exit(0);
}
#if defined(USE_GTEST_INSTEAD_OF_MAIN)
TEST(KDAllocDeathTest, Reuse) {
ASSERT_EXIT(reuse_test(), ::testing::ExitedWithCode(0), "");
}
#endif
|