Skip to content

Commit 998973e

Browse files
owen-mcCopilot
andcommitted
Go: Fix bad join orders in defer temporaries and barrier guards
Factor non-parameterized DeferReadInstruction helpers and delay globalValueNumber and getBasicBlock joins in ParameterizedBarrierGuardValue::getABarrierNodeForGuard to avoid GVN fanout when guards is large. Also make AllocationSizeCheckBarrier non-recursive and constrain binding order in TempVariable.getType and reachableFromDeferSave. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 892427e commit 998973e

3 files changed

Lines changed: 61 additions & 27 deletions

File tree

‎go/ql/lib/semmle/go/controlflow/IR.qll‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ module IR {
10491049
)
10501050
or
10511051
exists(FuncDef fd, int i | this = MkUnnamedResultTemp(fd, i) |
1052-
result = fd.getType().getResultType(i)
1052+
result = pragma[only_bind_out](fd).getType().getResultType(pragma[only_bind_into](i))
10531053
)
10541054
}
10551055

@@ -1146,9 +1146,15 @@ module IR {
11461146
override Instruction getRhs() { result = deferSavedArgInstruction(d, i) }
11471147
}
11481148

1149+
bindingset[insn]
1150+
pragma[inline_late]
1151+
private BasicBlock getInstructionBasicBlock(Instruction insn) { result = insn.getBasicBlock() }
1152+
11491153
private BasicBlock reachableFromDeferSave(BasicBlock start) {
1150-
exists(DeferReadInstruction dr | start = dr.getSavedInstruction().getBasicBlock()) and
1151-
result = start.getASuccessor()
1154+
exists(DeferReadInstruction dr |
1155+
start = getInstructionBasicBlock(dr.getSavedInstruction()) and
1156+
result = start.getASuccessor()
1157+
)
11521158
or
11531159
result = reachableFromDeferSave(start).getASuccessor()
11541160
}

‎go/ql/lib/semmle/go/dataflow/internal/DataFlowUtil.qll‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,38 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
376376
}
377377
}
378378

379+
/**
380+
* Holds if `drNode` is a `DeferReadInstruction` node that restores `savedNode`
381+
* outside a loop, and `savedNode` is a use of `var`.
382+
*/
383+
pragma[noinline]
384+
private predicate deferSavedUse(Node drNode, Node savedNode, SsaWithFields var) {
385+
exists(IR::DeferReadInstruction dr |
386+
savedNode = instructionNode(dr.getSavedInstruction()) and
387+
not dr.isSavedInLoop() and
388+
drNode = instructionNode(pragma[only_bind_out](dr)) and
389+
savedNode = var.getAUse()
390+
)
391+
}
392+
393+
/**
394+
* Holds if `drNode` restores a direct read of SSA variable `var`, which has no
395+
* indirect references, outside a loop.
396+
*/
397+
pragma[noinline]
398+
private predicate deferSavedDirectVarUse(Node drNode, SsaWithFields var) {
399+
exists(Node savedNode, SsaSourceVariable v |
400+
deferSavedUse(drNode, savedNode, var) and
401+
v = var.getBaseDefinition().getSourceVariable() and
402+
pragma[only_bind_out](savedNode).asInstruction().reads(v) and
403+
not v.mayHaveIndirectReferences()
404+
)
405+
}
406+
407+
bindingset[n1, n2]
408+
pragma[inline_late]
409+
private predicate sameGvn(Node n1, Node n2) { globalValueNumber(n1) = globalValueNumber(n2) }
410+
379411
/**
380412
* Provides a set of barrier nodes for a guard that validates an expression.
381413
*
@@ -405,26 +437,16 @@ module ParameterizedBarrierGuardValue<
405437
(
406438
result = pragma[only_bind_out](var).getAUse()
407439
or
408-
exists(IR::DeferReadInstruction dr, DataFlow::Node savedNode |
409-
result.asInstruction() = dr and
410-
not dr.isSavedInLoop() and
411-
savedNode = DataFlow::instructionNode(dr.getSavedInstruction()) and
412-
savedNode = pragma[only_bind_out](var).getAUse() and
413-
(
414-
exists(SsaSourceVariable v |
415-
v = var.getBaseDefinition().getSourceVariable() and
416-
savedNode.asInstruction().reads(v) and
417-
not v.mayHaveIndirectReferences()
418-
)
419-
or
420-
exists(DataFlow::Node guardedNode |
421-
guards(guardCheck, guard, value, guardedNode, var, param) and
422-
globalValueNumber(savedNode) = globalValueNumber(guardedNode)
423-
)
424-
)
425-
)
426-
) and
427-
guardControlsBlock(guard, result.getBasicBlock(), value)
440+
deferSavedDirectVarUse(result, var)
441+
)
442+
or
443+
exists(Node savedNode, Node guardedNode |
444+
guards(guardCheck, guard, value, guardedNode, var, param) and
445+
deferSavedUse(result, savedNode, var) and
446+
sameGvn(savedNode, guardedNode)
447+
)
448+
|
449+
guardControlsBlock(guard, pragma[only_bind_out](result).getBasicBlock(), value)
428450
)
429451
}
430452

‎go/ql/lib/semmle/go/security/AllocationSizeOverflowCustomizations.qll‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@ module AllocationSizeOverflow {
8181
)
8282
}
8383

84+
private DataFlow::Node directAllocationSizeCheckBarrier() {
85+
result = DataFlow::BarrierGuard<allocationSizeCheck/3>::getABarrierNode()
86+
}
87+
8488
/** A check of the allocation size, acting as a guard to prevent allocation-size overflow. */
8589
class AllocationSizeCheckBarrier extends DataFlow::Node {
8690
AllocationSizeCheckBarrier() {
87-
this = DataFlow::BarrierGuard<allocationSizeCheck/3>::getABarrierNode()
91+
this = directAllocationSizeCheckBarrier()
8892
or
89-
DataFlow::instructionNode(this.asInstruction()
90-
.(IR::DeferReadInstruction)
91-
.getSavedInstruction()) instanceof AllocationSizeCheckBarrier
93+
exists(IR::DeferReadInstruction dr |
94+
this = DataFlow::instructionNode(dr) and
95+
DataFlow::instructionNode(pragma[only_bind_out](dr).getSavedInstruction()) =
96+
directAllocationSizeCheckBarrier()
97+
)
9298
}
9399
}
94100

0 commit comments

Comments
 (0)