X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:mime-version:from:date:message-id:subject:to
	:content-type; q=dns; s=default; b=KAT/HKWaWQeleM4nqUtClmdTx6PSn
	x6jX/fKMDwL8xZ7NnzLINJ9THlO5AKPwRKpqM6Z8TFebdg7VPlcZyXp24AlZlwvD
	J+fTU9F3PLvfVQHlPCve8ilV6PNo24tmukG0QtJuuecdE6P8sfSLip6A3SSGUEZG
	wSubXI1OI0mCUk=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:mime-version:from:date:message-id:subject:to
	:content-type; s=default; bh=Goyv/AhQZgu4ZI4dea9kWNwon1w=; b=uB9
	Y8z/DZBxTFvOy/Hfc0zWviSh1tSUaELRreRqBwWrbLY9ZMK5t6Q8/RuU6nSd7ekN
	lpE6LdVn/HDQUyGnEW1myD9Uc5n5hXFGfUiahli+ti5ziKKyhx2VyEym7LgJXbAK
	SEKqo1TIlHs5LiACGZt/VKhlL25/PCaW4HlVClBo=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=dumped, ac
X-HELO: mail-ot1-f43.google.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=20161025;        h=mime-version:from:date:message-id:subject:to;        bh=6P5hEmaB1+IOj3/tq5gbnGJ4msX4BvdkFgdh+nqSXEQ=;        b=uO5w/JEm0av4XKy/GWF2C8vAbNaFWL92VY05Zb/9xoMemNcrGFPMsmhPd1M61PUQdG         xdbkbk3y7sYBTaqMHSR2jCZiyw7nmaqulIVEhZjQBK9MPtcQCYzYnm/8drsdzoAy8okR         WhyyBOkdoOJwMTVZYXgbRvU/UXHkvl6ITLb69mAMJiAmzskeg3Eb5t4RM22yJ0oc1ePE         FnSx6pQeZPgVGnlKqHZlbNSkwPrYHv8ooMGCRMqHG7Moq2T9ezZQBChWdRWy86TwUwe0         Rdu3qvSDdKR1U4AdO81rVG+a95SUBnSLDcSGdOBuwD1y5xbiIUkoHIjWNoofMeTYX5yS         OSSQ==
MIME-Version: 1.0
From: Stanislav Kascak <stanislav.kascak@gmail.com>
Date: Fri, 3 May 2019 13:33:43 +0200
Message-ID: <CALLhcm4QGY+eP0_CRiSbJwQ12kOetvTK=6-AtC17x7d+QhGKTw@mail.gmail.com>
Subject: possible problem with memory allocation using calloc/mmap/munmap
To: cygwin@cygwin.com
Content-Type: text/plain; charset="UTF-8"

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

