feat: protect existing objects during read-only calls and track reads
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出、宿主调用与字节码 rustdoc 示例 |
|
||||
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 注册显式委托给 objects |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、参数绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
|
||||
|
||||
## 常见修改入口
|
||||
|
||||
|
||||
@@ -48,6 +48,21 @@ impl Vm {
|
||||
self.call_with_limits(function, arguments, ExecutionLimits::default())
|
||||
}
|
||||
|
||||
/// Allow result allocation while rejecting mutations of objects that were
|
||||
/// already alive on entry. All reads share the call's normal frame budget.
|
||||
/// Returned read handles are borrowed dependency identities, not owned roots.
|
||||
pub fn call_read_only_with_limits(
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
arguments: &[Value],
|
||||
limits: ExecutionLimits,
|
||||
) -> Result<(Value, Vec<ObjectRef>), VmError> {
|
||||
let scope = self.objects.begin_read_only();
|
||||
let result = self.call_with_limits(function, arguments, limits);
|
||||
let reads = self.objects.end_read_only(scope)?;
|
||||
result.map(|value| (value, reads))
|
||||
}
|
||||
|
||||
pub fn call_with_limits(
|
||||
&mut self,
|
||||
function: ObjectRef,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
|
||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
|
||||
| [types.rs](types.rs) | 名义身份、继承与实例关系、类型边保活/循环回收、只读类型元数据与外来句柄;仅远端执行 |
|
||||
| [native_context.rs](native_context.rs) | 原生作用域根、分配失败清理、委托、弱引用过期与有界变更;仅远端执行 |
|
||||
| [native_context.rs](native_context.rs) | 原生作用域根、分配失败清理、委托、只读结果分配/写入拒绝/依赖与失败恢复、弱引用过期及有界变更;仅远端执行 |
|
||||
|
||||
核心对象现由 objects 定义,本目录保留既有 endor_core 导入与 Vm 方法的兼容性检查。独立对象存储检查见 [objects/tests](../../objects/tests/README.md)。堆内部伪造句柄与计数溢出检查在 objects 的 src/heap.rs,公开示例由 src/lib.rs rustdoc 提供。文件职责见 [src](../src/README.md),检查方式和结果边界见 [验证](../../../docs/operations/validation.md)。本地不执行测试;存在测试文件不代表某次提交已通过。返回 [core](../README.md)。
|
||||
|
||||
|
||||
@@ -148,3 +148,77 @@ fn notification_overflow_requires_rescan_instead_of_losing_changes_silently() {
|
||||
assert_eq!(vm.take_changes().changes.len(), 1);
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ReadResult {
|
||||
source: ObjectRef,
|
||||
mutate: bool,
|
||||
}
|
||||
impl NativeCallable for ReadResult {
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, _: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let value = cx.store().get_attr(self.source, "value")?;
|
||||
if self.mutate {
|
||||
cx.set_attr(self.source, "value", Value::Number(99_i64.into()))?;
|
||||
}
|
||||
let result = cx.new_object()?;
|
||||
cx.set_attr(result, "copied", value)?;
|
||||
Ok(NativeResult::Return(Value::Object(result)))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_only_calls_track_borrowed_dependencies_allow_results_and_restore_after_error() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let source = vm.new_object().unwrap();
|
||||
vm.set_attr(source, "value", Value::Number(7_i64.into()))
|
||||
.unwrap();
|
||||
let function = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"read result",
|
||||
ReadResult {
|
||||
source,
|
||||
mutate: false,
|
||||
},
|
||||
vec![source],
|
||||
))
|
||||
.unwrap();
|
||||
vm.take_changes();
|
||||
let (value, reads) = vm
|
||||
.call_read_only_with_limits(function, &[], ExecutionLimits::default())
|
||||
.unwrap();
|
||||
assert!(reads.contains(&source));
|
||||
assert!(vm.take_changes().changes.is_empty());
|
||||
let Value::Object(result) = value else {
|
||||
panic!("result")
|
||||
};
|
||||
assert_eq!(
|
||||
vm.get_attr(result, "copied").unwrap(),
|
||||
Value::Number(7_i64.into())
|
||||
);
|
||||
vm.release(result).unwrap();
|
||||
vm.replace_payload(
|
||||
function,
|
||||
ContextNativeFunction::new(
|
||||
"illegal write",
|
||||
ReadResult {
|
||||
source,
|
||||
mutate: true,
|
||||
},
|
||||
vec![source],
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
vm.call_read_only_with_limits(function, &[], ExecutionLimits::default())
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
vm.get_attr(source, "value").unwrap(),
|
||||
Value::Number(7_i64.into())
|
||||
);
|
||||
vm.set_attr(source, "value", Value::Number(8_i64.into()))
|
||||
.unwrap();
|
||||
vm.release(function).unwrap();
|
||||
vm.release(source).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user