1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
/*******************************************************************\
Module: Unit tests for instantiating generic classes.
Author: Diffblue Ltd.
\*******************************************************************/
#include <java-testing-utils/load_java_class.h>
#include <java-testing-utils/require_goto_statements.h>
#include <java-testing-utils/require_type.h>
#include <testing-utils/require_symbol.h>
#include <testing-utils/use_catch.h>
#include <util/config.h>
// NOTE: To inspect these tests at any point, use expr2java.
// A good way to verify the validity of a test is to iterate
// through all the entry point instructions and print them
// with expr2java.
SCENARIO(
"Instantiate generic parameters to methods or fields used within",
"[core][goto_program_generics][generic_parameters_test]")
{
config.ansi_c.set_LP64();
GIVEN("A class with a generic field")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$SimpleGenericField",
"./java_bytecode/goto_program_generics",
"GenericFields$SimpleGenericField.foo");
WHEN("The method input argument is created in the entry point function")
{
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
// We trace the creation of the object that is being supplied as
// the input to the method under test. There must be one non-null
// assignment only, and usually looks like this:
// this = &tmp_object_factory$1;
const irep_idt &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
ID_this, entry_point_code);
THEN("Object 'this' has field 'field_input' of type Wrapper")
{
const auto &field_input_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field_input",
"java::Wrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'field_input' has field 'field' of type IWrapper")
{
require_goto_statements::require_struct_component_assignment(
field_input_name,
{},
"field",
"java::IWrapper",
{},
entry_point_code,
symbol_table);
}
THEN("Object 'field_input' has field 'array_field' of type IWrapper[]")
{
require_goto_statements::require_struct_array_component_assignment(
field_input_name,
{},
"array_field",
JAVA_REFERENCE_ARRAY_CLASSID,
entry_point_code,
symbol_table);
}
}
}
}
GIVEN("A class with multiple generic fields")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$MultipleGenericFields",
"./java_bytecode/goto_program_generics",
"GenericFields$MultipleGenericFields.foo");
WHEN("The method input argument is created in the entry point function")
{
// Again, the logic here is the same as the first test:
// We trace the this pointer, which is the input to the
// function we have provided as the entry point, and we
// try to trace it down to its components, to make sure that
// the specific fields we are interested in belong to a class with the
// expected type, and that they behave to the expected type protocol
// themselves.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
// The this pointer will be assigned to the object whose class is the
// type we are looking for, and that contains the fields we are looking
// for, so we need to extract its identifier, and start looking for that.
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
ID_this, entry_point_code);
THEN("Type of multiple generic fields should be right")
{
const typet &class_type =
symbol_table.lookup_ref("java::GenericFields$MultipleGenericFields")
.type;
const auto &component = require_type::require_component(
to_java_class_type(class_type), "field_input2");
const java_generic_typet &type =
require_type::require_java_generic_type(component.type());
require_type::require_pointer(
type.generic_type_arguments()[0], struct_tag_typet{"java::BWrapper"});
}
THEN("Object 'this' has field 'field_input1' of type Wrapper")
{
const auto &field_input1_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field_input1",
"java::Wrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'field_input1' has field 'field' of type IWrapper")
{
require_goto_statements::require_struct_component_assignment(
field_input1_name,
{},
"field",
"java::IWrapper",
{},
entry_point_code,
symbol_table);
}
}
THEN("Object 'this' has field 'field_input2' of type Wrapper")
{
const auto &field_input2_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field_input2",
"java::Wrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'field_input2' has field 'field' of type BWrapper")
{
require_goto_statements::require_struct_component_assignment(
field_input2_name,
{},
"field",
"java::BWrapper",
{},
entry_point_code,
symbol_table);
}
}
}
}
GIVEN("A class with a nested generic field")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$NestedGenericFields",
"./java_bytecode/goto_program_generics",
"GenericFields$NestedGenericFields.foo");
WHEN("The method input argument is created in the entry point function")
{
// Same idea as in the other tests: Start tracing the expected types,
// starting from the this pointer, which symbolises the input object for
// the function we have denoted as our entry point above.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
ID_this, entry_point_code);
THEN("Object 'this' has field 'field_input1' of type Wrapper")
{
const auto &field_input1_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field_input1",
"java::Wrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'field_input1' has field 'field' of type Wrapper")
{
const auto &field_name =
require_goto_statements::require_struct_component_assignment(
field_input1_name,
{},
"field",
"java::Wrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'field' has field 'field' of type IWrapper")
{
require_goto_statements::require_struct_component_assignment(
field_name,
{},
"field",
"java::IWrapper",
{},
entry_point_code,
symbol_table);
}
}
}
}
}
GIVEN("A class with a pair generic field")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$PairGenericField",
"./java_bytecode/goto_program_generics",
"GenericFields$PairGenericField.foo");
WHEN("The method input argument is created in the entry point function")
{
// For an explanation of this part, look at the comments for the similar
// parts of the previous tests.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
ID_this, entry_point_code);
THEN("Object 'this' has field 'field_input' of type PairWrapper")
{
const auto &field_input_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field_input",
"java::PairWrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'field_input' has field 'key' of type IWrapper")
{
require_goto_statements::require_struct_component_assignment(
field_input_name,
{},
"first",
"java::IWrapper",
{},
entry_point_code,
symbol_table);
}
THEN("Object 'field_input' has field 'value' of type IWrapper")
{
require_goto_statements::require_struct_component_assignment(
field_input_name,
{},
"second",
"java::IWrapper",
{},
entry_point_code,
symbol_table);
}
}
}
}
GIVEN("A class with a method that accepts a generic type parameter")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$GenericMethodParameter",
"./java_bytecode/goto_program_generics",
"GenericFields$GenericMethodParameter.foo");
WHEN("The method input argument is created in the entry point function")
{
// For an explanation of this part, look at the comments for the similar
// parts of the previous tests.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
// Instead of the this pointer, we need to trace the "v" pointer, which
// is the name of the pointer of the object that gets passed as a
// parameter to the function.
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
"v", entry_point_code);
THEN("Object 'v' is of type Wrapper")
{
const auto &tmp_object_declaration =
require_goto_statements::require_declaration_of_name(
tmp_object_name, entry_point_code);
// Trace the assignments back to the declaration of the generic type
// and verify that it is what we expect.
const auto &tmp_object_struct_tag = to_struct_tag_type(
to_pointer_type(tmp_object_declaration.symbol().type()).base_type());
REQUIRE(tmp_object_struct_tag.get_identifier() == "java::Wrapper");
THEN("Object 'v' has field 'field' of type IWrapper")
{
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field",
"java::IWrapper",
{},
entry_point_code,
symbol_table);
}
}
}
}
GIVEN(
"A class with a method that accepts a generic uninstantiated type "
"parameter")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$GenericMethodUninstantiatedParameter",
"./java_bytecode/goto_program_generics",
"GenericFields$GenericMethodUninstantiatedParameter.foo_unspec");
WHEN("The method input argument is created in the entry point function")
{
// For an explanation of this part, look at the comments for the similar
// parts of the previous tests.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
// Instead of the this pointer, we need to trace the "v" pointer, which
// is the name of the pointer of the object that gets passed as a
// parameter to the function.
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
"v", entry_point_code);
THEN("Object 'v' is of type Wrapper")
{
const auto &tmp_object_declaration =
require_goto_statements::require_declaration_of_name(
tmp_object_name, entry_point_code);
// Trace the assignments back to the declaration of the generic type
// and verify that it is what we expect.
const auto &tmp_object_struct_tag = to_struct_tag_type(
to_pointer_type(tmp_object_declaration.symbol().type()).base_type());
REQUIRE(tmp_object_struct_tag.get_identifier() == "java::Wrapper");
THEN(
"Object 'v' has field 'field' of type Object (upper bound of the "
"uninstantiated parameter T)")
{
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field",
"java::java.lang.Object",
{},
entry_point_code,
symbol_table);
}
}
}
}
GIVEN("A generic class with an inner class (implicitly generic)")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$GenericInnerOuter",
"./java_bytecode/goto_program_generics",
"GenericFields$GenericInnerOuter.foo");
WHEN("The method input argument is created in the entry point function")
{
// For an explanation of this part, look at the comments for the similar
// parts of the previous tests.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
// Instead of the this pointer, we need to trace the "v" pointer, which
// is the name of the pointer of the object that gets passed as a
// parameter to the function.
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
"v", entry_point_code);
THEN("Object 'v' is of type Outer")
{
const auto &tmp_object_declaration =
require_goto_statements::require_declaration_of_name(
tmp_object_name, entry_point_code);
// Trace the assignments back to the declaration of the generic type
// and verify that it is what we expect.
const auto &tmp_object_struct_tag = to_struct_tag_type(
to_pointer_type(tmp_object_declaration.symbol().type()).base_type());
REQUIRE(
tmp_object_struct_tag.get_identifier() ==
"java::GenericFields$GenericInnerOuter$Outer");
THEN("Object 'v' has field 'field' of type InnerClass")
{
const auto &field_tmp_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field",
"java::GenericFields$GenericInnerOuter$Outer$InnerClass",
{},
entry_point_code,
symbol_table);
THEN("Object 'field' has field 't' of type Integer")
{
require_goto_statements::require_struct_component_assignment(
field_tmp_name,
{},
"t",
"java::java.lang.Integer",
{},
entry_point_code,
symbol_table);
}
}
}
}
}
GIVEN(
"A generic class that instantiates its parameter with different type "
"in different scope depth")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFields$GenericRewriteParameter",
"./java_bytecode/goto_program_generics",
"GenericFields$GenericRewriteParameter.foo");
WHEN("The method input argument is created in the entry point function")
{
// For an explanation of this part, look at the comments for the similar
// parts of the previous tests.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
// Instead of the this pointer, we need to trace the "v" pointer, which
// is the name of the pointer of the object that gets passed as a
// parameter to the function.
const auto &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
"v", entry_point_code);
THEN("Object 'v' is of type A")
{
const auto &tmp_object_declaration =
require_goto_statements::require_declaration_of_name(
tmp_object_name, entry_point_code);
// Trace the assignments back to the declaration of the generic type
// and verify that it is what we expect.
const auto &tmp_object_struct_tag = to_struct_tag_type(
to_pointer_type(tmp_object_declaration.symbol().type()).base_type());
REQUIRE(
tmp_object_struct_tag.get_identifier() ==
"java::GenericFields$GenericRewriteParameter$A");
THEN("Object 'v' has field 'value' of type Integer")
{
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"value",
"java::java.lang.Integer",
{},
entry_point_code,
symbol_table);
}
THEN("Object 'v' has field 'field' of type A")
{
const auto &field_tmp_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"field",
"java::GenericFields$GenericRewriteParameter$A",
{},
entry_point_code,
symbol_table);
THEN("Object 'field' has field 'value' of type Boolean")
{
require_goto_statements::require_struct_component_assignment(
field_tmp_name,
{},
"value",
"java::java.lang.Boolean",
{},
entry_point_code,
symbol_table);
}
}
}
}
}
}
SCENARIO(
"Ignore generic parameters in fields and methods with incomplete and "
"non-generic types",
"[core][goto_program_generics][generic_parameters_test]")
{
GIVEN(
"A class with a generic field pointing to a class with unsupported "
"signature (thus not marked as generic)")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFieldUnsupported",
"./java_bytecode/goto_program_generics",
"GenericFieldUnsupported.foo");
THEN("The struct for UnsupportedWrapper2 is complete and non-generic")
{
const std::string field_class_name = "java::UnsupportedWrapper2";
const symbolt &superclass_symbol =
require_symbol::require_symbol_exists(symbol_table, field_class_name);
require_type::require_complete_java_non_generic_class(
superclass_symbol.type);
}
WHEN("The method input argument is created in the entry point function")
{
// We trace the creation of the object that is being supplied as
// the input to the method under test. There must be one non-null
// assignment only, and usually looks like this:
// this = &tmp_object_factory$1;
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
const irep_idt &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
ID_this, entry_point_code);
THEN("Object 'this' has field 'f' of type UnsupportedWrapper2")
{
// tmp_object_factory$1.f = &tmp_object_factory$2;
// struct UnsupportedWrapper2 { struct java.lang.Object
// @java.lang.Object; struct java.lang.Object *field; }
// tmp_object_factory$2;
const auto &field_input_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"f",
"java::UnsupportedWrapper2",
{},
entry_point_code,
symbol_table);
THEN("Object 'f' has unspecialized field 'field'")
{
// tmp_object_factory$2.field = &tmp_object_factory$3;
// struct java.lang.Object { __CPROVER_string @class_identifier; }
// tmp_object_factory$3;
require_goto_statements::require_struct_component_assignment(
field_input_name,
{},
"field",
"java::java.lang.Object",
{},
entry_point_code,
symbol_table);
}
}
}
}
GIVEN(
"A class with a generic field pointing to a mocked class (thus "
"incomplete and not marked as generic)")
{
const symbol_tablet &symbol_table = load_java_class(
"GenericFieldOpaque",
"./java_bytecode/goto_program_generics",
"GenericFieldOpaque.foo");
THEN("The struct for OpaqueWrapper is incomplete and not-generic")
{
const std::string field_class_name = "java::OpaqueWrapper";
const symbolt &field_class_symbol =
require_symbol::require_symbol_exists(symbol_table, field_class_name);
require_type::require_incomplete_class(field_class_symbol.type);
require_type::require_java_non_generic_class(field_class_symbol.type);
}
WHEN("The method input argument is created in the entry point function")
{
// For an explanation of this part, look at the comments for the similar
// parts of the previous tests.
const std::vector<codet> &entry_point_code =
require_goto_statements::require_entry_point_statements(symbol_table);
const irep_idt &tmp_object_name =
require_goto_statements::require_entry_point_argument_assignment(
ID_this, entry_point_code);
THEN("Object 'this' has field 'f' of type OpaqueWrapper")
{
const auto &field_input_name =
require_goto_statements::require_struct_component_assignment(
tmp_object_name,
{},
"f",
"java::OpaqueWrapper",
{},
entry_point_code,
symbol_table);
THEN("Object 'f' has unspecialized field 'field'")
{
require_goto_statements::require_struct_component_assignment(
field_input_name,
{},
"field",
"java::java.lang.Object",
{},
entry_point_code,
symbol_table);
}
}
}
}
}
|