Skip to content

Commit

Permalink
fix handling of fidx in multimodule scenario
Browse files Browse the repository at this point in the history
temporary fix for danleh/wasabi#45
  • Loading branch information
doehyunbaek committed Jul 29, 2024
1 parent cfc6b5e commit a4c039f
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/tracer.cts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export default class Analysis implements AnalysisI<Trace> {
private shadowMemories: ArrayBuffer[] = []
private shadowGlobals: number[] = []
private shadowTables: WebAssembly.Table[] = []
private funcrefToIdx: Map<Function, number>

// helpers
private callStack: ('int' | { name: string, idx: number })[] = [{ name: 'main', 'idx': -1 }]
Expand Down Expand Up @@ -355,13 +356,16 @@ export default class Analysis implements AnalysisI<Trace> {

call_pre: (location, op, funcidx, args, tableTarget) => {
if (op === 'call_indirect') {
this.tableGetEvent(tableTarget.tableIdx, tableTarget.elemIdx)
}
let funcImport = Wasabi.module.info.functions[funcidx].import
if (funcImport !== null) {
let name = funcImport[1]
this.callStack.push({ name, idx: funcidx })
this.trace.push(`IC;${funcidx};${name}`)
const resolvedFuncIdx = this.funcrefToIdx.get(this.shadowTables[tableTarget.tableIdx].get(tableTarget.elemIdx));
console.trace(resolvedFuncIdx)
this.trace.push(`IC;${resolvedFuncIdx}`)
} else {
let funcImport = Wasabi.module.info.functions[funcidx].import
if (funcImport !== null) {
let name = funcImport[1]
this.callStack.push({ name, idx: funcidx })
this.trace.push(`IC;${funcidx}`)
}
}
},

Expand Down Expand Up @@ -506,7 +510,21 @@ export default class Analysis implements AnalysisI<Trace> {
}

init() {
// Init Memories
const funcrefToIdx = new Map();
for (let exp in this.Wasabi.module.exports) {
let funcref = this.Wasabi.module.exports[exp]
if (typeof funcref == 'function') {
let fidx;
this.Wasabi.module.info.functions.forEach((f, i) => {
if (f.export !== null && f.export[0] === exp) {
fidx = i
}
})
funcrefToIdx.set(funcref, fidx);
}
}
this.funcrefToIdx = funcrefToIdx

this.Wasabi.module.memories.forEach((mem, i) => {
let isImported = this.Wasabi.module.info.memories[i].import !== null
if (isImported) {
Expand Down

0 comments on commit a4c039f

Please sign in to comment.