Solved

EXCEPTION_ACCESS_VIOLATION in Win32 environment

Posted on 2002-04-16
6
498 Views
Last Modified: 2008-03-03
Is there a way to create a memory block in such a way that access outside the extent of that block will be guaranteed to hit a memory access (or protection) violation.

For example suppose I wanted to do some fairly heavy processing on an array of data but would prefer to let the hardware handle the bounds checking for me, can it be done?

e.g. in the following code snippet how would I go about constucting the array 'v', so that referencing element 2049 trips a hardware exception of some kind?

void DoIt()
{
  BYTE v[2048];
  while(FillBuffer(v)){
    BYTE* p = v;
    __try{
      while(true)*p++;
      }
    __except(GetExceptionCode()==EXCEPTION_ACCESS_VIOLATION?EXCEPTION_EXECUTE_HANDLER:0){
      continue;
      }
    }
  }
0
Comment
Question by:GGRUNDY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6944700
The simple answer is that C++ has no such facility.  Some operating systems have hooks of some sort that permits setting of hardware breakpoints (often via a hardware DEBUGGING facility) but these things are inherently complex, platform dependent, limited in scope, and not a part of C++.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6944729
The "usual" way would be to 'VirtualProtect()' the page that is to be watched and handle the exceptions accordingly...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6944783
jkr,

Good point but....

1) VirtualProtect() is a "Window-ism" and not a general purpose (i.e. C++ as in this topic area) solution to this question.

2) I also don't think you can quite do what GGRUNDY has asked with VirtualProtect().  The need expressed seems to be to limit access to a specific area and catch any out-of-bounds access.  Virtual protect has limitation in that you can't just set a specific range as OK.  You must set "guard" blocks and HOPE that an erroneous access hits one of the guard blocks.  Other problem is that the size of areas you can protect with VirtualProtect() is limited to a PAGE.  Normally that will be no smaller than 4KBytes.  So you can setup a PAGE and 2 more pages, one on either side, for a total of 3 pages, and then catch any access to the two pages.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:pellep
ID: 6945404
Well, you could implement your own 'memory allocation' functionality.

This is just a simple C example, but you could easily make it C++ and wrap it in a class.

#define POOLSIZE     (4096 * 1024)

static char          memPool[POOLSIZE];
static int          allocPoint;

void *Custom_Alloc( int size ) {
     char     *p;

     if ( allocPoint + size > POOLSIZE ) {
          //buffer overflow. Handle however you wish.
          return NULL;
     }

     p = &memPool[allocPoint];

     allocPoint += ( size + 31 ) & ~31;

     return p;
}

This, of course, does not allow freeing(deallocating) memory in the buffer. To do this, you'd have to add some code to maintain a list of pointers to allocated 'chunks' and the size of those 'chunks' and some memmove() logic to optimize the buffer usage.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6945557
>>VirtualProtect() is a "Window-ism" and not a general purpose (i.e. C++ as in this topic area)

As is "__try {} __except()" :o)

To your 2nd point - you *could*, even though I wouldn't recommend (BTW, that's the way VC++ guards against stack overflows)
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6946026
Thanks guys, as usual I'm in your debt.

jhance is perfectly correct, but as I asked what in retrospect is a damn fool question,
I'll accept jkr's answer as he first mentioned VirtualProtect. This comes closest to what I needed and seems to be the Ms approach (AKA Creating Guard Pages). (but is probably to coarse for what I wanted).

Hmmmm. Pellep I don't quite understand how your suggestion helps with hardware rangecheking but if you or someone else can enlighten me, I'll raise some extra points for you.

Once again thanks everyone.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 114
Question of running the program built from sample in codeproject website 3 101
Unable to start eclipse ? 17 173
C++ help/ Toy problem 19 69
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question