用sscli学习.NET实现

在Blog上记录过两篇和.NET中GC机制相关的学习笔记(这里这里),但都是从概念上泛泛而谈,所学习、参考和引用的资料也都是从概念上进行介绍,并没有涉及到代码层面的实现。由于工作中也用不到,所以没有继续关注下去。

最近则是在学习.NET Framework Library时想要更清楚地了解.NET对象的内存布局,才知道其实微软早就在10年前发布了开源的CLI实现sscli(Shared Source Common Language Infrastructure),代号Rotor。,并且于06年发布了它的2.0版本。尽管在之后这个项目就没再更新(有消息说已经下线),而且微软后续对于.NET Framework的更新使得现在的实现肯定和之前的大相径庭,但对于这个层面的代码来说,光是sscli2.0就够一般的学习者喝一壶,其中VM、JIT Compiler以及更上层的GC机制等实现,也远远还没有到过时的程度。如果非要追最新的实现,可以参考.NET CF 4.0,它现在从CLR层次就是开源的。

对于sscli,关注它的人并不多,毕竟这是底层到直接涉及托管程序运行、管理的框架。平时大家只要知道怎么编写和载入Assembly、让托管程序或网页跑起来,满足老板/PM/甲方的需求就行了,至于个中具体流程,没有必要关心。甚至于说,只要通读《CLR via C#》后清楚.NET各种概念的实现和关系、知道IL一级的实现,再加上了解大部分运行在CLR上层的机制,就能应付90%以上的工作场景。但sscli还是有必要看看:

  • 解决一些疑难杂症,例如奇怪的内存泄露、CPU满载问题。
  • 更清楚地了解原理。就拿刚才提到的Object内存布局问题来说,利用概念层次的讲解文章《浅析.NET中的引用类型和值类型(上)》以及文中提到的《Pro .NET Performance》这本书,结合sscli具体实现才能做到有自信掌握看破这一话题的能力。
  • 满足自己的好奇心……不是开玩笑,例如GC实现中的各种算法、safe point、hijacking什么的,你说知道具体实现又有何用?单纯是想知道而已。尽管如此,我觉得这类“Under the hood”学习应该算是程序员的自我修养,也是进行“写程序的人”和“程序员”之鉴别的标准之一。

下载了sscli2.0的代码之后,会发现除了clr文件夹内的核心实现外,还有.net相关工具集,以及一部分(老的).NET Framework代码,例如HttpRequest、Xml等实现。这些代码中有最近正在学的正则表达式引擎的完整实现,算是意外收获。另外还附送一个完整的jscript引擎。具体内容可以看docs文档。

接着可以参考下面几篇文章进行build和debug:

当然,windbg是不能少的,用来调试具体的托管程序,结合dump出来的信息,查看对应的cli代码,可以从将知识学习从逻辑层面过渡到实际的物理层面。

有关sscli整体学习,我还是不推荐自己抠代码,毕竟类似gc_heap这种类,光声明就好几千行,难以下手。看看大牛们的文章,例如CLR探索系列这个教程。里面从最基本的windbg载入sos调试程序开始,一步步介绍CLR基本概念,从CLR初始化到载入托管exe和dll,再到托管PE格式、GC机制等等,脉络清晰,图文并茂。另外还有一些针对具体问题的blog,例如《深入了解CLR的加载过程》等,结合阅读会事半功倍。

其他一些学习资源:

成为底层达人,就可以像这些大牛一样玩一些有趣的游戏了,比如自己定义新的IL Opcode或者是用托管代码让CLR挂掉之类。至于能不能在物质层面转化这些知识,就要看个人造化,但“天道酬勤”总是没错的。

247 Responses to “用sscli学习.NET实现”

  1. tube说道:

    A colleague in the field told me to check out your website.

  2. chat说道:

    I dont think Ive caught all the angles of this subject the way youve pointed them out. Youre a true star, a rock star man. Youve got so much to say and know so much about the subject that I think you should just teach a class about it

  3. tube说道:

    I saw a similar post on another website but the points were not as well articulated.

  4. hot说道:

    Appreciation for taking the time to discuss this topic, I would love to discover more on this topic. If viable, as you gain expertise, would you object to updating the website with further information? It is tremendously beneficial for me.

  5. live说道:

    I like this weblog very much so much great info .

  6. AndrewPag说道:

    cialis male enhancement tadalafil 20mg cialis dosing

  7. hub说道:

    We absolutely love your blog and find the majority of your post’s to be exactly what I’m looking for. Do you offer guest writers to write content to suit your needs? I wouldn’t mind composing a post or elaborating on a number of the subjects you write about here. Again, awesome weblog!

  8. free说道:

    Thanks , I’ve recently been searching for info about this topic for ages and yours is the best I have discovered so far. But, what concerning the bottom line? Are you certain concerning the source?

  9. AndrewPag说道:

    cialis drug interactions tadalafil cialis 10mg tablets

  10. 아이디판매说道:

    A neighbor of mine encouraged me to take a look at your blog site couple weeks ago, given that we both love similar stuff and I will need to say I am quite impressed.

  11. AndrewPag说道:

    cialis versus viagra cialis pills generic cialis india

  12. Oxygeneo说道:

    Woah this is just an insane amount of information, must of taken ages to compile so thanx so much for just sharing it with all of us. If your ever in any need of related information, just check out my own site!

  13. y2mate说道:

    Hi, possibly i’m being a little off topic here, but I was browsing your site and it looks stimulating. I’m writing a blog and trying to make it look neat, but everytime I touch it I mess something up. Did you design the blog yourself?

  14. Stromectol说道:

    stromectol how much it cost stromectol covid 19 ivermectin price comparison

  15. Stromectol说道:

    can you buy stromectol over the counter https://stromectolgf.com/

  16. Chestersnoto说道:

    https://stromectolgf.online/# stromectol ivermectin tablets

  17. StevenLeads说道:

    https://drwithoutdoctorprescription.com/# legal to buy prescription drugs without prescription

  18. bahis siteleri说道:

    perfect

    thank you for a very good article

  19. Spot on with this write-up, I truly believe this website requirements a lot much more consideration. I’ll probably be once more to read much much more, thanks for that info.

  20. whoah this weblog is great i really like studying your articles. Stay up the great work! You already know, lots of persons are looking round for this information, you can aid them greatly.

  21. RalphAberi说道:

    free samples for cialis generic tadalafil 20mg india

  22. ThomasDyemn说道:

    buy prescription drugs online prescription drugs online without doctor

  23. CameronBoste说道:

    ed meds online without prescription or membership buy prescription drugs without doctor

  24. Sometimes, the sheer magnitude of the information seems overwhelming.

  25. How come you do not have your website viewable in mobile format? cant see anything in my Droid.

  26. Michaeljealp说道:

    viagra online usa mexican viagra

  27. Sildenafil说道:

    best place to buy generic viagra online mexican viagra cheap viagra online
    generic viagra walmart

  28. Sildenafil说道:

    over the counter viagra viagra over the counter viagra over the counter
    viagra from india

  29. Sildenafil说道:

    cost of viagra cost of viagra best place to buy viagra online
    viagra price

  30. Sildenafil说道:

    best place to buy generic viagra online how much is viagra buy viagra online canada
    generic viagra walmart

  31. Sildenafil说道:

    buy viagra online canada best place to buy generic viagra online viagra discount
    buy viagra online usa

  32. Sildenafil说道:

    buying viagra online viagra without a doctor prescription usa cheap viagra online
    viagra over the counter walmart

  33. Sildenafil说道:

    buy generic 100mg viagra online best over the counter viagra viagra from canada
    best place to buy generic viagra online

  34. Sildenafil说道:

    viagra from canada where can i buy viagra over the counter mexican viagra
    viagra over the counter

  35. Sildenafil说道:

    viagra discount viagra over the counter viagra without a doctor prescription usa
    generic viagra walmart

  36. Sildenafil说道:

    buying viagra online viagra 100mg price viagra amazon

  37. MichaelVuT说道:

    https://sildenafilmg.com/ order viagra online

  38. Brandonblona说道:

    over the counter viagra viagra pills

  39. Sildenafil说道:

    where to buy viagra online https://sildenafilmg.online/
    viagra cost

  40. Sildenafil说道:

    how much is viagra viagra 100mg price buy generic 100mg viagra online