From patchwork Tue Oct 24 11:37:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 78461 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 447B03858433 for ; Tue, 24 Oct 2023 11:37:42 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by sourceware.org (Postfix) with ESMTPS id A64823858C50 for ; Tue, 24 Oct 2023 11:37:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A64823858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A64823858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::529 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698147448; cv=none; b=QpGKTMrpLX/0fswDsb09XUaHBUh3tdUFRCP9XQRaGqPNATIxgyV729vh8bXI9tTxHMc/QYXI3ql2Wwt+RQX4AFgl8tSZshCc8X3X9Hecax8tuOMQACFIYCExuTkmB617R/+8kFZDrWZ4yYXL585iM0q4ulne2/P+CaTQF8ED/7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698147448; c=relaxed/simple; bh=BoHvN498rhA/tBAAwfSg43+0rL0NtgPiLxrEzE2+mgk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=L2i2pfy/oK8FpEZyRZ5lBAiFLwowZi6aaucDLfFeCthDLpa/XJMkQ1he4vKjrF+acL3x8Yy8e0UOMmgpMVpmjywYxhMr8p9Zvy5meRYdKd885OapqQsvEMz3EP+yUP6L62PcxFJD1aAfY9cIbIt6PuHDyFO9YUZRsS9D7eYJ6ag= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-5b8f68ba4e5so112844a12.1 for ; Tue, 24 Oct 2023 04:37:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698147445; x=1698752245; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NOvdAysXA05qPpMnEVH8JkFq4y3flQAfl9BCWlDaPAI=; b=unUOqII56St3bNkRekE9UyerBJNve2LMk49G1qvwN/d7L4noaopXZJqS8LntdFvDaT zbjx3X5XejjnIS5W/02ZetCPvPHjIwrxEPrM35wKXRn1rmd2vnhvxrncUcCU5qOvhWTA d5JjhnIPv3+vBJnsByaQ1CFMloN4JoqCYH7k4+ga+zzczMjUN2VxIttE83/cCIARCEeq F5WZWIemUITmeYCBy0RvjHrKwt428QzcX8f87Xg/Fy40SLp+Aap00wyIxWmwW9qGe9ue n0SU/6QPPwJnpZrHCkJCIgRs/zyayjCa3De0txPkJo4GWdxekAzzjB2rpYgbUy/HiYkb h8Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698147445; x=1698752245; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NOvdAysXA05qPpMnEVH8JkFq4y3flQAfl9BCWlDaPAI=; b=LBN0OrPyVj9jikRZNkUAKhzXbVQAhwvkBxD6Cnr//Cxw+OQuMafnOsKfVqWpaQiAcd mHaf72oCZeoY4BSTkUrq666Nptg5UWxN7iTDzUtldlIsjmyF65I0MEMfZM5WaXbmnS+/ L46QR+kt5yzCw+gnva1q/tdCOHSokBpO932k2A/P7V47yYaWb6CPo3FhvjD9itnrlVIb EIdSlhMahUyXsTchxKIOnMj3jDU1NrhUtKK++EPtyTzRgb0MxTt0DQv7NqVWjsIPeE+H swb7UxcBJhNh9DneLogRfcRw9ZK+p2B6+uhI4OtT2MDx0URn1uTRZ5D4qyDlBUwmIrC9 1EFQ== X-Gm-Message-State: AOJu0YzFAlXs7EaTg58Qdxv/gB9OBl475229z+iEi8y3nIXHV3kN4crX UnD5VMBSzkDkM+lDZ1GLerMlwx0dAeeBcvbscvmviQ== X-Google-Smtp-Source: AGHT+IHfMP/8YbkoLcua05bk17+E7vJDDAAlJmcOqE9o1kSkgprGP0ANbIiE9Sz6XJJPgapi7J+YGQ== X-Received: by 2002:a05:6a20:7287:b0:17b:2b7e:923c with SMTP id o7-20020a056a20728700b0017b2b7e923cmr2651299pzk.16.1698147445144; Tue, 24 Oct 2023 04:37:25 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:a647:44d5:84b0:a3f2:fe73]) by smtp.gmail.com with ESMTPSA id k28-20020aa79d1c000000b006bde2480806sm7506663pfp.47.2023.10.24.04.37.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:37:24 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Bruno Haible Subject: [PATCH v2 1/3] powerpc: Do not raise exception traps for fesetexcept/fesetexceptflag (BZ 30988) Date: Tue, 24 Oct 2023 08:37:14 -0300 Message-Id: <20231024113716.3911015-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024113716.3911015-1-adhemerval.zanella@linaro.org> References: <20231024113716.3911015-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org According to ISO C23 (7.6.4.4), fesetexcept is supposed to set floating-point exception flags without raising a trap (unlike feraiseexcept, which is supposed to raise a trap if feenableexcept was called with the appropriate argument). This is a side-effect of how we implement the GNU extension feenableexcept, where feenableexcept/fesetenv/fesetmode/feupdateenv might issue prctl (PR_SET_FPEXC, PR_FP_EXC_PRECISE) depending of the argument. And on PR_FP_EXC_PRECISE, setting a floating-point exception flag triggers a trap. To make the both functions follow the C23, fesetexcept and fesetexceptflag now fail if the argument may trigger a trap. The math tests now check for an value different than 0, instead of bail out as unsupported for EXCEPTION_SET_FORCES_TRAP. Checked on powerpc64le-linux-gnu. --- math/test-fesetexcept-traps.c | 11 ++++------- math/test-fexcept-traps.c | 11 ++++------- sysdeps/powerpc/fpu/fesetexcept.c | 5 +++++ sysdeps/powerpc/fpu/fsetexcptflg.c | 9 ++++++++- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/math/test-fesetexcept-traps.c b/math/test-fesetexcept-traps.c index 71b6e45b33..96f6c4752f 100644 --- a/math/test-fesetexcept-traps.c +++ b/math/test-fesetexcept-traps.c @@ -39,16 +39,13 @@ do_test (void) return result; } - if (EXCEPTION_SET_FORCES_TRAP) - { - puts ("setting exceptions traps, cannot test on this architecture"); - return 77; - } - /* Verify fesetexcept does not cause exception traps. */ + /* Verify fesetexcept does not cause exception traps. For architectures + where setting the exception might result in traps the function should + return a nonzero value. */ ret = fesetexcept (FE_ALL_EXCEPT); if (ret == 0) puts ("fesetexcept (FE_ALL_EXCEPT) succeeded"); - else + else if (!EXCEPTION_SET_FORCES_TRAP) { puts ("fesetexcept (FE_ALL_EXCEPT) failed"); if (EXCEPTION_TESTS (float)) diff --git a/math/test-fexcept-traps.c b/math/test-fexcept-traps.c index 9701c3c320..9b8f583ae6 100644 --- a/math/test-fexcept-traps.c +++ b/math/test-fexcept-traps.c @@ -63,14 +63,11 @@ do_test (void) result = 1; } - if (EXCEPTION_SET_FORCES_TRAP) - { - puts ("setting exceptions traps, cannot test on this architecture"); - return 77; - } - /* The test is that this does not cause exception traps. */ + /* The test is that this does not cause exception traps. For architectures + where setting the exception might result in traps the function should + return a nonzero value. */ ret = fesetexceptflag (&saved, FE_ALL_EXCEPT); - if (ret != 0) + if (ret != 0 && !EXCEPTION_SET_FORCES_TRAP) { puts ("fesetexceptflag failed"); result = 1; diff --git a/sysdeps/powerpc/fpu/fesetexcept.c b/sysdeps/powerpc/fpu/fesetexcept.c index 609a148a95..2850156d3a 100644 --- a/sysdeps/powerpc/fpu/fesetexcept.c +++ b/sysdeps/powerpc/fpu/fesetexcept.c @@ -31,6 +31,11 @@ fesetexcept (int excepts) & FE_INVALID_SOFTWARE)); if (n.l != u.l) { + if (n.l & fenv_exceptions_to_reg (excepts)) + /* Setting the exception flags may trigger a trap. ISO C 23 § 7.6.4.4 + does not allow it. */ + return -1; + fesetenv_register (n.fenv); /* Deal with FE_INVALID_SOFTWARE not being implemented on some chips. */ diff --git a/sysdeps/powerpc/fpu/fsetexcptflg.c b/sysdeps/powerpc/fpu/fsetexcptflg.c index 2b22f913c0..6517e8ea03 100644 --- a/sysdeps/powerpc/fpu/fsetexcptflg.c +++ b/sysdeps/powerpc/fpu/fsetexcptflg.c @@ -44,7 +44,14 @@ __fesetexceptflag (const fexcept_t *flagp, int excepts) This may cause floating-point exceptions if the restored state requests it. */ if (n.l != u.l) - fesetenv_register (n.fenv); + { + if (n.l & fenv_exceptions_to_reg (excepts)) + /* Setting the exception flags may trigger a trap. ISO C 23 § 7.6.4.4 + does not allow it. */ + return -1; + + fesetenv_register (n.fenv); + } /* Deal with FE_INVALID_SOFTWARE not being implemented on some chips. */ if (flag & FE_INVALID)