Skip to content

Commit

Permalink
[compiler-v2] Add missing ability check of receiver function type pa…
Browse files Browse the repository at this point in the history
…rameters (#14987)

* Adding better error message for bytecode verifier ability check, plus displaying message in compiler pipeline.

This also demonstrates the problem with bug #14813

* [compiler-v2] Add missing ability check of receiver function type parameters

Closes #14813

The first commit here demonstrates the error and improves on how we display bytecode verification errors, the 2nd fixes the issue in the compiler.
  • Loading branch information
wrwg authored Oct 18, 2024
1 parent 87348fc commit 1645c96
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 5 deletions.
8 changes: 7 additions & 1 deletion third_party/move/move-bytecode-verifier/src/signature_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,13 @@ impl<'a, const N: usize> SignatureChecker<'a, N> {
for (offset, instr) in code.code.iter().enumerate() {
let map_err = |res: PartialVMResult<()>| {
res.map_err(|err| {
err.append_message_with_separator(' ', format!("at offset {}", offset))
err.append_message_with_separator(
' ',
format!(
"missing abilities for `{:?}` at code offset {}",
instr, offset
),
)
})
};
match instr {
Expand Down
5 changes: 4 additions & 1 deletion third_party/move/move-compiler-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,10 @@ fn report_bytecode_verification_error(
let debug_info = if command_line::get_move_compiler_backtrace_from_env() {
format!("\n{:#?}", e)
} else {
"".to_string()
format!(
"\nError message: {}",
e.message().cloned().unwrap_or_else(|| "none".to_string())
)
};
env.diag(
Severity::Bug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ B0:

Diagnostics:
bug: bytecode verification failed with unexpected status code `EQUALITY_OP_TYPE_MISMATCH_ERROR`. This is a compiler bug, consider reporting it.
Error message: none
┌─ tests/bytecode-verify-failure/equality.move:3:9
3 │ x == y
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

Diagnostics:
error: type `V` is missing required ability `drop`
┌─ tests/checking/abilities/bug_14813_fun_call_ability.move:6:9
6 │ self.search(key)
│ ^^^^^^^^^^^^^^^^
·
9 │ fun search<V: drop>(self: &Map<V>, key: u256): bool {
│ - declaration of type parameter `V`
= required by instantiating type parameter `V:drop` of function `search`
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module 0x815::red_black_map {

struct Map<phantom V> has drop {}

public fun contains<V>(self: &Map<V>, key: u256): bool {
self.search(key)
}

fun search<V: drop>(self: &Map<V>, key: u256): bool {
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Diagnostics:
bug: bytecode verification failed with unexpected status code `READREF_EXISTS_MUTABLE_BORROW_ERROR`. This is a compiler bug, consider reporting it.
Error message: none
┌─ tests/reference-safety/bug_12817_order_dependency.move:14:9
14 │ *r2 + *r1 // <- changed order here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Diagnostics:
bug: bytecode verification failed with unexpected status code `WRITEREF_EXISTS_BORROW_ERROR`. This is a compiler bug, consider reporting it.
Error message: none
┌─ tests/reference-safety/bug_13149.move:7:9
7 │ *r2 = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Diagnostics:
bug: bytecode verification failed with unexpected status code `CALL_BORROWED_MUTABLE_REFERENCE_ERROR`. This is a compiler bug, consider reporting it.
Error message: none
┌─ tests/reference-safety/bug_13976.move:12:13
12 │ lifted_lambda(&mut a, z))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Diagnostics:
bug: bytecode verification failed with unexpected status code `WRITEREF_EXISTS_BORROW_ERROR`. This is a compiler bug, consider reporting it.
Error message: none
┌─ tests/reference-safety/v1-borrow-tests/mutable_borrow_local_twice_invalid.move:6:9
6 │ *r2 = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Diagnostics:
bug: bytecode verification failed with unexpected status code `WRITEREF_EXISTS_BORROW_ERROR`. This is a compiler bug, consider reporting it.
Error message: none
┌─ tests/reference-safety/v1-borrow-tests/writeref_borrow_invalid.move:10:18
10 │ *g_mut = G { v: 0 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ comparison between v1 and v2 failed:
+ task 0 'publish'. lines 1-15:
+ Error: compilation errors:
+ bug: bytecode verification failed with unexpected status code `VALUE_STACK_OVERFLOW`. This is a compiler bug, consider reporting it.
+ Error message: none
+ ┌─ TEMPFILE:5:24
+ │
+ 5 │ let v = vector[0, 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, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ comparison between v1 and v2 failed:
+ task 0 'publish'. lines 1-15:
+ Error: compilation errors:
+ bug: bytecode verification failed with unexpected status code `VALUE_STACK_OVERFLOW`. This is a compiler bug, consider reporting it.
+ Error message: none
+ ┌─ TEMPFILE:5:24
+ │
+ 5 │ let v = vector[0, 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, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026];
Expand Down
11 changes: 8 additions & 3 deletions third_party/move/move-model/src/builder/exp_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,8 @@ impl<'env, 'builder, 'module_builder> UnificationContext
.lookup_receiver_function(ty, name)
.cloned()
{
let type_inst = self.fresh_type_vars(entry.type_params.len());
let type_params = entry.type_params.clone();
let type_inst = self.fresh_type_vars(type_params.len());
let arg_types = entry
.params
.iter()
Expand All @@ -794,6 +795,8 @@ impl<'env, 'builder, 'module_builder> UnificationContext
let result_type = entry.result_type.instantiate(&type_inst);
Some(ReceiverFunctionInstance {
id: entry.module_id.qualified(entry.fun_id),
fun_name: name,
type_params,
type_inst,
arg_types,
result_type,
Expand Down Expand Up @@ -2159,8 +2162,8 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
&inst.result_type,
)
.map_err(|_| ok = false);
// `type.inst` is now unified with the actual types,
// annotate the instance. Since this post processor
// `type_inst` is now unified with the actual types,
// annotate the instance. Since this post processor
// is run after type finalization, we need to finalize
// it to report any un-inferred type errors. However,
// to avoid follow-up errors, only do if unification
Expand All @@ -2173,6 +2176,8 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
.iter()
.map(|t| self.finalize_type(id, t, &mut BTreeSet::new()))
.collect();
// Also need to evaluate type constraints on type parameters

self.env().set_node_instantiation(id, inst)
}
}
Expand Down
24 changes: 24 additions & 0 deletions third_party/move/move-model/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,10 @@ pub trait UnificationContext: AbilityContext {
pub struct ReceiverFunctionInstance {
/// Qualified id
pub id: QualifiedId<FunId>,
/// Function name
pub fun_name: Symbol,
/// Type parameters
pub type_params: Vec<TypeParameter>,
/// Type instantiation of the function
pub type_inst: Vec<Type>,
/// Types of the arguments, instantiated
Expand Down Expand Up @@ -1789,6 +1793,7 @@ impl Substitution {
if let Some(receiver) = context.get_receiver_function(ty, *name) {
self.eval_receiver_function_constraint(
context,
loc,
variance,
ty_args_opt,
args_loc,
Expand Down Expand Up @@ -1968,6 +1973,7 @@ impl Substitution {
fn eval_receiver_function_constraint(
&mut self,
context: &mut impl UnificationContext,
loc: &Loc,
variance: Variance,
ty_args_opt: &Option<(Vec<Loc>, Vec<Type>)>,
args_loc: &[Loc],
Expand Down Expand Up @@ -1995,6 +2001,24 @@ impl Substitution {
&receiver.type_inst,
)?;
}
// Need to add any constraints for type parameters.
for (tparam, targ) in receiver.type_params.iter().zip(&receiver.type_inst) {
for ctr in Constraint::for_type_parameter(tparam) {
self.eval_constraint(
context,
loc,
&self.specialize(targ),
Variance::NoVariance,
WideningOrder::LeftToRight,
ctr,
Some(ConstraintContext::default().for_type_param(
false,
receiver.fun_name,
tparam.clone(),
)),
)?
}
}
self.unify_vec(
context,
variance,
Expand Down

0 comments on commit 1645c96

Please sign in to comment.