|
| 1 | +/** |
| 2 | + * Provides CIL-specific implementations of the Callable framework. |
| 3 | + * This module bridges CIL instructions and methods to the abstract Callable interface. |
| 4 | + */ |
| 5 | + |
| 6 | +private import binary |
| 7 | +private import semmle.code.binary.ast.internal.CilInstructions |
| 8 | + |
| 9 | +/** |
| 10 | + * A CIL type (class, struct, interface, etc.). |
| 11 | + */ |
| 12 | +class CilType extends @type { |
| 13 | + string toString() { result = this.getName() } |
| 14 | + |
| 15 | + /** Gets the full name of this type (e.g., "System.Collections.Generic.List`1"). */ |
| 16 | + string getFullName() { types(this, result, _, _) } |
| 17 | + |
| 18 | + /** Gets the namespace of this type (e.g., "System.Collections.Generic"). */ |
| 19 | + string getNamespace() { types(this, _, result, _) } |
| 20 | + |
| 21 | + /** Gets the simple name of this type (e.g., "List`1"). */ |
| 22 | + string getName() { types(this, _, _, result) } |
| 23 | + |
| 24 | + /** Gets a method declared in this type. */ |
| 25 | + CilMethodExt getAMethod() { result.getDeclaringType() = this } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * A CIL method with enhanced metadata for the Callable framework. |
| 30 | + * Extends the base CilMethod with type information and qualified names. |
| 31 | + */ |
| 32 | +class CilMethodExt extends CilMethod { |
| 33 | + /** Gets the type that declares this method. */ |
| 34 | + CilType getDeclaringType() { methods(this, _, _, result) } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets the fully qualified name of this method in the format: |
| 38 | + * "Namespace.ClassName.MethodName" |
| 39 | + */ |
| 40 | + string getFullyQualifiedName() { |
| 41 | + exists(CilType t | t = this.getDeclaringType() | |
| 42 | + result = t.getFullName() + "." + this.getName() |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Holds if this method matches the given namespace, class name, and method name. |
| 48 | + */ |
| 49 | + predicate hasFullyQualifiedName(string namespace, string className, string methodName) { |
| 50 | + exists(CilType t | t = this.getDeclaringType() | |
| 51 | + t.getNamespace() = namespace and |
| 52 | + t.getName() = className and |
| 53 | + this.getName() = methodName |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + /** Holds if this method is publicly accessible. */ |
| 58 | + predicate isPublic() { |
| 59 | + // TODO: Check actual visibility flags when available in dbscheme |
| 60 | + // For now, we can't determine visibility from IL alone |
| 61 | + any() |
| 62 | + } |
| 63 | + |
| 64 | + /** Gets the location of this method. */ |
| 65 | + Location getMethodLocation() { |
| 66 | + result = this.getInstruction(0).getLocation() |
| 67 | + or |
| 68 | + not exists(this.getInstruction(0)) and |
| 69 | + result instanceof EmptyLocation |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * A CIL call instruction with enhanced target resolution. |
| 75 | + */ |
| 76 | +class CilCallExt extends CilCall { |
| 77 | + CilCallExt() { any() } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets the fully qualified name of the call target. |
| 81 | + * This is extracted from il_call_target_unresolved. |
| 82 | + */ |
| 83 | + string getCallTargetFullyQualifiedName() { result = this.getExternalName() } |
| 84 | + |
| 85 | + /** |
| 86 | + * Holds if this call targets a method matching the given fully qualified components. |
| 87 | + * The external name format is "Namespace.ClassName.MethodName" (e.g., "System.Console.WriteLine"). |
| 88 | + */ |
| 89 | + predicate targetsMethod(string namespace, string className, string methodName) { |
| 90 | + exists(string target | target = this.getExternalName() | |
| 91 | + // Format: Namespace.ClassName.MethodName |
| 92 | + // We need to find the last two dots to split namespace, class, and method |
| 93 | + exists(int lastDot, int secondLastDot, string nsAndClass | |
| 94 | + lastDot = max(int i | target.charAt(i) = ".") and |
| 95 | + methodName = target.suffix(lastDot + 1) and |
| 96 | + nsAndClass = target.prefix(lastDot) and |
| 97 | + secondLastDot = max(int i | nsAndClass.charAt(i) = ".") and |
| 98 | + namespace = nsAndClass.prefix(secondLastDot) and |
| 99 | + className = nsAndClass.substring(secondLastDot + 1, nsAndClass.length()) |
| 100 | + ) |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + /** Gets the enclosing method with extended metadata. */ |
| 105 | + CilMethodExt getEnclosingMethodExt() { result = this.getEnclosingMethod() } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Holds if `caller` contains a call to a method identified by `(namespace, className, methodName)`. |
| 110 | + */ |
| 111 | +predicate hasCallTo(CilMethodExt caller, string namespace, string className, string methodName) { |
| 112 | + exists(CilCallExt call | |
| 113 | + call.getEnclosingMethodExt() = caller and |
| 114 | + call.targetsMethod(namespace, className, methodName) |
| 115 | + ) |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Holds if `caller` contains a call to a method with the given fully qualified target name. |
| 120 | + */ |
| 121 | +predicate hasCallToTarget(CilMethodExt caller, string targetFqn) { |
| 122 | + exists(CilCallExt call | |
| 123 | + call.getEnclosingMethodExt() = caller and |
| 124 | + call.getCallTargetFullyQualifiedName() = targetFqn |
| 125 | + ) |
| 126 | +} |
0 commit comments