mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
C#: Add QL and tests for handles.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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() |
|
||||
51
csharp/ql/test/library-tests/cil/consistency/Handles.ql
Normal file
51
csharp/ql/test/library-tests/cil/consistency/Handles.ql
Normal 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()
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user