Skip to content

Commit

Permalink
Merge pull request #145 from nevillegrech/cloning_tweaks
Browse files Browse the repository at this point in the history
New cloner-aiding transformation
  • Loading branch information
sifislag authored Aug 6, 2024
2 parents d8f11a1 + 8ad482c commit fa1e299
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
10 changes: 10 additions & 0 deletions logic/local.dl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ clonerHelperInsertor.insertOps(dup,
) :-
beforeClonerLocal.DUPStatementCopiesPushedLabelVar(dup, label).

// We can't clone fallthrough blocks, add a jump in that case
clonerHelperInsertor.insertOps(fallthrough,
LIST(
STMT(PUSH4, as(fallthrough, symbol)),
STMT(JUMP, "")
)
):-
beforeClonerLocal.FallthroughBlockUsedMultipleTimes(_, fallthrough),
beforeClonerLocal.JUMPDEST(fallthrough).

COPY_OUTPUT(blockCloner, clonerHelperInsertor)
// Transformation doesn't introduce new blocks, we can use revertCloner for the original block info
blockCloner.Prev_Block_OriginalBlock(block, originalBlock):- revertCloner.Block_OriginalBlock(block, originalBlock).
Expand Down
26 changes: 21 additions & 5 deletions logic/local_components.dl
Original file line number Diff line number Diff line change
Expand Up @@ -628,17 +628,33 @@ IsStackIndexLessThan(n, maximum) :- n = range(0, maximum, 1).
PrivateFunctionCall(block, _, _, _);
PrivateFunctionReturn(block).

.decl FallthroughBlockPushesContinuation(block: Block, fallthrough: Statement)
DEBUG_OUTPUT(FallthroughBlockPushesContinuation)
FallthroughBlockPushesContinuation(block, fallthrough):-
BlockPushesLabel(block, _),
// Falthrough edges (excluding JUMPIs)
.decl JumplessFalthroughBlock(block: Block, next: Block)
JumplessFalthroughBlock(block, next):-
FallthroughEdge(block, next),
BasicBlock_Tail(block, stmt),
Statement_Next(stmt, fallthrough),
!JUMP(stmt),
!JUMPI(stmt),
Statement_Opcode(stmt, opcode),
!OpcodePossiblyHalts(opcode).

// Insertor heuristic: Fallthrough block likely performs a function call
.decl FallthroughBlockPushesContinuation(block: Block, fallthrough: Statement)
FallthroughBlockPushesContinuation(block, fallthrough):-
BlockPushesLabel(block, _),
JumplessFalthroughBlock(block, next),
BasicBlock_Head(next, fallthrough).

// Insertor heuristic: Fallthrough block is used multiple times and its the next is a `StackBalanceBlock`
.decl FallthroughBlockUsedMultipleTimes(block: Block, fallthrough: Statement)
FallthroughBlockUsedMultipleTimes(block, fallthrough):-
JumplessFalthroughBlock(block, next),
StackBalanceBlock(next),
(BlockPushesLabel(block1, as(block, Value)); StaticBlockJumpTarget(block1, as(block, Value))),
(BlockPushesLabel(block2, as(block, Value)); StaticBlockJumpTarget(block2, as(block, Value))),
BasicBlock_Head(next, fallthrough),
block1 != block2.

.decl CODECOPYStatement(stmt: Statement, offset: Value, size: Value)
CODECOPYStatement(codeCopy, codeOffsetNumHex, smallNumHex) :-
Statement_Opcode(codeCopy, "CODECOPY"),
Expand Down
3 changes: 2 additions & 1 deletion logic/statement_insertor.dl
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ INITIALIZE_STATEMENT_INSERTOR(_insertor, to)
BlockPushesBlockToStack(_, otherFrom, to),
BlockCloningCandidate(to),
from != otherFrom,
!BlockPushesCloningCandidate(to, _, _),
// Don't think we need it after all.
// !BlockPushesCloningCandidate(to, _, _),
!FallthroughBlock(to),
// Make sure we don't break any CODECOPY stmts
!analysis.CODECOPYStatement(_, as(to,Value), _).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"client_path": null,
"gigahorse_args": ["-i", "--disable_inline"],
"expected_analytics": [["Analytics_JumpToMany", 1, 0], ["Analytics_UnreachableBlock", 2, 0]]
"expected_analytics": [["Analytics_JumpToMany", 2, 0], ["Analytics_UnreachableBlock", 2, 0]]
}

0 comments on commit fa1e299

Please sign in to comment.