diff --git a/.github/workflows/check-change-note.yml b/.github/workflows/check-change-note.yml new file mode 100644 index 00000000000..e4e385a5319 --- /dev/null +++ b/.github/workflows/check-change-note.yml @@ -0,0 +1,21 @@ +on: + pull_request_target: + types: [labeled, unlabeled, opened, synchronize, reopened, ready_for_review] + paths: + - "*/ql/src/**/*.ql" + - "*/ql/src/**/*.qll" + - "!**/experimental/**" + +jobs: + check-change-note: + runs-on: ubuntu-latest + steps: + - name: Fail if no change note found. To fix, either add one, or add the `no-change-note-required` label. + if: | + github.event.pull_request.draft == false && + !contains(github.event.pull_request.labels.*.name, 'no-change-note-required') + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh api 'repos/${{github.repository}}/pulls/${{github.event.number}}/files' --paginate | + jq 'any(.[].filename ; test("/change-notes/.*[.]md$"))' --exit-status diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 7f218e99da4..8ddae33fd7b 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -5,10 +5,14 @@ on: branches: - main - 'rc/*' + paths: + - 'csharp/**' pull_request: branches: - main - 'rc/*' + paths: + - 'csharp/**' schedule: - cron: '0 9 * * 1' diff --git a/.github/workflows/generate-query-help-docs.yml b/.github/workflows/generate-query-help-docs.yml index 031bf97e728..e3af19c6dea 100644 --- a/.github/workflows/generate-query-help-docs.yml +++ b/.github/workflows/generate-query-help-docs.yml @@ -50,7 +50,7 @@ jobs: uses: ammaraskar/sphinx-action@8b4f60114d7fd1faeba1a712269168508d4750d2 # v0.4 with: docs-folder: "query-help/" - pre-build-command: "python -m pip install --upgrade recommonmark" + pre-build-command: "python -m pip install --upgrade recommonmark && python -m pip install --upgrade sphinx-markdown-tables" build-command: "sphinx-build -b dirhtml . _build" - name: Upload HTML artifacts uses: actions/upload-artifact@v2 diff --git a/CODEOWNERS b/CODEOWNERS index 64bda94db77..95f707722ae 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -3,18 +3,3 @@ /java/ @github/codeql-java /javascript/ @github/codeql-javascript /python/ @github/codeql-python - -# Assign query help for docs review -/cpp/**/*.qhelp @hubwriter -/csharp/**/*.qhelp @jf205 -/java/**/*.qhelp @felicitymay -/javascript/**/*.qhelp @mchammer01 -/python/**/*.qhelp @felicitymay -/docs/language/ @shati-patel @jf205 - -# Exclude help for experimental queries from docs review -/cpp/**/experimental/**/*.qhelp @github/codeql-c-analysis -/csharp/**/experimental/**/*.qhelp @github/codeql-csharp -/java/**/experimental/**/*.qhelp @github/codeql-java -/javascript/**/experimental/**/*.qhelp @github/codeql-javascript -/python/**/experimental/**/*.qhelp @github/codeql-python diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 39552ad0bd9..f9f6fe310a4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -49,7 +49,11 @@ If you have an idea for a query that you would like to share with other CodeQL u - The query must have at least one true positive result on some revision of a real project. -Experimental queries and libraries may not be actively maintained as the [supported](docs/supported-queries.md) libraries evolve. They may also be changed in backwards-incompatible ways or may be removed entirely in the future without deprecation warnings. +6. **Query help files and unit tests** + + - Query help (`.qhelp`) files and unit tests are optional (but strongly encouraged!) for queries in the `experimental` directories. For more information about contributing query help files and unit tests, see [Supported CodeQL queries and libraries](docs/supported-queries.md). + +Experimental queries and libraries may not be actively maintained as the supported libraries evolve. They may also be changed in backwards-incompatible ways or may be removed entirely in the future without deprecation warnings. After the experimental query is merged, we welcome pull requests to improve it. Before a query can be moved out of the `experimental` subdirectory, it must satisfy [the requirements for being a supported query](docs/supported-queries.md). diff --git a/config/identical-files.json b/config/identical-files.json index 5a8efc3405a..d68dabba861 100644 --- a/config/identical-files.json +++ b/config/identical-files.json @@ -356,6 +356,7 @@ ], "Inline Test Expectations": [ "cpp/ql/test/TestUtilities/InlineExpectationsTest.qll", + "java/ql/test/TestUtilities/InlineExpectationsTest.qll", "python/ql/test/TestUtilities/InlineExpectationsTest.qll" ], "C++ ExternalAPIs": [ diff --git a/cpp/ql/src/Security/CWE/CWE-020/ExternalAPIsSpecific.qll b/cpp/ql/src/Security/CWE/CWE-020/ExternalAPIsSpecific.qll index 788baeddbff..9ca598f86d6 100644 --- a/cpp/ql/src/Security/CWE/CWE-020/ExternalAPIsSpecific.qll +++ b/cpp/ql/src/Security/CWE/CWE-020/ExternalAPIsSpecific.qll @@ -46,7 +46,7 @@ class UntrustedDataToExternalAPIConfig extends TaintTracking::Configuration { UntrustedDataToExternalAPIConfig() { this = "UntrustedDataToExternalAPIConfig" } override predicate isSource(DataFlow::Node source) { - exists(RemoteFlowFunction remoteFlow | + exists(RemoteFlowSourceFunction remoteFlow | remoteFlow = source.asExpr().(Call).getTarget() and remoteFlow.hasRemoteFlowSource(_, _) ) diff --git a/cpp/ql/src/Security/CWE/CWE-079/CgiXss.ql b/cpp/ql/src/Security/CWE/CWE-079/CgiXss.ql index 24484a9dcaf..d1e2fa12913 100644 --- a/cpp/ql/src/Security/CWE/CWE-079/CgiXss.ql +++ b/cpp/ql/src/Security/CWE/CWE-079/CgiXss.ql @@ -34,6 +34,10 @@ class Configuration extends TaintTrackingConfiguration { override predicate isSink(Element tainted) { exists(PrintStdoutCall call | call.getAnArgument() = tainted) } + + override predicate isBarrier(Expr e) { + super.isBarrier(e) or e.getUnspecifiedType() instanceof IntegralType + } } from QueryString query, Element printedArg, PathNode sourceNode, PathNode sinkNode diff --git a/cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.ql b/cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.ql index fe9b56bd521..d883b2e7356 100644 --- a/cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.ql +++ b/cpp/ql/src/Security/CWE/CWE-676/DangerousFunctionOverflow.ql @@ -8,6 +8,7 @@ * @tags reliability * security * external/cwe/cwe-242 + * external/cwe/cwe-676 */ import cpp diff --git a/cpp/ql/src/semmle/code/cpp/Declaration.qll b/cpp/ql/src/semmle/code/cpp/Declaration.qll index 35ae092780d..7ac79fd99c1 100644 --- a/cpp/ql/src/semmle/code/cpp/Declaration.qll +++ b/cpp/ql/src/semmle/code/cpp/Declaration.qll @@ -139,6 +139,19 @@ class Declaration extends Locatable, @declaration { this.hasQualifiedName("std", "", name) } + /** + * Holds if this declaration has the given name in the global namespace, + * the `std` namespace or the `bsl` namespace. + * We treat `std` and `bsl` as the same in some of our models. + */ + predicate hasGlobalOrStdOrBslName(string name) { + this.hasGlobalName(name) + or + this.hasQualifiedName("std", "", name) + or + this.hasQualifiedName("bsl", "", name) + } + /** Gets a specifier of this declaration. */ Specifier getASpecifier() { none() } // overridden in subclasses diff --git a/cpp/ql/src/semmle/code/cpp/Function.qll b/cpp/ql/src/semmle/code/cpp/Function.qll index 4ddf852f49f..a5ef1f8ca5e 100644 --- a/cpp/ql/src/semmle/code/cpp/Function.qll +++ b/cpp/ql/src/semmle/code/cpp/Function.qll @@ -467,7 +467,7 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function { // ... and likewise for destructors. this.(Destructor).getADestruction().mayBeGloballyImpure() else - // Unless it's a function that we know is side-effect-free, it may + // Unless it's a function that we know is side-effect free, it may // have side-effects. not this.hasGlobalOrStdName([ "strcmp", "wcscmp", "_mbscmp", "strlen", "wcslen", "_mbslen", "_mbslen_l", "_mbstrlen", @@ -680,7 +680,7 @@ class FunctionDeclarationEntry extends DeclarationEntry, @fun_decl { /** * A C/C++ non-member function (a function that is not a member of any - * class). For example the in the following code, `MyFunction` is a + * class). For example, in the following code, `MyFunction` is a * `TopLevelFunction` but `MyMemberFunction` is not: * ``` * void MyFunction() { diff --git a/cpp/ql/src/semmle/code/cpp/commons/Scanf.qll b/cpp/ql/src/semmle/code/cpp/commons/Scanf.qll index 4b75bbe27d2..461030f389d 100644 --- a/cpp/ql/src/semmle/code/cpp/commons/Scanf.qll +++ b/cpp/ql/src/semmle/code/cpp/commons/Scanf.qll @@ -34,10 +34,10 @@ class Scanf extends ScanfFunction { Scanf() { this instanceof TopLevelFunction and ( - hasName("scanf") or // scanf(format, args...) - hasName("wscanf") or // wscanf(format, args...) - hasName("_scanf_l") or // _scanf_l(format, locale, args...) - hasName("_wscanf_l") // _wscanf_l(format, locale, args...) + hasGlobalOrStdOrBslName("scanf") or // scanf(format, args...) + hasGlobalOrStdOrBslName("wscanf") or // wscanf(format, args...) + hasGlobalName("_scanf_l") or // _scanf_l(format, locale, args...) + hasGlobalName("_wscanf_l") // _wscanf_l(format, locale, args...) ) } @@ -53,10 +53,10 @@ class Fscanf extends ScanfFunction { Fscanf() { this instanceof TopLevelFunction and ( - hasName("fscanf") or // fscanf(src_stream, format, args...) - hasName("fwscanf") or // fwscanf(src_stream, format, args...) - hasName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...) - hasName("_fwscanf_l") // _fwscanf_l(src_stream, format, locale, args...) + hasGlobalOrStdOrBslName("fscanf") or // fscanf(src_stream, format, args...) + hasGlobalOrStdOrBslName("fwscanf") or // fwscanf(src_stream, format, args...) + hasGlobalName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...) + hasGlobalName("_fwscanf_l") // _fwscanf_l(src_stream, format, locale, args...) ) } @@ -72,10 +72,10 @@ class Sscanf extends ScanfFunction { Sscanf() { this instanceof TopLevelFunction and ( - hasName("sscanf") or // sscanf(src_stream, format, args...) - hasName("swscanf") or // swscanf(src, format, args...) - hasName("_sscanf_l") or // _sscanf_l(src, format, locale, args...) - hasName("_swscanf_l") // _swscanf_l(src, format, locale, args...) + hasGlobalOrStdOrBslName("sscanf") or // sscanf(src_stream, format, args...) + hasGlobalOrStdOrBslName("swscanf") or // swscanf(src, format, args...) + hasGlobalName("_sscanf_l") or // _sscanf_l(src, format, locale, args...) + hasGlobalName("_swscanf_l") // _swscanf_l(src, format, locale, args...) ) } @@ -91,10 +91,10 @@ class Snscanf extends ScanfFunction { Snscanf() { this instanceof TopLevelFunction and ( - hasName("_snscanf") or // _snscanf(src, max_amount, format, args...) - hasName("_snwscanf") or // _snwscanf(src, max_amount, format, args...) - hasName("_snscanf_l") or // _snscanf_l(src, max_amount, format, locale, args...) - hasName("_snwscanf_l") // _snwscanf_l(src, max_amount, format, locale, args...) + hasGlobalName("_snscanf") or // _snscanf(src, max_amount, format, args...) + hasGlobalName("_snwscanf") or // _snwscanf(src, max_amount, format, args...) + hasGlobalName("_snscanf_l") or // _snscanf_l(src, max_amount, format, locale, args...) + hasGlobalName("_snwscanf_l") // _snwscanf_l(src, max_amount, format, locale, args...) // note that the max_amount is not a limit on the output length, it's an input length // limit used with non null-terminated strings. ) diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/AddressFlow.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/AddressFlow.qll index 0ef286e8ab5..7024f9d8598 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/AddressFlow.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/AddressFlow.qll @@ -131,7 +131,22 @@ private predicate lvalueToUpdate(Expr lvalue, Expr outer, ControlFlowNode node) exists(Call call | node = call | outer = call.getQualifier().getFullyConverted() and outer.getUnspecifiedType() instanceof Class and - not call.getTarget().hasSpecifier("const") + not ( + call.getTarget().hasSpecifier("const") and + // Given the following program: + // ``` + // struct C { + // void* data_; + // void* data() const { return data; } + // }; + // C c; + // memcpy(c.data(), source, 16) + // ``` + // the data pointed to by `c.data_` is potentially modified by the call to `memcpy` even though + // `C::data` has a const specifier. So we further place the restriction that the type returned + // by `call` should not be of the form `const T*` (for some deeply const type `T`). + call.getType().isDeeplyConstBelow() + ) ) or assignmentTo(outer, node) @@ -170,7 +185,11 @@ private predicate pointerToUpdate(Expr pointer, Expr outer, ControlFlowNode node or outer = call.getQualifier().getFullyConverted() and outer.getUnspecifiedType() instanceof PointerType and - not call.getTarget().hasSpecifier("const") + not ( + call.getTarget().hasSpecifier("const") and + // See the `lvalueToUpdate` case for an explanation of this conjunct. + call.getType().isDeeplyConstBelow() + ) ) or exists(PointerFieldAccess fa | diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl2.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl2.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl2.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl2.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl3.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl3.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl3.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl3.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl4.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl4.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl4.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl4.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImplLocal.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImplLocal.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImplLocal.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImplLocal.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl.qll b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl2.qll b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl2.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl2.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl2.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl3.qll b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl3.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl3.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl3.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl4.qll b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl4.qll index d9f5acdd279..59cc8d529a7 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl4.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowImpl4.qll @@ -3598,6 +3598,7 @@ private module FlowExploration { or exists(PartialPathNodeRev mid | revPartialPathStep(mid, node, sc1, sc2, ap, config) and + not clearsContent(node, ap.getHead()) and not fullBarrier(node, config) and distSink(node.getEnclosingCallable(), config) <= config.explorationLimit() ) @@ -3611,6 +3612,7 @@ private module FlowExploration { exists(PartialPathNodeFwd mid | partialPathStep(mid, node, cc, sc1, sc2, ap, config) and not fullBarrier(node, config) and + not clearsContent(node, ap.getHead().getContent()) and if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any() diff --git a/cpp/ql/src/semmle/code/cpp/models/Models.qll b/cpp/ql/src/semmle/code/cpp/models/Models.qll index 7ec66b3a2e9..d5c7f50dde1 100644 --- a/cpp/ql/src/semmle/code/cpp/models/Models.qll +++ b/cpp/ql/src/semmle/code/cpp/models/Models.qll @@ -28,3 +28,8 @@ private import implementations.Swap private import implementations.GetDelim private import implementations.SmartPointer private import implementations.Sscanf +private import implementations.Send +private import implementations.Recv +private import implementations.Accept +private import implementations.Poll +private import implementations.Select diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Accept.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Accept.qll new file mode 100644 index 00000000000..150f481de28 --- /dev/null +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Accept.qll @@ -0,0 +1,56 @@ +/** + * Provides implementation classes modeling `accept` and various similar + * functions. See `semmle.code.cpp.models.Models` for usage information. + */ + +import semmle.code.cpp.Function +import semmle.code.cpp.models.interfaces.ArrayFunction +import semmle.code.cpp.models.interfaces.Taint +import semmle.code.cpp.models.interfaces.Alias +import semmle.code.cpp.models.interfaces.SideEffect + +/** + * The function `accept` and its assorted variants + */ +private class Accept extends ArrayFunction, AliasFunction, TaintFunction, SideEffectFunction { + Accept() { this.hasGlobalName(["accept", "accept4", "WSAAccept"]) } + + override predicate hasArrayWithVariableSize(int bufParam, int countParam) { + bufParam = 1 and countParam = 2 + } + + override predicate hasArrayInput(int bufParam) { bufParam = 1 } + + override predicate hasArrayOutput(int bufParam) { bufParam = 1 } + + override predicate parameterNeverEscapes(int index) { exists(this.getParameter(index)) } + + override predicate parameterEscapesOnlyViaReturn(int index) { none() } + + override predicate parameterIsAlwaysReturned(int index) { none() } + + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { + (input.isParameter(0) or input.isParameterDeref(1)) and + (output.isReturnValue() or output.isParameterDeref(1)) + } + + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { + i = 1 and buffer = true and mustWrite = false + or + i = 2 and buffer = false and mustWrite = false + } + + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { + i = 0 and buffer = true + or + i = 1 and buffer = false + } + + override ParameterIndex getParameterSizeIndex(ParameterIndex i) { i = 1 and result = 2 } + + // NOTE: We implement thse two predicates as none because we can't model the low-level changes made to + // the structure pointed to by the file-descriptor argument. + override predicate hasOnlySpecificReadSideEffects() { none() } + + override predicate hasOnlySpecificWriteSideEffects() { none() } +} diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll index 91ec1525834..25dae1c2fd1 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll @@ -15,7 +15,7 @@ private class MallocAllocationFunction extends AllocationFunction { MallocAllocationFunction() { // --- C library allocation - hasGlobalOrStdName("malloc") and // malloc(size) + hasGlobalOrStdOrBslName("malloc") and // malloc(size) sizeArg = 0 or hasGlobalName([ @@ -104,7 +104,7 @@ private class CallocAllocationFunction extends AllocationFunction { CallocAllocationFunction() { // --- C library allocation - hasGlobalOrStdName("calloc") and // calloc(num, size) + hasGlobalOrStdOrBslName("calloc") and // calloc(num, size) sizeArg = 1 and multArg = 0 } @@ -124,7 +124,7 @@ private class ReallocAllocationFunction extends AllocationFunction { ReallocAllocationFunction() { // --- C library allocation - hasGlobalOrStdName("realloc") and // realloc(ptr, size) + hasGlobalOrStdOrBslName("realloc") and // realloc(ptr, size) sizeArg = 1 and reallocArg = 0 or diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Deallocation.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Deallocation.qll index b56c8c21949..6bd2916b733 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Deallocation.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Deallocation.qll @@ -13,9 +13,13 @@ private class StandardDeallocationFunction extends DeallocationFunction { int freedArg; StandardDeallocationFunction() { - hasGlobalName([ + hasGlobalOrStdOrBslName([ // --- C library allocation - "free", "realloc", + "free", "realloc" + ]) and + freedArg = 0 + or + hasGlobalName([ // --- OpenSSL memory allocation "CRYPTO_free", "CRYPTO_secure_free" ]) and diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Fread.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Fread.qll index 11568c8a974..df2d92fbc4f 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Fread.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Fread.qll @@ -1,8 +1,8 @@ import semmle.code.cpp.models.interfaces.Alias import semmle.code.cpp.models.interfaces.FlowSource -private class Fread extends AliasFunction, RemoteFlowFunction { - Fread() { this.hasGlobalName("fread") } +private class Fread extends AliasFunction, RemoteFlowSourceFunction { + Fread() { this.hasGlobalOrStdOrBslName("fread") } override predicate parameterNeverEscapes(int n) { n = 0 or diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/GetDelim.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/GetDelim.qll index 3561caee7fb..e2015406346 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/GetDelim.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/GetDelim.qll @@ -7,7 +7,7 @@ import semmle.code.cpp.models.interfaces.FlowSource * The standard functions `getdelim`, `getwdelim` and the glibc variant `__getdelim`. */ private class GetDelimFunction extends TaintFunction, AliasFunction, SideEffectFunction, - RemoteFlowFunction { + RemoteFlowSourceFunction { GetDelimFunction() { hasGlobalName(["getdelim", "getwdelim", "__getdelim"]) } override predicate hasTaintFlow(FunctionInput i, FunctionOutput o) { diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Getenv.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Getenv.qll index 9761a4293a8..87e191241d2 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Getenv.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Getenv.qll @@ -8,8 +8,8 @@ import semmle.code.cpp.models.interfaces.FlowSource /** * The POSIX function `getenv`. */ -class Getenv extends LocalFlowFunction { - Getenv() { this.hasGlobalName("getenv") } +class Getenv extends LocalFlowSourceFunction { + Getenv() { this.hasGlobalOrStdOrBslName("getenv") } override predicate hasLocalFlowSource(FunctionOutput output, string description) { ( diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Gets.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Gets.qll index 91653cd6b37..08222c2cd6a 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Gets.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Gets.qll @@ -14,12 +14,12 @@ import semmle.code.cpp.models.interfaces.FlowSource * The standard functions `gets` and `fgets`. */ private class GetsFunction extends DataFlowFunction, TaintFunction, ArrayFunction, AliasFunction, - SideEffectFunction, RemoteFlowFunction { + SideEffectFunction, RemoteFlowSourceFunction { GetsFunction() { // gets(str) // fgets(str, num, stream) // fgetws(wstr, num, stream) - hasGlobalOrStdName(["gets", "fgets", "fgetws"]) + hasGlobalOrStdOrBslName(["gets", "fgets", "fgetws"]) } override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { @@ -54,13 +54,13 @@ private class GetsFunction extends DataFlowFunction, TaintFunction, ArrayFunctio } override predicate hasArrayWithVariableSize(int bufParam, int countParam) { - not hasGlobalOrStdName("gets") and + not hasName("gets") and bufParam = 0 and countParam = 1 } override predicate hasArrayWithUnknownSize(int bufParam) { - hasGlobalOrStdName("gets") and + hasName("gets") and bufParam = 0 } diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/IdentityFunction.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/IdentityFunction.qll index bd188cffe49..60afd2b25ef 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/IdentityFunction.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/IdentityFunction.qll @@ -6,12 +6,9 @@ import semmle.code.cpp.models.interfaces.SideEffect /** * The standard function templates `std::move` and `std::forward`. */ -private class IdentityFunction extends DataFlowFunction, SideEffectFunction, AliasFunction { - IdentityFunction() { - this.getNamespace().getParentNamespace() instanceof GlobalNamespace and - this.getNamespace().getName() = "std" and - this.getName() = ["move", "forward"] - } +private class IdentityFunction extends DataFlowFunction, SideEffectFunction, AliasFunction, + FunctionTemplateInstantiation { + IdentityFunction() { this.hasQualifiedName("std", ["move", "forward"]) } override predicate hasOnlySpecificReadSideEffects() { any() } diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Iterator.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Iterator.qll index a5b1c0e83ec..24d5456293f 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Iterator.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Iterator.qll @@ -15,7 +15,7 @@ import semmle.code.cpp.models.interfaces.Iterator */ private class IteratorTraits extends Class { IteratorTraits() { - this.hasQualifiedName("std", "iterator_traits") and + this.hasQualifiedName(["std", "bsl"], "iterator_traits") and not this instanceof TemplateClass and exists(TypedefType t | this.getAMember() = t and @@ -26,6 +26,14 @@ private class IteratorTraits extends Class { Type getIteratorType() { result = this.getTemplateArgument(0) } } +/** + * A type that is deduced to be an iterator because there is a corresponding + * `std::iterator_traits` instantiation for it. + */ +private class IteratorByTraits extends Iterator { + IteratorByTraits() { exists(IteratorTraits it | it.getIteratorType() = this) } +} + /** * A type which has the typedefs expected for an iterator. */ @@ -36,7 +44,7 @@ private class IteratorByTypedefs extends Iterator, Class { this.getAMember().(TypedefType).hasName("pointer") and this.getAMember().(TypedefType).hasName("reference") and this.getAMember().(TypedefType).hasName("iterator_category") and - not this.hasQualifiedName("std", "iterator_traits") + not this.hasQualifiedName(["std", "bsl"], "iterator_traits") } } @@ -44,17 +52,13 @@ private class IteratorByTypedefs extends Iterator, Class { * The `std::iterator` class. */ private class StdIterator extends Iterator, Class { - StdIterator() { this.hasQualifiedName("std", "iterator") } + StdIterator() { this.hasQualifiedName(["std", "bsl"], "iterator") } } /** - * A type that is deduced to be an iterator because there is a corresponding - * `std::iterator_traits` instantiation for it. + * Gets the `FunctionInput` corresponding to an iterator parameter to + * user-defined operator `op`, at `index`. */ -private class IteratorByTraits extends Iterator { - IteratorByTraits() { exists(IteratorTraits it | it.getIteratorType() = this) } -} - private FunctionInput getIteratorArgumentInput(Operator op, int index) { exists(Type t | t = @@ -155,17 +159,21 @@ private class IteratorSubOperator extends Operator, TaintFunction { private class IteratorAssignArithmeticOperator extends Operator, DataFlowFunction, TaintFunction { IteratorAssignArithmeticOperator() { this.hasName(["operator+=", "operator-="]) and - this.getDeclaringType() instanceof Iterator + exists(getIteratorArgumentInput(this, 0)) } override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { input.isParameter(0) and output.isReturnValue() - or - input.isParameterDeref(0) and output.isReturnValueDeref() } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { + input.isParameterDeref(0) and output.isReturnValueDeref() + or + // reverse flow from returned reference to the object referenced by the first parameter + input.isReturnValueDeref() and + output.isParameterDeref(0) + or input.isParameterDeref(1) and output.isParameterDeref(0) } @@ -177,8 +185,7 @@ private class IteratorAssignArithmeticOperator extends Operator, DataFlowFunctio class IteratorPointerDereferenceMemberOperator extends MemberFunction, TaintFunction, IteratorReferenceFunction { IteratorPointerDereferenceMemberOperator() { - this.hasName("operator*") and - this.getDeclaringType() instanceof Iterator + this.getClassAndName("operator*") instanceof Iterator } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -195,8 +202,7 @@ class IteratorPointerDereferenceMemberOperator extends MemberFunction, TaintFunc */ private class IteratorCrementMemberOperator extends MemberFunction, DataFlowFunction, TaintFunction { IteratorCrementMemberOperator() { - this.hasName(["operator++", "operator--"]) and - this.getDeclaringType() instanceof Iterator + this.getClassAndName(["operator++", "operator--"]) instanceof Iterator } override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { @@ -220,10 +226,7 @@ private class IteratorCrementMemberOperator extends MemberFunction, DataFlowFunc * A member `operator->` function for an iterator type. */ private class IteratorFieldMemberOperator extends Operator, TaintFunction { - IteratorFieldMemberOperator() { - this.hasName("operator->") and - this.getDeclaringType() instanceof Iterator - } + IteratorFieldMemberOperator() { this.getClassAndName("operator->") instanceof Iterator } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { input.isQualifierObject() and @@ -236,8 +239,7 @@ private class IteratorFieldMemberOperator extends Operator, TaintFunction { */ private class IteratorBinaryArithmeticMemberOperator extends MemberFunction, TaintFunction { IteratorBinaryArithmeticMemberOperator() { - this.hasName(["operator+", "operator-"]) and - this.getDeclaringType() instanceof Iterator + this.getClassAndName(["operator+", "operator-"]) instanceof Iterator } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -252,21 +254,24 @@ private class IteratorBinaryArithmeticMemberOperator extends MemberFunction, Tai private class IteratorAssignArithmeticMemberOperator extends MemberFunction, DataFlowFunction, TaintFunction { IteratorAssignArithmeticMemberOperator() { - this.hasName(["operator+=", "operator-="]) and - this.getDeclaringType() instanceof Iterator + this.getClassAndName(["operator+=", "operator-="]) instanceof Iterator } override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { input.isQualifierAddress() and output.isReturnValue() - or - input.isReturnValueDeref() and - output.isQualifierObject() } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { input.isQualifierObject() and output.isReturnValueDeref() + or + // reverse flow from returned reference to the qualifier + input.isReturnValueDeref() and + output.isQualifierObject() + or + input.isParameterDeref(0) and + output.isQualifierObject() } } @@ -275,10 +280,7 @@ private class IteratorAssignArithmeticMemberOperator extends MemberFunction, Dat */ private class IteratorArrayMemberOperator extends MemberFunction, TaintFunction, IteratorReferenceFunction { - IteratorArrayMemberOperator() { - this.hasName("operator[]") and - this.getDeclaringType() instanceof Iterator - } + IteratorArrayMemberOperator() { this.getClassAndName("operator[]") instanceof Iterator } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { input.isQualifierObject() and @@ -295,8 +297,7 @@ private class IteratorArrayMemberOperator extends MemberFunction, TaintFunction, */ private class IteratorAssignmentMemberOperator extends MemberFunction, TaintFunction { IteratorAssignmentMemberOperator() { - this.hasName("operator=") and - this.getDeclaringType() instanceof Iterator and + this.getClassAndName("operator=") instanceof Iterator and not this instanceof CopyAssignmentOperator and not this instanceof MoveAssignmentOperator } @@ -337,7 +338,7 @@ private class BeginOrEndFunction extends MemberFunction, TaintFunction, GetItera */ private class InserterIteratorFunction extends GetIteratorFunction { InserterIteratorFunction() { - this.hasQualifiedName("std", ["front_inserter", "inserter", "back_inserter"]) + this.hasQualifiedName(["std", "bsl"], ["front_inserter", "inserter", "back_inserter"]) } override predicate getsIterator(FunctionInput input, FunctionOutput output) { diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Memcpy.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Memcpy.qll index 02fa810142f..b7d8aed60fa 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Memcpy.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Memcpy.qll @@ -20,7 +20,7 @@ private class MemcpyFunction extends ArrayFunction, DataFlowFunction, SideEffect // memcpy(dest, src, num) // memmove(dest, src, num) // memmove(dest, src, num, remaining) - this.hasGlobalOrStdName(["memcpy", "memmove"]) + this.hasGlobalOrStdOrBslName(["memcpy", "memmove"]) or // bcopy(src, dest, num) // mempcpy(dest, src, num) diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Memset.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Memset.qll index 6636b34fe9d..d646be0363d 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Memset.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Memset.qll @@ -15,7 +15,9 @@ import semmle.code.cpp.models.interfaces.SideEffect private class MemsetFunction extends ArrayFunction, DataFlowFunction, AliasFunction, SideEffectFunction { MemsetFunction() { - this.hasGlobalOrStdName(["memset", "wmemset"]) + this.hasGlobalOrStdOrBslName("memset") + or + this.hasGlobalOrStdName("wmemset") or this.hasGlobalName([bzero(), "__builtin_memset", "__builtin_memset_chk"]) } diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Poll.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Poll.qll new file mode 100644 index 00000000000..020bd6aaa51 --- /dev/null +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Poll.qll @@ -0,0 +1,44 @@ +/** + * Provides implementation classes modeling `poll` and various similar + * functions. See `semmle.code.cpp.models.Models` for usage information. + */ + +import semmle.code.cpp.Function +import semmle.code.cpp.models.interfaces.ArrayFunction +import semmle.code.cpp.models.interfaces.Alias +import semmle.code.cpp.models.interfaces.SideEffect + +/** + * The function `poll` and its assorted variants + */ +private class Poll extends ArrayFunction, AliasFunction, SideEffectFunction { + Poll() { this.hasGlobalName(["poll", "ppoll", "WSAPoll"]) } + + override predicate hasArrayWithVariableSize(int bufParam, int countParam) { + bufParam = 0 and countParam = 1 + } + + override predicate hasArrayInput(int bufParam) { bufParam = 0 } + + override predicate hasArrayOutput(int bufParam) { bufParam = 0 } + + override predicate parameterNeverEscapes(int index) { exists(this.getParameter(index)) } + + override predicate parameterEscapesOnlyViaReturn(int index) { none() } + + override predicate parameterIsAlwaysReturned(int index) { none() } + + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { + i = 0 and buffer = true and mustWrite = false + } + + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { + i = 0 and buffer = true + or + this.hasGlobalName("ppoll") and i = [2, 3] and buffer = false + } + + override predicate hasOnlySpecificReadSideEffects() { any() } + + override predicate hasOnlySpecificWriteSideEffects() { any() } +} diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Printf.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Printf.qll index 1767261efac..ed201a14587 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Printf.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Printf.qll @@ -15,7 +15,7 @@ private class Printf extends FormattingFunction, AliasFunction { Printf() { this instanceof TopLevelFunction and ( - hasGlobalOrStdName(["printf", "wprintf"]) or + hasGlobalOrStdOrBslName(["printf", "wprintf"]) or hasGlobalName(["printf_s", "wprintf_s", "g_printf"]) ) and not exists(getDefinition().getFile().getRelativePath()) @@ -23,10 +23,7 @@ private class Printf extends FormattingFunction, AliasFunction { override int getFormatParameterIndex() { result = 0 } - deprecated override predicate isWideCharDefault() { - hasGlobalOrStdName("wprintf") or - hasGlobalName("wprintf_s") - } + deprecated override predicate isWideCharDefault() { hasName(["wprintf", "wprintf_s"]) } override predicate isOutputGlobal() { any() } @@ -44,7 +41,7 @@ private class Fprintf extends FormattingFunction { Fprintf() { this instanceof TopLevelFunction and ( - hasGlobalOrStdName(["fprintf", "fwprintf"]) or + hasGlobalOrStdOrBslName(["fprintf", "fwprintf"]) or hasGlobalName("g_fprintf") ) and not exists(getDefinition().getFile().getRelativePath()) @@ -52,7 +49,7 @@ private class Fprintf extends FormattingFunction { override int getFormatParameterIndex() { result = 1 } - deprecated override predicate isWideCharDefault() { hasGlobalOrStdName("fwprintf") } + deprecated override predicate isWideCharDefault() { hasName("fwprintf") } override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = true } } @@ -64,7 +61,7 @@ private class Sprintf extends FormattingFunction { Sprintf() { this instanceof TopLevelFunction and ( - hasGlobalOrStdName([ + hasGlobalOrStdOrBslName([ "sprintf", // sprintf(dst, format, args...) "wsprintf" // wsprintf(dst, format, args...) ]) @@ -90,22 +87,20 @@ private class Sprintf extends FormattingFunction { } override int getFormatParameterIndex() { - hasGlobalName("g_strdup_printf") and result = 0 + hasName("g_strdup_printf") and result = 0 or - hasGlobalName("__builtin___sprintf_chk") and result = 3 + hasName("__builtin___sprintf_chk") and result = 3 or not getName() = ["g_strdup_printf", "__builtin___sprintf_chk"] and result = 1 } override int getOutputParameterIndex(boolean isStream) { - not hasGlobalName("g_strdup_printf") and result = 0 and isStream = false + not hasName("g_strdup_printf") and result = 0 and isStream = false } override int getFirstFormatArgumentIndex() { - if hasGlobalName("__builtin___sprintf_chk") - then result = 4 - else result = getNumberOfParameters() + if hasName("__builtin___sprintf_chk") then result = 4 else result = getNumberOfParameters() } } @@ -116,7 +111,7 @@ private class SnprintfImpl extends Snprintf { SnprintfImpl() { this instanceof TopLevelFunction and ( - hasGlobalOrStdName([ + hasGlobalOrStdOrBslName([ "snprintf", // C99 defines snprintf "swprintf" // The s version of wide-char printf is also always the n version ]) @@ -163,10 +158,7 @@ private class SnprintfImpl extends Snprintf { } override predicate returnsFullFormatLength() { - ( - hasGlobalOrStdName("snprintf") or - hasGlobalName(["g_snprintf", "__builtin___snprintf_chk", "snprintf_s"]) - ) and + hasName(["snprintf", "g_snprintf", "__builtin___snprintf_chk", "snprintf_s"]) and not exists(getDefinition().getFile().getRelativePath()) } diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Pure.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Pure.qll index 0dfa7fe6d76..d728a66463d 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Pure.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Pure.qll @@ -3,11 +3,14 @@ import semmle.code.cpp.models.interfaces.Taint import semmle.code.cpp.models.interfaces.Alias import semmle.code.cpp.models.interfaces.SideEffect -/** Pure string functions. */ +/** + * A function that operates on strings and is pure. That is, its evaluation is + * guaranteed to be side-effect free. + */ private class PureStrFunction extends AliasFunction, ArrayFunction, TaintFunction, SideEffectFunction { PureStrFunction() { - hasGlobalOrStdName([ + hasGlobalOrStdOrBslName([ atoi(), "strcasestr", "strchnul", "strchr", "strchrnul", "strstr", "strpbrk", "strrchr", "strspn", strtol(), strrev(), strcmp(), strlwr(), strupr() ]) @@ -89,10 +92,12 @@ private string strcmp() { ] } -/** String standard `strlen` function, and related functions for computing string lengths. */ +/** + * A function such as `strlen` that returns the length of the given string. + */ private class StrLenFunction extends AliasFunction, ArrayFunction, SideEffectFunction { StrLenFunction() { - hasGlobalOrStdName(["strlen", "strnlen", "wcslen"]) + hasGlobalOrStdOrBslName(["strlen", "strnlen", "wcslen"]) or hasGlobalName(["_mbslen", "_mbslen_l", "_mbstrlen", "_mbstrlen_l"]) } @@ -123,9 +128,12 @@ private class StrLenFunction extends AliasFunction, ArrayFunction, SideEffectFun } } -/** Pure functions. */ +/** + * A function that is pure, that is, its evaluation is guaranteed to be + * side-effect free. Excludes functions modeled by `PureStrFunction` and `PureMemFunction`. + */ private class PureFunction extends TaintFunction, SideEffectFunction { - PureFunction() { hasGlobalOrStdName(["abs", "labs"]) } + PureFunction() { hasGlobalOrStdOrBslName(["abs", "labs"]) } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { exists(ParameterIndex i | @@ -140,11 +148,14 @@ private class PureFunction extends TaintFunction, SideEffectFunction { override predicate hasOnlySpecificWriteSideEffects() { any() } } -/** Pure raw-memory functions. */ +/** + * A function that operates on memory buffers and is pure. That is, its + * evaluation is guaranteed to be side-effect free. + */ private class PureMemFunction extends AliasFunction, ArrayFunction, TaintFunction, SideEffectFunction { PureMemFunction() { - hasGlobalOrStdName([ + hasGlobalOrStdOrBslName([ "memchr", "__builtin_memchr", "memrchr", "rawmemchr", "memcmp", "__builtin_memcmp", "memmem" ]) or this.hasGlobalName("memfrob") diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Recv.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Recv.qll new file mode 100644 index 00000000000..691ba528f42 --- /dev/null +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Recv.qll @@ -0,0 +1,88 @@ +/** + * Provides implementation classes modeling `recv` and various similar + * functions. See `semmle.code.cpp.models.Models` for usage information. + */ + +import semmle.code.cpp.models.interfaces.Taint +import semmle.code.cpp.models.interfaces.ArrayFunction +import semmle.code.cpp.models.interfaces.Alias +import semmle.code.cpp.models.interfaces.FlowSource +import semmle.code.cpp.models.interfaces.SideEffect + +/** The function `recv` and its assorted variants */ +private class Recv extends AliasFunction, ArrayFunction, SideEffectFunction, + RemoteFlowSourceFunction { + Recv() { + this.hasGlobalName([ + "recv", // recv(socket, dest, len, flags) + "recvfrom", // recvfrom(socket, dest, len, flags, from, fromlen) + "recvmsg", // recvmsg(socket, msg, flags) + "read", // read(socket, dest, len) + "pread", // pread(socket, dest, len, offset) + "readv", // readv(socket, dest, len) + "preadv", // readv(socket, dest, len, offset) + "preadv2" // readv2(socket, dest, len, offset, flags) + ]) + } + + override predicate parameterNeverEscapes(int index) { + this.getParameter(index).getUnspecifiedType() instanceof PointerType + } + + override predicate parameterEscapesOnlyViaReturn(int index) { none() } + + override predicate parameterIsAlwaysReturned(int index) { none() } + + override predicate hasArrayWithVariableSize(int bufParam, int countParam) { + not this.hasGlobalName("recvmsg") and + bufParam = 1 and + countParam = 2 + } + + override predicate hasArrayInput(int bufParam) { this.hasGlobalName("recvfrom") and bufParam = 4 } + + override predicate hasArrayOutput(int bufParam) { + bufParam = 1 + or + this.hasGlobalName("recvfrom") and bufParam = 4 + } + + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { + this.hasGlobalName("recvfrom") and + ( + i = 4 and buffer = true + or + i = 5 and buffer = false + ) + or + this.hasGlobalName("recvmsg") and + i = 1 and + buffer = true + } + + override ParameterIndex getParameterSizeIndex(ParameterIndex i) { i = 1 and result = 2 } + + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { + i = 1 and buffer = true and mustWrite = false + or + this.hasGlobalName("recvfrom") and + ( + i = 4 and buffer = true and mustWrite = false + or + i = 5 and buffer = false and mustWrite = false + ) + } + + override predicate hasOnlySpecificReadSideEffects() { any() } + + override predicate hasOnlySpecificWriteSideEffects() { any() } + + override predicate hasRemoteFlowSource(FunctionOutput output, string description) { + ( + output.isParameterDeref(1) + or + this.hasGlobalName("recvfrom") and output.isParameterDeref([4, 5]) + ) and + description = "Buffer read by " + this.getName() + } +} diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Select.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Select.qll new file mode 100644 index 00000000000..b2120b241b9 --- /dev/null +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Select.qll @@ -0,0 +1,40 @@ +/** + * Provides implementation classes modeling `select` and various similar + * functions. See `semmle.code.cpp.models.Models` for usage information. + */ + +import semmle.code.cpp.Function +import semmle.code.cpp.models.interfaces.ArrayFunction +import semmle.code.cpp.models.interfaces.Alias +import semmle.code.cpp.models.interfaces.SideEffect + +/** + * The function `select` and its assorted variants + */ +private class Select extends ArrayFunction, AliasFunction, SideEffectFunction { + Select() { this.hasGlobalName(["select", "pselect"]) } + + override predicate hasArrayWithUnknownSize(int bufParam) { bufParam = [1 .. 3] } + + override predicate hasArrayInput(int bufParam) { bufParam = [1 .. 3] } + + override predicate hasArrayOutput(int bufParam) { bufParam = [1 .. 3] } + + override predicate parameterNeverEscapes(int index) { exists(this.getParameter(index)) } + + override predicate parameterEscapesOnlyViaReturn(int index) { none() } + + override predicate parameterIsAlwaysReturned(int index) { none() } + + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { + i = [1 .. 3] and buffer = true and mustWrite = false + } + + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { + i = [1 .. 5] and buffer = true + } + + override predicate hasOnlySpecificReadSideEffects() { any() } + + override predicate hasOnlySpecificWriteSideEffects() { any() } +} diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Send.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Send.qll new file mode 100644 index 00000000000..6086bc7748f --- /dev/null +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Send.qll @@ -0,0 +1,63 @@ +/** + * Provides implementation classes modeling `send` and various similar + * functions. See `semmle.code.cpp.models.Models` for usage information. + */ + +import semmle.code.cpp.models.interfaces.Taint +import semmle.code.cpp.models.interfaces.ArrayFunction +import semmle.code.cpp.models.interfaces.Alias +import semmle.code.cpp.models.interfaces.FlowSource +import semmle.code.cpp.models.interfaces.SideEffect + +/** The function `send` and its assorted variants */ +private class Send extends AliasFunction, ArrayFunction, SideEffectFunction, RemoteFlowSinkFunction { + Send() { + this.hasGlobalName([ + "send", // send(socket, buf, len, flags) + "sendto", // sendto(socket, buf, len, flags, to, tolen) + "sendmsg", // sendmsg(socket, msg, flags) + "write", // write(socket, buf, len) + "writev", // writev(socket, buf, len) + "pwritev", // pwritev(socket, buf, len, offset) + "pwritev2" // pwritev2(socket, buf, len, offset, flags) + ]) + } + + override predicate parameterNeverEscapes(int index) { + this.getParameter(index).getUnspecifiedType() instanceof PointerType + } + + override predicate parameterEscapesOnlyViaReturn(int index) { none() } + + override predicate parameterIsAlwaysReturned(int index) { none() } + + override predicate hasArrayWithVariableSize(int bufParam, int countParam) { + not this.hasGlobalName("sendmsg") and + bufParam = 1 and + countParam = 2 + } + + override predicate hasArrayInput(int bufParam) { bufParam = 1 } + + override predicate hasOnlySpecificReadSideEffects() { any() } + + override predicate hasOnlySpecificWriteSideEffects() { any() } + + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { + none() + } + + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { + i = 1 and buffer = true + or + this.hasGlobalName("sendto") and i = 4 and buffer = false + or + this.hasGlobalName("sendmsg") and i = 1 and buffer = true + } + + override ParameterIndex getParameterSizeIndex(ParameterIndex i) { i = 1 and result = 2 } + + override predicate hasRemoteFlowSink(FunctionInput input, string description) { + input.isParameterDeref(1) and description = "Buffer sent by " + this.getName() + } +} diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/SmartPointer.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/SmartPointer.qll index b13b4a8801d..c6152624792 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/SmartPointer.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/SmartPointer.qll @@ -4,14 +4,14 @@ import semmle.code.cpp.models.interfaces.Taint * The `std::shared_ptr` and `std::unique_ptr` template classes. */ private class UniqueOrSharedPtr extends Class { - UniqueOrSharedPtr() { this.hasQualifiedName("std", ["shared_ptr", "unique_ptr"]) } + UniqueOrSharedPtr() { this.hasQualifiedName(["std", "bsl"], ["shared_ptr", "unique_ptr"]) } } /** * The `std::make_shared` and `std::make_unique` template functions. */ private class MakeUniqueOrShared extends TaintFunction { - MakeUniqueOrShared() { this.hasQualifiedName("std", ["make_shared", "make_unique"]) } + MakeUniqueOrShared() { this.hasQualifiedName(["bsl", "std"], ["make_shared", "make_unique"]) } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // Exclude the specializations of `std::make_shared` and `std::make_unique` that allocate arrays diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/StdContainer.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/StdContainer.qll index e7abde30e8c..367db1613fc 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/StdContainer.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/StdContainer.qll @@ -5,6 +5,41 @@ import semmle.code.cpp.models.interfaces.Taint import semmle.code.cpp.models.interfaces.Iterator +/** + * The `std::array` template class. + */ +private class Array extends Class { + Array() { this.hasQualifiedName(["std", "bsl"], "array") } +} + +/** + * The `std::deque` template class. + */ +private class Deque extends Class { + Deque() { this.hasQualifiedName(["std", "bsl"], "deque") } +} + +/** + * The `std::forward_list` template class. + */ +private class ForwardList extends Class { + ForwardList() { this.hasQualifiedName(["std", "bsl"], "forward_list") } +} + +/** + * The `std::list` template class. + */ +private class List extends Class { + List() { this.hasQualifiedName(["std", "bsl"], "list") } +} + +/** + * The `std::vector` template class. + */ +private class Vector extends Class { + Vector() { this.hasQualifiedName(["std", "bsl"], "vector") } +} + /** * Additional model for standard container constructors that reference the * value type of the container (that is, the `T` in `std::vector`). For @@ -15,7 +50,10 @@ import semmle.code.cpp.models.interfaces.Iterator */ private class StdSequenceContainerConstructor extends Constructor, TaintFunction { StdSequenceContainerConstructor() { - this.getDeclaringType().hasQualifiedName("std", ["vector", "deque", "list", "forward_list"]) + this.getDeclaringType() instanceof Vector or + this.getDeclaringType() instanceof Deque or + this.getDeclaringType() instanceof List or + this.getDeclaringType() instanceof ForwardList } /** @@ -50,7 +88,10 @@ private class StdSequenceContainerConstructor extends Constructor, TaintFunction * The standard container function `data`. */ private class StdSequenceContainerData extends TaintFunction { - StdSequenceContainerData() { this.hasQualifiedName("std", ["array", "vector"], "data") } + StdSequenceContainerData() { + this.getClassAndName("data") instanceof Array or + this.getClassAndName("data") instanceof Vector + } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from container itself (qualifier) to return value @@ -69,8 +110,10 @@ private class StdSequenceContainerData extends TaintFunction { */ private class StdSequenceContainerPush extends TaintFunction { StdSequenceContainerPush() { - this.hasQualifiedName("std", ["vector", "deque", "list"], "push_back") or - this.hasQualifiedName("std", ["deque", "list", "forward_list"], "push_front") + this.getClassAndName("push_back") instanceof Vector or + this.getClassAndName(["push_back", "push_front"]) instanceof Deque or + this.getClassAndName("push_front") instanceof ForwardList or + this.getClassAndName(["push_back", "push_front"]) instanceof List } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -85,8 +128,11 @@ private class StdSequenceContainerPush extends TaintFunction { */ private class StdSequenceContainerFrontBack extends TaintFunction { StdSequenceContainerFrontBack() { - this.hasQualifiedName("std", ["array", "vector", "deque", "list", "forward_list"], "front") or - this.hasQualifiedName("std", ["array", "vector", "deque", "list"], "back") + this.getClassAndName(["front", "back"]) instanceof Array or + this.getClassAndName(["front", "back"]) instanceof Deque or + this.getClassAndName("front") instanceof ForwardList or + this.getClassAndName(["front", "back"]) instanceof List or + this.getClassAndName(["front", "back"]) instanceof Vector } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -101,8 +147,10 @@ private class StdSequenceContainerFrontBack extends TaintFunction { */ private class StdSequenceContainerInsert extends TaintFunction { StdSequenceContainerInsert() { - this.hasQualifiedName("std", ["vector", "deque", "list"], "insert") or - this.hasQualifiedName("std", "forward_list", "insert_after") + this.getClassAndName("insert") instanceof Deque or + this.getClassAndName("insert") instanceof List or + this.getClassAndName("insert") instanceof Vector or + this.getClassAndName("insert_after") instanceof ForwardList } /** @@ -138,7 +186,10 @@ private class StdSequenceContainerInsert extends TaintFunction { */ private class StdSequenceContainerAssign extends TaintFunction { StdSequenceContainerAssign() { - this.hasQualifiedName("std", ["vector", "deque", "list", "forward_list"], "assign") + this.getClassAndName("assign") instanceof Deque or + this.getClassAndName("assign") instanceof ForwardList or + this.getClassAndName("assign") instanceof List or + this.getClassAndName("assign") instanceof Vector } /** @@ -170,7 +221,9 @@ private class StdSequenceContainerAssign extends TaintFunction { */ private class StdSequenceContainerAt extends TaintFunction { StdSequenceContainerAt() { - this.hasQualifiedName("std", ["vector", "array", "deque"], ["at", "operator[]"]) + this.getClassAndName(["at", "operator[]"]) instanceof Array or + this.getClassAndName(["at", "operator[]"]) instanceof Deque or + this.getClassAndName(["at", "operator[]"]) instanceof Vector } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -188,7 +241,7 @@ private class StdSequenceContainerAt extends TaintFunction { * The standard vector `emplace` function. */ class StdVectorEmplace extends TaintFunction { - StdVectorEmplace() { this.hasQualifiedName("std", "vector", "emplace") } + StdVectorEmplace() { this.getClassAndName("emplace") instanceof Vector } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from any parameter except the position iterator to qualifier and return value @@ -205,7 +258,7 @@ class StdVectorEmplace extends TaintFunction { * The standard vector `emplace_back` function. */ class StdVectorEmplaceBack extends TaintFunction { - StdVectorEmplaceBack() { this.hasQualifiedName("std", "vector", "emplace_back") } + StdVectorEmplaceBack() { this.getClassAndName("emplace_back") instanceof Vector } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from any parameter to qualifier diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/StdMap.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/StdMap.qll index f261038735d..aecd98981e8 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/StdMap.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/StdMap.qll @@ -5,14 +5,18 @@ import semmle.code.cpp.models.interfaces.Taint import semmle.code.cpp.models.interfaces.Iterator +/** + * The `std::map` and `std::unordered_map` template classes. + */ +private class MapOrUnorderedMap extends Class { + MapOrUnorderedMap() { this.hasQualifiedName(["std", "bsl"], ["map", "unordered_map"]) } +} + /** * Additional model for map constructors using iterator inputs. */ private class StdMapConstructor extends Constructor, TaintFunction { - StdMapConstructor() { - this.hasQualifiedName("std", "map", "map") or - this.hasQualifiedName("std", "unordered_map", "unordered_map") - } + StdMapConstructor() { this.getDeclaringType() instanceof MapOrUnorderedMap } /** * Gets the index of a parameter to this function that is an iterator. @@ -37,7 +41,7 @@ private class StdMapConstructor extends Constructor, TaintFunction { */ private class StdMapInsert extends TaintFunction { StdMapInsert() { - this.hasQualifiedName("std", ["map", "unordered_map"], ["insert", "insert_or_assign"]) + this.getClassAndName(["insert", "insert_or_assign"]) instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -55,9 +59,7 @@ private class StdMapInsert extends TaintFunction { * The standard map `emplace` and `emplace_hint` functions. */ private class StdMapEmplace extends TaintFunction { - StdMapEmplace() { - this.hasQualifiedName("std", ["map", "unordered_map"], ["emplace", "emplace_hint"]) - } + StdMapEmplace() { this.getClassAndName(["emplace", "emplace_hint"]) instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from the last parameter (which may be the value part used to @@ -79,7 +81,7 @@ private class StdMapEmplace extends TaintFunction { * The standard map `try_emplace` function. */ private class StdMapTryEmplace extends TaintFunction { - StdMapTryEmplace() { this.hasQualifiedName("std", ["map", "unordered_map"], "try_emplace") } + StdMapTryEmplace() { this.getClassAndName("try_emplace") instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from any parameter apart from the key to qualifier and return value @@ -106,7 +108,7 @@ private class StdMapTryEmplace extends TaintFunction { * The standard map `merge` function. */ private class StdMapMerge extends TaintFunction { - StdMapMerge() { this.hasQualifiedName("std", ["map", "unordered_map"], "merge") } + StdMapMerge() { this.getClassAndName("merge") instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // container1.merge(container2) @@ -119,7 +121,7 @@ private class StdMapMerge extends TaintFunction { * The standard map functions `at` and `operator[]`. */ private class StdMapAt extends TaintFunction { - StdMapAt() { this.hasQualifiedName("std", ["map", "unordered_map"], ["at", "operator[]"]) } + StdMapAt() { this.getClassAndName(["at", "operator[]"]) instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from qualifier to referenced return value @@ -136,7 +138,7 @@ private class StdMapAt extends TaintFunction { * The standard map `find` function. */ private class StdMapFind extends TaintFunction { - StdMapFind() { this.hasQualifiedName("std", ["map", "unordered_map"], "find") } + StdMapFind() { this.getClassAndName("find") instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { input.isQualifierObject() and @@ -148,7 +150,7 @@ private class StdMapFind extends TaintFunction { * The standard map `erase` function. */ private class StdMapErase extends TaintFunction { - StdMapErase() { this.hasQualifiedName("std", ["map", "unordered_map"], "erase") } + StdMapErase() { this.getClassAndName("erase") instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from qualifier to iterator return value @@ -163,8 +165,7 @@ private class StdMapErase extends TaintFunction { */ private class StdMapEqualRange extends TaintFunction { StdMapEqualRange() { - this.hasQualifiedName("std", ["map", "unordered_map"], - ["lower_bound", "upper_bound", "equal_range"]) + this.getClassAndName(["lower_bound", "upper_bound", "equal_range"]) instanceof MapOrUnorderedMap } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/StdPair.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/StdPair.qll index 41867e71acc..755f6a48520 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/StdPair.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/StdPair.qll @@ -7,10 +7,16 @@ import semmle.code.cpp.models.interfaces.Taint /** * An instantiation of `std::pair`. */ -class StdPairClass extends ClassTemplateInstantiation { - StdPairClass() { getTemplate().hasQualifiedName("std", "pair") } +private class StdPair extends ClassTemplateInstantiation { + StdPair() { this.hasQualifiedName(["std", "bsl"], "pair") } } +/** + * DEPRECATED: This is now called `StdPair` and is a private part of the + * library implementation. + */ +deprecated class StdPairClass = StdPair; + /** * Any of the single-parameter constructors of `std::pair` that takes a reference to an * instantiation of `std::pair`. These constructors allow conversion between pair types when the @@ -18,9 +24,9 @@ class StdPairClass extends ClassTemplateInstantiation { */ class StdPairCopyishConstructor extends Constructor, TaintFunction { StdPairCopyishConstructor() { - this.getDeclaringType() instanceof StdPairClass and + this.getDeclaringType() instanceof StdPair and this.getNumberOfParameters() = 1 and - this.getParameter(0).getUnspecifiedType().(ReferenceType).getBaseType() instanceof StdPairClass + this.getParameter(0).getUnspecifiedType().(ReferenceType).getBaseType() instanceof StdPair } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { @@ -38,7 +44,7 @@ class StdPairCopyishConstructor extends Constructor, TaintFunction { * Additional model for `std::pair` constructors. */ private class StdPairConstructor extends Constructor, TaintFunction { - StdPairConstructor() { this.hasQualifiedName("std", "pair", "pair") } + StdPairConstructor() { this.getDeclaringType() instanceof StdPair } /** * Gets the index of a parameter to this function that is a reference to diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/StdSet.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/StdSet.qll index 9fac9ae75f8..d2e9892abcb 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/StdSet.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/StdSet.qll @@ -5,14 +5,18 @@ import semmle.code.cpp.models.interfaces.Taint import semmle.code.cpp.models.interfaces.Iterator +/** + * An instantiation of `std::set` or `std::unordered_set`. + */ +private class StdSet extends ClassTemplateInstantiation { + StdSet() { this.hasQualifiedName(["std", "bsl"], ["set", "unordered_set"]) } +} + /** * Additional model for set constructors using iterator inputs. */ private class StdSetConstructor extends Constructor, TaintFunction { - StdSetConstructor() { - this.hasQualifiedName("std", "set", "set") or - this.hasQualifiedName("std", "unordered_set", "unordered_set") - } + StdSetConstructor() { this.getDeclaringType() instanceof StdSet } /** * Gets the index of a parameter to this function that is an iterator. @@ -36,7 +40,7 @@ private class StdSetConstructor extends Constructor, TaintFunction { * The standard set `insert` and `insert_or_assign` functions. */ private class StdSetInsert extends TaintFunction { - StdSetInsert() { this.hasQualifiedName("std", ["set", "unordered_set"], "insert") } + StdSetInsert() { this.getClassAndName("insert") instanceof StdSet } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from last parameter to qualifier and return value @@ -53,9 +57,7 @@ private class StdSetInsert extends TaintFunction { * The standard set `emplace` and `emplace_hint` functions. */ private class StdSetEmplace extends TaintFunction { - StdSetEmplace() { - this.hasQualifiedName("std", ["set", "unordered_set"], ["emplace", "emplace_hint"]) - } + StdSetEmplace() { this.getClassAndName(["emplace", "emplace_hint"]) instanceof StdSet } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from any parameter to qualifier and return value @@ -76,7 +78,7 @@ private class StdSetEmplace extends TaintFunction { * The standard set `merge` function. */ private class StdSetMerge extends TaintFunction { - StdSetMerge() { this.hasQualifiedName("std", ["set", "unordered_set"], "merge") } + StdSetMerge() { this.getClassAndName("merge") instanceof StdSet } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // container1.merge(container2) @@ -89,7 +91,7 @@ private class StdSetMerge extends TaintFunction { * The standard set `find` function. */ private class StdSetFind extends TaintFunction { - StdSetFind() { this.hasQualifiedName("std", ["set", "unordered_set"], "find") } + StdSetFind() { this.getClassAndName("find") instanceof StdSet } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { input.isQualifierObject() and @@ -101,7 +103,7 @@ private class StdSetFind extends TaintFunction { * The standard set `erase` function. */ private class StdSetErase extends TaintFunction { - StdSetErase() { this.hasQualifiedName("std", ["set", "unordered_set"], "erase") } + StdSetErase() { this.getClassAndName("erase") instanceof StdSet } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { // flow from qualifier to iterator return value @@ -116,8 +118,7 @@ private class StdSetErase extends TaintFunction { */ private class StdSetEqualRange extends TaintFunction { StdSetEqualRange() { - this.hasQualifiedName("std", ["set", "unordered_set"], - ["lower_bound", "upper_bound", "equal_range"]) + this.getClassAndName(["lower_bound", "upper_bound", "equal_range"]) instanceof StdSet } override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/StdString.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/StdString.qll index 9f4c458815f..73a0f6edf26 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/StdString.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/StdString.qll @@ -12,7 +12,7 @@ import semmle.code.cpp.models.interfaces.DataFlow * The `std::basic_string` template class instantiations. */ private class StdBasicString extends ClassTemplateInstantiation { - StdBasicString() { this.hasQualifiedName("std", "basic_string") } + StdBasicString() { this.hasQualifiedName(["std", "bsl"], "basic_string") } } /** @@ -127,7 +127,7 @@ private class StdStringFrontBack extends TaintFunction { */ private class StdStringPlus extends TaintFunction { StdStringPlus() { - this.hasQualifiedName("std", "operator+") and + this.hasQualifiedName(["std", "bsl"], "operator+") and this.getUnspecifiedType() instanceof StdBasicString } @@ -252,13 +252,6 @@ private class StdStringSubstr extends TaintFunction { } } -/** - * The `std::basic_stringstream` template class instantiations. - */ -private class StdBasicStringStream extends ClassTemplateInstantiation { - StdBasicStringStream() { this.hasQualifiedName("std", "basic_stringstream") } -} - /** * The `std::string` functions `at` and `operator[]`. */ @@ -280,7 +273,7 @@ private class StdStringAt extends TaintFunction { * The `std::basic_istream` template class instantiations. */ private class StdBasicIStream extends ClassTemplateInstantiation { - StdBasicIStream() { this.hasQualifiedName("std", "basic_istream") } + StdBasicIStream() { this.hasQualifiedName(["std", "bsl"], "basic_istream") } } /** @@ -314,7 +307,7 @@ private class StdIStreamIn extends DataFlowFunction, TaintFunction { */ private class StdIStreamInNonMember extends DataFlowFunction, TaintFunction { StdIStreamInNonMember() { - this.hasQualifiedName("std", "operator>>") and + this.hasQualifiedName(["std", "bsl"], "operator>>") and this.getUnspecifiedType().(ReferenceType).getBaseType() instanceof StdBasicIStream } @@ -462,7 +455,7 @@ private class StdIStreamGetLine extends DataFlowFunction, TaintFunction { * The (non-member) function `std::getline`. */ private class StdGetLine extends DataFlowFunction, TaintFunction { - StdGetLine() { this.hasQualifiedName("std", "getline") } + StdGetLine() { this.hasQualifiedName(["std", "bsl"], "getline") } override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { // flow from first parameter to return value @@ -488,7 +481,7 @@ private class StdGetLine extends DataFlowFunction, TaintFunction { * The `std::basic_ostream` template class instantiations. */ private class StdBasicOStream extends ClassTemplateInstantiation { - StdBasicOStream() { this.hasQualifiedName("std", "basic_ostream") } + StdBasicOStream() { this.hasQualifiedName(["std", "bsl"], "basic_ostream") } } /** @@ -535,7 +528,7 @@ private class StdOStreamOut extends DataFlowFunction, TaintFunction { */ private class StdOStreamOutNonMember extends DataFlowFunction, TaintFunction { StdOStreamOutNonMember() { - this.hasQualifiedName("std", "operator<<") and + this.hasQualifiedName(["std", "bsl"], "operator<<") and this.getUnspecifiedType().(ReferenceType).getBaseType() instanceof StdBasicOStream } @@ -563,6 +556,13 @@ private class StdOStreamOutNonMember extends DataFlowFunction, TaintFunction { } } +/** + * The `std::basic_stringstream` template class instantiations. + */ +private class StdBasicStringStream extends ClassTemplateInstantiation { + StdBasicStringStream() { this.hasQualifiedName(["std", "bsl"], "basic_stringstream") } +} + /** * Additional model for `std::stringstream` constructors that take a string * input parameter. @@ -609,7 +609,7 @@ private class StdStringStreamStr extends TaintFunction { * The `std::basic_ios` template class instantiations. */ private class StdBasicIOS extends ClassTemplateInstantiation { - StdBasicIOS() { this.hasQualifiedName("std", "basic_ios") } + StdBasicIOS() { this.hasQualifiedName(["std", "bsl"], "basic_ios") } } /** diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Strcat.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Strcat.qll index f31d688d010..ee9af547582 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Strcat.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Strcat.qll @@ -13,7 +13,7 @@ import semmle.code.cpp.models.interfaces.SideEffect */ class StrcatFunction extends TaintFunction, DataFlowFunction, ArrayFunction, SideEffectFunction { StrcatFunction() { - this.hasGlobalOrStdName([ + this.hasGlobalOrStdOrBslName([ "strcat", // strcat(dst, src) "strncat", // strncat(dst, src, max_amount) "wcscat", // wcscat(dst, src) diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Strcpy.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Strcpy.qll index e01d364c5a7..432fbf999ef 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Strcpy.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Strcpy.qll @@ -13,7 +13,7 @@ import semmle.code.cpp.models.interfaces.SideEffect */ class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction, SideEffectFunction { StrcpyFunction() { - this.hasGlobalOrStdName([ + this.hasGlobalOrStdOrBslName([ "strcpy", // strcpy(dst, src) "wcscpy", // wcscpy(dst, src) "strncpy", // strncpy(dst, src, max_amount) diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Strtok.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Strtok.qll index 659494ef2dd..f2cb6498819 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Strtok.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Strtok.qll @@ -15,7 +15,7 @@ import semmle.code.cpp.models.interfaces.Taint */ private class Strtok extends ArrayFunction, AliasFunction, TaintFunction, SideEffectFunction { Strtok() { - this.hasGlobalOrStdName("strtok") or + this.hasGlobalOrStdOrBslName("strtok") or this.hasGlobalName(["strtok_r", "_strtok_l", "wcstok", "_wcstok_l", "_mbstok", "_mbstok_l"]) } diff --git a/cpp/ql/src/semmle/code/cpp/models/implementations/Swap.qll b/cpp/ql/src/semmle/code/cpp/models/implementations/Swap.qll index 285d20b8660..b79f7afe5d9 100644 --- a/cpp/ql/src/semmle/code/cpp/models/implementations/Swap.qll +++ b/cpp/ql/src/semmle/code/cpp/models/implementations/Swap.qll @@ -9,7 +9,7 @@ import semmle.code.cpp.models.interfaces.Alias * ``` */ private class Swap extends DataFlowFunction { - Swap() { this.hasQualifiedName("std", "swap") } + Swap() { this.hasQualifiedName(["std", "bsl"], "swap") } override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { input.isParameterDeref(0) and diff --git a/cpp/ql/src/semmle/code/cpp/models/interfaces/FlowSource.qll b/cpp/ql/src/semmle/code/cpp/models/interfaces/FlowSource.qll index c0c95b38756..8c80377c8ec 100644 --- a/cpp/ql/src/semmle/code/cpp/models/interfaces/FlowSource.qll +++ b/cpp/ql/src/semmle/code/cpp/models/interfaces/FlowSource.qll @@ -1,9 +1,9 @@ /** - * Provides a class for modeling functions that return data from potentially untrusted sources. To use - * this QL library, create a QL class extending `DataFlowFunction` with a + * Provides classes for modeling functions that return data from (or send data to) potentially untrusted + * sources. To use this QL library, create a QL class extending `DataFlowFunction` with a * characteristic predicate that selects the function or set of functions you * are modeling. Within that class, override the predicates provided by - * `RemoteFlowFunction` to match the flow within that function. + * `RemoteFlowSourceFunction` or `RemoteFlowSinkFunction` to match the flow within that function. */ import cpp @@ -13,19 +13,42 @@ import semmle.code.cpp.models.Models /** * A library function that returns data that may be read from a network connection. */ -abstract class RemoteFlowFunction extends Function { +abstract class RemoteFlowSourceFunction extends Function { /** * Holds if remote data described by `description` flows from `output` of a call to this function. */ abstract predicate hasRemoteFlowSource(FunctionOutput output, string description); } +/** + * DEPRECATED: Use `RemoteFlowSourceFunction` instead. + * + * A library function that returns data that may be read from a network connection. + */ +deprecated class RemoteFlowFunction = RemoteFlowSourceFunction; + /** * A library function that returns data that is directly controlled by a user. */ -abstract class LocalFlowFunction extends Function { +abstract class LocalFlowSourceFunction extends Function { /** * Holds if data described by `description` flows from `output` of a call to this function. */ abstract predicate hasLocalFlowSource(FunctionOutput output, string description); } + +/** + * DEPRECATED: Use `LocalFlowSourceFunction` instead. + * + * A library function that returns data that is directly controlled by a user. + */ +deprecated class LocalFlowFunction = LocalFlowSourceFunction; + +/** A library function that sends data over a network connection. */ +abstract class RemoteFlowSinkFunction extends Function { + /** + * Holds if data described by `description` flows into `input` to a call to this function, and is then + * send over a network connection. + */ + abstract predicate hasRemoteFlowSink(FunctionInput input, string description); +} diff --git a/cpp/ql/src/semmle/code/cpp/security/FlowSources.qll b/cpp/ql/src/semmle/code/cpp/security/FlowSources.qll index c8bad67352b..b080651951f 100644 --- a/cpp/ql/src/semmle/code/cpp/security/FlowSources.qll +++ b/cpp/ql/src/semmle/code/cpp/security/FlowSources.qll @@ -23,7 +23,7 @@ private class RemoteReturnSource extends RemoteFlowSource { string sourceType; RemoteReturnSource() { - exists(RemoteFlowFunction func, CallInstruction instr, FunctionOutput output | + exists(RemoteFlowSourceFunction func, CallInstruction instr, FunctionOutput output | asInstruction() = instr and instr.getStaticCallTarget() = func and func.hasRemoteFlowSource(output, sourceType) and @@ -42,7 +42,7 @@ private class RemoteParameterSource extends RemoteFlowSource { string sourceType; RemoteParameterSource() { - exists(RemoteFlowFunction func, WriteSideEffectInstruction instr, FunctionOutput output | + exists(RemoteFlowSourceFunction func, WriteSideEffectInstruction instr, FunctionOutput output | asInstruction() = instr and instr.getPrimaryInstruction().(CallInstruction).getStaticCallTarget() = func and func.hasRemoteFlowSource(output, sourceType) and @@ -57,7 +57,7 @@ private class LocalReturnSource extends LocalFlowSource { string sourceType; LocalReturnSource() { - exists(LocalFlowFunction func, CallInstruction instr, FunctionOutput output | + exists(LocalFlowSourceFunction func, CallInstruction instr, FunctionOutput output | asInstruction() = instr and instr.getStaticCallTarget() = func and func.hasLocalFlowSource(output, sourceType) and @@ -76,7 +76,7 @@ private class LocalParameterSource extends LocalFlowSource { string sourceType; LocalParameterSource() { - exists(LocalFlowFunction func, WriteSideEffectInstruction instr, FunctionOutput output | + exists(LocalFlowSourceFunction func, WriteSideEffectInstruction instr, FunctionOutput output | asInstruction() = instr and instr.getPrimaryInstruction().(CallInstruction).getStaticCallTarget() = func and func.hasLocalFlowSource(output, sourceType) and @@ -98,3 +98,31 @@ private class ArgvSource extends LocalFlowSource { override string getSourceType() { result = "a command-line argument" } } + +/** A remote data flow sink. */ +abstract class RemoteFlowSink extends DataFlow::Node { + /** Gets a string that describes the type of this flow sink. */ + abstract string getSinkType(); +} + +private class RemoteParameterSink extends RemoteFlowSink { + string sourceType; + + RemoteParameterSink() { + exists(RemoteFlowSinkFunction func, FunctionInput input, CallInstruction call, int index | + func.hasRemoteFlowSink(input, sourceType) and call.getStaticCallTarget() = func + | + exists(ReadSideEffectInstruction read | + call = read.getPrimaryInstruction() and + read.getIndex() = index and + this.asOperand() = read.getSideEffectOperand() and + input.isParameterDerefOrQualifierObject(index) + ) + or + input.isParameterOrQualifierAddress(index) and + this.asOperand() = call.getArgumentOperand(index) + ) + } + + override string getSinkType() { result = sourceType } +} diff --git a/cpp/ql/src/semmle/code/cpp/security/Security.qll b/cpp/ql/src/semmle/code/cpp/security/Security.qll index 16dc89d9a6e..d39c13a25a0 100644 --- a/cpp/ql/src/semmle/code/cpp/security/Security.qll +++ b/cpp/ql/src/semmle/code/cpp/security/Security.qll @@ -6,6 +6,7 @@ import semmle.code.cpp.exprs.Expr import semmle.code.cpp.commons.Environment import semmle.code.cpp.security.SecurityOptions +import semmle.code.cpp.models.interfaces.FlowSource /** * Extend this class to customize the security queries for @@ -59,14 +60,14 @@ class SecurityOptions extends string { or functionCall.getTarget().hasGlobalName(fname) and exists(functionCall.getArgument(arg)) and - ( - fname = ["read", "recv", "recvmsg"] and arg = 1 - or - fname = "getaddrinfo" and arg = 3 - or - fname = "recvfrom" and - (arg = 1 or arg = 4 or arg = 5) - ) + fname = "getaddrinfo" and + arg = 3 + ) + or + exists(RemoteFlowSourceFunction remote, FunctionOutput output | + functionCall.getTarget() = remote and + output.isParameterDerefOrQualifierObject(arg) and + remote.hasRemoteFlowSource(output, _) ) } @@ -81,6 +82,12 @@ class SecurityOptions extends string { userInputReturn(fname) ) ) + or + exists(RemoteFlowSourceFunction remote, FunctionOutput output | + functionCall.getTarget() = remote and + (output.isReturnValue() or output.isReturnValueDeref()) and + remote.hasRemoteFlowSource(output, _) + ) } /** diff --git a/cpp/ql/test/README.md b/cpp/ql/test/README.md index 417d7bbe19d..9de2b10e1b8 100644 --- a/cpp/ql/test/README.md +++ b/cpp/ql/test/README.md @@ -1,6 +1,6 @@ # C/C++ CodeQL tests -This document provides additional information about the C/C++ CodeQL Tests located in `cpp/ql/test`. See [Contributing to CodeQL](/CONTRIBUTING.md) for general information about contributing to this repository. +This document provides additional information about the C/C++ CodeQL tests located in `cpp/ql/test`. The principles under "Copying code", below, also apply to any other C/C++ code in this repository, such as examples linked from query `.qhelp` files in `cpp/ql/src`. For more general information about contributing to this repository, see [Contributing to CodeQL](/CONTRIBUTING.md). The tests can be run through Visual Studio Code. Advanced users may also use the `codeql test run` command. diff --git a/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/defaulttainttracking.cpp b/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/defaulttainttracking.cpp index b24efc1dc8e..020a2f90b9e 100644 --- a/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/defaulttainttracking.cpp +++ b/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/defaulttainttracking.cpp @@ -216,3 +216,46 @@ void test_pointers2() sink(ptr4); // clean sink(*ptr4); // $ MISSING: ast,ir } + +// --- recv --- + +int recv(int s, char* buf, int len, int flags); + +void test_recv() { + char buffer[1024]; + recv(0, buffer, sizeof(buffer), 0); + sink(buffer); // $ ast,ir + sink(*buffer); // $ ast,ir +} + +// --- send and related functions --- + +int send(int, const void*, int, int); + +void test_send(char* buffer, int length) { + send(0, buffer, length, 0); // $ remote +} + +struct iovec { + void *iov_base; + unsigned iov_len; +}; + +int readv(int, const struct iovec*, int); +int writev(int, const struct iovec*, int); + +void sink(const iovec* iovs); +void sink(iovec); + +int test_readv_and_writev(iovec* iovs) { + readv(0, iovs, 16); + sink(iovs); // $ast,ir + sink(iovs[0]); // $ast MISSING: ir + sink(*iovs); // $ast MISSING: ir + + char* p = (char*)iovs[1].iov_base; + sink(p); // $ MISSING: ast,ir + sink(*p); // $ MISSING: ast,ir + + writev(0, iovs, 16); // $ remote +} diff --git a/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/remote-flow-sink.expected b/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/remote-flow-sink.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/remote-flow-sink.ql b/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/remote-flow-sink.ql new file mode 100644 index 00000000000..4fbbe8fa3bf --- /dev/null +++ b/cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/remote-flow-sink.ql @@ -0,0 +1,20 @@ +/** This tests that we are able to detect remote flow sinks. */ + +import cpp +import TestUtilities.InlineExpectationsTest +import semmle.code.cpp.security.FlowSources + +class RemoteFlowSinkTest extends InlineExpectationsTest { + RemoteFlowSinkTest() { this = "RemoteFlowSinkTest" } + + override string getARelevantTag() { result = "remote" } + + override predicate hasActualResult(Location location, string element, string tag, string value) { + tag = "remote" and + value = "" and + exists(RemoteFlowSink node | + location = node.getLocation() and + element = node.toString() + ) + } +} diff --git a/cpp/ql/test/library-tests/dataflow/fields/dataflow-consistency.expected b/cpp/ql/test/library-tests/dataflow/fields/dataflow-consistency.expected index c6528723d22..8cc13f226a9 100644 --- a/cpp/ql/test/library-tests/dataflow/fields/dataflow-consistency.expected +++ b/cpp/ql/test/library-tests/dataflow/fields/dataflow-consistency.expected @@ -36,9 +36,6 @@ argHasPostUpdate | arrays.cpp:10:8:10:15 | * ... | ArgumentNode is missing PostUpdateNode. | | arrays.cpp:16:8:16:13 | access to array | ArgumentNode is missing PostUpdateNode. | | arrays.cpp:17:8:17:13 | access to array | ArgumentNode is missing PostUpdateNode. | -| by_reference.cpp:51:8:51:8 | s | ArgumentNode is missing PostUpdateNode. | -| by_reference.cpp:57:8:57:8 | s | ArgumentNode is missing PostUpdateNode. | -| by_reference.cpp:63:8:63:8 | s | ArgumentNode is missing PostUpdateNode. | postWithInFlow | A.cpp:25:13:25:13 | c [post update] | PostUpdateNode should not be the target of local flow. | | A.cpp:27:28:27:28 | c [post update] | PostUpdateNode should not be the target of local flow. | diff --git a/cpp/ql/test/library-tests/dataflow/fields/partial-definition-diff.expected b/cpp/ql/test/library-tests/dataflow/fields/partial-definition-diff.expected index 996836a65b4..bd670c38ba5 100644 --- a/cpp/ql/test/library-tests/dataflow/fields/partial-definition-diff.expected +++ b/cpp/ql/test/library-tests/dataflow/fields/partial-definition-diff.expected @@ -227,14 +227,18 @@ | by_reference.cpp:20:23:20:27 | value | AST only | | by_reference.cpp:24:19:24:22 | this | AST only | | by_reference.cpp:24:25:24:29 | value | AST only | +| by_reference.cpp:40:12:40:15 | this | AST only | | by_reference.cpp:50:3:50:3 | s | AST only | | by_reference.cpp:50:17:50:26 | call to user_input | AST only | +| by_reference.cpp:51:8:51:8 | s | AST only | | by_reference.cpp:51:10:51:20 | call to getDirectly | AST only | | by_reference.cpp:56:3:56:3 | s | AST only | | by_reference.cpp:56:19:56:28 | call to user_input | AST only | +| by_reference.cpp:57:8:57:8 | s | AST only | | by_reference.cpp:57:10:57:22 | call to getIndirectly | AST only | | by_reference.cpp:62:3:62:3 | s | AST only | | by_reference.cpp:62:25:62:34 | call to user_input | AST only | +| by_reference.cpp:63:8:63:8 | s | AST only | | by_reference.cpp:63:10:63:28 | call to getThroughNonMember | AST only | | by_reference.cpp:68:17:68:18 | & ... | AST only | | by_reference.cpp:68:21:68:30 | call to user_input | AST only | diff --git a/cpp/ql/test/library-tests/dataflow/fields/partial-definition.expected b/cpp/ql/test/library-tests/dataflow/fields/partial-definition.expected index 9b03e4f8039..0a3ea441f93 100644 --- a/cpp/ql/test/library-tests/dataflow/fields/partial-definition.expected +++ b/cpp/ql/test/library-tests/dataflow/fields/partial-definition.expected @@ -266,14 +266,18 @@ | by_reference.cpp:20:23:20:27 | value | | by_reference.cpp:24:19:24:22 | this | | by_reference.cpp:24:25:24:29 | value | +| by_reference.cpp:40:12:40:15 | this | | by_reference.cpp:50:3:50:3 | s | | by_reference.cpp:50:17:50:26 | call to user_input | +| by_reference.cpp:51:8:51:8 | s | | by_reference.cpp:51:10:51:20 | call to getDirectly | | by_reference.cpp:56:3:56:3 | s | | by_reference.cpp:56:19:56:28 | call to user_input | +| by_reference.cpp:57:8:57:8 | s | | by_reference.cpp:57:10:57:22 | call to getIndirectly | | by_reference.cpp:62:3:62:3 | s | | by_reference.cpp:62:25:62:34 | call to user_input | +| by_reference.cpp:63:8:63:8 | s | | by_reference.cpp:63:10:63:28 | call to getThroughNonMember | | by_reference.cpp:68:17:68:18 | & ... | | by_reference.cpp:68:21:68:30 | call to user_input | diff --git a/cpp/ql/test/library-tests/dataflow/taint-tests/bsd.cpp b/cpp/ql/test/library-tests/dataflow/taint-tests/bsd.cpp new file mode 100644 index 00000000000..4846420a5a5 --- /dev/null +++ b/cpp/ql/test/library-tests/dataflow/taint-tests/bsd.cpp @@ -0,0 +1,24 @@ +void sink(...); +int source(); + +// --- accept --- + +struct sockaddr { + unsigned char length; + int sa_family; + char* sa_data; +}; + +int accept(int, const sockaddr*, int*); + +void sink(sockaddr); + +void test_accept() { + int s = source(); + sockaddr addr; + int size = sizeof(sockaddr); + int a = accept(s, &addr, &size); + + sink(a); // $ ast=17:11 SPURIOUS: ast=18:12 MISSING: ir + sink(addr); // $ ast MISSING: ir +} diff --git a/cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected b/cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected index 9f038101d67..2b5ce44aca6 100644 --- a/cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected +++ b/cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected @@ -135,6 +135,17 @@ | arrayassignment.cpp:145:12:145:12 | 5 | arrayassignment.cpp:145:7:145:13 | access to array | TAINT | | arrayassignment.cpp:146:7:146:10 | arr3 | arrayassignment.cpp:146:7:146:13 | access to array | | | arrayassignment.cpp:146:12:146:12 | 5 | arrayassignment.cpp:146:7:146:13 | access to array | TAINT | +| bsd.cpp:17:11:17:16 | call to source | bsd.cpp:20:18:20:18 | s | | +| bsd.cpp:18:12:18:15 | addr | bsd.cpp:20:22:20:25 | addr | | +| bsd.cpp:18:12:18:15 | addr | bsd.cpp:23:8:23:11 | addr | | +| bsd.cpp:19:14:19:29 | sizeof(sockaddr) | bsd.cpp:20:29:20:32 | size | | +| bsd.cpp:20:11:20:16 | call to accept | bsd.cpp:22:8:22:8 | a | | +| bsd.cpp:20:18:20:18 | s | bsd.cpp:20:11:20:16 | call to accept | TAINT | +| bsd.cpp:20:21:20:25 | & ... | bsd.cpp:20:11:20:16 | call to accept | TAINT | +| bsd.cpp:20:22:20:25 | addr | bsd.cpp:20:11:20:16 | call to accept | TAINT | +| bsd.cpp:20:22:20:25 | addr | bsd.cpp:20:21:20:25 | & ... | | +| bsd.cpp:20:28:20:32 | ref arg & ... | bsd.cpp:20:29:20:32 | size [inner post update] | | +| bsd.cpp:20:29:20:32 | size | bsd.cpp:20:28:20:32 | & ... | | | constructor_delegation.cpp:8:2:8:8 | this | constructor_delegation.cpp:8:20:8:24 | constructor init of field x [pre-this] | | | constructor_delegation.cpp:8:14:8:15 | _x | constructor_delegation.cpp:8:22:8:23 | _x | | | constructor_delegation.cpp:8:22:8:23 | _x | constructor_delegation.cpp:8:20:8:24 | constructor init of field x | TAINT | @@ -796,8 +807,23 @@ | map.cpp:146:40:146:41 | ref arg i1 | map.cpp:150:8:150:9 | i1 | | | map.cpp:148:8:148:8 | call to operator* | map.cpp:148:8:148:10 | call to pair | TAINT | | map.cpp:148:9:148:10 | i1 | map.cpp:148:8:148:8 | call to operator* | TAINT | +| map.cpp:148:9:148:10 | ref arg i1 | map.cpp:146:24:146:25 | i1 | | +| map.cpp:148:9:148:10 | ref arg i1 | map.cpp:146:40:146:41 | i1 | | +| map.cpp:148:9:148:10 | ref arg i1 | map.cpp:148:9:148:10 | i1 | | +| map.cpp:148:9:148:10 | ref arg i1 | map.cpp:149:8:149:9 | i1 | | +| map.cpp:148:9:148:10 | ref arg i1 | map.cpp:150:8:150:9 | i1 | | | map.cpp:149:8:149:9 | i1 | map.cpp:149:10:149:10 | call to operator-> | TAINT | +| map.cpp:149:8:149:9 | ref arg i1 | map.cpp:146:24:146:25 | i1 | | +| map.cpp:149:8:149:9 | ref arg i1 | map.cpp:146:40:146:41 | i1 | | +| map.cpp:149:8:149:9 | ref arg i1 | map.cpp:148:9:148:10 | i1 | | +| map.cpp:149:8:149:9 | ref arg i1 | map.cpp:149:8:149:9 | i1 | | +| map.cpp:149:8:149:9 | ref arg i1 | map.cpp:150:8:150:9 | i1 | | | map.cpp:150:8:150:9 | i1 | map.cpp:150:10:150:10 | call to operator-> | TAINT | +| map.cpp:150:8:150:9 | ref arg i1 | map.cpp:146:24:146:25 | i1 | | +| map.cpp:150:8:150:9 | ref arg i1 | map.cpp:146:40:146:41 | i1 | | +| map.cpp:150:8:150:9 | ref arg i1 | map.cpp:148:9:148:10 | i1 | | +| map.cpp:150:8:150:9 | ref arg i1 | map.cpp:149:8:149:9 | i1 | | +| map.cpp:150:8:150:9 | ref arg i1 | map.cpp:150:8:150:9 | i1 | | | map.cpp:152:12:152:13 | m2 | map.cpp:152:15:152:19 | call to begin | TAINT | | map.cpp:152:12:152:13 | ref arg m2 | map.cpp:152:30:152:31 | m2 | | | map.cpp:152:12:152:13 | ref arg m2 | map.cpp:182:7:182:8 | m2 | | @@ -830,8 +856,23 @@ | map.cpp:152:40:152:41 | ref arg i2 | map.cpp:156:8:156:9 | i2 | | | map.cpp:154:8:154:8 | call to operator* | map.cpp:154:8:154:10 | call to pair | TAINT | | map.cpp:154:9:154:10 | i2 | map.cpp:154:8:154:8 | call to operator* | TAINT | +| map.cpp:154:9:154:10 | ref arg i2 | map.cpp:152:24:152:25 | i2 | | +| map.cpp:154:9:154:10 | ref arg i2 | map.cpp:152:40:152:41 | i2 | | +| map.cpp:154:9:154:10 | ref arg i2 | map.cpp:154:9:154:10 | i2 | | +| map.cpp:154:9:154:10 | ref arg i2 | map.cpp:155:8:155:9 | i2 | | +| map.cpp:154:9:154:10 | ref arg i2 | map.cpp:156:8:156:9 | i2 | | | map.cpp:155:8:155:9 | i2 | map.cpp:155:10:155:10 | call to operator-> | TAINT | +| map.cpp:155:8:155:9 | ref arg i2 | map.cpp:152:24:152:25 | i2 | | +| map.cpp:155:8:155:9 | ref arg i2 | map.cpp:152:40:152:41 | i2 | | +| map.cpp:155:8:155:9 | ref arg i2 | map.cpp:154:9:154:10 | i2 | | +| map.cpp:155:8:155:9 | ref arg i2 | map.cpp:155:8:155:9 | i2 | | +| map.cpp:155:8:155:9 | ref arg i2 | map.cpp:156:8:156:9 | i2 | | | map.cpp:156:8:156:9 | i2 | map.cpp:156:10:156:10 | call to operator-> | TAINT | +| map.cpp:156:8:156:9 | ref arg i2 | map.cpp:152:24:152:25 | i2 | | +| map.cpp:156:8:156:9 | ref arg i2 | map.cpp:152:40:152:41 | i2 | | +| map.cpp:156:8:156:9 | ref arg i2 | map.cpp:154:9:154:10 | i2 | | +| map.cpp:156:8:156:9 | ref arg i2 | map.cpp:155:8:155:9 | i2 | | +| map.cpp:156:8:156:9 | ref arg i2 | map.cpp:156:8:156:9 | i2 | | | map.cpp:158:12:158:13 | m3 | map.cpp:158:15:158:19 | call to begin | TAINT | | map.cpp:158:12:158:13 | ref arg m3 | map.cpp:158:30:158:31 | m3 | | | map.cpp:158:12:158:13 | ref arg m3 | map.cpp:252:1:252:1 | m3 | | @@ -852,8 +893,23 @@ | map.cpp:158:40:158:41 | ref arg i3 | map.cpp:162:8:162:9 | i3 | | | map.cpp:160:8:160:8 | call to operator* | map.cpp:160:8:160:10 | call to pair | TAINT | | map.cpp:160:9:160:10 | i3 | map.cpp:160:8:160:8 | call to operator* | TAINT | +| map.cpp:160:9:160:10 | ref arg i3 | map.cpp:158:24:158:25 | i3 | | +| map.cpp:160:9:160:10 | ref arg i3 | map.cpp:158:40:158:41 | i3 | | +| map.cpp:160:9:160:10 | ref arg i3 | map.cpp:160:9:160:10 | i3 | | +| map.cpp:160:9:160:10 | ref arg i3 | map.cpp:161:8:161:9 | i3 | | +| map.cpp:160:9:160:10 | ref arg i3 | map.cpp:162:8:162:9 | i3 | | | map.cpp:161:8:161:9 | i3 | map.cpp:161:10:161:10 | call to operator-> | TAINT | +| map.cpp:161:8:161:9 | ref arg i3 | map.cpp:158:24:158:25 | i3 | | +| map.cpp:161:8:161:9 | ref arg i3 | map.cpp:158:40:158:41 | i3 | | +| map.cpp:161:8:161:9 | ref arg i3 | map.cpp:160:9:160:10 | i3 | | +| map.cpp:161:8:161:9 | ref arg i3 | map.cpp:161:8:161:9 | i3 | | +| map.cpp:161:8:161:9 | ref arg i3 | map.cpp:162:8:162:9 | i3 | | | map.cpp:162:8:162:9 | i3 | map.cpp:162:10:162:10 | call to operator-> | TAINT | +| map.cpp:162:8:162:9 | ref arg i3 | map.cpp:158:24:158:25 | i3 | | +| map.cpp:162:8:162:9 | ref arg i3 | map.cpp:158:40:158:41 | i3 | | +| map.cpp:162:8:162:9 | ref arg i3 | map.cpp:160:9:160:10 | i3 | | +| map.cpp:162:8:162:9 | ref arg i3 | map.cpp:161:8:161:9 | i3 | | +| map.cpp:162:8:162:9 | ref arg i3 | map.cpp:162:8:162:9 | i3 | | | map.cpp:166:27:166:29 | call to map | map.cpp:167:7:167:9 | m10 | | | map.cpp:166:27:166:29 | call to map | map.cpp:171:7:171:9 | m10 | | | map.cpp:166:27:166:29 | call to map | map.cpp:252:1:252:1 | m10 | | @@ -1460,8 +1516,23 @@ | map.cpp:298:40:298:41 | ref arg i1 | map.cpp:302:8:302:9 | i1 | | | map.cpp:300:8:300:8 | call to operator* | map.cpp:300:8:300:10 | call to pair | TAINT | | map.cpp:300:9:300:10 | i1 | map.cpp:300:8:300:8 | call to operator* | TAINT | +| map.cpp:300:9:300:10 | ref arg i1 | map.cpp:298:24:298:25 | i1 | | +| map.cpp:300:9:300:10 | ref arg i1 | map.cpp:298:40:298:41 | i1 | | +| map.cpp:300:9:300:10 | ref arg i1 | map.cpp:300:9:300:10 | i1 | | +| map.cpp:300:9:300:10 | ref arg i1 | map.cpp:301:8:301:9 | i1 | | +| map.cpp:300:9:300:10 | ref arg i1 | map.cpp:302:8:302:9 | i1 | | | map.cpp:301:8:301:9 | i1 | map.cpp:301:10:301:10 | call to operator-> | TAINT | +| map.cpp:301:8:301:9 | ref arg i1 | map.cpp:298:24:298:25 | i1 | | +| map.cpp:301:8:301:9 | ref arg i1 | map.cpp:298:40:298:41 | i1 | | +| map.cpp:301:8:301:9 | ref arg i1 | map.cpp:300:9:300:10 | i1 | | +| map.cpp:301:8:301:9 | ref arg i1 | map.cpp:301:8:301:9 | i1 | | +| map.cpp:301:8:301:9 | ref arg i1 | map.cpp:302:8:302:9 | i1 | | | map.cpp:302:8:302:9 | i1 | map.cpp:302:10:302:10 | call to operator-> | TAINT | +| map.cpp:302:8:302:9 | ref arg i1 | map.cpp:298:24:298:25 | i1 | | +| map.cpp:302:8:302:9 | ref arg i1 | map.cpp:298:40:298:41 | i1 | | +| map.cpp:302:8:302:9 | ref arg i1 | map.cpp:300:9:300:10 | i1 | | +| map.cpp:302:8:302:9 | ref arg i1 | map.cpp:301:8:301:9 | i1 | | +| map.cpp:302:8:302:9 | ref arg i1 | map.cpp:302:8:302:9 | i1 | | | map.cpp:304:12:304:13 | m2 | map.cpp:304:15:304:19 | call to begin | TAINT | | map.cpp:304:12:304:13 | ref arg m2 | map.cpp:304:30:304:31 | m2 | | | map.cpp:304:12:304:13 | ref arg m2 | map.cpp:334:7:334:8 | m2 | | @@ -1488,8 +1559,23 @@ | map.cpp:304:40:304:41 | ref arg i2 | map.cpp:308:8:308:9 | i2 | | | map.cpp:306:8:306:8 | call to operator* | map.cpp:306:8:306:10 | call to pair | TAINT | | map.cpp:306:9:306:10 | i2 | map.cpp:306:8:306:8 | call to operator* | TAINT | +| map.cpp:306:9:306:10 | ref arg i2 | map.cpp:304:24:304:25 | i2 | | +| map.cpp:306:9:306:10 | ref arg i2 | map.cpp:304:40:304:41 | i2 | | +| map.cpp:306:9:306:10 | ref arg i2 | map.cpp:306:9:306:10 | i2 | | +| map.cpp:306:9:306:10 | ref arg i2 | map.cpp:307:8:307:9 | i2 | | +| map.cpp:306:9:306:10 | ref arg i2 | map.cpp:308:8:308:9 | i2 | | | map.cpp:307:8:307:9 | i2 | map.cpp:307:10:307:10 | call to operator-> | TAINT | +| map.cpp:307:8:307:9 | ref arg i2 | map.cpp:304:24:304:25 | i2 | | +| map.cpp:307:8:307:9 | ref arg i2 | map.cpp:304:40:304:41 | i2 | | +| map.cpp:307:8:307:9 | ref arg i2 | map.cpp:306:9:306:10 | i2 | | +| map.cpp:307:8:307:9 | ref arg i2 | map.cpp:307:8:307:9 | i2 | | +| map.cpp:307:8:307:9 | ref arg i2 | map.cpp:308:8:308:9 | i2 | | | map.cpp:308:8:308:9 | i2 | map.cpp:308:10:308:10 | call to operator-> | TAINT | +| map.cpp:308:8:308:9 | ref arg i2 | map.cpp:304:24:304:25 | i2 | | +| map.cpp:308:8:308:9 | ref arg i2 | map.cpp:304:40:304:41 | i2 | | +| map.cpp:308:8:308:9 | ref arg i2 | map.cpp:306:9:306:10 | i2 | | +| map.cpp:308:8:308:9 | ref arg i2 | map.cpp:307:8:307:9 | i2 | | +| map.cpp:308:8:308:9 | ref arg i2 | map.cpp:308:8:308:9 | i2 | | | map.cpp:310:12:310:13 | m3 | map.cpp:310:15:310:19 | call to begin | TAINT | | map.cpp:310:12:310:13 | ref arg m3 | map.cpp:310:30:310:31 | m3 | | | map.cpp:310:12:310:13 | ref arg m3 | map.cpp:438:1:438:1 | m3 | | @@ -1510,8 +1596,23 @@ | map.cpp:310:40:310:41 | ref arg i3 | map.cpp:314:8:314:9 | i3 | | | map.cpp:312:8:312:8 | call to operator* | map.cpp:312:8:312:10 | call to pair | TAINT | | map.cpp:312:9:312:10 | i3 | map.cpp:312:8:312:8 | call to operator* | TAINT | +| map.cpp:312:9:312:10 | ref arg i3 | map.cpp:310:24:310:25 | i3 | | +| map.cpp:312:9:312:10 | ref arg i3 | map.cpp:310:40:310:41 | i3 | | +| map.cpp:312:9:312:10 | ref arg i3 | map.cpp:312:9:312:10 | i3 | | +| map.cpp:312:9:312:10 | ref arg i3 | map.cpp:313:8:313:9 | i3 | | +| map.cpp:312:9:312:10 | ref arg i3 | map.cpp:314:8:314:9 | i3 | | | map.cpp:313:8:313:9 | i3 | map.cpp:313:10:313:10 | call to operator-> | TAINT | +| map.cpp:313:8:313:9 | ref arg i3 | map.cpp:310:24:310:25 | i3 | | +| map.cpp:313:8:313:9 | ref arg i3 | map.cpp:310:40:310:41 | i3 | | +| map.cpp:313:8:313:9 | ref arg i3 | map.cpp:312:9:312:10 | i3 | | +| map.cpp:313:8:313:9 | ref arg i3 | map.cpp:313:8:313:9 | i3 | | +| map.cpp:313:8:313:9 | ref arg i3 | map.cpp:314:8:314:9 | i3 | | | map.cpp:314:8:314:9 | i3 | map.cpp:314:10:314:10 | call to operator-> | TAINT | +| map.cpp:314:8:314:9 | ref arg i3 | map.cpp:310:24:310:25 | i3 | | +| map.cpp:314:8:314:9 | ref arg i3 | map.cpp:310:40:310:41 | i3 | | +| map.cpp:314:8:314:9 | ref arg i3 | map.cpp:312:9:312:10 | i3 | | +| map.cpp:314:8:314:9 | ref arg i3 | map.cpp:313:8:313:9 | i3 | | +| map.cpp:314:8:314:9 | ref arg i3 | map.cpp:314:8:314:9 | i3 | | | map.cpp:318:37:318:39 | call to unordered_map | map.cpp:319:7:319:9 | m10 | | | map.cpp:318:37:318:39 | call to unordered_map | map.cpp:323:7:323:9 | m10 | | | map.cpp:318:37:318:39 | call to unordered_map | map.cpp:438:1:438:1 | m10 | | @@ -2347,6 +2448,9 @@ | set.cpp:55:40:55:41 | ref arg i1 | set.cpp:55:40:55:41 | i1 | | | set.cpp:55:40:55:41 | ref arg i1 | set.cpp:57:9:57:10 | i1 | | | set.cpp:57:9:57:10 | i1 | set.cpp:57:8:57:8 | call to operator* | TAINT | +| set.cpp:57:9:57:10 | ref arg i1 | set.cpp:55:24:55:25 | i1 | | +| set.cpp:57:9:57:10 | ref arg i1 | set.cpp:55:40:55:41 | i1 | | +| set.cpp:57:9:57:10 | ref arg i1 | set.cpp:57:9:57:10 | i1 | | | set.cpp:59:12:59:13 | ref arg s2 | set.cpp:59:30:59:31 | s2 | | | set.cpp:59:12:59:13 | ref arg s2 | set.cpp:126:1:126:1 | s2 | | | set.cpp:59:12:59:13 | s2 | set.cpp:59:15:59:19 | call to begin | TAINT | @@ -2362,6 +2466,9 @@ | set.cpp:59:40:59:41 | ref arg i2 | set.cpp:59:40:59:41 | i2 | | | set.cpp:59:40:59:41 | ref arg i2 | set.cpp:61:9:61:10 | i2 | | | set.cpp:61:9:61:10 | i2 | set.cpp:61:8:61:8 | call to operator* | TAINT | +| set.cpp:61:9:61:10 | ref arg i2 | set.cpp:59:24:59:25 | i2 | | +| set.cpp:61:9:61:10 | ref arg i2 | set.cpp:59:40:59:41 | i2 | | +| set.cpp:61:9:61:10 | ref arg i2 | set.cpp:61:9:61:10 | i2 | | | set.cpp:65:19:65:21 | call to set | set.cpp:66:2:66:4 | s11 | | | set.cpp:65:19:65:21 | call to set | set.cpp:67:2:67:4 | s11 | | | set.cpp:65:19:65:21 | call to set | set.cpp:68:2:68:4 | s11 | | @@ -2845,6 +2952,9 @@ | set.cpp:169:40:169:41 | ref arg i1 | set.cpp:169:40:169:41 | i1 | | | set.cpp:169:40:169:41 | ref arg i1 | set.cpp:171:9:171:10 | i1 | | | set.cpp:171:9:171:10 | i1 | set.cpp:171:8:171:8 | call to operator* | TAINT | +| set.cpp:171:9:171:10 | ref arg i1 | set.cpp:169:24:169:25 | i1 | | +| set.cpp:171:9:171:10 | ref arg i1 | set.cpp:169:40:169:41 | i1 | | +| set.cpp:171:9:171:10 | ref arg i1 | set.cpp:171:9:171:10 | i1 | | | set.cpp:173:12:173:13 | ref arg s2 | set.cpp:173:30:173:31 | s2 | | | set.cpp:173:12:173:13 | ref arg s2 | set.cpp:238:1:238:1 | s2 | | | set.cpp:173:12:173:13 | s2 | set.cpp:173:15:173:19 | call to begin | TAINT | @@ -2860,6 +2970,9 @@ | set.cpp:173:40:173:41 | ref arg i2 | set.cpp:173:40:173:41 | i2 | | | set.cpp:173:40:173:41 | ref arg i2 | set.cpp:175:9:175:10 | i2 | | | set.cpp:175:9:175:10 | i2 | set.cpp:175:8:175:8 | call to operator* | TAINT | +| set.cpp:175:9:175:10 | ref arg i2 | set.cpp:173:24:173:25 | i2 | | +| set.cpp:175:9:175:10 | ref arg i2 | set.cpp:173:40:173:41 | i2 | | +| set.cpp:175:9:175:10 | ref arg i2 | set.cpp:175:9:175:10 | i2 | | | set.cpp:179:29:179:31 | call to unordered_set | set.cpp:180:2:180:4 | s11 | | | set.cpp:179:29:179:31 | call to unordered_set | set.cpp:181:2:181:4 | s11 | | | set.cpp:179:29:179:31 | call to unordered_set | set.cpp:182:2:182:4 | s11 | | @@ -3111,21 +3224,27 @@ | smart_pointer.cpp:11:30:11:50 | call to make_shared | smart_pointer.cpp:13:10:13:10 | p | | | smart_pointer.cpp:11:52:11:57 | call to source | smart_pointer.cpp:11:30:11:50 | call to make_shared | TAINT | | smart_pointer.cpp:12:11:12:11 | p | smart_pointer.cpp:12:10:12:10 | call to operator* | TAINT | +| smart_pointer.cpp:12:11:12:11 | ref arg p | smart_pointer.cpp:13:10:13:10 | p | | | smart_pointer.cpp:17:32:17:54 | call to make_shared | smart_pointer.cpp:18:11:18:11 | p | | | smart_pointer.cpp:17:32:17:54 | call to make_shared | smart_pointer.cpp:19:10:19:10 | p | | | smart_pointer.cpp:18:11:18:11 | p | smart_pointer.cpp:18:10:18:10 | call to operator* | TAINT | +| smart_pointer.cpp:18:11:18:11 | ref arg p | smart_pointer.cpp:19:10:19:10 | p | | | smart_pointer.cpp:23:30:23:50 | call to make_unique | smart_pointer.cpp:24:11:24:11 | p | | | smart_pointer.cpp:23:30:23:50 | call to make_unique | smart_pointer.cpp:25:10:25:10 | p | | | smart_pointer.cpp:23:52:23:57 | call to source | smart_pointer.cpp:23:30:23:50 | call to make_unique | TAINT | | smart_pointer.cpp:24:11:24:11 | p | smart_pointer.cpp:24:10:24:10 | call to operator* | TAINT | +| smart_pointer.cpp:24:11:24:11 | ref arg p | smart_pointer.cpp:25:10:25:10 | p | | | smart_pointer.cpp:29:32:29:54 | call to make_unique | smart_pointer.cpp:30:11:30:11 | p | | | smart_pointer.cpp:29:32:29:54 | call to make_unique | smart_pointer.cpp:31:10:31:10 | p | | | smart_pointer.cpp:30:11:30:11 | p | smart_pointer.cpp:30:10:30:10 | call to operator* | TAINT | +| smart_pointer.cpp:30:11:30:11 | ref arg p | smart_pointer.cpp:31:10:31:10 | p | | | smart_pointer.cpp:35:30:35:50 | call to make_shared | smart_pointer.cpp:37:6:37:6 | p | | | smart_pointer.cpp:35:30:35:50 | call to make_shared | smart_pointer.cpp:38:10:38:10 | p | | | smart_pointer.cpp:35:30:35:50 | call to make_shared | smart_pointer.cpp:39:11:39:11 | p | | | smart_pointer.cpp:37:5:37:17 | ... = ... | smart_pointer.cpp:37:5:37:5 | call to operator* [post update] | | | smart_pointer.cpp:37:6:37:6 | p | smart_pointer.cpp:37:5:37:5 | call to operator* | TAINT | +| smart_pointer.cpp:37:6:37:6 | ref arg p | smart_pointer.cpp:38:10:38:10 | p | | +| smart_pointer.cpp:37:6:37:6 | ref arg p | smart_pointer.cpp:39:11:39:11 | p | | | smart_pointer.cpp:37:10:37:15 | call to source | smart_pointer.cpp:37:5:37:17 | ... = ... | | | smart_pointer.cpp:38:10:38:10 | ref arg p | smart_pointer.cpp:39:11:39:11 | p | | | smart_pointer.cpp:39:11:39:11 | p | smart_pointer.cpp:39:10:39:10 | call to operator* | TAINT | @@ -3134,6 +3253,8 @@ | smart_pointer.cpp:43:29:43:51 | call to unique_ptr | smart_pointer.cpp:47:11:47:11 | p | | | smart_pointer.cpp:45:5:45:17 | ... = ... | smart_pointer.cpp:45:5:45:5 | call to operator* [post update] | | | smart_pointer.cpp:45:6:45:6 | p | smart_pointer.cpp:45:5:45:5 | call to operator* | TAINT | +| smart_pointer.cpp:45:6:45:6 | ref arg p | smart_pointer.cpp:46:10:46:10 | p | | +| smart_pointer.cpp:45:6:45:6 | ref arg p | smart_pointer.cpp:47:11:47:11 | p | | | smart_pointer.cpp:45:10:45:15 | call to source | smart_pointer.cpp:45:5:45:17 | ... = ... | | | smart_pointer.cpp:46:10:46:10 | ref arg p | smart_pointer.cpp:47:11:47:11 | p | | | smart_pointer.cpp:47:11:47:11 | p | smart_pointer.cpp:47:10:47:10 | call to operator* | TAINT | @@ -3147,6 +3268,7 @@ | smart_pointer.cpp:65:28:65:46 | call to make_unique | smart_pointer.cpp:67:10:67:10 | p | | | smart_pointer.cpp:65:48:65:53 | call to source | smart_pointer.cpp:65:28:65:46 | call to make_unique | TAINT | | smart_pointer.cpp:65:58:65:58 | 0 | smart_pointer.cpp:65:28:65:46 | call to make_unique | TAINT | +| smart_pointer.cpp:66:10:66:10 | ref arg p | smart_pointer.cpp:67:10:67:10 | p | | | standalone_iterators.cpp:39:45:39:51 | source1 | standalone_iterators.cpp:39:45:39:51 | source1 | | | standalone_iterators.cpp:39:45:39:51 | source1 | standalone_iterators.cpp:40:11:40:17 | source1 | | | standalone_iterators.cpp:39:45:39:51 | source1 | standalone_iterators.cpp:41:12:41:18 | source1 | | @@ -3197,6 +3319,56 @@ | standalone_iterators.cpp:90:8:90:8 | call to operator-- | standalone_iterators.cpp:90:5:90:5 | call to operator* | TAINT | | standalone_iterators.cpp:90:8:90:8 | ref arg call to operator-- | standalone_iterators.cpp:90:6:90:7 | ref arg i2 | | | standalone_iterators.cpp:90:13:90:13 | 0 | standalone_iterators.cpp:90:5:90:5 | ref arg call to operator* | TAINT | +| standalone_iterators.cpp:98:15:98:16 | call to container | standalone_iterators.cpp:101:6:101:7 | c1 | | +| standalone_iterators.cpp:98:15:98:16 | call to container | standalone_iterators.cpp:102:6:102:7 | c1 | | +| standalone_iterators.cpp:98:15:98:16 | call to container | standalone_iterators.cpp:106:6:106:7 | c1 | | +| standalone_iterators.cpp:98:15:98:16 | call to container | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:101:6:101:7 | c1 | standalone_iterators.cpp:101:9:101:13 | call to begin | TAINT | +| standalone_iterators.cpp:101:6:101:7 | ref arg c1 | standalone_iterators.cpp:102:6:102:7 | c1 | | +| standalone_iterators.cpp:101:6:101:7 | ref arg c1 | standalone_iterators.cpp:106:6:106:7 | c1 | | +| standalone_iterators.cpp:101:6:101:7 | ref arg c1 | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:101:9:101:13 | call to begin | standalone_iterators.cpp:101:2:101:15 | ... = ... | | +| standalone_iterators.cpp:101:9:101:13 | call to begin | standalone_iterators.cpp:103:3:103:3 | a | | +| standalone_iterators.cpp:101:9:101:13 | call to begin | standalone_iterators.cpp:104:7:104:7 | a | | +| standalone_iterators.cpp:102:6:102:7 | c1 | standalone_iterators.cpp:102:9:102:13 | call to begin | TAINT | +| standalone_iterators.cpp:102:6:102:7 | ref arg c1 | standalone_iterators.cpp:106:6:106:7 | c1 | | +| standalone_iterators.cpp:102:6:102:7 | ref arg c1 | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:102:9:102:13 | call to begin | standalone_iterators.cpp:102:2:102:15 | ... = ... | | +| standalone_iterators.cpp:102:9:102:13 | call to begin | standalone_iterators.cpp:107:7:107:7 | b | | +| standalone_iterators.cpp:103:2:103:2 | ref arg call to operator* | standalone_iterators.cpp:103:3:103:3 | ref arg a | TAINT | +| standalone_iterators.cpp:103:2:103:2 | ref arg call to operator* | standalone_iterators.cpp:106:6:106:7 | c1 | | +| standalone_iterators.cpp:103:2:103:2 | ref arg call to operator* | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:103:3:103:3 | a | standalone_iterators.cpp:103:2:103:2 | call to operator* | TAINT | +| standalone_iterators.cpp:103:3:103:3 | ref arg a | standalone_iterators.cpp:104:7:104:7 | a | | +| standalone_iterators.cpp:103:7:103:12 | call to source | standalone_iterators.cpp:103:2:103:2 | ref arg call to operator* | TAINT | +| standalone_iterators.cpp:104:7:104:7 | a [post update] | standalone_iterators.cpp:106:6:106:7 | c1 | | +| standalone_iterators.cpp:104:7:104:7 | a [post update] | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:106:6:106:7 | c1 | standalone_iterators.cpp:106:9:106:13 | call to begin | TAINT | +| standalone_iterators.cpp:106:6:106:7 | ref arg c1 | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:106:9:106:13 | call to begin | standalone_iterators.cpp:106:2:106:15 | ... = ... | | +| standalone_iterators.cpp:106:9:106:13 | call to begin | standalone_iterators.cpp:108:7:108:7 | c | | +| standalone_iterators.cpp:107:7:107:7 | b [post update] | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:108:7:108:7 | c [post update] | standalone_iterators.cpp:109:7:109:8 | c1 | | +| standalone_iterators.cpp:113:15:113:16 | call to container | standalone_iterators.cpp:116:7:116:8 | c1 | | +| standalone_iterators.cpp:113:15:113:16 | call to container | standalone_iterators.cpp:122:7:122:8 | c1 | | +| standalone_iterators.cpp:116:7:116:8 | c1 | standalone_iterators.cpp:116:10:116:14 | call to begin | TAINT | +| standalone_iterators.cpp:116:7:116:8 | ref arg c1 | standalone_iterators.cpp:122:7:122:8 | c1 | | +| standalone_iterators.cpp:116:10:116:14 | call to begin | standalone_iterators.cpp:116:2:116:16 | ... = ... | | +| standalone_iterators.cpp:116:10:116:14 | call to begin | standalone_iterators.cpp:117:7:117:8 | it | | +| standalone_iterators.cpp:116:10:116:14 | call to begin | standalone_iterators.cpp:118:2:118:3 | it | | +| standalone_iterators.cpp:116:10:116:14 | call to begin | standalone_iterators.cpp:119:7:119:8 | it | | +| standalone_iterators.cpp:116:10:116:14 | call to begin | standalone_iterators.cpp:120:2:120:3 | it | | +| standalone_iterators.cpp:116:10:116:14 | call to begin | standalone_iterators.cpp:121:7:121:8 | it | | +| standalone_iterators.cpp:117:7:117:8 | it [post update] | standalone_iterators.cpp:122:7:122:8 | c1 | | +| standalone_iterators.cpp:118:2:118:3 | it | standalone_iterators.cpp:118:5:118:5 | call to operator+= | | +| standalone_iterators.cpp:118:2:118:3 | ref arg it | standalone_iterators.cpp:119:7:119:8 | it | | +| standalone_iterators.cpp:118:2:118:3 | ref arg it | standalone_iterators.cpp:120:2:120:3 | it | | +| standalone_iterators.cpp:118:2:118:3 | ref arg it | standalone_iterators.cpp:121:7:121:8 | it | | +| standalone_iterators.cpp:118:2:118:3 | ref arg it | standalone_iterators.cpp:122:7:122:8 | c1 | | +| standalone_iterators.cpp:118:8:118:8 | 1 | standalone_iterators.cpp:118:2:118:3 | ref arg it | TAINT | +| standalone_iterators.cpp:120:2:120:3 | it | standalone_iterators.cpp:120:5:120:5 | call to operator+= | | +| standalone_iterators.cpp:120:2:120:3 | ref arg it | standalone_iterators.cpp:121:7:121:8 | it | | +| standalone_iterators.cpp:120:8:120:13 | call to source | standalone_iterators.cpp:120:2:120:3 | ref arg it | TAINT | | stl.h:75:8:75:8 | Unknown literal | stl.h:75:8:75:8 | constructor init of field container | TAINT | | stl.h:75:8:75:8 | Unknown literal | stl.h:75:8:75:8 | constructor init of field container | TAINT | | stl.h:75:8:75:8 | this | stl.h:75:8:75:8 | constructor init of field container [pre-this] | | @@ -3408,6 +3580,9 @@ | string.cpp:121:15:121:15 | ref arg (__begin) | string.cpp:121:15:121:15 | (__begin) | | | string.cpp:121:15:121:15 | ref arg (__begin) | string.cpp:121:15:121:15 | (__begin) | | | string.cpp:121:15:121:15 | ref arg (__begin) | string.cpp:121:15:121:15 | (__begin) | | +| string.cpp:121:15:121:15 | ref arg (__begin) | string.cpp:121:15:121:15 | (__begin) | | +| string.cpp:121:15:121:15 | ref arg (__begin) | string.cpp:121:15:121:15 | (__begin) | | +| string.cpp:121:15:121:15 | ref arg (__begin) | string.cpp:121:15:121:15 | (__begin) | | | string.cpp:121:15:121:15 | ref arg (__range) | string.cpp:121:15:121:15 | (__range) | | | string.cpp:121:15:121:15 | s | string.cpp:121:15:121:15 | (__range) | | | string.cpp:121:15:121:15 | s | string.cpp:121:15:121:15 | (__range) | | @@ -3426,6 +3601,9 @@ | string.cpp:125:61:125:62 | ref arg it | string.cpp:125:61:125:62 | it | | | string.cpp:125:61:125:62 | ref arg it | string.cpp:126:9:126:10 | it | | | string.cpp:126:9:126:10 | it | string.cpp:126:8:126:8 | call to operator* | TAINT | +| string.cpp:126:9:126:10 | ref arg it | string.cpp:125:44:125:45 | it | | +| string.cpp:126:9:126:10 | ref arg it | string.cpp:125:61:125:62 | it | | +| string.cpp:126:9:126:10 | ref arg it | string.cpp:126:9:126:10 | it | | | string.cpp:129:16:129:16 | (__begin) | string.cpp:129:16:129:16 | call to operator* | TAINT | | string.cpp:129:16:129:16 | (__begin) | string.cpp:129:16:129:16 | call to operator++ | | | string.cpp:129:16:129:16 | (__end) | string.cpp:129:16:129:16 | call to iterator | | @@ -3439,6 +3617,9 @@ | string.cpp:129:16:129:16 | ref arg (__begin) | string.cpp:129:16:129:16 | (__begin) | | | string.cpp:129:16:129:16 | ref arg (__begin) | string.cpp:129:16:129:16 | (__begin) | | | string.cpp:129:16:129:16 | ref arg (__begin) | string.cpp:129:16:129:16 | (__begin) | | +| string.cpp:129:16:129:16 | ref arg (__begin) | string.cpp:129:16:129:16 | (__begin) | | +| string.cpp:129:16:129:16 | ref arg (__begin) | string.cpp:129:16:129:16 | (__begin) | | +| string.cpp:129:16:129:16 | ref arg (__begin) | string.cpp:129:16:129:16 | (__begin) | | | string.cpp:129:16:129:16 | ref arg (__range) | string.cpp:129:16:129:16 | (__range) | | | string.cpp:129:16:129:16 | s | string.cpp:129:16:129:16 | (__range) | | | string.cpp:129:16:129:16 | s | string.cpp:129:16:129:16 | (__range) | | @@ -3807,11 +3988,13 @@ | string.cpp:376:31:376:35 | call to begin | string.cpp:378:9:378:13 | iter1 | | | string.cpp:376:31:376:35 | call to begin | string.cpp:379:8:379:12 | iter1 | | | string.cpp:378:9:378:13 | iter1 | string.cpp:378:8:378:8 | call to operator* | TAINT | +| string.cpp:378:9:378:13 | ref arg iter1 | string.cpp:379:8:379:12 | iter1 | | | string.cpp:379:8:379:12 | iter1 | string.cpp:379:13:379:13 | call to operator[] | TAINT | | string.cpp:380:28:380:29 | s2 | string.cpp:380:31:380:35 | call to begin | TAINT | | string.cpp:380:31:380:35 | call to begin | string.cpp:382:9:382:13 | iter2 | | | string.cpp:380:31:380:35 | call to begin | string.cpp:383:8:383:12 | iter2 | | | string.cpp:382:9:382:13 | iter2 | string.cpp:382:8:382:8 | call to operator* | TAINT | +| string.cpp:382:9:382:13 | ref arg iter2 | string.cpp:383:8:383:12 | iter2 | | | string.cpp:383:8:383:12 | iter2 | string.cpp:383:13:383:13 | call to operator[] | TAINT | | string.cpp:388:18:388:24 | hello | string.cpp:388:18:388:25 | call to basic_string | TAINT | | string.cpp:388:18:388:25 | call to basic_string | string.cpp:391:25:391:26 | s1 | | @@ -3855,10 +4038,12 @@ | string.cpp:398:8:398:9 | i2 | string.cpp:398:3:398:9 | ... = ... | | | string.cpp:398:8:398:9 | i2 | string.cpp:399:12:399:13 | i3 | | | string.cpp:399:10:399:10 | call to operator++ | string.cpp:399:8:399:8 | call to operator* | TAINT | +| string.cpp:399:10:399:10 | ref arg call to operator++ | string.cpp:399:12:399:13 | ref arg i3 | | | string.cpp:399:12:399:13 | i3 | string.cpp:399:10:399:10 | call to operator++ | | | string.cpp:400:8:400:9 | i2 | string.cpp:400:3:400:9 | ... = ... | | | string.cpp:400:8:400:9 | i2 | string.cpp:401:12:401:13 | i4 | | | string.cpp:401:10:401:10 | call to operator-- | string.cpp:401:8:401:8 | call to operator* | TAINT | +| string.cpp:401:10:401:10 | ref arg call to operator-- | string.cpp:401:12:401:13 | ref arg i4 | | | string.cpp:401:12:401:13 | i4 | string.cpp:401:10:401:10 | call to operator-- | | | string.cpp:402:8:402:9 | i2 | string.cpp:402:3:402:9 | ... = ... | | | string.cpp:402:8:402:9 | i2 | string.cpp:403:3:403:4 | i5 | | @@ -3876,12 +4061,14 @@ | string.cpp:408:8:408:9 | i2 | string.cpp:409:10:409:11 | i7 | | | string.cpp:409:10:409:11 | i7 | string.cpp:409:12:409:12 | call to operator+= | | | string.cpp:409:12:409:12 | call to operator+= | string.cpp:409:8:409:8 | call to operator* | TAINT | -| string.cpp:409:14:409:14 | 1 | string.cpp:409:12:409:12 | call to operator+= | | +| string.cpp:409:12:409:12 | ref arg call to operator+= | string.cpp:409:10:409:11 | ref arg i7 | TAINT | +| string.cpp:409:14:409:14 | 1 | string.cpp:409:10:409:11 | ref arg i7 | TAINT | | string.cpp:410:8:410:9 | i2 | string.cpp:410:3:410:9 | ... = ... | | | string.cpp:410:8:410:9 | i2 | string.cpp:411:10:411:11 | i8 | | | string.cpp:411:10:411:11 | i8 | string.cpp:411:12:411:12 | call to operator-= | | | string.cpp:411:12:411:12 | call to operator-= | string.cpp:411:8:411:8 | call to operator* | TAINT | -| string.cpp:411:14:411:14 | 1 | string.cpp:411:12:411:12 | call to operator-= | | +| string.cpp:411:12:411:12 | ref arg call to operator-= | string.cpp:411:10:411:11 | ref arg i8 | TAINT | +| string.cpp:411:14:411:14 | 1 | string.cpp:411:10:411:11 | ref arg i8 | TAINT | | string.cpp:413:8:413:9 | s2 | string.cpp:413:11:413:13 | call to end | TAINT | | string.cpp:413:11:413:13 | call to end | string.cpp:413:3:413:15 | ... = ... | | | string.cpp:413:11:413:13 | call to end | string.cpp:414:5:414:6 | i9 | | @@ -6042,6 +6229,22 @@ | taint.cpp:631:6:631:14 | call to _strnextc | taint.cpp:631:2:631:18 | ... = ... | | | taint.cpp:631:6:631:14 | call to _strnextc | taint.cpp:632:7:632:7 | c | | | taint.cpp:631:16:631:17 | | taint.cpp:631:6:631:14 | call to _strnextc | TAINT | +| taint.cpp:640:9:640:12 | this | taint.cpp:640:25:640:29 | this | | +| taint.cpp:643:33:643:38 | source | taint.cpp:645:20:645:25 | source | | +| taint.cpp:644:30:644:30 | c | taint.cpp:645:10:645:10 | c | | +| taint.cpp:644:30:644:30 | c | taint.cpp:646:8:646:8 | c | | +| taint.cpp:645:10:645:10 | ref arg c | taint.cpp:646:8:646:8 | c | | +| taint.cpp:645:12:645:15 | call to data | taint.cpp:645:3:645:8 | call to memcpy | | +| taint.cpp:645:20:645:25 | source | taint.cpp:645:3:645:8 | call to memcpy | TAINT | +| taint.cpp:645:20:645:25 | source | taint.cpp:645:12:645:15 | ref arg call to data | TAINT | +| taint.cpp:652:9:652:12 | this | taint.cpp:652:31:652:35 | this | | +| taint.cpp:655:35:655:40 | source | taint.cpp:657:20:657:25 | source | | +| taint.cpp:656:27:656:27 | c | taint.cpp:657:10:657:10 | c | | +| taint.cpp:656:27:656:27 | c | taint.cpp:658:8:658:8 | c | | +| taint.cpp:657:10:657:10 | ref arg c | taint.cpp:658:8:658:8 | c | | +| taint.cpp:657:12:657:15 | call to data | taint.cpp:657:3:657:8 | call to memcpy | | +| taint.cpp:657:20:657:25 | source | taint.cpp:657:3:657:8 | call to memcpy | TAINT | +| taint.cpp:657:20:657:25 | source | taint.cpp:657:12:657:15 | ref arg call to data | TAINT | | vector.cpp:16:43:16:49 | source1 | vector.cpp:17:26:17:32 | source1 | | | vector.cpp:16:43:16:49 | source1 | vector.cpp:31:38:31:44 | source1 | | | vector.cpp:17:21:17:33 | call to vector | vector.cpp:19:14:19:14 | v | | @@ -6063,6 +6266,9 @@ | vector.cpp:19:14:19:14 | ref arg (__begin) | vector.cpp:19:14:19:14 | (__begin) | | | vector.cpp:19:14:19:14 | ref arg (__begin) | vector.cpp:19:14:19:14 | (__begin) | | | vector.cpp:19:14:19:14 | ref arg (__begin) | vector.cpp:19:14:19:14 | (__begin) | | +| vector.cpp:19:14:19:14 | ref arg (__begin) | vector.cpp:19:14:19:14 | (__begin) | | +| vector.cpp:19:14:19:14 | ref arg (__begin) | vector.cpp:19:14:19:14 | (__begin) | | +| vector.cpp:19:14:19:14 | ref arg (__begin) | vector.cpp:19:14:19:14 | (__begin) | | | vector.cpp:19:14:19:14 | ref arg (__range) | vector.cpp:19:14:19:14 | (__range) | | | vector.cpp:19:14:19:14 | v | vector.cpp:19:14:19:14 | (__range) | | | vector.cpp:19:14:19:14 | v | vector.cpp:19:14:19:14 | (__range) | | @@ -6083,6 +6289,9 @@ | vector.cpp:23:66:23:67 | ref arg it | vector.cpp:23:66:23:67 | it | | | vector.cpp:23:66:23:67 | ref arg it | vector.cpp:24:9:24:10 | it | | | vector.cpp:24:9:24:10 | it | vector.cpp:24:8:24:8 | call to operator* | TAINT | +| vector.cpp:24:9:24:10 | ref arg it | vector.cpp:23:49:23:50 | it | | +| vector.cpp:24:9:24:10 | ref arg it | vector.cpp:23:66:23:67 | it | | +| vector.cpp:24:9:24:10 | ref arg it | vector.cpp:24:9:24:10 | it | | | vector.cpp:27:15:27:15 | (__begin) | vector.cpp:27:15:27:15 | call to operator* | TAINT | | vector.cpp:27:15:27:15 | (__begin) | vector.cpp:27:15:27:15 | call to operator++ | | | vector.cpp:27:15:27:15 | (__end) | vector.cpp:27:15:27:15 | call to iterator | | @@ -6096,6 +6305,9 @@ | vector.cpp:27:15:27:15 | ref arg (__begin) | vector.cpp:27:15:27:15 | (__begin) | | | vector.cpp:27:15:27:15 | ref arg (__begin) | vector.cpp:27:15:27:15 | (__begin) | | | vector.cpp:27:15:27:15 | ref arg (__begin) | vector.cpp:27:15:27:15 | (__begin) | | +| vector.cpp:27:15:27:15 | ref arg (__begin) | vector.cpp:27:15:27:15 | (__begin) | | +| vector.cpp:27:15:27:15 | ref arg (__begin) | vector.cpp:27:15:27:15 | (__begin) | | +| vector.cpp:27:15:27:15 | ref arg (__begin) | vector.cpp:27:15:27:15 | (__begin) | | | vector.cpp:27:15:27:15 | ref arg (__range) | vector.cpp:27:15:27:15 | (__range) | | | vector.cpp:27:15:27:15 | v | vector.cpp:27:15:27:15 | (__range) | | | vector.cpp:27:15:27:15 | v | vector.cpp:27:15:27:15 | (__range) | | @@ -7019,16 +7231,20 @@ | vector.cpp:329:62:329:65 | iter | vector.cpp:329:62:329:65 | iter | | | vector.cpp:329:62:329:65 | iter | vector.cpp:330:3:330:6 | iter | | | vector.cpp:330:2:330:2 | call to operator* [post update] | vector.cpp:329:62:329:65 | iter | | +| vector.cpp:330:2:330:2 | call to operator* [post update] | vector.cpp:330:3:330:6 | ref arg iter | TAINT | | vector.cpp:330:2:330:17 | ... = ... | vector.cpp:330:2:330:2 | call to operator* [post update] | | | vector.cpp:330:3:330:6 | iter | vector.cpp:330:2:330:2 | call to operator* | TAINT | +| vector.cpp:330:3:330:6 | ref arg iter | vector.cpp:329:62:329:65 | iter | | | vector.cpp:330:10:330:15 | call to source | vector.cpp:330:2:330:2 | call to operator* [post update] | TAINT | | vector.cpp:330:10:330:15 | call to source | vector.cpp:330:2:330:17 | ... = ... | | | vector.cpp:333:64:333:67 | iter | vector.cpp:333:64:333:67 | iter | | | vector.cpp:333:64:333:67 | iter | vector.cpp:334:3:334:6 | iter | | | vector.cpp:333:74:333:74 | i | vector.cpp:334:10:334:10 | i | | | vector.cpp:334:2:334:2 | call to operator* [post update] | vector.cpp:333:64:333:67 | iter | | +| vector.cpp:334:2:334:2 | call to operator* [post update] | vector.cpp:334:3:334:6 | ref arg iter | TAINT | | vector.cpp:334:2:334:10 | ... = ... | vector.cpp:334:2:334:2 | call to operator* [post update] | | | vector.cpp:334:3:334:6 | iter | vector.cpp:334:2:334:2 | call to operator* | TAINT | +| vector.cpp:334:3:334:6 | ref arg iter | vector.cpp:333:64:333:67 | iter | | | vector.cpp:334:10:334:10 | i | vector.cpp:334:2:334:2 | call to operator* [post update] | TAINT | | vector.cpp:334:10:334:10 | i | vector.cpp:334:2:334:10 | ... = ... | | | vector.cpp:337:38:337:38 | b | vector.cpp:372:5:372:5 | b | | @@ -7086,6 +7302,7 @@ | vector.cpp:340:34:340:35 | ref arg v1 | vector.cpp:415:1:415:1 | v1 | | | vector.cpp:340:34:340:35 | v1 | vector.cpp:340:37:340:41 | call to begin | TAINT | | vector.cpp:340:37:340:41 | call to begin | vector.cpp:341:3:341:4 | i1 | | +| vector.cpp:341:2:341:2 | call to operator* [post update] | vector.cpp:341:3:341:4 | ref arg i1 | TAINT | | vector.cpp:341:2:341:2 | call to operator* [post update] | vector.cpp:342:7:342:8 | v1 | | | vector.cpp:341:2:341:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v1 | | | vector.cpp:341:2:341:15 | ... = ... | vector.cpp:341:2:341:2 | call to operator* [post update] | | @@ -7109,10 +7326,14 @@ | vector.cpp:344:68:344:69 | ref arg it | vector.cpp:344:68:344:69 | it | | | vector.cpp:344:68:344:69 | ref arg it | vector.cpp:345:4:345:5 | it | | | vector.cpp:345:3:345:3 | call to operator* [post update] | vector.cpp:344:56:344:57 | v2 | | +| vector.cpp:345:3:345:3 | call to operator* [post update] | vector.cpp:345:4:345:5 | ref arg it | TAINT | | vector.cpp:345:3:345:3 | call to operator* [post update] | vector.cpp:347:7:347:8 | v2 | | | vector.cpp:345:3:345:3 | call to operator* [post update] | vector.cpp:415:1:415:1 | v2 | | | vector.cpp:345:3:345:16 | ... = ... | vector.cpp:345:3:345:3 | call to operator* [post update] | | | vector.cpp:345:4:345:5 | it | vector.cpp:345:3:345:3 | call to operator* | TAINT | +| vector.cpp:345:4:345:5 | ref arg it | vector.cpp:344:50:344:51 | it | | +| vector.cpp:345:4:345:5 | ref arg it | vector.cpp:344:68:344:69 | it | | +| vector.cpp:345:4:345:5 | ref arg it | vector.cpp:345:4:345:5 | it | | | vector.cpp:345:9:345:14 | call to source | vector.cpp:345:3:345:3 | call to operator* [post update] | TAINT | | vector.cpp:345:9:345:14 | call to source | vector.cpp:345:3:345:16 | ... = ... | | | vector.cpp:347:7:347:8 | ref arg v2 | vector.cpp:415:1:415:1 | v2 | | @@ -7128,6 +7349,9 @@ | vector.cpp:349:15:349:15 | ref arg (__begin) | vector.cpp:349:15:349:15 | (__begin) | | | vector.cpp:349:15:349:15 | ref arg (__begin) | vector.cpp:349:15:349:15 | (__begin) | | | vector.cpp:349:15:349:15 | ref arg (__begin) | vector.cpp:349:15:349:15 | (__begin) | | +| vector.cpp:349:15:349:15 | ref arg (__begin) | vector.cpp:349:15:349:15 | (__begin) | | +| vector.cpp:349:15:349:15 | ref arg (__begin) | vector.cpp:349:15:349:15 | (__begin) | | +| vector.cpp:349:15:349:15 | ref arg (__begin) | vector.cpp:349:15:349:15 | (__begin) | | | vector.cpp:349:15:349:15 | ref arg (__range) | vector.cpp:349:15:349:15 | (__range) | | | vector.cpp:349:15:349:16 | v3 | vector.cpp:349:15:349:15 | (__range) | | | vector.cpp:349:15:349:16 | v3 | vector.cpp:349:15:349:15 | (__range) | | @@ -7164,15 +7388,18 @@ | vector.cpp:359:34:359:35 | v5 | vector.cpp:359:37:359:41 | call to begin | TAINT | | vector.cpp:359:37:359:41 | call to begin | vector.cpp:360:3:360:4 | i5 | | | vector.cpp:359:37:359:41 | call to begin | vector.cpp:362:3:362:4 | i5 | | +| vector.cpp:360:2:360:2 | call to operator* [post update] | vector.cpp:360:3:360:4 | ref arg i5 | TAINT | | vector.cpp:360:2:360:2 | call to operator* [post update] | vector.cpp:361:7:361:8 | v5 | | | vector.cpp:360:2:360:2 | call to operator* [post update] | vector.cpp:363:7:363:8 | v5 | | | vector.cpp:360:2:360:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v5 | | | vector.cpp:360:2:360:15 | ... = ... | vector.cpp:360:2:360:2 | call to operator* [post update] | | | vector.cpp:360:3:360:4 | i5 | vector.cpp:360:2:360:2 | call to operator* | TAINT | +| vector.cpp:360:3:360:4 | ref arg i5 | vector.cpp:362:3:362:4 | i5 | | | vector.cpp:360:8:360:13 | call to source | vector.cpp:360:2:360:2 | call to operator* [post update] | TAINT | | vector.cpp:360:8:360:13 | call to source | vector.cpp:360:2:360:15 | ... = ... | | | vector.cpp:361:7:361:8 | ref arg v5 | vector.cpp:363:7:363:8 | v5 | | | vector.cpp:361:7:361:8 | ref arg v5 | vector.cpp:415:1:415:1 | v5 | | +| vector.cpp:362:2:362:2 | call to operator* [post update] | vector.cpp:362:3:362:4 | ref arg i5 | TAINT | | vector.cpp:362:2:362:2 | call to operator* [post update] | vector.cpp:363:7:363:8 | v5 | | | vector.cpp:362:2:362:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v5 | | | vector.cpp:362:2:362:8 | ... = ... | vector.cpp:362:2:362:2 | call to operator* [post update] | | @@ -7186,6 +7413,7 @@ | vector.cpp:365:34:365:35 | ref arg v6 | vector.cpp:415:1:415:1 | v6 | | | vector.cpp:365:34:365:35 | v6 | vector.cpp:365:37:365:41 | call to begin | TAINT | | vector.cpp:365:37:365:41 | call to begin | vector.cpp:366:3:366:4 | i6 | | +| vector.cpp:366:2:366:2 | call to operator* [post update] | vector.cpp:366:3:366:4 | ref arg i6 | TAINT | | vector.cpp:366:2:366:2 | call to operator* [post update] | vector.cpp:367:7:367:8 | v6 | | | vector.cpp:366:2:366:2 | call to operator* [post update] | vector.cpp:368:2:368:3 | v6 | | | vector.cpp:366:2:366:2 | call to operator* [post update] | vector.cpp:369:7:369:8 | v6 | | @@ -7209,6 +7437,7 @@ | vector.cpp:371:34:371:35 | v7 | vector.cpp:371:37:371:41 | call to begin | TAINT | | vector.cpp:371:37:371:41 | call to begin | vector.cpp:373:4:373:5 | i7 | | | vector.cpp:371:37:371:41 | call to begin | vector.cpp:376:4:376:5 | i7 | | +| vector.cpp:373:3:373:3 | call to operator* [post update] | vector.cpp:373:4:373:5 | ref arg i7 | TAINT | | vector.cpp:373:3:373:3 | call to operator* [post update] | vector.cpp:374:8:374:9 | v7 | | | vector.cpp:373:3:373:3 | call to operator* [post update] | vector.cpp:379:7:379:8 | v7 | | | vector.cpp:373:3:373:3 | call to operator* [post update] | vector.cpp:415:1:415:1 | v7 | | @@ -7218,6 +7447,7 @@ | vector.cpp:373:9:373:14 | call to source | vector.cpp:373:3:373:16 | ... = ... | | | vector.cpp:374:8:374:9 | ref arg v7 | vector.cpp:379:7:379:8 | v7 | | | vector.cpp:374:8:374:9 | ref arg v7 | vector.cpp:415:1:415:1 | v7 | | +| vector.cpp:376:3:376:3 | call to operator* [post update] | vector.cpp:376:4:376:5 | ref arg i7 | TAINT | | vector.cpp:376:3:376:3 | call to operator* [post update] | vector.cpp:377:8:377:9 | v7 | | | vector.cpp:376:3:376:3 | call to operator* [post update] | vector.cpp:379:7:379:8 | v7 | | | vector.cpp:376:3:376:3 | call to operator* [post update] | vector.cpp:415:1:415:1 | v7 | | @@ -7234,15 +7464,18 @@ | vector.cpp:381:34:381:35 | v8 | vector.cpp:381:37:381:41 | call to begin | TAINT | | vector.cpp:381:37:381:41 | call to begin | vector.cpp:382:3:382:4 | i8 | | | vector.cpp:381:37:381:41 | call to begin | vector.cpp:384:3:384:4 | i8 | | +| vector.cpp:382:2:382:2 | call to operator* [post update] | vector.cpp:382:3:382:4 | ref arg i8 | TAINT | | vector.cpp:382:2:382:2 | call to operator* [post update] | vector.cpp:383:7:383:8 | v8 | | | vector.cpp:382:2:382:2 | call to operator* [post update] | vector.cpp:385:7:385:8 | v8 | | | vector.cpp:382:2:382:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v8 | | | vector.cpp:382:2:382:15 | ... = ... | vector.cpp:382:2:382:2 | call to operator* [post update] | | | vector.cpp:382:3:382:4 | i8 | vector.cpp:382:2:382:2 | call to operator* | TAINT | +| vector.cpp:382:3:382:4 | ref arg i8 | vector.cpp:384:3:384:4 | i8 | | | vector.cpp:382:8:382:13 | call to source | vector.cpp:382:2:382:2 | call to operator* [post update] | TAINT | | vector.cpp:382:8:382:13 | call to source | vector.cpp:382:2:382:15 | ... = ... | | | vector.cpp:383:7:383:8 | ref arg v8 | vector.cpp:385:7:385:8 | v8 | | | vector.cpp:383:7:383:8 | ref arg v8 | vector.cpp:415:1:415:1 | v8 | | +| vector.cpp:384:2:384:2 | call to operator* [post update] | vector.cpp:384:3:384:4 | ref arg i8 | TAINT | | vector.cpp:384:2:384:2 | call to operator* [post update] | vector.cpp:385:7:385:8 | v8 | | | vector.cpp:384:2:384:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v8 | | | vector.cpp:384:2:384:8 | ... = ... | vector.cpp:384:2:384:2 | call to operator* [post update] | | @@ -7255,10 +7488,12 @@ | vector.cpp:387:34:387:35 | v9 | vector.cpp:387:37:387:41 | call to begin | TAINT | | vector.cpp:387:37:387:41 | call to begin | vector.cpp:389:3:389:4 | i9 | | | vector.cpp:387:37:387:41 | call to begin | vector.cpp:390:31:390:32 | i9 | | +| vector.cpp:389:2:389:2 | call to operator* [post update] | vector.cpp:389:3:389:4 | ref arg i9 | TAINT | | vector.cpp:389:2:389:2 | call to operator* [post update] | vector.cpp:392:7:392:8 | v9 | | | vector.cpp:389:2:389:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v9 | | | vector.cpp:389:2:389:15 | ... = ... | vector.cpp:389:2:389:2 | call to operator* [post update] | | | vector.cpp:389:3:389:4 | i9 | vector.cpp:389:2:389:2 | call to operator* | TAINT | +| vector.cpp:389:3:389:4 | ref arg i9 | vector.cpp:390:31:390:32 | i9 | | | vector.cpp:389:8:389:13 | call to source | vector.cpp:389:2:389:2 | call to operator* [post update] | TAINT | | vector.cpp:389:8:389:13 | call to source | vector.cpp:389:2:389:15 | ... = ... | | | vector.cpp:390:31:390:32 | call to iterator [post update] | vector.cpp:392:7:392:8 | v9 | | @@ -7300,6 +7535,7 @@ | vector.cpp:403:6:403:6 | call to operator++ | vector.cpp:403:2:403:2 | call to operator* | TAINT | | vector.cpp:403:11:403:11 | 0 | vector.cpp:403:2:403:2 | call to operator* [post update] | TAINT | | vector.cpp:403:11:403:11 | 0 | vector.cpp:403:2:403:11 | ... = ... | | +| vector.cpp:404:2:404:2 | call to operator* [post update] | vector.cpp:404:3:404:5 | ref arg i12 | TAINT | | vector.cpp:404:2:404:2 | call to operator* [post update] | vector.cpp:405:7:405:9 | v12 | | | vector.cpp:404:2:404:2 | call to operator* [post update] | vector.cpp:415:1:415:1 | v12 | | | vector.cpp:404:2:404:16 | ... = ... | vector.cpp:404:2:404:2 | call to operator* [post update] | | @@ -7481,3 +7717,70 @@ | vector.cpp:496:25:496:30 | call to source | vector.cpp:496:2:496:3 | ref arg v2 | TAINT | | vector.cpp:496:25:496:30 | call to source | vector.cpp:496:5:496:11 | call to emplace | TAINT | | vector.cpp:497:7:497:8 | ref arg v2 | vector.cpp:498:1:498:1 | v2 | | +| vector.cpp:503:18:503:21 | {...} | vector.cpp:506:8:506:9 | as | | +| vector.cpp:503:18:503:21 | {...} | vector.cpp:507:8:507:9 | as | | +| vector.cpp:503:18:503:21 | {...} | vector.cpp:509:9:509:10 | as | | +| vector.cpp:503:18:503:21 | {...} | vector.cpp:515:8:515:9 | as | | +| vector.cpp:503:20:503:20 | 0 | vector.cpp:503:18:503:21 | {...} | TAINT | +| vector.cpp:506:8:506:9 | as | vector.cpp:506:8:506:12 | access to array | | +| vector.cpp:506:11:506:11 | 1 | vector.cpp:506:8:506:12 | access to array | TAINT | +| vector.cpp:507:8:507:9 | as | vector.cpp:507:8:507:19 | access to array | | +| vector.cpp:507:11:507:16 | call to source | vector.cpp:507:8:507:19 | access to array | TAINT | +| vector.cpp:509:9:509:10 | as | vector.cpp:509:3:509:10 | ... = ... | | +| vector.cpp:509:9:509:10 | as | vector.cpp:510:9:510:11 | ptr | | +| vector.cpp:509:9:509:10 | as | vector.cpp:511:3:511:5 | ptr | | +| vector.cpp:510:9:510:11 | ptr | vector.cpp:510:8:510:11 | * ... | TAINT | +| vector.cpp:511:3:511:5 | ptr | vector.cpp:511:3:511:10 | ... += ... | TAINT | +| vector.cpp:511:3:511:10 | ... += ... | vector.cpp:512:9:512:11 | ptr | | +| vector.cpp:511:3:511:10 | ... += ... | vector.cpp:513:3:513:5 | ptr | | +| vector.cpp:511:10:511:10 | 1 | vector.cpp:511:3:511:10 | ... += ... | TAINT | +| vector.cpp:512:9:512:11 | ptr | vector.cpp:512:8:512:11 | * ... | TAINT | +| vector.cpp:513:3:513:5 | ptr | vector.cpp:513:3:513:17 | ... += ... | TAINT | +| vector.cpp:513:3:513:17 | ... += ... | vector.cpp:514:9:514:11 | ptr | | +| vector.cpp:513:10:513:15 | call to source | vector.cpp:513:3:513:17 | ... += ... | TAINT | +| vector.cpp:514:9:514:11 | ptr | vector.cpp:514:8:514:11 | * ... | TAINT | +| vector.cpp:515:8:515:9 | as | vector.cpp:515:8:515:12 | access to array | | +| vector.cpp:515:11:515:11 | 1 | vector.cpp:515:8:515:12 | access to array | TAINT | +| vector.cpp:520:25:520:31 | call to vector | vector.cpp:523:8:523:9 | vs | | +| vector.cpp:520:25:520:31 | call to vector | vector.cpp:524:8:524:9 | vs | | +| vector.cpp:520:25:520:31 | call to vector | vector.cpp:526:8:526:9 | vs | | +| vector.cpp:520:25:520:31 | call to vector | vector.cpp:532:8:532:9 | vs | | +| vector.cpp:520:25:520:31 | call to vector | vector.cpp:533:2:533:2 | vs | | +| vector.cpp:520:30:520:30 | 0 | vector.cpp:520:25:520:31 | call to vector | TAINT | +| vector.cpp:523:8:523:9 | ref arg vs | vector.cpp:524:8:524:9 | vs | | +| vector.cpp:523:8:523:9 | ref arg vs | vector.cpp:526:8:526:9 | vs | | +| vector.cpp:523:8:523:9 | ref arg vs | vector.cpp:532:8:532:9 | vs | | +| vector.cpp:523:8:523:9 | ref arg vs | vector.cpp:533:2:533:2 | vs | | +| vector.cpp:523:8:523:9 | vs | vector.cpp:523:10:523:10 | call to operator[] | TAINT | +| vector.cpp:524:8:524:9 | ref arg vs | vector.cpp:526:8:526:9 | vs | | +| vector.cpp:524:8:524:9 | ref arg vs | vector.cpp:532:8:532:9 | vs | | +| vector.cpp:524:8:524:9 | ref arg vs | vector.cpp:533:2:533:2 | vs | | +| vector.cpp:524:8:524:9 | vs | vector.cpp:524:10:524:10 | call to operator[] | TAINT | +| vector.cpp:526:8:526:9 | ref arg vs | vector.cpp:532:8:532:9 | vs | | +| vector.cpp:526:8:526:9 | ref arg vs | vector.cpp:533:2:533:2 | vs | | +| vector.cpp:526:8:526:9 | vs | vector.cpp:526:11:526:15 | call to begin | TAINT | +| vector.cpp:526:11:526:15 | call to begin | vector.cpp:526:3:526:17 | ... = ... | | +| vector.cpp:526:11:526:15 | call to begin | vector.cpp:527:9:527:10 | it | | +| vector.cpp:526:11:526:15 | call to begin | vector.cpp:528:3:528:4 | it | | +| vector.cpp:526:11:526:15 | call to begin | vector.cpp:529:9:529:10 | it | | +| vector.cpp:526:11:526:15 | call to begin | vector.cpp:530:3:530:4 | it | | +| vector.cpp:526:11:526:15 | call to begin | vector.cpp:531:9:531:10 | it | | +| vector.cpp:527:9:527:10 | it | vector.cpp:527:8:527:8 | call to operator* | TAINT | +| vector.cpp:527:9:527:10 | ref arg it | vector.cpp:528:3:528:4 | it | | +| vector.cpp:527:9:527:10 | ref arg it | vector.cpp:529:9:529:10 | it | | +| vector.cpp:527:9:527:10 | ref arg it | vector.cpp:530:3:530:4 | it | | +| vector.cpp:527:9:527:10 | ref arg it | vector.cpp:531:9:531:10 | it | | +| vector.cpp:528:3:528:4 | it | vector.cpp:528:6:528:6 | call to operator+= | | +| vector.cpp:528:3:528:4 | ref arg it | vector.cpp:529:9:529:10 | it | | +| vector.cpp:528:3:528:4 | ref arg it | vector.cpp:530:3:530:4 | it | | +| vector.cpp:528:3:528:4 | ref arg it | vector.cpp:531:9:531:10 | it | | +| vector.cpp:528:9:528:9 | 1 | vector.cpp:528:3:528:4 | ref arg it | TAINT | +| vector.cpp:529:9:529:10 | it | vector.cpp:529:8:529:8 | call to operator* | TAINT | +| vector.cpp:529:9:529:10 | ref arg it | vector.cpp:530:3:530:4 | it | | +| vector.cpp:529:9:529:10 | ref arg it | vector.cpp:531:9:531:10 | it | | +| vector.cpp:530:3:530:4 | it | vector.cpp:530:6:530:6 | call to operator+= | | +| vector.cpp:530:3:530:4 | ref arg it | vector.cpp:531:9:531:10 | it | | +| vector.cpp:530:9:530:14 | call to source | vector.cpp:530:3:530:4 | ref arg it | TAINT | +| vector.cpp:531:9:531:10 | it | vector.cpp:531:8:531:8 | call to operator* | TAINT | +| vector.cpp:532:8:532:9 | ref arg vs | vector.cpp:533:2:533:2 | vs | | +| vector.cpp:532:8:532:9 | vs | vector.cpp:532:10:532:10 | call to operator[] | TAINT | diff --git a/cpp/ql/test/library-tests/dataflow/taint-tests/standalone_iterators.cpp b/cpp/ql/test/library-tests/dataflow/taint-tests/standalone_iterators.cpp index 3fb8c738305..362784eeb1a 100644 --- a/cpp/ql/test/library-tests/dataflow/taint-tests/standalone_iterators.cpp +++ b/cpp/ql/test/library-tests/dataflow/taint-tests/standalone_iterators.cpp @@ -90,3 +90,34 @@ void test_insert_iterator() { *i2-- = 0; sink(c2); // clean } + +void sink(insert_iterator_by_trait); +insert_iterator_by_trait &operator+=(insert_iterator_by_trait &it, int amount); + +void test_assign_through_iterator() { + container c1; + insert_iterator_by_trait a, b, c; + + a = c1.begin(); + b = c1.begin(); + *a = source(); + sink(a); // $ ast MISSING: ir + + c = c1.begin(); + sink(b); // MISSING: ast,ir + sink(c); // $ ast MISSING: ir + sink(c1); // $ ast MISSING: ir +} + +void test_nonmember_iterator() { + container c1; + insert_iterator_by_trait it; + + it = c1.begin(); + sink(it); + it += 1; + sink(it); + it += source(); + sink(it); // $ ast,ir + sink(c1); +} diff --git a/cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp b/cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp index 44d84c0c0b0..6db77f37ac1 100644 --- a/cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp +++ b/cpp/ql/test/library-tests/dataflow/taint-tests/taint.cpp @@ -630,4 +630,30 @@ void test__strnextc(const char* source) { } while(c != '\0'); c = _strnextc(""); sink(c); +} + +// --- taint through const specified function --- + +class C_no_const_member_function { + char* data_; +public: + char* data() { return data_; } +}; + +void test_no_const_member(char* source) { + C_no_const_member_function c; + memcpy(c.data(), source, 16); + sink(c.data()); // $ ast MISSING: ir +} + +class C_const_member_function { + char* data_; +public: + char* data() const { return data_; } +}; + +void test_with_const_member(char* source) { + C_const_member_function c; + memcpy(c.data(), source, 16); + sink(c.data()); // $ ast MISSING: ir } \ No newline at end of file diff --git a/cpp/ql/test/library-tests/dataflow/taint-tests/vector.cpp b/cpp/ql/test/library-tests/dataflow/taint-tests/vector.cpp index ec354da548c..4f0c8fab414 100644 --- a/cpp/ql/test/library-tests/dataflow/taint-tests/vector.cpp +++ b/cpp/ql/test/library-tests/dataflow/taint-tests/vector.cpp @@ -496,3 +496,39 @@ void test_vector_emplace() { v2.emplace(v2.begin(), source()); sink(v2); // $ ast,ir } + +void test_vector_iterator() { + { + // array behaviour, for comparison + short as[100] = {0}; + short *ptr; + + sink(as[1]); + sink(as[source()]); // $ ast,ir + + ptr = as; + sink(*ptr); + ptr += 1; + sink(*ptr); + ptr += source(); + sink(*ptr); // $ ast,ir + sink(as[1]); + } + + { + // iterator behaviour + std::vector vs(100, 0); + std::vector::iterator it; + + sink(vs[1]); + sink(vs[source()]); // $ MISSING: ast,ir + + it = vs.begin(); + sink(*it); + it += 1; + sink(*it); + it += source(); + sink(*it); // $ ast,ir + sink(vs[1]); + } +} diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/CgiXss.expected b/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/CgiXss.expected index 6f08104489e..de3326e74a5 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/CgiXss.expected +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/CgiXss.expected @@ -8,14 +8,14 @@ edges | search.c:22:24:22:28 | *query | search.c:23:39:23:43 | query | | search.c:22:24:22:28 | query | search.c:23:39:23:43 | query | | search.c:22:24:22:28 | query | search.c:23:39:23:43 | query | -| search.c:41:21:41:26 | call to getenv | search.c:14:24:14:28 | *query | -| search.c:41:21:41:26 | call to getenv | search.c:14:24:14:28 | *query | -| search.c:41:21:41:26 | call to getenv | search.c:14:24:14:28 | query | -| search.c:41:21:41:26 | call to getenv | search.c:14:24:14:28 | query | -| search.c:41:21:41:26 | call to getenv | search.c:22:24:22:28 | *query | -| search.c:41:21:41:26 | call to getenv | search.c:22:24:22:28 | *query | -| search.c:41:21:41:26 | call to getenv | search.c:22:24:22:28 | query | -| search.c:41:21:41:26 | call to getenv | search.c:22:24:22:28 | query | +| search.c:51:21:51:26 | call to getenv | search.c:14:24:14:28 | *query | +| search.c:51:21:51:26 | call to getenv | search.c:14:24:14:28 | *query | +| search.c:51:21:51:26 | call to getenv | search.c:14:24:14:28 | query | +| search.c:51:21:51:26 | call to getenv | search.c:14:24:14:28 | query | +| search.c:51:21:51:26 | call to getenv | search.c:22:24:22:28 | *query | +| search.c:51:21:51:26 | call to getenv | search.c:22:24:22:28 | *query | +| search.c:51:21:51:26 | call to getenv | search.c:22:24:22:28 | query | +| search.c:51:21:51:26 | call to getenv | search.c:22:24:22:28 | query | nodes | search.c:14:24:14:28 | *query | semmle.label | *query | | search.c:14:24:14:28 | query | semmle.label | query | @@ -29,12 +29,12 @@ nodes | search.c:23:39:23:43 | query | semmle.label | query | | search.c:23:39:23:43 | query | semmle.label | query | | search.c:23:39:23:43 | query | semmle.label | query | -| search.c:41:21:41:26 | call to getenv | semmle.label | call to getenv | -| search.c:41:21:41:26 | call to getenv | semmle.label | call to getenv | -| search.c:45:5:45:15 | Argument 0 | semmle.label | Argument 0 | -| search.c:45:17:45:25 | Argument 0 indirection | semmle.label | Argument 0 indirection | -| search.c:47:5:47:15 | Argument 0 | semmle.label | Argument 0 | -| search.c:47:17:47:25 | Argument 0 indirection | semmle.label | Argument 0 indirection | +| search.c:51:21:51:26 | call to getenv | semmle.label | call to getenv | +| search.c:51:21:51:26 | call to getenv | semmle.label | call to getenv | +| search.c:55:5:55:15 | Argument 0 | semmle.label | Argument 0 | +| search.c:55:17:55:25 | Argument 0 indirection | semmle.label | Argument 0 indirection | +| search.c:57:5:57:15 | Argument 0 | semmle.label | Argument 0 | +| search.c:57:17:57:25 | Argument 0 indirection | semmle.label | Argument 0 indirection | #select -| search.c:17:8:17:12 | query | search.c:41:21:41:26 | call to getenv | search.c:17:8:17:12 | query | Cross-site scripting vulnerability due to $@. | search.c:41:21:41:26 | call to getenv | this query data | -| search.c:23:39:23:43 | query | search.c:41:21:41:26 | call to getenv | search.c:23:39:23:43 | query | Cross-site scripting vulnerability due to $@. | search.c:41:21:41:26 | call to getenv | this query data | +| search.c:17:8:17:12 | query | search.c:51:21:51:26 | call to getenv | search.c:17:8:17:12 | query | Cross-site scripting vulnerability due to $@. | search.c:51:21:51:26 | call to getenv | this query data | +| search.c:23:39:23:43 | query | search.c:51:21:51:26 | call to getenv | search.c:23:39:23:43 | query | Cross-site scripting vulnerability due to $@. | search.c:51:21:51:26 | call to getenv | this query data | diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/search.c b/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/search.c index df9a09ded59..77c830985d2 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/search.c +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-079/semmle/CgiXss/search.c @@ -26,7 +26,7 @@ void bad_server2(char* query) { puts(do_search(query)); } -void good_server(char* query) { +void good_server1(char* query) { puts("

Query results for "); // GOOD: Escape HTML characters before adding to a page char* query_escaped = escape_html(query); @@ -37,14 +37,25 @@ void good_server(char* query) { puts(do_search(query)); } -int main(int argc, char** argv) { - char* raw_query = getenv("QUERY_STRING"); - if (strcmp("good", argv[0]) == 0) { - good_server(raw_query); - } else if (strcmp("bad1", argv[0]) == 0) { - bad_server1(raw_query); - } else { - bad_server2(raw_query); - } +int sscanf(const char *s, const char *format, ...); + +void good_server2(char* query) { + puts("

Query results for "); + // GOOD: Only an integer is added to the page. + int i = 0; + sscanf(query, "value=%i", &i); + printf("\n

%i

\n", i); } +int main(int argc, char** argv) { + char* raw_query = getenv("QUERY_STRING"); + if (strcmp("good1", argv[0]) == 0) { + good_server1(raw_query); + } else if (strcmp("bad1", argv[0]) == 0) { + bad_server1(raw_query); + } else if (strcmp("bad2", argv[0]) == 0) { + bad_server2(raw_query); + } else if (strcmp("good2", argv[0]) == 0) { + good_server2(raw_query); + } +} diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs index 2f16872dd74..c5d65794fb5 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/Solution.cs @@ -45,6 +45,7 @@ namespace Semmle.Autobuild.Shared private readonly SolutionFile? solution; private readonly IEnumerable includedProjects; + public override IEnumerable IncludedProjects => includedProjects; public IEnumerable Configurations => @@ -84,8 +85,12 @@ namespace Semmle.Autobuild.Shared .ToArray(); } - private IEnumerable ToolsVersions => includedProjects.Where(p => p.ValidToolsVersion).Select(p => p.ToolsVersion); + private IEnumerable ToolsVersions => includedProjects + .Where(p => p.ValidToolsVersion) + .Select(p => p.ToolsVersion); - public Version ToolsVersion => ToolsVersions.Any() ? ToolsVersions.Max() : new Version(); + public Version ToolsVersion => ToolsVersions.Any() + ? ToolsVersions.Max() + : new Version(); } } diff --git a/csharp/change-notes/2021-02-02-foreach-underlying-methods.md b/csharp/change-notes/2021-02-02-foreach-underlying-methods.md new file mode 100644 index 00000000000..a9e934a0e5d --- /dev/null +++ b/csharp/change-notes/2021-02-02-foreach-underlying-methods.md @@ -0,0 +1,3 @@ +lgtm,codescanning +* The underlying methods of `foreach` statements are now explicitly extracted and +they are made available on the `ForeachStmt` class. diff --git a/csharp/change-notes/2021-02-12-with-expression.md b/csharp/change-notes/2021-02-12-with-expression.md new file mode 100644 index 00000000000..b8f01cb2586 --- /dev/null +++ b/csharp/change-notes/2021-02-12-with-expression.md @@ -0,0 +1,4 @@ +lgtm,codescanning +* C# 9 `with` expressions are now extracted. Data flow support has been added to +handle flow through `with` expressions and also from `record` constructor arguments +to its properties. diff --git a/csharp/extractor/Semmle.Extraction.CIL.Driver/ExtractorOptions.cs b/csharp/extractor/Semmle.Extraction.CIL.Driver/ExtractorOptions.cs index 9964ff5bf0c..75405d562fe 100644 --- a/csharp/extractor/Semmle.Extraction.CIL.Driver/ExtractorOptions.cs +++ b/csharp/extractor/Semmle.Extraction.CIL.Driver/ExtractorOptions.cs @@ -163,7 +163,7 @@ namespace Semmle.Extraction.CIL.Driver } private readonly HashSet filesAnalyzed = new HashSet(); - public HashSet MissingReferences {get;} = new HashSet(); + public HashSet MissingReferences { get; } = new HashSet(); } /// diff --git a/csharp/extractor/Semmle.Extraction.CIL/Context.Factories.cs b/csharp/extractor/Semmle.Extraction.CIL/Context.Factories.cs index dd09727f3c0..36e420e3628 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Context.Factories.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Context.Factories.cs @@ -95,11 +95,11 @@ namespace Semmle.Extraction.CIL /// The handle of the entity. /// The generic context. /// - public IExtractedEntity CreateGeneric(GenericContext genericContext, Handle h) => genericHandleFactory[genericContext, h]; + public IExtractedEntity CreateGeneric(IGenericContext genericContext, Handle h) => genericHandleFactory[genericContext, h]; - private readonly GenericContext defaultGenericContext; + private readonly IGenericContext defaultGenericContext; - private IExtractedEntity CreateGenericHandle(GenericContext gc, Handle handle) + private IExtractedEntity CreateGenericHandle(IGenericContext gc, Handle handle) { IExtractedEntity entity; switch (handle.Kind) @@ -136,7 +136,7 @@ namespace Semmle.Extraction.CIL return entity; } - private IExtractedEntity Create(GenericContext gc, MemberReferenceHandle handle) + private IExtractedEntity Create(IGenericContext gc, MemberReferenceHandle handle) { var mr = MdReader.GetMemberReference(handle); switch (mr.GetKind()) @@ -228,7 +228,7 @@ namespace Semmle.Extraction.CIL #endregion - private readonly CachedFunction genericHandleFactory; + private readonly CachedFunction genericHandleFactory; /// /// Gets the short name of a member, without the preceding interface qualifier. diff --git a/csharp/extractor/Semmle.Extraction.CIL/Context.cs b/csharp/extractor/Semmle.Extraction.CIL/Context.cs index ae662f2e87c..99113714b7c 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Context.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Context.cs @@ -37,7 +37,7 @@ namespace Semmle.Extraction.CIL globalNamespace = new Lazy(() => Populate(new Entities.Namespace(this, "", null))); systemNamespace = new Lazy(() => Populate(new Entities.Namespace(this, "System"))); - genericHandleFactory = new CachedFunction(CreateGenericHandle); + genericHandleFactory = new CachedFunction(CreateGenericHandle); namespaceFactory = new CachedFunction(n => CreateNamespace(MdReader.GetString(n))); namespaceDefinitionFactory = new CachedFunction(CreateNamespace); sourceFiles = new CachedFunction(path => new Entities.PdbSourceFile(this, path)); diff --git a/csharp/extractor/Semmle.Extraction.CIL/EmptyContext.cs b/csharp/extractor/Semmle.Extraction.CIL/EmptyContext.cs index 70c73be1151..1105bc39cba 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/EmptyContext.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/EmptyContext.cs @@ -5,14 +5,18 @@ namespace Semmle.Extraction.CIL /// /// A generic context which does not contain any type parameters. /// - public class EmptyContext : GenericContext + public class EmptyContext : IGenericContext { - public EmptyContext(Context cx) : base(cx) + public EmptyContext(Context cx) { + Cx = cx; } - public override IEnumerable TypeParameters { get { yield break; } } + public Context Cx { get; } + + public IEnumerable TypeParameters { get { yield break; } } + + public IEnumerable MethodParameters { get { yield break; } } - public override IEnumerable MethodParameters { get { yield break; } } } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs index 8a843f1672d..ba332b2db97 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Assembly.cs @@ -40,6 +40,7 @@ namespace Semmle.Extraction.CIL.Entities trapFile.Write(FullName); trapFile.Write("#file:///"); trapFile.Write(Cx.AssemblyPath.Replace("\\", "/")); + trapFile.Write(";assembly"); } public override bool Equals(object? obj) @@ -49,8 +50,6 @@ namespace Semmle.Extraction.CIL.Entities public override int GetHashCode() => 7 * file.GetHashCode(); - public override string IdSuffix => ";assembly"; - private string FullName => assemblyName.GetPublicKey() is null ? assemblyName.FullName + ", PublicKeyToken=null" : assemblyName.FullName; public override IEnumerable Contents @@ -136,7 +135,7 @@ namespace Semmle.Extraction.CIL.Entities trapFile = trapWriter.TrapFile; if (nocache || !System.IO.File.Exists(trapFile)) { - var cx = extractor.CreateContext(null, trapWriter, null, false); + var cx = new Extraction.Context(extractor, trapWriter); ExtractCIL(cx, assemblyPath, extractPdbs); extracted = true; } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Attribute.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Attribute.cs index c862bc3fc4e..d79215f0adf 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Attribute.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Attribute.cs @@ -12,9 +12,9 @@ namespace Semmle.Extraction.CIL.Entities { private readonly CustomAttributeHandle handle; private readonly CustomAttribute attrib; - private readonly IEntity @object; + private readonly IExtractedEntity @object; - public Attribute(Context cx, IEntity @object, CustomAttributeHandle handle) : base(cx) + public Attribute(Context cx, IExtractedEntity @object, CustomAttributeHandle handle) : base(cx) { attrib = cx.MdReader.GetCustomAttribute(handle); this.handle = handle; @@ -80,7 +80,7 @@ namespace Semmle.Extraction.CIL.Entities return value?.ToString() ?? "null"; } - public static IEnumerable Populate(Context cx, IEntity @object, CustomAttributeHandleCollection attributes) + public static IEnumerable Populate(Context cx, IExtractedEntity @object, CustomAttributeHandleCollection attributes) { foreach (var attrib in attributes) { diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractedEntity.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractedEntity.cs new file mode 100644 index 00000000000..080227a63b9 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractedEntity.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace Semmle.Extraction.CIL +{ + /// + /// A CIL entity which has been extracted. + /// + public interface IExtractedEntity : IExtractionProduct, IEntity + { + /// + /// The contents of the entity. + /// + + IEnumerable Contents { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractionProduct.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractionProduct.cs new file mode 100644 index 00000000000..6383d7e1b4c --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IExtractionProduct.cs @@ -0,0 +1,24 @@ +namespace Semmle.Extraction.CIL +{ + /// + /// Something that is extracted from an entity. + /// + /// + /// + /// The extraction algorithm proceeds as follows: + /// - Construct entity + /// - Call Extract() + /// - IExtractedEntity check if already extracted + /// - Enumerate Contents to produce more extraction products + /// - Extract these until there is nothing left to extract + /// + public interface IExtractionProduct + { + /// + /// Perform further extraction/population of this item as necessary. + /// + /// + /// The extraction context. + void Extract(Context cx); + } +} diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IGenericContext.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IGenericContext.cs new file mode 100644 index 00000000000..21c69f7aeb4 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/IGenericContext.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace Semmle.Extraction.CIL +{ + /// + /// When we decode a type/method signature, we need access to + /// generic parameters. + /// + public interface IGenericContext + { + Context Cx { get; } + + /// + /// The list of generic type parameters/arguments, including type parameters/arguments of + /// containing types. + /// + IEnumerable TypeParameters { get; } + + /// + /// The list of generic method parameters/arguments. + /// + IEnumerable MethodParameters { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/LabelledEntity.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/LabelledEntity.cs new file mode 100644 index 00000000000..a66ecbb1fab --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/LabelledEntity.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Semmle.Extraction.CIL +{ + /// + /// An entity that needs to be populated during extraction. + /// This assigns a key and optionally extracts its contents. + /// + public abstract class LabelledEntity : Extraction.LabelledEntity, IExtractedEntity + { + // todo: with .NET 5 this can override the base context, and change the return type. + public Context Cx { get; } + + protected LabelledEntity(Context cx) : base(cx.Cx) + { + this.Cx = cx; + } + + public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException(); + + public void Extract(Context cx2) + { + cx2.Populate(this); + } + + public override string ToString() + { + using var writer = new StringWriter(); + WriteQuotedId(writer); + return writer.ToString(); + } + + public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel; + + public abstract IEnumerable Contents { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/Tuple.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/Tuple.cs new file mode 100644 index 00000000000..5657f072c9c --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/Tuple.cs @@ -0,0 +1,22 @@ +namespace Semmle.Extraction.CIL +{ + /// + /// A tuple that is an extraction product. + /// + internal class Tuple : IExtractionProduct + { + private readonly Extraction.Tuple tuple; + + public Tuple(string name, params object[] args) + { + tuple = new Extraction.Tuple(name, args); + } + + public void Extract(Context cx) + { + cx.Cx.Emit(tuple); + } + + public override string ToString() => tuple.ToString(); + } +} diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/UnlabelledEntity.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/UnlabelledEntity.cs new file mode 100644 index 00000000000..d8c18d291eb --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Base/UnlabelledEntity.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +namespace Semmle.Extraction.CIL +{ + /// + /// An entity that has contents to extract. There is no need to populate + /// a key as it's done in the contructor. + /// + public abstract class UnlabelledEntity : Extraction.UnlabelledEntity, IExtractedEntity + { + // todo: with .NET 5 this can override the base context, and change the return type. + public Context Cx { get; } + + protected UnlabelledEntity(Context cx) : base(cx.Cx) + { + Cx = cx; + } + + public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException(); + + public void Extract(Context cx2) + { + cx2.Extract(this); + } + + public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel; + + public abstract IEnumerable Contents { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionMethod.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionMethod.cs index 175da806597..8ff7fcf24bf 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionMethod.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/DefinitionMethod.cs @@ -23,7 +23,7 @@ namespace Semmle.Extraction.CIL.Entities public override IList? LocalVariables => locals; - public DefinitionMethod(GenericContext gc, MethodDefinitionHandle handle) : base(gc) + public DefinitionMethod(IGenericContext gc, MethodDefinitionHandle handle) : base(gc) { md = Cx.MdReader.GetMethodDefinition(handle); this.gc = gc; diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Event.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Event.cs index e2e27484a4c..a6ca9364e0f 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Event.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Event.cs @@ -25,10 +25,9 @@ namespace Semmle.Extraction.CIL.Entities parent.WriteId(trapFile); trapFile.Write('.'); trapFile.Write(Cx.ShortName(ed.Name)); + trapFile.Write(";cil-event"); } - public override string IdSuffix => ";cil-event"; - public override bool Equals(object? obj) { return obj is Event e && handle.Equals(e.handle); diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/ExceptionRegion.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/ExceptionRegion.cs index 75303e7903b..3026ff03030 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/ExceptionRegion.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/ExceptionRegion.cs @@ -7,13 +7,13 @@ namespace Semmle.Extraction.CIL.Entities /// internal class ExceptionRegion : UnlabelledEntity { - private readonly GenericContext gc; + private readonly IGenericContext gc; private readonly MethodImplementation method; private readonly int index; private readonly System.Reflection.Metadata.ExceptionRegion r; private readonly Dictionary jump_table; - public ExceptionRegion(GenericContext gc, MethodImplementation method, int index, System.Reflection.Metadata.ExceptionRegion r, Dictionary jump_table) : base(gc.Cx) + public ExceptionRegion(IGenericContext gc, MethodImplementation method, int index, System.Reflection.Metadata.ExceptionRegion r, Dictionary jump_table) : base(gc.Cx) { this.gc = gc; this.method = method; diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Field.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Field.cs index 3f051796887..8decef24128 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Field.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Field.cs @@ -8,40 +8,27 @@ namespace Semmle.Extraction.CIL.Entities /// /// An entity representing a field. /// - internal abstract class Field : GenericContext, IMember, ICustomModifierReceiver + internal abstract class Field : LabelledEntity, IGenericContext, IMember, ICustomModifierReceiver { protected Field(Context cx) : base(cx) { } - public Label Label { get; set; } - - public void WriteId(TextWriter trapFile) + public override void WriteId(TextWriter trapFile) { trapFile.WriteSubId(DeclaringType); trapFile.Write('.'); trapFile.Write(Name); + trapFile.Write(";cil-field"); } - public void WriteQuotedId(TextWriter trapFile) - { - trapFile.Write("@\""); - WriteId(trapFile); - trapFile.Write(idSuffix); - trapFile.Write('\"'); - } - - private const string idSuffix = ";cil-field"; - public abstract string Name { get; } public abstract Type DeclaringType { get; } - public Location ReportingLocation => throw new NotImplementedException(); - public abstract Type Type { get; } - public virtual IEnumerable Contents + public override IEnumerable Contents { get { @@ -55,11 +42,8 @@ namespace Semmle.Extraction.CIL.Entities } } - public void Extract(Context cx2) - { - cx2.Populate(this); - } + public abstract IEnumerable TypeParameters { get; } - TrapStackBehaviour IEntity.TrapStackBehaviour => TrapStackBehaviour.NoLabel; + public abstract IEnumerable MethodParameters { get; } } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/File.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/File.cs index d5cb10a43db..4b612ae86af 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/File.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/File.cs @@ -17,6 +17,7 @@ namespace Semmle.Extraction.CIL.Entities public override void WriteId(TextWriter trapFile) { trapFile.Write(TransformedPath.DatabaseId); + trapFile.Write(";sourcefile"); } public override bool Equals(object? obj) @@ -39,7 +40,5 @@ namespace Semmle.Extraction.CIL.Entities yield return Tuples.files(this, TransformedPath.Value, TransformedPath.NameWithoutExtension, TransformedPath.Extension); } } - - public override string IdSuffix => ";sourcefile"; } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Folder.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Folder.cs index 6121792600a..2294a525e4b 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Folder.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Folder.cs @@ -15,10 +15,9 @@ namespace Semmle.Extraction.CIL.Entities public override void WriteId(TextWriter trapFile) { trapFile.Write(transformedPath.DatabaseId); + trapFile.Write(";folder"); } - public override string IdSuffix => ";folder"; - public override IEnumerable Contents { get diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/ITypeSignature.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/ITypeSignature.cs index 81dcf7b7804..beaecdfab6f 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/ITypeSignature.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/ITypeSignature.cs @@ -4,6 +4,6 @@ namespace Semmle.Extraction.CIL.Entities { internal interface ITypeSignature { - void WriteId(TextWriter trapFile, GenericContext gc); + void WriteId(TextWriter trapFile, IGenericContext gc); } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Instruction.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Instruction.cs index 75d0aab454a..033010e18a3 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Instruction.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Instruction.cs @@ -8,7 +8,7 @@ namespace Semmle.Extraction.CIL.Entities /// /// A CIL instruction. /// - internal class Instruction : UnlabelledEntity, IEntity + internal class Instruction : UnlabelledEntity { /// /// The additional data following the opcode, if any. @@ -289,11 +289,6 @@ namespace Semmle.Extraction.CIL.Entities } } - Label IEntity.Label - { - get; set; - } - private readonly byte[] data; private int PayloadSize => payloadSizes[(int)PayloadType]; diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/LocalVariable.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/LocalVariable.cs index d96c430f3c9..5d7a00c64c9 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/LocalVariable.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/LocalVariable.cs @@ -21,10 +21,9 @@ namespace Semmle.Extraction.CIL.Entities trapFile.WriteSubId(method); trapFile.Write('_'); trapFile.Write(index); + trapFile.Write(";cil-local"); } - public override string IdSuffix => ";cil-local"; - public override IEnumerable Contents { get diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceField.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceField.cs index f8017a729e8..136ece10a8c 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceField.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceField.cs @@ -8,10 +8,10 @@ namespace Semmle.Extraction.CIL.Entities { private readonly MemberReferenceHandle handle; private readonly MemberReference mr; - private readonly GenericContext gc; + private readonly IGenericContext gc; private readonly Type declType; - public MemberReferenceField(GenericContext gc, MemberReferenceHandle handle) : base(gc.Cx) + public MemberReferenceField(IGenericContext gc, MemberReferenceHandle handle) : base(gc.Cx) { this.handle = handle; this.gc = gc; diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceMethod.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceMethod.cs index cacbc6ab874..703bb717cee 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceMethod.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/MemberReferenceMethod.cs @@ -12,10 +12,10 @@ namespace Semmle.Extraction.CIL.Entities private readonly MemberReferenceHandle handle; private readonly MemberReference mr; private readonly Type declaringType; - private readonly GenericContext parent; + private readonly IGenericContext parent; private readonly Method? sourceDeclaration; - public MemberReferenceMethod(GenericContext gc, MemberReferenceHandle handle) : base(gc) + public MemberReferenceMethod(IGenericContext gc, MemberReferenceHandle handle) : base(gc) { this.handle = handle; this.gc = gc; @@ -23,7 +23,7 @@ namespace Semmle.Extraction.CIL.Entities signature = mr.DecodeMethodSignature(new SignatureDecoder(), gc); - parent = (GenericContext)Cx.CreateGeneric(gc, mr.Parent); + parent = (IGenericContext)Cx.CreateGeneric(gc, mr.Parent); var declType = parent is Method parentMethod ? parentMethod.DeclaringType diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs index 30e94c8e16a..d1302837c16 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Method.cs @@ -12,10 +12,10 @@ namespace Semmle.Extraction.CIL.Entities internal abstract class Method : TypeContainer, IMember, ICustomModifierReceiver, IParameterizable { protected MethodTypeParameter[]? genericParams; - protected GenericContext gc; + protected IGenericContext gc; protected MethodSignature signature; - protected Method(GenericContext gc) : base(gc.Cx) + protected Method(IGenericContext gc) : base(gc.Cx) { this.gc = gc; } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodSpecificationMethod.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodSpecificationMethod.cs index 26811ce6c80..a3f87b14a8e 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodSpecificationMethod.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodSpecificationMethod.cs @@ -18,7 +18,7 @@ namespace Semmle.Extraction.CIL.Entities private readonly Method unboundMethod; private readonly ImmutableArray typeParams; - public MethodSpecificationMethod(GenericContext gc, MethodSpecificationHandle handle) : base(gc) + public MethodSpecificationMethod(IGenericContext gc, MethodSpecificationHandle handle) : base(gc) { this.handle = handle; ms = Cx.MdReader.GetMethodSpecification(handle); diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodTypeParameter.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodTypeParameter.cs index db5e56acb17..1795eb29269 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodTypeParameter.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/MethodTypeParameter.cs @@ -21,7 +21,7 @@ namespace Semmle.Extraction.CIL.Entities public override string Name => "!" + index; - public MethodTypeParameter(GenericContext gc, Method m, int index) : base(gc) + public MethodTypeParameter(IGenericContext gc, Method m, int index) : base(gc) { method = m; this.index = index; diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Namespace.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Namespace.cs index e916fd0020f..5d498c5ac54 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Namespace.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Namespace.cs @@ -14,9 +14,6 @@ namespace Semmle.Extraction.CIL.Entities public bool IsGlobalNamespace => ParentNamespace is null; - public override string IdSuffix => ";namespace"; - - public override void WriteId(TextWriter trapFile) { if (ParentNamespace != null && !ParentNamespace.IsGlobalNamespace) @@ -27,6 +24,8 @@ namespace Semmle.Extraction.CIL.Entities trapFile.Write(Name); } + public override string IdSuffix => ";namespacee"; + public override bool Equals(object? obj) { if (obj is Namespace ns && Name == ns.Name) diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Parameter.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Parameter.cs index 90452fe9265..9cf96412309 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Parameter.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Parameter.cs @@ -8,41 +8,40 @@ namespace Semmle.Extraction.CIL.Entities /// internal sealed class Parameter : LabelledEntity { - private readonly IParameterizable method; + private readonly IParameterizable parameterizable; private readonly int index; private readonly Type type; - public Parameter(Context cx, IParameterizable m, int i, Type t) : base(cx) + public Parameter(Context cx, IParameterizable p, int i, Type t) : base(cx) { - method = m; + parameterizable = p; index = i; type = t; } public override void WriteId(TextWriter trapFile) { - trapFile.WriteSubId(method); + trapFile.WriteSubId(parameterizable); trapFile.Write('_'); trapFile.Write(index); + trapFile.Write(";cil-parameter"); } public override bool Equals(object? obj) { - return obj is Parameter param && method.Equals(param.method) && index == param.index; + return obj is Parameter param && parameterizable.Equals(param.parameterizable) && index == param.index; } public override int GetHashCode() { - return 23 * method.GetHashCode() + index; + return 23 * parameterizable.GetHashCode() + index; } - public override string IdSuffix => ";cil-parameter"; - public override IEnumerable Contents { get { - yield return Tuples.cil_parameter(this, method, index, type); + yield return Tuples.cil_parameter(this, parameterizable, index, type); } } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Property.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Property.cs index 5fb22b40409..e605468827a 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Property.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Property.cs @@ -13,10 +13,9 @@ namespace Semmle.Extraction.CIL.Entities private readonly Handle handle; private readonly Type type; private readonly PropertyDefinition pd; - public override string IdSuffix => ";cil-property"; - private readonly GenericContext gc; + private readonly IGenericContext gc; - public Property(GenericContext gc, Type type, PropertyDefinitionHandle handle) : base(gc.Cx) + public Property(IGenericContext gc, Type type, PropertyDefinitionHandle handle) : base(gc.Cx) { this.gc = gc; this.handle = handle; @@ -38,6 +37,7 @@ namespace Semmle.Extraction.CIL.Entities param.WriteId(trapFile, gc); } trapFile.Write(")"); + trapFile.Write(";cil-property"); } public override bool Equals(object? obj) diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/SignatureDecoder.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/SignatureDecoder.cs index a955ef685c0..308846ff30a 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/SignatureDecoder.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/SignatureDecoder.cs @@ -17,7 +17,7 @@ namespace Semmle.Extraction.CIL.Entities this.shape = shape; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { elementType.WriteId(trapFile, gc); trapFile.Write('['); @@ -38,7 +38,7 @@ namespace Semmle.Extraction.CIL.Entities this.elementType = elementType; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { elementType.WriteId(trapFile, gc); trapFile.Write('&'); @@ -54,7 +54,7 @@ namespace Semmle.Extraction.CIL.Entities this.signature = signature; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { FunctionPointerType.WriteName( trapFile.Write, @@ -84,7 +84,7 @@ namespace Semmle.Extraction.CIL.Entities this.typeArguments = typeArguments; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { genericType.WriteId(trapFile, gc); trapFile.Write('<'); @@ -112,7 +112,7 @@ namespace Semmle.Extraction.CIL.Entities this.index = index; } - public void WriteId(TextWriter trapFile, GenericContext outerGc) + public void WriteId(TextWriter trapFile, IGenericContext outerGc) { if (!ReferenceEquals(innerGc, outerGc) && innerGc is Method method) { @@ -132,7 +132,7 @@ namespace Semmle.Extraction.CIL.Entities this.index = index; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { trapFile.Write("T!"); trapFile.Write(index); @@ -158,7 +158,7 @@ namespace Semmle.Extraction.CIL.Entities this.isRequired = isRequired; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { unmodifiedType.WriteId(trapFile, gc); trapFile.Write(isRequired ? " modreq(" : " modopt("); @@ -186,7 +186,7 @@ namespace Semmle.Extraction.CIL.Entities this.elementType = elementType; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { elementType.WriteId(trapFile, gc); trapFile.Write('*'); @@ -207,7 +207,7 @@ namespace Semmle.Extraction.CIL.Entities this.typeCode = typeCode; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { trapFile.Write(typeCode.Id()); } @@ -227,7 +227,7 @@ namespace Semmle.Extraction.CIL.Entities this.elementType = elementType; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { elementType.WriteId(trapFile, gc); trapFile.Write("[]"); @@ -248,7 +248,7 @@ namespace Semmle.Extraction.CIL.Entities this.handle = handle; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { var type = (Type)gc.Cx.Create(handle); type.WriteId(trapFile); @@ -269,7 +269,7 @@ namespace Semmle.Extraction.CIL.Entities this.handle = handle; } - public void WriteId(TextWriter trapFile, GenericContext gc) + public void WriteId(TextWriter trapFile, IGenericContext gc) { var type = (Type)gc.Cx.Create(handle); type.WriteId(trapFile); diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/SourceLocation.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/SourceLocation.cs index f0f1667bca2..318cac14930 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/SourceLocation.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/SourceLocation.cs @@ -26,6 +26,7 @@ namespace Semmle.Extraction.CIL.Entities trapFile.Write(location.EndLine); trapFile.Write(','); trapFile.Write(location.EndColumn); + trapFile.Write(";sourcelocation"); } public override bool Equals(object? obj) @@ -43,7 +44,5 @@ namespace Semmle.Extraction.CIL.Entities yield return Tuples.locations_default(this, file, location.StartLine, location.StartColumn, location.EndLine, location.EndColumn); } } - - public override string IdSuffix => ";sourcelocation"; } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs index edeb1a54136..ad5a6ababaa 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs @@ -11,7 +11,6 @@ namespace Semmle.Extraction.CIL.Entities /// public abstract class Type : TypeContainer, IMember { - public override string IdSuffix => ";cil-type"; internal const string AssemblyTypeNameSeparator = "::"; internal const string PrimitiveTypePrefix = "builtin" + AssemblyTypeNameSeparator + "System."; @@ -48,6 +47,8 @@ namespace Semmle.Extraction.CIL.Entities public sealed override void WriteId(TextWriter trapFile) => WriteId(trapFile, false); + public override string IdSuffix => ";cil-type"; + /// /// Returns the friendly qualified name of types, such as /// ``"System.Collection.Generic.List`1"`` or @@ -198,7 +199,7 @@ namespace Semmle.Extraction.CIL.Entities public sealed override IEnumerable MethodParameters => Enumerable.Empty(); - public static Type DecodeType(GenericContext gc, TypeSpecificationHandle handle) => + public static Type DecodeType(IGenericContext gc, TypeSpecificationHandle handle) => gc.Cx.MdReader.GetTypeSpecification(handle).DecodeSignature(gc.Cx.TypeSignatureDecoder, gc); } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeContainer.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeContainer.cs index a7b7a006af6..106b6fe83be 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeContainer.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeContainer.cs @@ -1,5 +1,3 @@ -using System; -using Microsoft.CodeAnalysis; using System.Collections.Generic; using System.IO; @@ -8,17 +6,15 @@ namespace Semmle.Extraction.CIL.Entities /// /// Base class for all type containers (namespaces, types, methods). /// - public abstract class TypeContainer : GenericContext, IExtractedEntity + public abstract class TypeContainer : LabelledEntity, IGenericContext { protected TypeContainer(Context cx) : base(cx) { } - public virtual Label Label { get; set; } + public abstract string IdSuffix { get; } - public abstract void WriteId(TextWriter trapFile); - - public void WriteQuotedId(TextWriter trapFile) + public override void WriteQuotedId(TextWriter trapFile) { trapFile.Write("@\""); WriteId(trapFile); @@ -26,21 +22,7 @@ namespace Semmle.Extraction.CIL.Entities trapFile.Write('\"'); } - public abstract string IdSuffix { get; } - - Location IEntity.ReportingLocation => throw new NotImplementedException(); - - public void Extract(Context cx2) { cx2.Populate(this); } - - public abstract IEnumerable Contents { get; } - - public override string ToString() - { - using var writer = new StringWriter(); - WriteQuotedId(writer); - return writer.ToString(); - } - - TrapStackBehaviour IEntity.TrapStackBehaviour => TrapStackBehaviour.NoLabel; + public abstract IEnumerable MethodParameters { get; } + public abstract IEnumerable TypeParameters { get; } } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeParameter.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeParameter.cs index 7c8d2222fa5..fd18374ed2d 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeParameter.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeParameter.cs @@ -9,9 +9,9 @@ namespace Semmle.Extraction.CIL.Entities { internal abstract class TypeParameter : Type { - protected readonly GenericContext gc; + protected readonly IGenericContext gc; - protected TypeParameter(GenericContext gc) : base(gc.Cx) + protected TypeParameter(IGenericContext gc) : base(gc.Cx) { this.gc = gc; } diff --git a/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeSignatureDecoder.cs b/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeSignatureDecoder.cs index dcb986f1526..4417becc58d 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeSignatureDecoder.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Entities/TypeSignatureDecoder.cs @@ -1,13 +1,14 @@ using System; using System.Reflection.Metadata; using System.Collections.Immutable; +using System.Linq; namespace Semmle.Extraction.CIL.Entities { /// /// Decodes a type signature and produces a Type, for use by DecodeSignature() and friends. /// - public class TypeSignatureDecoder : ISignatureTypeProvider + public class TypeSignatureDecoder : ISignatureTypeProvider { private readonly Context cx; @@ -22,22 +23,22 @@ namespace Semmle.Extraction.CIL.Entities Type IConstructedTypeProvider.GetByReferenceType(Type elementType) => new ByRefType(cx, elementType); - Type ISignatureTypeProvider.GetFunctionPointerType(MethodSignature signature) => + Type ISignatureTypeProvider.GetFunctionPointerType(MethodSignature signature) => cx.Populate(new FunctionPointerType(cx, signature)); Type IConstructedTypeProvider.GetGenericInstantiation(Type genericType, ImmutableArray typeArguments) => genericType.Construct(typeArguments); - Type ISignatureTypeProvider.GetGenericMethodParameter(GenericContext genericContext, int index) => - genericContext.GetGenericMethodParameter(index); + Type ISignatureTypeProvider.GetGenericMethodParameter(IGenericContext genericContext, int index) => + genericContext.MethodParameters.ElementAt(index); - Type ISignatureTypeProvider.GetGenericTypeParameter(GenericContext genericContext, int index) => - genericContext.GetGenericTypeParameter(index); + Type ISignatureTypeProvider.GetGenericTypeParameter(IGenericContext genericContext, int index) => + genericContext.TypeParameters.ElementAt(index); - Type ISignatureTypeProvider.GetModifiedType(Type modifier, Type unmodifiedType, bool isRequired) => + Type ISignatureTypeProvider.GetModifiedType(Type modifier, Type unmodifiedType, bool isRequired) => new ModifiedType(cx, unmodifiedType, modifier, isRequired); - Type ISignatureTypeProvider.GetPinnedType(Type elementType) => elementType; + Type ISignatureTypeProvider.GetPinnedType(Type elementType) => elementType; Type IConstructedTypeProvider.GetPointerType(Type elementType) => cx.Populate(new PointerType(cx, elementType)); @@ -53,7 +54,7 @@ namespace Semmle.Extraction.CIL.Entities Type ISimpleTypeProvider.GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) => (Type)cx.Create(handle); - Type ISignatureTypeProvider.GetTypeFromSpecification(MetadataReader reader, GenericContext genericContext, TypeSpecificationHandle handle, byte rawTypeKind) => + Type ISignatureTypeProvider.GetTypeFromSpecification(MetadataReader reader, IGenericContext genericContext, TypeSpecificationHandle handle, byte rawTypeKind) => throw new NotImplementedException(); } } diff --git a/csharp/extractor/Semmle.Extraction.CIL/ExtractionProduct.cs b/csharp/extractor/Semmle.Extraction.CIL/ExtractionProduct.cs deleted file mode 100644 index 52415af9349..00000000000 --- a/csharp/extractor/Semmle.Extraction.CIL/ExtractionProduct.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace Semmle.Extraction.CIL -{ - /// - /// Something that is extracted from an entity. - /// - /// - /// - /// The extraction algorithm proceeds as follows: - /// - Construct entity - /// - Call Extract() - /// - IExtractedEntity check if already extracted - /// - Enumerate Contents to produce more extraction products - /// - Extract these until there is nothing left to extract - /// - public interface IExtractionProduct - { - /// - /// Perform further extraction/population of this item as necessary. - /// - /// - /// The extraction context. - void Extract(Context cx); - } - - /// - /// An entity which has been extracted. - /// - public interface IExtractedEntity : IEntity, IExtractionProduct - { - /// - /// The contents of the entity. - /// - IEnumerable Contents { get; } - } - - /// - /// An entity that has contents to extract. There is no need to populate - /// a key as it's done in the contructor. - /// - public abstract class UnlabelledEntity : IExtractedEntity - { - public abstract IEnumerable Contents { get; } - public Label Label { get; set; } - - public void WriteId(System.IO.TextWriter trapFile) - { - trapFile.Write('*'); - } - - public void WriteQuotedId(TextWriter trapFile) - { - WriteId(trapFile); - } - - public Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException(); - - public virtual void Extract(Context cx2) - { - cx2.Extract(this); - } - - public Context Cx { get; } - - protected UnlabelledEntity(Context cx) - { - this.Cx = cx; - cx.Cx.AddFreshLabel(this); - } - - TrapStackBehaviour IEntity.TrapStackBehaviour => TrapStackBehaviour.NoLabel; - } - - /// - /// An entity that needs to be populated during extraction. - /// This assigns a key and optionally extracts its contents. - /// - public abstract class LabelledEntity : IExtractedEntity - { - public abstract IEnumerable Contents { get; } - public Label Label { get; set; } - public Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException(); - - public abstract void WriteId(System.IO.TextWriter trapFile); - - public abstract string IdSuffix { get; } - - public void WriteQuotedId(TextWriter trapFile) - { - trapFile.Write("@\""); - WriteId(trapFile); - trapFile.Write(IdSuffix); - trapFile.Write('\"'); - } - - public void Extract(Context cx2) - { - cx2.Populate(this); - } - - public Context Cx { get; } - - protected LabelledEntity(Context cx) - { - this.Cx = cx; - } - - public override string ToString() - { - using var writer = new StringWriter(); - WriteQuotedId(writer); - return writer.ToString(); - } - - TrapStackBehaviour IEntity.TrapStackBehaviour => TrapStackBehaviour.NoLabel; - } - - /// - /// A tuple that is an extraction product. - /// - internal class Tuple : IExtractionProduct - { - private readonly Extraction.Tuple tuple; - - public Tuple(string name, params object[] args) - { - tuple = new Extraction.Tuple(name, args); - } - - public void Extract(Context cx) - { - cx.Cx.Emit(tuple); - } - - public override string ToString() => tuple.ToString(); - } -} diff --git a/csharp/extractor/Semmle.Extraction.CIL/GenericContext.cs b/csharp/extractor/Semmle.Extraction.CIL/GenericContext.cs deleted file mode 100644 index 6db62bf5bbf..00000000000 --- a/csharp/extractor/Semmle.Extraction.CIL/GenericContext.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace Semmle.Extraction.CIL -{ - /// - /// When we decode a type/method signature, we need access to - /// generic parameters. - /// - public abstract class GenericContext - { - public Context Cx { get; } - - protected GenericContext(Context cx) - { - this.Cx = cx; - } - - /// - /// The list of generic type parameters/arguments, including type parameters/arguments of - /// containing types. - /// - public abstract IEnumerable TypeParameters { get; } - - /// - /// The list of generic method parameters. - /// - public abstract IEnumerable MethodParameters { get; } - - /// - /// Gets the `p`th type parameter. - /// - /// The index of the parameter. - /// - /// For constructed types, the supplied type. - /// For unbound types, the type parameter. - /// - public Entities.Type GetGenericTypeParameter(int p) - { - return TypeParameters.ElementAt(p); - } - - /// - /// Gets the `p`th method type parameter. - /// - /// The index of the parameter. - /// - /// For constructed types, the supplied type. - /// For unbound types, the type parameter. - /// - public Entities.Type GetGenericMethodParameter(int p) - { - return MethodParameters.ElementAt(p); - } - } -} diff --git a/csharp/extractor/Semmle.Extraction.CIL/PDB/MdProvider.cs b/csharp/extractor/Semmle.Extraction.CIL/PDB/MdProvider.cs index c6c943032a6..fc22f1f6e94 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/PDB/MdProvider.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/PDB/MdProvider.cs @@ -2,6 +2,8 @@ using System; using Microsoft.DiaSymReader; using System.Reflection; +#pragma warning disable IDE0060, CA1822 + namespace Semmle.Extraction.PDB { /// @@ -31,3 +33,5 @@ namespace Semmle.Extraction.PDB throw new NotImplementedException(); } } + +#pragma warning restore diff --git a/csharp/extractor/Semmle.Extraction.CIL/Tuples.cs b/csharp/extractor/Semmle.Extraction.CIL/Tuples.cs index 0ae92386c3e..c8ed3445c6f 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Tuples.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Tuples.cs @@ -14,10 +14,10 @@ namespace Semmle.Extraction.CIL internal static Tuple cil_adder(Event member, Method method) => new Tuple("cil_adder", member, method); - internal static Tuple cil_access(Instruction i, IEntity m) => + internal static Tuple cil_access(Instruction i, IExtractedEntity m) => new Tuple("cil_access", i, m); - internal static Tuple cil_attribute(Attribute attribute, IEntity @object, Method constructor) => + internal static Tuple cil_attribute(Attribute attribute, IExtractedEntity @object, Method constructor) => new Tuple("cil_attribute", attribute, @object, constructor); internal static Tuple cil_attribute_named_argument(Attribute attribute, string name, string value) => @@ -197,7 +197,7 @@ namespace Semmle.Extraction.CIL internal static Tuple cil_custom_modifiers(ICustomModifierReceiver receiver, Type modifier, bool isRequired) => new Tuple("cil_custom_modifiers", receiver, modifier, isRequired ? 1 : 0); - internal static Tuple cil_type_annotation(IEntity receiver, TypeAnnotation annotation) => + internal static Tuple cil_type_annotation(IExtractedEntity receiver, TypeAnnotation annotation) => new Tuple("cil_type_annotation", receiver, (int)annotation); internal static Tuple containerparent(Folder parent, IFileOrFolder child) => @@ -215,7 +215,7 @@ namespace Semmle.Extraction.CIL internal static Tuple locations_default(PdbSourceLocation label, File file, int startLine, int startCol, int endLine, int endCol) => new Tuple("locations_default", label, file, startLine, startCol, endLine, endCol); - internal static Tuple metadata_handle(IEntity entity, Assembly assembly, int handleValue) => + internal static Tuple metadata_handle(IExtractedEntity entity, Assembly assembly, int handleValue) => new Tuple("metadata_handle", entity, assembly, handleValue); internal static Tuple namespaces(Namespace ns, string name) => diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs index f9e0d7c3806..b04b429993d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs @@ -17,7 +17,7 @@ namespace Semmle.Extraction.CSharp /// public sealed class Analyser : IDisposable { - private IExtractor extractor; + private Extraction.Extractor extractor; private CSharpCompilation compilation; private Layout layout; private bool init; @@ -230,7 +230,7 @@ namespace Semmle.Extraction.CSharp var projectLayout = layout.LookupProjectOrDefault(transformedAssemblyPath); var trapWriter = projectLayout.CreateTrapWriter(Logger, transformedAssemblyPath, options.TrapCompression, discardDuplicates: false); compilationTrapFile = trapWriter; // Dispose later - var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix); + var cx = new Context(extractor, compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix); compilationEntity = Entities.Compilation.Create(cx); } @@ -285,7 +285,7 @@ namespace Semmle.Extraction.CSharp if (c.GetAssemblyOrModuleSymbol(r) is IAssemblySymbol assembly) { - var cx = extractor.CreateContext(c, trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix); + var cx = new Context(extractor, c, trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix); foreach (var module in assembly.Modules) { @@ -371,7 +371,7 @@ namespace Semmle.Extraction.CSharp if (!upToDate) { - var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new SourceScope(tree), AddAssemblyTrapPrefix); + var cx = new Context(extractor, compilation.Clone(), trapWriter, new SourceScope(tree), AddAssemblyTrapPrefix); // Ensure that the file itself is populated in case the source file is totally empty var root = tree.GetRoot(); Extraction.Entities.File.Create(cx, root.SyntaxTree.FilePath); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Accessor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Accessor.cs index 08782ec36a7..d5a20e8eef6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Accessor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Accessor.cs @@ -29,9 +29,9 @@ namespace Semmle.Extraction.CSharp.Entities /// /// Gets the property symbol associated with this accessor. /// - private IPropertySymbol PropertySymbol => GetPropertySymbol(symbol); + private IPropertySymbol PropertySymbol => GetPropertySymbol(Symbol); - public new Accessor OriginalDefinition => Create(Context, symbol.OriginalDefinition); + public new Accessor OriginalDefinition => Create(Context, Symbol.OriginalDefinition); public override void Populate(TextWriter trapFile) { @@ -42,42 +42,42 @@ namespace Semmle.Extraction.CSharp.Entities var prop = PropertySymbol; if (prop == null) { - Context.ModelError(symbol, "Unhandled accessor associated symbol"); + Context.ModelError(Symbol, "Unhandled accessor associated symbol"); return; } var parent = Property.Create(Context, prop); int kind; Accessor unboundAccessor; - if (SymbolEqualityComparer.Default.Equals(symbol, prop.GetMethod)) + if (SymbolEqualityComparer.Default.Equals(Symbol, prop.GetMethod)) { kind = 1; unboundAccessor = Create(Context, prop.OriginalDefinition.GetMethod); } - else if (SymbolEqualityComparer.Default.Equals(symbol, prop.SetMethod)) + else if (SymbolEqualityComparer.Default.Equals(Symbol, prop.SetMethod)) { kind = 2; unboundAccessor = Create(Context, prop.OriginalDefinition.SetMethod); } else { - Context.ModelError(symbol, "Unhandled accessor kind"); + Context.ModelError(Symbol, "Unhandled accessor kind"); return; } - trapFile.accessors(this, kind, symbol.Name, parent, unboundAccessor); + trapFile.accessors(this, kind, Symbol.Name, parent, unboundAccessor); foreach (var l in Locations) trapFile.accessor_location(this, l); Overrides(trapFile); - if (symbol.FromSource() && Block == null) + if (Symbol.FromSource() && Block == null) { trapFile.compiler_generated(this); } - if (symbol.IsInitOnly) + if (Symbol.IsInitOnly) { trapFile.init_only_accessors(this); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs index 9fef80b383a..ba1d2093765 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs @@ -47,7 +47,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void Populate(TextWriter trapFile) { - var type = Type.Create(Context, symbol.AttributeClass); + var type = Type.Create(Context, Symbol.AttributeClass); trapFile.attributes(this, type.TypeRef, entity); trapFile.attribute_location(this, Location); @@ -69,10 +69,10 @@ namespace Semmle.Extraction.CSharp.Entities var ctorArguments = attributeSyntax?.ArgumentList?.Arguments.Where(a => a.NameEquals == null).ToList(); var childIndex = 0; - for (var i = 0; i < symbol.ConstructorArguments.Length; i++) + for (var i = 0; i < Symbol.ConstructorArguments.Length; i++) { - var constructorArgument = symbol.ConstructorArguments[i]; - var paramName = symbol.AttributeConstructor?.Parameters[i].Name; + var constructorArgument = Symbol.ConstructorArguments[i]; + var paramName = Symbol.AttributeConstructor?.Parameters[i].Name; var argSyntax = ctorArguments?.SingleOrDefault(a => a.NameColon != null && a.NameColon.Name.Identifier.Text == paramName); if (argSyntax == null && // couldn't find named argument @@ -89,7 +89,7 @@ namespace Semmle.Extraction.CSharp.Entities childIndex++); } - foreach (var namedArgument in symbol.NamedArguments) + foreach (var namedArgument in Symbol.NamedArguments) { var expr = CreateExpressionFromArgument( namedArgument.Value, @@ -118,7 +118,7 @@ namespace Semmle.Extraction.CSharp.Entities private Semmle.Extraction.Entities.Location location; private Semmle.Extraction.Entities.Location Location => - location ?? (location = Semmle.Extraction.Entities.Location.Create(Context, attributeSyntax is null ? entity.ReportingLocation : attributeSyntax.Name.GetLocation())); + location ?? (location = Context.CreateLocation(attributeSyntax is null ? entity.ReportingLocation : attributeSyntax.Name.GetLocation())); public override bool NeedsPopulation => true; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CallTypeExtensions.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CallTypeExtensions.cs new file mode 100644 index 00000000000..6b9564e49dc --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CallTypeExtensions.cs @@ -0,0 +1,27 @@ +using Semmle.Extraction.Kinds; + +namespace Semmle.Extraction.CSharp.Entities +{ + internal static class CallTypeExtensions + { + /// + /// Adjust the expression kind to match this call type. + /// + public static ExprKind AdjustKind(this Expression.CallType ct, ExprKind k) + { + if (k == ExprKind.ADDRESS_OF) + { + return k; + } + + switch (ct) + { + case Expression.CallType.Dynamic: + case Expression.CallType.UserOperator: + return ExprKind.OPERATOR_INVOCATION; + default: + return k; + } + } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentBlock.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentBlock.cs index ab6802cf457..e06b6aebe7f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentBlock.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentBlock.cs @@ -13,8 +13,8 @@ namespace Semmle.Extraction.CSharp.Entities { trapFile.commentblock(this); var child = 0; - trapFile.commentblock_location(this, Context.Create(symbol.Location)); - foreach (var l in symbol.CommentLines) + trapFile.commentblock_location(this, Context.CreateLocation(Symbol.Location)); + foreach (var l in Symbol.CommentLines) { trapFile.commentblock_child(this, (CommentLine)l, child++); } @@ -24,11 +24,11 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - trapFile.WriteSubId(Context.Create(symbol.Location)); + trapFile.WriteSubId(Context.CreateLocation(Symbol.Location)); trapFile.Write(";commentblock"); } - public override Microsoft.CodeAnalysis.Location ReportingLocation => symbol.Location; + public override Microsoft.CodeAnalysis.Location ReportingLocation => Symbol.Location; public void BindTo(Label entity, CommentBinding binding) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentLine.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentLine.cs index e390cb8c509..102ea262b39 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentLine.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CommentLine.cs @@ -13,28 +13,28 @@ namespace Semmle.Extraction.CSharp.Entities RawText = raw; } - public Microsoft.CodeAnalysis.Location Location => symbol.Item1; + public Microsoft.CodeAnalysis.Location Location => Symbol.Item1; public CommentLineType Type { get; private set; } - public string Text { get { return symbol.Item2; } } + public string Text { get { return Symbol.Item2; } } public string RawText { get; private set; } private Location location; public override void Populate(TextWriter trapFile) { - location = Context.Create(Location); + location = Context.CreateLocation(Location); trapFile.commentline(this, Type == CommentLineType.MultilineContinuation ? CommentLineType.Multiline : Type, Text, RawText); trapFile.commentline_location(this, location); } - public override Microsoft.CodeAnalysis.Location ReportingLocation => location.symbol; + public override Microsoft.CodeAnalysis.Location ReportingLocation => location.Symbol; public override bool NeedsPopulation => true; public override void WriteId(TextWriter trapFile) { - trapFile.WriteSubId(Context.Create(Location)); + trapFile.WriteSubId(Context.CreateLocation(Location)); trapFile.Write(";commentline"); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Diagnostic.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Diagnostic.cs index 2900e5b0ae0..def6edbf5ad 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Diagnostic.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Diagnostic.cs @@ -17,7 +17,7 @@ namespace Semmle.Extraction.CSharp.Entities protected override void Populate(TextWriter trapFile) { trapFile.diagnostics(this, (int)diagnostic.Severity, diagnostic.Id, diagnostic.Descriptor.Title.ToString(), - diagnostic.GetMessage(), Extraction.Entities.Location.Create(cx, diagnostic.Location)); + diagnostic.GetMessage(), Context.CreateLocation(diagnostic.Location)); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs index 499e6ec418a..d04e80324ff 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs @@ -19,10 +19,10 @@ namespace Semmle.Extraction.CSharp.Entities PopulateModifiers(trapFile); ContainingType.PopulateGenerics(); - trapFile.constructors(this, symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition); + trapFile.constructors(this, Symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition); trapFile.constructor_location(this, Location); - if (symbol.IsImplicitlyDeclared) + if (Symbol.IsImplicitlyDeclared) { var lineCounts = new LineCounts() { Total = 2, Code = 1, Comment = 0 }; trapFile.numlines(this, lineCounts); @@ -48,10 +48,10 @@ namespace Semmle.Extraction.CSharp.Entities switch (initializer.Kind()) { case SyntaxKind.BaseConstructorInitializer: - initializerType = symbol.ContainingType.BaseType; + initializerType = Symbol.ContainingType.BaseType; break; case SyntaxKind.ThisConstructorInitializer: - initializerType = symbol.ContainingType; + initializerType = Symbol.ContainingType; break; default: Context.ModelError(initializer, "Unknown initializer"); @@ -60,7 +60,7 @@ namespace Semmle.Extraction.CSharp.Entities var initInfo = new ExpressionInfo(Context, AnnotatedTypeSymbol.CreateNotAnnotated(initializerType), - Context.Create(initializer.ThisOrBaseKeyword.GetLocation()), + Context.CreateLocation(initializer.ThisOrBaseKeyword.GetLocation()), Kinds.ExprKind.CONSTRUCTOR_INIT, this, -1, @@ -73,7 +73,7 @@ namespace Semmle.Extraction.CSharp.Entities if (target == null) { - Context.ModelError(symbol, "Unable to resolve call"); + Context.ModelError(Symbol, "Unable to resolve call"); return; } @@ -90,7 +90,7 @@ namespace Semmle.Extraction.CSharp.Entities { get { - return symbol.DeclaringSyntaxReferences + return Symbol.DeclaringSyntaxReferences .Select(r => r.GetSyntax()) .OfType() .FirstOrDefault(); @@ -114,15 +114,15 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - if (symbol.IsStatic) + if (Symbol.IsStatic) trapFile.Write("static"); trapFile.WriteSubId(ContainingType); - AddParametersToId(Context, trapFile, symbol); + AddParametersToId(Context, trapFile, Symbol); trapFile.Write(";constructor"); } private ConstructorDeclarationSyntax GetSyntax() => - symbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax()).OfType().FirstOrDefault(); + Symbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax()).OfType().FirstOrDefault(); public override Microsoft.CodeAnalysis.Location FullLocation => ReportingLocation; @@ -136,12 +136,12 @@ namespace Semmle.Extraction.CSharp.Entities return syn.Identifier.GetLocation(); } - if (symbol.IsImplicitlyDeclared) + if (Symbol.IsImplicitlyDeclared) { return ContainingType.ReportingLocation; } - return symbol.ContainingType.Locations.FirstOrDefault(); + return Symbol.ContainingType.Locations.FirstOrDefault(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs index aa125cae0a1..05a752cb35c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs @@ -17,11 +17,11 @@ namespace Semmle.Extraction.CSharp.Entities { get { - return symbol.DeclaringSyntaxReferences + return Symbol.DeclaringSyntaxReferences .Select(r => r.GetSyntax()) .OfType() .Select(s => s.FixedLocation()) - .Concat(symbol.Locations) + .Concat(Symbol.Locations) .FirstOrDefault(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Destructor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Destructor.cs index a5c4acc3c03..0862baa3600 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Destructor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Destructor.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities PopulateModifiers(trapFile); ContainingType.PopulateGenerics(); - trapFile.destructors(this, string.Format("~{0}", symbol.ContainingType.Name), ContainingType, OriginalDefinition(Context, this, symbol)); + trapFile.destructors(this, string.Format("~{0}", Symbol.ContainingType.Name), ContainingType, OriginalDefinition(Context, this, Symbol)); trapFile.destructor_location(this, Location); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Event.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Event.cs index 5c24cf97655..fe095ed4462 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Event.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Event.cs @@ -14,20 +14,20 @@ namespace Semmle.Extraction.CSharp.Entities { trapFile.WriteSubId(ContainingType); trapFile.Write('.'); - Method.AddExplicitInterfaceQualifierToId(Context, trapFile, symbol.ExplicitInterfaceImplementations); - trapFile.Write(symbol.Name); + Method.AddExplicitInterfaceQualifierToId(Context, trapFile, Symbol.ExplicitInterfaceImplementations); + trapFile.Write(Symbol.Name); trapFile.Write(";event"); } public override void Populate(TextWriter trapFile) { - PopulateNullability(trapFile, symbol.GetAnnotatedType()); + PopulateNullability(trapFile, Symbol.GetAnnotatedType()); - var type = Type.Create(Context, symbol.Type); - trapFile.events(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition)); + var type = Type.Create(Context, Symbol.Type); + trapFile.events(this, Symbol.GetName(), ContainingType, type.TypeRef, Create(Context, Symbol.OriginalDefinition)); - var adder = symbol.AddMethod; - var remover = symbol.RemoveMethod; + var adder = Symbol.AddMethod; + var remover = Symbol.RemoveMethod; if (!(adder is null)) Method.Create(Context, adder); @@ -39,10 +39,10 @@ namespace Semmle.Extraction.CSharp.Entities BindComments(); var declSyntaxReferences = IsSourceDeclaration - ? symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray() + ? Symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray() : Enumerable.Empty(); - foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType))) + foreach (var explicitInterface in Symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType))) { trapFile.explicitly_implements(this, explicitInterface.TypeRef); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/EventAccessor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/EventAccessor.cs index c75b6766dc7..82be6a6e406 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/EventAccessor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/EventAccessor.cs @@ -11,7 +11,7 @@ namespace Semmle.Extraction.CSharp.Entities /// /// Gets the event symbol associated with this accessor. /// - private IEventSymbol EventSymbol => symbol.AssociatedSymbol as IEventSymbol; + private IEventSymbol EventSymbol => Symbol.AssociatedSymbol as IEventSymbol; public override void Populate(TextWriter trapFile) { @@ -21,30 +21,30 @@ namespace Semmle.Extraction.CSharp.Entities var @event = EventSymbol; if (@event == null) { - Context.ModelError(symbol, "Unhandled event accessor associated symbol"); + Context.ModelError(Symbol, "Unhandled event accessor associated symbol"); return; } var parent = Event.Create(Context, @event); int kind; EventAccessor unboundAccessor; - if (SymbolEqualityComparer.Default.Equals(symbol, @event.AddMethod)) + if (SymbolEqualityComparer.Default.Equals(Symbol, @event.AddMethod)) { kind = 1; unboundAccessor = Create(Context, @event.OriginalDefinition.AddMethod); } - else if (SymbolEqualityComparer.Default.Equals(symbol, @event.RemoveMethod)) + else if (SymbolEqualityComparer.Default.Equals(Symbol, @event.RemoveMethod)) { kind = 2; unboundAccessor = Create(Context, @event.OriginalDefinition.RemoveMethod); } else { - Context.ModelError(symbol, "Undhandled event accessor kind"); + Context.ModelError(Symbol, "Undhandled event accessor kind"); return; } - trapFile.event_accessors(this, kind, symbol.Name, parent, unboundAccessor); + trapFile.event_accessors(this, kind, Symbol.Name, parent, unboundAccessor); foreach (var l in Locations) trapFile.event_accessor_location(this, l); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs index 6c738da3af1..fe62bf8323b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs @@ -2,8 +2,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Semmle.Extraction.CSharp.Entities.Expressions; -using Semmle.Extraction.CSharp.Populators; -using Semmle.Extraction.Entities; using Semmle.Extraction.Kinds; using System; using System.IO; @@ -11,14 +9,6 @@ using System.Linq; namespace Semmle.Extraction.CSharp.Entities { - public interface IExpressionParentEntity : IEntity - { - /// - /// Whether this entity is the parent of a top-level expression. - /// - bool IsTopLevelParent { get; } - } - internal class Expression : FreshEntity, IExpressionParentEntity { private readonly IExpressionInfo info; @@ -39,7 +29,7 @@ namespace Semmle.Extraction.CSharp.Entities protected sealed override void Populate(TextWriter trapFile) { - var type = Type.HasValue ? Entities.Type.Create(cx, Type.Value) : NullType.Create(cx); + var type = Type.HasValue ? Entities.Type.Create(Context, Type.Value) : NullType.Create(Context); trapFile.expressions(this, Kind, type.TypeRef); if (info.Parent.IsTopLevelParent) trapFile.expr_parent_top_level(this, info.Child, info.Parent); @@ -49,7 +39,7 @@ namespace Semmle.Extraction.CSharp.Entities if (Type.HasValue && !Type.Value.HasObliviousNullability()) { - var n = NullabilityEntity.Create(cx, Nullability.Create(Type.Value)); + var n = NullabilityEntity.Create(Context, Nullability.Create(Type.Value)); trapFile.type_nullability(this, n); } @@ -67,7 +57,7 @@ namespace Semmle.Extraction.CSharp.Entities type.PopulateGenerics(); } - public override Microsoft.CodeAnalysis.Location ReportingLocation => Location.symbol; + public override Microsoft.CodeAnalysis.Location ReportingLocation => Location.Symbol; bool IExpressionParentEntity.IsTopLevelParent => false; @@ -180,10 +170,10 @@ namespace Semmle.Extraction.CSharp.Entities /// The expression. public void OperatorCall(TextWriter trapFile, ExpressionSyntax node) { - var @operator = cx.GetSymbolInfo(node); + var @operator = Context.GetSymbolInfo(node); if (@operator.Symbol is IMethodSymbol method) { - var callType = GetCallType(cx, node); + var callType = GetCallType(Context, node); if (callType == CallType.Dynamic) { UserOperator.OperatorSymbol(method.Name, out var operatorName); @@ -191,7 +181,7 @@ namespace Semmle.Extraction.CSharp.Entities return; } - trapFile.expr_call(this, Method.Create(cx, method)); + trapFile.expr_call(this, Method.Create(Context, method)); } } @@ -277,7 +267,7 @@ namespace Semmle.Extraction.CSharp.Entities private void PopulateArgument(TextWriter trapFile, ArgumentSyntax arg, int child) { - var expr = Create(cx, arg.Expression, this, child); + var expr = Create(Context, arg.Expression, this, child); int mode; switch (arg.RefOrOutKeyword.Kind()) { @@ -308,314 +298,4 @@ namespace Semmle.Extraction.CSharp.Entities public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.OptionalLabel; } - - internal static class CallTypeExtensions - { - /// - /// Adjust the expression kind to match this call type. - /// - public static ExprKind AdjustKind(this Expression.CallType ct, ExprKind k) - { - if (k == ExprKind.ADDRESS_OF) - { - return k; - } - - switch (ct) - { - case Expression.CallType.Dynamic: - case Expression.CallType.UserOperator: - return ExprKind.OPERATOR_INVOCATION; - default: - return k; - } - } - } - - internal abstract class Expression : Expression - where TExpressionSyntax : ExpressionSyntax - { - public TExpressionSyntax Syntax { get; } - - protected Expression(ExpressionNodeInfo info) - : base(info) - { - Syntax = (TExpressionSyntax)info.Node; - } - - /// - /// Populates expression-type specific relations in the trap file. The general relations - /// expressions and expr_location are populated by the constructor - /// (should not fail), so even if expression-type specific population fails (e.g., in - /// standalone extraction), the expression created via - /// will - /// still be valid. - /// - protected abstract void PopulateExpression(TextWriter trapFile); - - protected new Expression TryPopulate() - { - cx.Try(Syntax, null, () => PopulateExpression(cx.TrapWriter.Writer)); - return this; - } - } - - /// - /// Holds all information required to create an Expression entity. - /// - internal interface IExpressionInfo - { - Context Context { get; } - - /// - /// The type of the expression. - /// - AnnotatedTypeSymbol? Type { get; } - - /// - /// The location of the expression. - /// - Extraction.Entities.Location Location { get; } - - /// - /// The kind of the expression. - /// - ExprKind Kind { get; } - - /// - /// The parent of the expression. - /// - IExpressionParentEntity Parent { get; } - - /// - /// The child index of the expression. - /// - int Child { get; } - - /// - /// Holds if this is an implicit expression. - /// - bool IsCompilerGenerated { get; } - - /// - /// Gets a string representation of the value. - /// null is encoded as the string "null". - /// If the expression does not have a value, then this - /// is null. - /// - string ExprValue { get; } - - NullableFlowState FlowState { get; } - } - - /// - /// Explicitly constructed expression information. - /// - internal class ExpressionInfo : IExpressionInfo - { - public Context Context { get; } - public AnnotatedTypeSymbol? Type { get; } - public Extraction.Entities.Location Location { get; } - public ExprKind Kind { get; } - public IExpressionParentEntity Parent { get; } - public int Child { get; } - public bool IsCompilerGenerated { get; } - public string ExprValue { get; } - - public ExpressionInfo(Context cx, AnnotatedTypeSymbol? type, Extraction.Entities.Location location, ExprKind kind, - IExpressionParentEntity parent, int child, bool isCompilerGenerated, string value) - { - Context = cx; - Type = type; - Location = location; - Kind = kind; - Parent = parent; - Child = child; - ExprValue = value; - IsCompilerGenerated = isCompilerGenerated; - } - - // Synthetic expressions don't have a flow state. - public NullableFlowState FlowState => NullableFlowState.None; - } - - /// - /// Expression information constructed from a syntax node. - /// - internal class ExpressionNodeInfo : IExpressionInfo - { - public ExpressionNodeInfo(Context cx, ExpressionSyntax node, IExpressionParentEntity parent, int child) : - this(cx, node, parent, child, cx.GetTypeInfo(node)) - { - } - - public ExpressionNodeInfo(Context cx, ExpressionSyntax node, IExpressionParentEntity parent, int child, TypeInfo typeInfo) - { - Context = cx; - Node = node; - Parent = parent; - Child = child; - TypeInfo = typeInfo; - Conversion = cx.GetModel(node).GetConversion(node); - } - - public Context Context { get; } - public ExpressionSyntax Node { get; private set; } - public IExpressionParentEntity Parent { get; set; } - public int Child { get; set; } - public TypeInfo TypeInfo { get; } - public Microsoft.CodeAnalysis.CSharp.Conversion Conversion { get; } - - public AnnotatedTypeSymbol ResolvedType => new AnnotatedTypeSymbol(TypeInfo.Type.DisambiguateType(), TypeInfo.Nullability.Annotation); - public AnnotatedTypeSymbol ConvertedType => new AnnotatedTypeSymbol(TypeInfo.ConvertedType.DisambiguateType(), TypeInfo.ConvertedNullability.Annotation); - - private AnnotatedTypeSymbol? cachedType; - private bool cachedTypeSet; - public AnnotatedTypeSymbol? Type - { - get - { - if (cachedTypeSet) - return cachedType; - - var type = ResolvedType; - - if (type.Symbol == null) - type.Symbol = (TypeInfo.Type ?? TypeInfo.ConvertedType).DisambiguateType(); - - // Roslyn workaround: It can't work out the type of "new object[0]" - // Clearly a bug. - if (type.Symbol?.TypeKind == Microsoft.CodeAnalysis.TypeKind.Error) - { - if (Node is ArrayCreationExpressionSyntax arrayCreation) - { - var elementType = Context.GetType(arrayCreation.Type.ElementType); - - if (elementType.Symbol != null) - // There seems to be no way to create an array with a nullable element at present. - return new AnnotatedTypeSymbol(Context.Compilation.CreateArrayTypeSymbol(elementType.Symbol, arrayCreation.Type.RankSpecifiers.Count), NullableAnnotation.NotAnnotated); - } - - Context.ModelError(Node, "Failed to determine type"); - } - - cachedType = type; - cachedTypeSet = true; - - return type; - } - } - - private Microsoft.CodeAnalysis.Location location; - - public Microsoft.CodeAnalysis.Location CodeAnalysisLocation - { - get - { - if (location == null) - location = Node.FixedLocation(); - return location; - } - set - { - location = value; - } - } - - public SemanticModel Model => Context.GetModel(Node); - - public string ExprValue - { - get - { - var c = Model.GetConstantValue(Node); - if (c.HasValue) - { - return Expression.ValueAsString(c.Value); - } - - if (TryGetBoolValueFromLiteral(out var val)) - { - return Expression.ValueAsString(val); - } - - return null; - } - } - - private Extraction.Entities.Location cachedLocation; - - public Extraction.Entities.Location Location - { - get - { - if (cachedLocation == null) - cachedLocation = Context.Create(CodeAnalysisLocation); - return cachedLocation; - } - - set - { - cachedLocation = value; - } - } - - public ExprKind Kind { get; set; } = ExprKind.UNKNOWN; - - public bool IsCompilerGenerated { get; set; } - - public ExpressionNodeInfo SetParent(IExpressionParentEntity parent, int child) - { - Parent = parent; - Child = child; - return this; - } - - public ExpressionNodeInfo SetKind(ExprKind kind) - { - Kind = kind; - return this; - } - - public ExpressionNodeInfo SetType(AnnotatedTypeSymbol? type) - { - cachedType = type; - cachedTypeSet = true; - return this; - } - - public ExpressionNodeInfo SetNode(ExpressionSyntax node) - { - Node = node; - return this; - } - - private SymbolInfo cachedSymbolInfo; - - public SymbolInfo SymbolInfo - { - get - { - if (cachedSymbolInfo.Symbol == null && cachedSymbolInfo.CandidateReason == CandidateReason.None) - cachedSymbolInfo = Model.GetSymbolInfo(Node); - return cachedSymbolInfo; - } - } - - public NullableFlowState FlowState => TypeInfo.Nullability.FlowState; - - private bool TryGetBoolValueFromLiteral(out bool val) - { - var isTrue = Node.IsKind(SyntaxKind.TrueLiteralExpression); - var isFalse = Node.IsKind(SyntaxKind.FalseLiteralExpression); - - val = isTrue; - return isTrue || isFalse; - } - - public bool IsBoolLiteral() - { - return TryGetBoolValueFromLiteral(out var _); - } - } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/ExpressionInfo.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/ExpressionInfo.cs new file mode 100644 index 00000000000..feb1eaa2d93 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/ExpressionInfo.cs @@ -0,0 +1,36 @@ +using Microsoft.CodeAnalysis; +using Semmle.Extraction.Kinds; + +namespace Semmle.Extraction.CSharp.Entities +{ + /// + /// Explicitly constructed expression information. + /// + internal class ExpressionInfo : IExpressionInfo + { + public Context Context { get; } + public AnnotatedTypeSymbol? Type { get; } + public Extraction.Entities.Location Location { get; } + public ExprKind Kind { get; } + public IExpressionParentEntity Parent { get; } + public int Child { get; } + public bool IsCompilerGenerated { get; } + public string ExprValue { get; } + + public ExpressionInfo(Context cx, AnnotatedTypeSymbol? type, Extraction.Entities.Location location, ExprKind kind, + IExpressionParentEntity parent, int child, bool isCompilerGenerated, string value) + { + Context = cx; + Type = type; + Location = location; + Kind = kind; + Parent = parent; + Child = child; + ExprValue = value; + IsCompilerGenerated = isCompilerGenerated; + } + + // Synthetic expressions don't have a flow state. + public NullableFlowState FlowState => NullableFlowState.None; + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/ExpressionNodeInfo.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/ExpressionNodeInfo.cs new file mode 100644 index 00000000000..6968ca65abb --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/ExpressionNodeInfo.cs @@ -0,0 +1,189 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Semmle.Extraction.CSharp.Populators; +using Semmle.Extraction.Entities; +using Semmle.Extraction.Kinds; + +namespace Semmle.Extraction.CSharp.Entities +{ + /// + /// Expression information constructed from a syntax node. + /// + internal class ExpressionNodeInfo : IExpressionInfo + { + public ExpressionNodeInfo(Context cx, ExpressionSyntax node, IExpressionParentEntity parent, int child) : + this(cx, node, parent, child, cx.GetTypeInfo(node)) + { + } + + public ExpressionNodeInfo(Context cx, ExpressionSyntax node, IExpressionParentEntity parent, int child, TypeInfo typeInfo) + { + Context = cx; + Node = node; + Parent = parent; + Child = child; + TypeInfo = typeInfo; + Conversion = cx.GetModel(node).GetConversion(node); + } + + public Context Context { get; } + public ExpressionSyntax Node { get; private set; } + public IExpressionParentEntity Parent { get; set; } + public int Child { get; set; } + public TypeInfo TypeInfo { get; } + public Microsoft.CodeAnalysis.CSharp.Conversion Conversion { get; } + + public AnnotatedTypeSymbol ResolvedType => new AnnotatedTypeSymbol(TypeInfo.Type.DisambiguateType(), TypeInfo.Nullability.Annotation); + public AnnotatedTypeSymbol ConvertedType => new AnnotatedTypeSymbol(TypeInfo.ConvertedType.DisambiguateType(), TypeInfo.ConvertedNullability.Annotation); + + private AnnotatedTypeSymbol? cachedType; + private bool cachedTypeSet; + public AnnotatedTypeSymbol? Type + { + get + { + if (cachedTypeSet) + return cachedType; + + var type = ResolvedType; + + if (type.Symbol == null) + type.Symbol = (TypeInfo.Type ?? TypeInfo.ConvertedType).DisambiguateType(); + + // Roslyn workaround: It can't work out the type of "new object[0]" + // Clearly a bug. + if (type.Symbol?.TypeKind == Microsoft.CodeAnalysis.TypeKind.Error) + { + if (Node is ArrayCreationExpressionSyntax arrayCreation) + { + var elementType = Context.GetType(arrayCreation.Type.ElementType); + + if (elementType.Symbol != null) + // There seems to be no way to create an array with a nullable element at present. + return new AnnotatedTypeSymbol(Context.Compilation.CreateArrayTypeSymbol(elementType.Symbol, arrayCreation.Type.RankSpecifiers.Count), NullableAnnotation.NotAnnotated); + } + + Context.ModelError(Node, "Failed to determine type"); + } + + cachedType = type; + cachedTypeSet = true; + + return type; + } + } + + private Microsoft.CodeAnalysis.Location location; + + public Microsoft.CodeAnalysis.Location CodeAnalysisLocation + { + get + { + if (location == null) + location = Node.FixedLocation(); + return location; + } + set + { + location = value; + } + } + + public SemanticModel Model => Context.GetModel(Node); + + public string ExprValue + { + get + { + var c = Model.GetConstantValue(Node); + if (c.HasValue) + { + return Expression.ValueAsString(c.Value); + } + + if (TryGetBoolValueFromLiteral(out var val)) + { + return Expression.ValueAsString(val); + } + + return null; + } + } + + private Extraction.Entities.Location cachedLocation; + + public Extraction.Entities.Location Location + { + get + { + if (cachedLocation == null) + cachedLocation = Context.CreateLocation(CodeAnalysisLocation); + return cachedLocation; + } + + set + { + cachedLocation = value; + } + } + + public ExprKind Kind { get; set; } = ExprKind.UNKNOWN; + + public bool IsCompilerGenerated { get; set; } + + public ExpressionNodeInfo SetParent(IExpressionParentEntity parent, int child) + { + Parent = parent; + Child = child; + return this; + } + + public ExpressionNodeInfo SetKind(ExprKind kind) + { + Kind = kind; + return this; + } + + public ExpressionNodeInfo SetType(AnnotatedTypeSymbol? type) + { + cachedType = type; + cachedTypeSet = true; + return this; + } + + public ExpressionNodeInfo SetNode(ExpressionSyntax node) + { + Node = node; + return this; + } + + private SymbolInfo cachedSymbolInfo; + + public SymbolInfo SymbolInfo + { + get + { + if (cachedSymbolInfo.Symbol == null && cachedSymbolInfo.CandidateReason == CandidateReason.None) + cachedSymbolInfo = Model.GetSymbolInfo(Node); + return cachedSymbolInfo; + } + } + + public NullableFlowState FlowState => TypeInfo.Nullability.FlowState; + + private bool TryGetBoolValueFromLiteral(out bool val) + { + var isTrue = Node.IsKind(SyntaxKind.TrueLiteralExpression); + var isFalse = Node.IsKind(SyntaxKind.FalseLiteralExpression); + + val = isTrue; + return isTrue || isFalse; + } + + public bool IsBoolLiteral() + { + return TryGetBoolValueFromLiteral(out var _); + } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression`1.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression`1.cs new file mode 100644 index 00000000000..6c027076472 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression`1.cs @@ -0,0 +1,34 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using System.IO; + +namespace Semmle.Extraction.CSharp.Entities +{ + internal abstract class Expression : Expression + where TExpressionSyntax : ExpressionSyntax + { + public TExpressionSyntax Syntax { get; } + + protected Expression(ExpressionNodeInfo info) + : base(info) + { + Syntax = (TExpressionSyntax)info.Node; + } + + /// + /// Populates expression-type specific relations in the trap file. The general relations + /// expressions and expr_location are populated by the constructor + /// (should not fail), so even if expression-type specific population fails (e.g., in + /// standalone extraction), the expression created via + /// will + /// still be valid. + /// + protected abstract void PopulateExpression(TextWriter trapFile); + + protected new Expression TryPopulate() + { + Context.Try(Syntax, null, () => PopulateExpression(Context.TrapWriter.Writer)); + return this; + } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Access.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Access.cs index d4d4c86c507..8ef72f8085c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Access.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Access.cs @@ -47,12 +47,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { if (!(target is null)) { - cx.TrapWriter.Writer.expr_access(this, target); + Context.TrapWriter.Writer.expr_access(this, target); } if (implicitThis && !symbol.IsStatic) { - This.CreateImplicit(cx, symbol.ContainingType, Location, this, -1); + This.CreateImplicit(Context, symbol.ContainingType, Location, this, -1); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs index ea818862d80..3d6d3b95722 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs @@ -31,7 +31,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (TypeSyntax is null) { - cx.ModelError(Syntax, "Array has unexpected type syntax"); + Context.ModelError(Syntax, "Array has unexpected type syntax"); } var firstLevelSizes = TypeSyntax.RankSpecifiers.First()?.Sizes ?? SyntaxFactory.SeparatedList(); @@ -44,20 +44,20 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { for (var sizeIndex = 0; sizeIndex < firstLevelSizes.Count; sizeIndex++) { - Create(cx, firstLevelSizes[sizeIndex], this, sizeIndex); + Create(Context, firstLevelSizes[sizeIndex], this, sizeIndex); } explicitlySized = true; } if (!(Initializer is null)) { - ArrayInitializer.Create(new ExpressionNodeInfo(cx, Initializer, this, InitializerIndex)); + ArrayInitializer.Create(new ExpressionNodeInfo(Context, Initializer, this, InitializerIndex)); } if (explicitlySized) trapFile.explicitly_sized_array_creation(this); - TypeMention.Create(cx, TypeSyntax, this, Type); + TypeMention.Create(Context, TypeSyntax, this, Type); } private void SetArraySizes(InitializerExpressionSyntax initializer, int rank) @@ -69,7 +69,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions return; } - Literal.CreateGenerated(cx, this, level, cx.Compilation.GetSpecialType(SpecialType.System_Int32), initializer.Expressions.Count, Location); + Literal.CreateGenerated(Context, this, level, Context.Compilation.GetSpecialType(SpecialType.System_Int32), initializer.Expressions.Count, Location); initializer = initializer.Expressions.FirstOrDefault() as InitializerExpressionSyntax; } @@ -143,7 +143,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - ArrayInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, InitializerIndex)); + ArrayInitializer.Create(new ExpressionNodeInfo(Context, Syntax.Initializer, this, InitializerIndex)); trapFile.implicitly_typed_array_creation(this); trapFile.stackalloc_array_creation(this); } @@ -159,7 +159,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { if (Syntax.Initializer != null) { - ArrayInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, InitializerIndex)); + ArrayInitializer.Create(new ExpressionNodeInfo(Context, Syntax.Initializer, this, InitializerIndex)); } trapFile.implicitly_typed_array_creation(this); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Assignment.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Assignment.cs index f785ce65da3..3325c8075a3 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Assignment.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Assignment.cs @@ -26,17 +26,17 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (operatorKind.HasValue) { // Convert assignment such as `a += b` into `a = a + b`. - var simpleAssignExpr = new Expression(new ExpressionInfo(cx, Type, Location, ExprKind.SIMPLE_ASSIGN, this, 2, false, null)); - Create(cx, Syntax.Left, simpleAssignExpr, 1); - var opexpr = new Expression(new ExpressionInfo(cx, Type, Location, operatorKind.Value, simpleAssignExpr, 0, false, null)); - Create(cx, Syntax.Left, opexpr, 0); - Create(cx, Syntax.Right, opexpr, 1); + var simpleAssignExpr = new Expression(new ExpressionInfo(Context, Type, Location, ExprKind.SIMPLE_ASSIGN, this, 2, false, null)); + Create(Context, Syntax.Left, simpleAssignExpr, 1); + var opexpr = new Expression(new ExpressionInfo(Context, Type, Location, operatorKind.Value, simpleAssignExpr, 0, false, null)); + Create(Context, Syntax.Left, opexpr, 0); + Create(Context, Syntax.Right, opexpr, 1); opexpr.OperatorCall(trapFile, Syntax); } else { - Create(cx, Syntax.Left, this, 1); - Create(cx, Syntax.Right, this, 0); + Create(Context, Syntax.Left, this, 1); + Create(Context, Syntax.Right, this, 0); if (Kind == ExprKind.ADD_EVENT || Kind == ExprKind.REMOVE_EVENT) { @@ -148,12 +148,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions case ExprKind.ASSIGN_COALESCE: return ExprKind.NULL_COALESCING; default: - cx.ModelError(Syntax, "Couldn't unfold assignment of type " + kind); + Context.ModelError(Syntax, "Couldn't unfold assignment of type " + kind); return ExprKind.UNKNOWN; } } } - public new CallType CallType => GetCallType(cx, Syntax); + public new CallType CallType => GetCallType(Context, Syntax); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Await.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Await.cs index 2f8726c8070..34088d9564a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Await.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Await.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Binary.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Binary.cs index 32ee3be5f73..74dcc9ff85f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Binary.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Binary.cs @@ -17,8 +17,8 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { OperatorCall(trapFile, Syntax); - CreateDeferred(cx, Syntax.Left, this, 0); - CreateDeferred(cx, Syntax.Right, this, 1); + CreateDeferred(Context, Syntax.Left, this, 0); + CreateDeferred(Context, Syntax.Right, this, 1); } private static ExprKind GetKind(Context cx, BinaryExpressionSyntax node) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Cast.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Cast.cs index 6f2551ec904..2afe7a9b37e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Cast.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Cast.cs @@ -16,17 +16,17 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, ExpressionIndex); + Create(Context, Syntax.Expression, this, ExpressionIndex); if (Kind == ExprKind.CAST) { // Type cast - TypeAccess.Create(new ExpressionNodeInfo(cx, Syntax.Type, this, TypeAccessIndex)); + TypeAccess.Create(new ExpressionNodeInfo(Context, Syntax.Type, this, TypeAccessIndex)); } else { // Type conversion OperatorCall(trapFile, Syntax); - TypeMention.Create(cx, Syntax.Type, this, Type); + TypeMention.Create(Context, Syntax.Type, this, Type); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Checked.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Checked.cs index e6958c4511c..4be8b378937 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Checked.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Checked.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Conditional.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Conditional.cs index bfa4a836feb..193c88b112a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Conditional.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Conditional.cs @@ -12,9 +12,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Condition, this, 0); - Create(cx, Syntax.WhenTrue, this, 1); - Create(cx, Syntax.WhenFalse, this, 2); + Create(Context, Syntax.Condition, this, 0); + Create(Context, Syntax.WhenTrue, this, 1); + Create(Context, Syntax.WhenFalse, this, 2); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Default.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Default.cs index 326516bba1b..9e1b42834b1 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Default.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Default.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - TypeAccess.Create(cx, Syntax.Type, this, 0); + TypeAccess.Create(Context, Syntax.Type, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Discard.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Discard.cs index ef35f901b92..4f2eecca9da 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Discard.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Discard.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions } private Discard(Context cx, CSharpSyntaxNode syntax, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, cx.GetType(syntax), cx.Create(syntax.GetLocation()), ExprKind.DISCARD, parent, child, false, null)) + base(new ExpressionInfo(cx, cx.GetType(syntax), cx.CreateLocation(syntax.GetLocation()), ExprKind.DISCARD, parent, child, false, null)) { } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs index 59e573984fe..e860731ece3 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs @@ -22,22 +22,22 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { if (Kind == ExprKind.POINTER_INDIRECTION) { - var qualifierInfo = new ExpressionNodeInfo(cx, qualifier, this, 0); - var add = new Expression(new ExpressionInfo(cx, qualifierInfo.Type, Location, ExprKind.ADD, this, 0, false, null)); + var qualifierInfo = new ExpressionNodeInfo(Context, qualifier, this, 0); + var add = new Expression(new ExpressionInfo(Context, qualifierInfo.Type, Location, ExprKind.ADD, this, 0, false, null)); qualifierInfo.SetParent(add, 0); CreateFromNode(qualifierInfo); PopulateArguments(trapFile, argumentList, 1); } else { - Create(cx, qualifier, this, -1); + Create(Context, qualifier, this, -1); PopulateArguments(trapFile, argumentList, 0); - var symbolInfo = cx.GetSymbolInfo(base.Syntax); + var symbolInfo = Context.GetSymbolInfo(base.Syntax); if (symbolInfo.Symbol is IPropertySymbol indexer) { - trapFile.expr_access(this, Indexer.Create(cx, indexer)); + trapFile.expr_access(this, Indexer.Create(Context, indexer)); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs index a21379d897c..48cd73944f3 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs @@ -250,6 +250,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions case SyntaxKind.SuppressNullableWarningExpression: return PostfixUnary.Create(info.SetKind(ExprKind.SUPPRESS_NULLABLE_WARNING), ((PostfixUnaryExpressionSyntax)info.Node).Operand); + case SyntaxKind.WithExpression: + return WithExpression.Create(info); + default: info.Context.ModelError(info.Node, $"Unhandled expression '{info.Node}' of kind '{info.Node.Kind()}'"); return new Unknown(info); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs index eccead4a4de..96d8dec039a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions public ImplicitCast(ExpressionNodeInfo info) : base(new ExpressionInfo(info.Context, info.ConvertedType, info.Location, ExprKind.CAST, info.Parent, info.Child, true, info.ExprValue)) { - Expr = Factory.Create(new ExpressionNodeInfo(cx, info.Node, this, 0)); + Expr = Factory.Create(new ExpressionNodeInfo(Context, info.Node, this, 0)); } public ImplicitCast(ExpressionNodeInfo info, IMethodSymbol method) @@ -22,11 +22,11 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { Expr = Factory.Create(info.SetParent(this, 0)); - var target = Method.Create(cx, method); + var target = Method.Create(Context, method); if (target != null) - cx.TrapWriter.Writer.expr_call(this, target); + Context.TrapWriter.Writer.expr_call(this, target); else - cx.ModelError(info.Node, "Failed to resolve target for operator invocation"); + Context.ModelError(info.Node, "Failed to resolve target for operator invocation"); } /// diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Initializer.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Initializer.cs index d96a44527d8..6f53f1f144e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Initializer.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Initializer.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected Initializer(ExpressionNodeInfo info) : base(info) { } } - internal class ArrayInitializer : Expression + internal class ArrayInitializer : Initializer { private ArrayInitializer(ExpressionNodeInfo info) : base(info.SetType(null).SetKind(ExprKind.ARRAY_INIT)) { } @@ -26,12 +26,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (e.Kind() == SyntaxKind.ArrayInitializerExpression) { // Recursively create another array initializer - Create(new ExpressionNodeInfo(cx, (InitializerExpressionSyntax)e, this, child++)); + Create(new ExpressionNodeInfo(Context, (InitializerExpressionSyntax)e, this, child++)); } else { // Create the expression normally. - Create(cx, e, this, child++); + Create(Context, e, this, child++); } } } @@ -61,7 +61,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - ArrayInitializer.Create(new ExpressionNodeInfo(cx, Syntax, this, -1)); + ArrayInitializer.Create(new ExpressionNodeInfo(Context, Syntax, this, -1)); trapFile.implicitly_typed_array_creation(this); } } @@ -81,9 +81,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { if (init is AssignmentExpressionSyntax assignment) { - var assignmentInfo = new ExpressionNodeInfo(cx, init, this, child++).SetKind(ExprKind.SIMPLE_ASSIGN); + var assignmentInfo = new ExpressionNodeInfo(Context, init, this, child++).SetKind(ExprKind.SIMPLE_ASSIGN); var assignmentEntity = new Expression(assignmentInfo); - var typeInfoRight = cx.GetTypeInfo(assignment.Right); + var typeInfoRight = Context.GetTypeInfo(assignment.Right); if (typeInfoRight.Type is null) // The type may be null for nested initializers such as // ```csharp @@ -92,15 +92,15 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions // In this case we take the type from the assignment // `As = { [0] = a }` instead typeInfoRight = assignmentInfo.TypeInfo; - CreateFromNode(new ExpressionNodeInfo(cx, assignment.Right, assignmentEntity, 0, typeInfoRight)); + CreateFromNode(new ExpressionNodeInfo(Context, assignment.Right, assignmentEntity, 0, typeInfoRight)); - var target = cx.GetSymbolInfo(assignment.Left); + var target = Context.GetSymbolInfo(assignment.Left); // If the target is null, then assume that this is an array initializer (of the form `[...] = ...`) var access = target.Symbol is null ? - new Expression(new ExpressionNodeInfo(cx, assignment.Left, assignmentEntity, 1).SetKind(ExprKind.ARRAY_ACCESS)) : - Access.Create(new ExpressionNodeInfo(cx, assignment.Left, assignmentEntity, 1), target.Symbol, false, cx.CreateEntity(target.Symbol)); + new Expression(new ExpressionNodeInfo(Context, assignment.Left, assignmentEntity, 1).SetKind(ExprKind.ARRAY_ACCESS)) : + Access.Create(new ExpressionNodeInfo(Context, assignment.Left, assignmentEntity, 1), target.Symbol, false, Context.CreateEntity(target.Symbol)); if (assignment.Left is ImplicitElementAccessSyntax iea) { @@ -109,14 +109,14 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions var indexChild = 0; foreach (var arg in iea.ArgumentList.Arguments) { - Expression.Create(cx, arg.Expression, access, indexChild++); + Expression.Create(Context, arg.Expression, access, indexChild++); } } } else { - cx.ModelError(init, "Unexpected object initialization"); - Create(cx, init, this, child++); + Context.ModelError(init, "Unexpected object initialization"); + Create(Context, init, this, child++); } } } @@ -133,16 +133,16 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions var child = 0; foreach (var i in Syntax.Expressions) { - var collectionInfo = cx.GetModel(Syntax).GetCollectionInitializerSymbolInfo(i); - var addMethod = Method.Create(cx, collectionInfo.Symbol as IMethodSymbol); - var voidType = AnnotatedTypeSymbol.CreateNotAnnotated(cx.Compilation.GetSpecialType(SpecialType.System_Void)); + var collectionInfo = Context.GetModel(Syntax).GetCollectionInitializerSymbolInfo(i); + var addMethod = Method.Create(Context, collectionInfo.Symbol as IMethodSymbol); + var voidType = AnnotatedTypeSymbol.CreateNotAnnotated(Context.Compilation.GetSpecialType(SpecialType.System_Void)); - var invocation = new Expression(new ExpressionInfo(cx, voidType, cx.Create(i.GetLocation()), ExprKind.METHOD_INVOCATION, this, child++, false, null)); + var invocation = new Expression(new ExpressionInfo(Context, voidType, Context.CreateLocation(i.GetLocation()), ExprKind.METHOD_INVOCATION, this, child++, false, null)); if (addMethod != null) trapFile.expr_call(invocation, addMethod); else - cx.ModelError(Syntax, "Unable to find an Add() method for collection initializer"); + Context.ModelError(Syntax, "Unable to find an Add() method for collection initializer"); if (i.Kind() == SyntaxKind.ComplexElementInitializerExpression) { @@ -154,12 +154,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions var addChild = 0; foreach (var arg in init.Expressions) { - Create(cx, arg, invocation, addChild++); + Create(Context, arg, invocation, addChild++); } } else { - Create(cx, i, invocation, 0); + Create(Context, i, invocation, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/InterpolatedString.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/InterpolatedString.cs index 152ff145897..ee69580f83a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/InterpolatedString.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/InterpolatedString.cs @@ -22,12 +22,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { case SyntaxKind.Interpolation: var interpolation = (InterpolationSyntax)c; - Create(cx, interpolation.Expression, this, child++); + Create(Context, interpolation.Expression, this, child++); break; case SyntaxKind.InterpolatedStringText: // Create a string literal var interpolatedText = (InterpolatedStringTextSyntax)c; - new Expression(new ExpressionInfo(cx, Type, cx.Create(c.GetLocation()), ExprKind.STRING_LITERAL, this, child++, false, interpolatedText.TextToken.Text)); + new Expression(new ExpressionInfo(Context, Type, Context.CreateLocation(c.GetLocation()), ExprKind.STRING_LITERAL, this, child++, false, interpolatedText.TextToken.Text)); break; default: throw new InternalError(c, $"Unhandled interpolation kind {c.Kind()}"); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs index 638822f3d27..408556dff63 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs @@ -37,15 +37,15 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions memberName = memberAccess.Name.Identifier.Text; if (Syntax.Expression.Kind() == SyntaxKind.SimpleMemberAccessExpression) // Qualified method call; `x.M()` - Create(cx, memberAccess.Expression, this, child++); + Create(Context, memberAccess.Expression, this, child++); else // Pointer member access; `x->M()` - Create(cx, Syntax.Expression, this, child++); + Create(Context, Syntax.Expression, this, child++); break; case MemberBindingExpressionSyntax memberBinding: // Conditionally qualified method call; `x?.M()` memberName = memberBinding.Name.Identifier.Text; - Create(cx, FindConditionalQualifier(memberBinding), this, child++); + Create(Context, FindConditionalQualifier(memberBinding), this, child++); MakeConditional(trapFile); break; case SimpleNameSyntax simpleName when (Kind == ExprKind.METHOD_INVOCATION): @@ -55,10 +55,10 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { // Implicit `this` qualifier; add explicitly - if (cx.GetModel(Syntax).GetEnclosingSymbol(Location.symbol.SourceSpan.Start) is IMethodSymbol callingMethod) - This.CreateImplicit(cx, callingMethod.ContainingType, Location, this, child++); + if (Context.GetModel(Syntax).GetEnclosingSymbol(Location.Symbol.SourceSpan.Start) is IMethodSymbol callingMethod) + This.CreateImplicit(Context, callingMethod.ContainingType, Location, this, child++); else - cx.ModelError(Syntax, "Couldn't determine implicit this type"); + Context.ModelError(Syntax, "Couldn't determine implicit this type"); } else { @@ -68,7 +68,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions break; default: // Delegate or function pointer call; `d()` - Create(cx, Syntax.Expression, this, child++); + Create(Context, Syntax.Expression, this, child++); break; } @@ -78,7 +78,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (memberName != null) trapFile.dynamic_member_name(this, memberName); else - cx.ModelError(Syntax, "Unable to get name for dynamic call."); + Context.ModelError(Syntax, "Unable to get name for dynamic call."); } PopulateArguments(trapFile, Syntax.ArgumentList, child); @@ -86,11 +86,11 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (target == null) { if (!isDynamicCall && !IsDelegateLikeCall(info)) - cx.ModelError(Syntax, "Unable to resolve target for call. (Compilation error?)"); + Context.ModelError(Syntax, "Unable to resolve target for call. (Compilation error?)"); return; } - var targetKey = Method.Create(cx, target); + var targetKey = Method.Create(Context, target); trapFile.expr_call(this, targetKey); } @@ -125,7 +125,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions .Where(method => method.Parameters.Length >= Syntax.ArgumentList.Arguments.Count) .Where(method => method.Parameters.Count(p => !p.HasExplicitDefaultValue) <= Syntax.ArgumentList.Arguments.Count); - return cx.Extractor.Standalone ? + return Context.Extractor.Standalone ? candidates.FirstOrDefault() : candidates.SingleOrDefault(); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/IsPattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/IsPattern.cs index 741d108ba01..1c335474a68 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/IsPattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/IsPattern.cs @@ -12,8 +12,8 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); - Expressions.Pattern.Create(cx, Syntax.Pattern, this, 1); + Create(Context, Syntax.Expression, this, 0); + Expressions.Pattern.Create(Context, Syntax.Pattern, this, 1); } public static Expression Create(ExpressionNodeInfo info) => new IsPattern(info).TryPopulate(); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs index 40f200ef5ef..2cd23101b1c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs @@ -17,34 +17,34 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions private void VisitParameter(ParameterSyntax p) { - var symbol = cx.GetModel(p).GetDeclaredSymbol(p); - Parameter.Create(cx, symbol, this); + var symbol = Context.GetModel(p).GetDeclaredSymbol(p); + Parameter.Create(Context, symbol, this); } private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable @params) : base(info) { - if (cx.GetModel(info.Node).GetSymbolInfo(info.Node).Symbol is IMethodSymbol symbol) + if (Context.GetModel(info.Node).GetSymbolInfo(info.Node).Symbol is IMethodSymbol symbol) { - Modifier.ExtractModifiers(cx, info.Context.TrapWriter.Writer, this, symbol); + Modifier.ExtractModifiers(Context, info.Context.TrapWriter.Writer, this, symbol); } else { - cx.ModelError(info.Node, "Unknown declared symbol"); + Context.ModelError(info.Node, "Unknown declared symbol"); } // No need to use `Populate` as the population happens later - cx.PopulateLater(() => + Context.PopulateLater(() => { foreach (var param in @params) VisitParameter(param); if (body is ExpressionSyntax exprBody) - Create(cx, exprBody, this, 0); + Create(Context, exprBody, this, 0); else if (body is BlockSyntax blockBody) - Statements.Block.Create(cx, blockBody, this, 0); + Statements.Block.Create(Context, blockBody, this, 0); else - cx.ModelError(body, "Unhandled lambda body"); + Context.ModelError(body, "Unhandled lambda body"); }); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MakeRef.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MakeRef.cs index 751d6b6e92c..66f2fd8676a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MakeRef.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MakeRef.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MemberAccess.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MemberAccess.cs index 7d2777540f0..fa9d3c9a6dd 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MemberAccess.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/MemberAccess.cs @@ -9,16 +9,16 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions private MemberAccess(ExpressionNodeInfo info, ExpressionSyntax qualifier, ISymbol target) : base(info) { var trapFile = info.Context.TrapWriter.Writer; - Qualifier = Create(cx, qualifier, this, -1); + Qualifier = Create(Context, qualifier, this, -1); if (target == null) { if (info.Kind != ExprKind.DYNAMIC_MEMBER_ACCESS) - cx.ModelError(info.Node, "Could not determine target for member access"); + Context.ModelError(info.Node, "Could not determine target for member access"); } else { - var t = cx.CreateEntity(target); + var t = Context.CreateEntity(target); trapFile.expr_access(this, t); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/AnonymousObjectCreation.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/AnonymousObjectCreation.cs index 5703c3101a2..df291c4e9fd 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/AnonymousObjectCreation.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/AnonymousObjectCreation.cs @@ -17,32 +17,32 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - var target = cx.GetSymbolInfo(Syntax); + var target = Context.GetSymbolInfo(Syntax); var method = (IMethodSymbol)target.Symbol; if (method != null) { - trapFile.expr_call(this, Method.Create(cx, method)); + trapFile.expr_call(this, Method.Create(Context, method)); } var child = 0; var objectInitializer = Syntax.Initializers.Any() ? - new Expression(new ExpressionInfo(cx, Type, Location, ExprKind.OBJECT_INIT, this, -1, false, null)) : + new Expression(new ExpressionInfo(Context, Type, Location, ExprKind.OBJECT_INIT, this, -1, false, null)) : null; foreach (var init in Syntax.Initializers) { // Create an "assignment" - var property = cx.GetModel(init).GetDeclaredSymbol(init); - var propEntity = Property.Create(cx, property); + var property = Context.GetModel(init).GetDeclaredSymbol(init); + var propEntity = Property.Create(Context, property); var type = property.GetAnnotatedType(); - var loc = cx.Create(init.GetLocation()); + var loc = Context.CreateLocation(init.GetLocation()); - var assignment = new Expression(new ExpressionInfo(cx, type, loc, ExprKind.SIMPLE_ASSIGN, objectInitializer, child++, false, null)); - Create(cx, init.Expression, assignment, 0); - Property.Create(cx, property); + var assignment = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.SIMPLE_ASSIGN, objectInitializer, child++, false, null)); + Create(Context, init.Expression, assignment, 0); + Property.Create(Context, property); - var access = new Expression(new ExpressionInfo(cx, type, loc, ExprKind.PROPERTY_ACCESS, assignment, 1, false, null)); + var access = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.PROPERTY_ACCESS, assignment, 1, false, null)); trapFile.expr_access(access, propEntity); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/BaseObjectCreation.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/BaseObjectCreation.cs index 10f4df8bf7f..6f2c38b63db 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/BaseObjectCreation.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/BaseObjectCreation.cs @@ -22,22 +22,22 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions PopulateArguments(trapFile, Syntax.ArgumentList, 0); } - var target = cx.GetModel(Syntax).GetSymbolInfo(Syntax); + var target = Context.GetModel(Syntax).GetSymbolInfo(Syntax); if (target.Symbol is IMethodSymbol method) { - trapFile.expr_call(this, Method.Create(cx, method)); + trapFile.expr_call(this, Method.Create(Context, method)); } - if (IsDynamicObjectCreation(cx, Syntax)) + if (IsDynamicObjectCreation(Context, Syntax)) { - if (cx.GetModel(Syntax).GetTypeInfo(Syntax).Type is INamedTypeSymbol type && + if (Context.GetModel(Syntax).GetTypeInfo(Syntax).Type is INamedTypeSymbol type && !string.IsNullOrEmpty(type.Name)) { trapFile.dynamic_member_name(this, type.Name); } else { - cx.ModelError(Syntax, "Unable to get name for dynamic object creation."); + Context.ModelError(Syntax, "Unable to get name for dynamic object creation."); } } @@ -46,13 +46,13 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions switch (Syntax.Initializer.Kind()) { case SyntaxKind.CollectionInitializerExpression: - CollectionInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, -1).SetType(Type)); + CollectionInitializer.Create(new ExpressionNodeInfo(Context, Syntax.Initializer, this, -1).SetType(Type)); break; case SyntaxKind.ObjectInitializerExpression: - ObjectInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, -1).SetType(Type)); + ObjectInitializer.Create(new ExpressionNodeInfo(Context, Syntax.Initializer, this, -1).SetType(Type)); break; default: - cx.ModelError("Unhandled initializer in object creation"); + Context.ModelError("Unhandled initializer in object creation"); break; } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/ExplicitObjectCreation.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/ExplicitObjectCreation.cs index 51b2db20a9e..f51ab0cb810 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/ExplicitObjectCreation.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/ExplicitObjectCreation.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { base.PopulateExpression(trapFile); - TypeMention.Create(cx, Syntax.Type, this, Type); + TypeMention.Create(Context, Syntax.Type, this, Type); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/BinaryPattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/BinaryPattern.cs index 33ea47d057b..2d0292ae720 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/BinaryPattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/BinaryPattern.cs @@ -9,7 +9,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions internal class BinaryPattern : Expression { public BinaryPattern(Context cx, BinaryPatternSyntax syntax, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, null, cx.Create(syntax.GetLocation()), GetKind(syntax.OperatorToken, syntax), parent, child, false, null)) + base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), GetKind(syntax.OperatorToken, syntax), parent, child, false, null)) { Pattern.Create(cx, syntax.Left, this, 0); Pattern.Create(cx, syntax.Right, this, 1); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/Pattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/Pattern.cs index 65912ba12f1..05f3aab2526 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/Pattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/Pattern.cs @@ -34,7 +34,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol) { var type = symbol.GetAnnotatedType(); - return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.Create(syntax.GetLocation()), false, parent, child); + return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child); } if (designation is DiscardDesignationSyntax) { @@ -61,7 +61,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { var type = symbol.GetAnnotatedType(); - return VariableDeclaration.Create(cx, symbol, type, null, cx.Create(syntax.GetLocation()), true, parent, child); + return VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), true, parent, child); } throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation."); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PositionalPattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PositionalPattern.cs index 0f3c2bf8800..f0ae03ae9dc 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PositionalPattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PositionalPattern.cs @@ -7,7 +7,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions internal class PositionalPattern : Expression { internal PositionalPattern(Context cx, PositionalPatternClauseSyntax posPc, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, null, cx.Create(posPc.GetLocation()), ExprKind.POSITIONAL_PATTERN, parent, child, false, null)) + base(new ExpressionInfo(cx, null, cx.CreateLocation(posPc.GetLocation()), ExprKind.POSITIONAL_PATTERN, parent, child, false, null)) { child = 0; foreach (var sub in posPc.Subpatterns) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PropertyPattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PropertyPattern.cs index ed947990045..7bd13e7a8a3 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PropertyPattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/PropertyPattern.cs @@ -7,7 +7,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions internal class PropertyPattern : Expression { internal PropertyPattern(Context cx, PropertyPatternClauseSyntax pp, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, null, cx.Create(pp.GetLocation()), ExprKind.PROPERTY_PATTERN, parent, child, false, null)) + base(new ExpressionInfo(cx, null, cx.CreateLocation(pp.GetLocation()), ExprKind.PROPERTY_PATTERN, parent, child, false, null)) { child = 0; var trapFile = cx.TrapWriter.Writer; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RecursivePattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RecursivePattern.cs index 02246311648..d29292b3073 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RecursivePattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RecursivePattern.cs @@ -16,7 +16,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions /// The parent pattern/expression. /// The child index of this pattern. public RecursivePattern(Context cx, RecursivePatternSyntax syntax, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, null, cx.Create(syntax.GetLocation()), ExprKind.RECURSIVE_PATTERN, parent, child, false, null)) + base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.RECURSIVE_PATTERN, parent, child, false, null)) { // Extract the type access if (syntax.Type is TypeSyntax t) @@ -27,7 +27,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { var type = symbol.GetAnnotatedType(); - VariableDeclaration.Create(cx, symbol, type, null, cx.Create(syntax.GetLocation()), false, this, 0); + VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), false, this, 0); } if (syntax.PositionalPatternClause is PositionalPatternClauseSyntax posPc) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RelationalPattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RelationalPattern.cs index 77efd5668c4..c1a140005dc 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RelationalPattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RelationalPattern.cs @@ -9,7 +9,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions internal class RelationalPattern : Expression { public RelationalPattern(Context cx, RelationalPatternSyntax syntax, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, null, cx.Create(syntax.GetLocation()), GetKind(syntax.OperatorToken), parent, child, false, null)) + base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), GetKind(syntax.OperatorToken), parent, child, false, null)) { Expression.Create(cx, syntax.Expression, this, 0); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/UnaryPattern.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/UnaryPattern.cs index d80c390efff..ce2cf773a92 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/UnaryPattern.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/UnaryPattern.cs @@ -7,7 +7,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions internal class UnaryPattern : Expression { public UnaryPattern(Context cx, UnaryPatternSyntax syntax, IExpressionParentEntity parent, int child) : - base(new ExpressionInfo(cx, null, cx.Create(syntax.GetLocation()), ExprKind.NOT_PATTERN, parent, child, false, null)) + base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.NOT_PATTERN, parent, child, false, null)) { Pattern.Create(cx, syntax.Pattern, this, 0); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PointerMemberAccess.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PointerMemberAccess.cs index 01df338d1b3..a8ea0a30780 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PointerMemberAccess.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PointerMemberAccess.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); // !! We do not currently look at the member (or store the member name). } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PostfixUnary.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PostfixUnary.cs index 30c6058d95f..24d07441964 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PostfixUnary.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/PostfixUnary.cs @@ -20,7 +20,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, operand, this, 0); + Create(Context, operand, this, 0); OperatorCall(trapFile, Syntax); if ((operatorKind == ExprKind.POST_INCR || operatorKind == ExprKind.POST_DECR) && diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Query.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Query.cs index 1db6c39fef2..f919aaca568 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Query.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Query.cs @@ -23,7 +23,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { public QueryCall(Context cx, IMethodSymbol method, SyntaxNode clause, IExpressionParentEntity parent, int child) : base(new ExpressionInfo(cx, method?.GetAnnotatedReturnType(), - cx.Create(clause.GetLocation()), + cx.CreateLocation(clause.GetLocation()), ExprKind.METHOD_INVOCATION, parent, child, false, null)) { if (method != null) @@ -89,7 +89,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions variableSymbol, declType, declTypeSyntax, - cx.Create(node.GetLocation()), + cx.CreateLocation(node.GetLocation()), true, parent, child @@ -97,7 +97,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions Expression.Create(cx, Expr, decl, 0); - var nameLoc = cx.Create(name.GetLocation()); + var nameLoc = cx.CreateLocation(name.GetLocation()); var access = new Expression(new ExpressionInfo(cx, type, nameLoc, ExprKind.LOCAL_VARIABLE_ACCESS, decl, 1, false, null)); cx.TrapWriter.Writer.expr_access(access, LocalVariable.Create(cx, variableSymbol)); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RangeExpression.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RangeExpression.cs index db5cd138155..2011f4ae367 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RangeExpression.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RangeExpression.cs @@ -13,9 +13,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { if (!(Syntax.LeftOperand is null)) - Expression.Create(cx, Syntax.LeftOperand, this, 0); + Expression.Create(Context, Syntax.LeftOperand, this, 0); if (!(Syntax.RightOperand is null)) - Expression.Create(cx, Syntax.RightOperand, this, 1); + Expression.Create(Context, Syntax.RightOperand, this, 1); } public static Expression Create(ExpressionNodeInfo info) => new RangeExpression(info).TryPopulate(); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Ref.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Ref.cs index 5b38b9b8ee6..ef28f5d7e25 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Ref.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Ref.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefType.cs index 37f7e2a3f76..815a5928f51 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefType.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefValue.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefValue.cs index 2372cc4bb09..7d9187533ae 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefValue.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/RefValue.cs @@ -12,8 +12,8 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); - Create(cx, Syntax.Type, this, 1); // A type-access + Create(Context, Syntax.Expression, this, 0); + Create(Context, Syntax.Type, this, 1); // A type-access } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Sizeof.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Sizeof.cs index a4b89928e00..d55fe1781ce 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Sizeof.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Sizeof.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - TypeAccess.Create(cx, Syntax.Type, this, 0); + TypeAccess.Create(Context, Syntax.Type, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Switch.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Switch.cs index 5fdc08c66d3..3dbe496cd4c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Switch.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Switch.cs @@ -17,10 +17,10 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - SwitchedExpr = Expression.Create(cx, Syntax.GoverningExpression, this, -1); + SwitchedExpr = Expression.Create(Context, Syntax.GoverningExpression, this, -1); for (var i = 0; i < Syntax.Arms.Count; i++) { - new SwitchCase(cx, Syntax.Arms[i], this, i); + new SwitchCase(Context, Syntax.Arms[i], this, i); } } } @@ -29,7 +29,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions { internal SwitchCase(Context cx, SwitchExpressionArmSyntax arm, Switch parent, int child) : base(new ExpressionInfo( - cx, cx.GetType(arm.Expression), cx.Create(arm.GetLocation()), + cx, cx.GetType(arm.Expression), cx.CreateLocation(arm.GetLocation()), ExprKind.SWITCH_CASE, parent, child, false, null)) { Expressions.Pattern.Create(cx, arm.Pattern, this, 0); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Throw.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Throw.cs index 5ef8feaebb6..36d0b34bf95 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Throw.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Throw.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Tuple.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Tuple.cs index f2911bdcf75..726f8f540d7 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Tuple.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Tuple.cs @@ -18,7 +18,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions var child = 0; foreach (var argument in Syntax.Arguments.Select(a => a.Expression)) { - Expression.Create(cx, argument, this, child++); + Expression.Create(Context, argument, this, child++); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeAccess.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeAccess.cs index e5c0fd3ac66..54b9730e3d7 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeAccess.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeAccess.cs @@ -18,17 +18,17 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions if (Type?.Symbol.ContainingType is null) { // namespace qualifier - TypeMention.Create(cx, maes.Name, this, Type, Syntax.GetLocation()); + TypeMention.Create(Context, maes.Name, this, Type, Syntax.GetLocation()); } else { // type qualifier - TypeMention.Create(cx, maes.Name, this, Type); - Create(cx, maes.Expression, this, -1); + TypeMention.Create(Context, maes.Name, this, Type); + Create(Context, maes.Expression, this, -1); } return; default: - TypeMention.Create(cx, (TypeSyntax)Syntax, this, Type); + TypeMention.Create(Context, (TypeSyntax)Syntax, this, Type); return; } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeOf.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeOf.cs index 686e4156528..6e5dc3e05f1 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeOf.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/TypeOf.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - TypeAccess.Create(cx, Syntax.Type, this, TypeAccessIndex); + TypeAccess.Create(Context, Syntax.Type, this, TypeAccessIndex); } public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Microsoft.CodeAnalysis.ITypeSymbol type, Extraction.Entities.Location location) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unary.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unary.cs index f3c2c73b074..8cad73d6565 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unary.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unary.cs @@ -23,7 +23,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Operand, this, 0); + Create(Context, Syntax.Operand, this, 0); OperatorCall(trapFile, Syntax); if ((operatorKind == ExprKind.PRE_INCR || operatorKind == ExprKind.PRE_DECR) && diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unchecked.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unchecked.cs index 5ffcf414132..016c3fcefc8 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unchecked.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Unchecked.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions protected override void PopulateExpression(TextWriter trapFile) { - Create(cx, Syntax.Expression, this, 0); + Create(Context, Syntax.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/VariableDeclaration.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/VariableDeclaration.cs index 349236e893a..674f45b3f21 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/VariableDeclaration.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/VariableDeclaration.cs @@ -50,7 +50,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions public static Expression CreateParenthesized(Context cx, DeclarationExpressionSyntax node, ParenthesizedVariableDesignationSyntax designation, IExpressionParentEntity parent, int child) { AnnotatedTypeSymbol? type = null; // Should ideally be a corresponding tuple type - var tuple = new Expression(new ExpressionInfo(cx, type, cx.Create(node.GetLocation()), ExprKind.TUPLE, parent, child, false, null)); + var tuple = new Expression(new ExpressionInfo(cx, type, cx.CreateLocation(node.GetLocation()), ExprKind.TUPLE, parent, child, false, null)); cx.Try(null, null, () => { @@ -65,7 +65,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions public static Expression CreateParenthesized(Context cx, VarPatternSyntax varPattern, ParenthesizedVariableDesignationSyntax designation, IExpressionParentEntity parent, int child) { AnnotatedTypeSymbol? type = null; // Should ideally be a corresponding tuple type - var tuple = new Expression(new ExpressionInfo(cx, type, cx.Create(varPattern.GetLocation()), ExprKind.TUPLE, parent, child, false, null)); + var tuple = new Expression(new ExpressionInfo(cx, type, cx.CreateLocation(varPattern.GetLocation()), ExprKind.TUPLE, parent, child, false, null)); cx.Try(null, null, () => { @@ -123,7 +123,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions Create(cx, node, node.Designation, parent, child); public static VariableDeclaration Create(Context cx, CSharpSyntaxNode c, AnnotatedTypeSymbol? type, IExpressionParentEntity parent, int child) => - new VariableDeclaration(new ExpressionInfo(cx, type, cx.Create(c.FixedLocation()), ExprKind.LOCAL_VAR_DECL, parent, child, false, null)); + new VariableDeclaration(new ExpressionInfo(cx, type, cx.CreateLocation(c.FixedLocation()), ExprKind.LOCAL_VAR_DECL, parent, child, false, null)); public static VariableDeclaration Create(Context cx, CatchDeclarationSyntax d, bool isVar, IExpressionParentEntity parent, int child) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/WithExpression.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/WithExpression.cs new file mode 100644 index 00000000000..dc4e6ad12a0 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/WithExpression.cs @@ -0,0 +1,20 @@ +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Semmle.Extraction.Kinds; +using System.IO; + +namespace Semmle.Extraction.CSharp.Entities.Expressions +{ + internal class WithExpression : Expression + { + private WithExpression(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.WITH)) { } + + public static Expression Create(ExpressionNodeInfo info) => new WithExpression(info).TryPopulate(); + + protected override void PopulateExpression(TextWriter trapFile) + { + Create(Context, Syntax.Expression, this, 0); + + ObjectInitializer.Create(new ExpressionNodeInfo(Context, Syntax.Initializer, this, 1).SetType(Type)); + } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Field.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Field.cs index aadd9d2fbab..1c435ebd7d1 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Field.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Field.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities private Field(Context cx, IFieldSymbol init) : base(cx, init) { - type = new Lazy(() => Entities.Type.Create(cx, symbol.Type)); + type = new Lazy(() => Entities.Type.Create(cx, Symbol.Type)); } public static Field Create(Context cx, IFieldSymbol field) => FieldFactory.Instance.CreateEntityFromSymbol(cx, field); @@ -22,78 +22,78 @@ namespace Semmle.Extraction.CSharp.Entities // Do not populate backing fields. // Populate Tuple fields. public override bool NeedsPopulation => - (base.NeedsPopulation && !symbol.IsImplicitlyDeclared) || symbol.ContainingType.IsTupleType; + (base.NeedsPopulation && !Symbol.IsImplicitlyDeclared) || Symbol.ContainingType.IsTupleType; public override void Populate(TextWriter trapFile) { PopulateMetadataHandle(trapFile); PopulateAttributes(); ContainingType.PopulateGenerics(); - PopulateNullability(trapFile, symbol.GetAnnotatedType()); + PopulateNullability(trapFile, Symbol.GetAnnotatedType()); - var unboundFieldKey = Field.Create(Context, symbol.OriginalDefinition); - trapFile.fields(this, (symbol.IsConst ? 2 : 1), symbol.Name, ContainingType, Type.TypeRef, unboundFieldKey); + var unboundFieldKey = Field.Create(Context, Symbol.OriginalDefinition); + trapFile.fields(this, (Symbol.IsConst ? 2 : 1), Symbol.Name, ContainingType, Type.TypeRef, unboundFieldKey); PopulateModifiers(trapFile); - if (symbol.IsVolatile) + if (Symbol.IsVolatile) Modifier.HasModifier(Context, trapFile, this, "volatile"); - if (symbol.IsConst) + if (Symbol.IsConst) { Modifier.HasModifier(Context, trapFile, this, "const"); - if (symbol.HasConstantValue) + if (Symbol.HasConstantValue) { - trapFile.constant_value(this, Expression.ValueAsString(symbol.ConstantValue)); + trapFile.constant_value(this, Expression.ValueAsString(Symbol.ConstantValue)); } } foreach (var l in Locations) trapFile.field_location(this, l); - if (!IsSourceDeclaration || !symbol.FromSource()) + if (!IsSourceDeclaration || !Symbol.FromSource()) return; - Context.BindComments(this, Location.symbol); + Context.BindComments(this, Location.Symbol); var child = 0; - foreach (var initializer in symbol.DeclaringSyntaxReferences + foreach (var initializer in Symbol.DeclaringSyntaxReferences .Select(n => n.GetSyntax()) .OfType() .Where(n => n.Initializer != null)) { Context.PopulateLater(() => { - var loc = Context.Create(initializer.GetLocation()); + var loc = Context.CreateLocation(initializer.GetLocation()); var fieldAccess = AddInitializerAssignment(trapFile, initializer.Initializer.Value, loc, null, ref child); - if (!symbol.IsStatic) + if (!Symbol.IsStatic) { - This.CreateImplicit(Context, symbol.ContainingType, Location, fieldAccess, -1); + This.CreateImplicit(Context, Symbol.ContainingType, Location, fieldAccess, -1); } }); } - foreach (var initializer in symbol.DeclaringSyntaxReferences + foreach (var initializer in Symbol.DeclaringSyntaxReferences .Select(n => n.GetSyntax()) .OfType() .Where(n => n.EqualsValue != null)) { // Mark fields that have explicit initializers. - var constValue = symbol.HasConstantValue - ? Expression.ValueAsString(symbol.ConstantValue) + var constValue = Symbol.HasConstantValue + ? Expression.ValueAsString(Symbol.ConstantValue) : null; - var loc = Context.Create(initializer.GetLocation()); + var loc = Context.CreateLocation(initializer.GetLocation()); AddInitializerAssignment(trapFile, initializer.EqualsValue.Value, loc, constValue, ref child); } if (IsSourceDeclaration) { - foreach (var syntax in symbol.DeclaringSyntaxReferences + foreach (var syntax in Symbol.DeclaringSyntaxReferences .Select(d => d.GetSyntax()) .OfType() .Select(d => d.Parent) @@ -107,7 +107,7 @@ namespace Semmle.Extraction.CSharp.Entities private Expression AddInitializerAssignment(TextWriter trapFile, ExpressionSyntax initializer, Extraction.Entities.Location loc, string constValue, ref int child) { - var type = symbol.GetAnnotatedType(); + var type = Symbol.GetAnnotatedType(); var simpleAssignExpr = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.SIMPLE_ASSIGN, this, child++, false, constValue)); Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer, simpleAssignExpr, 0)); var access = new Expression(new ExpressionInfo(Context, type, Location, ExprKind.FIELD_ACCESS, simpleAssignExpr, 1, false, constValue)); @@ -124,7 +124,7 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.Write(" "); trapFile.WriteSubId(ContainingType); trapFile.Write('.'); - trapFile.Write(symbol.Name); + trapFile.Write(Symbol.Name); trapFile.Write(";field"); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/IExpressionInfo.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/IExpressionInfo.cs new file mode 100644 index 00000000000..1a43a6fbd02 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/IExpressionInfo.cs @@ -0,0 +1,53 @@ +using Microsoft.CodeAnalysis; +using Semmle.Extraction.Kinds; + +namespace Semmle.Extraction.CSharp.Entities +{ + /// + /// Holds all information required to create an Expression entity. + /// + internal interface IExpressionInfo + { + Context Context { get; } + + /// + /// The type of the expression. + /// + AnnotatedTypeSymbol? Type { get; } + + /// + /// The location of the expression. + /// + Extraction.Entities.Location Location { get; } + + /// + /// The kind of the expression. + /// + ExprKind Kind { get; } + + /// + /// The parent of the expression. + /// + IExpressionParentEntity Parent { get; } + + /// + /// The child index of the expression. + /// + int Child { get; } + + /// + /// Holds if this is an implicit expression. + /// + bool IsCompilerGenerated { get; } + + /// + /// Gets a string representation of the value. + /// null is encoded as the string "null". + /// If the expression does not have a value, then this + /// is null. + /// + string ExprValue { get; } + + NullableFlowState FlowState { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/IExpressionParentEntity.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/IExpressionParentEntity.cs new file mode 100644 index 00000000000..a8e4df3d6ca --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/IExpressionParentEntity.cs @@ -0,0 +1,10 @@ +namespace Semmle.Extraction.CSharp.Entities +{ + public interface IExpressionParentEntity : IEntity + { + /// + /// Whether this entity is the parent of a top-level expression. + /// + bool IsTopLevelParent { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/IStatementParentEntity.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/IStatementParentEntity.cs new file mode 100644 index 00000000000..40bc0f218c0 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/IStatementParentEntity.cs @@ -0,0 +1,10 @@ +namespace Semmle.Extraction.CSharp.Entities +{ + /// + /// Whether this entity is the parent of a top-level statement. + /// + public interface IStatementParentEntity : IEntity + { + bool IsTopLevelParent { get; } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs index 70b15a6fe16..04bc319c93e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs @@ -10,22 +10,22 @@ namespace Semmle.Extraction.CSharp.Entities protected Indexer(Context cx, IPropertySymbol init) : base(cx, init) { } - private Indexer OriginalDefinition => IsSourceDeclaration ? this : Create(Context, symbol.OriginalDefinition); + private Indexer OriginalDefinition => IsSourceDeclaration ? this : Create(Context, Symbol.OriginalDefinition); public override void Populate(TextWriter trapFile) { - PopulateNullability(trapFile, symbol.GetAnnotatedType()); + PopulateNullability(trapFile, Symbol.GetAnnotatedType()); - var type = Type.Create(Context, symbol.Type); - trapFile.indexers(this, symbol.GetName(useMetadataName: true), ContainingType, type.TypeRef, OriginalDefinition); + var type = Type.Create(Context, Symbol.Type); + trapFile.indexers(this, Symbol.GetName(useMetadataName: true), ContainingType, type.TypeRef, OriginalDefinition); foreach (var l in Locations) trapFile.indexer_location(this, l); - var getter = symbol.GetMethod; - var setter = symbol.SetMethod; + var getter = Symbol.GetMethod; + var setter = Symbol.SetMethod; if (getter is null && setter is null) - Context.ModelError(symbol, "No indexer accessor defined"); + Context.ModelError(Symbol, "No indexer accessor defined"); if (!(getter is null)) Method.Create(Context, getter); @@ -33,10 +33,10 @@ namespace Semmle.Extraction.CSharp.Entities if (!(setter is null)) Method.Create(Context, setter); - for (var i = 0; i < symbol.Parameters.Length; ++i) + for (var i = 0; i < Symbol.Parameters.Length; ++i) { - var original = Parameter.Create(Context, symbol.OriginalDefinition.Parameters[i], OriginalDefinition); - Parameter.Create(Context, symbol.Parameters[i], this, original); + var original = Parameter.Create(Context, Symbol.OriginalDefinition.Parameters[i], OriginalDefinition); + Parameter.Create(Context, Symbol.Parameters[i], this, original); } if (IsSourceDeclaration) @@ -46,7 +46,7 @@ namespace Semmle.Extraction.CSharp.Entities { // The expression may need to reference parameters in the getter. // So we need to arrange that the expression is populated after the getter. - Context.PopulateLater(() => Expression.CreateFromNode(new ExpressionNodeInfo(Context, expressionBody, this, 0).SetType(symbol.GetAnnotatedType()))); + Context.PopulateLater(() => Expression.CreateFromNode(new ExpressionNodeInfo(Context, expressionBody, this, 0).SetType(Symbol.GetAnnotatedType()))); } } @@ -54,11 +54,11 @@ namespace Semmle.Extraction.CSharp.Entities BindComments(); var declSyntaxReferences = IsSourceDeclaration - ? symbol.DeclaringSyntaxReferences. + ? Symbol.DeclaringSyntaxReferences. Select(d => d.GetSyntax()).OfType().ToArray() : Enumerable.Empty(); - foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType))) + foreach (var explicitInterface in Symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType))) { trapFile.explicitly_implements(this, explicitInterface.TypeRef); @@ -77,9 +77,9 @@ namespace Semmle.Extraction.CSharp.Entities { trapFile.WriteSubId(ContainingType); trapFile.Write('.'); - trapFile.Write(symbol.MetadataName); + trapFile.Write(Symbol.MetadataName); trapFile.Write('('); - trapFile.BuildList(",", symbol.Parameters, (p, tb0) => tb0.WriteSubId(Type.Create(Context, p.Type))); + trapFile.BuildList(",", Symbol.Parameters, (p, tb0) => tb0.WriteSubId(Type.Create(Context, p.Type))); trapFile.Write(");indexer"); } @@ -87,11 +87,11 @@ namespace Semmle.Extraction.CSharp.Entities { get { - return symbol.DeclaringSyntaxReferences + return Symbol.DeclaringSyntaxReferences .Select(r => r.GetSyntax()) .OfType() .Select(s => s.GetLocation()) - .Concat(symbol.Locations) + .Concat(Symbol.Locations) .First(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalFunction.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalFunction.cs index b376566e841..efc50f7f85f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalFunction.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalFunction.cs @@ -34,10 +34,10 @@ namespace Semmle.Extraction.CSharp.Entities PopulateMethod(trapFile); PopulateModifiers(trapFile); - var originalDefinition = IsSourceDeclaration ? this : Create(Context, symbol.OriginalDefinition); - var returnType = Type.Create(Context, symbol.ReturnType); - trapFile.local_functions(this, symbol.Name, returnType, originalDefinition); - ExtractRefReturn(trapFile, symbol, this); + var originalDefinition = IsSourceDeclaration ? this : Create(Context, Symbol.OriginalDefinition); + var returnType = Type.Create(Context, Symbol.ReturnType); + trapFile.local_functions(this, Symbol.Name, returnType, originalDefinition); + ExtractRefReturn(trapFile, Symbol, this); } public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NeedsLabel; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalVariable.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalVariable.cs index e988ec2c815..4f9a0c4f7f6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalVariable.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/LocalVariable.cs @@ -23,12 +23,12 @@ namespace Semmle.Extraction.CSharp.Entities public void PopulateManual(Expression parent, bool isVar) { var trapFile = Context.TrapWriter.Writer; - var (kind, type) = symbol is ILocalSymbol l + var (kind, type) = Symbol is ILocalSymbol l ? (l.IsRef ? 3 : l.IsConst ? 2 : 1, l.GetAnnotatedType()) : (1, parent.Type); - trapFile.localvars(this, kind, symbol.Name, isVar ? 1 : 0, Type.Create(Context, type).TypeRef, parent); + trapFile.localvars(this, kind, Symbol.Name, isVar ? 1 : 0, Type.Create(Context, type).TypeRef, parent); - if (symbol is ILocalSymbol local) + if (Symbol is ILocalSymbol local) { PopulateNullability(trapFile, local.GetAnnotatedType()); if (local.IsRef) @@ -47,7 +47,7 @@ namespace Semmle.Extraction.CSharp.Entities private void DefineConstantValue(TextWriter trapFile) { - if (symbol is ILocalSymbol local && local.HasConstantValue) + if (Symbol is ILocalSymbol local && local.HasConstantValue) { trapFile.constant_value(this, Expression.ValueAsString(local.ConstantValue)); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs index 6a52087e384..8a1572796bc 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Method.cs @@ -1,4 +1,5 @@ using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Semmle.Extraction.CSharp.Populators; using System.Collections.Generic; @@ -15,8 +16,8 @@ namespace Semmle.Extraction.CSharp.Entities protected void PopulateParameters() { var originalMethod = OriginalDefinition; - IEnumerable parameters = symbol.Parameters; - IEnumerable originalParameters = originalMethod.symbol.Parameters; + IEnumerable parameters = Symbol.Parameters; + IEnumerable originalParameters = originalMethod.Symbol.Parameters; if (IsReducedExtension) { @@ -24,14 +25,14 @@ namespace Semmle.Extraction.CSharp.Entities { // Non-generic reduced extensions must be extracted exactly like the // non-reduced counterparts - parameters = symbol.ReducedFrom.Parameters; + parameters = Symbol.ReducedFrom.Parameters; } else { // Constructed reduced extensions are special because their non-reduced // counterparts are not constructed. Therefore, we need to manually add // the `this` parameter based on the type of the receiver - var originalThisParamSymbol = originalMethod.symbol.Parameters.First(); + var originalThisParamSymbol = originalMethod.Symbol.Parameters.First(); var originalThisParam = Parameter.Create(Context, originalThisParamSymbol, originalMethod); ConstructedExtensionParameter.Create(Context, this, originalThisParam); originalParameters = originalParameters.Skip(1); @@ -46,7 +47,7 @@ namespace Semmle.Extraction.CSharp.Entities Parameter.Create(Context, p.paramSymbol, this, original); } - if (symbol.IsVararg) + if (Symbol.IsVararg) { // Mono decided that "__arglist" should be an explicit parameter, // so now we need to populate it. @@ -83,14 +84,24 @@ namespace Semmle.Extraction.CSharp.Entities else Expression.Create(Context, expr, this, 0); - Context.NumberOfLines(trapFile, BodyDeclaringSymbol, this); + NumberOfLines(trapFile, BodyDeclaringSymbol, this); }); } } + public static void NumberOfLines(TextWriter trapFile, ISymbol symbol, IEntity callable) + { + foreach (var decl in symbol.DeclaringSyntaxReferences) + { + var node = (CSharpSyntaxNode)decl.GetSyntax(); + var lineCounts = node.Accept(new AstLineCounter()); + trapFile.numlines(callable, lineCounts); + } + } + public void Overrides(TextWriter trapFile) { - foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations + foreach (var explicitInterface in Symbol.ExplicitInterfaceImplementations .Where(sym => sym.MethodKind == MethodKind.Ordinary) .Select(impl => Type.Create(Context, impl.ContainingType))) { @@ -98,14 +109,14 @@ namespace Semmle.Extraction.CSharp.Entities if (IsSourceDeclaration) { - foreach (var syntax in symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).OfType()) + foreach (var syntax in Symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).OfType()) TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier.Name, this, explicitInterface); } } - if (symbol.OverriddenMethod != null) + if (Symbol.OverriddenMethod != null) { - trapFile.overrides(this, Method.Create(Context, symbol.OverriddenMethod)); + trapFile.overrides(this, Method.Create(Context, Symbol.OverriddenMethod)); } } @@ -114,22 +125,22 @@ namespace Semmle.Extraction.CSharp.Entities /// private static void BuildMethodId(Method m, TextWriter trapFile) { - m.symbol.ReturnType.BuildOrWriteId(m.Context, trapFile, m.symbol); + m.Symbol.ReturnType.BuildOrWriteId(m.Context, trapFile, m.Symbol); trapFile.Write(" "); trapFile.WriteSubId(m.ContainingType); - AddExplicitInterfaceQualifierToId(m.Context, trapFile, m.symbol.ExplicitInterfaceImplementations); + AddExplicitInterfaceQualifierToId(m.Context, trapFile, m.Symbol.ExplicitInterfaceImplementations); trapFile.Write("."); - trapFile.Write(m.symbol.Name); + trapFile.Write(m.Symbol.Name); - if (m.symbol.IsGenericMethod) + if (m.Symbol.IsGenericMethod) { - if (SymbolEqualityComparer.Default.Equals(m.symbol, m.symbol.OriginalDefinition)) + if (SymbolEqualityComparer.Default.Equals(m.Symbol, m.Symbol.OriginalDefinition)) { trapFile.Write('`'); - trapFile.Write(m.symbol.TypeParameters.Length); + trapFile.Write(m.Symbol.TypeParameters.Length); } else { @@ -138,13 +149,13 @@ namespace Semmle.Extraction.CSharp.Entities // Type arguments with different nullability can result in // a constructed method with different nullability of its parameters and return type, // so we need to create a distinct database entity for it. - trapFile.BuildList(",", m.symbol.GetAnnotatedTypeArguments(), (ta, tb0) => { ta.Symbol.BuildOrWriteId(m.Context, tb0, m.symbol); trapFile.Write((int)ta.Nullability); }); + trapFile.BuildList(",", m.Symbol.GetAnnotatedTypeArguments(), (ta, tb0) => { ta.Symbol.BuildOrWriteId(m.Context, tb0, m.Symbol); trapFile.Write((int)ta.Nullability); }); trapFile.Write('>'); } } - AddParametersToId(m.Context, trapFile, m.symbol); - switch (m.symbol.MethodKind) + AddParametersToId(m.Context, trapFile, m.Symbol); + switch (m.Symbol.MethodKind) { case MethodKind.PropertyGet: trapFile.Write(";getter"); @@ -213,7 +224,7 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.AppendList(",", explicitInterfaceImplementations.Select(impl => cx.CreateEntity(impl.ContainingType))); } - public virtual string Name => symbol.Name; + public virtual string Name => Symbol.Name; /// /// Creates a method of the appropriate subtype. @@ -267,28 +278,28 @@ namespace Semmle.Extraction.CSharp.Entities public Method OriginalDefinition => IsReducedExtension - ? Create(Context, symbol.ReducedFrom) - : Create(Context, symbol.OriginalDefinition); + ? Create(Context, Symbol.ReducedFrom) + : Create(Context, Symbol.OriginalDefinition); public override Microsoft.CodeAnalysis.Location FullLocation => ReportingLocation; - public override bool IsSourceDeclaration => symbol.IsSourceDeclaration(); + public override bool IsSourceDeclaration => Symbol.IsSourceDeclaration(); /// /// Whether this method has type parameters. /// - public bool IsGeneric => symbol.IsGenericMethod; + public bool IsGeneric => Symbol.IsGenericMethod; /// /// Whether this method has unbound type parameters. /// - public bool IsUnboundGeneric => IsGeneric && SymbolEqualityComparer.Default.Equals(symbol.ConstructedFrom, symbol); + public bool IsUnboundGeneric => IsGeneric && SymbolEqualityComparer.Default.Equals(Symbol.ConstructedFrom, Symbol); public bool IsBoundGeneric => IsGeneric && !IsUnboundGeneric; - private bool IsReducedExtension => symbol.MethodKind == MethodKind.ReducedExtension; + private bool IsReducedExtension => Symbol.MethodKind == MethodKind.ReducedExtension; - protected IMethodSymbol ConstructedFromSymbol => symbol.ConstructedFrom.ReducedFrom ?? symbol.ConstructedFrom; + protected IMethodSymbol ConstructedFromSymbol => Symbol.ConstructedFrom.ReducedFrom ?? Symbol.ConstructedFrom; bool IExpressionParentEntity.IsTopLevelParent => true; @@ -305,19 +316,19 @@ namespace Semmle.Extraction.CSharp.Entities if (isFullyConstructed) { trapFile.constructed_generic(this, Method.Create(Context, ConstructedFromSymbol)); - foreach (var tp in symbol.GetAnnotatedTypeArguments()) + foreach (var tp in Symbol.GetAnnotatedTypeArguments()) { trapFile.type_arguments(Type.Create(Context, tp.Symbol), child, this); child++; } - var nullability = new Nullability(symbol); + var nullability = new Nullability(Symbol); if (!nullability.IsOblivious) trapFile.type_nullability(this, NullabilityEntity.Create(Context, nullability)); } else { - foreach (var typeParam in symbol.TypeParameters.Select(tp => TypeParameter.Create(Context, tp))) + foreach (var typeParam in Symbol.TypeParameters.Select(tp => TypeParameter.Create(Context, tp))) { trapFile.type_parameters(typeParam, child, this); child++; @@ -343,7 +354,7 @@ namespace Semmle.Extraction.CSharp.Entities PopulateMethodBody(trapFile); PopulateGenerics(trapFile); PopulateMetadataHandle(trapFile); - PopulateNullability(trapFile, symbol.GetAnnotatedReturnType()); + PopulateNullability(trapFile, Symbol.GetAnnotatedReturnType()); } public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.PushesLabel; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs index 4b2725b89f3..b644558f799 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Modifier.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - trapFile.Write(symbol); + trapFile.Write(Symbol); trapFile.Write(";modifier"); } @@ -20,7 +20,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void Populate(TextWriter trapFile) { - trapFile.modifiers(Label, symbol); + trapFile.modifiers(Label, Symbol); } public static string AccessbilityModifier(Accessibility access) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Namespace.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Namespace.cs index 6ae31c82b64..31e66656eb3 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Namespace.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Namespace.cs @@ -12,11 +12,11 @@ namespace Semmle.Extraction.CSharp.Entities public override void Populate(TextWriter trapFile) { - trapFile.namespaces(this, symbol.Name); + trapFile.namespaces(this, Symbol.Name); - if (symbol.ContainingNamespace != null) + if (Symbol.ContainingNamespace != null) { - var parent = Create(Context, symbol.ContainingNamespace); + var parent = Create(Context, Symbol.ContainingNamespace); trapFile.parent_namespace(this, parent); } } @@ -25,11 +25,11 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - if (!symbol.IsGlobalNamespace) + if (!Symbol.IsGlobalNamespace) { - trapFile.WriteSubId(Create(Context, symbol.ContainingNamespace)); + trapFile.WriteSubId(Create(Context, Symbol.ContainingNamespace)); trapFile.Write('.'); - trapFile.Write(symbol.Name); + trapFile.Write(Symbol.Name); } trapFile.Write(";namespace"); } @@ -47,7 +47,7 @@ namespace Semmle.Extraction.CSharp.Entities public override int GetHashCode() => QualifiedName.GetHashCode(); - private string QualifiedName => symbol.ToDisplayString(); + private string QualifiedName => Symbol.ToDisplayString(); public override bool Equals(object obj) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/NamespaceDeclaration.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/NamespaceDeclaration.cs index 17f9a198a39..3a19417008a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/NamespaceDeclaration.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/NamespaceDeclaration.cs @@ -22,7 +22,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - trapFile.WriteSubId(Context.Create(ReportingLocation)); + trapFile.WriteSubId(Context.CreateLocation(ReportingLocation)); trapFile.Write(";namespacedeclaration"); } @@ -31,7 +31,7 @@ namespace Semmle.Extraction.CSharp.Entities var @namespace = (INamespaceSymbol)Context.GetModel(node).GetSymbolInfo(node.Name).Symbol; var ns = Namespace.Create(Context, @namespace); trapFile.namespace_declarations(this, ns); - trapFile.namespace_declaration_location(this, Context.Create(node.Name.GetLocation())); + trapFile.namespace_declaration_location(this, Context.CreateLocation(node.Name.GetLocation())); var visitor = new Populators.TypeOrNamespaceVisitor(Context, trapFile, this); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/OrdinaryMethod.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/OrdinaryMethod.cs index d81fc25f72b..90f17d70ea4 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/OrdinaryMethod.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/OrdinaryMethod.cs @@ -11,20 +11,20 @@ namespace Semmle.Extraction.CSharp.Entities private OrdinaryMethod(Context cx, IMethodSymbol init) : base(cx, init) { } - public override string Name => symbol.GetName(); + public override string Name => Symbol.GetName(); - protected override IMethodSymbol BodyDeclaringSymbol => symbol.PartialImplementationPart ?? symbol; + protected override IMethodSymbol BodyDeclaringSymbol => Symbol.PartialImplementationPart ?? Symbol; public IMethodSymbol SourceDeclaration { get { - var reducedFrom = symbol.ReducedFrom ?? symbol; + var reducedFrom = Symbol.ReducedFrom ?? Symbol; return reducedFrom.OriginalDefinition; } } - public override Microsoft.CodeAnalysis.Location ReportingLocation => symbol.GetSymbolLocation(); + public override Microsoft.CodeAnalysis.Location ReportingLocation => Symbol.GetSymbolLocation(); public override void Populate(TextWriter trapFile) { @@ -32,12 +32,12 @@ namespace Semmle.Extraction.CSharp.Entities PopulateModifiers(trapFile); ContainingType.PopulateGenerics(); - var returnType = Type.Create(Context, symbol.ReturnType); + var returnType = Type.Create(Context, Symbol.ReturnType); trapFile.methods(this, Name, ContainingType, returnType.TypeRef, OriginalDefinition); if (IsSourceDeclaration) { - foreach (var declaration in symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).OfType()) + foreach (var declaration in Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).OfType()) { Context.BindComments(this, declaration.Identifier.GetLocation()); TypeMention.Create(Context, declaration.ReturnType, this, returnType); @@ -49,7 +49,7 @@ namespace Semmle.Extraction.CSharp.Entities PopulateGenerics(trapFile); Overrides(trapFile); - ExtractRefReturn(trapFile, symbol, this); + ExtractRefReturn(trapFile, Symbol, this); ExtractCompilerGenerated(trapFile); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Parameter.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Parameter.cs index c42a3edd236..b1b7a5444fe 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Parameter.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Parameter.cs @@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp.Entities Original = original ?? this; } - public override Microsoft.CodeAnalysis.Location ReportingLocation => symbol.GetSymbolLocation(); + public override Microsoft.CodeAnalysis.Location ReportingLocation => Symbol.GetSymbolLocation(); public enum Kind { @@ -35,9 +35,9 @@ namespace Semmle.Extraction.CSharp.Entities // actually numbered from 1. // This is to be consistent from the original (unreduced) extension method. var isReducedExtension = - symbol.ContainingSymbol is IMethodSymbol method && + Symbol.ContainingSymbol is IMethodSymbol method && method.MethodKind == MethodKind.ReducedExtension; - return symbol.Ordinal + (isReducedExtension ? 1 : 0); + return Symbol.Ordinal + (isReducedExtension ? 1 : 0); } } @@ -45,7 +45,7 @@ namespace Semmle.Extraction.CSharp.Entities { get { - switch (symbol.RefKind) + switch (Symbol.RefKind) { case RefKind.Out: return Kind.Out; @@ -54,12 +54,12 @@ namespace Semmle.Extraction.CSharp.Entities case RefKind.In: return Kind.In; default: - if (symbol.IsParams) + if (Symbol.IsParams) return Kind.Params; if (Ordinal == 0) { - if (symbol.ContainingSymbol is IMethodSymbol method && method.IsExtensionMethod) + if (Symbol.ContainingSymbol is IMethodSymbol method && method.IsExtensionMethod) return Kind.This; } return Kind.None; @@ -76,7 +76,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { if (Parent == null) - Parent = Method.Create(Context, symbol.ContainingSymbol as IMethodSymbol); + Parent = Method.Create(Context, Symbol.ContainingSymbol as IMethodSymbol); trapFile.WriteSubId(Parent); trapFile.Write('_'); trapFile.Write(Ordinal); @@ -92,34 +92,42 @@ namespace Semmle.Extraction.CSharp.Entities // Very rarely, two parameters have the same name according to the data model. // This breaks our database constraints. // Generate an impossible name to ensure that it doesn't conflict. - var conflictingCount = symbol.ContainingSymbol.GetParameters().Count(p => p.Ordinal < symbol.Ordinal && p.Name == symbol.Name); - return conflictingCount > 0 ? symbol.Name + "`" + conflictingCount : symbol.Name; + var conflictingCount = Symbol.ContainingSymbol.GetParameters().Count(p => p.Ordinal < Symbol.Ordinal && p.Name == Symbol.Name); + return conflictingCount > 0 ? Symbol.Name + "`" + conflictingCount : Symbol.Name; } } public override void Populate(TextWriter trapFile) { PopulateAttributes(); - PopulateNullability(trapFile, symbol.GetAnnotatedType()); - PopulateRefKind(trapFile, symbol.RefKind); + PopulateNullability(trapFile, Symbol.GetAnnotatedType()); + PopulateRefKind(trapFile, Symbol.RefKind); - if (symbol.Name != Original.symbol.Name) - Context.ModelError(symbol, "Inconsistent parameter declaration"); + if (Symbol.Name != Original.Symbol.Name) + Context.ModelError(Symbol, "Inconsistent parameter declaration"); - var type = Type.Create(Context, symbol.Type); + var type = Type.Create(Context, Symbol.Type); trapFile.@params(this, Name, type.TypeRef, Ordinal, ParamKind, Parent, Original); - foreach (var l in symbol.Locations) - trapFile.param_location(this, Context.Create(l)); + foreach (var l in Symbol.Locations) + trapFile.param_location(this, Context.CreateLocation(l)); - if (!IsSourceDeclaration || !symbol.FromSource()) + if (!Symbol.Locations.Any() && + Symbol.ContainingSymbol is IMethodSymbol ms && + ms.Name == WellKnownMemberNames.TopLevelStatementsEntryPointMethodName && + ms.ContainingType.Name == WellKnownMemberNames.TopLevelStatementsEntryPointTypeName) + { + trapFile.param_location(this, Context.CreateLocation()); + } + + if (!IsSourceDeclaration || !Symbol.FromSource()) return; BindComments(); if (IsSourceDeclaration) { - foreach (var syntax in symbol.DeclaringSyntaxReferences + foreach (var syntax in Symbol.DeclaringSyntaxReferences .Select(d => d.GetSyntax()) .OfType() .Where(s => s.Type != null)) @@ -128,21 +136,21 @@ namespace Semmle.Extraction.CSharp.Entities } } - if (symbol.HasExplicitDefaultValue && Context.Defines(symbol)) + if (Symbol.HasExplicitDefaultValue && Context.Defines(Symbol)) { // This is a slight bug in the dbscheme // We should really define param_default(param, string) // And use parameter child #0 to encode the default expression. - var defaultValue = GetParameterDefaultValue(symbol); + var defaultValue = GetParameterDefaultValue(Symbol); if (defaultValue == null) { // In case this parameter belongs to an accessor of an indexer, we need // to get the default value from the corresponding parameter belonging // to the indexer itself - var method = (IMethodSymbol)symbol.ContainingSymbol; + var method = (IMethodSymbol)Symbol.ContainingSymbol; if (method != null) { - var i = method.Parameters.IndexOf(symbol); + var i = method.Parameters.IndexOf(Symbol); var indexer = (IPropertySymbol)method.AssociatedSymbol; if (indexer != null) defaultValue = GetParameterDefaultValue(indexer.Parameters[i]); @@ -159,7 +167,7 @@ namespace Semmle.Extraction.CSharp.Entities } } - public override bool IsSourceDeclaration => symbol.IsSourceDeclaration(); + public override bool IsSourceDeclaration => Symbol.IsSourceDeclaration(); bool IExpressionParentEntity.IsTopLevelParent => true; @@ -231,7 +239,7 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.param_location(this, GeneratedLocation.Create(Context)); } - protected override int Ordinal => ((Method)Parent).OriginalDefinition.symbol.Parameters.Length; + protected override int Ordinal => ((Method)Parent).OriginalDefinition.Symbol.Parameters.Length; public override int GetHashCode() { @@ -258,20 +266,20 @@ namespace Semmle.Extraction.CSharp.Entities private readonly ITypeSymbol constructedType; private ConstructedExtensionParameter(Context cx, Method method, Parameter original) - : base(cx, original.symbol, method, original) + : base(cx, original.Symbol, method, original) { - constructedType = method.symbol.ReceiverType; + constructedType = method.Symbol.ReceiverType; } public override void Populate(TextWriter trapFile) { var typeKey = Type.Create(Context, constructedType); - trapFile.@params(this, Original.symbol.Name, typeKey.TypeRef, 0, Kind.This, Parent, Original); + trapFile.@params(this, Original.Symbol.Name, typeKey.TypeRef, 0, Kind.This, Parent, Original); trapFile.param_location(this, Original.Location); } public static ConstructedExtensionParameter Create(Context cx, Method method, Parameter parameter) => - ExtensionParamFactory.Instance.CreateEntity(cx, (new SymbolEqualityWrapper(parameter.symbol), new SymbolEqualityWrapper(method.symbol.ReceiverType)), (method, parameter)); + ExtensionParamFactory.Instance.CreateEntity(cx, (new SymbolEqualityWrapper(parameter.Symbol), new SymbolEqualityWrapper(method.Symbol.ReceiverType)), (method, parameter)); private class ExtensionParamFactory : ICachedEntityFactory<(Method, Parameter), ConstructedExtensionParameter> { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/ElifDirective.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/ElifDirective.cs index ace89464b96..ece87fd1f42 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/ElifDirective.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/ElifDirective.cs @@ -22,7 +22,7 @@ namespace Semmle.Extraction.CSharp.Entities { trapFile.directive_elifs(this, trivia.BranchTaken, trivia.ConditionValue, start, index); - Expression.Create(cx, trivia.Condition, this, 0); + Expression.Create(Context, trivia.Condition, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/IfDirective.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/IfDirective.cs index e1b81e60d8a..29c6f741620 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/IfDirective.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/IfDirective.cs @@ -16,7 +16,7 @@ namespace Semmle.Extraction.CSharp.Entities { trapFile.directive_ifs(this, trivia.BranchTaken, trivia.ConditionValue); - Expression.Create(cx, trivia.Condition, this, 0); + Expression.Create(Context, trivia.Condition, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineDirective.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineDirective.cs index 54681d317e9..470f54f379a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineDirective.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineDirective.cs @@ -31,7 +31,7 @@ namespace Semmle.Extraction.CSharp.Entities if (!string.IsNullOrWhiteSpace(trivia.File.ValueText)) { - var file = Extraction.Entities.File.Create(cx, trivia.File.ValueText); + var file = Extraction.Entities.File.Create(Context, trivia.File.ValueText); trapFile.directive_line_file(this, file); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PragmaChecksumDirective.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PragmaChecksumDirective.cs index dff901fa826..572b77e2c73 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PragmaChecksumDirective.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PragmaChecksumDirective.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CSharp.Entities protected override void PopulatePreprocessor(TextWriter trapFile) { - var file = Extraction.Entities.File.Create(cx, trivia.File.ValueText); + var file = Extraction.Entities.File.Create(Context, trivia.File.ValueText); trapFile.pragma_checksums(this, file, trivia.Guid.ToString(), trivia.Bytes.ToString()); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PreprocessorDirective.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PreprocessorDirective.cs index 6ea6172a2d3..13e702603ab 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PreprocessorDirective.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PreprocessorDirective.cs @@ -23,11 +23,11 @@ namespace Semmle.Extraction.CSharp.Entities PopulatePreprocessor(trapFile); trapFile.preprocessor_directive_active(this, trivia.IsActive); - trapFile.preprocessor_directive_location(this, cx.Create(ReportingLocation)); + trapFile.preprocessor_directive_location(this, Context.CreateLocation(ReportingLocation)); - if (!cx.Extractor.Standalone) + if (!Context.Extractor.Standalone) { - var compilation = Compilation.Create(cx); + var compilation = Compilation.Create(Context); trapFile.preprocessor_directive_compilation(this, compilation); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs index 08d5ae47c5e..085047d0812 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities protected Property(Context cx, IPropertySymbol init) : base(cx, init) { - type = new Lazy(() => Type.Create(Context, symbol.Type)); + type = new Lazy(() => Type.Create(base.Context, Symbol.Type)); } private readonly Lazy type; @@ -27,8 +27,8 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.Write(" "); trapFile.WriteSubId(ContainingType); trapFile.Write('.'); - Method.AddExplicitInterfaceQualifierToId(Context, trapFile, symbol.ExplicitInterfaceImplementations); - trapFile.Write(symbol.Name); + Method.AddExplicitInterfaceQualifierToId(Context, trapFile, Symbol.ExplicitInterfaceImplementations); + trapFile.Write(Symbol.Name); trapFile.Write(";property"); } @@ -38,14 +38,14 @@ namespace Semmle.Extraction.CSharp.Entities PopulateAttributes(); PopulateModifiers(trapFile); BindComments(); - PopulateNullability(trapFile, symbol.GetAnnotatedType()); - PopulateRefKind(trapFile, symbol.RefKind); + PopulateNullability(trapFile, Symbol.GetAnnotatedType()); + PopulateRefKind(trapFile, Symbol.RefKind); var type = Type; - trapFile.properties(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition)); + trapFile.properties(this, Symbol.GetName(), ContainingType, type.TypeRef, Create(Context, Symbol.OriginalDefinition)); - var getter = symbol.GetMethod; - var setter = symbol.SetMethod; + var getter = Symbol.GetMethod; + var setter = Symbol.SetMethod; if (!(getter is null)) Method.Create(Context, getter); @@ -54,11 +54,11 @@ namespace Semmle.Extraction.CSharp.Entities Method.Create(Context, setter); var declSyntaxReferences = IsSourceDeclaration ? - symbol.DeclaringSyntaxReferences. + Symbol.DeclaringSyntaxReferences. Select(d => d.GetSyntax()).OfType().ToArray() : Enumerable.Empty(); - foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType))) + foreach (var explicitInterface in Symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType))) { trapFile.explicitly_implements(this, explicitInterface.TypeRef); @@ -69,7 +69,7 @@ namespace Semmle.Extraction.CSharp.Entities foreach (var l in Locations) trapFile.property_location(this, l); - if (IsSourceDeclaration && symbol.FromSource()) + if (IsSourceDeclaration && Symbol.FromSource()) { var expressionBody = ExpressionBody; if (expressionBody != null) @@ -84,15 +84,15 @@ namespace Semmle.Extraction.CSharp.Entities { Context.PopulateLater(() => { - var loc = Context.Create(initializer.GetLocation()); - var annotatedType = AnnotatedTypeSymbol.CreateNotAnnotated(symbol.Type); + var loc = Context.CreateLocation(initializer.GetLocation()); + var annotatedType = AnnotatedTypeSymbol.CreateNotAnnotated(Symbol.Type); var simpleAssignExpr = new Expression(new ExpressionInfo(Context, annotatedType, loc, ExprKind.SIMPLE_ASSIGN, this, child++, false, null)); Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer.Value, simpleAssignExpr, 0)); var access = new Expression(new ExpressionInfo(Context, annotatedType, Location, ExprKind.PROPERTY_ACCESS, simpleAssignExpr, 1, false, null)); trapFile.expr_access(access, this); - if (!symbol.IsStatic) + if (!Symbol.IsStatic) { - This.CreateImplicit(Context, symbol.ContainingType, Location, access, -1); + This.CreateImplicit(Context, Symbol.ContainingType, Location, access, -1); } }); } @@ -106,11 +106,11 @@ namespace Semmle.Extraction.CSharp.Entities { get { - return symbol.DeclaringSyntaxReferences + return Symbol.DeclaringSyntaxReferences .Select(r => r.GetSyntax()) .OfType() .Select(s => s.GetLocation()) - .Concat(symbol.Locations) + .Concat(Symbol.Locations) .First(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement.cs index 44bd7f80583..94ab9a7ce61 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement.cs @@ -1,24 +1,41 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; -using Semmle.Extraction.CSharp.Populators; using Microsoft.CodeAnalysis.CSharp; -using Semmle.Extraction.Entities; using System.IO; namespace Semmle.Extraction.CSharp.Entities { - /// - /// Whether this entity is the parent of a top-level statement. - /// - public interface IStatementParentEntity : IEntity - { - bool IsTopLevelParent { get; } - } - internal abstract class Statement : FreshEntity, IExpressionParentEntity, IStatementParentEntity { - protected Statement(Context cx) : base(cx) { } + private readonly int child; + private readonly Kinds.StmtKind kind; + private readonly IStatementParentEntity parent; - public static Statement Create(Context cx, StatementSyntax node, Statement parent, int child) => + protected Statement(Context cx, Kinds.StmtKind kind, IStatementParentEntity parent, int child) + : base(cx) + { + this.kind = kind; + this.parent = parent; + this.child = child; + } + + protected override void Populate(TextWriter trapFile) + { + trapFile.statements(this, kind); + if (parent.IsTopLevelParent) + { + trapFile.stmt_parent_top_level(this, child, parent); + } + else + { + trapFile.stmt_parent(this, child, parent); + } + + PopulateStatement(trapFile); + } + + protected abstract void PopulateStatement(TextWriter trapFile); + + public static Statement Create(Context cx, StatementSyntax node, IStatementParentEntity parent, int child) => Statements.Factory.Create(cx, node, parent, child); /// @@ -27,54 +44,10 @@ namespace Semmle.Extraction.CSharp.Entities /// public virtual int NumberOfStatements => 1; - public override Microsoft.CodeAnalysis.Location ReportingLocation => GetStatementSyntax().GetLocation(); - bool IExpressionParentEntity.IsTopLevelParent => false; bool IStatementParentEntity.IsTopLevelParent => false; - protected abstract CSharpSyntaxNode GetStatementSyntax(); - public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NeedsLabel; } - - internal abstract class Statement : Statement where TSyntax : CSharpSyntaxNode - { - protected readonly TSyntax Stmt; - private readonly int child; - private readonly Kinds.StmtKind kind; - private readonly IStatementParentEntity parent; - private readonly Location location; - - protected override CSharpSyntaxNode GetStatementSyntax() => Stmt; - - protected Statement(Context cx, TSyntax stmt, Kinds.StmtKind kind, IStatementParentEntity parent, int child, Location location) - : base(cx) - { - Stmt = stmt; - this.parent = parent; - this.child = child; - this.location = location; - this.kind = kind; - cx.BindComments(this, location.symbol); - } - - protected sealed override void Populate(TextWriter trapFile) - { - trapFile.statements(this, kind); - if (parent.IsTopLevelParent) - trapFile.stmt_parent_top_level(this, child, parent); - else - trapFile.stmt_parent(this, child, parent); - trapFile.stmt_location(this, location); - PopulateStatement(trapFile); - } - - protected abstract void PopulateStatement(TextWriter trapFile); - - protected Statement(Context cx, TSyntax stmt, Kinds.StmtKind kind, IStatementParentEntity parent, int child) - : this(cx, stmt, kind, parent, child, cx.Create(stmt.FixedLocation())) { } - - public override string ToString() => Label.ToString(); - } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement`1.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement`1.cs new file mode 100644 index 00000000000..21dd32e17cf --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statement`1.cs @@ -0,0 +1,35 @@ +using Semmle.Extraction.CSharp.Populators; +using Microsoft.CodeAnalysis.CSharp; +using Semmle.Extraction.Entities; +using System.IO; + +namespace Semmle.Extraction.CSharp.Entities +{ + internal abstract class Statement : Statement where TSyntax : CSharpSyntaxNode + { + protected readonly TSyntax Stmt; + private readonly Location location; + + protected Statement(Context cx, TSyntax stmt, Kinds.StmtKind kind, IStatementParentEntity parent, int child, Location location) + : base(cx, kind, parent, child) + { + Stmt = stmt; + this.location = location; + cx.BindComments(this, location.Symbol); + } + + protected Statement(Context cx, TSyntax stmt, Kinds.StmtKind kind, IStatementParentEntity parent, int child) + : this(cx, stmt, kind, parent, child, cx.CreateLocation(stmt.FixedLocation())) { } + + protected sealed override void Populate(TextWriter trapFile) + { + base.Populate(trapFile); + + trapFile.stmt_location(this, location); + } + + public override Microsoft.CodeAnalysis.Location ReportingLocation => Stmt.GetLocation(); + + public override string ToString() => Label.ToString(); + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Block.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Block.cs index c3e2e15baf3..c50cad9b6cd 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Block.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Block.cs @@ -20,7 +20,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { var child = 0; - foreach (var childStmt in Stmt.Statements.Select(c => Statement.Create(cx, c, this, child))) + foreach (var childStmt in Stmt.Statements.Select(c => Statement.Create(Context, c, this, child))) { child += childStmt.NumberOfStatements; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Case.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Case.cs index e2a712c006b..73a2582060b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Case.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Case.cs @@ -10,7 +10,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements internal abstract class Case : Statement where TSyntax : SwitchLabelSyntax { protected Case(Context cx, TSyntax node, Switch parent, int child) - : base(cx, node, StmtKind.CASE, parent, child, cx.Create(node.GetLocation())) { } + : base(cx, node, StmtKind.CASE, parent, child, cx.CreateLocation(node.GetLocation())) { } public static Statement Create(Context cx, SwitchLabelSyntax node, Switch parent, int child) { @@ -36,8 +36,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { var value = Stmt.Value; - Expression.Create(cx, value, this, 0); - Switch.LabelForValue(cx.GetModel(Stmt).GetConstantValue(value).Value); + Expression.Create(Context, value, this, 0); + Switch.LabelForValue(Context.GetModel(Stmt).GetConstantValue(value).Value); } public static CaseLabel Create(Context cx, CaseSwitchLabelSyntax node, Switch parent, int child) @@ -70,11 +70,11 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Expressions.Pattern.Create(cx, Stmt.Pattern, this, 0); + Expressions.Pattern.Create(Context, Stmt.Pattern, this, 0); if (Stmt.WhenClause != null) { - Expression.Create(cx, Stmt.WhenClause.Condition, this, 1); + Expression.Create(Context, Stmt.WhenClause.Condition, this, 1); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Catch.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Catch.cs index db1a0d413c1..f7484c72d56 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Catch.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Catch.cs @@ -10,7 +10,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements private static readonly string systemExceptionName = typeof(System.Exception).ToString(); private Catch(Context cx, CatchClauseSyntax node, Try parent, int child) - : base(cx, node, StmtKind.CATCH, parent, child, cx.Create(node.GetLocation())) { } + : base(cx, node, StmtKind.CATCH, parent, child, cx.CreateLocation(node.GetLocation())) { } protected override void PopulateStatement(TextWriter trapFile) { @@ -19,26 +19,26 @@ namespace Semmle.Extraction.CSharp.Entities.Statements if (hasVariableDeclaration) // A catch clause of the form 'catch(Ex ex) { ... }' { - var decl = Expressions.VariableDeclaration.Create(cx, Stmt.Declaration, false, this, 0); - trapFile.catch_type(this, Type.Create(cx, decl.Type).TypeRef, true); + var decl = Expressions.VariableDeclaration.Create(Context, Stmt.Declaration, false, this, 0); + trapFile.catch_type(this, Type.Create(Context, decl.Type).TypeRef, true); } else if (isSpecificCatchClause) // A catch clause of the form 'catch(Ex) { ... }' { - trapFile.catch_type(this, Type.Create(cx, cx.GetType(Stmt.Declaration.Type)).TypeRef, true); + trapFile.catch_type(this, Type.Create(Context, Context.GetType(Stmt.Declaration.Type)).TypeRef, true); } else // A catch clause of the form 'catch { ... }' { - var exception = Type.Create(cx, cx.Compilation.GetTypeByMetadataName(systemExceptionName)); + var exception = Type.Create(Context, Context.Compilation.GetTypeByMetadataName(systemExceptionName)); trapFile.catch_type(this, exception, false); } if (Stmt.Filter != null) { // For backward compatibility, the catch filter clause is child number 2. - Expression.Create(cx, Stmt.Filter.FilterExpression, this, 2); + Expression.Create(Context, Stmt.Filter.FilterExpression, this, 2); } - Create(cx, Stmt.Block, this, 1); + Create(Context, Stmt.Block, this, 1); } public static Catch Create(Context cx, CatchClauseSyntax node, Try parent, int child) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs index 4ce000bed3f..0f5766fad23 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Checked.cs @@ -18,7 +18,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Statement.Create(cx, Stmt.Block, this, 0); + Statement.Create(Context, Stmt.Block, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Do.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Do.cs index a0cd9da7372..e3518505270 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Do.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Do.cs @@ -8,7 +8,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements internal class Do : Statement { private Do(Context cx, DoStatementSyntax node, IStatementParentEntity parent, int child) - : base(cx, node, StmtKind.DO, parent, child, cx.Create(node.GetLocation())) { } + : base(cx, node, StmtKind.DO, parent, child, cx.CreateLocation(node.GetLocation())) { } public static Do Create(Context cx, DoStatementSyntax node, IStatementParentEntity parent, int child) { @@ -19,8 +19,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Create(cx, Stmt.Statement, this, 1); - Expression.Create(cx, Stmt.Condition, this, 0); + Create(Context, Stmt.Statement, this, 1); + Expression.Create(Context, Stmt.Condition, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ExpressionStatement.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ExpressionStatement.cs index 252e867efa9..76d369bde23 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ExpressionStatement.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ExpressionStatement.cs @@ -18,9 +18,9 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { if (Stmt.Expression != null) - Expression.Create(cx, Stmt.Expression, this, 0); + Expression.Create(Context, Stmt.Expression, this, 0); else - cx.ModelError(Stmt, "Invalid expression statement"); + Context.ModelError(Stmt, "Invalid expression statement"); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Factory.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Factory.cs index 69ebdca1c16..2960982061e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Factory.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Factory.cs @@ -5,7 +5,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements { internal static class Factory { - internal static Statement Create(Context cx, StatementSyntax node, Statement parent, int child) + internal static Statement Create(Context cx, StatementSyntax node, IStatementParentEntity parent, int child) { switch (node.Kind()) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Fixed.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Fixed.cs index 7cd8350c7d1..1d01b1708f2 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Fixed.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Fixed.cs @@ -19,8 +19,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - VariableDeclarations.Populate(cx, Stmt.Declaration, this, -1, childIncrement: -1); - Create(cx, Stmt.Statement, this, 0); + VariableDeclarations.Populate(Context, Stmt.Declaration, this, -1, childIncrement: -1); + Create(Context, Stmt.Statement, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/For.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/For.cs index 15c7151a8a6..678373168f6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/For.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/For.cs @@ -22,25 +22,25 @@ namespace Semmle.Extraction.CSharp.Entities.Statements var child = -1; if (Stmt.Declaration != null) - VariableDeclarations.Populate(cx, Stmt.Declaration, this, child, childIncrement: -1); + VariableDeclarations.Populate(Context, Stmt.Declaration, this, child, childIncrement: -1); foreach (var init in Stmt.Initializers) { - Expression.Create(cx, init, this, child--); + Expression.Create(Context, init, this, child--); } if (Stmt.Condition != null) { - Expression.Create(cx, Stmt.Condition, this, 0); + Expression.Create(Context, Stmt.Condition, this, 0); } child = 1; foreach (var inc in Stmt.Incrementors) { - Expression.Create(cx, inc, this, child++); + Expression.Create(Context, inc, this, child++); } - Statement.Create(cx, Stmt.Statement, this, 1 + Stmt.Incrementors.Count); + Statement.Create(Context, Stmt.Statement, this, 1 + Stmt.Incrementors.Count); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ForEach.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ForEach.cs index 262b3f0e328..bd9b1528a13 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ForEach.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/ForEach.cs @@ -8,6 +8,15 @@ namespace Semmle.Extraction.CSharp.Entities.Statements { internal class ForEach : Statement { + internal enum ForeachSymbolType + { + GetEnumeratorMethod = 1, + CurrentProperty, + MoveNextMethod, + DisposeMethod, + ElementType + } + private ForEach(Context cx, ForEachStatementSyntax stmt, IStatementParentEntity parent, int child) : base(cx, stmt, StmtKind.FOREACH, parent, child) { } @@ -18,18 +27,59 @@ namespace Semmle.Extraction.CSharp.Entities.Statements return ret; } - protected override void PopulateStatement(TextWriter _) + protected override void PopulateStatement(TextWriter trapFile) { - Expression.Create(cx, Stmt.Expression, this, 1); + Expression.Create(Context, Stmt.Expression, this, 1); - var typeSymbol = cx.GetModel(Stmt).GetDeclaredSymbol(Stmt); + var semanticModel = Context.GetModel(Stmt); + var typeSymbol = semanticModel.GetDeclaredSymbol(Stmt); var type = typeSymbol.GetAnnotatedType(); - var location = cx.Create(Stmt.Identifier.GetLocation()); + var location = Context.CreateLocation(Stmt.Identifier.GetLocation()); - Expressions.VariableDeclaration.Create(cx, typeSymbol, type, Stmt.Type, location, Stmt.Type.IsVar, this, 0); + Expressions.VariableDeclaration.Create(Context, typeSymbol, type, Stmt.Type, location, Stmt.Type.IsVar, this, 0); - Statement.Create(cx, Stmt.Statement, this, 2); + Statement.Create(Context, Stmt.Statement, this, 2); + + var info = semanticModel.GetForEachStatementInfo(Stmt); + + if (info.Equals(default)) + { + Context.ExtractionError("Could not get foreach statement info", null, Context.CreateLocation(this.ReportingLocation), severity: Util.Logging.Severity.Info); + return; + } + + trapFile.foreach_stmt_info(this, info.IsAsynchronous); + + if (info.GetEnumeratorMethod != null) + { + var m = Method.Create(Context, info.GetEnumeratorMethod); + trapFile.foreach_stmt_desugar(this, m, ForeachSymbolType.GetEnumeratorMethod); + } + + if (info.MoveNextMethod != null) + { + var m = Method.Create(Context, info.MoveNextMethod); + trapFile.foreach_stmt_desugar(this, m, ForeachSymbolType.MoveNextMethod); + } + + if (info.DisposeMethod != null) + { + var m = Method.Create(Context, info.DisposeMethod); + trapFile.foreach_stmt_desugar(this, m, ForeachSymbolType.DisposeMethod); + } + + if (info.CurrentProperty != null) + { + var p = Property.Create(Context, info.CurrentProperty); + trapFile.foreach_stmt_desugar(this, p, ForeachSymbolType.CurrentProperty); + } + + if (info.ElementType != null) + { + var t = Type.Create(Context, info.ElementType); + trapFile.foreach_stmt_desugar(this, t, ForeachSymbolType.ElementType); + } } } @@ -47,9 +97,9 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Expression.Create(cx, Stmt.Variable, this, 0); - Expression.Create(cx, Stmt.Expression, this, 1); - Statement.Create(cx, Stmt.Statement, this, 2); + Expression.Create(Context, Stmt.Variable, this, 0); + Expression.Create(Context, Stmt.Expression, this, 1); + Statement.Create(Context, Stmt.Statement, this, 2); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/GlobalStatementsBlock.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/GlobalStatementsBlock.cs new file mode 100644 index 00000000000..011437fac9b --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/GlobalStatementsBlock.cs @@ -0,0 +1,42 @@ +using Semmle.Extraction.Kinds; +using System.Linq; +using System.IO; +using Semmle.Extraction.Entities; + +namespace Semmle.Extraction.CSharp.Entities.Statements +{ + internal class GlobalStatementsBlock : Statement + { + private readonly Method parent; + + private GlobalStatementsBlock(Context cx, Method parent) + : base(cx, StmtKind.BLOCK, parent, 0) + { + this.parent = parent; + } + + public override Microsoft.CodeAnalysis.Location ReportingLocation + { + get + { + return parent.Symbol + ?.DeclaringSyntaxReferences + .FirstOrDefault() + ?.GetSyntax() + .GetLocation(); + } + } + + public static GlobalStatementsBlock Create(Context cx, Method parent) + { + var ret = new GlobalStatementsBlock(cx, parent); + ret.TryPopulate(); + return ret; + } + + protected override void PopulateStatement(TextWriter trapFile) + { + trapFile.stmt_location(this, Context.CreateLocation(ReportingLocation)); + } + } +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Goto.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Goto.cs index 772d69cff7c..20548dd056a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Goto.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Goto.cs @@ -40,8 +40,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements trapFile.exprorstmt_name(this, target); break; case StmtKind.GOTO_CASE: - Expr = Expression.Create(cx, Stmt.Expression, this, 0); - ConstantValue = Switch.LabelForValue(cx.GetModel(Stmt).GetConstantValue(Stmt.Expression).Value); + Expr = Expression.Create(Context, Stmt.Expression, this, 0); + ConstantValue = Switch.LabelForValue(Context.GetModel(Stmt).GetConstantValue(Stmt.Expression).Value); break; case StmtKind.GOTO_DEFAULT: ConstantValue = Switch.DefaultLabel; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/If.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/If.cs index b132c6ebe54..010cd9a793b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/If.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/If.cs @@ -18,12 +18,12 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Expression.Create(cx, Stmt.Condition, this, 0); + Expression.Create(Context, Stmt.Condition, this, 0); - Create(cx, Stmt.Statement, this, 1); + Create(Context, Stmt.Statement, this, 1); if (Stmt.Else != null) - Create(cx, Stmt.Else.Statement, this, 2); + Create(Context, Stmt.Else.Statement, this, 2); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Labeled.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Labeled.cs index 1fce622b347..d549008daaf 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Labeled.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Labeled.cs @@ -6,17 +6,18 @@ namespace Semmle.Extraction.CSharp.Entities.Statements { internal class Labeled : Statement { - private readonly Statement parent; + private readonly IStatementParentEntity parent; private readonly int child; + private Statement labelledStmt; - private Labeled(Context cx, LabeledStatementSyntax stmt, Statement parent, int child) + private Labeled(Context cx, LabeledStatementSyntax stmt, IStatementParentEntity parent, int child) : base(cx, stmt, StmtKind.LABEL, parent, child) { this.parent = parent; this.child = child; } - public static Labeled Create(Context cx, LabeledStatementSyntax node, Statement parent, int child) + public static Labeled Create(Context cx, LabeledStatementSyntax node, IStatementParentEntity parent, int child) { var ret = new Labeled(cx, node, parent, child); ret.TryPopulate(); @@ -27,13 +28,11 @@ namespace Semmle.Extraction.CSharp.Entities.Statements { trapFile.exprorstmt_name(this, Stmt.Identifier.ToString()); - // For compatilibty with the Mono extractor, make insert the labelled statement into the same block + // For compatibility with the Mono extractor, make insert the labelled statement into the same block // as this one. The parent MUST be a block statement. - labelledStmt = Statement.Create(cx, Stmt.Statement, parent, child + 1); + labelledStmt = Statement.Create(Context, Stmt.Statement, parent, child + 1); } - private Statement labelledStmt; - public override int NumberOfStatements => 1 + labelledStmt.NumberOfStatements; } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalDeclaration.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalDeclaration.cs index 87559681fd8..b147e5693ca 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalDeclaration.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalDeclaration.cs @@ -30,8 +30,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - VariableDeclarations.Populate(cx, Stmt.Declaration, this, 0); - cx.BindComments(this, Stmt.GetLocation()); + VariableDeclarations.Populate(Context, Stmt.Declaration, this, 0); + Context.BindComments(this, Stmt.GetLocation()); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalFunction.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalFunction.cs index eb81f27cb3a..aaebd538c67 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalFunction.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/LocalFunction.cs @@ -10,7 +10,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements internal class LocalFunction : Statement { private LocalFunction(Context cx, LocalFunctionStatementSyntax node, IStatementParentEntity parent, int child) - : base(cx, node, StmtKind.LOCAL_FUNCTION, parent, child, cx.Create(node.GetLocation())) { } + : base(cx, node, StmtKind.LOCAL_FUNCTION, parent, child, cx.CreateLocation(node.GetLocation())) { } public static LocalFunction Create(Context cx, LocalFunctionStatementSyntax node, IStatementParentEntity parent, int child) { @@ -26,7 +26,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements { get { - var m = cx.GetModel(Stmt); + var m = Context.GetModel(Stmt); return m.GetDeclaredSymbol(Stmt) as IMethodSymbol; } } @@ -34,7 +34,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements /// /// Gets the function defined by this local statement. /// - private Entities.LocalFunction Function => Entities.LocalFunction.Create(cx, Symbol); + private Entities.LocalFunction Function => Entities.LocalFunction.Create(Context, Symbol); protected override void PopulateStatement(TextWriter trapFile) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Lock.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Lock.cs index fbcb346b367..141edcea63a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Lock.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Lock.cs @@ -18,8 +18,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Expression.Create(cx, Stmt.Expression, this, 0); - Statement.Create(cx, Stmt.Statement, this, 1); + Expression.Create(Context, Stmt.Expression, this, 0); + Statement.Create(Context, Stmt.Statement, this, 1); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs index 2ff04b82b87..64692630b4c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Return.cs @@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { if (Stmt.Expression != null) - Expression.Create(cx, Stmt.Expression, this, 0); + Expression.Create(Context, Stmt.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Switch.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Switch.cs index f64067be9c4..df73554f389 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Switch.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Switch.cs @@ -30,17 +30,17 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Expression.Create(cx, Stmt.Expression, this, 0); + Expression.Create(Context, Stmt.Expression, this, 0); var childIndex = 0; foreach (var section in Stmt.Sections) { - foreach (var stmt in section.Labels.Select(label => Case.Create(cx, label, this, childIndex))) + foreach (var stmt in section.Labels.Select(label => Case.Create(Context, label, this, childIndex))) { childIndex += stmt.NumberOfStatements; } - foreach (var stmt in section.Statements.Select(s => Create(cx, s, this, childIndex))) + foreach (var stmt in section.Statements.Select(s => Create(Context, s, this, childIndex))) { childIndex += stmt.NumberOfStatements; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs index c550b1f1e39..dbff60d890b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Throw.cs @@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { if (Stmt.Expression != null) - Expression.Create(cx, Stmt.Expression, this, 0); + Expression.Create(Context, Stmt.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Try.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Try.cs index 5a6937790c2..bb17c2beaa2 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Try.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Try.cs @@ -22,14 +22,14 @@ namespace Semmle.Extraction.CSharp.Entities.Statements var child = 1; foreach (var c in Stmt.Catches) { - Catch.Create(cx, c, this, child++); + Catch.Create(Context, c, this, child++); } - Create(cx, Stmt.Block, this, 0); + Create(Context, Stmt.Block, this, 0); if (Stmt.Finally != null) { - Create(cx, Stmt.Finally.Block, this, -1); + Create(Context, Stmt.Finally.Block, this, -1); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs index 2d69bdc8d4c..37de04f870b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unchecked.cs @@ -18,7 +18,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Statement.Create(cx, Stmt.Block, this, 0); + Statement.Create(Context, Stmt.Block, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unsafe.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unsafe.cs index 4e371604333..936c5e84296 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unsafe.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Unsafe.cs @@ -18,7 +18,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Create(cx, Stmt.Block, this, 0); + Create(Context, Stmt.Block, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Using.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Using.cs index 93d0f6a88ff..b62e6556a8c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Using.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Using.cs @@ -20,13 +20,13 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { if (Stmt.Declaration != null) - VariableDeclarations.Populate(cx, Stmt.Declaration, this, -1, childIncrement: -1); + VariableDeclarations.Populate(Context, Stmt.Declaration, this, -1, childIncrement: -1); if (Stmt.Expression != null) - Expression.Create(cx, Stmt.Expression, this, 0); + Expression.Create(Context, Stmt.Expression, this, 0); if (Stmt.Statement != null) - Statement.Create(cx, Stmt.Statement, this, 1); + Statement.Create(Context, Stmt.Statement, this, 1); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/While.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/While.cs index 8def91a8418..99e96e7b683 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/While.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/While.cs @@ -18,8 +18,8 @@ namespace Semmle.Extraction.CSharp.Entities.Statements protected override void PopulateStatement(TextWriter trapFile) { - Expression.Create(cx, Stmt.Condition, this, 0); - Create(cx, Stmt.Statement, this, 1); + Expression.Create(Context, Stmt.Condition, this, 0); + Create(Context, Stmt.Statement, this, 1); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Yield.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Yield.cs index 9aace8e0b83..605a7cacdbc 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Yield.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Yield.cs @@ -20,7 +20,7 @@ namespace Semmle.Extraction.CSharp.Entities.Statements { if (Stmt.Expression != null) { - Expression.Create(cx, Stmt.Expression, this, 0); + Expression.Create(Context, Stmt.Expression, this, 0); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Symbol.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Symbol.cs index 5e0850ba8c8..2ac5c6e90e5 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Symbol.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Symbol.cs @@ -14,18 +14,18 @@ namespace Semmle.Extraction.CSharp.Entities protected CachedSymbol(Context cx, T init) : base(cx, init) { } - public virtual Type ContainingType => symbol.ContainingType != null ? Type.Create(Context, symbol.ContainingType) : null; + public virtual Type ContainingType => Symbol.ContainingType != null ? Type.Create(Context, Symbol.ContainingType) : null; public void PopulateModifiers(TextWriter trapFile) { - Modifier.ExtractModifiers(Context, trapFile, this, symbol); + Modifier.ExtractModifiers(Context, trapFile, this, Symbol); } protected void PopulateAttributes() { // Only extract attributes for source declarations - if (ReferenceEquals(symbol, symbol.OriginalDefinition)) - Attribute.ExtractAttributes(Context, symbol, this); + if (ReferenceEquals(Symbol, Symbol.OriginalDefinition)) + Attribute.ExtractAttributes(Context, Symbol, this); } protected void PopulateNullability(TextWriter trapFile, AnnotatedTypeSymbol type) @@ -55,7 +55,7 @@ namespace Semmle.Extraction.CSharp.Entities protected void ExtractCompilerGenerated(TextWriter trapFile) { - if (symbol.IsImplicitlyDeclared) + if (Symbol.IsImplicitlyDeclared) trapFile.compiler_generated(this); } @@ -63,12 +63,12 @@ namespace Semmle.Extraction.CSharp.Entities /// The location which is stored in the database and is used when highlighing source code. /// It's generally short, e.g. a method name. /// - public override Microsoft.CodeAnalysis.Location ReportingLocation => symbol.Locations.FirstOrDefault(); + public override Microsoft.CodeAnalysis.Location ReportingLocation => Symbol.Locations.FirstOrDefault(); /// /// The full text span of the entity, e.g. for binding comments. /// - public virtual Microsoft.CodeAnalysis.Location FullLocation => symbol.Locations.FirstOrDefault(); + public virtual Microsoft.CodeAnalysis.Location FullLocation => Symbol.Locations.FirstOrDefault(); public virtual IEnumerable Locations { @@ -78,7 +78,7 @@ namespace Semmle.Extraction.CSharp.Entities if (loc != null) { // Some built in operators lack locations, so loc is null. - yield return Context.Create(ReportingLocation); + yield return Context.CreateLocation(ReportingLocation); if (Context.Extractor.OutputPath != null && loc.Kind == LocationKind.SourceFile) yield return Assembly.CreateOutputAssembly(Context); } @@ -91,11 +91,11 @@ namespace Semmle.Extraction.CSharp.Entities /// protected void BindComments() { - if (!symbol.IsImplicitlyDeclared && IsSourceDeclaration && symbol.FromSource()) + if (!Symbol.IsImplicitlyDeclared && IsSourceDeclaration && Symbol.FromSource()) Context.BindComments(this, FullLocation); } - protected virtual T BodyDeclaringSymbol => symbol; + protected virtual T BodyDeclaringSymbol => Symbol; public BlockSyntax Block { @@ -120,11 +120,11 @@ namespace Semmle.Extraction.CSharp.Entities } } - public virtual bool IsSourceDeclaration => symbol.IsSourceDeclaration(); + public virtual bool IsSourceDeclaration => Symbol.IsSourceDeclaration(); - public override bool NeedsPopulation => Context.Defines(symbol); + public override bool NeedsPopulation => Context.Defines(Symbol); - public Extraction.Entities.Location Location => Context.Create(ReportingLocation); + public Extraction.Entities.Location Location => Context.CreateLocation(ReportingLocation); protected void PopulateMetadataHandle(TextWriter trapFile) { @@ -143,15 +143,15 @@ namespace Semmle.Extraction.CSharp.Entities { get { - var handleProp = GetPropertyInfo(symbol, "Handle"); - object handleObj = symbol; + var handleProp = GetPropertyInfo(Symbol, "Handle"); + object handleObj = Symbol; if (handleProp is null) { - var underlyingSymbolProp = GetPropertyInfo(symbol, "UnderlyingSymbol"); + var underlyingSymbolProp = GetPropertyInfo(Symbol, "UnderlyingSymbol"); if (underlyingSymbolProp is object) { - if (underlyingSymbolProp.GetValue(symbol) is object underlying) + if (underlyingSymbolProp.GetValue(Symbol) is object underlying) { handleProp = GetPropertyInfo(underlying, "Handle"); handleObj = underlying; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/TypeMention.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/TypeMention.cs index 8d6074f3e8d..e2fd72eae56 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/TypeMention.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/TypeMention.cs @@ -32,7 +32,7 @@ namespace Semmle.Extraction.CSharp.Entities case NullableTypeSyntax nts: // int[]? -> int[] -> int // int? -> int? - return cx.GetTypeInfo(nts.ElementType).Type.IsReferenceType + return Context.GetTypeInfo(nts.ElementType).Type.IsReferenceType ? GetArrayElementType(nts.ElementType) : nts; case PointerTypeSyntax pts: @@ -48,8 +48,8 @@ namespace Semmle.Extraction.CSharp.Entities { case ArrayType at: return GetArrayElementType(at.ElementType); - case NamedType nt when nt.symbol.IsBoundSpan() || - nt.symbol.IsBoundReadOnlySpan(): + case NamedType nt when nt.Symbol.IsBoundSpan() || + nt.Symbol.IsBoundReadOnlySpan(): return nt.TypeArguments.Single(); case PointerType pt: return GetArrayElementType(pt.PointedAtType); @@ -65,48 +65,48 @@ namespace Semmle.Extraction.CSharp.Entities case SyntaxKind.ArrayType: case SyntaxKind.PointerType: Emit(trapFile, loc ?? syntax.GetLocation(), parent, type); - Create(cx, GetArrayElementType(syntax), this, GetArrayElementType(type)); + Create(Context, GetArrayElementType(syntax), this, GetArrayElementType(type)); return; case SyntaxKind.NullableType: var nts = (NullableTypeSyntax)syntax; if (type is NamedType nt) { - if (!nt.symbol.IsReferenceType) + if (!nt.Symbol.IsReferenceType) { Emit(trapFile, loc ?? syntax.GetLocation(), parent, type); - Create(cx, nts.ElementType, this, nt.TypeArguments[0]); + Create(Context, nts.ElementType, this, nt.TypeArguments[0]); } else { - Create(cx, nts.ElementType, parent, type); + Create(Context, nts.ElementType, parent, type); } } else if (type is ArrayType) { - Create(cx, nts.ElementType, parent, type); + Create(Context, nts.ElementType, parent, type); } return; case SyntaxKind.TupleType: var tts = (TupleTypeSyntax)syntax; var tt = (TupleType)type; Emit(trapFile, loc ?? syntax.GetLocation(), parent, type); - tts.Elements.Zip(tt.TupleElements, (s, t) => Create(cx, s.Type, this, t.Type)).Enumerate(); + tts.Elements.Zip(tt.TupleElements, (s, t) => Create(Context, s.Type, this, t.Type)).Enumerate(); return; case SyntaxKind.GenericName: Emit(trapFile, loc ?? syntax.GetLocation(), parent, type); - cx.PopulateLater(() => + Context.PopulateLater(() => ((GenericNameSyntax)syntax) .TypeArgumentList .Arguments - .Zip(type.TypeMentions, (s, t) => Create(cx, s, this, t)).Enumerate()); + .Zip(type.TypeMentions, (s, t) => Create(Context, s, this, t)).Enumerate()); return; case SyntaxKind.QualifiedName: var qns = (QualifiedNameSyntax)syntax; - var right = Create(cx, qns.Right, parent, type); + var right = Create(Context, qns.Right, parent, type); if (type.ContainingType is object) { // Type qualifier - Create(cx, qns.Left, right, type.ContainingType); + Create(Context, qns.Left, right, type.ContainingType); } return; default: @@ -118,7 +118,7 @@ namespace Semmle.Extraction.CSharp.Entities private void Emit(TextWriter trapFile, Microsoft.CodeAnalysis.Location loc, IEntity parent, Type type) { trapFile.type_mention(this, type.TypeRef, parent); - trapFile.type_mention_location(this, cx.Create(loc)); + trapFile.type_mention_location(this, Context.CreateLocation(loc)); } public static TypeMention Create(Context cx, TypeSyntax syntax, IEntity parent, Type type, Microsoft.CodeAnalysis.Location loc = null) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/ArrayType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/ArrayType.cs index 4ec1db558e5..62a10396b4b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/ArrayType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/ArrayType.cs @@ -9,12 +9,12 @@ namespace Semmle.Extraction.CSharp.Entities private ArrayType(Context cx, IArrayTypeSymbol init) : base(cx, init) { - elementLazy = new Lazy(() => Create(cx, symbol.ElementType)); + elementLazy = new Lazy(() => Create(cx, Symbol.ElementType)); } private readonly Lazy elementLazy; - public int Rank => symbol.Rank; + public int Rank => Symbol.Rank; public Type ElementType => elementLazy.Value; @@ -33,7 +33,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { trapFile.WriteSubId(ElementType); - symbol.BuildArraySuffix(trapFile); + Symbol.BuildArraySuffix(trapFile); trapFile.Write(";type"); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/FunctionPointerType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/FunctionPointerType.cs index 1bf68a4c3c2..903de837a73 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/FunctionPointerType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/FunctionPointerType.cs @@ -13,7 +13,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - symbol.BuildTypeId(Context, trapFile, symbol); + Symbol.BuildTypeId(Context, trapFile, Symbol); trapFile.Write(";functionpointertype"); } @@ -21,8 +21,8 @@ namespace Semmle.Extraction.CSharp.Entities public override void Populate(TextWriter trapFile) { - trapFile.function_pointer_calling_conventions(this, (int)symbol.Signature.CallingConvention); - foreach (var (conv, i) in symbol.Signature.UnmanagedCallingConventionTypes.Select((nt, i) => (Create(Context, nt), i))) + trapFile.function_pointer_calling_conventions(this, (int)Symbol.Signature.CallingConvention); + foreach (var (conv, i) in Symbol.Signature.UnmanagedCallingConventionTypes.Select((nt, i) => (Create(Context, nt), i))) { trapFile.has_unmanaged_calling_conventions(this, i, conv.TypeRef); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs index bded5f60c57..4c250e22692 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.CSharp.Entities private NamedType(Context cx, INamedTypeSymbol init, bool constructUnderlyingTupleType) : base(cx, init) { - typeArgumentsLazy = new Lazy(() => symbol.TypeArguments.Select(t => Create(cx, t)).ToArray()); + typeArgumentsLazy = new Lazy(() => Symbol.TypeArguments.Select(t => Create(cx, t)).ToArray()); this.constructUnderlyingTupleType = constructUnderlyingTupleType; } @@ -30,32 +30,32 @@ namespace Semmle.Extraction.CSharp.Entities public static NamedType CreateNamedTypeFromTupleType(Context cx, INamedTypeSymbol type) => UnderlyingTupleTypeFactory.Instance.CreateEntity(cx, (new SymbolEqualityWrapper(type), typeof(TupleType)), type); - public override bool NeedsPopulation => base.NeedsPopulation || symbol.TypeKind == TypeKind.Error; + public override bool NeedsPopulation => base.NeedsPopulation || Symbol.TypeKind == TypeKind.Error; public override void Populate(TextWriter trapFile) { - if (symbol.TypeKind == TypeKind.Error) + if (Symbol.TypeKind == TypeKind.Error) { - Context.Extractor.MissingType(symbol.ToString(), Context.FromSource); + Context.Extractor.MissingType(Symbol.ToString(), Context.FromSource); return; } if (UsesTypeRef) trapFile.typeref_type((NamedTypeRef)TypeRef, this); - if (symbol.IsGenericType) + if (Symbol.IsGenericType) { - if (symbol.IsBoundNullable()) + if (Symbol.IsBoundNullable()) { // An instance of Nullable - trapFile.nullable_underlying_type(this, Create(Context, symbol.TypeArguments[0]).TypeRef); + trapFile.nullable_underlying_type(this, Create(Context, Symbol.TypeArguments[0]).TypeRef); } - else if (symbol.IsReallyUnbound()) + else if (Symbol.IsReallyUnbound()) { - for (var i = 0; i < symbol.TypeParameters.Length; ++i) + for (var i = 0; i < Symbol.TypeParameters.Length; ++i) { - TypeParameter.Create(Context, symbol.TypeParameters[i]); - var param = symbol.TypeParameters[i]; + TypeParameter.Create(Context, Symbol.TypeParameters[i]); + var param = Symbol.TypeParameters[i]; var typeParameter = TypeParameter.Create(Context, param); trapFile.type_parameters(typeParameter, i, this); } @@ -63,11 +63,11 @@ namespace Semmle.Extraction.CSharp.Entities else { var unbound = constructUnderlyingTupleType - ? CreateNamedTypeFromTupleType(Context, symbol.ConstructedFrom) - : Type.Create(Context, symbol.ConstructedFrom); + ? CreateNamedTypeFromTupleType(Context, Symbol.ConstructedFrom) + : Type.Create(Context, Symbol.ConstructedFrom); trapFile.constructed_generic(this, unbound.TypeRef); - for (var i = 0; i < symbol.TypeArguments.Length; ++i) + for (var i = 0; i < Symbol.TypeArguments.Length; ++i) { trapFile.type_arguments(TypeArguments[i].TypeRef, i, this); } @@ -76,19 +76,19 @@ namespace Semmle.Extraction.CSharp.Entities PopulateType(trapFile, constructUnderlyingTupleType); - if (symbol.EnumUnderlyingType != null) + if (Symbol.EnumUnderlyingType != null) { - trapFile.enum_underlying_type(this, Type.Create(Context, symbol.EnumUnderlyingType).TypeRef); + trapFile.enum_underlying_type(this, Type.Create(Context, Symbol.EnumUnderlyingType).TypeRef); } // Class location - if (!symbol.IsGenericType || symbol.IsReallyUnbound()) + if (!Symbol.IsGenericType || Symbol.IsReallyUnbound()) { foreach (var l in Locations) trapFile.type_location(this, l); } - if (symbol.IsAnonymousType) + if (Symbol.IsAnonymousType) { trapFile.anonymous_types(this); } @@ -105,10 +105,10 @@ namespace Semmle.Extraction.CSharp.Entities { get { - foreach (var l in GetLocations(symbol)) - yield return Context.Create(l); + foreach (var l in GetLocations(Symbol)) + yield return Context.CreateLocation(l); - if (Context.Extractor.OutputPath != null && symbol.DeclaringSyntaxReferences.Any()) + if (Context.Extractor.OutputPath != null && Symbol.DeclaringSyntaxReferences.Any()) yield return Assembly.CreateOutputAssembly(Context); } } @@ -124,9 +124,9 @@ namespace Semmle.Extraction.CSharp.Entities ); } - public override Microsoft.CodeAnalysis.Location ReportingLocation => GetLocations(symbol).FirstOrDefault(); + public override Microsoft.CodeAnalysis.Location ReportingLocation => GetLocations(Symbol).FirstOrDefault(); - private bool IsAnonymousType() => symbol.IsAnonymousType || symbol.Name.Contains("__AnonymousType"); + private bool IsAnonymousType() => Symbol.IsAnonymousType || Symbol.Name.Contains("__AnonymousType"); public override void WriteId(TextWriter trapFile) { @@ -136,7 +136,7 @@ namespace Semmle.Extraction.CSharp.Entities } else { - symbol.BuildTypeId(Context, trapFile, symbol, constructUnderlyingTupleType); + Symbol.BuildTypeId(Context, trapFile, Symbol, constructUnderlyingTupleType); trapFile.Write(";type"); } } @@ -167,9 +167,9 @@ namespace Semmle.Extraction.CSharp.Entities // Create typerefs for constructed error types in case they are fully defined elsewhere. // We cannot use `!this.NeedsPopulation` because this would not be stable as it would depend on // the assembly that was being extracted at the time. - private bool UsesTypeRef => symbol.TypeKind == TypeKind.Error || SymbolEqualityComparer.Default.Equals(symbol.OriginalDefinition, symbol); + private bool UsesTypeRef => Symbol.TypeKind == TypeKind.Error || SymbolEqualityComparer.Default.Equals(Symbol.OriginalDefinition, Symbol); - public override Type TypeRef => UsesTypeRef ? (Type)NamedTypeRef.Create(Context, symbol) : this; + public override Type TypeRef => UsesTypeRef ? (Type)NamedTypeRef.Create(Context, Symbol) : this; } internal class NamedTypeRef : Type @@ -203,7 +203,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void Populate(TextWriter trapFile) { - trapFile.typerefs(this, symbol.Name); + trapFile.typerefs(this, Symbol.Name); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Nullability.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Nullability.cs index 106efdd0cde..a0d9e2a1f8c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Nullability.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Nullability.cs @@ -110,10 +110,10 @@ namespace Semmle.Extraction.CSharp.Entities public override void Populate(TextWriter trapFile) { - trapFile.nullability(this, symbol.Annotation); + trapFile.nullability(this, Symbol.Annotation); var i = 0; - foreach (var s in symbol.NullableParameters) + foreach (var s in Symbol.NullableParameters) { trapFile.nullability_parent(Create(Context, s), i, this); i++; @@ -122,7 +122,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - symbol.WriteId(trapFile); + Symbol.WriteId(trapFile); } public static NullabilityEntity Create(Context cx, Nullability init) => NullabilityFactory.Instance.CreateEntity(cx, init, init); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/PointerType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/PointerType.cs index e2fbb8677cc..dd9b749f53e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/PointerType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/PointerType.cs @@ -8,7 +8,7 @@ namespace Semmle.Extraction.CSharp.Entities private PointerType(Context cx, IPointerTypeSymbol init) : base(cx, init) { - PointedAtType = Create(cx, symbol.PointedAtType); + PointedAtType = Create(cx, Symbol.PointedAtType); } public override void WriteId(TextWriter trapFile) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs index 4ddc258f5c7..18321f84d32 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs @@ -24,7 +24,7 @@ namespace Semmle.Extraction.CSharp.Entities private TupleType(Context cx, INamedTypeSymbol init) : base(cx, init) { - tupleElementsLazy = new Lazy(() => symbol.TupleElements.Select(t => Field.Create(cx, t)).ToArray()); + tupleElementsLazy = new Lazy(() => Symbol.TupleElements.Select(t => Field.Create(cx, t)).ToArray()); } // All tuple types are "local types" @@ -32,7 +32,7 @@ namespace Semmle.Extraction.CSharp.Entities public override void WriteId(TextWriter trapFile) { - symbol.BuildTypeId(Context, trapFile, symbol); + Symbol.BuildTypeId(Context, trapFile, Symbol); trapFile.Write(";tuple"); } @@ -41,7 +41,7 @@ namespace Semmle.Extraction.CSharp.Entities PopulateType(trapFile); PopulateGenerics(); - var underlyingType = NamedType.CreateNamedTypeFromTupleType(Context, symbol.TupleUnderlyingType ?? symbol); + var underlyingType = NamedType.CreateNamedTypeFromTupleType(Context, Symbol.TupleUnderlyingType ?? Symbol); trapFile.tuple_underlying_type(this, underlyingType); @@ -52,8 +52,8 @@ namespace Semmle.Extraction.CSharp.Entities // Note: symbol.Locations seems to be very inconsistent // about what locations are available for a tuple type. // Sometimes it's the source code, and sometimes it's empty. - foreach (var l in symbol.Locations) - trapFile.type_location(this, Context.Create(l)); + foreach (var l in Symbol.Locations) + trapFile.type_location(this, Context.CreateLocation(l)); } private readonly Lazy tupleElementsLazy; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs index 8c96a334565..3931c6ef1d8 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs @@ -15,7 +15,7 @@ namespace Semmle.Extraction.CSharp.Entities public override bool NeedsPopulation => - base.NeedsPopulation || symbol.TypeKind == TypeKind.Dynamic || symbol.TypeKind == TypeKind.TypeParameter; + base.NeedsPopulation || Symbol.TypeKind == TypeKind.Dynamic || Symbol.TypeKind == TypeKind.TypeParameter; public static bool ConstructedOrParentIsConstructed(INamedTypeSymbol symbol) { @@ -75,24 +75,24 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.Write("types("); trapFile.WriteColumn(this); trapFile.Write(','); - trapFile.WriteColumn((int)GetClassType(Context, symbol, constructUnderlyingTupleType)); + trapFile.WriteColumn((int)GetClassType(Context, Symbol, constructUnderlyingTupleType)); trapFile.Write(",\""); - symbol.BuildDisplayName(Context, trapFile, constructUnderlyingTupleType); + Symbol.BuildDisplayName(Context, trapFile, constructUnderlyingTupleType); trapFile.WriteLine("\")"); // Visit base types var baseTypes = new List(); - if (symbol.GetNonObjectBaseType(Context) is INamedTypeSymbol @base) + if (Symbol.GetNonObjectBaseType(Context) is INamedTypeSymbol @base) { var baseKey = Create(Context, @base); trapFile.extend(this, baseKey.TypeRef); - if (symbol.TypeKind != TypeKind.Struct) + if (Symbol.TypeKind != TypeKind.Struct) baseTypes.Add(baseKey); } - if (!(base.symbol is IArrayTypeSymbol)) + if (!(base.Symbol is IArrayTypeSymbol)) { - foreach (var t in base.symbol.Interfaces.Select(i => Create(Context, i))) + foreach (var t in base.Symbol.Interfaces.Select(i => Create(Context, i))) { trapFile.implement(this, t.TypeRef); baseTypes.Add(t); @@ -100,17 +100,17 @@ namespace Semmle.Extraction.CSharp.Entities } var containingType = ContainingType; - if (containingType != null && symbol.Kind != SymbolKind.TypeParameter) + if (containingType != null && Symbol.Kind != SymbolKind.TypeParameter) { - var originalDefinition = symbol.TypeKind == TypeKind.Error ? this : Create(Context, symbol.OriginalDefinition); + var originalDefinition = Symbol.TypeKind == TypeKind.Error ? this : Create(Context, Symbol.OriginalDefinition); trapFile.nested_types(this, containingType, originalDefinition); } - else if (symbol.ContainingNamespace != null) + else if (Symbol.ContainingNamespace != null) { - trapFile.parent_namespace(this, Namespace.Create(Context, symbol.ContainingNamespace)); + trapFile.parent_namespace(this, Namespace.Create(Context, Symbol.ContainingNamespace)); } - if (symbol is IArrayTypeSymbol array) + if (Symbol is IArrayTypeSymbol array) { // They are in the namespace of the original object var elementType = array.ElementType; @@ -119,7 +119,7 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.parent_namespace(this, Namespace.Create(Context, ns)); } - if (symbol is IPointerTypeSymbol pointer) + if (Symbol is IPointerTypeSymbol pointer) { var elementType = pointer.PointedAtType; var ns = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace; @@ -128,26 +128,26 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.parent_namespace(this, Namespace.Create(Context, ns)); } - if (symbol.BaseType != null && symbol.BaseType.SpecialType == SpecialType.System_MulticastDelegate) + if (Symbol.BaseType != null && Symbol.BaseType.SpecialType == SpecialType.System_MulticastDelegate) { // This is a delegate. // The method "Invoke" has the return type. - var invokeMethod = ((INamedTypeSymbol)symbol).DelegateInvokeMethod; + var invokeMethod = ((INamedTypeSymbol)Symbol).DelegateInvokeMethod; ExtractParametersForDelegateLikeType(trapFile, invokeMethod, t => trapFile.delegate_return_type(this, t)); } - if (symbol is IFunctionPointerTypeSymbol functionPointer) + if (Symbol is IFunctionPointerTypeSymbol functionPointer) { ExtractParametersForDelegateLikeType(trapFile, functionPointer.Signature, t => trapFile.function_pointer_return_type(this, t)); } - Modifier.ExtractModifiers(Context, trapFile, this, symbol); + Modifier.ExtractModifiers(Context, trapFile, this, Symbol); - if (IsSourceDeclaration && symbol.FromSource()) + if (IsSourceDeclaration && Symbol.FromSource()) { - var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray(); + var declSyntaxReferences = Symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray(); var baseLists = declSyntaxReferences.OfType().Select(c => c.BaseList); baseLists = baseLists.Concat(declSyntaxReferences.OfType().Select(c => c.BaseList)); @@ -157,7 +157,7 @@ namespace Semmle.Extraction.CSharp.Entities .Where(bl => bl != null) .SelectMany(bl => bl.Types) .Zip( - baseTypes.Where(bt => bt.symbol.SpecialType != SpecialType.System_Object), + baseTypes.Where(bt => bt.Symbol.SpecialType != SpecialType.System_Object), (s, t) => TypeMention.Create(Context, s.Type, this, t)) .Enumerate(); } @@ -171,7 +171,7 @@ namespace Semmle.Extraction.CSharp.Entities var originalParam = invokeMethod.OriginalDefinition.Parameters[i]; var originalParamEntity = SymbolEqualityComparer.Default.Equals(param, originalParam) ? null - : DelegateTypeParameter.Create(Context, originalParam, Create(Context, ((INamedTypeSymbol)symbol).OriginalDefinition)); + : DelegateTypeParameter.Create(Context, originalParam, Create(Context, ((INamedTypeSymbol)Symbol).OriginalDefinition)); DelegateTypeParameter.Create(Context, param, this, originalParamEntity); } @@ -187,12 +187,12 @@ namespace Semmle.Extraction.CSharp.Entities /// public void ExtractRecursive() { - foreach (var l in symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax().GetLocation())) + foreach (var l in Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax().GetLocation())) { Context.BindComments(this, l); } - foreach (var member in symbol.GetMembers()) + foreach (var member in Symbol.GetMembers()) { switch (member.Kind) { @@ -211,21 +211,21 @@ namespace Semmle.Extraction.CSharp.Entities /// public void PopulateGenerics() { - if (symbol == null || !NeedsPopulation || !Context.ExtractGenerics(this)) + if (Symbol == null || !NeedsPopulation || !Context.ExtractGenerics(this)) return; var members = new List(); - foreach (var member in symbol.GetMembers()) + foreach (var member in Symbol.GetMembers()) members.Add(member); - foreach (var member in symbol.GetTypeMembers()) + foreach (var member in Symbol.GetTypeMembers()) members.Add(member); // Mono extractor puts all BASE interface members as members of the current interface. - if (symbol.TypeKind == TypeKind.Interface) + if (Symbol.TypeKind == TypeKind.Interface) { - foreach (var baseInterface in symbol.Interfaces) + foreach (var baseInterface in Symbol.Interfaces) { foreach (var member in baseInterface.GetMembers()) members.Add(member); @@ -239,10 +239,10 @@ namespace Semmle.Extraction.CSharp.Entities Context.CreateEntity(member); } - if (symbol.BaseType != null) - Create(Context, symbol.BaseType).PopulateGenerics(); + if (Symbol.BaseType != null) + Create(Context, Symbol.BaseType).PopulateGenerics(); - foreach (var i in symbol.Interfaces) + foreach (var i in Symbol.Interfaces) { Create(Context, i).PopulateGenerics(); } @@ -250,7 +250,7 @@ namespace Semmle.Extraction.CSharp.Entities public void ExtractRecursive(TextWriter trapFile, IEntity parent) { - if (symbol.ContainingSymbol.Kind == SymbolKind.Namespace && !symbol.ContainingNamespace.IsGlobalNamespace) + if (Symbol.ContainingSymbol.Kind == SymbolKind.Namespace && !Symbol.ContainingNamespace.IsGlobalNamespace) { trapFile.parent_namespace_declaration(this, (NamespaceDeclaration)parent); } @@ -315,10 +315,10 @@ namespace Semmle.Extraction.CSharp.Entities public override bool Equals(object obj) { var other = obj as Type; - return other?.GetType() == GetType() && SymbolEqualityComparer.Default.Equals(other.symbol, symbol); + return other?.GetType() == GetType() && SymbolEqualityComparer.Default.Equals(other.Symbol, Symbol); } - public override int GetHashCode() => SymbolEqualityComparer.Default.GetHashCode(symbol); + public override int GetHashCode() => SymbolEqualityComparer.Default.GetHashCode(Symbol); } internal abstract class Type : Type where T : ITypeSymbol @@ -326,6 +326,7 @@ namespace Semmle.Extraction.CSharp.Entities protected Type(Context cx, T init) : base(cx, init) { } - public new T symbol => (T)base.symbol; + // todo: change this with .net 5 to be an override + public new T Symbol => (T)base.Symbol; } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs index b4d35f7b506..fc8ac7e39d8 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs @@ -24,22 +24,22 @@ namespace Semmle.Extraction.CSharp.Entities var constraints = new TypeParameterConstraints(Context); trapFile.type_parameter_constraints(constraints, this); - if (symbol.HasReferenceTypeConstraint) + if (Symbol.HasReferenceTypeConstraint) trapFile.general_type_parameter_constraints(constraints, 1); - if (symbol.HasValueTypeConstraint) + if (Symbol.HasValueTypeConstraint) trapFile.general_type_parameter_constraints(constraints, 2); - if (symbol.HasConstructorConstraint) + if (Symbol.HasConstructorConstraint) trapFile.general_type_parameter_constraints(constraints, 3); - if (symbol.HasUnmanagedTypeConstraint) + if (Symbol.HasUnmanagedTypeConstraint) trapFile.general_type_parameter_constraints(constraints, 4); - if (symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated) + if (Symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated) trapFile.general_type_parameter_constraints(constraints, 5); - foreach (var abase in symbol.GetAnnotatedTypeConstraints()) + foreach (var abase in Symbol.GetAnnotatedTypeConstraints()) { var t = Create(Context, abase.Symbol); trapFile.specific_type_parameter_constraints(constraints, t.TypeRef); @@ -47,19 +47,19 @@ namespace Semmle.Extraction.CSharp.Entities trapFile.specific_type_parameter_nullability(constraints, t.TypeRef, NullabilityEntity.Create(Context, Nullability.Create(abase))); } - trapFile.types(this, Kinds.TypeKind.TYPE_PARAMETER, symbol.Name); + trapFile.types(this, Kinds.TypeKind.TYPE_PARAMETER, Symbol.Name); - var parentNs = Namespace.Create(Context, symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : symbol.ContainingNamespace); + var parentNs = Namespace.Create(Context, Symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : Symbol.ContainingNamespace); trapFile.parent_namespace(this, parentNs); - foreach (var l in symbol.Locations) + foreach (var l in Symbol.Locations) { - trapFile.type_location(this, Context.Create(l)); + trapFile.type_location(this, Context.CreateLocation(l)); } if (IsSourceDeclaration) { - var declSyntaxReferences = symbol.DeclaringSyntaxReferences + var declSyntaxReferences = Symbol.DeclaringSyntaxReferences .Select(d => d.GetSyntax()) .Select(s => s.Parent) .Where(p => p != null) @@ -69,7 +69,7 @@ namespace Semmle.Extraction.CSharp.Entities clauses = clauses.Concat(declSyntaxReferences.OfType().SelectMany(c => c.ConstraintClauses)); clauses = clauses.Concat(declSyntaxReferences.OfType().SelectMany(c => c.ConstraintClauses)); clauses = clauses.Concat(declSyntaxReferences.OfType().SelectMany(c => c.ConstraintClauses)); - foreach (var clause in clauses.Where(c => c.Name.Identifier.Text == symbol.Name)) + foreach (var clause in clauses.Where(c => c.Name.Identifier.Text == Symbol.Name)) { TypeMention.Create(Context, clause.Name, this, this); foreach (var constraint in clause.Constraints.OfType()) @@ -92,13 +92,13 @@ namespace Semmle.Extraction.CSharp.Entities { get { - switch (symbol.Variance) + switch (Symbol.Variance) { case VarianceKind.None: return Variance.None; case VarianceKind.Out: return Variance.Out; case VarianceKind.In: return Variance.In; default: - throw new InternalError($"Unexpected VarianceKind {symbol.Variance}"); + throw new InternalError($"Unexpected VarianceKind {Symbol.Variance}"); } } } @@ -107,22 +107,22 @@ namespace Semmle.Extraction.CSharp.Entities { string kind; IEntity containingEntity; - switch (symbol.TypeParameterKind) + switch (Symbol.TypeParameterKind) { case TypeParameterKind.Method: kind = "methodtypeparameter"; - containingEntity = Method.Create(Context, (IMethodSymbol)symbol.ContainingSymbol); + containingEntity = Method.Create(Context, (IMethodSymbol)Symbol.ContainingSymbol); break; case TypeParameterKind.Type: kind = "typeparameter"; - containingEntity = Create(Context, symbol.ContainingType); + containingEntity = Create(Context, Symbol.ContainingType); break; default: - throw new InternalError(symbol, $"Unhandled type parameter kind {symbol.TypeParameterKind}"); + throw new InternalError(Symbol, $"Unhandled type parameter kind {Symbol.TypeParameterKind}"); } trapFile.WriteSubId(containingEntity); trapFile.Write('_'); - trapFile.Write(symbol.Ordinal); + trapFile.Write(Symbol.Ordinal); trapFile.Write(';'); trapFile.Write(kind); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/UserOperator.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/UserOperator.cs index d4c6fa457c1..2643363773b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/UserOperator.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/UserOperator.cs @@ -15,10 +15,10 @@ namespace Semmle.Extraction.CSharp.Entities PopulateMethod(trapFile); PopulateModifiers(trapFile); - var returnType = Type.Create(Context, symbol.ReturnType); + var returnType = Type.Create(Context, Symbol.ReturnType); trapFile.operators(this, - symbol.Name, - OperatorSymbol(Context, symbol.Name), + Symbol.Name, + OperatorSymbol(Context, Symbol.Name), ContainingType, returnType.TypeRef, (UserOperator)OriginalDefinition); @@ -28,7 +28,7 @@ namespace Semmle.Extraction.CSharp.Entities if (IsSourceDeclaration) { - var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray(); + var declSyntaxReferences = Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray(); foreach (var declaration in declSyntaxReferences.OfType()) TypeMention.Create(Context, declaration.ReturnType, this, returnType); foreach (var declaration in declSyntaxReferences.OfType()) @@ -38,7 +38,7 @@ namespace Semmle.Extraction.CSharp.Entities ContainingType.PopulateGenerics(); } - public override bool NeedsPopulation => Context.Defines(symbol) || IsImplicitOperator(out _); + public override bool NeedsPopulation => Context.Defines(Symbol) || IsImplicitOperator(out _); public override Type ContainingType { @@ -57,22 +57,22 @@ namespace Semmle.Extraction.CSharp.Entities /// private bool IsImplicitOperator(out ITypeSymbol containingType) { - containingType = symbol.ContainingType; + containingType = Symbol.ContainingType; if (containingType != null) { var containingNamedType = containingType as INamedTypeSymbol; return containingNamedType == null || - !containingNamedType.GetMembers(symbol.Name).Contains(symbol); + !containingNamedType.GetMembers(Symbol.Name).Contains(Symbol); } - var pointerType = symbol.Parameters.Select(p => p.Type).OfType().FirstOrDefault(); + var pointerType = Symbol.Parameters.Select(p => p.Type).OfType().FirstOrDefault(); if (pointerType != null) { containingType = pointerType; return true; } - Context.ModelError(symbol, "Unexpected implicit operator"); + Context.ModelError(Symbol, "Unexpected implicit operator"); return true; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/UsingDirective.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/UsingDirective.cs index caa12ab0082..96ce5f339cf 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/UsingDirective.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/UsingDirective.cs @@ -21,30 +21,30 @@ namespace Semmle.Extraction.CSharp.Entities protected override void Populate(TextWriter trapFile) { - var info = cx.GetModel(node).GetSymbolInfo(node.Name); + var info = Context.GetModel(node).GetSymbolInfo(node.Name); if (node.StaticKeyword.Kind() == SyntaxKind.None) { // A normal using if (info.Symbol is INamespaceSymbol namespaceSymbol) { - var ns = Namespace.Create(cx, namespaceSymbol); + var ns = Namespace.Create(Context, namespaceSymbol); trapFile.using_namespace_directives(this, ns); - trapFile.using_directive_location(this, cx.Create(ReportingLocation)); + trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation)); } else { - cx.Extractor.MissingNamespace(node.Name.ToFullString(), cx.FromSource); - cx.ModelError(node, "Namespace not found"); + Context.Extractor.MissingNamespace(node.Name.ToFullString(), Context.FromSource); + Context.ModelError(node, "Namespace not found"); return; } } else { // A "using static" - var m = Type.Create(cx, (ITypeSymbol)info.Symbol); + var m = Type.Create(Context, (ITypeSymbol)info.Symbol); trapFile.using_static_directives(this, m.TypeRef); - trapFile.using_directive_location(this, cx.Create(ReportingLocation)); + trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation)); } if (parent != null) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Kinds/ExprKind.cs b/csharp/extractor/Semmle.Extraction.CSharp/Kinds/ExprKind.cs index 292e8c47d98..de48df1dc71 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Kinds/ExprKind.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Kinds/ExprKind.cs @@ -124,7 +124,7 @@ namespace Semmle.Extraction.Kinds AND_PATTERN = 127, OR_PATTERN = 128, FUNCTION_POINTER_INVOCATION = 129, - + WITH = 130, DEFINE_SYMBOL = 999 } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/AstLineCounter.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/AstLineCounter.cs new file mode 100644 index 00000000000..a185675ee27 --- /dev/null +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/AstLineCounter.cs @@ -0,0 +1,48 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Semmle.Util; + +namespace Semmle.Extraction.CSharp.Populators +{ + internal class AstLineCounter : CSharpSyntaxVisitor + { + public override LineCounts DefaultVisit(SyntaxNode node) + { + var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString(); + return LineCounter.ComputeLineCounts(text); + } + + public override LineCounts VisitMethodDeclaration(MethodDeclarationSyntax method) + { + return Visit(method.Identifier, method.Body ?? (SyntaxNode)method.ExpressionBody); + } + + public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body) + { + var start = identifier.GetLocation().SourceSpan.Start; + var end = body.GetLocation().SourceSpan.End - 1; + + var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start); + + var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n"; + return LineCounter.ComputeLineCounts(text); + } + + public override LineCounts VisitConstructorDeclaration(ConstructorDeclarationSyntax method) + { + return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody); + } + + public override LineCounts VisitDestructorDeclaration(DestructorDeclarationSyntax method) + { + return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody); + } + + public override LineCounts VisitOperatorDeclaration(OperatorDeclarationSyntax node) + { + return Visit(node.OperatorToken, node.Body ?? (SyntaxNode)node.ExpressionBody); + } + } + +} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/CompilationUnitVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/CompilationUnitVisitor.cs index bebdee933cd..58e5404ccd7 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/CompilationUnitVisitor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/CompilationUnitVisitor.cs @@ -2,6 +2,9 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Semmle.Util.Logging; +using Semmle.Extraction.CSharp.Entities; +using Semmle.Extraction.CSharp.Entities.Statements; +using System.Linq; namespace Semmle.Extraction.CSharp.Populators { @@ -13,30 +16,57 @@ namespace Semmle.Extraction.CSharp.Populators public override void VisitExternAliasDirective(ExternAliasDirectiveSyntax node) { // This information is not yet extracted. - cx.ExtractionError("Not implemented extern alias directive", node.ToFullString(), Extraction.Entities.Location.Create(cx, node.GetLocation()), "", Severity.Info); + Cx.ExtractionError("Not implemented extern alias directive", node.ToFullString(), Cx.CreateLocation(node.GetLocation()), "", Severity.Info); } public override void VisitCompilationUnit(CompilationUnitSyntax compilationUnit) { foreach (var m in compilationUnit.ChildNodes()) { - cx.Try(m, null, () => ((CSharpSyntaxNode)m).Accept(this)); + Cx.Try(m, null, () => ((CSharpSyntaxNode)m).Accept(this)); } + ExtractGlobalStatements(compilationUnit); + // Gather comments: foreach (var trivia in compilationUnit.DescendantTrivia(compilationUnit.Span, descendIntoTrivia: true)) { - CommentPopulator.ExtractComment(cx, trivia); + CommentPopulator.ExtractComment(Cx, trivia); } foreach (var trivia in compilationUnit.GetLeadingTrivia()) { - CommentPopulator.ExtractComment(cx, trivia); + CommentPopulator.ExtractComment(Cx, trivia); } foreach (var trivia in compilationUnit.GetTrailingTrivia()) { - CommentPopulator.ExtractComment(cx, trivia); + CommentPopulator.ExtractComment(Cx, trivia); + } + } + + private void ExtractGlobalStatements(CompilationUnitSyntax compilationUnit) + { + var globalStatements = compilationUnit + .ChildNodes() + .OfType() + .ToList(); + + if (!globalStatements.Any()) + { + return; + } + + var entryPoint = Cx.Compilation.GetEntryPoint(System.Threading.CancellationToken.None); + var entryMethod = Method.Create(Cx, entryPoint); + var block = GlobalStatementsBlock.Create(Cx, entryMethod); + + for (var i = 0; i < globalStatements.Count; i++) + { + if (globalStatements[i].Statement is object) + { + Statement.Create(Cx, globalStatements[i].Statement, block, i); + } } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/DirectiveVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/DirectiveVisitor.cs index 9ec8d095290..0c400728554 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/DirectiveVisitor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/DirectiveVisitor.cs @@ -67,7 +67,7 @@ namespace Semmle.Extraction.CSharp.Populators if (regionStarts.Count == 0) { cx.ExtractionError("Couldn't find start region", null, - Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning); + cx.CreateLocation(node.GetLocation()), null, Util.Logging.Severity.Warning); return; } @@ -100,7 +100,7 @@ namespace Semmle.Extraction.CSharp.Populators if (ifStarts.Count == 0) { cx.ExtractionError("Couldn't find start if", null, - Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning); + cx.CreateLocation(node.GetLocation()), null, Util.Logging.Severity.Warning); return; } @@ -113,7 +113,7 @@ namespace Semmle.Extraction.CSharp.Populators if (ifStarts.Count == 0) { cx.ExtractionError("Couldn't find start if", null, - Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning); + cx.CreateLocation(node.GetLocation()), null, Util.Logging.Severity.Warning); return; } @@ -126,7 +126,7 @@ namespace Semmle.Extraction.CSharp.Populators if (ifStarts.Count == 0) { cx.ExtractionError("Couldn't find start if", null, - Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning); + cx.CreateLocation(node.GetLocation()), null, Util.Logging.Severity.Warning); return; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Methods.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Methods.cs deleted file mode 100644 index 148160b8815..00000000000 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Methods.cs +++ /dev/null @@ -1,65 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Semmle.Util; -using System.IO; - -namespace Semmle.Extraction.CSharp.Populators -{ - public static class MethodExtensions - { - private class AstLineCounter : CSharpSyntaxVisitor - { - public override LineCounts DefaultVisit(SyntaxNode node) - { - var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString(); - return Semmle.Util.LineCounter.ComputeLineCounts(text); - } - - public override LineCounts VisitMethodDeclaration(MethodDeclarationSyntax method) - { - return Visit(method.Identifier, method.Body ?? (SyntaxNode)method.ExpressionBody); - } - - public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body) - { - var start = identifier.GetLocation().SourceSpan.Start; - var end = body.GetLocation().SourceSpan.End - 1; - - var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start); - - var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n"; - return Semmle.Util.LineCounter.ComputeLineCounts(text); - } - - public override LineCounts VisitConstructorDeclaration(ConstructorDeclarationSyntax method) - { - return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody); - } - - public override LineCounts VisitDestructorDeclaration(DestructorDeclarationSyntax method) - { - return Visit(method.Identifier, (SyntaxNode)method.Body ?? method.ExpressionBody); - } - - public override LineCounts VisitOperatorDeclaration(OperatorDeclarationSyntax node) - { - return Visit(node.OperatorToken, node.Body ?? (SyntaxNode)node.ExpressionBody); - } - } - - public static void NumberOfLines(this Context cx, TextWriter trapFile, ISymbol symbol, IEntity callable) - { - foreach (var decl in symbol.DeclaringSyntaxReferences) - { - cx.NumberOfLines(trapFile, (CSharpSyntaxNode)decl.GetSyntax(), callable); - } - } - - public static void NumberOfLines(this Context cx, TextWriter trapFile, CSharpSyntaxNode node, IEntity callable) - { - var lineCounts = node.Accept(new AstLineCounter()); - trapFile.numlines(callable, lineCounts); - } - } -} diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs index 6e5b45e9e3a..ededf2978b9 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs @@ -11,16 +11,17 @@ namespace Semmle.Extraction.CSharp.Populators { public class TypeContainerVisitor : CSharpSyntaxVisitor { - protected Context cx { get; } - protected IEntity parent { get; } - protected TextWriter trapFile { get; } + protected Context Cx { get; } + protected IEntity Parent { get; } + protected TextWriter TrapFile { get; } private readonly Lazy> attributeLookup; public TypeContainerVisitor(Context cx, TextWriter trapFile, IEntity parent) { - this.cx = cx; - this.parent = parent; - this.trapFile = trapFile; + Cx = cx; + Parent = parent; + TrapFile = trapFile; + attributeLookup = new Lazy>(() => { var dict = new Dictionary(); @@ -38,48 +39,59 @@ namespace Semmle.Extraction.CSharp.Populators throw new InternalError(node, "Unhandled top-level syntax node"); } + public override void VisitGlobalStatement(GlobalStatementSyntax node) + { + // Intentionally left empty. + // Global statements are handled in CompilationUnitVisitor + } + public override void VisitDelegateDeclaration(DelegateDeclarationSyntax node) { - Entities.NamedType.Create(cx, cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(trapFile, parent); + Entities.NamedType.Create(Cx, Cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(TrapFile, Parent); } public override void VisitRecordDeclaration(RecordDeclarationSyntax node) { - Entities.Type.Create(cx, cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(trapFile, parent); + ExtractTypeDeclaration(node); } - public override void VisitClassDeclaration(ClassDeclarationSyntax classDecl) + public override void VisitClassDeclaration(ClassDeclarationSyntax node) { - Entities.Type.Create(cx, cx.GetModel(classDecl).GetDeclaredSymbol(classDecl)).ExtractRecursive(trapFile, parent); + ExtractTypeDeclaration(node); } public override void VisitStructDeclaration(StructDeclarationSyntax node) { - Entities.Type.Create(cx, cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(trapFile, parent); + ExtractTypeDeclaration(node); } public override void VisitEnumDeclaration(EnumDeclarationSyntax node) { - Entities.Type.Create(cx, cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(trapFile, parent); + ExtractTypeDeclaration(node); } public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node) { - Entities.Type.Create(cx, cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(trapFile, parent); + ExtractTypeDeclaration(node); + } + + private void ExtractTypeDeclaration(BaseTypeDeclarationSyntax node) + { + Entities.Type.Create(Cx, Cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(TrapFile, Parent); } public override void VisitAttributeList(AttributeListSyntax node) { - if (cx.Extractor.Standalone) + if (Cx.Extractor.Standalone) return; - var outputAssembly = Assembly.CreateOutputAssembly(cx); + var outputAssembly = Assembly.CreateOutputAssembly(Cx); foreach (var attribute in node.Attributes) { if (attributeLookup.Value(attribute) is AttributeData attributeData) { - var ae = Semmle.Extraction.CSharp.Entities.Attribute.Create(cx, attributeData, outputAssembly); - cx.BindComments(ae, attribute.GetLocation()); + var ae = Entities.Attribute.Create(Cx, attributeData, outputAssembly); + Cx.BindComments(ae, attribute.GetLocation()); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeOrNamespaceVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeOrNamespaceVisitor.cs index aacdc5d2a4f..a3daae08a60 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeOrNamespaceVisitor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeOrNamespaceVisitor.cs @@ -13,12 +13,12 @@ namespace Semmle.Extraction.CSharp.Populators { // Only deal with "using namespace" not "using X = Y" if (usingDirective.Alias == null) - new UsingDirective(cx, usingDirective, (NamespaceDeclaration)parent); + new UsingDirective(Cx, usingDirective, (NamespaceDeclaration)Parent); } public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) { - NamespaceDeclaration.Create(cx, node, (NamespaceDeclaration)parent); + NamespaceDeclaration.Create(Cx, node, (NamespaceDeclaration)Parent); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs b/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs index ec2bd66d32e..df39d6a836c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs @@ -53,6 +53,17 @@ namespace Semmle.Extraction.CSharp trapFile.WriteTuple("catch_type", @catch, type, explicityCaught ? 1 : 2); } + internal static void foreach_stmt_info(this TextWriter trapFile, Entities.Statements.ForEach @foreach, bool isAsync) + { + trapFile.WriteTuple("foreach_stmt_info", @foreach, isAsync ? 2 : 1); + } + + internal static void foreach_stmt_desugar(this TextWriter trapFile, Entities.Statements.ForEach @foreach, IEntity entity, + Entities.Statements.ForEach.ForeachSymbolType type) + { + trapFile.WriteTuple("foreach_stmt_desugar", @foreach, entity, (int)type); + } + internal static void commentblock(this TextWriter trapFile, CommentBlock k) { trapFile.WriteTuple("commentblock", k); diff --git a/csharp/extractor/Semmle.Extraction/AssemblyScope.cs b/csharp/extractor/Semmle.Extraction/AssemblyScope.cs index 84c117d17c4..27c9377bb30 100644 --- a/csharp/extractor/Semmle.Extraction/AssemblyScope.cs +++ b/csharp/extractor/Semmle.Extraction/AssemblyScope.cs @@ -21,7 +21,5 @@ namespace Semmle.Extraction public bool InScope(ISymbol symbol) => SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assembly) || SymbolEqualityComparer.Default.Equals(symbol, assembly); - - public bool FromSource => false; } } diff --git a/csharp/extractor/Semmle.Extraction/Context.cs b/csharp/extractor/Semmle.Extraction/Context.cs index e6b4826f2ef..a4b84d5d047 100644 --- a/csharp/extractor/Semmle.Extraction/Context.cs +++ b/csharp/extractor/Semmle.Extraction/Context.cs @@ -18,7 +18,7 @@ namespace Semmle.Extraction /// /// Access various extraction functions, e.g. logger, trap writer. /// - public IExtractor Extractor { get; } + public Extractor Extractor { get; } /// /// The program database provided by Roslyn. @@ -51,7 +51,7 @@ namespace Semmle.Extraction // A recursion guard against writing to the trap file whilst writing an id to the trap file. private bool writingLabel = false; - public void DefineLabel(IEntity entity, TextWriter trapFile, IExtractor extractor) + public void DefineLabel(IEntity entity, TextWriter trapFile, Extractor extractor) { if (writingLabel) { @@ -73,11 +73,11 @@ namespace Semmle.Extraction } #if DEBUG_LABELS - private void CheckEntityHasUniqueLabel(string id, ICachedEntity entity) + private void CheckEntityHasUniqueLabel(string id, CachedEntity entity) { if (idLabelCache.ContainsKey(id)) { - this.Extractor.Message(new Message("Label collision for " + id, entity.Label.ToString(), Entities.Location.Create(this, entity.ReportingLocation), "", Severity.Warning)); + this.Extractor.Message(new Message("Label collision for " + id, entity.Label.ToString(), CreateLocation(entity.ReportingLocation), "", Severity.Warning)); } else { @@ -89,12 +89,12 @@ namespace Semmle.Extraction public Label GetNewLabel() => new Label(GetNewId()); public TEntity CreateEntity(ICachedEntityFactory factory, object cacheKey, TInit init) - where TEntity : ICachedEntity => + where TEntity : CachedEntity => cacheKey is ISymbol s ? CreateEntity(factory, s, init, symbolEntityCache) : CreateEntity(factory, cacheKey, init, objectEntityCache); public TEntity CreateEntityFromSymbol(ICachedEntityFactory factory, TSymbol init) where TSymbol : ISymbol - where TEntity : ICachedEntity => CreateEntity(factory, init, init, symbolEntityCache); + where TEntity : CachedEntity => CreateEntity(factory, init, init, symbolEntityCache); /// /// Creates and populates a new entity, or returns the existing one from the cache. @@ -104,9 +104,9 @@ namespace Semmle.Extraction /// The initializer for the entity. /// The dictionary to use for caching. /// The new/existing entity. - private TEntity CreateEntity(ICachedEntityFactory factory, TCacheKey cacheKey, TInit init, IDictionary dictionary) + private TEntity CreateEntity(ICachedEntityFactory factory, TCacheKey cacheKey, TInit init, IDictionary dictionary) where TCacheKey : notnull - where TEntity : ICachedEntity + where TEntity : CachedEntity { if (dictionary.TryGetValue(cacheKey, out var cached)) return (TEntity)cached; @@ -143,7 +143,7 @@ namespace Semmle.Extraction /// /// The entity to extract. /// True only on the first call for a particular entity. - public bool ExtractGenerics(ICachedEntity entity) + public bool ExtractGenerics(CachedEntity entity) { if (extractedGenerics.Contains(entity.Label)) { @@ -158,18 +158,18 @@ namespace Semmle.Extraction /// Creates a fresh label with ID "*", and set it on the /// supplied object. /// - public void AddFreshLabel(IEntity entity) + public void AddFreshLabel(Entity entity) { entity.Label = GetNewLabel(); entity.DefineFreshLabel(TrapWriter.Writer); } #if DEBUG_LABELS - private readonly Dictionary idLabelCache = new Dictionary(); + private readonly Dictionary idLabelCache = new Dictionary(); #endif - private readonly IDictionary objectEntityCache = new Dictionary(); - private readonly IDictionary symbolEntityCache = new Dictionary(10000, SymbolEqualityComparer.Default); + private readonly IDictionary objectEntityCache = new Dictionary(); + private readonly IDictionary symbolEntityCache = new Dictionary(10000, SymbolEqualityComparer.Default); private readonly HashSet