Skip to content

Commit 063398f

Browse files
committed
C#: Use synthetic fields for Task instead of referring to private fields.
1 parent 60f3ff8 commit 063398f

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/LibraryTypeDataFlow.qll

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ module AccessPath {
102102
result = singleton(any(FieldContent c | c.getField() = f.getUnboundDeclaration()))
103103
}
104104

105+
/** Gets a singleton synthetic field access path. */
106+
AccessPath synthetic(SyntheticField f) {
107+
result = singleton(any(SyntheticFieldContent c | c.getField() = f))
108+
}
109+
105110
/** Gets an access path representing a property inside a collection. */
106111
AccessPath properties(Property p) { result = TConsAccessPath(any(ElementContent c), property(p)) }
107112
}
@@ -1968,9 +1973,7 @@ class SystemThreadingTasksTaskTFlow extends LibraryTypeDataFlow, SystemThreading
19681973
source = TCallableFlowSourceQualifier() and
19691974
sourceAp = AccessPath::empty() and
19701975
sink = TCallableFlowSinkReturn() and
1971-
sinkAp =
1972-
AccessPath::field(any(SystemRuntimeCompilerServicesTaskAwaiterStruct s)
1973-
.getUnderlyingTaskField())
1976+
sinkAp = AccessPath::synthetic(any(SyntheticTaskAwaiterUnderlyingTaskField s))
19741977
or
19751978
// var awaitable = task.ConfigureAwait(false); // <-- new ConfiguredTaskAwaitable<>(task, false)
19761979
// // m_configuredTaskAwaiter = new ConfiguredTaskAwaiter(task, false)
@@ -1982,21 +1985,40 @@ class SystemThreadingTasksTaskTFlow extends LibraryTypeDataFlow, SystemThreading
19821985
sourceAp = AccessPath::empty() and
19831986
sink = TCallableFlowSinkReturn() and
19841987
sinkAp =
1985-
AccessPath::cons(any(FieldContent fc |
1986-
fc.getField() =
1987-
any(SystemRuntimeCompilerServicesConfiguredTaskAwaitableTStruct t)
1988-
.getUnderlyingAwaiterField()
1989-
),
1990-
AccessPath::field(any(SystemRuntimeCompilerServicesConfiguredTaskAwaitableTConfiguredTaskAwaiterStruct s
1991-
).getUnderlyingTaskField()))
1988+
AccessPath::cons(any(SyntheticFieldContent sfc |
1989+
sfc.getField() instanceof SyntheticConfiguredTaskAwaiterField
1990+
), AccessPath::synthetic(any(SyntheticConfiguredTaskAwaitableUnderlyingTaskField s)))
19921991
}
19931992

19941993
override predicate requiresAccessPath(Content head, AccessPath tail) {
1995-
head.(FieldContent).getField() =
1996-
any(SystemRuntimeCompilerServicesConfiguredTaskAwaitableTStruct t).getUnderlyingAwaiterField() and
1997-
tail =
1998-
AccessPath::field(any(SystemRuntimeCompilerServicesConfiguredTaskAwaitableTConfiguredTaskAwaiterStruct s
1999-
).getUnderlyingTaskField())
1994+
head.(SyntheticFieldContent).getField() instanceof SyntheticConfiguredTaskAwaiterField and
1995+
tail = AccessPath::synthetic(any(SyntheticConfiguredTaskAwaitableUnderlyingTaskField s))
1996+
}
1997+
}
1998+
1999+
abstract private class SyntheticTaskField extends SyntheticField {
2000+
bindingset[this]
2001+
SyntheticTaskField() { any() }
2002+
2003+
override Type getType() { result instanceof SystemThreadingTasksTaskTClass }
2004+
}
2005+
2006+
private class SyntheticTaskAwaiterUnderlyingTaskField extends SyntheticTaskField {
2007+
SyntheticTaskAwaiterUnderlyingTaskField() { this = "m_task_task_awaiter" }
2008+
}
2009+
2010+
private class SyntheticConfiguredTaskAwaitableUnderlyingTaskField extends SyntheticTaskField {
2011+
SyntheticConfiguredTaskAwaitableUnderlyingTaskField() {
2012+
this = "m_task_configured_task_awaitable"
2013+
}
2014+
}
2015+
2016+
private class SyntheticConfiguredTaskAwaiterField extends SyntheticField {
2017+
SyntheticConfiguredTaskAwaiterField() { this = "m_configuredTaskAwaiter" }
2018+
2019+
override Type getType() {
2020+
result instanceof
2021+
SystemRuntimeCompilerServicesConfiguredTaskAwaitableTConfiguredTaskAwaiterStruct
20002022
}
20012023
}
20022024

@@ -2012,9 +2034,7 @@ private class SystemRuntimeCompilerServicesConfiguredTaskAwaitableTFlow extends
20122034
// var result = awaiter.GetResult();
20132035
c = this.getGetAwaiterMethod() and
20142036
source = TCallableFlowSourceQualifier() and
2015-
sourceAp =
2016-
AccessPath::field(any(SystemRuntimeCompilerServicesConfiguredTaskAwaitableTStruct s)
2017-
.getUnderlyingAwaiterField()) and
2037+
sourceAp = AccessPath::synthetic(any(SyntheticConfiguredTaskAwaiterField s)) and
20182038
sink = TCallableFlowSinkReturn() and
20192039
sinkAp = AccessPath::empty() and
20202040
preservesValue = true
@@ -2113,14 +2133,15 @@ class SystemRuntimeCompilerServicesTaskAwaiterFlow extends LibraryTypeDataFlow,
21132133
c = this.getGetResultMethod() and
21142134
source = TCallableFlowSourceQualifier() and
21152135
sourceAp =
2116-
AccessPath::cons(any(FieldContent fc | fc.getField() = this.getUnderlyingTaskField()),
2117-
AccessPath::property(any(SystemThreadingTasksTaskTClass t).getResultProperty())) and
2136+
AccessPath::cons(any(SyntheticFieldContent sfc |
2137+
sfc.getField() instanceof SyntheticTaskAwaiterUnderlyingTaskField
2138+
), AccessPath::property(any(SystemThreadingTasksTaskTClass t).getResultProperty())) and
21182139
sink = TCallableFlowSinkReturn() and
21192140
sinkAp = AccessPath::empty()
21202141
}
21212142

21222143
override predicate requiresAccessPath(Content head, AccessPath tail) {
2123-
head.(FieldContent).getField() = this.getUnderlyingTaskField() and
2144+
head.(SyntheticFieldContent).getField() instanceof SyntheticTaskAwaiterUnderlyingTaskField and
21242145
tail = AccessPath::property(any(SystemThreadingTasksTaskTClass t).getResultProperty())
21252146
}
21262147
}
@@ -2139,14 +2160,16 @@ class SystemRuntimeCompilerServicesConfiguredTaskAwaitableTConfiguredTaskAwaiter
21392160
c = this.getGetResultMethod() and
21402161
source = TCallableFlowSourceQualifier() and
21412162
sourceAp =
2142-
AccessPath::cons(any(FieldContent fc | fc.getField() = this.getUnderlyingTaskField()),
2143-
AccessPath::property(any(SystemThreadingTasksTaskTClass t).getResultProperty())) and
2163+
AccessPath::cons(any(SyntheticFieldContent sfc |
2164+
sfc.getField() instanceof SyntheticConfiguredTaskAwaitableUnderlyingTaskField
2165+
), AccessPath::property(any(SystemThreadingTasksTaskTClass t).getResultProperty())) and
21442166
sink = TCallableFlowSinkReturn() and
21452167
sinkAp = AccessPath::empty()
21462168
}
21472169

21482170
override predicate requiresAccessPath(Content head, AccessPath tail) {
2149-
head.(FieldContent).getField() = this.getUnderlyingTaskField() and
2171+
head.(SyntheticFieldContent).getField() instanceof
2172+
SyntheticConfiguredTaskAwaitableUnderlyingTaskField and
21502173
tail = AccessPath::property(any(SystemThreadingTasksTaskTClass t).getResultProperty())
21512174
}
21522175
}

0 commit comments

Comments
 (0)