Repository: dotnet/runtime
File: src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunHardwareIntrinsicRootProvider.cs (C#)

Code before:
        {
            InstructionSetSupport specifiedInstructionSet = context.InstructionSetSupport;
            TargetArchitecture targetArch = context.Target.Architecture;

            // Hardware intrinsics can only live in the system module.
            foreach (MetadataType type in context.SystemModule.GetAllTypes())
            {
                InstructionSet instructionSet = InstructionSetParser.LookupPlatformIntrinsicInstructionSet(targetArch, type);

                if (instructionSet == InstructionSet.ILLEGAL)
                {
                    // Not a HardwareIntrinsics type for our platform.
                    continue;
                }

                if (specifiedInstructionSet.IsInstructionSetSupported(instructionSet))
                {
                    foreach (MethodDesc method in type.GetMethods())
                    {
                        rootProvider.AddCompilationRoot(method, rootMinimalDependencies: false, "Hardware intrinsic method fallback implementation");
                    }
                }
                else
                {
                    MethodDesc isSupportedMethod = type.GetMethod("get_IsSupported"u8, new MethodSignature(MethodSignatureFlags.Static, 0, context.GetWellKnownType(WellKnownType.Boolean), []));
                    rootProvider.AddCompilationRoot(isSupportedMethod, rootMinimalDependencies: false, "IsSupported getter for unsupported hardware intrinsic");
                }
            }
        }
    }
}


Review comment (suggestion):
The `GetMethod` call can return `null` if the `get_IsSupported` method is not found on the hardware intrinsic type. This null value is then passed to `AddCompilationRoot`, which will cause a `NullReferenceException` when it tries to call `method.GetCanonMethodTarget()`.

Consider adding a null check:
```csharp
MethodDesc isSupportedMethod = type.GetMethod("get_IsSupported"u8, new MethodSignature(MethodSignatureFlags.Static, 0, context.GetWellKnownType(WellKnownType.Boolean), []));
if (isSupportedMethod is not null)
{
    rootProvider.AddCompilationRoot(isSupportedMethod, rootMinimalDependencies: false, "IsSupported getter for unsupported hardware intrinsic");
}
```
```suggestion
                    if (isSupportedMethod is not null)
                    {
                        rootProvider.AddCompilationRoot(isSupportedMethod, rootMinimalDependencies: false, "IsSupported getter for unsupported hardware intrinsic");
                    }
```

Code after:
        {
            InstructionSetSupport specifiedInstructionSet = context.InstructionSetSupport;
            TargetArchitecture targetArch = context.Target.Architecture;

            // Hardware intrinsics can only live in the system module.
            foreach (MetadataType type in context.SystemModule.GetAllTypes())
            {
                InstructionSet instructionSet = InstructionSetParser.LookupPlatformIntrinsicInstructionSet(targetArch, type);

                if (instructionSet == InstructionSet.ILLEGAL)
                {
                    // Not a HardwareIntrinsics type for our platform.
                    continue;
                }

                if (specifiedInstructionSet.IsInstructionSetSupported(instructionSet))
                {
                    foreach (MethodDesc method in type.GetMethods())
                    {
                        rootProvider.AddCompilationRoot(method, rootMinimalDependencies: false, "Hardware intrinsic method fallback implementation");
                    }
                }
                else
                {
                    MethodDesc isSupportedMethod = type.GetMethod("get_IsSupported"u8, new MethodSignature(MethodSignatureFlags.Static, 0, context.GetWellKnownType(WellKnownType.Boolean), []));
                    if (isSupportedMethod is not null)
                    {
                        rootProvider.AddCompilationRoot(isSupportedMethod, rootMinimalDependencies: false, "IsSupported getter for unsupported hardware intrinsic");
                    }
                }
            }
        }
    }
}