当前位置:网站首页>Why does C throw exceptions when accessing null fields?
Why does C throw exceptions when accessing null fields?
2022-06-27 03:47:00 【Dotnet cross platform】
One : background
1. An interesting topic
Recently in to see Hardware exception Related knowledge , Found an interesting null reference exception problem , Take it out and share it with you , For the convenience of narration , Let's start with a problematic code .
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;
}
} because person It's a null object , Obviously this code throws exceptions , So why throw exceptions ? To find out why , You need to start with the lowest level of assembly .
Two : Abnormal principle analysis
1. Look for the answer from the compilation
have access to Visual Studio 2022 Disassembly window for , Observe var age = person.age; What exactly is generated at .
---------------- 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 These three sentences are easy to understand ,4C41F4Ch Deposit is person object , ecx+4 Is to take person.age, The last sentence is to age Put it in ebp-3Ch Stack position , Let's take a look at null At the time of the ecx How much is it , The screenshot is as follows :

As you can see from the diagram , At this time ecx=0000000, If you know windows Virtual memory layout , You should know that in the virtual memory 0~0x0000ffff The scope belongs to null Exclusion zone , Anyone who falls in this area is a visit violation , Draw a picture like this .

The principle will be clear here , because [ecx+4] = [4] It fell on this null Area caused , however .... Have you found a problem , Yes , It's here [ecx+4], Because there's a +4 Offset to get age Field , Can I just person Define more fields in , Then take the last field from null District Rush out ... ha-ha .
2. Can really rush out null District
With this idea , I've decided to Person Definition in class 10w individual age Field , The reference codes are as follows :
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 Generation completed ");
}
}
} After code execution ,Person.cs Will be generated as scheduled , Next read person.age99999 See if miracles happen , The reference codes are as follows :
internal class Program
{
static Person person = null;
static void Main(string[] args)
{
var age = person.age99999;
Console.WriteLine(age);
}
}
I went to , Never in my wildest dreams , hold ClassLoader It broke up .... have to , That can only be changed 20000 individual age Have a try. , The reference codes are as follows :
internal class Program
{
static Person person = null;
static void Main(string[] args)
{
var age = person.age19999;
Console.WriteLine(age);
}
} Next, we put the breakpoint in var age = person.age19999; Continue to look at disassembly code .
------------- 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],ecxFrom the assembly code above, we can see some information .
There are too many lines of assembly code .
ecx+13880h Out null District (FFFF) The boundary of the .
Next, step through the assembly , Found in cmp dword ptr [ecx],ecx An exception was thrown at ...

We all know that at this time ecx The address is 0 , from ecx Fetching content will definitely throw access violations , And this code is weird , Generally speaking cmp After that, it is similar jz,jnz Jump instruction , And it is only a half broken sentence ...
From these characteristics , This is a JIT Deliberately try to judge before taking the offset ecx Is it right? null, The motive is not pure ....
3、 ... and : summary
From these analyses we can see that ,JIT Still very intelligent .
When the offset value falls at
0~FFFFNo entry zone ,JIT No judgment code is generated to reduce the code volume .At the offset value, the
0~FFFFExclusion zone ,JIT Have to generate code to judge .
ha-ha , Is this article very interesting , Hopefully that helped .
边栏推荐
- PAT甲级 1024 Palindromic Number
- ERP demand and sales management Kingdee
- Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
- Overview of Tsinghua & Huawei | semantic communication: Principles and challenges
- iOS开发:对于动态库共享缓存(dyld)的了解
- 发现一款 JSON 可视化工具神器,太爱了!
- Further exploration of handler (I) (the most complete analysis of the core principle of handler)
- Products change the world
- Window 加密壳实现
- 苹果唯一图谱架构常识
猜你喜欢

电商产品如何在知乎上进行推广和打广告?
![[Shangshui Shuo series] day 6](/img/47/7cd44f4e361af53cac7cea9d0d7ecb.png)
[Shangshui Shuo series] day 6
![[数组]BM94 接雨水问题-较难](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
[数组]BM94 接雨水问题-较难

Fplan power planning

Agile development - self use

I found a JSON visualization tool artifact. I love it!

Argo Workflows —— Kubernetes的工作流引擎入门

Yuantou firm offer weekly record 20220627

Kotlin Compose 隐式传参 CompositionLocalProvider

PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
随机推荐
Pat grade a 1026 table tennis
超级详细,2 万字详解,吃透 ES!
文旅夜游|以沉浸式视觉体验激发游客的热情
2021:Zero-shot Visual Question Answering using Knowledge Graphs使用知识图的零次视觉问答
Pat class a 1024 palindromic number
MySQL development environment
Pat grade a 1020 tree Traversals
There are two problems when Nacos calls microservices: 1 Load balancer does not contain an instance for the service 2. Connection refused
LDR6028 手机设备一边充电一边OTG传输数据方案
CVPR2021:Separating Skills and Concepts for Novel Visual Question Answering将技巧与概念分开的新视觉问答
【一起上水硕系列】Day 6
解码苹果手机证书文件方法
SAI钢笔工具如何使用,入门篇
PAT甲级 1023 Have Fun with Numbers
List of best reading materials for machine learning in communication
Is the money invested in financial products guaranteed? Is there no more?
ESP8266
TP5 spreadsheet excel table export
PAT甲级 1020 Tree Traversals
语义化版本 2.0.0