Solved

EXCEPTION_ACCESS_VIOLATION in Win32 environment

Posted on 2002-04-16
6
475 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now