From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 85243384EF79 for ; Wed, 14 Dec 2022 02:42:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 85243384EF79 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670985724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C3a/1LtAmDHoVOlu1Ckl8RL62PohovhDlp96KocA4ZI=; b=AkVgI3GI/U6G8VTJj1JvBAqO0STgstELr6jdjWayRL1WXH6B0rStQqnRYq0kPT8AqGymn7 ExCHjfZAnyZjQD/n2mk15Bu96M6C1/+fchO3wSyCScETA3jVgcamL0WyxHxGvyVOPJreIZ SyCOxsL45v4xGkGAqWVJzydtPNBaWgk= Received: from mail-il1-f199.google.com (mail-il1-f199.google.com [209.85.166.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-180-cjVcmmgKN6SqH0k2GdoNiA-1; Tue, 13 Dec 2022 21:41:39 -0500 X-MC-Unique: cjVcmmgKN6SqH0k2GdoNiA-1 Received: by mail-il1-f199.google.com with SMTP id l3-20020a056e021aa300b00304be32e9e5so4642019ilv.12 for ; Tue, 13 Dec 2022 18:41:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=C3a/1LtAmDHoVOlu1Ckl8RL62PohovhDlp96KocA4ZI=; b=FBplEPNEiwZ1cpc77lDlK7HXlNIG3+f1UAWRAtcYaksA0yCdurJMkAmcURcNK8K50R kj2FrZ2HXHuYLghOwjOXGYB7kYc1paOWVBDpsnLoGvHBaUasDTy9w+QP5NAyyk4MBMNa QzsgkZMcKUH+vT9klTQgsS3HcHAWDLCJPfPVQ6spDuLc8xrSeHN1BGGIDk0jGwXVh1Wd 7Q9stvT+u/pDgDKKCvGiG9Hk+5ovTXAvbaKbgk8K8ZTYHY7zYrc25DU7K11rd5Qa1WMH cKYXjx6aF/CkGnvYGw+4lsD1p9KJSgQlhfy2tN+eO0wMU/LL6nDegD8HoTGagFAYEWgO Eekw== X-Gm-Message-State: ANoB5pk1JAe5oXKrObj5WKE/s8WDIxgr9W9JGsMfBNzXxMhnratpTGUv oQOK2hYq7HizNoBWxSQOZcTqwdG0XbNSRiRPokYhMYezrFNQoUaD5Eg2UUIF6c0mRwNgVrGpHiM xOx610FLCQQAeAOaTCywg X-Received: by 2002:a92:d108:0:b0:302:e2d7:b4c0 with SMTP id a8-20020a92d108000000b00302e2d7b4c0mr12857448ilb.5.1670985698375; Tue, 13 Dec 2022 18:41:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf7PRMkDZnX2wTcmvaI9ybaFFMI2UeCAH1+RDlWSrSBLe9l50N7T0CqDK8uG/UwjYIEpIRtw6w== X-Received: by 2002:a92:d108:0:b0:302:e2d7:b4c0 with SMTP id a8-20020a92d108000000b00302e2d7b4c0mr12857437ilb.5.1670985698029; Tue, 13 Dec 2022 18:41:38 -0800 (PST) Received: from [192.168.0.241] (192-0-145-146.cpe.teksavvy.com. [192.0.145.146]) by smtp.gmail.com with ESMTPSA id x17-20020a0566380cb100b00374fe4f0bc3sm1337220jad.158.2022.12.13.18.41.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 13 Dec 2022 18:41:37 -0800 (PST) Message-ID: <8dc9f68a-c736-0996-76f8-ab7aeb4ccc00@redhat.com> Date: Tue, 13 Dec 2022 21:41:36 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: [PATCH v1] x86: Prevent SIG11 in memcmp-sse2 when data is concurrently modified [BZ #29863] To: Noah Goldstein , libc-alpha@sourceware.org Cc: hjl.tools@gmail.com, carlos@systemhalted.org References: <20221214001147.2814047-1-goldstein.w.n@gmail.com> From: Carlos O'Donell Organization: Red Hat In-Reply-To: <20221214001147.2814047-1-goldstein.w.n@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Please post a v2. Thanks! Subject: x86: Prevent SIGSEGV in memcmp-sse2 when data is concurrently modified [BZ #29863] Replaces SIG11 with SIGSEGV (the documented name of the signal). On 12/13/22 19:11, Noah Goldstein via Libc-alpha wrote: > In the case of INCORRECT usage of `memcmp(a, b, N)` where `a` and `b` > are concurrently modified as `memcmp` runs, there can be a SIG11 in s/SIG11/SIGSEGV/g > `L(ret_nonzero_vec_end_0)` because the sequential logic assumes > that `(rdx - 32 + rax)` is a positive 32-bit integer. > > To be clear, this "fix" does not mean this usage of `memcmp` is > supported. `memcmp` is incorrect when the values of `a` and/or `b` > are modified while its running, and that incorrectness may manifest > itself as a SIG-11. That being said, if we can make the results s/SIG-11/SIGSEGV/g > less dramatic with no cost to regular uses cases, there is no harm > in doing so. I agree that a user focused change like this is going to be a balance between keeping it working for an unsupported use case versus the cost to the library. Given that you've found a low-cost way to support the incorrect but idiomatic use case then I have no sustained objections to this patch. However, this won't be the last we hear of this as we continue down the path of optimizing against a well defined memory model. > The fix replaces a 32-bit `addl %edx, %eax` with the 64-bit variant > `addq %rdx, %rax`. The 1-extra byte of code size from using the > 64-bit instruction doesn't contribute to overall code size as the > next target is aligned and has multiple bytes of `nop` padding > before it. As well all the logic between the add and `ret` still > fits in the same fetch block, so the cost of this change is > basically zero. OK. > The sequential logic makes the assume behind the following code: Suggest: The relevant sequential logic can be seen in the following code: > ``` > /* > * rsi = a > * rdi = b > * rdx = len - 32 > */ > /* cmp a[0:15] and b[0:15]. Since length is known to be [17, 32] > in this case, this check is also assume to cover a[0:(31 - len)] s/assume/assumed/g > and b[0:(31 - len)]. */ > movups (%rsi), %xmm0 > movups (%rdi), %xmm1 > PCMPEQ %xmm0, %xmm1 > pmovmskb %xmm1, %eax > subl %ecx, %eax > jnz L(END_NEQ) > > /* cmp a[len-16:len-1] and b[len-16:len-1]. */ > movups 16(%rsi, %rdx), %xmm0 > movups 16(%rdi, %rdx), %xmm1 > PCMPEQ %xmm0, %xmm1 > pmovmskb %xmm1, %eax > subl %ecx, %eax > jnz L(END_NEQ2) > ret > > L(END2): > /* Position first mismatch. */ > bsfl %eax, %eax > > /* BUG IS FROM THIS. The sequential version is able to assume this s/BUG IS FROM THIS. //g > value is a positive 32-bit value because first check included s/because first/because the first/g > bytes in range a[0:(31 - len)], b[0:(31 - len)] so `eax` must be s/,/ and/g > greater than `31 - len` so the minimum value of `edx` + `eax` is > `(len - 32) + (32 - len) >= 0`. In the concurrent case, however, > `a` or `b` could have been changed so a mismatch in `eax` less or > equal than `(31 - len)` is possible (the new low bound in `(16 - s/in/is/g > len)`. This can result in a negative 32-bit signed integer, which > when non-sign extended to 64-bits is a random large value out of s/out of/that is out of/g > bounds. */ > addl %edx, %eax > > /* Crash here because 32-bit negative number in `eax` non-sign > extends to out of bounds 64-bit offset. */ > movzbl 16(%rdi, %rax), %ecx > movzbl 16(%rsi, %rax), %eax > ``` > > This fix is quite simple, just make the `addl %edx, %eax` 64 bit (i.e > `addq %rdx, %rax`). This prevent the 32-bit non-sign extension s/prevent/prevents/g > and since `eax` still a low bound of `16 - len` the `rdx + rax` s/still/is still/g > is bound by `(len - 32) - (16 - len) >= -16`. Since we have a > fixed offset of `16` in the memory access this must be inbounds. s/inbounds/in bounds/g > --- > sysdeps/x86_64/multiarch/memcmp-sse2.S | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/sysdeps/x86_64/multiarch/memcmp-sse2.S b/sysdeps/x86_64/multiarch/memcmp-sse2.S > index afd450d020..34e60e567d 100644 > --- a/sysdeps/x86_64/multiarch/memcmp-sse2.S > +++ b/sysdeps/x86_64/multiarch/memcmp-sse2.S > @@ -308,7 +308,7 @@ L(ret_nonzero_vec_end_0): > setg %dl > leal -1(%rdx, %rdx), %eax > # else > - addl %edx, %eax > + addq %rdx, %rax OK. 64-bit addq. > movzbl (VEC_SIZE * -1 + SIZE_OFFSET)(%rsi, %rax), %ecx > movzbl (VEC_SIZE * -1 + SIZE_OFFSET)(%rdi, %rax), %eax > subl %ecx, %eax -- Cheers, Carlos.