-
Notifications
You must be signed in to change notification settings - Fork 274
Fix max malloc size #5298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix max malloc size #5298
Changes from 1 commit
3f86b3e
48497ff
5a2e85e
4fe6842
12d94f1
448ff24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include "ansi_c_internal_additions.h" | ||
|
||
#include <limits> | ||
|
||
#include <util/c_types.h> | ||
#include <util/config.h> | ||
|
||
|
@@ -120,6 +122,24 @@ static std::string architecture_string(T value, const char *s) | |
std::string(s) + "=" + std::to_string(value) + ";\n"; | ||
} | ||
|
||
using max_alloc_sizet = uint64_t; | ||
/// The maximum allocation size is determined by the number of bits that | ||
/// are left in the pointer of width \p pointer_width after allowing for the | ||
/// signed bit, and the bits used for the objects ID (determined by | ||
/// \p object_bits). | ||
/// \param pointer_width: The width of the pointer | ||
/// \param object_bits : The number of bits used to represent the ID | ||
/// \return The size in bytes of the maximum allocation supported. | ||
static max_alloc_sizet | ||
max_malloc_size(std::size_t pointer_width, std::size_t object_bits) | ||
{ | ||
PRECONDITION(object_bits < pointer_width - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think not: you need one bit for the sign? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just saw second comment - OK I misunderstood what you said - I thought the signed bit was used for something else, rather than the offset? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The offset is signed. So with just one bit, 0 (in binary) would be 0 (in decimal), and 1 (in binary) would be -1 (in decimal). |
||
PRECONDITION(object_bits >= 1); | ||
const auto bits_for_offset = pointer_width - object_bits - 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming is not quite right as the bits for the offset is |
||
PRECONDITION(bits_for_offset < std::numeric_limits<max_alloc_sizet>::digits); | ||
return ((max_alloc_sizet)1) << (max_alloc_sizet)bits_for_offset; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think casting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to be explicit here, since the specific rules of what type gets promoted are fiddly. |
||
} | ||
|
||
void ansi_c_internal_additions(std::string &code) | ||
{ | ||
// clang-format off | ||
|
@@ -162,8 +182,8 @@ void ansi_c_internal_additions(std::string &code) | |
"int " CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+ | ||
std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n" | ||
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+ | ||
std::to_string(1 << (config.ansi_c.pointer_width - | ||
config.bv_encoding.object_bits - 1))+";\n" | ||
std::to_string(max_malloc_size(config.ansi_c.pointer_width, config | ||
.bv_encoding.object_bits))+";\n" | ||
|
||
// this is ANSI-C | ||
"extern " CPROVER_PREFIX "thread_local const char __func__[" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
size_t
? That's the type with which__CPROVER_max_malloc_size
is declared.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this makes sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though the advantage of using a platform agnostic width is writing the unit tests is easier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danpoe The problem is that cbmcs
size_t
that we analyze with isn't necessarily the same as that which we get when compiling cbmc itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As pointed out by @thk123 in private communication, when using
size_t
instead ofuint64_t
, the computation would overflow when running on a 32-bit machine and using e.g.--LLP64
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please use
mp_integer
here instead of any architecturally limited type? We do not want to run into limitations of the host or compiler with this computation.