Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5898 from trufflesuite/push0
Browse files Browse the repository at this point in the history
Add support for PUSH0 instruction to debugger & disassembler
  • Loading branch information
haltman-at authored Feb 16, 2023
2 parents 831f7c1 + 740700b commit 761f7a3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/code-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function parseCode(
name: parseOpcode(code[pc])
};
if (opcode.name.slice(0, 4) === "PUSH") {
const length = code[pc] - 0x60 + 1; //0x60 is code for PUSH1
const length = code[pc] - 0x5f; //0x5f is code for PUSH0
let pushData = code.subarray(pc + 1, pc + length + 1);
if (pushData.length < length) {
//if we run out of bytes for our pushdata, fill the rest
Expand Down
1 change: 1 addition & 0 deletions packages/code-utils/src/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const codes = {
0x5b: "JUMPDEST",

// 0x60 & 0x70 range - pushes
0x5f: "PUSH0",
0x60: "PUSH1",
0x61: "PUSH2",
0x62: "PUSH3",
Expand Down
4 changes: 1 addition & 3 deletions packages/codec/lib/abi-data/allocate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,9 +1014,7 @@ function constructorOutputAllocation(
if (contractKind === "library") {
//note: I am relying on this being present!
//(also this part is a bit HACKy)
const pushAddressInstruction = (0x60 + Evm.Utils.ADDRESS_SIZE - 1).toString(
16
); //"73"
const pushAddressInstruction = (0x5f + Evm.Utils.ADDRESS_SIZE).toString(16); //"73"
const delegateCallGuardString =
"0x" + pushAddressInstruction + "..".repeat(Evm.Utils.ADDRESS_SIZE);
if (binary.startsWith(delegateCallGuardString)) {
Expand Down
8 changes: 2 additions & 6 deletions packages/codec/lib/contexts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ export function normalizeContexts(contexts: Contexts): Contexts {
//now we must handle the delegatecall guard -- libraries' deployedBytecode will include
//0s in place of their own address instead of a link reference at the
//beginning, so we need to account for that too
const pushAddressInstruction = (0x60 + Evm.Utils.ADDRESS_SIZE - 1).toString(
16
); //"73"
const pushAddressInstruction = (0x5f + Evm.Utils.ADDRESS_SIZE).toString(16); //"73"
for (let context of Object.values(newContexts)) {
if (context.contractKind === "library" && !context.isConstructor) {
context.binary = context.binary.replace(
Expand Down Expand Up @@ -401,9 +399,7 @@ function contractKind(
const deployedBytecode = Shims.NewToLegacy.forBytecode(
contract.deployedBytecode
);
const pushAddressInstruction = (0x60 + Evm.Utils.ADDRESS_SIZE - 1).toString(
16
); //"73"
const pushAddressInstruction = (0x5f + Evm.Utils.ADDRESS_SIZE).toString(16); //"73"
const libraryString =
"0x" + pushAddressInstruction + "00".repeat(Evm.Utils.ADDRESS_SIZE);
return deployedBytecode.startsWith(libraryString) ? "library" : "contract";
Expand Down
4 changes: 3 additions & 1 deletion packages/core/lib/commands/opcode/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ module.exports = async function (options) {
console.log(
Conversion.toHexString(opcode.pc, lastPCByteLength) + ":",
opcode.name,
opcode.pushData || ""
opcode.pushData !== undefined && opcode.pushData !== "0x"
? opcode.pushData
: "" //display just "PUSH0", not "PUSH0 0x"
);
});
};
6 changes: 5 additions & 1 deletion packages/debug-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,11 @@ var DebugUtils = {

formatInstruction: function (instruction) {
return truffleColors.mint(
instruction.name + " " + (instruction.pushData || "")
instruction.name +
" " +
(instruction.pushData !== undefined && instruction.pushData !== "0x"
? instruction.pushData
: "") //display just "PUSH0", not "PUSH0 0x"
);
},

Expand Down

0 comments on commit 761f7a3

Please sign in to comment.