Bug in memory out-of-bound related to kMaxDim of RuntimeShape at tflite kernels
Hmmm.. I'm not sure but the error occurred at the following codes.
```c++
// tensorflow/lite/kernels/internal/runtime_shape.h L179-L180
std::memcpy(DimsData() + size_increase, shape.DimsData(),
                sizeof(int32_t) * shape.DimensionsCount());
```

I think there could be three candidates that cause an error.
1. Caller fault
2. Callee fault
3. Compiler bug

When it comes to the third one, I think the compiler doesn't evaluate properly `DimsData` which should return `dims_` or `dims_pointer` according to its size. Well, but I don't know exact reason. I just show you trial and error codes where the build is successful with that codes, which might help you guess what causes an errorr.

1. Just change `kMaxSmallSize` to 8 instead of 5.
4. Make `dims_` a pointer.

<details>
<summary> diff </summary>

```diff
diff --git a/tensorflow/lite/kernels/internal/runtime_shape.h b/tensorflow/lite/kernels/internal/runtime_shape.h
index 855c305d270..3c1e0783465 100644
--- a/tensorflow/lite/kernels/internal/runtime_shape.h
+++ b/tensorflow/lite/kernels/internal/runtime_shape.h
@@ -44,6 +44,9 @@ class RuntimeShape {
     if (dimensions_count > kMaxSmallSize) {
       dims_pointer_ = new int32_t[dimensions_count];
     }
+    else {
+      dims_ = new int32_t[kMaxSmallSize];
+    }
   }
 
   RuntimeShape(int shape_size, int32_t value) : size_(0) {
@@ -67,6 +70,9 @@ class RuntimeShape {
     if (size_ > kMaxSmallSize) {
       dims_pointer_ = new int32_t[size_];
     }
+    else {
+      dims_ = new int32_t[kMaxSmallSize];
+    }
     std::memcpy(DimsData(), other.DimsData(), sizeof(int32_t) * size_);
   }
 
@@ -80,6 +86,10 @@ class RuntimeShape {
     if (size_ > kMaxSmallSize) {
       delete[] dims_pointer_;
     }
+    else {
+      if (size_ != 0)
+        delete[] dims_;
+    }
   }
 
   inline int32_t DimensionsCount() const { return size_; }
@@ -115,6 +125,9 @@ class RuntimeShape {
     if (dimensions_count > kMaxSmallSize) {
       dims_pointer_ = new int32_t[dimensions_count];
     }
+    else {
+      dims_ = new int32_t[kMaxSmallSize];
+    }
   }
 
   inline void ReplaceWith(int dimensions_count, const int32_t* dims_data) {
@@ -182,7 +195,7 @@ class RuntimeShape {
 
   int32_t size_;
   union {
-    int32_t dims_[kMaxSmallSize];
+    int32_t* dims_;
     int32_t* dims_pointer_;
   };
 };
```

</details>

4. Comment out `input_shape.Dims(i)` part in `CopyDimsToDesc`.

<details>
<summary> diff </summary>

```diff
diff --git a/tensorflow/lite/kernels/internal/common.h b/tensorflow/lite/kernels/internal/common.h
index 5e8778f183e..80e4589909d 100644
--- a/tensorflow/lite/kernels/internal/common.h
+++ b/tensorflow/lite/kernels/internal/common.h
@@ -950,7 +950,7 @@ inline void CopyDimsToDesc(const RuntimeShape& input_shape,
                            NdArrayDesc<N>* desc_out) {
   int desc_stride = 1;
   for (int i = N - 1; i >= 0; --i) {
-    desc_out->extents[i] = input_shape.Dims(i);
+    // desc_out->extents[i] = input_shape.Dims(i);
     desc_out->strides[i] = desc_stride;
     desc_stride *= input_shape.Dims(i);
   }
```

</details> @Seunghui98 Could you please Use a different model quantization method and avoid using models with more than 8 dimensions. Please try with the latest TF version as you are using an older TF version(2.8.0) ? 
Thank you! @sushreebarsa 

This problem(array out-of-bound) seems to be caused by the gcc version of c++.  -> Compiler Bug
In Ubuntu 18.04, the basic version of gcc is 7.4, and 
it seems that there is a bug related to `built_in_memcpy` in tensorflow only in this version. 
This is because the upper versions, Ubuntu 20.04 and 22.04, have a basic version of gcc of 9.4 and no related errors occur here. :) @pkgoogle,
Please look into the issue.

Thank You Hi @Seunghui98, is there any reason you can't use a newer version of TF & gcc?

I should also note that clang will be better supported, now and in the future so it is recommended, especially with versions >= 2.13, to use clang instead of gcc. @pkgoogle 

> is there any reason you can't use a newer version of TF & gcc?

Yes. Our project using tflite kernel has dependency on specific `gcc` and `TF` version now.

> so it is recommended, especially with versions >= 2.13, to use clang instead of gcc.

Okay.. Thank you for your reply. :) Hi @Seunghui98, as you stated yourself gcc == 9.4 already resolves the issue, so I think the solution would be to figure out how to update your project/stack as in some sense this is already fixed right? Let us know if for some reason this does not resolve the issue.

Please review the compatibility matrix: https://www.tensorflow.org/install/source#cpu This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62440">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62440">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

source

### TensorFlow version

tf 2.8.0

### Custom code

Yes

### OS platform and distribution

Linux Ubuntu 18.04

### Mobile device

No

### Python version

3.8

### Bazel version

no

### GCC/compiler version

gcc 8.4.0

### CUDA/cuDNN version

no

### GPU model and memory

no

### Current behavior?

- I am using the tensorflow-lite interpreter in the project using [tensorflow/lite/kernels/internal/reference/broadcast_to.h](https://github.com/tensorflow/tensorflow/blob/28b008fdf686a760dd4b4989978bb98d594831ae/tensorflow/lite/kernels/internal/reference/broadcast_to.h#L62). 
- However, by setting kMaxDims to 8 in file [tensorflow/lite/kernels/broadcast_to.cc](https://github.com/tensorflow/tensorflow/blob/28b008fdf686a760dd4b4989978bb98d594831ae/tensorflow/lite/kernels/broadcast_to.cc#L34), I also want to set kMaxDims to 8 in my project and use `tensorflow/lite/kernels/internal/reference/broadcast_to.h`. 
- In Ubuntu 20.04 and 22.04, there is no problem using kMaxDims as 8 and calling, but an `out-of-bounds error` occurred in a specific version of `Ubuntu18.04`. 
- It seems that the size check of the dimension in `tflite::RuntimeShape` exceeds the kMaxSmallSize in [tensorflow/lite/kernels/internal/runtime_shape.h](https://github.com/tensorflow/tensorflow/blob/1b8f5c396f0c016ebe81fe1af029e6f205c926a4/tensorflow/lite/kernels/internal/runtime_shape.h#L37) of 5, but I do not know why this problem occurs only in Ubuntu 18.04. 
- Could you tell me about this bug?

### Standalone code to reproduce the issue

- my codes in samsung ONE project - [link](https://github.com/Samsung/ONE/pull/11701/commits/06164d1a7503e667b5b8206f73fec8a35bbb1b21#diff-df03d71bae09c805951c819c718f8d8eb62fc772a8d3038683c6db1bcba560bb)
```shell

/*
 * Copyright (c) 2023 Samsung Electronics Co., Ltd. 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.
 */

#ifndef LUCI_INTERPRETER_PAL_BROADCASTTO_H
#define LUCI_INTERPRETER_PAL_BROADCASTTO_H

#include <tensorflow/lite/kernels/internal/reference/broadcast_to.h>

namespace luci_interpreter_pal
{
static inline void BroadcastTo(const tflite::RuntimeShape &input_shape, const char *input_data,
                               const tflite::RuntimeShape &output_shape, char *output_data,
                               TfLiteType data_type)
{
  constexpr int kMaxDims = 8;
  tflite::reference_ops::BroadcastTo<kMaxDims>(input_shape, input_data, output_shape, output_data,
                                               data_type);
}
} // namespace luci_interpreter_pal

#endif // LUCI_INTERPRETER_PAL_BROADCASTTO_H
```
```


### Relevant log output

```shell
- error logs in Ubuntu 18.04


[2023-11-20T06:36:26.326Z] In member function ‘void luci_interpreter::kernels::BroadcastTo::evalFloat() const’:

[2023-11-20T06:36:26.326Z] cc1plus: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ forming offset [33, 40] is out of the bounds [0, 32] of object ‘<anonymous>’ with type ‘tflite::RuntimeShape’ [-Werror=array-bounds]

[2023-11-20T06:36:26.326Z] In file included from /opt/jenkins_agent/workspace/nnfw/master/pr-nncc-release/compiler/luci-interpreter/pal/linux/PALBroadcastTo.h:20,

[2023-11-20T06:36:26.326Z]                  from /opt/jenkins_agent/workspace/nnfw/master/pr-nncc-release/compiler/luci-interpreter/src/kernels/BroadcastTo.cpp:20:

[2023-11-20T06:36:26.326Z] /opt/jenkins_agent/workspace/nnfw/master/pr-nncc-release/externals/TENSORFLOW-2.8.0/tensorflow/lite/kernels/internal/reference/broadcast_to.h:68:45: note: ‘<anonymous>’ declared here

[2023-11-20T06:36:26.326Z]    CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_input_shape),

[2023-11-20T06:36:26.326Z]                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~

[2023-11-20T06:36:26.326Z] cc1plus: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ forming offset [33, 40] is out of the bounds [0, 32] of object ‘<anonymous>’ with type ‘tflite::RuntimeShape’ [-Werror=array-bounds]

[2023-11-20T06:36:26.326Z] /opt/jenkins_agent/workspace/nnfw/master/pr-nncc-release/externals/TENSORFLOW-2.8.0/tensorflow/lite/kernels/internal/reference/broadcast_to.h:70:45: note: ‘<anonymous>’ declared here

[2023-11-20T06:36:26.326Z]    CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape),

[2023-11-20T06:36:26.326Z]                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

[2023-11-20T06:36:26.326Z] [ 15%] Building CXX object compiler/tflchef/tflite/CMakeFiles/tflchef_tflite.dir/src/Op/ReduceMax.cpp.o

[2023-11-20T06:36:26.326Z] cc1plus: all warnings being treated as errors

[2023-11-20T06:36:26.326Z] compiler/luci-interpreter/src/kernels/CMakeFiles/luci_interpreter_kernels.dir/build.make:278: recipe for target 'compiler/luci-interpreter/src/kernels/CMakeFiles/luci_interpreter_kernels.dir/BroadcastTo.cpp.o' failed

[2023-11-20T06:36:26.326Z] make[2]: *** [compiler/luci-interpreter/src/kernels/CMakeFiles/luci_interpreter_kernels.dir/BroadcastTo.cpp.o] Error 1

[2023-11-20T06:36:26.326Z] CMakeFiles/Makefile2:8048: recipe for target 'compiler/luci-interpreter/src/kernels/CMakeFiles/luci_interpreter_kernels.dir/all' failed

[2023-11-20T06:36:26.326Z] make[1]: *** [compiler/luci-interpreter/src/kernels/CMakeFiles/luci_interpreter_kernels.dir/all] Error 2

[2023-11-20T06:36:26.326Z] make[1]: *** Waiting for unfinished jobs....
```

