X-Recipient: archive-cygwin AT delorie DOT 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:date:from:to:cc:subject:message-id:reply-to :references:mime-version:content-type:in-reply-to; q=dns; s= default; b=lIOnedyAPF9oG+mbFrIbVoQXGLnPfN6zASZY6dBXEFfb29BNAw28e Kj9Xo1W+eNUkNPEqSb1LYTQtAmQMyOocc1VilvBSLAeHoK3PVcSKcad12eKtgR8r n0xLwwrTjG/deiMt95BoYT6ZD4ClAhIpJZ7QlSM/mV8Or2r+PpNm9I= 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:date:from:to:cc:subject:message-id:reply-to :references:mime-version:content-type:in-reply-to; s=default; bh=tqfCZzwwZkoQQKuLOpqzKYVkPZo=; b=DSnCN835hN3Hyp51QepaX1/Jns89 dd10ckLrauYLtNd0Pz6msmjYOcDoA6O8S3NiUwJKq3MkvQqwi0CXCqeKLBKVGap3 gK7W4FOkO5tSc8pOlodZFCxKNfXPnztNcO5hWsiSGlXibPp/V1NAR13qFeUj5ar5 ycR8mZy1KifZl+4= Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-101.8 required=5.0 tests=AWL,BAYES_00,GOOD_FROM_CORINNA_CYGWIN,RCVD_IN_DNSWL_NONE,UNSUBSCRIBE_BODY autolearn=ham version=3.3.1 spammy=H*F:D*cygwin.com X-HELO: mout.kundenserver.de Date: Wed, 5 Jun 2019 20:23:15 +0200 From: Corinna Vinschen To: Stanislav Kascak Cc: cygwin AT cygwin DOT com Subject: Re: possible problem with memory allocation using calloc/mmap/munmap Message-ID: <20190605182315.GZ3437@calimero.vinschen.de> Reply-To: cygwin AT cygwin DOT com Mail-Followup-To: Stanislav Kascak , cygwin AT cygwin DOT com References: <20190603115456 DOT GG3437 AT calimero DOT vinschen DOT de> <20190604131836 DOT GS3437 AT calimero DOT vinschen DOT de> <20190604144948 DOT GT3437 AT calimero DOT vinschen DOT de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="xMJbWctQEcNQiBqK" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.11.3 (2019-02-01) --xMJbWctQEcNQiBqK Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Jun 4 18:01, Stanislav Kascak wrote: > > > > > > > It seems that when mmap() is called with length argument exce= eding > > > > > > > size of file, only memory to fit that file is allocated. munm= ap() > > > > > > > however frees the full specified length. [...] > > > > > > [...] > > > > > > I know this situation is unsatisfying, but I have no easy worka= round > > > > > > to allow this. Cygwin could add the anonymous mapping on the n= ext > > > > > > 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 th= e file > > > > > > length. If only 64 bit would allow 4K-aligned mappings :( > > > > > > > > > > Thanks for the answer. It is appreciated. > > > > > I understand the problem and difficulty to resolve it. Maybe retu= rning > > > > > an error from mmap (and putting a comment to code for its reason) > > > > > would be sufficient. mmap caller could just adjust requested > > > > > allocation size to file size. Without error, caller has no way of > > > > > knowing memory was not allocated and segfault is then thrown in an > > > > > unrelated memory segment which makes the root cause hard to track > > > > > down. But, I do not know all the implication that could result fr= om > > > > > that, so evaluation of this approach is up to you. > > > > [...] > > > > Eventually Cygwin adds another mapping to fullfill the entire mappi= ng > > > > request: > > > > > > > > |-- file 4K --|-- filler 60K --|-- filler 192K --| > > > > > > > > The problem on WOW64 and real 64 bit is that it's impossible to map > > > > the first filler. However, this area in the VM will *never* be > > > > allocated by other application functions due to the allocation > > > > granularity of 64K! > > > > > > > > So my workaround for 64 bit and WOW64 is to just skip allocating the > > > > first filler: > > > > > > > > |-- file 4K --|-- THE VOID 60K --|-- filler 192K --| > > > > > > > > The advantage is now that the following munmap of 256K will only > > > > unmap the map for the file and the filler, but not the region you > > > > calloced before, which formerly was accidentally mapped to the > > > > filler region. This just can't happen anymore now. > > > > > > > > Would that be feasible? If so I can push my patch and create a > > > > developer snapshot for testing. > > > > > > Two questions arise when I'm thinking about workaround solution: > > > - what happens if caller tries to write to |-- THE VOID 60K --|. Since > > > this is unallocated, would there be a segfault? > > > > Accessing the VOID would raise SIGSEGV, while accessing the filler > > raises SIGBUS. The latter is also used to implement MAP_NORESERVE, > > which the VOID can't support. >=20 > I played around a bit and I can confirm it would be consistent with > current behavior: > memwrite <0 - filesize) - no error, written to file > memwrite memwrite <4k, 64k) - SIGSEGV > memwrite <64k, mmap alloc size) - SIGSEGV or another mem alloc > overwrite (depending on whether there is another allocation) > With workaround last line would be fixed to SIGBUS (along with proper > allocation length). I believe this is completely OK. >=20 > > > > > - is it possible that some subsequent mem alloc request would return > > > region from |-- THE VOID 60K --| which could again cause segfault > > > after munmap? > > > > No, as stated above. Allocations are restricted to Windows' 64K > > allocation granularity. >=20 > I apologize. I missed that sentence. So, your workaround seems fine. Please try the latest snapshot from https://cygwin.com/snapshots/ Just replacing the Cygwin DLL is sufficient. Thanks, Corinna --=20 Corinna Vinschen Cygwin Maintainer --xMJbWctQEcNQiBqK Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoVYPmneWZnwT6kwF9TYGna5ET6AFAlz4CJIACgkQ9TYGna5E T6CGHA/9HrlEZdc5cHDBNQgSbrqUgSaK3d+lw7TnXxGa2mUk4oTdLmqu5ozHj+w7 AcD2UFeILocu3AZ50B19N968ko9Y6Jya/f5Dud04lvzmdOTycsU8mOpqM4/o/PLb BompBgo76C+4DsRctbWzNtRHawqp70js2/n3pWOZi70e+9bA1fDXO2Lxn62MRUQi 7QBnphA4A5Cq1qeinyTXZgQckbaxGxK5IQUfjrODk85pBOGHap2Ql2ZcLW/W3ehq E0Z4N9HktmpHjtiUMteCTh9xt+o44+ZHrgKcgtlpSFsmvwpjZF1+kaRCfhFhcUtr UYe+fcWq5kKBUujOsDtg1IIZXzBfOegtcbam9fghenUyKPS0O7C7i/QL4E/55EG1 QOGcL85c8ORvaiTvrx27PsnR520s6fiegIblWrWQ6jgFLv518a8LhitCxywxZNlY p0jgcR/kJQx67dT38NWFpQ3Ip6JGmhq8hKRa+96llyWvWmchzIZRzr+ddhdalg5H zznmzOxdxMv1jrE1jic6+Uiagc24W7r6kHf7/h0+LHvTaCrMIUfUPhY6qKLTLXHD bVnUaXOFB5f7jzcZTk2/L87N540Mwx98IRgb+WHJI0kmsss3jT0F5yhQ+qD5AdAs Kii9nMCaag3fovNCna56B7wbnTsew7qY7taR1WvtfRp5GMLdIYA= =nlk7 -----END PGP SIGNATURE----- --xMJbWctQEcNQiBqK--