aom: Change av1_get_uniform_tile_size() to return bool

From cc715882bae69606c2834cf49239c7c93de31d92 Mon Sep 17 00:00:00 2001
From: Wan-Teh Chang <[EMAIL REDACTED]>
Date: Mon, 4 Mar 2024 13:52:25 -0800
Subject: [PATCH] Change av1_get_uniform_tile_size() to return bool

Not all the callers of av1_get_uniform_tile_size() have the jmp_buf set
up, so av1_get_uniform_tile_size() can't call aom_internal_error().
Change av1_get_uniform_tile_size() to return bool to indicate
success/failure.

Bug: oss-fuzz:67058
Bug: oss-fuzz:67161
Change-Id: I92f9ee78c28b2a4fbe71d486569959b30d1ff76a
---
 av1/av1_dx_iface.c        | 12 +++++++++---
 av1/common/tile_common.c  | 11 ++++-------
 av1/common/tile_common.h  |  9 ++++++---
 av1/decoder/decodeframe.c |  6 +++++-
 av1/decoder/obu.c         |  5 ++++-
 5 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index e05a7057e..6ab04f610 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -866,7 +866,9 @@ static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
   if (pbi->ext_tile_debug && tiles->single_tile_decoding &&
       pbi->dec_tile_row >= 0) {
     int tile_width, tile_height;
-    av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
+    if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
+      return NULL;
+    }
     const int tile_row = AOMMIN(pbi->dec_tile_row, tiles->rows - 1);
     const int mi_row = tile_row * tile_height;
     const int ssy = ctx->img.y_chroma_shift;
@@ -885,7 +887,9 @@ static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
   if (pbi->ext_tile_debug && tiles->single_tile_decoding &&
       pbi->dec_tile_col >= 0) {
     int tile_width, tile_height;
-    av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
+    if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
+      return NULL;
+    }
     const int tile_col = AOMMIN(pbi->dec_tile_col, tiles->cols - 1);
     const int mi_col = tile_col * tile_width;
     const int ssx = ctx->img.x_chroma_shift;
@@ -1429,7 +1433,9 @@ static aom_codec_err_t ctrl_get_tile_size(aom_codec_alg_priv_t *ctx,
           (FrameWorkerData *)worker->data1;
       const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
       int tile_width, tile_height;
-      av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
+      if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
+        return AOM_CODEC_CORRUPT_FRAME;
+      }
       *tile_size = ((tile_width * MI_SIZE) << 16) + tile_height * MI_SIZE;
       return AOM_CODEC_OK;
     } else {
diff --git a/av1/common/tile_common.c b/av1/common/tile_common.c
index 31ab9c8c5..b073759f3 100644
--- a/av1/common/tile_common.c
+++ b/av1/common/tile_common.c
@@ -177,7 +177,7 @@ int av1_get_sb_cols_in_tile(const AV1_COMMON *cm, const TileInfo *tile) {
                            cm->seq_params->mib_size_log2);
 }
 
-void av1_get_uniform_tile_size(const AV1_COMMON *cm, int *w, int *h) {
+bool av1_get_uniform_tile_size(const AV1_COMMON *cm, int *w, int *h) {
   const CommonTileParams *const tiles = &cm->tiles;
   if (tiles->uniform_spacing) {
     *w = tiles->width;
@@ -189,9 +189,7 @@ void av1_get_uniform_tile_size(const AV1_COMMON *cm, int *w, int *h) {
       const int tile_w = tile_width_sb * cm->seq_params->mib_size;
       // ensure all tiles have same dimension
       if (i != 0 && tile_w != *w) {
-        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
-                           "tile %d does not have the same width: %d != %d", i,
-                           tile_w, *w);
+        return false;
       }
       *w = tile_w;
     }
@@ -202,13 +200,12 @@ void av1_get_uniform_tile_size(const AV1_COMMON *cm, int *w, int *h) {
       const int tile_h = tile_height_sb * cm->seq_params->mib_size;
       // ensure all tiles have same dimension
       if (i != 0 && tile_h != *h) {
-        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
-                           "tile %d does not have the same height: %d != %d", i,
-                           tile_h, *h);
+        return false;
       }
       *h = tile_h;
     }
   }
+  return true;
 }
 
 int av1_is_min_tile_width_satisfied(const AV1_COMMON *cm) {
diff --git a/av1/common/tile_common.h b/av1/common/tile_common.h
index 851125fb5..203c35452 100644
--- a/av1/common/tile_common.h
+++ b/av1/common/tile_common.h
@@ -12,12 +12,14 @@
 #ifndef AOM_AV1_COMMON_TILE_COMMON_H_
 #define AOM_AV1_COMMON_TILE_COMMON_H_
 
+#include <stdbool.h>
+
+#include "config/aom_config.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#include "config/aom_config.h"
-
 struct AV1Common;
 struct SequenceHeader;
 struct CommonTileParams;
@@ -51,7 +53,8 @@ int av1_get_sb_cols_in_tile(const struct AV1Common *cm, const TileInfo *tile);
 #define MAX_TILE_AREA_LEVEL_7_AND_ABOVE (4096 * 4608)
 #endif
 
-void av1_get_uniform_tile_size(const struct AV1Common *cm, int *w, int *h);
+// Returns true on success, false on failure.
+bool av1_get_uniform_tile_size(const struct AV1Common *cm, int *w, int *h);
 void av1_get_tile_limits(struct AV1Common *const cm);
 void av1_calculate_tile_cols(const struct SequenceHeader *const seq_params,
                              int cm_mi_rows, int cm_mi_cols,
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 4e3fc45a5..e65423008 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -2294,7 +2294,11 @@ static const uint8_t *get_ls_tile_buffers(
     const int tile_col_size_bytes = pbi->tile_col_size_bytes;
     const int tile_size_bytes = pbi->tile_size_bytes;
     int tile_width, tile_height;
-    av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
+    if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
+      aom_internal_error(
+          &pbi->error, AOM_CODEC_CORRUPT_FRAME,
+          "Not all the tiles in the tile list have the same size.");
+    }
     const int tile_copy_mode =
         ((AOMMAX(tile_width, tile_height) << MI_SIZE_LOG2) <= 256) ? 1 : 0;
     // Read tile column sizes for all columns (we need the last tile buffer)
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 992ed7514..b6444028e 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -501,7 +501,10 @@ static uint32_t read_and_decode_one_tile_list(AV1Decoder *pbi,
   }
 
   int tile_width, tile_height;
-  av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
+  if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
+    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
+    return 0;
+  }
   const int tile_width_in_pixels = tile_width * MI_SIZE;
   const int tile_height_in_pixels = tile_height * MI_SIZE;