@@ -8,6 +8,8 @@ Author: Daniel Kroening, kroening@kroening.com
88
99#include " ansi_c_internal_additions.h"
1010
11+ #include < limits>
12+
1113#include < util/c_types.h>
1214#include < util/config.h>
1315
@@ -120,6 +122,34 @@ static std::string architecture_string(T value, const char *s)
120122 std::string (s) + " =" + std::to_string (value) + " ;\n " ;
121123}
122124
125+ // / The maximum allocation size is determined by the number of bits that
126+ // / are left in the pointer of width \p pointer_width.
127+ // /
128+ // / The allocation size cannot exceed the number represented by the (signed)
129+ // / offset, otherwise it would not be possible to store a pointer into a
130+ // / valid bit of memory. Therefore, the max allocation size is
131+ // / 2^(offset_bits - 1), where the offset bits is the number of bits left in the
132+ // / pointer after the object bits.
133+ // /
134+ // / The offset must be signed, as a pointer can point to the end of the memory
135+ // / block, and needs to be able to point back to the start.
136+ // / \param pointer_width: The width of the pointer
137+ // / \param object_bits : The number of bits used to represent the ID
138+ // / \return The size in bytes of the maximum allocation supported.
139+ static mp_integer
140+ max_malloc_size (std::size_t pointer_width, std::size_t object_bits)
141+ {
142+ PRECONDITION (pointer_width >= 1 );
143+ PRECONDITION (object_bits < pointer_width);
144+ PRECONDITION (object_bits >= 1 );
145+ const auto offset_bits = pointer_width - object_bits;
146+ // We require the offset to be able to express upto allocation_size - 1,
147+ // but also down to -allocation_size, therefore the size is allowable
148+ // is number of bits, less the signed bit.
149+ const auto bits_for_positive_offset = offset_bits - 1 ;
150+ return ((mp_integer)1 ) << (mp_integer)bits_for_positive_offset;
151+ }
152+
123153void ansi_c_internal_additions (std::string &code)
124154{
125155 // clang-format off
@@ -162,8 +192,8 @@ void ansi_c_internal_additions(std::string &code)
162192 " int " CPROVER_PREFIX " malloc_failure_mode_assert_then_assume=" +
163193 std::to_string (config.ansi_c .malloc_failure_mode_assert_then_assume )+" ;\n "
164194 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 "
195+ integer2string ( max_malloc_size ( config.ansi_c .pointer_width , config
196+ .bv_encoding .object_bits ))+" ;\n "
167197
168198 // this is ANSI-C
169199 " extern " CPROVER_PREFIX " thread_local const char __func__["
0 commit comments