Files
zoneminder/tests/zm_image_delta.cpp
T
Isaac ConnorandClaude Opus 4.8 bb7204449b fix: handle a non-block-aligned row width in Image::Delta
The delta8_* helpers consume whole blocks per iteration (16 pixels for
the gray8 unrolled/SSE2/NEON variants, 4 pixels for the rgb32 ones). The
SSE2 and NEON variants require the pixel count to be an exact multiple of
that block: a shorter count wraps the loop counter past zero and runs off
the end of memory, and the unrolled C variants overrun by up to a block.

Since PR #4788 made Delta stride-aware it drives those helpers row by row
with the row width as the count. update_function_pointers() only falls
back to the scalar helpers when total pixels % 4 is non-zero, which does
not imply each row width is block-aligned. A monitor whose width is not a
multiple of 16 (e.g. 1366) therefore reaches sse2_delta8_gray8 with a
count of 1366 and zmc dies with SIGSEGV in that loop.

Run the block-aligned bulk of each row through the fast/SIMD helper and
finish the remaining tail pixels with the matching one-pixel-at-a-time
scalar helper.

tests/zm_image_delta.cpp exercises gray8 (widths 17 and 1366) and rgb32
(width 6) at counts that select the SIMD path; each reproduces the crash
on the unfixed code and verifies the per-pixel result on the fix.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 21:53:49 -05:00

154 lines
5.8 KiB
C++

/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Image::Delta drives the delta8_* helpers row by row at the image's own
// linesize (so per-row padding is not read as image data). Those helpers
// consume whole blocks per iteration (16 gray8 pixels, 4 rgb32 pixels); the
// SSE2/NEON variants require the pixel count to be an exact multiple of the
// block or they wrap the loop counter past zero and run off the end of memory
// (SIGSEGV), and the unrolled C variants overrun by up to a block. A per-row
// width that is not block-aligned therefore crashes/overruns even though
// update_function_pointers() only guards on total pixels % 4. These cases pick
// widths that are NOT block-aligned while keeping pixels % 4 == 0 (so the fast
// path is selected) and assert Delta produces the correct per-pixel result.
#include "zm_catch2.h"
#include "zm_config.h"
#include "zm_image.h"
#include "zm_rgb.h"
#include "zm_utils.h"
#include <cstdint>
#include <cstdlib>
#include <vector>
namespace {
// Image::Initialise() dereferences config.font_file_location; also select the
// SIMD delta helpers so these cases exercise the block-aligned fast path (and
// reproduce the crash on the unfixed code) rather than the always-safe scalar
// fallback. Must run before the first Image is constructed, as Initialise()
// latches on first use.
void EnsureImageInit() {
static bool done = false;
if (done) return;
if (!config.font_file_location) config.font_file_location = "";
config.cpu_extensions = 1;
HwCapsDetect(); // sets the global sse_version / neonversion
done = true;
}
// Fill a freshly allocated (self-owned, aligned buffer + aligned linesize)
// image with a reproducible per-pixel pattern across `channels` bytes/pixel.
void FillPattern(Image &img, unsigned int channels, int seed) {
for (unsigned int y = 0; y < img.Height(); y++) {
for (unsigned int x = 0; x < img.Width(); x++) {
uint8_t *p = img.Buffer(x, y);
for (unsigned int c = 0; c < channels; c++)
p[c] = static_cast<uint8_t>((x * 7 + y * 13 + c * 29 + seed) & 0xff);
}
}
}
} // namespace
TEST_CASE("Image::Delta gray8 with a non-16-aligned width", "[Image][Delta]") {
EnsureImageInit();
// 17x12: width % 16 == 1 (not block-aligned) but pixels == 204, % 4 == 0, so
// update_function_pointers() selects the 16-pixel SIMD gray8 helper.
constexpr unsigned int kW = 17;
constexpr unsigned int kH = 12;
Image a(kW, kH, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_NONE);
Image b(kW, kH, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_NONE);
FillPattern(a, 1, 0);
FillPattern(b, 1, 97);
Image delta;
REQUIRE(a.Delta(b, &delta)); // unfixed code SIGSEGVs here
REQUIRE(delta.Width() == kW);
REQUIRE(delta.Height() == kH);
// gray8 delta is an exact abs difference (pmaxub/pminub/psubb == |a-b|).
for (unsigned int y = 0; y < kH; y++) {
for (unsigned int x = 0; x < kW; x++) {
const int expected = std::abs(static_cast<int>(*a.Buffer(x, y)) -
static_cast<int>(*b.Buffer(x, y)));
REQUIRE(static_cast<int>(*delta.Buffer(x, y)) == expected);
}
}
}
TEST_CASE("Image::Delta gray8 with a realistic non-16-aligned width", "[Image][Delta]") {
EnsureImageInit();
// 1366x4 is a real-world odd width: 1366 % 16 == 6, pixels == 5464, % 4 == 0.
constexpr unsigned int kW = 1366;
constexpr unsigned int kH = 4;
Image a(kW, kH, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_NONE);
Image b(kW, kH, ZM_COLOUR_GRAY8, ZM_SUBPIX_ORDER_NONE);
FillPattern(a, 1, 5);
FillPattern(b, 1, 211);
Image delta;
REQUIRE(a.Delta(b, &delta));
for (unsigned int y = 0; y < kH; y++) {
for (unsigned int x = 0; x < kW; x++) {
const int expected = std::abs(static_cast<int>(*a.Buffer(x, y)) -
static_cast<int>(*b.Buffer(x, y)));
REQUIRE(static_cast<int>(*delta.Buffer(x, y)) == expected);
}
}
}
TEST_CASE("Image::Delta rgb32 with a non-4-aligned width", "[Image][Delta]") {
EnsureImageInit();
// 6x4: width % 4 == 2 (not block-aligned), pixels == 24, % 4 == 0, so the
// 4-pixel SIMD rgba helper is selected.
constexpr unsigned int kW = 6;
constexpr unsigned int kH = 4;
Image a(kW, kH, ZM_COLOUR_RGB32, ZM_SUBPIX_ORDER_RGBA);
Image b(kW, kH, ZM_COLOUR_RGB32, ZM_SUBPIX_ORDER_RGBA);
FillPattern(a, 4, 0);
FillPattern(b, 4, 131);
Image delta;
REQUIRE(a.Delta(b, &delta)); // unfixed code SIGSEGVs / overruns here
REQUIRE(delta.Width() == kW);
REQUIRE(delta.Height() == kH);
// Reference: the format's own scalar helper on tightly packed rows. The SIMD
// bulk may round slightly differently from the scalar tail, so allow the same
// tolerance the delta self-test uses (see Image::Initialise).
for (unsigned int y = 0; y < kH; y++) {
std::vector<uint8_t> ra(kW * 4), rb(kW * 4), rref(kW);
for (unsigned int x = 0; x < kW; x++)
for (unsigned int c = 0; c < 4; c++) {
ra[x * 4 + c] = a.Buffer(x, y)[c];
rb[x * 4 + c] = b.Buffer(x, y)[c];
}
std_delta8_rgba(ra.data(), rb.data(), rref.data(), kW);
for (unsigned int x = 0; x < kW; x++)
REQUIRE(std::abs(static_cast<int>(*delta.Buffer(x, y)) -
static_cast<int>(rref[x])) <= 8);
}
}