|
| 1 | +/** |
| 2 | + * Provides classes and predicates for definining flow summaries. |
| 3 | + */ |
| 4 | + |
| 5 | +import csharp |
| 6 | +private import internal.FlowSummaryImpl as Impl |
| 7 | +private import internal.FlowSummarySpecific::Private |
| 8 | +private import internal.DataFlowPublic as DataFlowPublic |
| 9 | +// import all instances below |
| 10 | +private import semmle.code.csharp.dataflow.LibraryTypeDataFlow |
| 11 | + |
| 12 | +class SummarizableCallable = Impl::Public::SummarizableCallable; |
| 13 | + |
| 14 | +/** An unbound method. */ |
| 15 | +class SummarizableMethod extends SummarizableCallable, Method { } |
| 16 | + |
| 17 | +class ContentList = Impl::Public::ContentList; |
| 18 | + |
| 19 | +/** Provides predicates for constructing content lists. */ |
| 20 | +module ContentList { |
| 21 | + import Impl::Public::ContentList |
| 22 | + |
| 23 | + /** Gets the singleton "element content" content list. */ |
| 24 | + ContentList element() { result = singleton(any(DataFlowPublic::ElementContent c)) } |
| 25 | + |
| 26 | + /** Gets a singleton property content list. */ |
| 27 | + ContentList property(Property p) { |
| 28 | + result = |
| 29 | + singleton(any(DataFlowPublic::PropertyContent c | c.getProperty() = p.getSourceDeclaration())) |
| 30 | + } |
| 31 | + |
| 32 | + /** Gets a singleton field content list. */ |
| 33 | + ContentList field(Field f) { |
| 34 | + result = |
| 35 | + singleton(any(DataFlowPublic::FieldContent c | c.getField() = f.getSourceDeclaration())) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class SummaryInput = Impl::Public::SummaryInput; |
| 40 | + |
| 41 | +/** Provides predicates for constructing flow-summary input specifications */ |
| 42 | +module SummaryInput { |
| 43 | + private import semmle.code.csharp.frameworks.system.Collections |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets an input specification that specifies the `i`th parameter as |
| 47 | + * the input. |
| 48 | + */ |
| 49 | + SummaryInput parameter(int i) { result = TParameterSummaryInput(i) } |
| 50 | + |
| 51 | + private predicate isCollectionType(ValueOrRefType t) { |
| 52 | + t.getABaseType*() instanceof SystemCollectionsIEnumerableInterface and |
| 53 | + not t instanceof StringType |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets an input specification that specifies the `i`th parameter as |
| 58 | + * the input. |
| 59 | + * |
| 60 | + * `inputContents` is either empty or a singleton element content list, |
| 61 | + * depending on whether the type of the `i`th parameter of `c` is a |
| 62 | + * collection type. |
| 63 | + */ |
| 64 | + SummaryInput parameter(SummarizableCallable c, int i, ContentList inputContents) { |
| 65 | + result = parameter(i) and |
| 66 | + exists(Parameter p | |
| 67 | + p = c.getParameter(i) and |
| 68 | + if isCollectionType(p.getType()) |
| 69 | + then inputContents = ContentList::element() |
| 70 | + else inputContents = ContentList::empty() |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets an input specification that specifies the implicit `this` parameter |
| 76 | + * as the input. |
| 77 | + */ |
| 78 | + SummaryInput thisParameter() { result = TParameterSummaryInput(-1) } |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets an input specification that specifies output from the delegate at |
| 82 | + * parameter `i` as the input. |
| 83 | + */ |
| 84 | + SummaryInput delegate(int i) { result = TDelegateSummaryInput(i) } |
| 85 | + |
| 86 | + /** |
| 87 | + * Gets an input specification that specifies output from the delegate at |
| 88 | + * parameter `i` as the input. |
| 89 | + * |
| 90 | + * `c` must be a compatible callable, that is, a callable where the `i`th |
| 91 | + * parameter is a delegate. |
| 92 | + */ |
| 93 | + SummaryInput delegate(SummarizableCallable c, int i) { |
| 94 | + result = delegate(i) and |
| 95 | + hasDelegateArgumentPosition(c, i) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class SummaryOutput = Impl::Public::SummaryOutput; |
| 100 | + |
| 101 | +/** Provides predicates for constructing flow-summary output specifications. */ |
| 102 | +module SummaryOutput { |
| 103 | + /** |
| 104 | + * Gets an output specification that specifies the return value from a call as |
| 105 | + * the output. |
| 106 | + */ |
| 107 | + SummaryOutput return() { result = TReturnSummaryOutput() } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets an output specification that specifies the `i`th parameter as the |
| 111 | + * output. |
| 112 | + */ |
| 113 | + SummaryOutput parameter(int i) { result = TParameterSummaryOutput(i) } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets an output specification that specifies the implicit `this` parameter |
| 117 | + * as the output. |
| 118 | + */ |
| 119 | + SummaryOutput thisParameter() { result = TParameterSummaryOutput(-1) } |
| 120 | + |
| 121 | + /** |
| 122 | + * Gets an output specification that specifies parameter `j` of the delegate at |
| 123 | + * parameter `i` as the output. |
| 124 | + */ |
| 125 | + SummaryOutput delegate(int i, int j) { result = TDelegateSummaryOutput(i, j) } |
| 126 | + |
| 127 | + /** |
| 128 | + * Gets an output specification that specifies parameter `j` of the delegate at |
| 129 | + * parameter `i` as the output. |
| 130 | + * |
| 131 | + * `c` must be a compatible callable, that is, a callable where the `i`th |
| 132 | + * parameter is a delegate with a parameter at position `j`. |
| 133 | + */ |
| 134 | + SummaryOutput delegate(SummarizableCallable c, int i, int j) { |
| 135 | + result = TDelegateSummaryOutput(i, j) and |
| 136 | + hasDelegateArgumentPosition2(c, i, j) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +class SummarizedCallable = Impl::Public::SummarizedCallable; |
0 commit comments