Enforce native type signatures and effects across delegated calls
This commit is contained in:
+49
-5
@@ -1,4 +1,4 @@
|
||||
use crate::frame::StackEntry;
|
||||
use crate::frame::{NativeReturn, StackEntry};
|
||||
use crate::{
|
||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
|
||||
@@ -125,6 +125,7 @@ impl Vm {
|
||||
}
|
||||
|
||||
fn clear_frame(&mut self, frame: EndorFrame) {
|
||||
self.clear_native_returns(frame.native_returns);
|
||||
for entry in frame.stack {
|
||||
if let StackEntry::Value(value) = entry {
|
||||
self.drop_root(value);
|
||||
@@ -204,6 +205,7 @@ impl Vm {
|
||||
}
|
||||
let mut frame = EndorFrame {
|
||||
return_type,
|
||||
native_returns: Vec::new(),
|
||||
function,
|
||||
previous: None,
|
||||
code: data.code(),
|
||||
@@ -273,7 +275,15 @@ impl Vm {
|
||||
if let Some(callable) = self.object(function)?.payload().native_callable() {
|
||||
return NativeContext::execute(&mut self.objects, function, arguments, callable);
|
||||
}
|
||||
let signature = self.objects.native_signature(function)?;
|
||||
let name = self.object(function)?.payload().kind_name().to_owned();
|
||||
if let Some(signature) = &signature {
|
||||
signature.check_arguments(&self.objects, &name, arguments)?;
|
||||
}
|
||||
let result = self.object(function)?.payload().call(arguments)?;
|
||||
if let Some(signature) = &signature {
|
||||
signature.check_return(&self.objects, &name, &result)?;
|
||||
}
|
||||
self.retain_value(&result)?;
|
||||
Ok(NativeResult::Return(result))
|
||||
}
|
||||
@@ -286,6 +296,7 @@ impl Vm {
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<PreparedCall, VmError> {
|
||||
let mut owned = false;
|
||||
let mut returns = Vec::new();
|
||||
let result = (|| loop {
|
||||
if self
|
||||
.object(function)?
|
||||
@@ -293,16 +304,36 @@ impl Vm {
|
||||
.as_any()
|
||||
.is::<EndorFunctionObject>()
|
||||
{
|
||||
return self
|
||||
.prepare_frame(function, &arguments)
|
||||
.map(PreparedCall::Frame);
|
||||
let mut frame = self.prepare_frame(function, &arguments)?;
|
||||
frame.native_returns = std::mem::take(&mut returns);
|
||||
return Ok(PreparedCall::Frame(frame));
|
||||
}
|
||||
if *executed >= limits.max_instructions {
|
||||
return Err(VmError::ExecutionLimit("instructions"));
|
||||
}
|
||||
*executed += 1;
|
||||
if let Some(signature) = self.objects.native_signature(function)?
|
||||
&& !matches!(signature.returns, crate::AnnotationType::Any)
|
||||
{
|
||||
let check = NativeReturn {
|
||||
name: self.object(function)?.payload().kind_name().into(),
|
||||
expected: signature.returns,
|
||||
};
|
||||
if let Some(reference) = check.expected.reference() {
|
||||
self.retain(reference)?;
|
||||
}
|
||||
returns.push(check);
|
||||
}
|
||||
match self.invoke_native(function, &arguments)? {
|
||||
NativeResult::Return(value) => return Ok(PreparedCall::Value(value)),
|
||||
NativeResult::Return(value) => {
|
||||
for check in returns.iter().rev() {
|
||||
if let Err(error) = check.check(&self.objects, &value) {
|
||||
self.drop_root(value);
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
return Ok(PreparedCall::Value(value));
|
||||
}
|
||||
NativeResult::Invoke {
|
||||
function: next,
|
||||
arguments: values,
|
||||
@@ -319,6 +350,7 @@ impl Vm {
|
||||
}
|
||||
}
|
||||
})();
|
||||
self.clear_native_returns(returns);
|
||||
if owned {
|
||||
self.release(function)
|
||||
.expect("delegated call owns its function");
|
||||
@@ -329,6 +361,15 @@ impl Vm {
|
||||
result
|
||||
}
|
||||
|
||||
fn clear_native_returns(&mut self, returns: Vec<NativeReturn>) {
|
||||
for check in returns {
|
||||
if let Some(reference) = check.expected.reference() {
|
||||
self.release(reference)
|
||||
.expect("native return check owns its type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stack_value(frame: &EndorFrame, index: usize) -> Result<Value, VmError> {
|
||||
match frame.stack.get(index) {
|
||||
Some(StackEntry::Value(value)) => Ok(value.clone()),
|
||||
@@ -681,6 +722,9 @@ impl Vm {
|
||||
actual: self.objects.value_type_name(&result)?,
|
||||
});
|
||||
}
|
||||
for check in frame.native_returns.iter().rev() {
|
||||
check.check(&self.objects, &result)?;
|
||||
}
|
||||
frame.stack.pop();
|
||||
self.clear_frame(frames.pop().unwrap());
|
||||
if let Some(caller) = frames.last_mut() {
|
||||
|
||||
Reference in New Issue
Block a user