Solved

EXCEPTION_ACCESS_VIOLATION in Win32 environment

Posted on 2002-04-16
6
503 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

632 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