Skip to content

Commit b7f0b6c

Browse files
committed
fix: explicit constraint violations.
1 parent 95c1227 commit b7f0b6c

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

aptos-move/aptos-vm/src/aptos_vm.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,11 @@ impl AptosVM {
750750
let module_id = traversal_context
751751
.referenced_module_ids
752752
.alloc(entry_fn.module().clone());
753-
session.check_dependencies_and_charge_gas(gas_meter, traversal_context, [(
754-
module_id.address(),
755-
module_id.name(),
756-
)])?;
753+
session.check_dependencies_and_charge_gas(
754+
gas_meter,
755+
traversal_context,
756+
[(module_id.address(), module_id.name())],
757+
)?;
757758
}
758759

759760
let function =
@@ -1318,7 +1319,14 @@ impl AptosVM {
13181319
)?;
13191320
} else {
13201321
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
1321-
.finish(Location::Undefined));
1322+
.with_message(format!(
1323+
"initializer not found in module '{}'",
1324+
module.self_id().name()
1325+
))
1326+
.finish(Location::Constraint(format!(
1327+
"module must have an initializer function name '{}'",
1328+
init_func_name
1329+
))));
13221330
}
13231331
}
13241332
}
@@ -1541,7 +1549,9 @@ impl AptosVM {
15411549
fn metadata_validation_error(msg: &str) -> VMError {
15421550
PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
15431551
.with_message(format!("metadata and code bundle mismatch: {}", msg))
1544-
.finish(Location::Undefined)
1552+
.finish(Location::Constraint(
1553+
"metadata must match the code bundle".to_string(),
1554+
))
15451555
}
15461556

15471557
fn validate_signed_transaction(

aptos-move/aptos-vm/src/verifier/resource_groups.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ fn metadata_validation_err(msg: &str) -> Result<(), VMError> {
2222
fn metadata_validation_error(msg: &str) -> VMError {
2323
PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
2424
.with_message(format!("metadata and code bundle mismatch: {}", msg))
25-
.finish(Location::Undefined)
25+
.finish(Location::Constraint(
26+
"metadata must match code bundle".to_string(),
27+
))
2628
}
2729

2830
/// Perform validation and upgrade checks on resource groups

third_party/move/move-binary-format/src/errors.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub enum Location {
2121
Undefined,
2222
Script,
2323
Module(ModuleId),
24+
Constraint(String),
2425
}
2526

2627
/// A representation of the execution state (e.g., stack trace) at an
@@ -103,6 +104,21 @@ impl VMError {
103104
message,
104105
};
105106
},
107+
Location::Constraint(constraint) => {
108+
let message_string = message.unwrap_or_else(|| "opaque error".to_string());
109+
110+
return VMStatus::Error {
111+
status_code: major_status,
112+
sub_status,
113+
message: Some(
114+
format!(
115+
"constraint violation: {} with: {}",
116+
constraint, message_string
117+
)
118+
.to_string(),
119+
),
120+
};
121+
},
106122
};
107123
// Errors for OUT_OF_GAS do not always have index set: if it does not, it should already return above.
108124
debug_assert!(
@@ -246,6 +262,7 @@ impl VMError {
246262
Location::Module(id) => {
247263
format!("0x{}::{}", id.address().short_str_lossless(), id.name())
248264
},
265+
Location::Constraint(constraint) => format!("constraint: {}", constraint),
249266
};
250267
let indices = if comparison_mode {
251268
// During comparison testing, abstract this data.
@@ -532,6 +549,7 @@ impl fmt::Display for Location {
532549
Location::Undefined => write!(f, "UNDEFINED"),
533550
Location::Script => write!(f, "Script"),
534551
Location::Module(id) => write!(f, "Module {:?}", id),
552+
Location::Constraint(constraint) => write!(f, "Constraint {}", constraint),
535553
}
536554
}
537555
}

third_party/move/move-bytecode-verifier/src/signature_v2.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ impl<const N: usize> BitsetTypeParameterConstraints<N> {
9898
fn check_in_context(&self, context: &Self) -> PartialVMResult<()> {
9999
for i in 0..N {
100100
if self.words[i] | context.words[i] != context.words[i] {
101-
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED));
101+
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
102+
.with_message(format!(
103+
"expected abilities {:?} got abilities {:?}",
104+
self.words[i], context.words[i]
105+
)));
102106
}
103107
}
104108

third_party/move/move-compiler/src/unit_test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl<'a> fmt::Display for ExpectedMoveErrorDisplay<'a> {
166166
Location::Undefined => write!(f, " in an unknown location"),
167167
Location::Script => write!(f, " in the script"),
168168
Location::Module(id) => write!(f, " in the module {id}"),
169+
Location::Constraint(constraint) => write!(f, " w.r.t. the constraint {constraint}"),
169170
}
170171
}
171172
}

third_party/move/move-vm/types/src/loaded_data/runtime_types.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ impl Type {
326326
}
327327
for (ty, expected_ability_set) in ty_args.iter().zip(ty_param_abilities) {
328328
if !expected_ability_set.is_subset(ty.abilities()?) {
329-
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED));
329+
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
330+
.with_message(format!(
331+
"type {} does not satisfy the expected abilities set {}",
332+
ty, expected_ability_set
333+
)));
330334
}
331335
}
332336
Ok(())
@@ -546,11 +550,11 @@ impl Type {
546550
"Unexpected TyParam type after translating from TypeTag to Type".to_string(),
547551
)),
548552

549-
Type::Vector(ty) => {
550-
AbilitySet::polymorphic_abilities(AbilitySet::VECTOR, vec![false], vec![
551-
ty.abilities()?
552-
])
553-
},
553+
Type::Vector(ty) => AbilitySet::polymorphic_abilities(
554+
AbilitySet::VECTOR,
555+
vec![false],
556+
vec![ty.abilities()?],
557+
),
554558
Type::Struct { ability, .. } => Ok(ability.base_ability_set),
555559
Type::StructInstantiation {
556560
ty_args,

0 commit comments

Comments
 (0)