bad_alloc
Hi @SiriusHsh, I am unable to replicate your issue in master or 2.14, or with gcc or clang (to build benchmark_model), I keep getting this error with your uploaded model, which seems reasonable given the invalid nature of your model:

```sh
./benchmark_model --graph=malloc_large_1.tflite
2023-09-19 22:11:56.591754: I tensorflow/tools/benchmark/benchmark_model.cc:475] Graph: [malloc_large_1.tflite]
2023-09-19 22:11:56.591838: I tensorflow/tools/benchmark/benchmark_model.cc:476] Init ops:
2023-09-19 22:11:56.591849: I tensorflow/tools/benchmark/benchmark_model.cc:477] Input layers: [input:0]
2023-09-19 22:11:56.591858: I tensorflow/tools/benchmark/benchmark_model.cc:478] Input shapes: [1,224,224,3]
2023-09-19 22:11:56.591865: I tensorflow/tools/benchmark/benchmark_model.cc:479] Input types: [float]
2023-09-19 22:11:56.591874: I tensorflow/tools/benchmark/benchmark_model.cc:480] Output layers: [output:0]
2023-09-19 22:11:56.591892: I tensorflow/tools/benchmark/benchmark_model.cc:481] Target layers: []
2023-09-19 22:11:56.591905: I tensorflow/tools/benchmark/benchmark_model.cc:482] Num runs: [1000]
2023-09-19 22:11:56.591928: I tensorflow/tools/benchmark/benchmark_model.cc:483] Inter-inference delay (seconds): [-1.0]
2023-09-19 22:11:56.591948: I tensorflow/tools/benchmark/benchmark_model.cc:484] Inter-benchmark delay (seconds): [-1.0]
2023-09-19 22:11:56.591964: I tensorflow/tools/benchmark/benchmark_model.cc:486] Num threads: [-1]
2023-09-19 22:11:56.591973: I tensorflow/tools/benchmark/benchmark_model.cc:487] Benchmark name: []
2023-09-19 22:11:56.591980: I tensorflow/tools/benchmark/benchmark_model.cc:488] Output prefix: []
2023-09-19 22:11:56.591987: I tensorflow/tools/benchmark/benchmark_model.cc:489] Show sizes: [0]
2023-09-19 22:11:56.591994: I tensorflow/tools/benchmark/benchmark_model.cc:490] Warmup runs: [1]
2023-09-19 22:11:56.592010: I tensorflow/tools/benchmark/benchmark_model.cc:256] Loading TensorFlow.
2023-09-19 22:11:56.592036: I tensorflow/tools/benchmark/benchmark_model.cc:264] Got config, 0 devices
2023-09-19 22:11:56.592131: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
[libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:1: Invalid control characters encountered in text.
[libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:9: Invalid control characters encountered in text.
[libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:33: Interpreting non ascii codepoint 148.
[libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:33: Message type "tensorflow.GraphDef" has no field named "TFL3".
2023-09-19 22:11:56.602256: E tensorflow/tools/benchmark/benchmark_model.cc:275] Could not create TensorFlow Graph: DATA_LOSS: Can't parse malloc_large_1.tflite as text proto
2023-09-19 22:11:56.602315: I tensorflow/tools/benchmark/benchmark_model.cc:502] Initialized session in 0.010285s

```

Can you ensure your bazel is updated, additionally can you make sure you rebuild benchmark_model with the latest code?

Thanks. 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/61908">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/61908">No</a>
 > Hi @SiriusHsh, I am unable to replicate your issue in master or 2.14, or with gcc or clang (to build benchmark_model), I keep getting this error with your uploaded model, which seems reasonable given the invalid nature of your model:
> 
> ```shell
> ./benchmark_model --graph=malloc_large_1.tflite
> 2023-09-19 22:11:56.591754: I tensorflow/tools/benchmark/benchmark_model.cc:475] Graph: [malloc_large_1.tflite]
> 2023-09-19 22:11:56.591838: I tensorflow/tools/benchmark/benchmark_model.cc:476] Init ops:
> 2023-09-19 22:11:56.591849: I tensorflow/tools/benchmark/benchmark_model.cc:477] Input layers: [input:0]
> 2023-09-19 22:11:56.591858: I tensorflow/tools/benchmark/benchmark_model.cc:478] Input shapes: [1,224,224,3]
> 2023-09-19 22:11:56.591865: I tensorflow/tools/benchmark/benchmark_model.cc:479] Input types: [float]
> 2023-09-19 22:11:56.591874: I tensorflow/tools/benchmark/benchmark_model.cc:480] Output layers: [output:0]
> 2023-09-19 22:11:56.591892: I tensorflow/tools/benchmark/benchmark_model.cc:481] Target layers: []
> 2023-09-19 22:11:56.591905: I tensorflow/tools/benchmark/benchmark_model.cc:482] Num runs: [1000]
> 2023-09-19 22:11:56.591928: I tensorflow/tools/benchmark/benchmark_model.cc:483] Inter-inference delay (seconds): [-1.0]
> 2023-09-19 22:11:56.591948: I tensorflow/tools/benchmark/benchmark_model.cc:484] Inter-benchmark delay (seconds): [-1.0]
> 2023-09-19 22:11:56.591964: I tensorflow/tools/benchmark/benchmark_model.cc:486] Num threads: [-1]
> 2023-09-19 22:11:56.591973: I tensorflow/tools/benchmark/benchmark_model.cc:487] Benchmark name: []
> 2023-09-19 22:11:56.591980: I tensorflow/tools/benchmark/benchmark_model.cc:488] Output prefix: []
> 2023-09-19 22:11:56.591987: I tensorflow/tools/benchmark/benchmark_model.cc:489] Show sizes: [0]
> 2023-09-19 22:11:56.591994: I tensorflow/tools/benchmark/benchmark_model.cc:490] Warmup runs: [1]
> 2023-09-19 22:11:56.592010: I tensorflow/tools/benchmark/benchmark_model.cc:256] Loading TensorFlow.
> 2023-09-19 22:11:56.592036: I tensorflow/tools/benchmark/benchmark_model.cc:264] Got config, 0 devices
> 2023-09-19 22:11:56.592131: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
> To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
> [libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:1: Invalid control characters encountered in text.
> [libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:9: Invalid control characters encountered in text.
> [libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:33: Interpreting non ascii codepoint 148.
> [libprotobuf ERROR external/com_google_protobuf/src/google/protobuf/text_format.cc:337] Error parsing text-format tensorflow.GraphDef: 1:33: Message type "tensorflow.GraphDef" has no field named "TFL3".
> 2023-09-19 22:11:56.602256: E tensorflow/tools/benchmark/benchmark_model.cc:275] Could not create TensorFlow Graph: DATA_LOSS: Can't parse malloc_large_1.tflite as text proto
> 2023-09-19 22:11:56.602315: I tensorflow/tools/benchmark/benchmark_model.cc:502] Initialized session in 0.010285s
> ```
> 
> Can you ensure your bazel is updated, additionally can you make sure you rebuild benchmark_model with the latest code?
> 
> Thanks.

I'm sorry for the delayed response. I don't think it's a Bazel version issue. It's possible that the way I'm building differs from yours.

I'm using the build method recommended in [this official guide](https://www.tensorflow.org/lite/guide/build_cmake#step_1_install_cmake_tool), as follows:
1. git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
2. mkdir tflite_build
cd tflite_build
3. cmake ../tensorflow_src/tensorflow/lite
4. cmake --build . -j
5. cmake --build . -j -t benchmark_model

The benchmark is in the tools directory
I still encounter the same issue in the latest code, commit id: abbbcb50208cf6d5a965b7bf60efec5bb078e142. I was able to replicate with those steps:

```bash
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
mkdir tflite_build
cd tflite_build
cmake ../tensorflow_src/tensorflow/lite
cmake --build . -j
cmake --build . -j -t benchmark_model
cd tools/benchmark
cp path/to/malloc_large_1.tflite .
./benchmark_model --graph=malloc_large_1.tflite
```

```
INFO: STARTING!
INFO: Log parameter values verbosely: [0]
INFO: Graph: [malloc_large_1.tflite]
INFO: Loaded model malloc_large_1.tflite
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
```

Hi @alankelly, can you please take a look? Thanks. Not having a security background, I will ask the naive question. You try to allocate more memory than the system has. The allocation fails and the program terminates. What's the issue? Why should I be concerned? What should the behaviour be?  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/61908">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/61908">No</a>

### Issue type

Bug

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

Yes

### Source

source

### TensorFlow version

tf 2.14.0

### Custom code

Yes

### OS platform and distribution

Ubuntu 18.04.6

### Mobile device

_No response_

### Python version

Python 3.8.3

### Bazel version

bazel 5.3.0

### GCC/compiler version

gcc 7.5.0

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

When allocating memory, since the value of `required_size` is determined by the model parameters, a maliciously constructed model may cause memory allocation to fail, leading to a DOS.
```
// simple_memory_arena.cc
TfLiteStatus SimpleMemoryArena::Commit(TfLiteContext* context,
                                       bool* arena_reallocated) {
  size_t required_size = RequiredBufferSize();
  if (required_size > underlying_buffer_size_) {
    *arena_reallocated = true;
#ifdef TF_LITE_TENSORFLOW_PROFILER
    PauseHeapMonitoring(/*pause=*/true);
    OnTfLiteArenaAlloc(subgraph_index_, reinterpret_cast<std::uintptr_t>(this),
                       required_size);
#endif
    char* new_alloc = new char[required_size];  // here
    char* new_underlying_buffer_aligned_ptr = reinterpret_cast<char*>(
        AlignTo(arena_alignment_, reinterpret_cast<intptr_t>(new_alloc)));
```

Construct a malicious model with a shape size of `0x640000000 * 0x13 * 0x13 * 0x60`, ultimately resulting in size being an extremely large value.
```
//simple_memory_arena.cc
TfLiteStatus SimpleMemoryArena::Allocate(
    TfLiteContext* context, size_t alignment, size_t size, int32_t tensor,
    int32_t first_node, int32_t last_node,
    ArenaAllocWithUsageInterval* new_alloc) {
         ...
  // Update the required buffer size.
  high_water_mark_ = std::max(high_water_mark_, best_offset + size); // size can be externally controlled.
```

[malloc_large_1.zip](https://github.com/tensorflow/tensorflow/files/12656923/malloc_large_1.zip)


### Standalone code to reproduce the issue

```shell
When I use the benchmark tool for PoC validation, it causes the TensorFlow Lite inference process to be subjected to a DOS（coredump).


❯ ./benchmark_model --graph=../poc/malloc_large_1.tflite
INFO: STARTING!
INFO: Log parameter values verbosely: [0]
INFO: Graph: [../poc/malloc_large_1.tflite]
INFO: Loaded model ../poc/malloc_large_1.tflite
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
[1]    13318 abort (core dumped)  ./benchmark_model --graph=../poc/malloc_large_1.tflite
```


### Relevant log output

_No response_