C#: Add QL and tests for handles.

This commit is contained in:
calum
2018-11-30 10:56:17 +00:00
parent 0ee209e6a4
commit 2acde22f43
4 changed files with 74 additions and 0 deletions

View File

@@ -82,6 +82,14 @@ class NamedElement extends Element, @dotnet_named_element {
/** Gets a unique string label for this element. */
string getLabel() { none() }
/** Holds if `other` has the same metadata handle in the same assembly. */
predicate matchesHandle(NamedElement other) {
exists(Assembly asm, int handle |
metadata_handle(this, asm, handle) and
metadata_handle(other, asm, handle)
)
}
/**
* Holds if this element was compiled from source code that is also present in the
* database. That is, this element corresponds to another element from source.

View File

@@ -1703,4 +1703,5 @@ cil_attribute_positional_argument(
@metadata_entity = @cil_method | @cil_type | @cil_field | @cil_property | @field | @property |
@callable | @value_or_ref_type | @void_type;
#keyset[entity, location]
metadata_handle(int entity : @metadata_entity ref, int location: @assembly ref, int handle: int ref)

View File

@@ -0,0 +1,14 @@
tooManyMatchingHandles
missingCil
cilLocationViolation
csharpLocationViolation
matchingObjectMethods
| Equals(object) | System.Boolean System.Object.Equals(System.Object) |
| Equals(object, object) | System.Boolean System.Object.Equals(System.Object,System.Object) |
| GetHashCode() | System.Int32 System.Object.GetHashCode() |
| GetType() | System.Type System.Object.GetType() |
| MemberwiseClone() | System.Object System.Object.MemberwiseClone() |
| Object() | System.Void System.Object..ctor() |
| ReferenceEquals(object, object) | System.Boolean System.Object.ReferenceEquals(System.Object,System.Object) |
| ToString() | System.String System.Object.ToString() |
| ~Object() | System.Void System.Object.Finalize() |

View File

@@ -0,0 +1,51 @@
import csharp
import cil
import dotnet
class MetadataEntity extends DotNet::NamedElement, @metadata_entity {
int getHandle() { metadata_handle(this, _, result) }
predicate hasHandle() { exists(getHandle()) }
Assembly getAssembly() { metadata_handle(this, result, _) }
}
query predicate tooManyMatchingHandles(MetadataEntity e) {
count(MetadataEntity e2 | e.matchesHandle(e2))>2
}
query predicate missingCil(Element e) {
(
e instanceof Callable
or
e instanceof Type
or
e instanceof Field
) and
e.fromLibrary() and
e.(MetadataEntity).hasHandle() and
not exists(CIL::Element ce | ce.(MetadataEntity).matchesHandle(e))
}
query predicate cilLocationViolation(CIL::Element e) {
e instanceof MetadataEntity
and
exists(e.getALocation())
and
not e.getALocation() = e.(MetadataEntity).getAssembly()
}
query predicate csharpLocationViolation(Element e) {
e.fromLibrary() and
e.(MetadataEntity).hasHandle() and
not e.getALocation() = e.(MetadataEntity).getAssembly()
}
query predicate matchingObjectMethods(string s1, string s2) {
exists(Callable m1, CIL::Method m2 |
m1.getDeclaringType().getQualifiedName() = "System.Object"
and m1.matchesHandle(m2) and
s1 = m1.toStringWithTypes() and
s2 = m2.toStringWithTypes()
)
}