From nobody Sat Feb 25 22:29:52 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PPLzn5CCBz3tx8b; Sat, 25 Feb 2023 22:29:57 +0000 (UTC) (envelope-from steffen@sdaoden.eu) Received: from sdaoden.eu (sdaoden.eu [217.144.132.164]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4PPLzn1gvsz4M0g; Sat, 25 Feb 2023 22:29:57 +0000 (UTC) (envelope-from steffen@sdaoden.eu) Authentication-Results: mx1.freebsd.org; none Date: Sat, 25 Feb 2023 23:29:52 +0100 Author: Steffen Nurpmeso From: Steffen Nurpmeso To: Ed Maste Cc: src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org Subject: Re: git: 2c709ee70ade - main - libc: handle zero alignment in memalign() Message-ID: <20230225222952.t3oH3%steffen@sdaoden.eu> In-Reply-To: References: <202302241821.31OIL4ux075893@gitrepo.freebsd.org> <20230224205001.Fbn1A%steffen@sdaoden.eu> <20230225012736.PT3cH%steffen@sdaoden.eu> Mail-Followup-To: Ed Maste , src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org User-Agent: s-nail v14.9.24-418-gbf8954aec4 OpenPGP: id=EE19E1C1F2F7054F8D3954D8308964B51883A0DD; url=https://ftp.sdaoden.eu/steffen.asc; preference=signencrypt BlahBlahBlah: Any stupid boy can crush a beetle. But all the professors in the world can make no bugs. X-Rspamd-Queue-Id: 4PPLzn1gvsz4M0g X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15987, ipnet:217.144.128.0/20, country:DE] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org Ed Maste wrote in : |On Fri, 24 Feb 2023 at 20:27, Steffen Nurpmeso wrote: |> Me too. 'Was just thinking of allocators which give back valid |> but inaccessible memory for 0 bytes so that each access would |> fault. | |The size is not (necessarily) zero though. The alignment requested is. I personally would fail EINVAL for 0: something really must be bogus if you reqest an alignment of 0. The standard says Upon successful completion, posix_memalign( ) shall return zero; otherwise, an error number shall be returned to indicate the error and the contents of memptr shall either be left unmodified or be set to a null pointer. If size is 0, either: . posix_memalign( ) shall not attempt to allocate any space, in which case either an implementation-defined error number shall be returned, or zero shall be returned with a null pointer returned in memptr, or . posix_memalign( ) shall attempt to allocate some space and, if the allocation succeeds, zero shall be returned and a pointer to the allocated space shall be returned in memptr. The application shall ensure that the pointer is not used to access an object. The latter is hard (expensive, special-cased) to implement (here). Also the claim of the commit message was wrong: The address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *). As can be seen: #?0|kent:tmp$ cat t.c; for i in $(seq 8); do tcc -run t.c $i;echo $?;done #include #include #include #include int main(int argc, char **argv){ char *vp; int err; if(!(err = posix_memalign((void**)&vp, atoi(argv[1]), 1))){ if(vp) *vp = '\0'; else printf("x1\n"); }else printf("x2: %s\n",strerror(err)); return err != 0; } x2: Invalid argument 1 x2: Invalid argument 1 x2: Invalid argument 1 x2: Invalid argument 1 x2: Invalid argument 1 x2: Invalid argument 1 x2: Invalid argument 1 0 On the other "size" is ok: #?0|kent:tmp$ cat t.c; for i in $(seq 0 2); do tcc -run t.c $i;echo $?;done #include #include #include #include int main(int argc, char **argv){ char *vp; int err; if(!(err = posix_memalign((void**)&vp, 8, atoi(argv[1])))){ if(vp) *vp = '\0'; else printf("x1\n"); }else printf("x2: %s\n",strerror(err)); return err != 0; } 0 0 Yet it is not because *vp is accessible, contradicting the standard -- no? (Exaggerating a bit, but the last two weeks for me and GNU: iconv(3) is special for ILSEQ on output [non-conforming], sysconf(3) PROCESSORS_ONLN is wrong in cgroup [echo 1-3 > /sys/fs/cgroup/NAME/cpuset.cpus], causing over-thread-usage in programs like xz and zstd [seems wrong and implementation-specific non-conforming], whereas those using nproc(1) are right, and now posix_memalign().) Ciao, and a nice Sunday everone (who can)! --steffen | |Der Kragenbaer, The moon bear, |der holt sich munter he cheerfully and one by one |einen nach dem anderen runter wa.ks himself off |(By Robert Gernhardt)