Linux Small Program Analysis

Source: Internet
Author: User

Environment CentOS 5.x

Program Example 1: #include <stdio.h> #include <stdlib.h>   void bug () {     system ("reboot");//re boot system    exit (0);//end process    }   int stack_test (INT&NBSP;A,INT&NBSP;B)//function stack   Press in   main function, a copy value, b in high address  {//the stack order is the parameter from right to left, then the stack frame, and the order of evaluation is undefined, determined by the compiler implementation.      printf ("before write: 0x%x\n", b);//output write before B value       int *p=&a;    //p Point to a     p++;     //p offset to b      *p=0xdddd;//*p value change  b changed       printf ("after write: 0x%x\n", b);     int c=0xcccc;      return c; } int main ()  {     int a=0xaaaa;      int b=0xbbbb;     int rEt=stack_test (A, b);      printf ("you shoule run here\n");      return ; }  //Program Example 2: #include <stdio.h> #include <stdlib.h>  void bug () {    system ("reboot");//reboot system     Exit (0);//end process   }   int stack_test (INT&NBSP;A,INT&NBSP;B)  {     int *p=&a;     p--;      *p=bug;     int c=0xcccc;     return c ;  }  int main ()  {     int a=0xaaaa;      int b=0xbbbb;     int ret=stack_test (A, b);      printf ("you shoule run here\n");      return ; }    //Output Result: BEFORE&Nbsp;write: 0xbbbb//          after write:  0xdddd//          you shoule run here//(GDB)  p &a  centos  gdb Debug Information/*  (GDB)  p&a$1 =  (int *)   0xbffff630 (GDB)  p&b$2 =  (int *)  0xbffff634 (GDB)  p&p$3 =  (int  **)  0xbffff620 (gdb)  p&c$4 =  (int *)  0xbffff624 (gdb)  n14p--;(gdb)  p p$5 =  (int *)  0xbffff630 (gdb)  n15*p=bug; (GDB)  p p$6 =   (int *)  0xbffff62c (gdb)  p *p$7 = 134513797 (GDB) &NBSP;N21INT&NBSP;C=0XCCCC ;(gdb)  p* p$8 = 134513684 (GDB)  p bug$9 = {void  ()} 0x8048414  <bug> (GDB)  p& bug$10 =  (void  (*) ())  0x8048414 <bug> GDB)  n22return  c; (GDB)  n23} (GDB)  nbug  ()  at process.c:55{(GDB)  nbreakpoint 2, bug   ()  at process.c:66system ("reboot");//re boot system by debugging   information and available: ESP moves first when a function is called   Press in   parameter b,a   return function pointer  return (a add-4)   then press ebp  to make EBP equal to ESP when EBP records the return information   When the ESP moves sequentially into the local variable  p c  ,p begins to point to a,p--, p points to ebp to point to space, and when P=bug   p logs the bug function stack frame, return c   called the bug  function   system restart */


Linux Small Program Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.