Skip to content

Commit f2f83f7

Browse files
authored
Merge pull request #18013 from MathiasVP/non-boolean-consistency-check
C++: Add another IR consistency query
2 parents f7ee5f4 + ccca0b6 commit f2f83f7

21 files changed

+2776
-1
lines changed

Diff for: cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRConsistency.qll

+22
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ module InstructionConsistency {
546546
"' has no associated variable, in function '$@'." and
547547
irFunc = getInstructionIRFunction(instr, irFuncText)
548548
}
549+
550+
query predicate nonBooleanOperand(
551+
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
552+
) {
553+
exists(Instruction unary |
554+
unary = instr.(LogicalNotInstruction).getUnary() and
555+
not unary.getResultIRType() instanceof IRBooleanType and
556+
irFunc = getInstructionIRFunction(instr, irFuncText) and
557+
message =
558+
"Logical Not instruction " + instr.toString() +
559+
" with non-Boolean operand, in function '$@'."
560+
)
561+
or
562+
exists(Instruction cond |
563+
cond = instr.(ConditionalBranchInstruction).getCondition() and
564+
not cond.getResultIRType() instanceof IRBooleanType and
565+
irFunc = getInstructionIRFunction(instr, irFuncText) and
566+
message =
567+
"Conditional branch instruction " + instr.toString() +
568+
" with non-Boolean condition, in function '$@'."
569+
)
570+
}
549571
}

Diff for: cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/IRConsistency.qll

+22
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ module InstructionConsistency {
546546
"' has no associated variable, in function '$@'." and
547547
irFunc = getInstructionIRFunction(instr, irFuncText)
548548
}
549+
550+
query predicate nonBooleanOperand(
551+
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
552+
) {
553+
exists(Instruction unary |
554+
unary = instr.(LogicalNotInstruction).getUnary() and
555+
not unary.getResultIRType() instanceof IRBooleanType and
556+
irFunc = getInstructionIRFunction(instr, irFuncText) and
557+
message =
558+
"Logical Not instruction " + instr.toString() +
559+
" with non-Boolean operand, in function '$@'."
560+
)
561+
or
562+
exists(Instruction cond |
563+
cond = instr.(ConditionalBranchInstruction).getCondition() and
564+
not cond.getResultIRType() instanceof IRBooleanType and
565+
irFunc = getInstructionIRFunction(instr, irFuncText) and
566+
message =
567+
"Conditional branch instruction " + instr.toString() +
568+
" with non-Boolean condition, in function '$@'."
569+
)
570+
}
549571
}

Diff for: cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/IRConsistency.qll

+22
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ module InstructionConsistency {
546546
"' has no associated variable, in function '$@'." and
547547
irFunc = getInstructionIRFunction(instr, irFuncText)
548548
}
549+
550+
query predicate nonBooleanOperand(
551+
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
552+
) {
553+
exists(Instruction unary |
554+
unary = instr.(LogicalNotInstruction).getUnary() and
555+
not unary.getResultIRType() instanceof IRBooleanType and
556+
irFunc = getInstructionIRFunction(instr, irFuncText) and
557+
message =
558+
"Logical Not instruction " + instr.toString() +
559+
" with non-Boolean operand, in function '$@'."
560+
)
561+
or
562+
exists(Instruction cond |
563+
cond = instr.(ConditionalBranchInstruction).getCondition() and
564+
not cond.getResultIRType() instanceof IRBooleanType and
565+
irFunc = getInstructionIRFunction(instr, irFuncText) and
566+
message =
567+
"Conditional branch instruction " + instr.toString() +
568+
" with non-Boolean condition, in function '$@'."
569+
)
570+
}
549571
}

Diff for: cpp/ql/src/Metrics/Internal/IRConsistency.ql

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ select count(Instruction i | IRConsistency::missingOperand(i, _, _, _) | i) as m
4040
count(Instruction i | IRConsistency::nonUniqueEnclosingIRFunction(i, _, _, _) | i) as nonUniqueEnclosingIRFunction,
4141
count(FieldAddressInstruction i | IRConsistency::fieldAddressOnNonPointer(i, _, _, _) | i) as fieldAddressOnNonPointer,
4242
count(Instruction i | IRConsistency::thisArgumentIsNonPointer(i, _, _, _) | i) as thisArgumentIsNonPointer,
43-
count(Instruction i | IRConsistency::nonUniqueIRVariable(i, _, _, _) | i) as nonUniqueIRVariable
43+
count(Instruction i | IRConsistency::nonUniqueIRVariable(i, _, _, _) | i) as nonUniqueIRVariable,
44+
count(Instruction i | IRConsistency::nonBooleanOperand(i, _, _, _) | i) as nonBooleanOperand

0 commit comments

Comments
 (0)