Skip to content

Commit eaa6568

Browse files
peterschrammeltautschnig
authored andcommitted
Set malloc_may_fail through symbol table
Do not fix up the value by tweaking __CPROVER_initialize. This simplifies the code and allows us to regenerate __CPROVER_initialize at any time (at least as far as __CPROVER_malloc_may_fail and __CPROVER_malloc_failure_mode are concerned).
1 parent 0a31a6c commit eaa6568

10 files changed

+58
-104
lines changed

jbmc/src/jbmc/jbmc_parse_options.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,6 @@ int jbmc_parse_optionst::get_goto_program(
721721

722722
goto_modelt &goto_model = dynamic_cast<goto_modelt &>(*goto_model_ptr);
723723

724-
// if we added the ansi-c library models here this should also call
725-
// add_malloc_may_fail_variable_initializations(goto_model);
726-
// here
727-
728724
if(cmdline.isset("validate-goto-model"))
729725
{
730726
goto_model.validate();

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,21 @@ void ansi_c_internal_additions(std::string &code)
188188
CPROVER_PREFIX "size_t size, " CPROVER_PREFIX "bool zero);\n"
189189
"const void *" CPROVER_PREFIX "alloca_object = 0;\n"
190190

191-
"extern int " CPROVER_PREFIX "malloc_failure_mode;\n"
192-
"int " CPROVER_PREFIX "malloc_failure_mode_return_null="+
193-
std::to_string(config.ansi_c.malloc_failure_mode_return_null)+";\n"
194-
"int " CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+
195-
std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n"
191+
"int " CPROVER_PREFIX "thread_local "
192+
CPROVER_PREFIX "malloc_failure_mode=" +
193+
std::to_string(config.ansi_c.malloc_may_fail) + ";\n"
194+
"int " CPROVER_PREFIX "thread_local "
195+
CPROVER_PREFIX "malloc_failure_mode_return_null="+
196+
std::to_string(config.ansi_c.malloc_failure_mode_return_null)+";\n"
197+
"int " CPROVER_PREFIX "thread_local "
198+
CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+
199+
std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n"
196200
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
197201
integer2string(max_malloc_size(config.ansi_c.pointer_width, config
198202
.bv_encoding.object_bits))+";\n"
199-
"extern " CPROVER_PREFIX "bool " CPROVER_PREFIX "malloc_may_fail;\n"
203+
CPROVER_PREFIX "bool " CPROVER_PREFIX "thread_local "
204+
CPROVER_PREFIX "malloc_may_fail=" +
205+
std::to_string(config.ansi_c.malloc_may_fail) + ";\n"
200206

201207
// this is ANSI-C
202208
"extern " CPROVER_PREFIX "thread_local const char __func__["

src/cbmc/cbmc_parse_options.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ Author: Daniel Kroening, [email protected]
5050
#include <goto-checker/stop_on_fail_verifier.h>
5151
#include <goto-checker/stop_on_fail_verifier_with_fault_localization.h>
5252

53-
#include <goto-programs/add_malloc_may_fail_variable_initializations.h>
5453
#include <goto-programs/initialize_goto_model.h>
5554
#include <goto-programs/link_to_library.h>
5655
#include <goto-programs/loop_ids.h>
@@ -893,8 +892,6 @@ bool cbmc_parse_optionst::process_goto_program(
893892
link_to_library(
894893
goto_model, log.get_message_handler(), cprover_c_library_factory);
895894

896-
add_malloc_may_fail_variable_initializations(goto_model);
897-
898895
// Common removal of types and complex constructs
899896
if(::process_goto_program(goto_model, options, log))
900897
return true;

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Author: Daniel Kroening, [email protected]
2222

2323
#include <cpp/cprover_library.h>
2424

25-
#include <goto-programs/add_malloc_may_fail_variable_initializations.h>
2625
#include <goto-programs/initialize_goto_model.h>
2726
#include <goto-programs/link_to_library.h>
2827
#include <goto-programs/process_goto_program.h>
@@ -667,8 +666,6 @@ bool goto_analyzer_parse_optionst::process_goto_program(
667666
link_to_library(goto_model, ui_message_handler, cprover_cpp_library_factory);
668667
link_to_library(goto_model, ui_message_handler, cprover_c_library_factory);
669668

670-
add_malloc_may_fail_variable_initializations(goto_model);
671-
672669
// Common removal of types and complex constructs
673670
if(::process_goto_program(goto_model, options, log))
674671
return true;

src/goto-diff/goto_diff_parse_options.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Author: Peter Schrammel
2020
#include <util/options.h>
2121
#include <util/version.h>
2222

23-
#include <goto-programs/add_malloc_may_fail_variable_initializations.h>
2423
#include <goto-programs/initialize_goto_model.h>
2524
#include <goto-programs/link_to_library.h>
2625
#include <goto-programs/loop_ids.h>
@@ -175,8 +174,6 @@ bool goto_diff_parse_optionst::process_goto_program(
175174
link_to_library(goto_model, ui_message_handler, cprover_cpp_library_factory);
176175
link_to_library(goto_model, ui_message_handler, cprover_c_library_factory);
177176

178-
add_malloc_may_fail_variable_initializations(goto_model);
179-
180177
// Common removal of types and complex constructs
181178
if(::process_goto_program(goto_model, options, log))
182179
return true;

src/goto-programs/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
SRC = allocate_objects.cpp \
2-
add_malloc_may_fail_variable_initializations.cpp \
32
adjust_float_expressions.cpp \
43
builtin_functions.cpp \
54
class_hierarchy.cpp \

src/goto-programs/add_malloc_may_fail_variable_initializations.cpp

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/goto-programs/add_malloc_may_fail_variable_initializations.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/util/config.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,48 @@ static unsigned unsigned_from_ns(
12281228
return numeric_cast_v<unsigned>(int_value);
12291229
}
12301230

1231+
/// Set the values of malloc_may_fail and malloc_failure_mode based on
1232+
/// their entries found in the symbol table, if such entries exist.
1233+
void configt::set_malloc_failure_mode_from_symbol_table(
1234+
const symbol_tablet &symbol_table)
1235+
{
1236+
const auto malloc_may_fail_id = irep_idt{CPROVER_PREFIX "malloc_may_fail"};
1237+
if(!symbol_table.has_symbol(malloc_may_fail_id))
1238+
return;
1239+
1240+
const namespacet ns(symbol_table);
1241+
1242+
const symbolt &malloc_may_fail_sym = ns.lookup(malloc_may_fail_id);
1243+
config.ansi_c.malloc_may_fail =
1244+
numeric_cast_v<int>(to_constant_expr(malloc_may_fail_sym.value)) != 0;
1245+
1246+
const auto malloc_failure_mode_id =
1247+
irep_idt{CPROVER_PREFIX "malloc_failure_mode"};
1248+
const symbolt &malloc_failure_mode_sym = ns.lookup(malloc_failure_mode_id);
1249+
const int parsed_failure_mode =
1250+
numeric_cast_v<int>(to_constant_expr(malloc_failure_mode_sym.value));
1251+
INVARIANT(
1252+
parsed_failure_mode >= 0 && parsed_failure_mode <= 2,
1253+
"unexpected value found for " + id2string(malloc_failure_mode_id));
1254+
// the following is perhaps more verbose than necessary, but compiler
1255+
// warnings will make sure we keep handling all enum values
1256+
switch(parsed_failure_mode)
1257+
{
1258+
case ansi_ct::malloc_failure_modet::malloc_failure_mode_none:
1259+
config.ansi_c.malloc_failure_mode =
1260+
ansi_ct::malloc_failure_modet::malloc_failure_mode_none;
1261+
break;
1262+
case ansi_ct::malloc_failure_modet::malloc_failure_mode_return_null:
1263+
config.ansi_c.malloc_failure_mode =
1264+
ansi_ct::malloc_failure_modet::malloc_failure_mode_return_null;
1265+
break;
1266+
case ansi_ct::malloc_failure_modet::malloc_failure_mode_assert_then_assume:
1267+
config.ansi_c.malloc_failure_mode =
1268+
ansi_ct::malloc_failure_modet::malloc_failure_mode_assert_then_assume;
1269+
break;
1270+
}
1271+
}
1272+
12311273
void configt::set_from_symbol_table(
12321274
const symbol_tablet &symbol_table)
12331275
{
@@ -1284,6 +1326,8 @@ void configt::set_from_symbol_table(
12841326
// lib, string_abstraction not stored in namespace
12851327

12861328
set_object_bits_from_symbol_table(symbol_table);
1329+
1330+
set_malloc_failure_mode_from_symbol_table(symbol_table);
12871331
}
12881332

12891333
/// Sets the number of bits used for object addresses

src/util/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ class configt
334334

335335
private:
336336
void set_classpath(const std::string &cp);
337+
338+
void set_malloc_failure_mode_from_symbol_table(const symbol_tablet &);
337339
};
338340

339341
extern configt config;

0 commit comments

Comments
 (0)