Skip to content

Instantly share code, notes, and snippets.

@kenpusney
Created March 27, 2013 07:11
Show Gist options
  • Save kenpusney/5252352 to your computer and use it in GitHub Desktop.
Save kenpusney/5252352 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
typedef void (*handle)();
int main(){
atexit((handle)main);
return 0;
}
@kenpusney
Copy link
Author

gcc under Code::blocks runs only twice, and Visual studio gets an undead cycle.

## Source Code:

#include <stdlib.h>
#include <stdio.h>

typedef void (*handle)();

int main(){
    atexit((handle)main);
    return 0;
}

Visual Studio 2010

     1: 
     2: #include <stdlib.h>
     3: #include <stdio.h>
     4: 
     5: typedef void (*handle)();
     6: 
     7: int main(){
013C1380 55                   push        ebp  
013C1381 8B EC                mov         ebp,esp  
013C1383 81 EC C0 00 00 00    sub         esp,0C0h  
013C1389 53                   push        ebx  
013C138A 56                   push        esi  
013C138B 57                   push        edi  
013C138C 8D BD 40 FF FF FF    lea         edi,[ebp-0C0h]  
013C1392 B9 30 00 00 00       mov         ecx,30h  
013C1397 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
013C139C F3 AB                rep stos    dword ptr es:[edi]  
     8:     atexit((handle)main);
013C139E 68 31 11 3C 01       push        offset @ILT+300(_main) (13C1131h)  
013C13A3 E8 B7 FC FF FF       call        @ILT+90(_atexit) (13C105Fh)  
013C13A8 83 C4 04             add         esp,4  
     9:     return 0;
013C13AB 33 C0                xor         eax,eax  
    10: }
013C13AD 5F                   pop         edi  
013C13AE 5E                   pop         esi  
013C13AF 5B                   pop         ebx  
013C13B0 81 C4 C0 00 00 00    add         esp,0C0h  
013C13B6 3B EC                cmp         ebp,esp  
013C13B8 E8 6F FD FF FF       call        @ILT+295(__RTC_CheckEsp) (13C112Ch)  
013C13BD 8B E5                mov         esp,ebp  
013C13BF 5D                   pop         ebp  
013C13C0 C3                   ret  

gcc 4.7.1 / gdb 7.5

7:  int main(){
0x00401334  push   %ebp
0x00401335  mov    %esp,%ebp
0x00401337  and    $0xfffffff0,%esp
0x0040133A  sub    $0x20,%esp
0x0040133D  call   0x401920 <__main>
8:      int dying = 1;
0x00401342  movl   $0x1,0x1c(%esp)
9:      atexit((handle)main);
0x0040134A  movl   $0x401334,(%esp)
0x00401351  call   0x4012c0 <atexit>
10:     dying = 2;
0x00401356  movl   $0x2,0x1c(%esp)
11:     return dying;
0x0040135E  mov    0x1c(%esp),%eax
12: }
0x00401362  leave
0x00401363  ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment