Automated sync from github.com/tensorflow/tensorflow (#2110)

BUG=automated sync from upstream
NO_CHECK_TFLITE_FILES=automated sync from upstream
diff --git a/tensorflow/lite/kernels/BUILD b/tensorflow/lite/kernels/BUILD
index 6e1efce..4b48db1 100644
--- a/tensorflow/lite/kernels/BUILD
+++ b/tensorflow/lite/kernels/BUILD
@@ -14,7 +14,7 @@
         "op_macros.h",
     ],
     copts = tflite_copts(),
-    deps = ["//tensorflow/lite/micro:debug_log"],
+    deps = ["//tensorflow/lite/micro:micro_log"],
 )
 
 cc_library(
diff --git a/tensorflow/lite/kernels/kernel_util.h b/tensorflow/lite/kernels/kernel_util.h
index 24061ab..e318118 100644
--- a/tensorflow/lite/kernels/kernel_util.h
+++ b/tensorflow/lite/kernels/kernel_util.h
@@ -24,6 +24,9 @@
 
 #include "tensorflow/lite/core/c/builtin_op_data.h"
 #include "tensorflow/lite/core/c/common.h"
+#ifndef NDEBUG
+#include "tensorflow/lite/kernels/op_macros.h"
+#endif
 
 namespace tflite {
 
@@ -165,26 +168,33 @@
 }
 #endif  // TF_LITE_STATIC_MEMORY
 
-inline int64_t NumElements(const TfLiteIntArray* dims) {
+inline int64_t NumElements(const int* dims, int num_dims) {
   int64_t count = 1;
-  for (int i = 0; i < dims->size; ++i) {
-    count *= dims->data[i];
+  for (int i = 0; i < num_dims; ++i) {
+#ifndef NDEBUG
+    if (count <= 0) {
+      break;
+    }
+    // Check that number of elements can fit in 32 bit int. Most of tflite
+    // assumes the result of `NumElements` is < MAX_INT and static or implicit
+    // casts to `int32_t` without any checks. It is more meaningful to check
+    // that the result fits into 32 bits than for standard overflow on 64 bit
+    // type.
+    TF_LITE_ASSERT(dims[i] < std::numeric_limits<int>::max() / count);
+#endif
+    count *= dims[i];
   }
   return count;
 }
 
+inline int64_t NumElements(const TfLiteIntArray* dims) {
+  return NumElements(dims->data, dims->size);
+}
+
 inline int64_t NumElements(const TfLiteTensor* t) {
   return NumElements(t->dims);
 }
 
-inline int64_t NumElements(const int* dims, int num_dims) {
-  int64_t count = 1;
-  for (int i = 0; i < num_dims; ++i) {
-    count *= dims[i];
-  }
-  return count;
-}
-
 // Determines whether tensor is constant.
 // TODO(b/138199592): Introduce new query which checks for constant OR
 // persistent-read-only, which would be useful for most tensor kernels that
diff --git a/tensorflow/lite/kernels/op_macros.h b/tensorflow/lite/kernels/op_macros.h
index 39e3119..9c4e2fd 100644
--- a/tensorflow/lite/kernels/op_macros.h
+++ b/tensorflow/lite/kernels/op_macros.h
@@ -15,11 +15,12 @@
 #ifndef TENSORFLOW_LITE_KERNELS_OP_MACROS_H_
 #define TENSORFLOW_LITE_KERNELS_OP_MACROS_H_
 
+#include "tensorflow/lite/micro/micro_log.h"
+
 #if !defined(TF_LITE_MCU_DEBUG_LOG)
 #include <cstdlib>
 #define TFLITE_ABORT abort()
 #else
-#include "tensorflow/lite/micro/micro_log.h"
 inline void AbortImpl() {
   MicroPrintf("HALTED");
   while (1) {
@@ -34,4 +35,15 @@
 #define TFLITE_ASSERT_FALSE TFLITE_ABORT
 #endif
 
+#define TF_LITE_FATAL(msg)    \
+  do {                        \
+    MicroPrintf("%s", (msg)); \
+    TFLITE_ABORT;             \
+  } while (0)
+
+#define TF_LITE_ASSERT(x)        \
+  do {                           \
+    if (!(x)) TF_LITE_FATAL(#x); \
+  } while (0)
+
 #endif  // TENSORFLOW_LITE_KERNELS_OP_MACROS_H_