Skip to content

Commit 38319a4

Browse files
committed
C/C++: Make Content public as DataFlow::Content.
1 parent aa82d0b commit 38319a4

File tree

4 files changed

+108
-102
lines changed

4 files changed

+108
-102
lines changed

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -133,46 +133,6 @@ OutNode getAnOutNode(DataFlowCall call, ReturnKind kind) {
133133
*/
134134
predicate jumpStep(Node n1, Node n2) { none() }
135135

136-
private newtype TContent =
137-
TFieldContent(Field f) or
138-
TCollectionContent() or
139-
TArrayContent()
140-
141-
/**
142-
* A reference contained in an object. Examples include instance fields, the
143-
* contents of a collection object, or the contents of an array.
144-
*/
145-
class Content extends TContent {
146-
/** Gets a textual representation of this element. */
147-
abstract string toString();
148-
149-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
150-
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
151-
}
152-
}
153-
154-
private class FieldContent extends Content, TFieldContent {
155-
Field f;
156-
157-
FieldContent() { this = TFieldContent(f) }
158-
159-
Field getField() { result = f }
160-
161-
override string toString() { result = f.toString() }
162-
163-
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
164-
f.getLocation().hasLocationInfo(path, sl, sc, el, ec)
165-
}
166-
}
167-
168-
private class CollectionContent extends Content, TCollectionContent {
169-
override string toString() { result = "collection" }
170-
}
171-
172-
private class ArrayContent extends Content, TArrayContent {
173-
override string toString() { result = "array" }
174-
}
175-
176136
/**
177137
* Holds if data can flow from `node1` to `node2` via an assignment to `f`.
178138
* Thus, `node2` references an object with a field `f` that contains the

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowUtil.qll

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,50 @@ VariableAccess getAnAccessToAssignedVariable(Expr assign) {
768768
)
769769
}
770770

771+
private newtype TContent =
772+
TFieldContent(Field f) or
773+
TCollectionContent() or
774+
TArrayContent()
775+
776+
/**
777+
* A description of the way data may be stored inside an object. Examples
778+
* include instance fields, the contents of a collection object, or the contents
779+
* of an array.
780+
*/
781+
class Content extends TContent {
782+
/** Gets a textual representation of this element. */
783+
abstract string toString();
784+
785+
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
786+
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
787+
}
788+
}
789+
790+
/** A reference through an instance field. */
791+
class FieldContent extends Content, TFieldContent {
792+
Field f;
793+
794+
FieldContent() { this = TFieldContent(f) }
795+
796+
Field getField() { result = f }
797+
798+
override string toString() { result = f.toString() }
799+
800+
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
801+
f.getLocation().hasLocationInfo(path, sl, sc, el, ec)
802+
}
803+
}
804+
805+
/** A reference through an array. */
806+
private class ArrayContent extends Content, TArrayContent {
807+
override string toString() { result = "[]" }
808+
}
809+
810+
/** A reference through the contents of some collection-like container. */
811+
private class CollectionContent extends Content, TCollectionContent {
812+
override string toString() { result = "<element>" }
813+
}
814+
771815
/**
772816
* A guard that validates some expression.
773817
*

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

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -184,64 +184,6 @@ OutNode getAnOutNode(DataFlowCall call, ReturnKind kind) {
184184
*/
185185
predicate jumpStep(Node n1, Node n2) { none() }
186186

187-
/**
188-
* Gets a field corresponding to the bit range `[startBit..endBit)` of class `c`, if any.
189-
*/
190-
private Field getAField(Class c, int startBit, int endBit) {
191-
result.getDeclaringType() = c and
192-
startBit = 8 * result.getByteOffset() and
193-
endBit = 8 * result.getType().getSize() + startBit
194-
or
195-
exists(Field f, Class cInner |
196-
f = c.getAField() and
197-
cInner = f.getUnderlyingType() and
198-
result = getAField(cInner, startBit - 8 * f.getByteOffset(), endBit - 8 * f.getByteOffset())
199-
)
200-
}
201-
202-
private newtype TContent =
203-
TFieldContent(Class c, int startBit, int endBit) { exists(getAField(c, startBit, endBit)) } or
204-
TCollectionContent() or
205-
TArrayContent()
206-
207-
/**
208-
* A reference contained in an object. Examples include instance fields, the
209-
* contents of a collection object, or the contents of an array.
210-
*/
211-
class Content extends TContent {
212-
/** Gets a textual representation of this element. */
213-
abstract string toString();
214-
215-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
216-
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
217-
}
218-
}
219-
220-
private class FieldContent extends Content, TFieldContent {
221-
Class c;
222-
int startBit;
223-
int endBit;
224-
225-
FieldContent() { this = TFieldContent(c, startBit, endBit) }
226-
227-
// Ensure that there's just 1 result for `toString`.
228-
override string toString() { result = min(Field f | f = getAField() | f.toString()) }
229-
230-
predicate hasOffset(Class cl, int start, int end) { cl = c and start = startBit and end = endBit }
231-
232-
Field getAField() { result = getAField(c, startBit, endBit) }
233-
}
234-
235-
private class CollectionContent extends Content, TCollectionContent {
236-
override string toString() { result = "collection" }
237-
}
238-
239-
private class ArrayContent extends Content, TArrayContent {
240-
ArrayContent() { this = TArrayContent() }
241-
242-
override string toString() { result = "array content" }
243-
}
244-
245187
private predicate fieldStoreStepNoChi(Node node1, FieldContent f, PostUpdateNode node2) {
246188
exists(StoreInstruction store, Class c |
247189
store = node2.asInstruction() and
@@ -288,7 +230,7 @@ private predicate fieldStoreStepChi(Node node1, FieldContent f, PostUpdateNode n
288230
}
289231

290232
private predicate arrayStoreStepChi(Node node1, ArrayContent a, PostUpdateNode node2) {
291-
a = TArrayContent() and
233+
exists(a) and
292234
exists(ChiPartialOperand operand, ChiInstruction chi, StoreInstruction store |
293235
chi.getPartialOperand() = operand and
294236
store = operand.getDef() and
@@ -383,7 +325,7 @@ private predicate fieldReadStep(Node node1, FieldContent f, Node node2) {
383325
* predicate in `storeStep` ensures that we push the right `FieldContent` onto the access path.
384326
*/
385327
predicate suppressArrayRead(Node node1, ArrayContent a, Node node2) {
386-
a = TArrayContent() and
328+
exists(a) and
387329
exists(WriteSideEffectInstruction write, ChiInstruction chi |
388330
node1.asInstruction() = write and
389331
node2.asInstruction() = chi and
@@ -412,7 +354,7 @@ private Instruction skipCopyValueInstructions(Operand op) {
412354
}
413355

414356
private predicate arrayReadStep(Node node1, ArrayContent a, Node node2) {
415-
a = TArrayContent() and
357+
exists(a) and
416358
// Explicit dereferences such as `*p` or `p[i]` where `p` is a pointer or array.
417359
exists(LoadOperand operand, Instruction address |
418360
operand.isDefinitionInexact() and
@@ -443,7 +385,7 @@ private predicate arrayReadStep(Node node1, ArrayContent a, Node node2) {
443385
* from the access path.
444386
*/
445387
private predicate exactReadStep(Node node1, ArrayContent a, Node node2) {
446-
a = TArrayContent() and
388+
exists(a) and
447389
exists(WriteSideEffectInstruction write, ChiInstruction chi |
448390
not chi.isResultConflated() and
449391
chi.getPartial() = write and

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,66 @@ predicate localInstructionFlow(Instruction e1, Instruction e2) {
788788
*/
789789
predicate localExprFlow(Expr e1, Expr e2) { localFlow(exprNode(e1), exprNode(e2)) }
790790

791+
/**
792+
* Gets a field corresponding to the bit range `[startBit..endBit)` of class `c`, if any.
793+
*/
794+
private Field getAField(Class c, int startBit, int endBit) {
795+
result.getDeclaringType() = c and
796+
startBit = 8 * result.getByteOffset() and
797+
endBit = 8 * result.getType().getSize() + startBit
798+
or
799+
exists(Field f, Class cInner |
800+
f = c.getAField() and
801+
cInner = f.getUnderlyingType() and
802+
result = getAField(cInner, startBit - 8 * f.getByteOffset(), endBit - 8 * f.getByteOffset())
803+
)
804+
}
805+
806+
private newtype TContent =
807+
TFieldContent(Class c, int startBit, int endBit) { exists(getAField(c, startBit, endBit)) } or
808+
TCollectionContent() or
809+
TArrayContent()
810+
811+
/**
812+
* A description of the way data may be stored inside an object. Examples
813+
* include instance fields, the contents of a collection object, or the contents
814+
* of an array.
815+
*/
816+
class Content extends TContent {
817+
/** Gets a textual representation of this element. */
818+
abstract string toString();
819+
820+
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
821+
path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
822+
}
823+
}
824+
825+
/** A reference through an instance field. */
826+
class FieldContent extends Content, TFieldContent {
827+
Class c;
828+
int startBit;
829+
int endBit;
830+
831+
FieldContent() { this = TFieldContent(c, startBit, endBit) }
832+
833+
// Ensure that there's just 1 result for `toString`.
834+
override string toString() { result = min(Field f | f = getAField() | f.toString()) }
835+
836+
predicate hasOffset(Class cl, int start, int end) { cl = c and start = startBit and end = endBit }
837+
838+
Field getAField() { result = getAField(c, startBit, endBit) }
839+
}
840+
841+
/** A reference through an array. */
842+
class ArrayContent extends Content, TArrayContent {
843+
override string toString() { result = "[]" }
844+
}
845+
846+
/** A reference through the contents of some collection-like container. */
847+
private class CollectionContent extends Content, TCollectionContent {
848+
override string toString() { result = "<element>" }
849+
}
850+
791851
/**
792852
* A guard that validates some instruction.
793853
*

0 commit comments

Comments
 (0)