Skip to content

Commit 5370bb4

Browse files
authored
Merge branch 'main' into ben_refactoring
2 parents bbee2c9 + 41e4ada commit 5370bb4

File tree

4 files changed

+114
-35
lines changed

4 files changed

+114
-35
lines changed

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmValueConsumers/DirectAlgorithmValueConsumer.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ private import experimental.quantum.Language
33
private import experimental.quantum.OpenSSL.AlgorithmInstances.KnownAlgorithmConstants
44
private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgorithmValueConsumerBase
55

6-
// TODO: can self referential to itself, which is also an algorithm (Known algorithm)
76
/**
87
* Cases like EVP_MD5(),
98
* there is no input, rather it directly gets an algorithm

cpp/ql/lib/experimental/quantum/OpenSSL/AlgorithmValueConsumers/HashAlgorithmValueConsumer.qll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,34 @@ class EVP_Q_Digest_Algorithm_Consumer extends OpenSSLAlgorithmValueConsumer {
3030
none()
3131
}
3232
}
33+
34+
/**
35+
* The EVP digest algorithm getters
36+
* https://docs.openssl.org/3.0/man3/EVP_DigestInit/#synopsis
37+
*/
38+
class EVPDigestAlgorithmValueConsumer extends OpenSSLAlgorithmValueConsumer {
39+
DataFlow::Node valueArgNode;
40+
DataFlow::Node resultNode;
41+
42+
EVPDigestAlgorithmValueConsumer() {
43+
resultNode.asExpr() = this and
44+
isPossibleOpenSSLFunction(this.(Call).getTarget()) and
45+
(
46+
this.(Call).getTarget().getName() in [
47+
"EVP_get_digestbyname", "EVP_get_digestbynid", "EVP_get_digestbyobj"
48+
] and
49+
valueArgNode.asExpr() = this.(Call).getArgument(0)
50+
or
51+
this.(Call).getTarget().getName() = "EVP_MD_fetch" and
52+
valueArgNode.asExpr() = this.(Call).getArgument(1)
53+
)
54+
}
55+
56+
override DataFlow::Node getResultNode() { result = resultNode }
57+
58+
override Crypto::ConsumerInputDataFlowNode getInputNode() { result = valueArgNode }
59+
60+
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {
61+
exists(OpenSSLAlgorithmInstance i | i.getAVC() = this and result = i)
62+
}
63+
}

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,6 @@ private predicate typeEquality(AstNode n1, TypePath path1, AstNode n2, TypePath
229229
path1 = path2
230230
)
231231
or
232-
n2 =
233-
any(PrefixExpr pe |
234-
pe.getOperatorName() = "*" and
235-
pe.getExpr() = n1 and
236-
path1 = TypePath::cons(TRefTypeParameter(), path2)
237-
)
238-
or
239232
n1 = n2.(ParenExpr).getExpr() and
240233
path1 = path2
241234
or
@@ -261,12 +254,36 @@ private predicate typeEquality(AstNode n1, TypePath path1, AstNode n2, TypePath
261254
)
262255
}
263256

257+
bindingset[path1]
258+
private predicate typeEqualityLeft(AstNode n1, TypePath path1, AstNode n2, TypePath path2) {
259+
typeEquality(n1, path1, n2, path2)
260+
or
261+
n2 =
262+
any(PrefixExpr pe |
263+
pe.getOperatorName() = "*" and
264+
pe.getExpr() = n1 and
265+
path1.isCons(TRefTypeParameter(), path2)
266+
)
267+
}
268+
269+
bindingset[path2]
270+
private predicate typeEqualityRight(AstNode n1, TypePath path1, AstNode n2, TypePath path2) {
271+
typeEquality(n1, path1, n2, path2)
272+
or
273+
n2 =
274+
any(PrefixExpr pe |
275+
pe.getOperatorName() = "*" and
276+
pe.getExpr() = n1 and
277+
path1 = TypePath::cons(TRefTypeParameter(), path2)
278+
)
279+
}
280+
264281
pragma[nomagic]
265282
private Type inferTypeEquality(AstNode n, TypePath path) {
266283
exists(AstNode n2, TypePath path2 | result = inferType(n2, path2) |
267-
typeEquality(n, path, n2, path2)
284+
typeEqualityRight(n, path, n2, path2)
268285
or
269-
typeEquality(n2, path2, n, path)
286+
typeEqualityLeft(n2, path2, n, path)
270287
)
271288
}
272289

@@ -931,7 +948,7 @@ private Type inferRefExprType(Expr e, TypePath path) {
931948
e = re.getExpr() and
932949
exists(TypePath exprPath, TypePath refPath, Type exprType |
933950
result = inferType(re, exprPath) and
934-
exprPath = TypePath::cons(TRefTypeParameter(), refPath) and
951+
exprPath.isCons(TRefTypeParameter(), refPath) and
935952
exprType = inferType(e)
936953
|
937954
if exprType = TRefType()
@@ -945,8 +962,9 @@ private Type inferRefExprType(Expr e, TypePath path) {
945962

946963
pragma[nomagic]
947964
private Type inferTryExprType(TryExpr te, TypePath path) {
948-
exists(TypeParam tp |
949-
result = inferType(te.getExpr(), TypePath::cons(TTypeParamTypeParameter(tp), path))
965+
exists(TypeParam tp, TypePath path0 |
966+
result = inferType(te.getExpr(), path0) and
967+
path0.isCons(TTypeParamTypeParameter(tp), path)
950968
|
951969
tp = any(ResultEnum r).getGenericParamList().getGenericParam(0)
952970
or
@@ -1020,7 +1038,7 @@ private module Cached {
10201038
pragma[nomagic]
10211039
Type getTypeAt(TypePath path) {
10221040
exists(TypePath path0 | result = inferType(this, path0) |
1023-
path0 = TypePath::cons(TRefTypeParameter(), path)
1041+
path0.isCons(TRefTypeParameter(), path)
10241042
or
10251043
not path0.isCons(TRefTypeParameter(), _) and
10261044
not (path0.isEmpty() and result = TRefType()) and

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,29 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
181181
/** Holds if this type path is empty. */
182182
predicate isEmpty() { this = "" }
183183

184+
/** Gets the length of this path, assuming the length is at least 2. */
185+
bindingset[this]
186+
pragma[inline_late]
187+
private int lengthAtLeast2() {
188+
// Same as
189+
// `result = strictcount(this.indexOf(".")) + 1`
190+
// but performs better because it doesn't use an aggregate
191+
result = this.regexpReplaceAll("[0-9]+", "").length() + 1
192+
}
193+
184194
/** Gets the length of this path. */
185195
bindingset[this]
186196
pragma[inline_late]
187197
int length() {
188-
this.isEmpty() and result = 0
189-
or
190-
result = strictcount(this.indexOf(".")) + 1
198+
if this.isEmpty()
199+
then result = 0
200+
else
201+
if exists(TypeParameter::decode(this))
202+
then result = 1
203+
else result = this.lengthAtLeast2()
191204
}
192205

193206
/** Gets the path obtained by appending `suffix` onto this path. */
194-
bindingset[suffix, result]
195-
bindingset[this, result]
196207
bindingset[this, suffix]
197208
TypePath append(TypePath suffix) {
198209
if this.isEmpty()
@@ -202,21 +213,40 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
202213
then result = this
203214
else (
204215
result = this + "." + suffix and
205-
not result.length() > getTypePathLimit()
216+
(
217+
not exists(getTypePathLimit())
218+
or
219+
result.lengthAtLeast2() <= getTypePathLimit()
220+
)
206221
)
207222
}
208223

224+
/**
225+
* Gets the path obtained by appending `suffix` onto this path.
226+
*
227+
* Unlike `append`, this predicate has `result` in the binding set,
228+
* so there is no need to check the length of `result`.
229+
*/
230+
bindingset[this, result]
231+
TypePath appendInverse(TypePath suffix) { suffix = result.stripPrefix(this) }
232+
233+
/** Gets the path obtained by removing `prefix` from this path. */
234+
bindingset[this, prefix]
235+
TypePath stripPrefix(TypePath prefix) {
236+
if prefix.isEmpty()
237+
then result = this
238+
else (
239+
this = prefix and
240+
result.isEmpty()
241+
or
242+
this = prefix + "." + result
243+
)
244+
}
245+
209246
/** Holds if this path starts with `tp`, followed by `suffix`. */
210247
bindingset[this]
211248
predicate isCons(TypeParameter tp, TypePath suffix) {
212-
tp = TypeParameter::decode(this) and
213-
suffix.isEmpty()
214-
or
215-
exists(int first |
216-
first = min(this.indexOf(".")) and
217-
suffix = this.suffix(first + 1) and
218-
tp = TypeParameter::decode(this.prefix(first))
219-
)
249+
suffix = this.stripPrefix(TypePath::singleton(tp))
220250
}
221251
}
222252

@@ -232,7 +262,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
232262
* Gets the type path obtained by appending the singleton type path `tp`
233263
* onto `suffix`.
234264
*/
235-
bindingset[result]
236265
bindingset[suffix]
237266
TypePath cons(TypeParameter tp, TypePath suffix) { result = singleton(tp).append(suffix) }
238267
}
@@ -556,7 +585,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
556585
TypeMention tm1, TypeMention tm2, TypeParameter tp, TypePath path, Type t
557586
) {
558587
exists(TypePath prefix |
559-
tm2.resolveTypeAt(prefix) = tp and t = tm1.resolveTypeAt(prefix.append(path))
588+
tm2.resolveTypeAt(prefix) = tp and t = tm1.resolveTypeAt(prefix.appendInverse(path))
560589
)
561590
}
562591

@@ -899,7 +928,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
899928
exists(AccessPosition apos, DeclarationPosition dpos, TypePath pathToTypeParam |
900929
tp = target.getDeclaredType(dpos, pathToTypeParam) and
901930
accessDeclarationPositionMatch(apos, dpos) and
902-
adjustedAccessType(a, apos, target, pathToTypeParam.append(path), t)
931+
adjustedAccessType(a, apos, target, pathToTypeParam.appendInverse(path), t)
903932
)
904933
}
905934

@@ -998,7 +1027,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
9981027

9991028
RelevantAccess() { this = MkRelevantAccess(a, apos, path) }
10001029

1001-
Type getTypeAt(TypePath suffix) { a.getInferredType(apos, path.append(suffix)) = result }
1030+
Type getTypeAt(TypePath suffix) {
1031+
a.getInferredType(apos, path.appendInverse(suffix)) = result
1032+
}
10021033

10031034
/** Holds if this relevant access has the type `type` and should satisfy `constraint`. */
10041035
predicate hasTypeConstraint(Type type, Type constraint) {
@@ -1077,7 +1108,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
10771108
t0 = abs.getATypeParameter() and
10781109
exists(TypePath path3, TypePath suffix |
10791110
sub.resolveTypeAt(path3) = t0 and
1080-
at.getTypeAt(path3.append(suffix)) = t and
1111+
at.getTypeAt(path3.appendInverse(suffix)) = t and
10811112
path = prefix0.append(suffix)
10821113
)
10831114
)
@@ -1149,7 +1180,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
11491180
not exists(getTypeArgument(a, target, tp, _)) and
11501181
target = a.getTarget() and
11511182
exists(AccessPosition apos, DeclarationPosition dpos, Type base, TypePath pathToTypeParam |
1152-
accessBaseType(a, apos, base, pathToTypeParam.append(path), t) and
1183+
accessBaseType(a, apos, base, pathToTypeParam.appendInverse(path), t) and
11531184
declarationBaseType(target, dpos, base, pathToTypeParam, tp) and
11541185
accessDeclarationPositionMatch(apos, dpos)
11551186
)
@@ -1217,7 +1248,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12171248
typeParameterConstraintHasTypeParameter(target, dpos, pathToTp2, _, constraint, pathToTp,
12181249
tp) and
12191250
AccessConstraint::satisfiesConstraintTypeMention(a, apos, pathToTp2, constraint,
1220-
pathToTp.append(path), t)
1251+
pathToTp.appendInverse(path), t)
12211252
)
12221253
}
12231254

0 commit comments

Comments
 (0)