From 16661ba8c0103c2571e84a59a107c9e41dbe60dc Mon Sep 17 00:00:00 2001 From: Jeff McGlynn Date: Thu, 16 Aug 2018 16:53:26 -0700 Subject: Fix ASAN failures in integer_sequence_codec and partition Introduce UTILS_RELEASE_ASSERT, which crashes if the condition isn't met, even on release builds. Update integer_sequence_codec and partition to use the new tests to validate input parameters, and update the tests so that they expect the crash to occur even on release builds. Bug: 112691516, 112669735 Change-Id: Ic82edeffc64ca0f2b0d17f1c63563dfd8d9cdd71 --- src/base/BUILD.bazel | 1 + src/base/utils.h | 35 +++++++++++++++++++++++++ src/decoder/integer_sequence_codec.cc | 5 ++-- src/decoder/partition.cc | 7 ++--- src/decoder/test/integer_sequence_codec_test.cc | 5 ++-- tools/bazel.rc | 6 +++++ 6 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 src/base/utils.h create mode 100644 tools/bazel.rc diff --git a/src/base/BUILD.bazel b/src/base/BUILD.bazel index 9d8b9a0..09f723d 100644 --- a/src/base/BUILD.bazel +++ b/src/base/BUILD.bazel @@ -22,6 +22,7 @@ cc_library( "string_utils.h", "type_traits.h", "uint128.h", + "utils.h", ], visibility = ["//src/decoder:__pkg__"], ) diff --git a/src/base/utils.h b/src/base/utils.h new file mode 100644 index 0000000..0a4fabd --- /dev/null +++ b/src/base/utils.h @@ -0,0 +1,35 @@ +// Copyright 2018 Google LLC +// +// 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 +// +// https://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. + +#ifndef ASTC_CODEC_BASE_UTILS_H_ +#define ASTC_CODEC_BASE_UTILS_H_ + +#include +#include +#include + +#ifdef NDEBUG +#define UTILS_RELEASE_ASSERT(x) \ + do { \ + const bool result = (x); \ + if (!result) { \ + fprintf(stderr, "Error: UTILS_RELEASE_ASSERT failed: %s\n", #x); \ + abort(); \ + } \ + } while (false) +#else +#define UTILS_RELEASE_ASSERT(x) assert(x) +#endif + +#endif // ASTC_CODEC_BASE_UTILS_H_ diff --git a/src/decoder/integer_sequence_codec.cc b/src/decoder/integer_sequence_codec.cc index da7bc56..83c0359 100644 --- a/src/decoder/integer_sequence_codec.cc +++ b/src/decoder/integer_sequence_codec.cc @@ -14,6 +14,7 @@ #include "src/decoder/integer_sequence_codec.h" #include "src/base/math_utils.h" +#include "src/base/utils.h" #include #include @@ -383,8 +384,8 @@ void IntegerSequenceCodec::GetCountsForRange( // These are generally errors -- there should never be any ASTC values // outside of this range - assert(range > 0); - assert(range < 1 << kLog2MaxRangeForBits); + UTILS_RELEASE_ASSERT(range > 0); + UTILS_RELEASE_ASSERT(range < 1 << kLog2MaxRangeForBits); *bits = 0; *trits = 0; diff --git a/src/decoder/partition.cc b/src/decoder/partition.cc index 90d39fd..43ff6f0 100644 --- a/src/decoder/partition.cc +++ b/src/decoder/partition.cc @@ -14,6 +14,7 @@ #include "src/decoder/partition.h" #include "src/base/bottom_n.h" +#include "src/base/utils.h" #include "src/decoder/footprint.h" #include @@ -399,12 +400,12 @@ constexpr int EncodeDims(int width, int height) { int PartitionMetric(const Partition& a, const Partition& b) { // Make sure that one partition is at least a subset of the other... - assert(a.footprint == b.footprint); + UTILS_RELEASE_ASSERT(a.footprint == b.footprint); // Make sure that the number of parts is within our limits. ASTC has a maximum // of four subsets per block according to the specification. - assert(a.num_parts <= kMaxNumSubsets); - assert(b.num_parts <= kMaxNumSubsets); + UTILS_RELEASE_ASSERT(a.num_parts <= kMaxNumSubsets); + UTILS_RELEASE_ASSERT(b.num_parts <= kMaxNumSubsets); const int w = a.footprint.Width(); const int h = b.footprint.Height(); diff --git a/src/decoder/test/integer_sequence_codec_test.cc b/src/decoder/test/integer_sequence_codec_test.cc index b66ff2b..120e8b0 100644 --- a/src/decoder/test/integer_sequence_codec_test.cc +++ b/src/decoder/test/integer_sequence_codec_test.cc @@ -74,9 +74,8 @@ TEST(ASTCIntegerSequenceCodecTest, TestGetCountsForRange) { EXPECT_EQ(b, kExpectedCounts[i - 1][2]); } - ASSERT_DEBUG_DEATH(IntegerSequenceCodec::GetCountsForRange(0, &t, &q, &b), ""); - ASSERT_DEBUG_DEATH( - IntegerSequenceCodec::GetCountsForRange(256, &t, &q, &b), ""); + ASSERT_DEATH(IntegerSequenceCodec::GetCountsForRange(0, &t, &q, &b), ""); + ASSERT_DEATH(IntegerSequenceCodec::GetCountsForRange(256, &t, &q, &b), ""); IntegerSequenceCodec::GetCountsForRange(1, &t, &q, &b); EXPECT_EQ(t, 0); diff --git a/tools/bazel.rc b/tools/bazel.rc new file mode 100644 index 0000000..e47b006 --- /dev/null +++ b/tools/bazel.rc @@ -0,0 +1,6 @@ +# For building with the clang-specific flavor of ASAN. +build:clang-asan --client_env=CC=clang-5.0 +build:clang-asan --copt -g3 +build:clang-asan --copt -fsanitize=address +build:clang-asan --linkopt -fsanitize=address +build:clang-asan --copt -fno-omit-frame-pointer -- cgit v1.2.3