From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12169 Path: news.gmane.org!.POSTED!not-for-mail From: Nicholas Wilson Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] Wasm support patch 1 (support systems without mmap) Date: Tue, 28 Nov 2017 16:34:32 +0000 Message-ID: References: ,<20171128154634.GZ1627@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1511886901 32748 195.159.176.226 (28 Nov 2017 16:35:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 28 Nov 2017 16:35:01 +0000 (UTC) To: "musl@lists.openwall.com" Original-X-From: musl-return-12185-gllmg-musl=m.gmane.org@lists.openwall.com Tue Nov 28 17:34:54 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1eJiqI-0007QT-80 for gllmg-musl@m.gmane.org; Tue, 28 Nov 2017 17:34:42 +0100 Original-Received: (qmail 22514 invoked by uid 550); 28 Nov 2017 16:34:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 22490 invoked from network); 28 Nov 2017 16:34:46 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=realvnc.onmicrosoft.com; s=selector1-realvnc-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=c8X5aZmiG2oLHmvYoxGJbHnmOHIhcUVaKNTqVDL9RvM=; b=DTN44AwPIydx/TcaSstHGYNXBiO93NNCKB1sM7quzpKBaCl36eS2TYBu2eq+r8An790hIaC/uUyNtgwI1r5YvodVeAZaOqgpJ7RLHxGPMYDm7UdhcfvVLtpfnG8B1uCDFRSw6Iih4fXfrCWOxWgNwPMlNt+Dqy2dDnOCivpl0Xg= Thread-Topic: [musl] [PATCH] Wasm support patch 1 (support systems without mmap) Thread-Index: AQHTaD5ihFuxGjOePUWt0iLA2VAUU6Mp76UAgAALLUw= In-Reply-To: <20171128154634.GZ1627@brightrain.aerifal.cx> Accept-Language: en-GB, en-US Content-Language: en-GB X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [2a02:390:a001:192:d6be:d9ff:fe9c:1892] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;VI1PR0502MB3886;6:PqEi9GMCj+nwaLW3yzMT1dbfNbsHHA2ariESEwa6XHmK7PlQ2hzg8vxxxmY45WOViRXmru75MfbDJOVecXW6k2b9M1Yrq0FETV6mBhUkMm7L1YimFWCTXnt/bQYox/43GzqaxqEIBT7LkUiwaRFY8mpFo/Qob5bjeKSztZAqEMnZqkO7E+n/lm9LkBXZxKG4OchzWE+qK7xEsNnaVHExDrDWONUHejNXAi4qL7LEtoGEJHmgm3MriLOThbjQazN2i4IQzHX/aal8/w1S2FPIm3C2eBBZLC4AlKELzH6tNoGAQjhEF2iOQUJPZqJ6pRIlZeB6tbL+IW/oJnWj8is0PNfEcYdSuBXyFDSxZUryJvA=;5:mUOLUZiLJoXIq5LGmmjrdxkyOlsnB0WBbnIzWpcJrGCIMKbjbos4r9lursLx7a3AQ93l3O8yir7HYp6m8ungWCEqoxM0KGSNQgkANYRK1qmRTvcwDCyM47sXrpXnENBRogmOGVM17FRizygMMJAOkqttnBXYoA0+/DaLVr0eV4s=;24:tx+aFh2UyHWbYGeor4jKSaeVwEmkvYqtCYRfte7tEjXm8n0EowINKjN38Codm3Uv34EOQvPsul43gA8ICmvwhOX/iTv1O8uiakg35QKj4k8=;7:/srgaHcLXdmw84eAeqlfDs3Z6i5ckvbtnDuqURob+mLaaI5ywb6+7UPFxH/eZDID51XwzRk9TL31s/3N/WIeZwgK0m+l g/dxa04dvZLSLgRWVw+0wxyA+UJaVe/82GFcyZxHwnkeyZz+IFDRdw5LiU//s0PSxL08kwUDuV+w5DhHv8ULp297iLkdFz9C4A x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: 2cac76c2-6c6d-4531-28e9-08d5367de774 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4603075)(4627115)(201702281549075)(5600026)(4604075)(2017052603258);SRVR:VI1PR0502MB3886; x-ms-traffictypediagnostic: VI1PR0502MB3886: authentication-results: spf=none (sender IP is ) smtp.mailfrom=nicholas.wilson@realvnc.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3231022)(3002001)(6041248)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123562025)(20161123560025)(2016111802025)(20161123558100)(6072148)(6043046)(201708071742011);SRVR:VI1PR0502MB3886;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:VI1PR0502MB3886; x-forefront-prvs: 0505147DDB x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(6009001)(366004)(346002)(376002)(189002)(24454002)(199003)(3660700001)(68736007)(53936002)(99286004)(5660300001)(50986999)(3280700002)(14454004)(54356999)(76176999)(81166006)(6916009)(81156014)(105586002)(5640700003)(5250100002)(1730700003)(2900100001)(2351001)(9686003)(2950100002)(2501003)(53546010)(305945005)(97736004)(106356001)(6246003)(7736002)(33656002)(86362001)(8936002)(102836003)(6116002)(8676002)(74316002)(189998001)(7696005)(2906002)(25786009)(55016002)(316002)(6506006)(6436002)(229853002)(478600001)(101416001);DIR:OUT;SFP:1101;SCL:1;SRVR:VI1PR0502MB3886;H:VI1PR0502MB3885.eurprd05.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: realvnc.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM X-MS-Exchange-CrossTenant-Network-Message-Id: 2cac76c2-6c6d-4531-28e9-08d5367de774 X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Nov 2017 16:34:32.3660 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9ad766d3-c6a5-4458-8c58-244e7c118728 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0502MB3886 X-OriginatorOrg: realvnc.com Xref: news.gmane.org gmane.linux.lib.musl.general:12169 Archived-At: Oh, I'd missed that. Let's scrap this patch then, I'll post an updated vers= ion of Patch #3 (Wasm-specific files) that implements mmap using Wasm's und= erlying "brk" capability. I could use a bitmap to track free pages - but there are 32768 pages (64KiB= page size, 2GiB max memory) and that would take up 4KiB just to track the = free pages, plus if we're asked to mmap several pages together, we'd like t= o be able to search quickly for a block of free pages of the right size... = What we'd need would be some kind of free-list data structure with sized bi= ns and granular locking! Oh well... Nick ________________________________________ From: Rich Felker on behalf of Rich Felker Sent: 28 November 2017 15:46:34 To: musl@lists.openwall.com Subject: Re: [musl] [PATCH] Wasm support patch 1 (support systems without m= map) On Tue, Nov 28, 2017 at 11:50:08AM +0000, Nicholas Wilson wrote: > I'm hoping the first patch is uncontroversial. > > WebAssembly has a linear/flat memory model, whereby it's simply > impossible for the addressable memory to contain "holes". Therefore, > mmap can't really be emulated, and all memory has to be allocated > via brk. This is not supported or supportable in musl's malloc. The heap that can be serviced by brk is only usable for allocation sizes less than ~128k. > I've done this by allowing malloc to fall back to brk (even for > allocations above MMAP_THRESHOLD) if mmap returns ENOSYS. > > It's one line of code that will do harm in "normal" systems, and > allows platforms that are emulating syscalls to choose not to > support mmap, and still get a working malloc if brk is working. The patch as written almost surely corrupts the heap structures or at least produces new heap expansion (no possibility of reuse) each time an allocation larger than ~128k is requested. This is because bin_index[_up] is not defined for n > MMAP_THRESHOLD. There are other places in musl that also depend on mmap working, like the TLS init code. brk on the other hand is only used as an optimization. I think what you should do, if it's hard to support both brk and mmap, is omit brk instead (have it return -ENOSYS), then have mmap always allocate lowest-available-page and keep some data structure (even just a bit array) representing which pages are free. This way you still keep everything in a contiguous range (possibly with some reusable gaps due to munmap) like you want. Rich > diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c > index 9e05e1d6..572232e1 100644 > --- a/src/malloc/malloc.c > +++ b/src/malloc/malloc.c > @@ -328,13 +328,17 @@ void *malloc(size_t n) > size_t len =3D n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE; > char *base =3D __mmap(0, len, PROT_READ|PROT_WRITE, > MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); > - if (base =3D=3D (void *)-1) return 0; > + if (base =3D=3D MAP_FAILED) { > + if (errno =3D=3D ENOSYS) goto nommap; > + return 0; > + } > c =3D (void *)(base + SIZE_ALIGN - OVERHEAD); > c->csize =3D len - (SIZE_ALIGN - OVERHEAD); > c->psize =3D SIZE_ALIGN - OVERHEAD; > return CHUNK_TO_MEM(c); > } > > +nommap: > i =3D bin_index_up(n); > for (;;) { > uint64_t mask =3D mal.binmap & -(1ULL< @@ -405,7 +409,7 @@ void *realloc(void *p, size_t n) > newlen =3D (newlen + PAGE_SIZE-1) & -PAGE_SIZE; > if (oldlen =3D=3D newlen) return p; > base =3D __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); > - if (base =3D=3D (void *)-1) > + if (base =3D=3D MAP_FAILED) > goto copy_realloc; > self =3D (void *)(base + extra); > self->csize =3D newlen - extra;