Skip to content

Commit 7f0fcb0

Browse files
committed
C++: Create a common base class for 'NonUnionContent' and 'UnionContent' called 'FieldContent'.
1 parent 7527d88 commit 7f0fcb0

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -873,23 +873,16 @@ predicate jumpStep(Node n1, Node n2) {
873873
predicate storeStepImpl(Node node1, Content c, Node node2, boolean certain) {
874874
exists(
875875
PostFieldUpdateNode postFieldUpdate, int indirectionIndex1, int numberOfLoads,
876-
StoreInstruction store
876+
StoreInstruction store, FieldContent fc
877877
|
878878
postFieldUpdate = node2 and
879+
fc = c and
879880
nodeHasInstruction(node1, store, pragma[only_bind_into](indirectionIndex1)) and
880881
postFieldUpdate.getIndirectionIndex() = 1 and
881882
numberOfLoadsFromOperand(postFieldUpdate.getFieldAddress(),
882-
store.getDestinationAddressOperand(), numberOfLoads, certain)
883-
|
884-
exists(FieldContent fc | fc = c |
885-
fc.getField() = postFieldUpdate.getUpdatedField() and
886-
fc.getIndirectionIndex() = 1 + indirectionIndex1 + numberOfLoads
887-
)
888-
or
889-
exists(UnionContent uc | uc = c |
890-
uc.getAField() = postFieldUpdate.getUpdatedField() and
891-
uc.getIndirectionIndex() = 1 + indirectionIndex1 + numberOfLoads
892-
)
883+
store.getDestinationAddressOperand(), numberOfLoads, certain) and
884+
fc.getAField() = postFieldUpdate.getUpdatedField() and
885+
fc.getIndirectionIndex() = 1 + indirectionIndex1 + numberOfLoads
893886
)
894887
or
895888
// models-as-data summarized flow
@@ -965,22 +958,17 @@ predicate nodeHasInstruction(Node node, Instruction instr, int indirectionIndex)
965958
* `node2`.
966959
*/
967960
predicate readStep(Node node1, ContentSet c, Node node2) {
968-
exists(FieldAddress fa1, Operand operand, int numberOfLoads, int indirectionIndex2 |
961+
exists(
962+
FieldAddress fa1, Operand operand, int numberOfLoads, int indirectionIndex2, FieldContent fc
963+
|
964+
fc = c and
969965
nodeHasOperand(node2, operand, indirectionIndex2) and
970966
// The `1` here matches the `node2.getIndirectionIndex() = 1` conjunct
971967
// in `storeStep`.
972968
nodeHasOperand(node1, fa1.getObjectAddressOperand(), 1) and
973-
numberOfLoadsFromOperand(fa1, operand, numberOfLoads, _)
974-
|
975-
exists(FieldContent fc | fc = c |
976-
fc.getField() = fa1.getField() and
977-
fc.getIndirectionIndex() = indirectionIndex2 + numberOfLoads
978-
)
979-
or
980-
exists(UnionContent uc | uc = c |
981-
uc.getAField() = fa1.getField() and
982-
uc.getIndirectionIndex() = indirectionIndex2 + numberOfLoads
983-
)
969+
numberOfLoadsFromOperand(fa1, operand, numberOfLoads, _) and
970+
fc.getAField() = fa1.getField() and
971+
fc.getIndirectionIndex() = indirectionIndex2 + numberOfLoads
984972
)
985973
or
986974
// models-as-data summarized flow

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,16 +2162,39 @@ private module ContentStars {
21622162

21632163
private import ContentStars
21642164

2165+
private class TFieldContent = TNonUnionContent or TUnionContent;
2166+
2167+
/**
2168+
* A `Content` that references a `Field`. This may be a field of a `struct`,
2169+
* `class`, or `union`. In the case of a `union` there may be multiple fields
2170+
* associated with the same `Content`.
2171+
*/
2172+
class FieldContent extends Content, TFieldContent {
2173+
/** Gets a `Field` of this `Content`. */
2174+
Field getAField() { none() }
2175+
2176+
/**
2177+
* Gets the field associated with this `Content`, if a unique one exists.
2178+
*/
2179+
final Field getField() { result = unique( | | this.getAField()) }
2180+
2181+
override int getIndirectionIndex() { none() } // overridden in subclasses
2182+
2183+
override string toString() { none() } // overridden in subclasses
2184+
2185+
override predicate impliesClearOf(Content c) { none() } // overridden in subclasses
2186+
}
2187+
21652188
/** A reference through a non-union instance field. */
2166-
class NonUnionFieldContent extends Content, TNonUnionContent {
2189+
class NonUnionFieldContent extends FieldContent, TNonUnionContent {
21672190
private Field f;
21682191
private int indirectionIndex;
21692192

21702193
NonUnionFieldContent() { this = TNonUnionContent(f, indirectionIndex) }
21712194

21722195
override string toString() { result = contentStars(this) + f.toString() }
21732196

2174-
Field getField() { result = f }
2197+
override Field getAField() { result = f }
21752198

21762199
/** Gets the indirection index of this `FieldContent`. */
21772200
pragma[inline]
@@ -2191,7 +2214,7 @@ class NonUnionFieldContent extends Content, TNonUnionContent {
21912214
}
21922215

21932216
/** A reference through an instance field of a union. */
2194-
class UnionContent extends Content, TUnionContent {
2217+
class UnionContent extends FieldContent, TUnionContent {
21952218
private Union u;
21962219
private int indirectionIndex;
21972220
private int bytes;
@@ -2201,7 +2224,7 @@ class UnionContent extends Content, TUnionContent {
22012224
override string toString() { result = contentStars(this) + u.toString() }
22022225

22032226
/** Gets a field of the underlying union of this `UnionContent`, if any. */
2204-
Field getAField() { result = u.getAField() and getFieldSize(result) = bytes }
2227+
override Field getAField() { result = u.getAField() and getFieldSize(result) = bytes }
22052228

22062229
/** Gets the underlying union of this `UnionContent`. */
22072230
Union getUnion() { result = u }

cpp/ql/src/utils/modelgenerator/internal/CaptureModels.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,7 @@ private module SummaryModelGeneratorInput implements SummaryModelGeneratorInputS
340340
)
341341
}
342342

343-
predicate isField(DataFlow::ContentSet cs) {
344-
exists(DataFlow::Content c | cs.isSingleton(c) |
345-
c instanceof DataFlow::FieldContent or
346-
c instanceof DataFlow::UnionContent
347-
)
348-
}
343+
predicate isField(DataFlow::ContentSet cs) { cs.isSingleton(any(DataFlow::FieldContent fc)) }
349344

350345
predicate isCallback(DataFlow::ContentSet c) { none() }
351346

0 commit comments

Comments
 (0)