Skip to content

Commit 2a9ccc9

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. Fixes: #5443 fixup! Permit re-setting --object-bits and malloc failure mode
1 parent baf7ccf commit 2a9ccc9

27 files changed

+141
-160
lines changed

doc/man/goto-cc.1

-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ 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.
10299
.SH ENVIRONMENT
103100
All tools honor the TMPDIR environment variable when generating temporary
104101
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

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

8-
extern size_t __CPROVER_max_malloc_size;
98
int __builtin_clzll(unsigned long long);
109

1110
#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))+";\n"
156+
CPROVER_PREFIX "thread_local " CPROVER_PREFIX "size_t "
157+
CPROVER_PREFIX "max_malloc_size="+
158+
integer2string(config.max_malloc_size())+";\n"
187159

188160
// this is ANSI-C
189161
"extern " CPROVER_PREFIX "thread_local const char __func__["

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
@@ -744,6 +744,8 @@ int cbmc_parse_optionst::get_goto_program(
744744

745745
goto_model = initialize_goto_model(cmdline.args, ui_message_handler, options);
746746

747+
update_max_malloc_size(goto_model, ui_message_handler);
748+
747749
if(cmdline.isset("show-symbol-table"))
748750
{
749751
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
@@ -69,7 +69,6 @@ void goto_cc_modet::help()
6969
" --mangle-suffix suffix append suffix to exported file-local symbols\n"
7070
" --print-rejected-preprocessed-source file\n"
7171
" copy failing (preprocessed) source to file\n"
72-
" --object-bits number of bits used for object addresses\n"
7372
"\n";
7473
// clang-format on
7574
}

src/goto-programs/initialize_goto_model.cpp

+35-5
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "initialize_goto_model.h"
1313

14-
#include <fstream>
15-
14+
#include <util/arith_tools.h>
15+
#include <util/c_types.h>
1616
#include <util/config.h>
1717
#include <util/message.h>
1818
#include <util/options.h>
1919

20+
#include <fstream>
21+
2022
#ifdef _MSC_VER
2123
# include <util/unicode.h>
2224
#endif
2325

26+
#include <util/exception_utils.h>
27+
28+
#include <goto-programs/rebuild_goto_start_function.h>
29+
2430
#include <langapi/language.h>
2531
#include <langapi/language_file.h>
2632
#include <langapi/mode.h>
27-
28-
#include <goto-programs/rebuild_goto_start_function.h>
29-
#include <util/exception_utils.h>
33+
#include <linking/static_lifetime_init.h>
3034

3135
#include "goto_convert_functions.h"
3236
#include "read_goto_binary.h"
@@ -245,3 +249,29 @@ goto_modelt initialize_goto_model(
245249

246250
return goto_model;
247251
}
252+
253+
void update_max_malloc_size(
254+
goto_modelt &goto_model,
255+
message_handlert &message_handler)
256+
{
257+
if(!goto_model.symbol_table.has_symbol(CPROVER_PREFIX "max_malloc_size"))
258+
return;
259+
260+
const auto previous_max_malloc_size_value = numeric_cast<mp_integer>(
261+
goto_model.symbol_table.lookup_ref(CPROVER_PREFIX "max_malloc_size").value);
262+
const mp_integer current_max_malloc_size = config.max_malloc_size();
263+
264+
if(
265+
!previous_max_malloc_size_value.has_value() ||
266+
*previous_max_malloc_size_value != current_max_malloc_size)
267+
{
268+
symbolt &max_malloc_size_sym = goto_model.symbol_table.get_writeable_ref(
269+
CPROVER_PREFIX "max_malloc_size");
270+
max_malloc_size_sym.value =
271+
from_integer(current_max_malloc_size, size_type());
272+
max_malloc_size_sym.value.set(ID_C_no_nondet_initialization, true);
273+
274+
if(goto_model.can_produce_function(INITIALIZE_FUNCTION))
275+
recreate_initialize_function(goto_model, message_handler);
276+
}
277+
}

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
@@ -12,6 +12,7 @@ Author: Qinheping Hu
1212
#include <util/help_formatter.h>
1313
#include <util/version.h>
1414

15+
#include <goto-programs/initialize_goto_model.h>
1516
#include <goto-programs/read_goto_binary.h>
1617
#include <goto-programs/set_properties.h>
1718
#include <goto-programs/write_goto_binary.h>
@@ -61,6 +62,8 @@ int goto_synthesizer_parse_optionst::doit()
6162
configure_gcc(gcc_version);
6263
}
6364

65+
update_max_malloc_size(goto_model, ui_message_handler);
66+
6467
// Get options and preprocess `goto_model`.
6568
const auto &options = get_options();
6669

src/util/config.cpp

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

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

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
big-int/big-int.cpp \
5554
compound_block_locations.cpp \
@@ -165,6 +164,7 @@ SRC += analyses/ai/ai.cpp \
165164
util/json_object.cpp \
166165
util/lazy.cpp \
167166
util/lower_byte_operators.cpp \
167+
util/max_malloc_size.cpp \
168168
util/memory_info.cpp \
169169
util/message.cpp \
170170
util/optional.cpp \

unit/ansi-c/max_malloc_size.cpp

-46
This file was deleted.

0 commit comments

Comments
 (0)