Solved

EXCEPTION_ACCESS_VIOLATION in Win32 environment

Posted on 2002-04-16
6
494 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

820 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