feat: protect existing objects during read-only calls and track reads
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
| [changes.rs](changes.rs) | 合并变更批次、变更键与有界通知额度 |
|
||||
| [number.rs](number.rs) | Integer/Decimal/Rational/BinaryFloat/Complex 的无损表示和装箱载荷 |
|
||||
| [builtins.rs](builtins.rs) | String、Dict、Cell、Function、BaseException 载荷与 trace 边 |
|
||||
| [heap.rs](heap.rs) | 私有 Heap、不可伪造 ObjectRef、稳定 Box、引用事务、迭代释放、循环回收及私有边界测试 |
|
||||
| [heap.rs](heap.rs) | 私有 Heap、不可伪造 ObjectRef、稳定 Box、引用事务、迭代释放、循环回收、只读变更屏障/读取跟踪及私有边界测试 |
|
||||
| [store.rs](store.rs) | ObjectStore、空堆创建、对象操作、retain/release 和回收入口 |
|
||||
| [store_builtins.rs](store_builtins.rs) | 载荷校验、代码/函数工厂、Dict/Cell 操作及键语义 |
|
||||
| [instruction.rs](instruction.rs) | Opcode、Instruction、wordcode 编解码、MakeFunction/比较参数与跳转读取接口 |
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::fmt;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use crate::changes::MAX_PENDING_CHANGES;
|
||||
@@ -34,6 +35,12 @@ struct Slot {
|
||||
object: Option<Box<EndorObject>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ReadOnly {
|
||||
protected: BTreeSet<ObjectRef>,
|
||||
reads: Mutex<BTreeSet<ObjectRef>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Heap {
|
||||
id: u64,
|
||||
@@ -42,6 +49,7 @@ pub(crate) struct Heap {
|
||||
live: usize,
|
||||
changes: BTreeMap<(ObjectRef, ChangeKey), u64>,
|
||||
changes_overflow: bool,
|
||||
read_only: Vec<ReadOnly>,
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
@@ -56,9 +64,61 @@ impl Heap {
|
||||
live: 0,
|
||||
changes: BTreeMap::new(),
|
||||
changes_overflow: false,
|
||||
read_only: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn begin_read_only(&mut self) -> usize {
|
||||
let protected = self
|
||||
.slots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, slot)| {
|
||||
slot.object.as_ref().map(|_| ObjectRef {
|
||||
heap_id: self.id,
|
||||
index,
|
||||
generation: slot.generation,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
self.read_only.push(ReadOnly {
|
||||
protected,
|
||||
reads: Mutex::new(BTreeSet::new()),
|
||||
});
|
||||
self.read_only.len()
|
||||
}
|
||||
|
||||
pub(crate) fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
||||
if scope == 0 || scope != self.read_only.len() {
|
||||
return Err(VmError::InvalidArgument("read-only scope nesting mismatch"));
|
||||
}
|
||||
Ok(self
|
||||
.read_only
|
||||
.pop()
|
||||
.unwrap()
|
||||
.reads
|
||||
.into_inner()
|
||||
.expect("read tracker poisoned")
|
||||
.into_iter()
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub(crate) fn is_read_only(&self) -> bool {
|
||||
!self.read_only.is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn record_read(&self, reference: ObjectRef) {
|
||||
for scope in &self.read_only {
|
||||
if scope.protected.contains(&reference) {
|
||||
scope
|
||||
.reads
|
||||
.lock()
|
||||
.expect("read tracker poisoned")
|
||||
.insert(reference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn allocate(
|
||||
&mut self,
|
||||
payload: Box<dyn ObjectPayload>,
|
||||
@@ -268,6 +328,16 @@ impl Heap {
|
||||
}
|
||||
|
||||
pub(crate) fn prepare_change(&self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
self.get(reference)?;
|
||||
if self
|
||||
.read_only
|
||||
.iter()
|
||||
.any(|scope| scope.protected.contains(&reference))
|
||||
{
|
||||
return Err(VmError::UnsupportedOperation(
|
||||
"mutation of existing object during read-only call",
|
||||
));
|
||||
}
|
||||
self.get(reference)?
|
||||
.header
|
||||
.change_version
|
||||
@@ -280,6 +350,11 @@ impl Heap {
|
||||
let object = self.get_mut(reference).expect("validated change target");
|
||||
object.header.change_version += 1;
|
||||
let version = object.header.change_version;
|
||||
// Fresh result construction has no existing observers. Keep versions,
|
||||
// but do not notify application dependencies about derived allocations.
|
||||
if self.is_read_only() {
|
||||
return;
|
||||
}
|
||||
if self.changes_overflow {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,18 @@ pub struct ObjectStore {
|
||||
}
|
||||
|
||||
impl ObjectStore {
|
||||
/// Existing objects are immutable; fresh result graphs may be constructed.
|
||||
/// Scopes must end in LIFO order. Read handles are borrowed, never GC roots.
|
||||
pub fn begin_read_only(&mut self) -> usize {
|
||||
self.heap.begin_read_only()
|
||||
}
|
||||
pub fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
|
||||
self.heap.end_read_only(scope)
|
||||
}
|
||||
pub fn is_read_only(&self) -> bool {
|
||||
self.heap.is_read_only()
|
||||
}
|
||||
|
||||
pub fn downgrade(&self, reference: ObjectRef) -> Result<WeakObjectRef, VmError> {
|
||||
self.object(reference)?;
|
||||
Ok(WeakObjectRef(reference))
|
||||
@@ -107,6 +119,7 @@ impl ObjectStore {
|
||||
}
|
||||
|
||||
pub fn object(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
|
||||
self.heap.record_read(reference);
|
||||
self.heap.get(reference)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,6 @@
|
||||
| 文件 | 覆盖 |
|
||||
| --- | --- |
|
||||
| [attribute_storage.rs](attribute_storage.rs) | 可转移字段存储、原类型身份、替换释放、属性写删和非法路径拒绝 |
|
||||
| [store.rs](store.rs) | 稳定地址、跨存储句柄拒绝与更新原子性、Dict/Cell 循环回收、函数校验失败无泄漏、共享代码与常量生命周期 |
|
||||
| [store.rs](store.rs) | 只读嵌套、容器与类型写入屏障、稳定地址、跨存储句柄拒绝与更新原子性、Dict/Cell 循环回收、函数校验失败无泄漏、共享代码与常量生命周期 |
|
||||
|
||||
非法句柄索引、槽位代数与计数溢出仍在 src/heap.rs 的私有单元测试内,不开放句柄构造器。VM 委托 API 与求值行为继续由 [core/tests](../../core/tests/README.md) 检查。随工作区在指定远端运行,本地不测试;现有测试源码不代表已通过,见 [验证](../../../docs/operations/validation.md)。返回 [objects](../README.md)。
|
||||
|
||||
@@ -94,3 +94,61 @@ fn standalone_function_factories_validate_and_own_shared_executable_data() {
|
||||
store.release(second).unwrap();
|
||||
assert_eq!(store.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_only_scopes_block_existing_container_and_type_writes_but_allow_fresh_results() {
|
||||
let mut store = objects::ObjectStore::new().unwrap();
|
||||
let roots = store.bootstrap_types().unwrap();
|
||||
let record = store.new_object().unwrap();
|
||||
let dict = store.new_dict().unwrap();
|
||||
let cell = store
|
||||
.new_cell(Some(objects::Value::Number(1_i64.into())))
|
||||
.unwrap();
|
||||
let scope = store.begin_read_only();
|
||||
assert!(
|
||||
store
|
||||
.set_attr(record, "value", objects::Value::Nil)
|
||||
.is_err()
|
||||
);
|
||||
assert!(store.attach_type(record, roots.object).is_err());
|
||||
assert!(
|
||||
store
|
||||
.dict_set(
|
||||
dict,
|
||||
objects::Value::Text("key".into()),
|
||||
objects::Value::Nil
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.cell_set(cell, objects::Value::Number(2_i64.into()))
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
store.cell_get(cell).unwrap(),
|
||||
objects::Value::Number(1_i64.into())
|
||||
);
|
||||
let fresh = store.new_object().unwrap();
|
||||
store.set_attr(fresh, "value", objects::Value::Nil).unwrap();
|
||||
let nested = store.begin_read_only();
|
||||
assert!(
|
||||
store
|
||||
.set_attr(fresh, "value", objects::Value::Bool(true))
|
||||
.is_err()
|
||||
);
|
||||
store.end_read_only(nested).unwrap();
|
||||
store
|
||||
.set_attr(fresh, "value", objects::Value::Bool(true))
|
||||
.unwrap();
|
||||
let reads = store.end_read_only(scope).unwrap();
|
||||
assert!(reads.contains(&cell));
|
||||
store
|
||||
.set_attr(record, "value", objects::Value::Nil)
|
||||
.unwrap();
|
||||
for object in [record, dict, cell, fresh, roots.object, roots.type_type] {
|
||||
store.release(object).unwrap();
|
||||
}
|
||||
store.collect_cycles();
|
||||
assert_eq!(store.object_count(), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user