feat(deref): 解引用写入 *p = v 采用 DerefAs 安全语义(方案1)
当 v 是指针且 pointee(p) != typeof(v)(原会因'指针<->非基本类型不匹配'fail-fast)时,把指针 p bitcast 成 typeof(v)* 再按值存 v,即 *(T*(p)) = v(DerefAs 期望写法;旧 DerefAs 的 *(intptr_t*(p))=(intptr_t)v 是错实现)。标量宽度等其余情形维持 pointee(p) 语义不变
This commit is contained in:
1 parent
3122793139
commit
ddf40e02c5
1 file changed
+9
@@ -605,6 +605,15 @@ class AssignHandle(BaseHandle):
|
||||
if DerefValue is None:
|
||||
self._RaiseUnresolvedExpr("解引用写入", Node.value, Gen)
|
||||
return
|
||||
# 方案1(DerefAs 的安全语义):`*p = v` 只在本会被"指针↔pointee 类型不匹配"
|
||||
# 拒绝时才改用 `*(T*(p)) = v`(T = v 的类型):把 **指针** p bitcast 成 T* 再按值
|
||||
# 存 v。这正是 c.DerefAs 期望的写法(旧 DerefAs 的 `*(intptr_t*(p))=(intptr_t)v`
|
||||
# 把"值"bitcast 成指针大小整数,是错实现)。标量宽度等其余情形维持原
|
||||
# `pointee(p)` 语义不变(不引入标量宽度漂移)。
|
||||
PtrPointee: ir.Type | None = Ptr.type.pointee if isinstance(Ptr.type, ir.PointerType) else None
|
||||
if (isinstance(DerefValue.type, ir.PointerType) and PtrPointee is not None
|
||||
and PtrPointee != DerefValue.type):
|
||||
Ptr = Gen.builder.bitcast(Ptr, ir.PointerType(DerefValue.type), name="deref_store_as")
|
||||
self._StoreWithCoerce(DerefValue, Ptr, Node.value)
|
||||
elif isinstance(Target, (ast.Tuple, ast.List)):
|
||||
# ast.List 与 ast.Tuple 字段表逐字相同(Nodes.py:119-120 均为
|
||||
|
||||
Reference in new issue
Block a user