From 18b35dc3fe3d983a2a93a25b468d11fe052d16d2 Mon Sep 17 00:00:00 2001 From: Skillbert Date: Sat, 8 Jun 2024 22:37:08 +0200 Subject: [PATCH] fix subtype detection for local vars --- src/clientscript/ast.ts | 10 +++++++++- src/clientscript/codewriter.ts | 1 + src/clientscript/subtypedetector.ts | 18 ++++++++++-------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/clientscript/ast.ts b/src/clientscript/ast.ts index 791ebbc..489f2c4 100644 --- a/src/clientscript/ast.ts +++ b/src/clientscript/ast.ts @@ -974,7 +974,15 @@ export function varArgtype(stringconst: string | unknown, lastintconst: number | } export function setRawOpcodeStackDiff(consts: StackConstants | null, calli: ClientscriptObfuscation, node: RawOpcodeNode) { - if (node.opinfo.id == namedClientScriptOps.dbrow_getfield) { + if (branchInstructionsInt.includes(node.opinfo.id)) { + //make sure that left and right side are same type + let uuid = typeuuids.int++; + node.knownStackDiff = StackInOut.fromExact([uuid, uuid], []); + } else if (branchInstructionsLong.includes(node.opinfo.id)) { + //make sure that left and right side are same type + let uuid = typeuuids.long++; + node.knownStackDiff = StackInOut.fromExact([uuid, uuid], []); + } else if (node.opinfo.id == namedClientScriptOps.dbrow_getfield) { //args are rowid,tablefield,subrow let tablefield = consts?.values.at(-2); if (typeof tablefield == "number") { diff --git a/src/clientscript/codewriter.ts b/src/clientscript/codewriter.ts index 184a943..111022e 100644 --- a/src/clientscript/codewriter.ts +++ b/src/clientscript/codewriter.ts @@ -301,6 +301,7 @@ addWriter(RawOpcodeNode, (node, ctx) => { let gettypecast = (subt: PrimitiveType) => { if (exacttype == -1) { return ""; } if (exacttype == subtypes.int || exacttype == subtypes.string || exacttype == subtypes.long) { return ""; } + if (exacttype == subtypes.unknown_int || exacttype == subtypes.unknown_string || exacttype == subtypes.unknown_long) { return ""; } return ` as ${subtypeToTs(exacttype)}`; } if (typeof node.op.imm_obj == "string") { diff --git a/src/clientscript/subtypedetector.ts b/src/clientscript/subtypedetector.ts index 3e4fa60..de72f28 100644 --- a/src/clientscript/subtypedetector.ts +++ b/src/clientscript/subtypedetector.ts @@ -188,10 +188,11 @@ class CombinedExactStack { depfunc = getPositionalDep; } - if (stackinout.exactin) { - for (let i = stackinout.exactin.int.length - 1; i >= 0; i--) { this.ctx.entangle(knownDependency(stackinout.exactin.int[i]), this.intstack.pop()); } - for (let i = stackinout.exactin.long.length - 1; i >= 0; i--) { this.ctx.entangle(knownDependency(stackinout.exactin.long[i]), this.longstack.pop()); } - for (let i = stackinout.exactin.string.length - 1; i >= 0; i--) { this.ctx.entangle(knownDependency(stackinout.exactin.string[i]), this.stringstack.pop()); } + if (node.knownStackDiff?.exactin) { + let exact = node.knownStackDiff.exactin; + for (let i = exact.int.length - 1; i >= 0; i--) { this.ctx.entangle(knownDependency(exact.int[i]), this.intstack.pop()); } + for (let i = exact.long.length - 1; i >= 0; i--) { this.ctx.entangle(knownDependency(exact.long[i]), this.longstack.pop()); } + for (let i = exact.string.length - 1; i >= 0; i--) { this.ctx.entangle(knownDependency(exact.string[i]), this.stringstack.pop()); } } else { let stackin = stackinout.in; //need to do inputs in correct order because of vararg @@ -218,10 +219,11 @@ class CombinedExactStack { } } - if (stackinout.exactout) { - for (let i = 0; i < stackinout.exactout.int.length; i++) { this.intstack.push(knownDependency(stackinout.exactout.int[i])); } - for (let i = 0; i < stackinout.exactout.long.length; i++) { this.longstack.push(knownDependency(stackinout.exactout.long[i])); } - for (let i = 0; i < stackinout.exactout.string.length; i++) { this.stringstack.push(knownDependency(stackinout.exactout.string[i])); } + if (node.knownStackDiff?.exactout) { + let exact = node.knownStackDiff.exactout; + for (let i = 0; i < exact.int.length; i++) { this.intstack.push(knownDependency(exact.int[i])); } + for (let i = 0; i < exact.long.length; i++) { this.longstack.push(knownDependency(exact.long[i])); } + for (let i = 0; i < exact.string.length; i++) { this.stringstack.push(knownDependency(exact.string[i])); } } else { //only ensure order per primitive type let totalout = stackinout.out.getStackdiff();