|
8 | 8 |
|
9 | 9 | #include "ansi_c_internal_additions.h"
|
10 | 10 |
|
| 11 | +#include <limits> |
| 12 | + |
11 | 13 | #include <util/c_types.h>
|
12 | 14 | #include <util/config.h>
|
13 | 15 |
|
@@ -120,6 +122,24 @@ static std::string architecture_string(T value, const char *s)
|
120 | 122 | std::string(s) + "=" + std::to_string(value) + ";\n";
|
121 | 123 | }
|
122 | 124 |
|
| 125 | +using max_alloc_sizet = uint64_t; |
| 126 | +/// The maximum allocation size is determined by the number of bits that |
| 127 | +/// are left in the pointer of width \p pointer_width after allowing for the |
| 128 | +/// signed bit, and the bits used for the objects ID (determined by |
| 129 | +/// \p object_bits). |
| 130 | +/// \param pointer_width: The width of the pointer |
| 131 | +/// \param object_bits : The number of bits used to represent the ID |
| 132 | +/// \return The size in bytes of the maximum allocation supported. |
| 133 | +static max_alloc_sizet |
| 134 | +max_malloc_size(std::size_t pointer_width, std::size_t object_bits) |
| 135 | +{ |
| 136 | + PRECONDITION(object_bits < pointer_width - 1); |
| 137 | + PRECONDITION(object_bits >= 1); |
| 138 | + const auto bits_for_offset = pointer_width - object_bits - 1; |
| 139 | + PRECONDITION(bits_for_offset < std::numeric_limits<max_alloc_sizet>::digits); |
| 140 | + return ((max_alloc_sizet)1) << (max_alloc_sizet)bits_for_offset; |
| 141 | +} |
| 142 | + |
123 | 143 | void ansi_c_internal_additions(std::string &code)
|
124 | 144 | {
|
125 | 145 | // clang-format off
|
@@ -162,8 +182,8 @@ void ansi_c_internal_additions(std::string &code)
|
162 | 182 | "int " CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+
|
163 | 183 | std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n"
|
164 | 184 | CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
|
165 |
| - std::to_string(1 << (config.ansi_c.pointer_width - |
166 |
| - config.bv_encoding.object_bits - 1))+";\n" |
| 185 | + std::to_string(max_malloc_size(config.ansi_c.pointer_width, config |
| 186 | + .bv_encoding.object_bits))+";\n" |
167 | 187 |
|
168 | 188 | // this is ANSI-C
|
169 | 189 | "extern " CPROVER_PREFIX "thread_local const char __func__["
|
|
0 commit comments