public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: "cygwin@cygwin.com" <cygwin@cygwin.com>
Cc: Stanislav Kascak <stanislav.kascak@gmail.com>
Subject: Re: possible problem with memory allocation using calloc/mmap/munmap
Date: Mon, 20 May 2019 22:26:00 -0000	[thread overview]
Message-ID: <949f0c5f-9806-a861-6a48-089b83aab032@cornell.edu> (raw)
In-Reply-To: <CALLhcm4QGY+eP0_CRiSbJwQ12kOetvTK=6-AtC17x7d+QhGKTw@mail.gmail.com>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 2257 bytes --]

On 5/3/2019 7:33 AM, Stanislav Kascak wrote:
> I came across a problem with memory allocation/deallocation when
> trying to compile and run tests of openldap under cygwin.

Corinna can give you a definitive response when she returns from vacation, but I 
looked at the code and have a few comments.

> 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.

That's correct.  The following comment in mmap.cc:1100 explains why: "don't map 
beyond EOF, since Windows would change the file to the new length, in contrast 
to POSIX."

> 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().

I think munmap does exactly what it's supposed to do according to Posix, which 
is "remove any mappings for those entire pages containing any part of the 
address space of the process starting at addr and continuing for len bytes."  In 
your case, that includes the memory allocated by calloc, which uses mmap.

I'm not sure what openldap is doing in your actual use case, but is there really 
a good reason to call mmap with requested size much bigger than the actual file 
size?  After all, you can never access that extra memory.  Posix says, 
"References within the address range starting at pa and continuing for len bytes 
to whole pages following the end of an object shall result in delivery of a 
SIGBUS signal."

But let's wait and see what Corinna says.

Ken
\0ТÒÐÐ¥\a&ö&ÆVÒ\a&W\x06÷'G3¢\x02\x02\x02\x02\x02\x02\x06‡GG\x03¢òö7–wv–âæ6öÒ÷\a&ö&ÆV×2æ‡FÖÀФd\x15\x13¢\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x06‡GG\x03¢òö7–wv–âæ6öÒöf\x17\x12ðФFö7VÖVçF\x17F–öã¢\x02\x02\x02\x02\x02\x02\x02\x02\x06‡GG\x03¢òö7–wv–âæ6öÒöFö72æ‡FÖÀÐ¥Vç7V'67&–&R\x06–æfó¢\x02\x02\x02\x02\x02\x06‡GG\x03¢òö7–wv–âæ6öÒöÖÂò7Vç7V'67&–&R×6–×\x06ÆPРÐ

  reply	other threads:[~2019-05-20 22:26 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 [this message]
2019-06-03 11:55 ` Corinna Vinschen
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=949f0c5f-9806-a861-6a48-089b83aab032@cornell.edu \
    --to=kbrown@cornell.edu \
    --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).