Skip to content

Commit

Permalink
New option --mmio-regions to specify memory regions
Browse files Browse the repository at this point in the history
This new option will enable us to eventually drop support for
__CPROVER_allocated_memory, which
1) requires awkward scanning of goto functions in goto_check_c,
2) wrongly suggests these declarations are limited to some scope, and
3) yields a spurious undefined-function warning in symex.
  • Loading branch information
tautschnig committed Jul 6, 2022
1 parent 365871d commit fafe18a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 2 deletions.
12 changes: 12 additions & 0 deletions regression/cbmc/memory_allocation1/cmdline.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE broken-smt-backend
main.c
--pointer-check -DCMDLINE --mmio-regions 0x10:4
^EXIT=10$
^SIGNAL=0$
^\[main\.pointer_dereference\.2\] .* dereference failure: invalid integer address in \*p: SUCCESS$
^\[main\.assertion\.1\] .* assertion \*p==42: SUCCESS$
^\[main\.pointer_dereference\.[0-9]+\] .* dereference failure: invalid integer address in p\[.*1\]: FAILURE$
^\[main\.assertion\.2\] .* assertion \*\(p\+1\)==42: SUCCESS$
^VERIFICATION FAILED$
--
^warning: ignoring
2 changes: 2 additions & 0 deletions regression/cbmc/memory_allocation1/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ int main()
{
int *p=0x10;

#ifndef CMDLINE
__CPROVER_allocated_memory(0x10, sizeof(int));
#endif
*p=42;
assert(*p==42);
*(p+1)=42;
Expand Down
11 changes: 11 additions & 0 deletions regression/cbmc/memory_allocation2/cmdline.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
--bounds-check -DCMDLINE --mmio-regions 0x1000:400,0x11000:400 --mmio-regions 0x21000:400,0x31000:400
^EXIT=10$
^SIGNAL=0$
^\[main\.array_bounds\.[1-5]\] .*: SUCCESS$
^\[main\.array_bounds\.[67]\] line 40 array.buffer (dynamic object )?upper bound in buffers\[\(signed long (long )?int\)0\]->buffer\[\(signed long (long )?int\)100\]: FAILURE$
^\*\* 1 of 12 failed
^VERIFICATION FAILED$
--
^warning: ignoring
5 changes: 5 additions & 0 deletions regression/cbmc/memory_allocation2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ static buffert(*const buffers[4]) = {BUF0, BUF1, BUF2, BUF3};

main()
{
#ifndef CMDLINE
__CPROVER_allocated_memory(BUF0_BASE, sizeof(buffert));
__CPROVER_allocated_memory(BUF1_BASE, sizeof(buffert));
__CPROVER_allocated_memory(BUF2_BASE, sizeof(buffert));
__CPROVER_allocated_memory(BUF3_BASE, sizeof(buffert));
#endif

_cbmc_printf2(sizeof_buffers, sizeof(buffers));
_cbmc_printf2(sizeof_buffers_0, sizeof(buffers[0]));
Expand All @@ -36,4 +38,7 @@ main()
buffers[0]->buffer[0];
buffers[0]->buffer[BUFFER_SIZE - 1];
buffers[0]->buffer[BUFFER_SIZE]; // should be out-of-bounds
buffers[1]->buffer[0];
buffers[2]->buffer[0];
buffers[3]->buffer[0];
}
4 changes: 2 additions & 2 deletions regression/cbmc/memory_allocation2/test.desc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ main.c
^EXIT=10$
^SIGNAL=0$
^\[main\.array_bounds\.[1-5]\] .*: SUCCESS$
^\[main\.array_bounds\.[67]\] line 38 array.buffer (dynamic object )?upper bound in buffers\[\(signed long (long )?int\)0\]->buffer\[\(signed long (long )?int\)100\]: FAILURE$
^\*\* 1 of 6 failed
^\[main\.array_bounds\.[67]\] line 40 array.buffer (dynamic object )?upper bound in buffers\[\(signed long (long )?int\)0\]->buffer\[\(signed long (long )?int\)100\]: FAILURE$
^\*\* 1 of 12 failed
^VERIFICATION FAILED$
--
^warning: ignoring
22 changes: 22 additions & 0 deletions src/ansi-c/goto_check_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Author: Daniel Kroening, kroening@kroening.com
#include <algorithm>
#include <optional>

#include "literals/convert_integer_literal.h"

class goto_check_ct
{
public:
Expand Down Expand Up @@ -76,6 +78,8 @@ class goto_check_ct
error_labels = _options.get_list_option("error-label");
enable_pointer_primitive_check =
_options.get_bool_option("pointer-primitive-check");

parse_mmio_regions(_options.get_list_option("mmio-regions"));
}

typedef goto_functionst::goto_functiont goto_functiont;
Expand Down Expand Up @@ -327,6 +331,8 @@ class goto_check_ct
/// \returns a pair (name, status) if the match succeeds
/// and the name is known, nothing otherwise.
named_check_statust match_named_check(const irep_idt &named_check) const;

void parse_mmio_regions(const optionst::value_listt &regions);
};

/// Allows to:
Expand Down Expand Up @@ -463,6 +469,22 @@ void goto_check_ct::collect_allocations(const goto_functionst &goto_functions)
}
}

void goto_check_ct::parse_mmio_regions(const optionst::value_listt &regions)
{
for(const std::string &range : regions)
{
const auto sep = range.find(':');
if(sep == std::string::npos || sep + 1 == range.size())
continue;

const std::string start = range.substr(0, sep);
const std::string size = range.substr(sep + 1);

allocations.push_back(
{convert_integer_literal(start), convert_integer_literal(size)});
}
}

void goto_check_ct::invalidate(const exprt &lhs)
{
if(lhs.id() == ID_index)
Expand Down
6 changes: 6 additions & 0 deletions src/ansi-c/goto_check_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void goto_check_c(
"(pointer-overflow-check)(conversion-check)(undefined-shift-check)" \
"(float-overflow-check)(nan-check)(no-built-in-assertions)" \
"(pointer-primitive-check)" \
"(mmio-regions):" \
"(retain-trivial-checks)" \
"(error-label):" \
"(no-assertions)(no-assumptions)" \
Expand All @@ -64,6 +65,8 @@ void goto_check_c(
" --nan-check check floating-point for NaN\n" \
" --enum-range-check checks that all enum type expressions have values in the enum range\n" /* NOLINT(whitespace/line_length) */ \
" --pointer-primitive-check checks that all pointers in pointer primitives are valid or null\n" /* NOLINT(whitespace/line_length) */ \
" --mmio-regions addr:sz,... list of start-address:size address ranges\n" \
" permitted for read/write access\n" \
" --retain-trivial-checks include checks that are trivially true\n" \
" --error-label label check that label is unreachable\n" \
" --no-built-in-assertions ignore assertions in built-in library\n" \
Expand All @@ -86,6 +89,9 @@ void goto_check_c(
options.set_option("nan-check", cmdline.isset("nan-check")); \
options.set_option("built-in-assertions", !cmdline.isset("no-built-in-assertions")); /* NOLINT(whitespace/line_length) */ \
options.set_option("pointer-primitive-check", cmdline.isset("pointer-primitive-check")); /* NOLINT(whitespace/line_length) */ \
if(cmdline.isset("mmio-regions")) \
options.set_option( \
"mmio-regions", cmdline.get_comma_separated_values("mmio-regions")); \
options.set_option("retain-trivial-checks", \
cmdline.isset("retain-trivial-checks")); \
options.set_option("assertions", !cmdline.isset("no-assertions")); /* NOLINT(whitespace/line_length) */ \
Expand Down

0 comments on commit fafe18a

Please sign in to comment.