Enforce native type signatures and effects across delegated calls
This commit is contained in:
@@ -4,10 +4,10 @@
|
||||
|
||||
| 文件 | 职责 |
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出(含 ReadAccess)、宿主调用与字节码 rustdoc 示例 |
|
||||
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出(含 ReadAccess/CallSignature/CallEffect)、宿主调用与字节码 rustdoc 示例 |
|
||||
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 和显式Trait注册/关系查询显式委托给 objects;函数/字段检查统一开关、统一参数区间查询与函数类型配置 |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、返回类型执行根、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、原生实现前数量检查、可选注解检查、源码类字段环境绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、源码与原生委托返回契约执行根、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、原生实现前数量/类型/效果检查和委托返回检查、可选注解检查、源码类字段环境绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||
|
||||
## 常见修改入口
|
||||
|
||||
|
||||
+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() {
|
||||
|
||||
@@ -10,12 +10,32 @@ pub(crate) enum StackEntry {
|
||||
Value(Value),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NativeReturn {
|
||||
pub name: String,
|
||||
pub expected: crate::AnnotationType,
|
||||
}
|
||||
impl NativeReturn {
|
||||
pub fn check(&self, store: &objects::ObjectStore, value: &Value) -> Result<(), crate::VmError> {
|
||||
if !store.matches_annotation(value, self.expected)? {
|
||||
return Err(crate::VmError::TypeMismatch {
|
||||
function: self.name.clone(),
|
||||
position: "return".into(),
|
||||
expected: store.annotation_type_name(self.expected)?,
|
||||
actual: store.value_type_name(value)?,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal activation record, analogous to _PyInterpreterFrame. It has no
|
||||
/// EndorObject header; introspection objects can be added independently later.
|
||||
#[derive(Debug)]
|
||||
pub struct EndorFrame {
|
||||
pub(crate) function: ObjectRef,
|
||||
pub(crate) return_type: Option<crate::AnnotationType>,
|
||||
pub(crate) native_returns: Vec<NativeReturn>,
|
||||
/// Index of the caller in the execution's frame vector, never a pointer.
|
||||
pub(crate) previous: Option<usize>,
|
||||
pub(crate) code: ObjectRef,
|
||||
|
||||
@@ -50,12 +50,13 @@ pub use objects::{
|
||||
TypeConstructor,
|
||||
};
|
||||
pub use objects::{
|
||||
CallArity, ChangeBatch, ChangeKey, CodeLayout, CompareOp, ContextNativeFunction,
|
||||
DecodedInstruction, EndorBaseExceptionObject, EndorCellObject, EndorCode, EndorCodeObject,
|
||||
EndorDecimal, EndorDictObject, EndorFunctionObject, EndorInteger, EndorNativeCall,
|
||||
EndorNativeFunctionObject, EndorNumber, EndorNumberObject, EndorObject, EndorRational,
|
||||
EndorReal, EndorStringObject, EndorTracebackEntry, Instruction, NativeCall, NativeCallable,
|
||||
NativeContext, NativeFunction, NativeResult, ObjectChange, ObjectHeader, ObjectPayload,
|
||||
ObjectRef, Opcode, ReadAccess, Record, SourceSignature, Value, VmError, WeakObjectRef,
|
||||
CallArity, CallEffect, CallSignature, ChangeBatch, ChangeKey, CodeLayout, CompareOp,
|
||||
ContextNativeFunction, DecodedInstruction, EndorBaseExceptionObject, EndorCellObject,
|
||||
EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject, EndorFunctionObject, EndorInteger,
|
||||
EndorNativeCall, EndorNativeFunctionObject, EndorNumber, EndorNumberObject, EndorObject,
|
||||
EndorRational, EndorReal, EndorStringObject, EndorTracebackEntry, Instruction, NativeCall,
|
||||
NativeCallable, NativeContext, NativeFunction, NativeResult, ObjectChange, ObjectHeader,
|
||||
ObjectPayload, ObjectRef, Opcode, ReadAccess, Record, SourceSignature, Value, VmError,
|
||||
WeakObjectRef,
|
||||
};
|
||||
pub use vm::Vm;
|
||||
|
||||
Reference in New Issue
Block a user