Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Feb 27, 2025
1 parent 02105d7 commit 5665816
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 17 deletions.
54 changes: 39 additions & 15 deletions src/decompiler/disasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,44 @@ function findDictOpcode(opcodes: DecompiledInstruction[]): DecompiledInstruction
return opcodes.find(it => it.op.definition.mnemonic === "DICTPUSHCONST")
}

function findRootMethods(opcodes: DecompiledInstruction[]): MethodNode[] {
const methods: MethodNode[] = []

if (opcodes[2]?.op.definition.mnemonic === "PUSHCONT") {
const cont = opcodes[2].op.operands.at(0)
if (!cont || cont.type !== "subslice") {
return methods
}

const recvInternal = disassembleRawRoot(cont.value)
methods.push({
type: "method",
hash: recvInternal.hash,
offset: recvInternal.offset,
body: recvInternal,
id: 0,
})
}

if (opcodes[6]?.op.definition.mnemonic === "PUSHCONT") {
const cont = opcodes[6].op.operands.at(0)
if (!cont || cont.type !== "subslice") {
return methods
}

const recvExternal = disassembleRawRoot(cont.value)
methods.push({
type: "method",
hash: recvExternal.hash,
offset: recvExternal.offset,
body: recvExternal,
id: -1,
})
}

return methods
}

/**
* Disassembles the root cell into a list of instructions.
*
Expand All @@ -363,21 +401,7 @@ export function disassembleRoot(
onCellReference: undefined,
}

const rootMethods: MethodNode[] = []

if (opcodes[2]?.op.definition.mnemonic === "PUSHCONT") {
const cont = opcodes[2].op.operands[0]
if (cont.type === "subslice") {
const recvInternal = disassembleRawRoot(cont.value)
rootMethods.push({
type: "method",
hash: recvInternal.hash,
offset: recvInternal.offset,
body: recvInternal,
id: 0,
})
}
}
const rootMethods = findRootMethods(opcodes)

const dictOpcode = findDictOpcode(opcodes)
if (!dictOpcode) {
Expand Down
94 changes: 92 additions & 2 deletions src/test/e2e/__snapshots__/known-contracts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,100 @@
exports[`known contracts > should decompile Tact 1.6.0 with other layout 1`] = `
""Asm.fif" include
PROGRAM{
DECLPROC recv_internal
DECLPROC main
78250 DECLMETHOD ?fun_78250
DECLPROC ?fun_ref_92183b49329bb4e4
recv_internal PROC:<{
main PROC:<{
DROP
DROP
CTOS
TWO
SDSKIPFIRST
1 LDI
1 LDI
LDMSGADDR
OVER
s3 s4 XCHG
s5 s5 XCHG2
4 TUPLE
1 SETGLOB
SWAP
2 SETGLOB
PUSHROOT
CTOS
1 LDI
DROP
<{
NULL
}> PUSHCONT
<{
NULL
}> PUSHCONT
IFELSE
DROP
IFRET
130 THROW
}>
?fun_78250 PROC:<{
PUSHROOT
CTOS
1 LDI
DROP
<{
NULL
}> PUSHCONT
<{
NULL
}> PUSHCONT
IFELSE
?fun_ref_92183b49329bb4e4 INLINECALLDICT
NIP
}>
?fun_ref_92183b49329bb4e4 PROCREF:<{
x{68656C6C6F20776F726C64} PUSHSLICE
}>
}END>c"
`;
exports[`known contracts > should decompile Tact 1.6.0 with other layout and recv_external 1`] = `
""Asm.fif" include
PROGRAM{
-1 DECLMETHOD recv_external
DECLPROC main
78250 DECLMETHOD ?fun_78250
DECLPROC ?fun_ref_92183b49329bb4e4
recv_external PROC:<{
DROP
DROP
PUSHROOT
CTOS
1 LDI
DROP
<{
NULL
}> PUSHCONT
<{
NULL
}> PUSHCONT
IFELSE
1 GETGLOB
4 UNTUPLE
s2 s3 XCHG
3 BLKDROP
41351 PUSHINT
MYADDR
ROT
SDEQ
THROWANYIFNOT
DROP
NEWC
-1 PUSHINT
SWAP
1 STI
ENDC
POPROOT
}>
main PROC:<{
DROP
DROP
CTOS
Expand Down
9 changes: 9 additions & 0 deletions src/test/e2e/known-contracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,13 @@ describe("known contracts", () => {
const res = decompileAll(wallet)
expect(res).toMatchSnapshot()
})

it("should decompile Tact 1.6.0 with other layout and recv_external", async () => {
const wallet = Buffer.from(
"te6ccgEBAwEAjAAByv8AII4oMDDQctch0gDSAPpAIRA0UFVvBPhhAfhi7UTQ0gAwkW2RbeIw3PLAguEgwP+OKTAw7UTQ0gAwkW2RbeL4QW8kECNfA4IAoYf4KFjHBfL0MMh/AcoAye1U4PSkE/S88sgLAQEjpkxqu1E0NIAMJFtkW3i2zwxgAgAai7aGVsbG8gd29ybGSA==",
"base64",
)
const res = decompileAll(wallet)
expect(res).toMatchSnapshot()
})
})

0 comments on commit 5665816

Please sign in to comment.