public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: Stanislav Kascak <stanislav.kascak@gmail.com>
Cc: cygwin@cygwin.com
Subject: Re: possible problem with memory allocation using calloc/mmap/munmap
Date: Mon, 03 Jun 2019 11:55:00 -0000	[thread overview]
Message-ID: <20190603115456.GG3437@calimero.vinschen.de> (raw)
In-Reply-To: <CALLhcm4QGY+eP0_CRiSbJwQ12kOetvTK=6-AtC17x7d+QhGKTw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1981 bytes --]

On May  3 13:33, Stanislav Kascak wrote:
> Hello cygwin team,
> 
> I came across a problem with memory allocation/deallocation when
> trying to compile and run tests of openldap under cygwin.
> 
> I created a test program to simulate sequence of actions. First a
> bigger chunk of memory (>~262kB) is allocated using calloc(), then
> mmap() is called with requested memory size much bigger than actual
> file size. Subsequently,  munmap() to free previous mmap(), and at the
> end an attempt to access memory allocated with calloc(). That last
> memory access is causing Segmentation fault.
> 
> It seems that when mmap() is called with length argument exceeding
> size of file, only memory to fit that file is allocated. munmap()
> however frees the full specified length. Since (at least on my
> computer) big chunk of memory allocated by calloc() is located after
> mmap() allocation, munmap() frees even memory of that calloc().

Ken's right.  Due to the differences between mapping files on Windows
vs. Unix, Cygwin can't map beyond the file size + the remainder of the
last page.  Cygwin tries to workaround that on 32 bit by allocating
an anonymous mapping following the file mapping to keep the range free
from other mappings.  But on 64 bit this workaround doesn't work anymore
because the OS is missing an (undocumented) flag which allows to
create mappings on 4K boundaries, rather than just on 64K boundaries.

I know this situation is unsatisfying, but I have no easy workaround
to allow this.  Cygwin could add the anonymous mapping on the next
64K boundary on 64 bit, but that would result in a hole in the mapping
which seemed like a rather bad idea when porting mmap to 64 bit.

Ken's also right that munmap is doing the right thing here.  If
anything's wrong, it's mmap's workaround for mappings beyond the file
length.  If only 64 bit would allow 4K-aligned mappings :(


Corinna

-- 
Corinna Vinschen
Cygwin Maintainer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2019-06-03 11:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-03 11:33 Stanislav Kascak
2019-05-20 22:26 ` Ken Brown
2019-06-03 11:55 ` Corinna Vinschen [this message]
2019-06-04  9:38   ` Stanislav Kascak
2019-06-04 13:18     ` Corinna Vinschen
2019-06-04 13:49       ` Stanislav Kascak
2019-06-04 14:49         ` Corinna Vinschen
2019-06-04 16:02           ` Stanislav Kascak
2019-06-05 18:23             ` Corinna Vinschen
2019-06-06 13:14               ` Stanislav Kascak
2019-06-07  7:27                 ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190603115456.GG3437@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    --cc=stanislav.kascak@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).