Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function signatures aren't available during Text2Abstract on function definitions #663

Open
gtrepta opened this issue Jun 27, 2024 · 0 comments

Comments

@gtrepta
Copy link
Contributor

gtrepta commented Jun 27, 2024

Some information about a module doesn't get passed down to one of the text2abstract operations that need it:

rule #t2aModule<ctx(... funcIds: FIDS) #as C>(#module(... types: TS, funcs: FS, tables: TABS, mems: MS, globals: GS, elem: EL, data: DAT, start: S, importDefns: IS, exports: ES, metadata: #meta(... id: OID)))
=> #module( ... types: #t2aDefns<C>(TS)
, funcs: #t2aDefns<C>(FS)
, tables: #t2aDefns<C>(TABS)
, mems: #t2aDefns<C>(MS)
, globals: #t2aDefns<C>(GS)
, elem: #t2aDefns<C>(EL)
, data: #t2aDefns<C>(DAT)
, start: #t2aDefns<C>(S)
, importDefns: #t2aDefns<C>(IS)
, exports: #t2aDefns<C>(ES)
, metadata: #meta(... id: OID, funcIds: FIDS, filename: .String)
)

On line 839, t2aDefns is invoked on the functions of the module with the context C. However, this context doesn't contain the signatures for the types in the module, only their ids. So, when t2a goes to calculate the indices of the variables in a function body, it doesn't have the function parameters for named types.

Here's an example from the wasm tests func.wast that fails because of this:

(module
  (type $sig (func (param i32) (result i32)))

  (func (export "f") (type $sig)
    (local $var i32)
    (local.get $var)
  )
)

(assert_return (invoke "f" (i32.const 42)) (i32.const 0))

$var here is assigned index 0. But, it should be 1, as the (param i32) in the function signature is at index 0 already. So, when "f" gets invoked, (local.get 0) is evaluated and returned, which is where the 42 that was passed in is at.

Relevant rules:

rule #t2aDefn<ctx(... typeIds: TIDS) #as C>(( func OID:OptionalId T:TypeUse LS:LocalDecls IS:Instrs ))
=> #func(... type: typeUse2typeIdx(T, TIDS)
, locals: locals2vectype(LS)
, body: #t2aInstrs <#updateLocalIds(C, #ids2Idxs(T, LS))>(IS)
, metadata: #meta(... id: OID, localIds: #ids2Idxs(T, LS))
)

syntax Map ::= #ids2Idxs(TypeUse, LocalDecls) [function, total]
| #ids2Idxs(Int, TypeUse, LocalDecls) [function, total]
// -------------------------------------------------------------------------
rule #ids2Idxs(TU, LDS) => #ids2Idxs(0, TU, LDS)
rule #ids2Idxs(_, .TypeDecls, .LocalDecls) => .Map
rule #ids2Idxs(N, (type _) , LDS) => #ids2Idxs(N, .TypeDecls, LDS)
rule #ids2Idxs(N, (type _) TDS, LDS) => #ids2Idxs(N, TDS , LDS)
rule #ids2Idxs(N, (param ID:Identifier _) TDS, LDS)
=> (ID |-> N) #ids2Idxs(N +Int 1, TDS, LDS)
rule #ids2Idxs(N, (param _) TDS, LDS) => #ids2Idxs(N +Int 1, TDS, LDS)
rule #ids2Idxs(N, _TD:TypeDecl TDS, LDS) => #ids2Idxs(N , TDS, LDS) [owise]
rule #ids2Idxs(N, .TypeDecls, local ID:Identifier _ LDS:LocalDecls)
=> (ID |-> N) #ids2Idxs(N +Int 1, .TypeDecls, LDS)
rule #ids2Idxs(N, .TypeDecls, _LD:LocalDecl LDS) => #ids2Idxs(N +Int 1, .TypeDecls, LDS) [owise]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant