Skip to content

Commit 92a8c84

Browse files
committed
PS: Add predicates for getting the static type of a parameter.
1 parent ef36d6b commit 92a8c84

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

powershell/ql/lib/semmle/code/powershell/Type.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ class Type extends @type_definition, Stmt {
2222
}
2323

2424
Method getAMethod() { result = this.getMethod(_) }
25+
26+
TypeConstraint getBaseType(int i) { type_definition_base_type(this, i, result) }
27+
28+
TypeConstraint getABaseType() { result = this.getBaseType(_) }
29+
30+
Type getASubtype() { result.getABaseType().getName() = this.getName() }
2531
}

powershell/ql/lib/semmle/code/powershell/Variable.qll

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ private predicate isParameterImpl(string name, Scope scope) {
5353
name = "_"
5454
}
5555

56+
private predicate isThisParameter(Scope scope, Type t) {
57+
t = scope.getEnclosingFunction().getDeclaringType()
58+
}
59+
5660
private newtype TParameterImpl =
5761
TInternalParameter(Internal::Parameter p) or
5862
TUnderscore(Scope scope) {
5963
exists(VarAccess va | va.getUserPath() = ["_", "PSItem"] and scope = va.getEnclosingScope())
6064
} or
61-
TThisParameter(Scope scope) { exists(scope.getEnclosingFunction().getDeclaringType()) }
65+
TThisParameter(Scope scope) { isThisParameter(scope, _) }
6266

6367
private class ParameterImpl extends TParameterImpl {
6468
abstract Location getLocation();
@@ -88,6 +92,13 @@ private class ParameterImpl extends TParameterImpl {
8892
abstract predicate isPipeline();
8993

9094
abstract predicate isPipelineByPropertyName();
95+
96+
/**
97+
* Gets the static type of this parameter.
98+
* The type of this parameter at runtime may be a subtype of this static
99+
* type.
100+
*/
101+
abstract string getStaticType();
91102
}
92103

93104
private class InternalParameter extends ParameterImpl, TInternalParameter {
@@ -122,6 +133,8 @@ private class InternalParameter extends ParameterImpl, TInternalParameter {
122133
override predicate isPipelineByPropertyName() {
123134
this.getAnAttribute().getANamedArgument() instanceof ValueFromPipelineByPropertyName
124135
}
136+
137+
final override string getStaticType() { result = p.getStaticType() }
125138
}
126139

127140
/**
@@ -158,6 +171,8 @@ private class Underscore extends ParameterImpl, TUnderscore {
158171
final override predicate isPipelineByPropertyName() { none() }
159172

160173
final override predicate isFunctionParameter(Function f, int i) { f.getBody() = scope and i = -1 }
174+
175+
final override string getStaticType() { none() }
161176
}
162177

163178
private class ThisParameter extends ParameterImpl, TThisParameter {
@@ -176,6 +191,13 @@ private class ThisParameter extends ParameterImpl, TThisParameter {
176191
final override predicate isPipeline() { none() }
177192

178193
final override predicate isPipelineByPropertyName() { none() }
194+
195+
final override string getStaticType() {
196+
exists(Type t |
197+
isThisParameter(scope, t) and
198+
result = t.getName()
199+
)
200+
}
179201
}
180202

181203
private predicate isPipelineIteratorVariable(ParameterImpl p, ProcessBlock pb) {
@@ -305,6 +327,8 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
305327
predicate isPipeline() { p.isPipeline() }
306328

307329
predicate isPipelineByPropertyName() { p.isPipelineByPropertyName() }
330+
331+
string getStaticType() { result = p.getStaticType() }
308332
}
309333

310334
class PipelineParameter extends Parameter {

0 commit comments

Comments
 (0)