From 8b63ae4ca2a3583286445fd1bf3b5d2f929b4d56 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 9 Oct 2026 09:45:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(gen):=20=E6=A8=A1=E5=9D=97=E5=89=8D?= =?UTF-8?q?=E8=A8=80=E7=9A=84=E5=A3=B0=E6=98=8E=E6=B1=A0=E9=80=90=E7=BA=A7?= =?UTF-8?q?=E5=88=A4=E7=A9=BA=EF=BC=8C=E9=81=BF=E5=85=8D=20PermPool=20?= =?UTF-8?q?=E6=9C=AA=E5=B0=B1=E7=BB=AA=E6=97=B6=E7=A9=BA=E6=8C=87=E9=92=88?= =?UTF-8?q?=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:TransPyV.exe 编译任何工程都 AddressSanitizer access-violation on address 0x0(pc=0)。根因:_emit_module_prelude 跑在每个模块翻译的开头,此时 CompilerFactory.factory 或其 PermPool 可能尚未就绪,直接 factory.PermPool.alloc(...) 解引用空指针(PermPool 是 factory 的首字段,偏移 0,与 ASan 报的地址 0x0 吻合)。改为 factory / PermPool 逐级判空,不可用时回退模块池。 --- App/lib/core/Gen/Translator.vp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/App/lib/core/Gen/Translator.vp b/App/lib/core/Gen/Translator.vp index f9fdd8d..673a146 100644 --- a/App/lib/core/Gen/Translator.vp +++ b/App/lib/core/Gen/Translator.vp @@ -414,13 +414,15 @@ novirtual class Translator: i8_ty: mlirlite.LLVMType* = mlirlite.Int8(pool) i8_ptr_ty: mlirlite.LLVMType* = mlirlite.Ptr(pool, i8_ty) - # 模块前言里 C 库声明的**名字**必须落在 PermPool:create_declare 会 _str_dup(pool, name) - # 把名字拷进传入的池,而本模块池(trans.Pool)在大模块上会被复用/回绕,覆盖早期分配 → - # 名字悬空(现场:@malloc 被渲染成乱码 @"PL\xEF\xBF\xBDp…",mlir-opt 报 - # "'llvm.call' op 'malloc' does not reference a symbol in the current scope"; - # 类型是 mlirlite 内部 interned 的,所以只有名字坏、参数类型正常)。 - # PermPool 是"persistent data; never reset",正好符合声明名的生命周期。 - decl_pool: memhub.MemBuddy* = CompilerFactory.factory.PermPool + # 模块前言里 C 库声明的**名字**应当落在长生命周期池(PermPool):create_declare 会 + # _str_dup(pool, name) 把名字拷进传入的池,而本模块池(trans.Pool)在大模块上会被 + # 复用/回绕,覆盖早期分配 → 名字悬空(现场:@malloc 渲染成乱码)。 + # **但本函数跑得非常早**(每个模块翻译的开头),此时 CompilerFactory.factory 或它的 + # PermPool 可能尚未就绪:直接 `factory.PermPool.alloc(...)` 会解引用空指针 + # (现场:TransPyV.exe 编译任何工程都 access-violation on address 0x0 —— PermPool 是 + # factory 的首字段,偏移 0)。故逐级判空并回退到模块池。 + decl_pool: memhub.MemBuddy* = None + if CompilerFactory.factory is not None: decl_pool = CompilerFactory.factory.PermPool if decl_pool is None: decl_pool = pool # Declare printf