From 458ee13345a93021f1da581d4cc9ccc36b2918ce Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 19 Mar 2024 16:23:57 +0000 Subject: [PATCH] C++: Add constant analysis for bitwise operations now that these are no longer constant folded by IR construction. --- .../implementation/raw/constant/ConstantAnalysis.qll | 6 ++++++ .../semmle/code/cpp/ir/internal/IntegerPartial.qll | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll index 47b744b3f7c..f65799f9a61 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll @@ -38,6 +38,12 @@ private int getBinaryInstructionValue(BinaryInstruction instr) { or instr instanceof DivInstruction and result = div(left, right) or + instr instanceof BitOrInstruction and result = bitOr(left, right) + or + instr instanceof BitAndInstruction and result = bitAnd(left, right) + or + instr instanceof BitXorInstruction and result = bitXor(left, right) + or instr instanceof CompareEQInstruction and result = compareEQ(left, right) or instr instanceof CompareNEInstruction and result = compareNE(left, right) diff --git a/cpp/ql/lib/semmle/code/cpp/ir/internal/IntegerPartial.qll b/cpp/ql/lib/semmle/code/cpp/ir/internal/IntegerPartial.qll index 0e24f283b17..33681dde0d4 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/internal/IntegerPartial.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/internal/IntegerPartial.qll @@ -89,6 +89,18 @@ int compareLE(int a, int b) { if a <= b then result = 1 else result = 0 } bindingset[a, b] int compareGE(int a, int b) { if a >= b then result = 1 else result = 0 } +/** Returns `a >= b`. */ +bindingset[a, b] +int bitOr(int a, int b) { result = a.bitOr(b) } + +/** Returns `a >= b`. */ +bindingset[a, b] +int bitAnd(int a, int b) { result = a.bitAnd(b) } + +/** Returns `a >= b`. */ +bindingset[a, b] +int bitXor(int a, int b) { result = a.bitXor(b) } + /** * Returns `-a`. If the negation would overflow, there is no result. */