闽公网安备 35020302035485号
namespace ConsoleApp2
{
internal class Program
{
static Person person = null;
static void Main(string[] args)
{
var age = person.age;
Console.WriteLine(age);
}
}
public class Person
{
public int age;
}
}
由于 person 是一个 null 对象,很显然这段代码会抛异常,那为什么会抛异常呢?要想找原因,需要从最底层的汇编研究起。
---------------- var age = person.age; ---------------- 081D6154 mov ecx,dword ptr ds:[4C41F4Ch] 081D615A mov ecx,dword ptr [ecx+4] 081D615D mov dword ptr [ebp-3Ch],ecx这三句汇编还是很好理解的,4C41F4Ch 存放的是 person 对象, ecx+4 是取 person.age,最后一句就是将 age 放在 ebp-3Ch 栈位置上,接下来我们来看下 null 时的 ecx 到底是多少,截图如下:


到这里原理就搞清楚了,因为 [ecx+4] = [4] 是落在这个 null 区所致, 但是。。。。 大家有没有发现一个问题,对,就是这里的 [ecx+4],因为这里有一个 +4 偏移来取 age 字段,那我能不能在 person 中多定义一些字段,然后取最后一个字段从而从 null 区 冲出去。。。哈哈。
namespace ConsoleApp2
{
internal class Program
{
static Person person = null;
static void Main(string[] args)
{
var str = @"public class Person
{
{0}
}";
var lines = Enumerable.Range(0, 100000).Select(m => $"public int age{m};");
var fields = string.Join("\n", lines);
var txt = str.Replace("{0}", fields);
File.WriteAllText("Person.cs", txt);
Console.WriteLine("person.cs 生成完毕");
}
}
}
代码执行后,Person.cs 就会如期生成,接下来读取 person.age99999 看看有没有奇迹发生,参考代码如下: internal class Program
{
static Person person = null;
static void Main(string[] args)
{
var age = person.age99999;
Console.WriteLine(age);
}
}

internal class Program
{
static Person person = null;
static void Main(string[] args)
{
var age = person.age19999;
Console.WriteLine(age);
}
}
接下来我们将断点放在 var age = person.age19999; 上继续看反汇编代码。------------- var age = person.age19999; ------------- 0804657E mov ecx,dword ptr ds:[49F1F4Ch] 08046584 mov dword ptr [ebp-40h],ecx 08046587 mov ecx,dword ptr [ebp-40h] 0804658A cmp dword ptr [ecx],ecx 0804658C mov ecx,dword ptr [ebp-40h] 0804658F mov ecx,dword ptr [ecx+13880h] 08046595 mov dword ptr [ebp-3Ch],ecx从上面的汇编代码可以看出几点信息。
