Skip to content

Commit

Permalink
Permit re-setting --object-bits
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tautschnig committed Dec 13, 2023
1 parent 1000f59 commit eb7bd6d
Show file tree
Hide file tree
Showing 28 changed files with 142 additions and 161 deletions.
5 changes: 0 additions & 5 deletions doc/cprover-manual/goto-cc.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ most important architectural parameters are:
`sizeof(long int)` on various machines.
- The width of pointers; for example, compare the value of `sizeof(int *)` on
various machines.
- The number of bits in a pointer which are used to differentiate between
different objects. The remaining bits of a pointer are used for offsets
within objects.
- The [endianness](http://en.wikipedia.org/wiki/Endianness) of
the architecture.

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

When using a goto binary, CBMC and the other tools read the
configuration from the binary. The setting when running goto-cc is
Expand Down
7 changes: 4 additions & 3 deletions doc/man/goto-cc.1
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ files.
.TP
\fB\-\-print\-rejected\-preprocessed\-source\fR \fIfile\fR
Copy failing (preprocessed) source to \fIfile\fR.
.TP
\fB\-\-object\-bits\fR \fIN\fR
Configure the number of bits used for object numbering in CBMC's pointer encoding.
.SH BACKWARD COMPATIBILITY
.B goto\-cc
will warn and ignore the use of \fB\-\-object\-bits\fR, which previous versions
processed. This option now instead needs to be passed to \fBcbmc\fR(1).
.SH ENVIRONMENT
All tools honor the TMPDIR environment variable when generating temporary
files and directories.
Expand Down
1 change: 0 additions & 1 deletion regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ else()
add_subdirectory(goto-cl)
endif()
add_subdirectory(goto-cc-cbmc)
add_subdirectory(goto-cc-cbmc-shared-options)
add_subdirectory(cbmc-cpp)
add_subdirectory(goto-cc-goto-analyzer)
add_subdirectory(goto-analyzer-simplify)
Expand Down
1 change: 0 additions & 1 deletion regression/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ DIRS = cbmc-shadow-memory \
goto-gcc \
goto-cl \
goto-cc-cbmc \
goto-cc-cbmc-shared-options \
cbmc-cpp \
goto-cc-goto-analyzer \
goto-analyzer-simplify \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <stdlib.h>
#include <stdint.h>

extern size_t __CPROVER_max_malloc_size;

#if defined(_WIN32) && defined(_M_X64)
int __builtin_clzll(unsigned long long);
#define __nof_symex_objects \
Expand Down
11 changes: 0 additions & 11 deletions regression/goto-cc-cbmc-shared-options/CMakeLists.txt

This file was deleted.

30 changes: 0 additions & 30 deletions regression/goto-cc-cbmc-shared-options/Makefile

This file was deleted.

4 changes: 0 additions & 4 deletions regression/goto-cc-cbmc-shared-options/README.md

This file was deleted.

20 changes: 0 additions & 20 deletions regression/goto-cc-cbmc-shared-options/chain.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE winbug
CORE
test.c
--function main --object-bits 6
^EXIT=10$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE winbug
CORE
test.c
--function main --object-bits 10
^EXIT=10$
Expand Down
File renamed without changes.
34 changes: 3 additions & 31 deletions src/ansi-c/ansi_c_internal_additions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,6 @@ static std::string architecture_string(T value, const char *s)
std::string(s) + "=" + std::to_string(value) + ";\n";
}

/// The maximum allocation size is determined by the number of bits that
/// are left in the pointer of width \p pointer_width.
///
/// The allocation size cannot exceed the number represented by the (signed)
/// offset, otherwise it would not be possible to store a pointer into a
/// valid bit of memory. Therefore, the max allocation size is
/// 2^(offset_bits - 1), where the offset bits is the number of bits left in the
/// pointer after the object bits.
///
/// The offset must be signed, as a pointer can point to the end of the memory
/// block, and needs to be able to point back to the start.
/// \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 mp_integer
max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
{
PRECONDITION(pointer_width >= 1);
PRECONDITION(object_bits < pointer_width);
PRECONDITION(object_bits >= 1);
const auto offset_bits = pointer_width - object_bits;
// We require the offset to be able to express upto allocation_size - 1,
// but also down to -allocation_size, therefore the size is allowable
// is number of bits, less the signed bit.
const auto bits_for_positive_offset = offset_bits - 1;
return ((mp_integer)1) << (mp_integer)bits_for_positive_offset;
}

void ansi_c_internal_additions(std::string &code)
{
// clang-format off
Expand All @@ -181,9 +153,9 @@ void ansi_c_internal_additions(std::string &code)
CPROVER_PREFIX "size_t size, " CPROVER_PREFIX "bool zero);\n"
"void " CPROVER_PREFIX "deallocate(void *);\n"

CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
integer2string(max_malloc_size(config.ansi_c.pointer_width, config
.bv_encoding.object_bits));
CPROVER_PREFIX "thread_local " CPROVER_PREFIX "size_t "
CPROVER_PREFIX "max_malloc_size="+
integer2string(config.max_malloc_size());
if(config.ansi_c.pointer_width==config.ansi_c.long_int_width)
code += "UL;\n";
else if(config.ansi_c.pointer_width==config.ansi_c.long_long_int_width)
Expand Down
1 change: 1 addition & 0 deletions src/ansi-c/cprover_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ void cprover_c_library_factory_force_load(
const std::set<irep_idt> &functions,
symbol_table_baset &symbol_table,
message_handlert &message_handler);

#endif // CPROVER_ANSI_C_CPROVER_LIBRARY_H
5 changes: 4 additions & 1 deletion src/ansi-c/library/cprover.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ extern const void *__CPROVER_deallocated;
extern const void *__CPROVER_memory_leak;

extern int __CPROVER_malloc_failure_mode;
extern __CPROVER_size_t __CPROVER_max_malloc_size;
extern __CPROVER_bool __CPROVER_malloc_may_fail;
// The maximum size of an object that we can handle under the object:offset
// pointer encoding. Marked thread-local as it is a per-analysis constant that
// can safely be constant-propagated even in concurrent execution.
extern __CPROVER_thread_local __CPROVER_size_t __CPROVER_max_malloc_size;

// malloc failure modes
extern int __CPROVER_malloc_failure_mode_return_null;
Expand Down
1 change: 0 additions & 1 deletion src/ansi-c/library/cprover_contracts.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#define __CPROVER_contracts_library_defined

// external dependencies
extern __CPROVER_size_t __CPROVER_max_malloc_size;
const void *__CPROVER_alloca_object = 0;
extern const void *__CPROVER_deallocated;
const void *__CPROVER_new_object = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/cbmc/cbmc_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,8 @@ int cbmc_parse_optionst::get_goto_program(

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

update_max_malloc_size(goto_model, ui_message_handler);

if(cmdline.isset("show-symbol-table"))
{
show_symbol_table(goto_model, ui_message_handler);
Expand Down
1 change: 0 additions & 1 deletion src/goto-cc/gcc_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const char *goto_cc_options_with_separated_argument[]=
"--native-linker",
"--print-rejected-preprocessed-source",
"--mangle-suffix",
"--object-bits",
nullptr
};

Expand Down
1 change: 0 additions & 1 deletion src/goto-cc/goto_cc_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ void goto_cc_modet::help()
"symbols\n"
" {y--print-rejected-preprocessed-source} {ufile} \t "
"copy failing (preprocessed) source to file\n"
" {y--object-bits} {N} \t number of bits used for object addresses\n"
"\n");
}

Expand Down
29 changes: 29 additions & 0 deletions src/goto-programs/initialize_goto_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Author: Daniel Kroening, kroening@kroening.com

#include "initialize_goto_model.h"

#include <util/arith_tools.h>
#include <util/c_types.h>
#include <util/config.h>
#include <util/exception_utils.h>
#include <util/message.h>
Expand All @@ -22,6 +24,7 @@ Author: Daniel Kroening, kroening@kroening.com
#include <langapi/language.h>
#include <langapi/language_file.h>
#include <langapi/mode.h>
#include <linking/static_lifetime_init.h>

#include "goto_convert_functions.h"
#include "read_goto_binary.h"
Expand Down Expand Up @@ -236,3 +239,29 @@ goto_modelt initialize_goto_model(

return goto_model;
}

void update_max_malloc_size(
goto_modelt &goto_model,
message_handlert &message_handler)
{
if(!goto_model.symbol_table.has_symbol(CPROVER_PREFIX "max_malloc_size"))
return;

const auto previous_max_malloc_size_value = numeric_cast<mp_integer>(
goto_model.symbol_table.lookup_ref(CPROVER_PREFIX "max_malloc_size").value);
const mp_integer current_max_malloc_size = config.max_malloc_size();

if(
!previous_max_malloc_size_value.has_value() ||
*previous_max_malloc_size_value != current_max_malloc_size)
{
symbolt &max_malloc_size_sym = goto_model.symbol_table.get_writeable_ref(
CPROVER_PREFIX "max_malloc_size");
max_malloc_size_sym.value =
from_integer(current_max_malloc_size, size_type());
max_malloc_size_sym.value.set(ID_C_no_nondet_initialization, true);

if(goto_model.can_produce_function(INITIALIZE_FUNCTION))
recreate_initialize_function(goto_model, message_handler);
}
}
4 changes: 4 additions & 0 deletions src/goto-programs/initialize_goto_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ void set_up_custom_entry_point(
bool try_mode_lookup,
message_handlert &message_handler);

/// Update the initial value of `__CPROVER_max_malloc_size` in case the number
/// of object bits has changed.
void update_max_malloc_size(goto_modelt &, message_handlert &);

#endif // CPROVER_GOTO_PROGRAMS_INITIALIZE_GOTO_MODEL_H
3 changes: 3 additions & 0 deletions src/goto-synthesizer/goto_synthesizer_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Author: Qinheping Hu
#include <util/unicode.h>
#include <util/version.h>

#include <goto-programs/initialize_goto_model.h>
#include <goto-programs/read_goto_binary.h>
#include <goto-programs/set_properties.h>
#include <goto-programs/show_goto_functions.h>
Expand Down Expand Up @@ -73,6 +74,8 @@ int goto_synthesizer_parse_optionst::doit()
configure_gcc(gcc_version);
}

update_max_malloc_size(goto_model, ui_message_handler);

// Get options for the backend verifier and preprocess `goto_model`.
const auto &options = get_options();

Expand Down
25 changes: 25 additions & 0 deletions src/util/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,3 +1468,28 @@ irep_idt configt::this_operating_system()

return this_os;
}

/// The maximum allocation size is determined by the number of bits that
/// are left in the pointer of width `ansi_c.pointer_width`.
///
/// The allocation size cannot exceed the number represented by the (signed)
/// offset, otherwise it would not be possible to store a pointer into a
/// valid bit of memory. Therefore, the max allocation size is
/// 2^(offset_bits - 1), where the offset bits is the number of bits left in the
/// pointer after the object bits.
///
/// The offset must be signed, as a pointer can point to the end of the memory
/// block, and needs to be able to point back to the start.
/// \return The size in bytes of the maximum allocation supported.
mp_integer configt::max_malloc_size() const
{
PRECONDITION(ansi_c.pointer_width >= 1);
PRECONDITION(bv_encoding.object_bits < ansi_c.pointer_width);
PRECONDITION(bv_encoding.object_bits >= 1);
const auto offset_bits = ansi_c.pointer_width - bv_encoding.object_bits;
// We require the offset to be able to express upto allocation_size - 1,
// but also down to -allocation_size, therefore the size is allowable
// is number of bits, less the signed bit.
const auto bits_for_positive_offset = offset_bits - 1;
return ((mp_integer)1) << (mp_integer)bits_for_positive_offset;
}
1 change: 1 addition & 0 deletions src/util/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class configt
void set_object_bits_from_symbol_table(const symbol_table_baset &);
std::string object_bits_info();
mp_integer max_malloc_size() const;
static irep_idt this_architecture();
static irep_idt this_operating_system();
Expand Down
2 changes: 1 addition & 1 deletion unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ SRC += analyses/ai/ai.cpp \
analyses/variable-sensitivity/variable_sensitivity_domain/to_predicate.cpp \
analyses/variable-sensitivity/variable_sensitivity_test_helpers.cpp \
ansi-c/expr2c.cpp \
ansi-c/max_malloc_size.cpp \
ansi-c/type2name.cpp \
ansi-c/c_typecheck_base.cpp \
big-int/big-int.cpp \
Expand Down Expand Up @@ -168,6 +167,7 @@ SRC += analyses/ai/ai.cpp \
util/json_object.cpp \
util/lazy.cpp \
util/lower_byte_operators.cpp \
util/max_malloc_size.cpp \
util/memory_info.cpp \
util/message.cpp \
util/optional_utils.cpp \
Expand Down
Loading

0 comments on commit eb7bd6d

Please sign in to comment.