aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/util/overflow_test.cc
diff options
context:
space:
mode:
authorGravatar Geoffrey Irving <geoffreyi@google.com>2017-03-30 17:34:30 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-03-30 18:48:13 -0700
commite8ee5286a686c6fc3057ba7cf9ba9ef7003789a6 (patch)
tree4e15373bce36c1327573a178c2c97ecaa176dd35 /tensorflow/core/util/overflow_test.cc
parent35e3f6c44f5edcb659519b444e1aa5215c9e36b0 (diff)
Remove 2**40 size limit on TensorShape and harden to overflow
The limit was preventing valid uses of TensorShape as the dense shape of very large sparse tensors. There's no security advantage to the limit, since a memory allocation of 2**40 bytes is already far beyond a reasonable machine size. The new limit is std::numeric_limits<int64>::max(). In addition, the previous TensorShape code did not check for overflow when multiplying, which meant an operation as simple as tf.gather(tf.zeros([2**5, 2**60 + 1]), 7).eval() would appear as valid during TensorShape construction and then crash. A new MultiplyWithoutOverflow function does the correct overflow checking. Fixes #8494. Change: 151778176
Diffstat (limited to 'tensorflow/core/util/overflow_test.cc')
-rw-r--r--tensorflow/core/util/overflow_test.cc60
1 files changed, 60 insertions, 0 deletions
diff --git a/tensorflow/core/util/overflow_test.cc b/tensorflow/core/util/overflow_test.cc
new file mode 100644
index 0000000000..627f77164e
--- /dev/null
+++ b/tensorflow/core/util/overflow_test.cc
@@ -0,0 +1,60 @@
+/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
+
+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.
+==============================================================================*/
+
+#include "tensorflow/core/util/overflow.h"
+#include <cmath>
+#include "tensorflow/core/platform/macros.h"
+#include "tensorflow/core/platform/test.h"
+
+namespace tensorflow {
+namespace {
+
+TEST(OverflowTest, Nonnegative) {
+ // Various interesting values
+ std::vector<int64> interesting = {0, std::numeric_limits<int64>::max()};
+ for (int i = 0; i < 63; i++) {
+ int64 bit = static_cast<int64>(1) << i;
+ interesting.push_back(bit);
+ interesting.push_back(bit + 1);
+ interesting.push_back(bit - 1);
+ }
+ auto mid = static_cast<int64>(std::pow(2, 63.0 / 2));
+ for (int i = -5; i < 5; i++) interesting.push_back(mid + i);
+
+ // Check all pairs
+ for (auto x : interesting) {
+ for (auto y : interesting) {
+ int64 xy = MultiplyWithoutOverflow(x, y);
+ long double dxy = static_cast<long double>(x) * y;
+ if (dxy > std::numeric_limits<int64>::max()) {
+ EXPECT_LT(xy, 0);
+ } else {
+ EXPECT_EQ(dxy, xy);
+ }
+ }
+ }
+}
+
+TEST(OverflowTest, Negative) {
+ const int64 negatives[] = {-1, std::numeric_limits<int64>::min()};
+ for (const int64 n : negatives) {
+ EXPECT_DEATH(MultiplyWithoutOverflow(n, 0), "");
+ EXPECT_DEATH(MultiplyWithoutOverflow(0, n), "");
+ EXPECT_DEATH(MultiplyWithoutOverflow(n, n), "");
+ }
+}
+
+} // namespace
+} // namespace tensorflow