munmap slowness; IsBadReadPtr considered harmful

Brian Ford
Tue Feb 24 23:23:00 GMT 2004

On Tue, 24 Feb 2004, Corinna Vinschen wrote:
> On Feb 23 18:04, Brian Ford wrote:
> > Ok, no response.  That means either:
> >
> > 4.) No one likes me :( ...
> >
> 5.) No one has enough spare time currently.
Yeah, I hoped that was it.  But, I saw enough activity from you and Chris
on the cygwin list about, IMHO, less interesting issues, that I wondered.

I'll be more patient now, I promise :D.

> > FWIW, the following replacement for IsBadReadPtr appears to have the
> > same functionality without the associated page fault overhead.
> >
> > So, any comments about the function above, where to put it, or what to
> > name it?
> >
> IMHO, the name's ok.  It could perhaps get its place in
Here is my current version based on others in

int __stdcall
__check_invalid_addr_range(const void *addr, size_t len)
  if (addr == NULL || len == 0)
    return EINVAL;

  const void *end;

  for (end = (char *)addr + len, addr = (void *)((DWORD)addr % getpagesize());
       addr < end;
       addr = (char *)addr + mbuf.RegionSize)
      if (!VirtualQuery (addr, &mbuf, sizeof (mbuf))
          || mbuf.State != MEM_COMMIT
          || mbuf.AllocationProtect & (PAGE_NOACCESS|PAGE_GUARD))
        return EINVAL;

  return 0;

> > I see in the archives that the IsBadXPtr vs. VirtualQuery debate has
> > happened before in different contexts.  It seams to me that VirtualQuery
> > wins here hands down, though.
> >
> Did you check on 9x and NT?
No.  I'll try, but that is difficult around here.

> Is that really correctly implemented?
Probably not, but I think it is better now.

> AFAIU VirtualQuery, it returns information about consecutive pages
> which share the same attributes.  So the length returned must be >=
> the len parameter or you must check the actual attributes returned
> by VirtualQuery.
Um, why must it be >= len?  I agree about checking the attributes, though.

> One possible attribute would be PAGE_NOACCESS.
> In that case you would return true, even though the pages are not
> accessible, right?

I guess it all depends on your interpretation of the following lines from:


    Addresses in the range [addr,addr+len) are outside the valid range for
the address space of a process.

What does that *really* mean, especially in terms of

Thanks again for your volunteer time.

Brian Ford
Senior Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
Phone: 314-551-8460
Fax:   314-551-8444

More information about the Cygwin-developers mailing list