Coverage Report

Created: 2026-06-16 07:20

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