| 
 | 1 | +/*******************************************************************\  | 
 | 2 | +
  | 
 | 3 | + Module: Tests for the replace nondet pass to make sure it works both  | 
 | 4 | +         when remove_returns has been run before and after the call.  | 
 | 5 | +
  | 
 | 6 | + Author: Diffblue Ltd.  | 
 | 7 | +
  | 
 | 8 | +\*******************************************************************/  | 
 | 9 | + | 
 | 10 | +#include <testing-utils/catch.hpp>  | 
 | 11 | +#include <testing-utils/load_java_class.h>  | 
 | 12 | + | 
 | 13 | +#include <goto-programs/goto_convert_functions.h>  | 
 | 14 | +#include <goto-programs/remove_virtual_functions.h>  | 
 | 15 | +#include <util/config.h>  | 
 | 16 | +#include <goto-instrument/cover.h>  | 
 | 17 | +#include <util/options.h>  | 
 | 18 | +#include <iostream>  | 
 | 19 | +#include <goto-programs/remove_returns.h>  | 
 | 20 | +#include <goto-programs/replace_java_nondet.h>  | 
 | 21 | +#include <goto-programs/remove_instanceof.h>  | 
 | 22 | + | 
 | 23 | +void validate_method_removal(  | 
 | 24 | +  std::list<goto_programt::instructiont> instructions)  | 
 | 25 | +{  | 
 | 26 | +  bool method_removed = true, replacement_nondet_exists = false;  | 
 | 27 | + | 
 | 28 | +  // Quick loop to check that our method call has been replaced.  | 
 | 29 | +  for(const auto &inst : instructions)  | 
 | 30 | +  {  | 
 | 31 | +    if(inst.is_assign())  | 
 | 32 | +    {  | 
 | 33 | +      const code_assignt &assignment = to_code_assign(inst.code);  | 
 | 34 | +      if (assignment.rhs().id() == ID_side_effect)  | 
 | 35 | +      {  | 
 | 36 | +        const side_effect_exprt &see = to_side_effect_expr(assignment.rhs());  | 
 | 37 | +        if(see.get_statement() == ID_nondet)  | 
 | 38 | +        {  | 
 | 39 | +          replacement_nondet_exists = true;  | 
 | 40 | +        }  | 
 | 41 | +      }  | 
 | 42 | +    }  | 
 | 43 | + | 
 | 44 | +    if(inst.is_function_call())  | 
 | 45 | +    {  | 
 | 46 | +      const code_function_callt &function_call =  | 
 | 47 | +        to_code_function_call(inst.code);  | 
 | 48 | + | 
 | 49 | +      // Small check to make sure the instruction is a symbol.  | 
 | 50 | +      if(function_call.function().id() != ID_symbol)  | 
 | 51 | +        continue;  | 
 | 52 | + | 
 | 53 | +      const irep_idt function_id =  | 
 | 54 | +        to_symbol_expr(function_call.function()).get_identifier();  | 
 | 55 | +      if(  | 
 | 56 | +        function_id ==  | 
 | 57 | +        "java::org.cprover.CProver.nondetWithoutNull:()"  | 
 | 58 | +        "Ljava/lang/Object;")  | 
 | 59 | +      {  | 
 | 60 | +        method_removed = false;  | 
 | 61 | +      }  | 
 | 62 | +    }  | 
 | 63 | +  }  | 
 | 64 | + | 
 | 65 | +  REQUIRE(method_removed);  | 
 | 66 | +  REQUIRE(replacement_nondet_exists);  | 
 | 67 | +}  | 
 | 68 | + | 
 | 69 | +TEST_CASE(  | 
 | 70 | +  "Load class with a generated java nondet method call, run remove returns "  | 
 | 71 | +  "both before and after the nondet statements have been removed, check results "  | 
 | 72 | +  "are as expected.", "[core][java_bytecode][replace_nondet]")  | 
 | 73 | +{  | 
 | 74 | +  GIVEN("A class with a call to CProver.nondetWithoutNull()")  | 
 | 75 | +  {  | 
 | 76 | +    symbol_tablet raw_symbol_table = load_java_class(  | 
 | 77 | +      "Main", "./java_bytecode/java_replace_nondet", "Main.replaceNondet");  | 
 | 78 | + | 
 | 79 | +    journalling_symbol_tablet symbol_table =  | 
 | 80 | +      journalling_symbol_tablet::wrap(raw_symbol_table);  | 
 | 81 | + | 
 | 82 | +    null_message_handlert null_output;  | 
 | 83 | +    goto_functionst functions;  | 
 | 84 | +    goto_convert(symbol_table, functions, null_output);  | 
 | 85 | + | 
 | 86 | +    const std::string function_name = "java::Main.replaceNondet:()V";  | 
 | 87 | +    goto_functionst::goto_functiont &goto_function =  | 
 | 88 | +      functions.function_map.at(function_name);  | 
 | 89 | + | 
 | 90 | +    goto_model_functiont model_function(  | 
 | 91 | +      symbol_table, functions, function_name, goto_function);  | 
 | 92 | + | 
 | 93 | +    remove_instanceof(goto_function, symbol_table);  | 
 | 94 | + | 
 | 95 | +    remove_virtual_functions(model_function);  | 
 | 96 | + | 
 | 97 | +    WHEN("Remove returns is called before java nondet.")  | 
 | 98 | +    {  | 
 | 99 | +      remove_returns(model_function, [](const irep_idt &) { return false; });  | 
 | 100 | + | 
 | 101 | +      replace_java_nondet(functions);  | 
 | 102 | + | 
 | 103 | +      THEN("The nondet method call should have been removed.")  | 
 | 104 | +      {  | 
 | 105 | +        validate_method_removal(goto_function.body.instructions);  | 
 | 106 | +      }  | 
 | 107 | +    }  | 
 | 108 | + | 
 | 109 | +    WHEN("Remove returns is called after java nondet.")  | 
 | 110 | +    {  | 
 | 111 | +      replace_java_nondet(functions);  | 
 | 112 | + | 
 | 113 | +      remove_returns(model_function, [](const irep_idt &) { return false; });  | 
 | 114 | + | 
 | 115 | +      THEN("The nondet method call should have been removed.")  | 
 | 116 | +      {  | 
 | 117 | +        validate_method_removal(goto_function.body.instructions);  | 
 | 118 | +      }  | 
 | 119 | +    }  | 
 | 120 | +  }  | 
 | 121 | +}  | 
0 commit comments