Coverage Report

Created: 2026-03-31 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/image.cc
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include "lib/jxl/image.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>  // fill, swap
11
#include <cstddef>
12
#include <cstdint>
13
#include <limits>
14
15
#include "lib/jxl/base/status.h"
16
#include "lib/jxl/memory_manager_internal.h"
17
18
#if defined(MEMORY_SANITIZER)
19
#include "lib/jxl/base/common.h"
20
#include "lib/jxl/base/sanitizers.h"
21
#include "lib/jxl/simd_util.h"
22
#endif
23
24
namespace jxl {
25
namespace detail {
26
27
namespace {
28
29
// Initializes the minimum bytes required to suppress MSAN warnings from
30
// legitimate vector loads/stores on the right border, where some lanes are
31
// uninitialized and assumed to be unused.
32
29.1M
void InitializePadding(PlaneBase& plane, const size_t sizeof_t) {
33
#if defined(MEMORY_SANITIZER)
34
  size_t xsize = plane.xsize();
35
  size_t ysize = plane.ysize();
36
  if (xsize == 0 || ysize == 0) return;
37
38
  const size_t vec_size = MaxVectorSize();
39
  if (vec_size == 0) return;  // Scalar mode: no padding needed
40
41
  const size_t valid_size = xsize * sizeof_t;
42
  const size_t initialize_size = RoundUpTo(valid_size, vec_size);
43
  if (valid_size == initialize_size) return;
44
45
  for (size_t y = 0; y < ysize; ++y) {
46
    uint8_t* JXL_RESTRICT row = plane.bytes() + y * plane.bytes_per_row();
47
#if defined(__clang__) &&                                           \
48
    ((!defined(__apple_build_version__) && __clang_major__ <= 6) || \
49
     (defined(__apple_build_version__) &&                           \
50
      __apple_build_version__ <= 10001145))
51
    // There's a bug in MSAN in clang-6 when handling AVX2 operations. This
52
    // workaround allows tests to pass on MSAN, although it is slower and
53
    // prevents MSAN warnings from uninitialized images.
54
    std::fill(row, msan::kSanitizerSentinelByte, initialize_size);
55
#else
56
    memset(row + valid_size, msan::kSanitizerSentinelByte,
57
           initialize_size - valid_size);
58
#endif  // clang6
59
  }
60
#endif  // MEMORY_SANITIZER
61
29.1M
}
62
63
}  // namespace
64
65
PlaneBase::PlaneBase(const uint32_t xsize, const uint32_t ysize,
66
                     const size_t sizeof_t)
67
30.0M
    : xsize_(xsize),
68
30.0M
      ysize_(ysize),
69
30.0M
      orig_xsize_(xsize),
70
30.0M
      orig_ysize_(ysize),
71
30.0M
      bytes_per_row_(BytesPerRow(xsize_, sizeof_t)),
72
30.0M
      sizeof_t_(sizeof_t) {}
73
74
Status PlaneBase::Allocate(JxlMemoryManager* memory_manager,
75
30.0M
                           size_t pre_padding) {
76
30.0M
  JXL_ENSURE(bytes_.address<void>() == nullptr);
77
78
  // Dimensions can be zero, e.g. for lazily-allocated images. Only allocate
79
  // if nonzero, because "zero" bytes still have padding/bookkeeping overhead.
80
30.0M
  if (xsize_ == 0 || ysize_ == 0) {
81
975k
    return true;
82
975k
  }
83
84
29.1M
  size_t total_bytes;
85
29.1M
  if (!SafeMul<size_t>(ysize_, bytes_per_row_, total_bytes)) {
86
0
    return JXL_FAILURE("Image dimensions are too large");
87
0
  }
88
89
29.1M
  JXL_ASSIGN_OR_RETURN(bytes_,
90
29.1M
                       AlignedMemory::Create(memory_manager, total_bytes,
91
29.1M
                                             pre_padding * sizeof_t_));
92
93
29.1M
  InitializePadding(*this, sizeof_t_);
94
95
29.1M
  return true;
96
29.1M
}
97
98
32.1k
void PlaneBase::Swap(PlaneBase& other) {
99
32.1k
  std::swap(xsize_, other.xsize_);
100
32.1k
  std::swap(ysize_, other.ysize_);
101
32.1k
  std::swap(orig_xsize_, other.orig_xsize_);
102
32.1k
  std::swap(orig_ysize_, other.orig_ysize_);
103
32.1k
  std::swap(bytes_per_row_, other.bytes_per_row_);
104
32.1k
  std::swap(bytes_, other.bytes_);
105
32.1k
}
106
107
}  // namespace detail
108
}  // namespace jxl