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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - 比尔盖房

USACO: Section 1.5 -- PROB Prime Palindromes USACO: Section 1.5 -- PROB Number Triangles USACO: Section 1.4 -- PROB Arithmetic Progressions USACO: Section 1.3 -- PROB Prime Cryptarithm USACO: Section 1.3 -- PROB Barn Repair USACO: Section 1.3 -- PROB Mixing Milk USACO: Section 1.2 -- PROB Dual Palindromes USACO: Section 1.2 -- PROB Palindromic Squares Programming Pearls: Chatper3 Problem6 [Form letter generator] Programming Pearls: Chatper3 Problem5 [Hyphenation Words] Programming Pearls: Chatper3 Problem4 [Dates Caculation] Programming Pearls: Chatper3 Problem3 [Print Banner] Studying Probability Theory Studying "Concrete Mathematics" Studying "Introduction to Algorithms" How DebuggerRCThread is lauched? Public Symbols vs Private Symbols[zt] The magic of NativeWindow-- How does .Net Winform manage Win32 controls .Net Windows Service
Testing SEH tips
比尔盖房 · 2006-10-28 · via 博客园 - 比尔盖房

I use the code snippet below to test some points of SEH:

#define WIN32_LEAN_AND_MEAN
#include
<stdio.h>
#include
<windows.h>

void main()
{
    
char *str="this is a local string";
    printf(
"Entering main function\n");
    
    __try
    
{
        
int dw=3;
        printf(
"Entering first __try block\n");
        __try
        
{
            printf(
"Entering second __try block\n");
            __try
            
{
                
*(DWORD *)0=1;
            }

            __finally
            
{
                printf(
"Clean in finally block\n");
            }

        }

        __except(EXCEPTION_EXECUTE_HANDLER)
        
{
            printf(
"We handled this exception %d, %s\n", dw, str);
        }

    }

    __except(EXCEPTION_CONTINUE_SEARCH)
    
{
        printf(
"We do not handle this exception\n");
    }

    printf(
"Exiting main function\n");
}

Outcome:
1. How does __except_handler3 call the __except block without returning back?
Before entering __except block, the compiler always generates the following statement:
mov         esp,dword ptr [ebp-18h]  
It will restore the esp register to the value of before executing any code.

2. Since the __except bock will restore the esp to the value of before executing any code, how does it use the local variables?
Since the local variables are accessed through the offset of ebp, although the esp does not point to the stack top anymore, compiler can still use ebp offset to use them. Take the local variables, "dw" and "str" for example, see below:

 char *str="this is a local string";
0041338B  mov         dword ptr [ebp-20h],424E70h
  int dw=3;
004133A6  mov         dword ptr [ebp-2Ch],3

the __except block will use them like this:
0041340A  mov         esp,dword ptr [ebp-18h]
  {
   printf("We handled this exception %d, %s\n", dw, str);
0041340D  mov         eax,dword ptr [ebp-20h]
00413410  push        eax 
00413411  mov         ecx,dword ptr [ebp-2Ch]
00413414  push        ecx 

Additionally, based on my test, once the SEH is used in the function, the VC optimizer will not omit Frame Pointer register(ebp) anyway, it will always save the ebp for offset usage.

3. 2 points of code in __except_handler3.
Before calling the __except code block, the __except_handler3 sets the ebp register to the original saved ebp value in the extended EXCEPTION_REGISTRATION so that when the __except block is executed, the ebp register can be used to lookup the function local variables that contains __except block.

Also, before calling the __except code block, the __except_handler3 sets the trylevel field in the EXCEPTION_REGISTRATION to the parent __try block trylevel value, so that it can handle any nested exception generated in __except code block.