Commit 029437edc6 for aom

commit 029437edc64b697200329c8a54c1aedbf71a97bb
Author: Jerome Jiang <jianj@google.com>
Date:   Tue Nov 17 17:55:15 2026 -0500

    Validate film grain params in av1_add_film_grain()

    - Validate aom_film_grain_t fields and image bit depth in
      av1_add_film_grain() against AV1 bitstream syntax bounds to
      prevent OOB accesses, divide-by-zero, and integer overflows when
      called directly.
    - Set raw and denoised aom_image_t bit_depth from --bit-depth in
      examples/noise_model.c instead of leaving the 16-bit container
      default from aom_img_alloc().
    - Add GrainSynthesisParamsTest and GrainSynthesisInvalidParamsTest.

    TAG=agy
    CONV=d503a301-125b-4da6-82f0-6ce00253f143

    Bug: 527353135
    Change-Id: I451cd2af045e9334f8af0c1bf4e202da6697fba9

diff --git a/av1/decoder/grain_synthesis.c b/av1/decoder/grain_synthesis.c
index a1ab1bf7ab..1d7198e0b7 100644
--- a/av1/decoder/grain_synthesis.c
+++ b/av1/decoder/grain_synthesis.c
@@ -1384,6 +1384,89 @@ static int add_film_grain_run(const aom_film_grain_t *params, uint8_t *luma,
   return 0;
 }

+// Returns true if the scaling function points are within the ranges allowed by
+// the film_grain_params() syntax: at most max_num_points points, 8-bit
+// coordinates, and strictly increasing x coordinates.
+static bool scaling_points_are_valid(const int points[][2], int num_points,
+                                     int max_num_points) {
+  if (num_points < 0 || num_points > max_num_points) return false;
+  for (int i = 0; i < num_points; i++) {
+    if (points[i][0] < 0 || points[i][0] > 255 || points[i][1] < 0 ||
+        points[i][1] > 255) {
+      return false;
+    }
+    if (i > 0 && points[i][0] <= points[i - 1][0]) return false;
+  }
+  return true;
+}
+
+static bool ar_coeffs_are_valid(const int *coeffs, int num_coeffs) {
+  for (int i = 0; i < num_coeffs; i++) {
+    if (coeffs[i] < -128 || coeffs[i] > 127) return false;
+  }
+  return true;
+}
+
+// Returns true if params could have been parsed from a conforming bitstream
+// for an image with the given format (see read_film_grain_params() in
+// av1/decoder/decodeframe.c and Section 6.8.20 of the AV1 specification). The
+// grain synthesis code relies on these ranges to avoid out-of-bounds accesses,
+// divisions by zero and signed integer overflows.
+static bool film_grain_params_are_valid(const aom_film_grain_t *params,
+                                        int monochrome, int chroma_subsamp_x,
+                                        int chroma_subsamp_y) {
+  if (params->bit_depth != 8 && params->bit_depth != 10 &&
+      params->bit_depth != 12) {
+    return false;
+  }
+  if (!scaling_points_are_valid(params->scaling_points_y, params->num_y_points,
+                                NELEMENTS(params->scaling_points_y)) ||
+      !scaling_points_are_valid(params->scaling_points_cb,
+                                params->num_cb_points,
+                                NELEMENTS(params->scaling_points_cb)) ||
+      !scaling_points_are_valid(params->scaling_points_cr,
+                                params->num_cr_points,
+                                NELEMENTS(params->scaling_points_cr))) {
+    return false;
+  }
+  if (params->scaling_shift < 8 || params->scaling_shift > 11) return false;
+  if (params->ar_coeff_lag < 0 || params->ar_coeff_lag > 3) return false;
+  const int num_pos_luma =
+      2 * params->ar_coeff_lag * (params->ar_coeff_lag + 1);
+  const int num_pos_chroma = num_pos_luma + (params->num_y_points > 0);
+  // generate_chroma_grain_blocks() uses the coefficients of both chroma planes
+  // even if grain is applied to only one of them.
+  if (!ar_coeffs_are_valid(params->ar_coeffs_y, num_pos_luma) ||
+      !ar_coeffs_are_valid(params->ar_coeffs_cb, num_pos_chroma) ||
+      !ar_coeffs_are_valid(params->ar_coeffs_cr, num_pos_chroma)) {
+    return false;
+  }
+  if (params->ar_coeff_shift < 6 || params->ar_coeff_shift > 9) return false;
+  if (params->grain_scale_shift < 0 || params->grain_scale_shift > 3) {
+    return false;
+  }
+  if (params->cb_mult < 0 || params->cb_mult > 255 ||
+      params->cb_luma_mult < 0 || params->cb_luma_mult > 255 ||
+      params->cb_offset < 0 || params->cb_offset > 511 || params->cr_mult < 0 ||
+      params->cr_mult > 255 || params->cr_luma_mult < 0 ||
+      params->cr_luma_mult > 255 || params->cr_offset < 0 ||
+      params->cr_offset > 511) {
+    return false;
+  }
+  // Chroma constraints of the film_grain_params() syntax.
+  const bool is_420 = chroma_subsamp_x == 1 && chroma_subsamp_y == 1;
+  if (monochrome && params->chroma_scaling_from_luma) return false;
+  if ((monochrome || params->chroma_scaling_from_luma ||
+       (is_420 && params->num_y_points == 0)) &&
+      (params->num_cb_points != 0 || params->num_cr_points != 0)) {
+    return false;
+  }
+  if (is_420 && (params->num_cb_points == 0) != (params->num_cr_points == 0)) {
+    return false;
+  }
+  return true;
+}
+
 int av1_add_film_grain(const aom_film_grain_t *params, const aom_image_t *src,
                        aom_image_t *dst) {
   uint8_t *luma, *cb, *cr;
@@ -1391,7 +1474,11 @@ int av1_add_film_grain(const aom_film_grain_t *params, const aom_image_t *src,
   int use_high_bit_depth = 0;
   int chroma_subsamp_x = 0;
   int chroma_subsamp_y = 0;
-  int mc_identity = src->mc == AOM_CICP_MC_IDENTITY ? 1 : 0;
+  int mc_identity;
+
+  if (!params || !src || !dst) return -1;
+
+  mc_identity = src->mc == AOM_CICP_MC_IDENTITY ? 1 : 0;

   switch (src->fmt) {
     case AOM_IMG_FMT_I420:
@@ -1430,7 +1517,14 @@ int av1_add_film_grain(const aom_film_grain_t *params, const aom_image_t *src,
       return -1;
   }

-  assert(params->bit_depth == src->bit_depth);
+  // The grain synthesis code assumes the parameters are within the ranges that
+  // can be signaled in a bitstream and that the bit depth matches the image.
+  if (params->bit_depth != src->bit_depth ||
+      (!use_high_bit_depth && params->bit_depth != 8) ||
+      !film_grain_params_are_valid(params, src->monochrome, chroma_subsamp_x,
+                                   chroma_subsamp_y)) {
+    return -1;
+  }

   dst->fmt = src->fmt;
   dst->bit_depth = src->bit_depth;
diff --git a/examples/noise_model.c b/examples/noise_model.c
index ac7f5f56cc..e0da53f84a 100644
--- a/examples/noise_model.c
+++ b/examples/noise_model.c
@@ -311,6 +311,9 @@ int main(int argc, char *argv[]) {
       (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
   }
+  if (args.bit_depth != 8 && args.bit_depth != 10 && args.bit_depth != 12) {
+    die("Invalid bit depth: %d\n", args.bit_depth);
+  }
   if (!aom_img_alloc(&raw, args.img_fmt, info.frame_width, info.frame_height,
                      1)) {
     die("Failed to allocate image.");
@@ -319,6 +322,8 @@ int main(int argc, char *argv[]) {
                      info.frame_height, 1)) {
     die("Failed to allocate image.");
   }
+  raw.bit_depth = args.bit_depth;
+  denoised.bit_depth = args.bit_depth;
   infile = fopen(args.input, "rb");
   if (!infile) {
     die("Failed to open input file: %s", args.input);
@@ -328,8 +333,10 @@ int main(int argc, char *argv[]) {
   const int high_bd = args.bit_depth > 8;
   const int block_size = args.block_size;
   aom_flat_block_finder_t block_finder;
-  aom_flat_block_finder_init(&block_finder, block_size, args.bit_depth,
-                             high_bd);
+  if (!aom_flat_block_finder_init(&block_finder, block_size, args.bit_depth,
+                                  high_bd)) {
+    die("Failed to initialize flat block finder\n");
+  }

   const int num_blocks_w = (info.frame_width + block_size - 1) / block_size;
   const int num_blocks_h = (info.frame_height + block_size - 1) / block_size;
@@ -340,7 +347,9 @@ int main(int argc, char *argv[]) {
   aom_noise_model_t noise_model;
   aom_noise_model_params_t params = { AOM_NOISE_SHAPE_SQUARE, 3, args.bit_depth,
                                       high_bd };
-  aom_noise_model_init(&noise_model, params);
+  if (!aom_noise_model_init(&noise_model, params)) {
+    die("Failed to initialize noise model\n");
+  }

   FILE *denoised_file = 0;
   if (args.input_denoised) {
diff --git a/test/grain_synthesis_test.cc b/test/grain_synthesis_test.cc
new file mode 100644
index 0000000000..71c4e89627
--- /dev/null
+++ b/test/grain_synthesis_test.cc
@@ -0,0 +1,271 @@
+/*
+ * Copyright (c) 2026, Alliance for Open Media. All rights reserved.
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#include <climits>
+#include <cstring>
+#include <ostream>
+#include <string>
+
+#include "gtest/gtest.h"
+
+#include "aom/aom_image.h"
+#include "aom_dsp/grain_params.h"
+#include "av1/decoder/grain_synthesis.h"
+
+namespace {
+
+// Film grain parameters that are valid for every image format used below.
+aom_film_grain_t ValidParams() {
+  aom_film_grain_t params = {};
+  params.apply_grain = 1;
+  params.update_parameters = 1;
+  params.num_y_points = 2;
+  params.scaling_points_y[0][0] = 0;
+  params.scaling_points_y[0][1] = 96;
+  params.scaling_points_y[1][0] = 255;
+  params.scaling_points_y[1][1] = 96;
+  params.num_cb_points = 2;
+  params.scaling_points_cb[0][0] = 0;
+  params.scaling_points_cb[0][1] = 64;
+  params.scaling_points_cb[1][0] = 255;
+  params.scaling_points_cb[1][1] = 64;
+  params.num_cr_points = 2;
+  params.scaling_points_cr[0][0] = 0;
+  params.scaling_points_cr[0][1] = 64;
+  params.scaling_points_cr[1][0] = 255;
+  params.scaling_points_cr[1][1] = 64;
+  params.scaling_shift = 11;
+  params.ar_coeff_lag = 3;
+  for (int i = 0; i < 24; ++i) params.ar_coeffs_y[i] = (i & 1) ? 4 : -4;
+  for (int i = 0; i < 25; ++i) {
+    params.ar_coeffs_cb[i] = (i & 1) ? -3 : 3;
+    params.ar_coeffs_cr[i] = (i & 1) ? 2 : -2;
+  }
+  params.ar_coeff_shift = 7;
+  params.cb_mult = 128;
+  params.cb_luma_mult = 192;
+  params.cb_offset = 256;
+  params.cr_mult = 128;
+  params.cr_luma_mult = 192;
+  params.cr_offset = 256;
+  params.overlap_flag = 1;
+  params.bit_depth = 8;
+  params.random_seed = 7391;
+  return params;
+}
+
+// Calls av1_add_film_grain() on a 64x64 image of the given format.
+int AddGrain(const aom_film_grain_t &params, aom_img_fmt_t fmt,
+             unsigned int bit_depth) {
+  constexpr int kSize = 64;
+  aom_image_t *src = aom_img_alloc(nullptr, fmt, kSize, kSize, 32);
+  aom_image_t *dst = aom_img_alloc(nullptr, fmt, kSize, kSize, 32);
+  EXPECT_NE(src, nullptr);
+  EXPECT_NE(dst, nullptr);
+  if (src == nullptr || dst == nullptr) {
+    aom_img_free(src);
+    aom_img_free(dst);
+    return INT_MIN;
+  }
+  const int bytes = (fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
+  // Keep the samples within the range of the smaller of the image bit depth
+  // and 12 bits.
+  const int sample_mask =
+      bytes == 2 ? (1 << (bit_depth < 12 ? bit_depth : 12)) - 1 : 0xff;
+  for (int plane = 0; plane < 3; ++plane) {
+    const int w = aom_img_plane_width(src, plane);
+    const int h = aom_img_plane_height(src, plane);
+    for (int y = 0; y < h; ++y) {
+      uint8_t *row = src->planes[plane] + y * src->stride[plane];
+      if (bytes == 2) {
+        uint16_t *row16 = reinterpret_cast<uint16_t *>(row);
+        for (int x = 0; x < w; ++x) row16[x] = (x * 37 + y * 11) & sample_mask;
+      } else {
+        for (int x = 0; x < w; ++x) row[x] = (x * 7 + y * 3) & sample_mask;
+      }
+    }
+  }
+  src->bit_depth = bit_depth;
+  src->mc = AOM_CICP_MC_BT_709;
+  const int ret = av1_add_film_grain(&params, src, dst);
+  aom_img_free(src);
+  aom_img_free(dst);
+  return ret;
+}
+
+TEST(GrainSynthesisParamsTest, AcceptsValidParams) {
+  const aom_film_grain_t params = ValidParams();
+  EXPECT_EQ(AddGrain(params, AOM_IMG_FMT_I420, 8), 0);
+  EXPECT_EQ(AddGrain(params, AOM_IMG_FMT_I422, 8), 0);
+  EXPECT_EQ(AddGrain(params, AOM_IMG_FMT_I444, 8), 0);
+  aom_film_grain_t params_hbd = params;
+  params_hbd.bit_depth = 10;
+  EXPECT_EQ(AddGrain(params_hbd, AOM_IMG_FMT_I42016, 10), 0);
+  params_hbd.bit_depth = 12;
+  EXPECT_EQ(AddGrain(params_hbd, AOM_IMG_FMT_I44416, 12), 0);
+  // 8-bit content stored in a high bit depth buffer.
+  EXPECT_EQ(AddGrain(params, AOM_IMG_FMT_I42016, 8), 0);
+}
+
+TEST(GrainSynthesisParamsTest, AcceptsBoundaryValues) {
+  aom_film_grain_t params = ValidParams();
+  params.num_y_points = 14;
+  for (int i = 0; i < 14; ++i) {
+    params.scaling_points_y[i][0] = (i == 13) ? 255 : i * 18;
+    params.scaling_points_y[i][1] = (i & 1) ? 255 : 0;
+  }
+  params.num_cb_points = 10;
+  params.num_cr_points = 10;
+  for (int i = 0; i < 10; ++i) {
+    params.scaling_points_cb[i][0] = params.scaling_points_cr[i][0] = i * 28;
+    params.scaling_points_cb[i][1] = params.scaling_points_cr[i][1] = 255 - i;
+  }
+  for (int i = 0; i < 24; ++i) params.ar_coeffs_y[i] = (i & 1) ? 127 : -128;
+  for (int i = 0; i < 25; ++i) {
+    params.ar_coeffs_cb[i] = (i & 1) ? -128 : 127;
+    params.ar_coeffs_cr[i] = (i & 1) ? 127 : -128;
+  }
+  params.scaling_shift = 8;
+  params.ar_coeff_shift = 6;
+  params.grain_scale_shift = 3;
+  params.cb_mult = params.cr_luma_mult = 255;
+  params.cb_luma_mult = params.cr_mult = 0;
+  params.cb_offset = 511;
+  params.cr_offset = 0;
+  params.clip_to_restricted_range = 1;
+  EXPECT_EQ(AddGrain(params, AOM_IMG_FMT_I420, 8), 0);
+  params.bit_depth = 12;
+  EXPECT_EQ(AddGrain(params, AOM_IMG_FMT_I42216, 12), 0);
+}
+
+struct InvalidParamsCase {
+  const char *name;
+  void (*mutate)(aom_film_grain_t *params);
+  aom_img_fmt_t fmt;
+  unsigned int image_bit_depth;
+};
+
+void PrintTo(const InvalidParamsCase &c, std::ostream *os) { *os << c.name; }
+
+// Each case corresponds to one of the crash inputs attached to b/527353135
+// (fuzz_film_grain.c), or to a parameter combination that cannot be signaled
+// in an AV1 bitstream.
+const InvalidParamsCase kInvalidParamsCases[] = {
+  // id:000000: negative num_y_points, OOB read in init_scaling_function().
+  { "NegativeNumYPoints", [](aom_film_grain_t *p) { p->num_y_points = -1; },
+    AOM_IMG_FMT_I420, 8 },
+  { "TooManyYPoints", [](aom_film_grain_t *p) { p->num_y_points = 15; },
+    AOM_IMG_FMT_I420, 8 },
+  { "NegativeNumCrPoints", [](aom_film_grain_t *p) { p->num_cr_points = -1; },
+    AOM_IMG_FMT_I444, 8 },
+  { "TooManyCbPoints", [](aom_film_grain_t *p) { p->num_cb_points = 11; },
+    AOM_IMG_FMT_I444, 8 },
+  // id:000001..000003: repeated x coordinate, division by zero.
+  { "RepeatedYPointX",
+    [](aom_film_grain_t *p) { p->scaling_points_y[1][0] = 0; },
+    AOM_IMG_FMT_I420, 8 },
+  { "RepeatedCbPointX",
+    [](aom_film_grain_t *p) { p->scaling_points_cb[1][0] = 0; },
+    AOM_IMG_FMT_I420, 8 },
+  { "RepeatedCrPointX",
+    [](aom_film_grain_t *p) { p->scaling_points_cr[1][0] = 0; },
+    AOM_IMG_FMT_I420, 8 },
+  { "DecreasingYPointX",
+    [](aom_film_grain_t *p) {
+      p->num_y_points = 3;
+      p->scaling_points_y[2][0] = 100;
+      p->scaling_points_y[2][1] = 10;
+    },
+    AOM_IMG_FMT_I420, 8 },
+  // id:000010, id:000037: x coordinate out of range, OOB write.
+  { "NegativeCbPointX",
+    [](aom_film_grain_t *p) { p->scaling_points_cb[1][0] = -16842752; },
+    AOM_IMG_FMT_I420, 8 },
+  { "LargeYPointX",
+    [](aom_film_grain_t *p) { p->scaling_points_y[1][0] = 65535; },
+    AOM_IMG_FMT_I420, 8 },
+  // id:000037: y coordinate out of range, signed overflow.
+  { "YPointYOutOfRange",
+    [](aom_film_grain_t *p) { p->scaling_points_y[0][1] = INT_MIN + 50; },
+    AOM_IMG_FMT_I420, 8 },
+  { "CrPointYOutOfRange",
+    [](aom_film_grain_t *p) { p->scaling_points_cr[1][1] = 256; },
+    AOM_IMG_FMT_I420, 8 },
+  // AR coefficients out of range, signed overflow (id:000033 for chroma).
+  { "ArCoeffYOutOfRange",
+    [](aom_film_grain_t *p) { p->ar_coeffs_y[5] = 1 << 24; }, AOM_IMG_FMT_I420,
+    8 },
+  { "ArCoeffCrOutOfRange",
+    [](aom_film_grain_t *p) { p->ar_coeffs_cr[12] = -50331648; },
+    AOM_IMG_FMT_I420, 8 },
+  { "ArCoeffCbOutOfRange",
+    [](aom_film_grain_t *p) { p->ar_coeffs_cb[24] = 128; }, AOM_IMG_FMT_I420,
+    8 },
+  // id:000036, id:000040: negative ar_coeff_lag, uninitialized pred_pos and
+  // signed overflow of the number of AR positions.
+  { "NegativeArCoeffLag", [](aom_film_grain_t *p) { p->ar_coeff_lag = -4; },
+    AOM_IMG_FMT_I420, 8 },
+  { "HugeNegativeArCoeffLag",
+    [](aom_film_grain_t *p) { p->ar_coeff_lag = -2147483420; },
+    AOM_IMG_FMT_I420, 8 },
+  { "ArCoeffLagTooLarge", [](aom_film_grain_t *p) { p->ar_coeff_lag = 4; },
+    AOM_IMG_FMT_I420, 8 },
+  // id:000020: negative grain_scale_shift, negative shift exponent.
+  { "NegativeGrainScaleShift",
+    [](aom_film_grain_t *p) { p->grain_scale_shift = -486539264; },
+    AOM_IMG_FMT_I420, 8 },
+  { "ScalingShiftTooSmall", [](aom_film_grain_t *p) { p->scaling_shift = 7; },
+    AOM_IMG_FMT_I420, 8 },
+  { "ArCoeffShiftTooLarge", [](aom_film_grain_t *p) { p->ar_coeff_shift = 10; },
+    AOM_IMG_FMT_I420, 8 },
+  { "CbMultOutOfRange", [](aom_film_grain_t *p) { p->cb_mult = 1 << 30; },
+    AOM_IMG_FMT_I444, 8 },
+  { "CrOffsetOutOfRange", [](aom_film_grain_t *p) { p->cr_offset = 512; },
+    AOM_IMG_FMT_I444, 8 },
+  // bit_depth > 8 with an 8-bit image: OOB read of the scaling LUT.
+  { "HighBitDepthParamsOn8BitImage",
+    [](aom_film_grain_t *p) {
+      p->bit_depth = 10;
+      p->cr_offset = 511;
+    },
+    AOM_IMG_FMT_I420, 10 },
+  // aom_img_alloc() sets bit_depth to 16 for high bit depth formats
+  // (examples/noise_model.c --bit-depth=10 --debug-file).
+  { "BitDepth16", [](aom_film_grain_t *p) { p->bit_depth = 16; },
+    AOM_IMG_FMT_I42016, 16 },
+  { "BitDepthMismatch", [](aom_film_grain_t *p) { p->bit_depth = 12; },
+    AOM_IMG_FMT_I42016, 10 },
+  // In 4:2:0, grain must be applied to both chroma planes or neither.
+  { "Only420CbPoints", [](aom_film_grain_t *p) { p->num_cr_points = 0; },
+    AOM_IMG_FMT_I420, 8 },
+  { "ChromaScalingFromLumaWithChromaPoints",
+    [](aom_film_grain_t *p) { p->chroma_scaling_from_luma = 1; },
+    AOM_IMG_FMT_I444, 8 },
+};
+
+class GrainSynthesisInvalidParamsTest
+    : public ::testing::TestWithParam<InvalidParamsCase> {};
+
+TEST_P(GrainSynthesisInvalidParamsTest, Rejects) {
+  const InvalidParamsCase &c = GetParam();
+  aom_film_grain_t params = ValidParams();
+  c.mutate(&params);
+  EXPECT_EQ(AddGrain(params, c.fmt, c.image_bit_depth), -1);
+}
+
+INSTANTIATE_TEST_SUITE_P(
+    All, GrainSynthesisInvalidParamsTest,
+    ::testing::ValuesIn(kInvalidParamsCases),
+    [](const ::testing::TestParamInfo<InvalidParamsCase> &param_info) {
+      return std::string(param_info.param.name);
+    });
+
+}  // namespace
diff --git a/test/test.cmake b/test/test.cmake
index 3ba70283b4..6d4639c620 100644
--- a/test/test.cmake
+++ b/test/test.cmake
@@ -193,6 +193,8 @@ if(NOT BUILD_SHARED_LIBS)
     add_to_libaom_test_srcs(AOM_UNIT_TEST_COMMON_INTRIN_AVX2)
   endif()

+  list(APPEND AOM_UNIT_TEST_DECODER_SOURCES
+              "${AOM_ROOT}/test/grain_synthesis_test.cc")
   if(CONFIG_MULTITHREAD)
     list(APPEND AOM_UNIT_TEST_DECODER_SOURCES
                 "${AOM_ROOT}/test/grain_synthesis_race_test.cc")