public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Stanislav Kascak <stanislav.kascak@gmail.com>
To: cygwin@cygwin.com
Subject: possible problem with memory allocation using calloc/mmap/munmap
Date: Fri, 03 May 2019 11:33:00 -0000	[thread overview]
Message-ID: <CALLhcm4QGY+eP0_CRiSbJwQ12kOetvTK=6-AtC17x7d+QhGKTw@mail.gmail.com> (raw)

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

I'm not sure if this is only a problem of my environment, or there is
something I'm missing.

Below is the test (notice that memory allocated by mmap() is only 64k
bytes in front of the memory allocated by calloc(), not 32MB as
requested).

My system is Windows 7 64 bit, cygwin64 3.0.7.

Best regards,

Stanislav Kascak

$ cat a.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char** argv){
  char* x;
  char* y;
  int fd;
  size_t s1 = 0x40000;
  size_t s2 = 0x2000000;
  x = (char*)calloc(1, s1);
  if(x == NULL){
    printf("calloc failed\n");
  }
  x[0] = 'a';
  fd = open("testfile", O_RDONLY);
  if(fd == -1){
    printf("open failed: %d\n", errno);
  }
  y = mmap(NULL, s2, PROT_READ, MAP_SHARED, fd, 0);
  if(y == MAP_FAILED){
    printf("mmap failed: %d\n", errno);
  }
  close(fd);
  printf("%lx -> %c\n", x, x[0]);
  printf("%lx\n", y);
  if(munmap(y, s2)){
    printf("munmap failed: %d\n", errno);
  }
  printf("%lx -> %c\n", x, x[0]);
  free(x);
}

$ gcc a.c

$ echo "testdata" > testfile

$ ./a.exe
6fffffb0010 -> a
6fffffa0000
Segmentation fault (core dumped)

$ uname -a
CYGWIN_NT-6.1 myhost 3.0.7(0.338/5/3) 2019-04-30 18:08 x86_64 Cygwin

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

             reply	other threads:[~2019-05-03 11:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-03 11:33 Stanislav Kascak [this message]
2019-05-20 22:26 ` Ken Brown
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='CALLhcm4QGY+eP0_CRiSbJwQ12kOetvTK=6-AtC17x7d+QhGKTw@mail.gmail.com' \
    --to=stanislav.kascak@gmail.com \
    --cc=cygwin@cygwin.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).