Skip to content

Commit 549b3e4

Browse files
committed
Permit re-setting --object-bits
Support choosing the number of object bits to be used by CBMC at runtime even with pre-compiled goto binaries. Update the goto-cc section in the CPROVER manual to remove any documentation of object-bits (it remains documented in the "Memory and pointers in CBMC" section). Fixes: #5443
1 parent b204648 commit 549b3e4

28 files changed

+139
-161
lines changed

doc/cprover-manual/goto-cc.md

-5
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ most important architectural parameters are:
132132
`sizeof(long int)` on various machines.
133133
- The width of pointers; for example, compare the value of `sizeof(int *)` on
134134
various machines.
135-
- The number of bits in a pointer which are used to differentiate between
136-
different objects. The remaining bits of a pointer are used for offsets
137-
within objects.
138135
- The [endianness](http://en.wikipedia.org/wiki/Endianness) of
139136
the architecture.
140137

@@ -152,8 +149,6 @@ following command-line arguments can be passed to the CPROVER tools:
152149
- The word-width can be set with `--16`, `--32`, `--64`.
153150
- The endianness can be defined with `--little-endian` and
154151
`--big-endian`.
155-
- The number of bits in a pointer used to differentiate between different
156-
objects can be set using `--object-bits x`. Where `x` is the number of bits.
157152

158153
When using a goto binary, CBMC and the other tools read the
159154
configuration from the binary. The setting when running goto-cc is

doc/man/goto-cc.1

+4-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ files.
9696
.TP
9797
\fB\-\-print\-rejected\-preprocessed\-source\fR \fIfile\fR
9898
Copy failing (preprocessed) source to \fIfile\fR.
99-
.TP
100-
\fB\-\-object\-bits\fR \fIN\fR
101-
Configure the number of bits used for object numbering in CBMC's pointer encoding.
99+
.SH BACKWARD COMPATIBILITY
100+
.B goto\-cc
101+
will warn and ignore the use of \fB\-\-object\-bits\fR, which previous versions
102+
processed. This option now instead needs to be passed to \fBcbmc\fR(1).
102103
.SH ENVIRONMENT
103104
All tools honor the TMPDIR environment variable when generating temporary
104105
files and directories.

regression/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ else()
6363
add_subdirectory(goto-cl)
6464
endif()
6565
add_subdirectory(goto-cc-cbmc)
66-
add_subdirectory(goto-cc-cbmc-shared-options)
6766
add_subdirectory(cbmc-cpp)
6867
add_subdirectory(goto-cc-goto-analyzer)
6968
add_subdirectory(goto-analyzer-simplify)

regression/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ DIRS = cbmc-shadow-memory \
3636
goto-gcc \
3737
goto-cl \
3838
goto-cc-cbmc \
39-
goto-cc-cbmc-shared-options \
4039
cbmc-cpp \
4140
goto-cc-goto-analyzer \
4241
goto-analyzer-simplify \

regression/cbmc/simplify_singleton_interval_7690/singleton_interval_in_assume_7690.c

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <stdlib.h>
66
#include <stdint.h>
77

8-
extern size_t __CPROVER_max_malloc_size;
9-
108
#if defined(_WIN32) && defined(_M_X64)
119
int __builtin_clzll(unsigned long long);
1210
#define __nof_symex_objects \

regression/goto-cc-cbmc-shared-options/CMakeLists.txt

-11
This file was deleted.

regression/goto-cc-cbmc-shared-options/Makefile

-30
This file was deleted.

regression/goto-cc-cbmc-shared-options/README.md

-4
This file was deleted.

regression/goto-cc-cbmc-shared-options/chain.sh

-20
This file was deleted.

regression/goto-cc-cbmc-shared-options/object-bits/fewer_bits.desc regression/goto-cc-cbmc/object-bits/fewer_bits.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE winbug
1+
CORE
22
test.c
33
--function main --object-bits 6
44
^EXIT=10$

regression/goto-cc-cbmc-shared-options/object-bits/more_bits.desc regression/goto-cc-cbmc/object-bits/more_bits.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE winbug
1+
CORE
22
test.c
33
--function main --object-bits 10
44
^EXIT=10$

src/ansi-c/ansi_c_internal_additions.cpp

+3-31
Original file line numberDiff line numberDiff line change
@@ -130,34 +130,6 @@ static std::string architecture_string(T value, const char *s)
130130
std::string(s) + "=" + std::to_string(value) + ";\n";
131131
}
132132

133-
/// The maximum allocation size is determined by the number of bits that
134-
/// are left in the pointer of width \p pointer_width.
135-
///
136-
/// The allocation size cannot exceed the number represented by the (signed)
137-
/// offset, otherwise it would not be possible to store a pointer into a
138-
/// valid bit of memory. Therefore, the max allocation size is
139-
/// 2^(offset_bits - 1), where the offset bits is the number of bits left in the
140-
/// pointer after the object bits.
141-
///
142-
/// The offset must be signed, as a pointer can point to the end of the memory
143-
/// block, and needs to be able to point back to the start.
144-
/// \param pointer_width: The width of the pointer
145-
/// \param object_bits : The number of bits used to represent the ID
146-
/// \return The size in bytes of the maximum allocation supported.
147-
static mp_integer
148-
max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
149-
{
150-
PRECONDITION(pointer_width >= 1);
151-
PRECONDITION(object_bits < pointer_width);
152-
PRECONDITION(object_bits >= 1);
153-
const auto offset_bits = pointer_width - object_bits;
154-
// We require the offset to be able to express upto allocation_size - 1,
155-
// but also down to -allocation_size, therefore the size is allowable
156-
// is number of bits, less the signed bit.
157-
const auto bits_for_positive_offset = offset_bits - 1;
158-
return ((mp_integer)1) << (mp_integer)bits_for_positive_offset;
159-
}
160-
161133
void ansi_c_internal_additions(std::string &code)
162134
{
163135
// clang-format off
@@ -181,9 +153,9 @@ void ansi_c_internal_additions(std::string &code)
181153
CPROVER_PREFIX "size_t size, " CPROVER_PREFIX "bool zero);\n"
182154
"void " CPROVER_PREFIX "deallocate(void *);\n"
183155

184-
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
185-
integer2string(max_malloc_size(config.ansi_c.pointer_width, config
186-
.bv_encoding.object_bits));
156+
CPROVER_PREFIX "thread_local " CPROVER_PREFIX "size_t "
157+
CPROVER_PREFIX "max_malloc_size="+
158+
integer2string(config.max_malloc_size());
187159
if(config.ansi_c.pointer_width==config.ansi_c.long_int_width)
188160
code += "UL;\n";
189161
else if(config.ansi_c.pointer_width==config.ansi_c.long_long_int_width)

src/ansi-c/cprover_library.h

+1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ void cprover_c_library_factory_force_load(
5252
const std::set<irep_idt> &functions,
5353
symbol_table_baset &symbol_table,
5454
message_handlert &message_handler);
55+
5556
#endif // CPROVER_ANSI_C_CPROVER_LIBRARY_H

src/ansi-c/library/cprover.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ extern const void *__CPROVER_deallocated;
2626
extern const void *__CPROVER_memory_leak;
2727

2828
extern int __CPROVER_malloc_failure_mode;
29-
extern __CPROVER_size_t __CPROVER_max_malloc_size;
3029
extern __CPROVER_bool __CPROVER_malloc_may_fail;
30+
extern __CPROVER_thread_local __CPROVER_size_t __CPROVER_max_malloc_size;
3131

3232
// malloc failure modes
3333
extern int __CPROVER_malloc_failure_mode_return_null;

src/ansi-c/library/cprover_contracts.c

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#define __CPROVER_contracts_library_defined
88

99
// external dependencies
10-
extern __CPROVER_size_t __CPROVER_max_malloc_size;
1110
const void *__CPROVER_alloca_object = 0;
1211
extern const void *__CPROVER_deallocated;
1312
const void *__CPROVER_new_object = 0;

src/cbmc/cbmc_parse_options.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ int cbmc_parse_optionst::get_goto_program(
738738

739739
goto_model = initialize_goto_model(cmdline.args, ui_message_handler, options);
740740

741+
update_max_malloc_size(goto_model, ui_message_handler);
742+
741743
if(cmdline.isset("show-symbol-table"))
742744
{
743745
show_symbol_table(goto_model, ui_message_handler);

src/goto-cc/gcc_cmdline.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const char *goto_cc_options_with_separated_argument[]=
2929
"--native-linker",
3030
"--print-rejected-preprocessed-source",
3131
"--mangle-suffix",
32-
"--object-bits",
3332
nullptr
3433
};
3534

src/goto-cc/goto_cc_mode.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ void goto_cc_modet::help()
7474
"symbols\n"
7575
" {y--print-rejected-preprocessed-source} {ufile} \t "
7676
"copy failing (preprocessed) source to file\n"
77-
" {y--object-bits} {N} \t number of bits used for object addresses\n"
7877
"\n");
7978
}
8079

src/goto-programs/initialize_goto_model.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "initialize_goto_model.h"
1313

14+
#include <util/arith_tools.h>
15+
#include <util/c_types.h>
1416
#include <util/config.h>
1517
#include <util/exception_utils.h>
1618
#include <util/message.h>
@@ -22,6 +24,7 @@ Author: Daniel Kroening, [email protected]
2224
#include <langapi/language.h>
2325
#include <langapi/language_file.h>
2426
#include <langapi/mode.h>
27+
#include <linking/static_lifetime_init.h>
2528

2629
#include "goto_convert_functions.h"
2730
#include "read_goto_binary.h"
@@ -237,3 +240,29 @@ goto_modelt initialize_goto_model(
237240

238241
return goto_model;
239242
}
243+
244+
void update_max_malloc_size(
245+
goto_modelt &goto_model,
246+
message_handlert &message_handler)
247+
{
248+
if(!goto_model.symbol_table.has_symbol(CPROVER_PREFIX "max_malloc_size"))
249+
return;
250+
251+
const auto previous_max_malloc_size_value = numeric_cast<mp_integer>(
252+
goto_model.symbol_table.lookup_ref(CPROVER_PREFIX "max_malloc_size").value);
253+
const mp_integer current_max_malloc_size = config.max_malloc_size();
254+
255+
if(
256+
!previous_max_malloc_size_value.has_value() ||
257+
*previous_max_malloc_size_value != current_max_malloc_size)
258+
{
259+
symbolt &max_malloc_size_sym = goto_model.symbol_table.get_writeable_ref(
260+
CPROVER_PREFIX "max_malloc_size");
261+
max_malloc_size_sym.value =
262+
from_integer(current_max_malloc_size, size_type());
263+
max_malloc_size_sym.value.set(ID_C_no_nondet_initialization, true);
264+
265+
if(goto_model.can_produce_function(INITIALIZE_FUNCTION))
266+
recreate_initialize_function(goto_model, message_handler);
267+
}
268+
}

src/goto-programs/initialize_goto_model.h

+4
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ void set_up_custom_entry_point(
5656
bool try_mode_lookup,
5757
message_handlert &message_handler);
5858

59+
/// Update the initial value of `__CPROVER_max_malloc_size` in case the number
60+
/// of object bits has changed.
61+
void update_max_malloc_size(goto_modelt &, message_handlert &);
62+
5963
#endif // CPROVER_GOTO_PROGRAMS_INITIALIZE_GOTO_MODEL_H

src/goto-synthesizer/goto_synthesizer_parse_options.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Qinheping Hu
1313
#include <util/unicode.h>
1414
#include <util/version.h>
1515

16+
#include <goto-programs/initialize_goto_model.h>
1617
#include <goto-programs/read_goto_binary.h>
1718
#include <goto-programs/set_properties.h>
1819
#include <goto-programs/show_goto_functions.h>
@@ -73,6 +74,8 @@ int goto_synthesizer_parse_optionst::doit()
7374
configure_gcc(gcc_version);
7475
}
7576

77+
update_max_malloc_size(goto_model, ui_message_handler);
78+
7679
// Get options for the backend verifier and preprocess `goto_model`.
7780
const auto &options = get_options();
7881

src/util/config.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -1468,3 +1468,28 @@ irep_idt configt::this_operating_system()
14681468

14691469
return this_os;
14701470
}
1471+
1472+
/// The maximum allocation size is determined by the number of bits that
1473+
/// are left in the pointer of width `ansi_c.pointer_width`.
1474+
///
1475+
/// The allocation size cannot exceed the number represented by the (signed)
1476+
/// offset, otherwise it would not be possible to store a pointer into a
1477+
/// valid bit of memory. Therefore, the max allocation size is
1478+
/// 2^(offset_bits - 1), where the offset bits is the number of bits left in the
1479+
/// pointer after the object bits.
1480+
///
1481+
/// The offset must be signed, as a pointer can point to the end of the memory
1482+
/// block, and needs to be able to point back to the start.
1483+
/// \return The size in bytes of the maximum allocation supported.
1484+
mp_integer configt::max_malloc_size() const
1485+
{
1486+
PRECONDITION(ansi_c.pointer_width >= 1);
1487+
PRECONDITION(bv_encoding.object_bits < ansi_c.pointer_width);
1488+
PRECONDITION(bv_encoding.object_bits >= 1);
1489+
const auto offset_bits = ansi_c.pointer_width - bv_encoding.object_bits;
1490+
// We require the offset to be able to express upto allocation_size - 1,
1491+
// but also down to -allocation_size, therefore the size is allowable
1492+
// is number of bits, less the signed bit.
1493+
const auto bits_for_positive_offset = offset_bits - 1;
1494+
return ((mp_integer)1) << (mp_integer)bits_for_positive_offset;
1495+
}

src/util/config.h

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ class configt
358358
359359
void set_object_bits_from_symbol_table(const symbol_table_baset &);
360360
std::string object_bits_info();
361+
mp_integer max_malloc_size() const;
361362
362363
static irep_idt this_architecture();
363364
static irep_idt this_operating_system();

unit/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ SRC += analyses/ai/ai.cpp \
4949
analyses/variable-sensitivity/variable_sensitivity_domain/to_predicate.cpp \
5050
analyses/variable-sensitivity/variable_sensitivity_test_helpers.cpp \
5151
ansi-c/expr2c.cpp \
52-
ansi-c/max_malloc_size.cpp \
5352
ansi-c/type2name.cpp \
5453
ansi-c/c_typecheck_base.cpp \
5554
big-int/big-int.cpp \
@@ -168,6 +167,7 @@ SRC += analyses/ai/ai.cpp \
168167
util/json_object.cpp \
169168
util/lazy.cpp \
170169
util/lower_byte_operators.cpp \
170+
util/max_malloc_size.cpp \
171171
util/memory_info.cpp \
172172
util/message.cpp \
173173
util/optional.cpp \

0 commit comments

Comments
 (0)