惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
S
Secure Thoughts
Stack Overflow Blog
Stack Overflow Blog
G
Google Developers Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
SegmentFault 最新的问题
博客园 - Franky
T
Tenable Blog
T
The Blog of Author Tim Ferriss
博客园 - 三生石上(FineUI控件)
V
V2EX
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Troy Hunt's Blog
罗磊的独立博客
WordPress大学
WordPress大学
SecWiki News
SecWiki News
The Cloudflare Blog
S
Securelist
小众软件
小众软件
Schneier on Security
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 叶小钗
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
阮一峰的网络日志
阮一峰的网络日志
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
A
Arctic Wolf
大猫的无限游戏
大猫的无限游戏
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 司徒正美
Vercel News
Vercel News
H
Help Net Security
Y
Y Combinator Blog
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - lbq1221119

时间同步算法与Simple Ring-based election algorithm算法分析 SSCLI中GC垃圾回收源码分析(3) - GarbageCollectGeneration()与SuspendEE SSCLI中GC垃圾回收源码分析(2) - GarbageCollect()与Spin Lock SSCLI中GC源码分析(1) - EE与BCL之间的调用接口FCall (北京宇思信德科技公司)诚聘C#软件工程师 , Middle及Senior Level .Net Micro framework在开发过程中的bugs/problems及解决方案。 MF中使用GPRS:如何通过一个串口终端实现GPRS Modem拨号上网 博客园北京俱乐部第三次活动讲义PPT ValueType.Equals(null)的底层实现及CLR虚拟机对其结构支持 WebService传输数据流及数据交互解析 托管线程退出之后Dump文件特征 国外精品研究论文(持续更新) Random()中具体实现(含种子数组的实现) From double Click to Main: The initialization of Process in OS Microsoft Micro Framework 3.0对Serial Peripheral Interface 的支持 使用CorDbg进行托管调试 CLR内核调试之:Malloc函数实现 Build SSCLI20 under VS2008 full Document (完全手册) MethodTable内存空间分配中加法运算算法解析
From double Click to Main: PAL initialization - lbq1221119
lbq1221119 · 2008-12-09 · via 博客园 - lbq1221119

在上一篇文章,讲到了双击一个应用程序之后,操作系统如何初始化Process,以及创建相关的context,最后引导到应用程序的Main方法中。

在托管代码中,对于Main的启动还有点不同,有一个PAL层在启动main方法之前启动:

#ifdef __cplusplus 

extern "C"

#endif

int __cdecl main(int argc, char **argv) {

    struct _mainargs mainargs;

#ifdef _MSC_VER

    if (PAL_Initialize(0, NULL)) {

        return 1;

    }

#else

    if (PAL_Initialize(argc, argv)) {

        return 1;

    }

#endif

可以参考上篇文章里面的call stack,找到对main(int argc, char **argv)方法的调用。_MSC_VER是预编译控制,标识microsoftC编译器的版本。

DotNet中,对于PAL层的initializationterminal,是通过PAL_Initialize/PAL_Terminate两个方法来完成的,打开d:\Rotor\sscli20\pal\win32\win32pal.c就可以看到:

PALIMPORT int PALAPI PAL_Initialize(int argc,char *argv[])

{   

    int RetVal = 0;

    LONG RefCount;

    RefCount = InterlockedIncrement(&PalReferenceCount);

    if (RefCount == 1)  {

        RetVal = PAL_Startup(argc, argv);//Responsible for Start.

    }

    return RetVal;

}

PALIMPORT void PALAPI PAL_Terminate(void)

{

    LONG RefCount;

    LOGAPI("PAL_Terminate()\n");

    RefCount = InterlockedDecrement(&PalReferenceCount);

    //

    // if this hits, the host has a refcounting bug - the

    // count has underflowed.  This is not a PAL bug.

    //

    PALASSERT(RefCount >= 0);

    if (RefCount == 0) {

        PAL_Shutdown();//Responsible for terminate

    }

}

这里可以看出,PAL_InitializePAL_Terminate只是负责PAL Startupshotdown的一个转换的函数,在PAL_Initializae中,维护一个LONG PalReferenceCount;这个变量主要是为了避免对PAL初始化或者shutdown的重复调用,使用c:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\WinBase.h中的两个函数来维护这个PalReferenceCount

WINBASEAPI

LONG

WINAPI

InterlockedIncrement (

    __inout LONG volatile *lpAddend

);

WINBASEAPI

LONG

WINAPI

InterlockedDecrement (

    __inout LONG volatile *lpAddend

    );

         然后通过执行RetVal = PAL_Startup(argc, argv)来启动PAL

Int PALAPI PAL_Startup(int argc,char *argv[])

{

    int RetVal = -1;

    HMODULE hMod;

    WCHAR ModulePath[_MAX_PATH];

    hMod = GetModuleHandleW(L"rotor_pal.dll");

    if (!hMod) {

        return RetVal;

    }

    if (!GetModuleFileNameW(hMod,ModulePath,

                            sizeof(ModulePath)/sizeof(WCHAR))) {

        return RetVal;

    }

SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);

这里,首先加载rotor_pal.dll,然后获取这个DLL文件的ModulePath。同时设置错误模式。

RegisterWithEventLog(ModulePath);

    if (!InitializeObjectNameMangler(ModulePath)) {

        return RetVal;

}

    // Note: MSVCRT has already registered their exception filter at this point

    // SEH_CurrentTopLevelFilter won't be NULL

SEH_CurrentTopLevelFilter = (PAL_LPTOP_LEVEL_EXCEPTION_FILTER)        SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)PAL_ExceptionFilter);

    if ((SEH_Tls = TlsAlloc()) == TLS_OUT_OF_INDEXES) {

        goto LExit;

    }

    PALASSERT(PAL_TRY_LOCAL_SIZE >= sizeof(jmp_buf));

    // exceptions masked, round to nearest, 53 bit precision.

    _controlfp(_MCW_EM | _RC_NEAR | _PC_53 | _IC_PROJECTIVE | _DN_SAVE,

               _MCW_EM | _MCW_RC | _MCW_PC | _MCW_IC | _MCW_DN);

    RetVal = 0;

LExit:

    return RetVal;

}

RegisterWithEventLog(ModulePath);

记录EventLogsource,被忽略掉的错误信息,以防这个程序是non-admin权限的。如果没有注册成功的话,这个信息同样会被保留,但是在Event Viewer里面,就会以“The description for Event ID ( 0 ) in Source ( Rotor ) cannot be found.”开头。记录到:

SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\Rotor

    InitializeObjectNameMangler(ModulePath)所做的工作,就是初始化一个Object NameMangler,使用了OS提供的SHA1加密算法,来得到一个hash过后的字符串。这就会使用在不同的Rotor实例中,rotor_pal.dll不会冲突。

         接下来,在调试版本中,会初始化API tracing机制,首先获取到PAL_API_TRACING的值,然后设置对不同的APItracing

SEH_CurrentTopLevelFilter = (PAL_LPTOP_LEVEL_EXCEPTION_FILTER) SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)PAL_ExceptionFilter);

         这里,注册安装PAL自己的TOP exception handle。因为这个地方MSVCRT已经注册了一个Exception handle,所以这个地方的SEH_CurrentTopLevelFilter不会为空。这样注册之后,就可以由PAL自己来处理Unhandled Exception

         这里也是调用了OSBase API

WINBASEAPI

LPTOP_LEVEL_EXCEPTION_FILTER

WINAPI

SetUnhandledExceptionFilter(

    __in LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter

);

if ((SEH_Tls = TlsAlloc()) == TLS_OUT_OF_INDEXES) {

        goto LExit;

}

获取当前线程的一个TLSSLOT,暂时不清楚哪里会用到。

PALASSERT(PAL_TRY_LOCAL_SIZE >= sizeof(jmp_buf));

保证PAL_TRY_LOCAL_SIZE会大于一个获取程序状态的缓冲区。

    // exceptions masked, round to nearest, 53 bit precision.

    _controlfp(_MCW_EM | _RC_NEAR | _PC_53 | _IC_PROJECTIVE | _DN_SAVE,

               _MCW_EM | _MCW_RC | _MCW_PC | _MCW_IC | _MCW_DN);

设置浮点数的控制字,读写浮点控制或者状态字时候用的,managed code不支持,所以这里需要手工设置。

OK,到这里PAL启动完毕,下面一章看看如何启动EE并且launch应用程序的Main函数的。

Tuesday, December 09, 2008

lbq1221119@bj first post at sscli.cnblogs.com