Protobuf 4.24.0 break tensorflow and causes segfault with TF 2.12
Hi @naruto-raj ,

Thanks for reaching out. I tried building and training a CNN model with TF prebuilt binary of 2.12v with Protobuf 4.24.0 version but the code executes fine with no such segmentation problem as reported. Please refer to attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/2a5c866d4d93983427003199b4bfbb39/61551.ipynb).

Could you please check and confirm with exact code snippet with which you have tested. [Protobuf_issue.zip](https://github.com/tensorflow/tensorflow/files/12335011/Protobuf_issue.zip)
Notebook to reproduce the issue. I was using optimise_for_inference api and it seems to cause the issue. I can reproduce this issue too, using the following code (see below, it is a very simple CNN model built with tf.v1 that includes a saver and an optimizer).
If fails when `tf.compat.v1.saved_model.simple_save()` is called.

It runs fine with `protobuf==4.23.1` but fails with `protobuf==4.24.0` (Segmentation fault).
It looks like the segfault happens before writing the `.pb` file (other assets like variables seems to be written).

# Code

Run the code like this (with `/tmp` some place where you can write):

```bash
python3 bug.py /tmp/my_savedmodel
```

*bug.py*:

```python
import sys
import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

nclasses=6

def create_savedmodel(sess, inputs, outputs, directory):
    print("Create a SavedModel in " + directory)
    graph = tf.compat.v1.get_default_graph()
    inputs_names = {i: graph.get_tensor_by_name(i) for i in inputs}
    outputs_names = {o: graph.get_tensor_by_name(o) for o in outputs}
    tf.compat.v1.saved_model.simple_save(sess, directory, inputs=inputs_names, outputs=outputs_names)

def my_model(x):
  
  # input patches: 16x16x4
  conv1 = tf.layers.conv2d(inputs=x, filters=16, kernel_size=[5,5], padding="valid", activation=tf.nn.relu)
  pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
  conv2 = tf.layers.conv2d(inputs=pool1, filters=16, kernel_size=[3,3], padding="valid", activation=tf.nn.relu)
  pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
  conv3 = tf.layers.conv2d(inputs=pool2, filters=32, kernel_size=[2,2], padding="valid", activation=tf.nn.relu)
  
  # Features
  features = tf.reshape(conv3, shape=[-1, 32], name="features")
  
  # Neurons for classes
  estimated = tf.layers.dense(inputs=features, units=nclasses, activation=None)
  estimated_label = tf.argmax(estimated, 1, name="prediction")

  return estimated, estimated_label
 
""" Main """
if len(sys.argv) != 2:
  print("Usage : <output directory for SavedModel>")
  sys.exit(1)

# Create the TensorFlow graph
with tf.Graph().as_default():
  
  # Placeholders
  x = tf.placeholder(tf.float32, [None, None, None, 4], name="x")
  y = tf.placeholder(tf.int32  , [None, None, None, 1], name="y")
  lr = tf.placeholder_with_default(tf.constant(0.0002, dtype=tf.float32, shape=[]), shape=[], name="lr")
  
  # Output
  y_estimated, y_label = my_model(x)
  
  # Loss function
  cost = tf.losses.sparse_softmax_cross_entropy(labels=tf.reshape(y, [-1, 1]), logits=tf.reshape(y_estimated, [-1, nclasses]))
  
  # Optimizer
  optimizer = tf.train.AdamOptimizer(learning_rate=lr, name="optimizer").minimize(cost)
  
  # Initializer, saver, session
  init = tf.global_variables_initializer()
  saver = tf.train.Saver( max_to_keep=20 )
  sess = tf.Session()
  sess.run(init)

  # Create a SavedModel
  # The segfault happens here 
  create_savedmodel(sess, ["x:0", "y:0"], ["features:0", "prediction:0"], sys.argv[1])

```

# Output

```
Create a SavedModel in /tmp/toto_model
WARNING:tensorflow:From /tmp/tf_bug.py:13: simple_save (from tensorflow.python.saved_model.simple_save) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
WARNING:tensorflow:From /opt/otbtf/lib/python3/dist-packages/tensorflow/python/saved_model/signature_def_utils_impl.py:203: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
Segmentation fault (core dumped)
```

# Details

- Have you reproduced the bug with TensorFlow Nightly? No
- Source: built from source
- TensorFlow version: TF 2.12
- OS platform and distribution: ubuntu:22.04
- Python version: 3.10
- No cuda Hi @naruto-raj ,

I have replicated the reported behaviour.When tested on colab with protobuf=4.24v the session getting crashed and with 4.23v there is no crash.Attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/58ef76652cee800df726fb3c63c1c42d/61551_protobuf_issue.ipynb) for reference with snapshot below.

<img width="1509" alt="Screenshot 2023-08-16 at 6 03 08 PM" src="https://github.com/tensorflow/tensorflow/assets/116063290/5473f9f4-3aa5-474b-92e6-107dceebf4f3">




But `tf.compat.v1` is deprecated in current versions and hence I doubt whether it would considered for fix. Will escalate to Engg team to hear from them. Thanks!

### Issue type

Build/Install

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

No

### Source

source

### TensorFlow version

TF 2.12

### Custom code

No

### OS platform and distribution

Linux,Ubuntu

### Mobile device

_No response_

### Python version

3.8

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

With source build as well as pip install TF execution models fails with segfaults and the root cause seems to be because of protobuf version 4.24.0. It works fine till protobuf 4.23.4. adding a constraint in setup.py can be an immediate fix.

### Standalone code to reproduce the issue

```shell
Any CNN model execution causes the workflow to fail.
```


### Relevant log output

```shell
2023-08-14 08:01:56.274633: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
WARNING:tensorflow:From /home/anaconda3/envs/test/lib/python3.8/site-packages/tensorflow/python/tools/strip_unused_lib.py:84: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
scripts/benchmark_CNN.sh: line 243: 1488387 Segmentation fault
```
