home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sftick.zip / adv / guard / GP.C < prev    next >
Text File  |  1994-04-21  |  5KB  |  131 lines

  1. #define INCL_DOSMEMMGR
  2. #define INCL_DOSEXCEPTIONS
  3.  
  4. #include <os2.h>
  5. #include <stdio.h>
  6.  
  7. #define NUM_PAGES                3
  8. #define SZ_PAGE                  4096
  9.  
  10. ULONG MyExceptionHandler ( PEXCEPTIONREPORTRECORD pTrap ) ;
  11.  
  12. PBYTE pbBase ; /* Base of entire memory object */
  13.  
  14. INT main ( VOID )
  15. {
  16.    LONG     lIndex ;
  17.    EXCEPTIONREGISTRATIONRECORD      errRegister ;
  18.    APIRET      arReturn ;
  19.  
  20.    pbBase = NULL ;
  21.  
  22.    /* register exception handler */
  23.    errRegister.ExceptionHandler = (_ERR *) &MyExceptionHandler ;
  24.    arReturn = DosSetExceptionHandler ( &errRegister ) ;
  25.    printf ( "DosSetExceptionHandler returns %ld\n", arReturn ) ;
  26.  
  27.    /* allocate some memory */
  28.    arReturn = DosAllocMem ( (PPVOID) &pbBase,
  29.                              NUM_PAGES * SZ_PAGE,
  30.                              PAG_READ | PAG_WRITE ) ;
  31.  
  32.    printf ( "DosAllocMem returns %ld ( pbBase = %p ) \n",
  33.       arReturn,
  34.       pbBase ) ;
  35.  
  36.    //-----------------------------------------------------------
  37.    // Commit first page and set to guard page
  38.    //-----------------------------------------------------------
  39.    arReturn = DosSetMem ( pbBase,
  40.                           SZ_PAGE,
  41.                           PAG_COMMIT | PAG_READ |
  42.                           PAG_WRITE | PAG_GUARD ) ;
  43.    printf ( "Return Code from DosSetMem, "
  44.             "%ld - pbBase = %p\n",
  45.             arReturn,
  46.             pbBase ) ;
  47.  
  48.    //-----------------------------------------------------------
  49.    // Write to pages, from bottom to top
  50.    //-----------------------------------------------------------
  51.    for ( lIndex = 0L ;
  52.       lIndex < ( NUM_PAGES * SZ_PAGE ) ;
  53.       lIndex += 0x0010L ) {
  54.        printf ( "\rWriting to offset 0x%08lX", lIndex ) ;
  55.        pbBase [lIndex] = 1 ;
  56.        printf ( "\rWritten to offset 0x%08lX", lIndex ) ;
  57.    } /* endfor */
  58.  
  59.    printf ( "\n" ) ;
  60.  
  61.    //--------------------------------------------------------------
  62.    // Free memory area
  63.    //--------------------------------------------------------------
  64.    printf ( "Freeing pbBase = %p\n", pbBase ) ;
  65.    arReturn = DosFreeMem ( pbBase ) ;
  66.  
  67.    return 0 ;
  68. }
  69.  
  70. ULONG MyExceptionHandler ( PEXCEPTIONREPORTRECORD perrTrap )
  71. {
  72.    ULONG       ulReturn ; /* return from exception handler */
  73.    APIRET      arReturn ; /* return code from API's        */
  74.    PBYTE       pbTrap ;   /* fault address                 */
  75.  
  76.    ulReturn = XCPT_CONTINUE_SEARCH ;
  77.  
  78.    /* this is the only exception we're interested in */
  79.  
  80.    if ( perrTrap -> ExceptionNum == XCPT_GUARD_PAGE_VIOLATION ) {
  81.       printf ( "\n *** Guard exception *** \n" ) ;
  82.  
  83.       pbTrap = ( PBYTE ) perrTrap -> ExceptionInfo [1] ;
  84.  
  85.       //---------------------------------------------------------
  86.       // Check that the fault is within our memory zone, so that
  87.       // we won't interfer with system handling of stack growth
  88.       //---------------------------------------------------------
  89.       if (( pbTrap >= pbBase ) &&
  90.        ( pbTrap < pbBase + (NUM_PAGES * SZ_PAGE) )) {
  91.  
  92.          //-----------------------------------------------------
  93.          // Unguard guard page
  94.          //-----------------------------------------------------
  95.          arReturn = DosSetMem ( pbTrap,
  96.                                 SZ_PAGE,
  97.                                 PAG_READ | PAG_WRITE ) ;
  98.  
  99.          printf ( "DosSetMem returns %ld "
  100.                   "( pbTrap = 0x%08lX ) \n",
  101.                   arReturn,
  102.                   pbTrap ) ;
  103.  
  104.          pbTrap += SZ_PAGE ;
  105.  
  106.          //-----------------------------------------------------
  107.          // Commit and guard next page above
  108.          //-----------------------------------------------------
  109.          if (( pbTrap >= pbBase ) &&
  110.              ( pbTrap < pbBase + NUM_PAGES * SZ_PAGE )) {
  111.             arReturn = DosSetMem ( pbTrap,
  112.                                    SZ_PAGE,
  113.                                    PAG_COMMIT | PAG_READ |
  114.                                    PAG_WRITE | PAG_GUARD ) ;
  115.  
  116.             printf ( "DosSetMem returns %ld "
  117.                      "( pbTrap = 0x%08lX ) \n",
  118.                      arReturn,
  119.                      pbTrap ) ;
  120.          } /* endif */
  121.  
  122.          //-----------------------------------------------------
  123.          // We can continue execution
  124.          //-----------------------------------------------------
  125.          ulReturn = XCPT_CONTINUE_EXECUTION ;
  126.        } /* endif */
  127.    } /* endif */
  128.  
  129.    return ulReturn ;
  130. }
  131.