From 3dfe34c7e0295f733449eb150745fbfdfecffb5b Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Sun, 3 Mar 2019 18:32:41 -0800 Subject: Make gl_pi_cr exception-safe It used to be the case that we could be interrupted or run out of memory after pushing nulls onto the two arrays capturing past computations, but before changing the nulls to real values. This left us in a corrupted state. Bug: 117705512 Test: crcalc tests, Calculator tests Change-Id: I35a5cf97967ede6fa298c178b5056461906bea1e --- src/com/hp/creals/CR.java | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/com/hp/creals/CR.java b/src/com/hp/creals/CR.java index 35ab0dc..c5a1c41 100644 --- a/src/com/hp/creals/CR.java +++ b/src/com/hp/creals/CR.java @@ -114,6 +114,8 @@ // Fix a couple of unused variable bugs. Notably selector_sign was // accidentally locally redeclared. (This turns out to be safe but useless.) // hboehm@google.com 11/20/2018. +// Fix an exception-safety issue in gl_pi_CR.approximate. +// hboehm@google.com 3/3/2019. package com.hp.creals; @@ -1577,6 +1579,11 @@ class gl_pi_CR extends slow_CR { private static CR SQRT_HALF = new sqrt_CR(ONE.shiftRight(1)); protected BigInteger approximate(int p) { + // Get us back into a consistent state if the last computation + // was interrupted after pushing onto b_prec. + if (b_prec.size() > b_val.size()) { + b_prec.remove(b_prec.size() - 1); + } // Rough approximations are easy. if (p >= 0) return scale(BigInteger.valueOf(3), -p); // We need roughly log2(p) iterations. Each iteration should @@ -1595,28 +1602,35 @@ class gl_pi_CR extends slow_CR { // Current values correspond to n, next_ values to n + 1 // b_prec.size() == b_val.size() >= n + 1 final BigInteger next_a = a.add(b).shiftRight(1); + final BigInteger next_b; final BigInteger a_diff = a.subtract(next_a); - CR next_b_as_CR; final BigInteger b_prod = a.multiply(b).shiftRight(-eval_prec); - // We the compute square root approximations using a nested + // We compute square root approximations using a nested // temporary CR computation, to avoid implementing BigInteger // square roots separately. final CR b_prod_as_CR = CR.valueOf(b_prod).shiftRight(-eval_prec); if (b_prec.size() == n + 1) { - // Need an n+1st slot. - b_prec.add(null); - b_val.add(null); - next_b_as_CR = b_prod_as_CR.sqrt(); + // Add an n+1st slot. + // Take care to make this exception-safe; b_prec and b_val + // must remain consistent, even if we are interrupted, or run + // out of memory. It's OK to just push on b_prec in that case. + final CR next_b_as_CR = b_prod_as_CR.sqrt(); + next_b = next_b_as_CR.get_appr(eval_prec); + final BigInteger scaled_next_b = scale(next_b, -extra_eval_prec); + b_prec.add(p); + b_val.add(scaled_next_b); } else { // Reuse previous approximation to reduce sqrt iterations, // hopefully to one. - next_b_as_CR = new sqrt_CR(b_prod_as_CR, b_prec.get(n + 1), - b_val.get(n + 1)); + final CR next_b_as_CR = + new sqrt_CR(b_prod_as_CR, + b_prec.get(n + 1), b_val.get(n + 1)); + next_b = next_b_as_CR.get_appr(eval_prec); + // We assume that set() doesn't throw for any reason. + b_prec.set(n + 1, p); + b_val.set(n + 1, scale(next_b, -extra_eval_prec)); } // b_prec.size() == b_val.size() >= n + 2 - final BigInteger next_b = next_b_as_CR.get_appr(eval_prec); - b_prec.set(n + 1, Integer.valueOf(p)); - b_val.set(n + 1, scale(next_b, -extra_eval_prec)); final BigInteger next_t = t.subtract(a_diff.multiply(a_diff) .shiftLeft(n + eval_prec)); // shift dist. usually neg. -- cgit v1.2.3 From 3e8c12aba8b34a9f00453569a0693b922f387f13 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Fri, 22 Mar 2019 10:53:21 -0700 Subject: Convert external/crcalc to Android.bp See build/soong/README.md for more information. Fixes: 122331934 Test: atest CRTests Change-Id: I28874e7c5327914929cdd0fa292664dd27031307 --- Android.bp | 26 ++++++++++++++++++++++++++ Android.mk | 32 -------------------------------- tests/Android.bp | 26 ++++++++++++++++++++++++++ tests/Android.mk | 30 ------------------------------ 4 files changed, 52 insertions(+), 62 deletions(-) create mode 100644 Android.bp delete mode 100644 Android.mk create mode 100644 tests/Android.bp delete mode 100644 tests/Android.mk diff --git a/Android.bp b/Android.bp new file mode 100644 index 0000000..3020f7b --- /dev/null +++ b/Android.bp @@ -0,0 +1,26 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Other files in this directory carry a different license. + +// build the client library +//------------------------------- +java_library { + name: "cr", + + srcs: ["src/**/*.java"], + + sdk_version: "19", + +} diff --git a/Android.mk b/Android.mk deleted file mode 100644 index b78e9e1..0000000 --- a/Android.mk +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (C) 2014 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# Other files in this directory carry a different license. - -LOCAL_PATH := $(call my-dir) - -# build the client library -#------------------------------- -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := $(call all-java-files-under, src) - -LOCAL_MODULE := cr -LOCAL_MODULE_TAGS := optional -LOCAL_SDK_VERSION := 19 - -include $(BUILD_STATIC_JAVA_LIBRARY) - -include $(call all-makefiles-under,$(LOCAL_PATH)) - diff --git a/tests/Android.bp b/tests/Android.bp new file mode 100644 index 0000000..c8e308c --- /dev/null +++ b/tests/Android.bp @@ -0,0 +1,26 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Other files in this directory carry a different license. + +// build the test apk +//------------------------------- +android_test { + name: "CRTests", + sdk_version: "19", + srcs: [ + "src/**/*.java", + ], + static_libs: ["cr"], +} diff --git a/tests/Android.mk b/tests/Android.mk deleted file mode 100644 index 441752e..0000000 --- a/tests/Android.mk +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (C) 2015 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# Other files in this directory carry a different license. - -LOCAL_PATH := $(call my-dir) - -# build the test apk -#------------------------------- -include $(CLEAR_VARS) - -LOCAL_PACKAGE_NAME := CRTests -LOCAL_SDK_VERSION := 19 -LOCAL_MODULE_TAGS := tests -LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-java-files-under, ../src) -# Empirically, LOCAL_INSTRUMENTATION_FOR doesn't work, perhaps because it -# expects an apk, not a library. - -include $(BUILD_PACKAGE) -- cgit v1.2.3