make float divide-by-zero fatal
Change-Id: I9ba1caa4862bdf9ffc9c0e637bd69cce91fd8468
Reviewed-on: https://skia-review.googlesource.com/c/168740
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index e93bb76..6201792 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -226,8 +226,6 @@
# or pass one of the couple common aliases used by the bots.
sanitizers = sanitize
- # fyi_sanitizers only print out stacktraces of their issues
- fyi_sanitizers = fyi_sanitize
if (sanitize == "ASAN" || sanitize == "UBSAN") {
# ASAN implicitly runs all UBSAN checks also.
sanitizers = "undefined"
@@ -235,11 +233,6 @@
sanitizers += ",address"
}
- # A whitelist of checks we can't yet pass.
- if (fyi_sanitize == "" && !is_android) {
- fyi_sanitizers = "float-divide-by-zero"
- }
-
if (is_android) {
# TODO(mtklein): work out UBSAN link errors
sanitizers = "address"
@@ -255,7 +248,6 @@
cflags += [
"-fsanitize=$sanitizers",
"-fno-sanitize-recover=$sanitizers",
- "-fsanitize-recover=$fyi_sanitizers",
"-fsanitize-blacklist=$_blacklist",
]
ldflags += [ "-fsanitize=$sanitizers" ]
diff --git a/gn/BUILDCONFIG.gn b/gn/BUILDCONFIG.gn
index 9ab3637..031839d 100644
--- a/gn/BUILDCONFIG.gn
+++ b/gn/BUILDCONFIG.gn
@@ -20,7 +20,6 @@
}
sanitize = ""
- fyi_sanitize = ""
ar = "ar"
cc = "cc"
diff --git a/include/core/SkScalar.h b/include/core/SkScalar.h
index 938f698..8adf7cb 100644
--- a/include/core/SkScalar.h
+++ b/include/core/SkScalar.h
@@ -122,9 +122,9 @@
static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
-#define SkScalarDiv(numer, denom) sk_ieee_float_divide(numer, denom)
-#define SkScalarInvert(x) sk_ieee_float_divide(SK_Scalar1, (x))
-#define SkScalarFastInvert(x) sk_ieee_float_divide(SK_Scalar1, (x))
+#define SkScalarDiv(numer, denom) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(numer, denom)
+#define SkScalarInvert(x) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
+#define SkScalarFastInvert(x) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
#define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
#define SkScalarHalf(a) ((a) * SK_ScalarHalf)
diff --git a/include/private/SkFloatingPoint.h b/include/private/SkFloatingPoint.h
index 1787494..ba30475 100644
--- a/include/private/SkFloatingPoint.h
+++ b/include/private/SkFloatingPoint.h
@@ -210,4 +210,12 @@
return numer / denom;
}
+// While we clean up divide by zero, we'll replace places that do divide by zero with this TODO.
+static inline float sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(float n, float d) {
+ return sk_ieee_float_divide(n,d);
+}
+static inline float sk_ieee_double_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(double n, double d) {
+ return sk_ieee_double_divide(n,d);
+}
+
#endif
diff --git a/src/core/SkGaussFilter.cpp b/src/core/SkGaussFilter.cpp
index a134b06..1b5c06f 100644
--- a/src/core/SkGaussFilter.cpp
+++ b/src/core/SkGaussFilter.cpp
@@ -6,10 +6,10 @@
*/
+#include "SkFloatingPoint.h"
#include "SkGaussFilter.h"
-
-#include <cmath>
#include "SkTypes.h"
+#include <cmath>
static constexpr double kPi = 3.14159265358979323846264338327950288;
@@ -115,8 +115,8 @@
// Use the recursion relation from "Incremental Computation of the Gaussian" by Ken
// Turkowski in GPUGems 3. Page 877.
- double g0 = 1.0 / normalizeDenom;
- double g1 = std::exp(1.0 / expGaussDenom);
+ double g0 = sk_ieee_double_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(1.0, normalizeDenom);
+ double g1 = std::exp(sk_ieee_double_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(1.0, expGaussDenom));
double g2 = g1 * g1;
gauss[0] = g0;
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index ac8a4fc..66035b5 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -346,9 +346,9 @@
}
void SkGlyphCache::AddLine(const SkPoint pts[2], SkScalar axis, bool yAxis,
- SkGlyph::Intercept* intercept) {
- SkScalar t = yAxis ? (axis - pts[0].fX) / (pts[1].fX - pts[0].fX)
- : (axis - pts[0].fY) / (pts[1].fY - pts[0].fY);
+ SkGlyph::Intercept* intercept) {
+ SkScalar t = yAxis ? sk_ieee_float_divide(axis - pts[0].fX, pts[1].fX - pts[0].fX)
+ : sk_ieee_float_divide(axis - pts[0].fY, pts[1].fY - pts[0].fY);
if (0 <= t && t < 1) { // this handles divide by zero above
AddInterval(yAxis ? pts[0].fY + t * (pts[1].fY - pts[0].fY)
: pts[0].fX + t * (pts[1].fX - pts[0].fX), intercept);
diff --git a/src/shaders/gradients/Sk4fLinearGradient.cpp b/src/shaders/gradients/Sk4fLinearGradient.cpp
index 1575741..cb830f7 100644
--- a/src/shaders/gradients/Sk4fLinearGradient.cpp
+++ b/src/shaders/gradients/Sk4fLinearGradient.cpp
@@ -310,7 +310,7 @@
SkScalar currentAdvance() const {
SkASSERT(fAdvX >= 0);
- SkASSERT(fAdvX <= (fInterval->fT1 - fInterval->fT0) / fDx || !std::isfinite(fAdvX));
+ SkASSERT(!std::isfinite(fAdvX) || fAdvX <= (fInterval->fT1 - fInterval->fT0) / fDx);
return fAdvX;
}
diff --git a/tests/InfRectTest.cpp b/tests/InfRectTest.cpp
index 966f67f..af00bc9 100644
--- a/tests/InfRectTest.cpp
+++ b/tests/InfRectTest.cpp
@@ -5,14 +5,11 @@
* found in the LICENSE file.
*/
+#include "SkFloatingPoint.h"
#include "SkRandom.h"
#include "SkRect.h"
#include "Test.h"
-static float make_zero() {
- return sk_float_sin(0);
-}
-
static void check_invalid(skiatest::Reporter* reporter,
SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
SkRect rect;
@@ -23,8 +20,8 @@
// Tests that isFinite() will reject any rect with +/-inf values
// as one of its coordinates.
DEF_TEST(InfRect, reporter) {
- float inf = 1 / make_zero(); // infinity
- float nan = inf * 0;
+ float inf = SK_FloatInfinity;
+ float nan = SK_FloatNaN;
SkASSERT(!(nan == nan));
SkScalar small = SkIntToScalar(10);
SkScalar big = SkIntToScalar(100);
diff --git a/tests/ScaleToSidesTest.cpp b/tests/ScaleToSidesTest.cpp
index 79391c1..b56df0d 100644
--- a/tests/ScaleToSidesTest.cpp
+++ b/tests/ScaleToSidesTest.cpp
@@ -50,7 +50,7 @@
float radius1 = (float)interestingValues[i];
float radius2 = (float)interestingValues[j];
double width = interestingValues[k];
- double scale = width / ((double)radius1 + (double)radius2);
+ double scale = sk_ieee_double_divide(width, (double)radius1 + (double)radius2);
if (width > 0.0) {
if (s != 0) {
scale = std::min(scale, interestingValues[s-1]);
diff --git a/tools/Stats.h b/tools/Stats.h
index 040ddb4..ecd5d74 100644
--- a/tools/Stats.h
+++ b/tools/Stats.h
@@ -8,6 +8,7 @@
#ifndef Stats_DEFINED
#define Stats_DEFINED
+#include "SkFloatingPoint.h"
#include "SkString.h"
#include "SkTSort.h"
@@ -42,7 +43,7 @@
for (int i = 0 ; i < n; i++) {
err += (samples[i] - mean) * (samples[i] - mean);
}
- var = err / (n-1);
+ var = sk_ieee_double_divide(err, n-1);
SkAutoTMalloc<double> sorted(n);
memcpy(sorted.get(), samples.begin(), n * sizeof(double));