summaryrefslogtreecommitdiff
path: root/lib/jxl/sanitizers.h
blob: 2123cfa4df82ca29ce2c4a3bcfc1fec53fc2a9ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#ifndef LIB_JXL_SANITIZERS_H_
#define LIB_JXL_SANITIZERS_H_

#include <stddef.h>

#include "lib/jxl/base/compiler_specific.h"
#include "lib/jxl/image.h"

#ifdef MEMORY_SANITIZER
#define JXL_MEMORY_SANITIZER 1
#elif defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define JXL_MEMORY_SANITIZER 1
#else
#define JXL_MEMORY_SANITIZER 0
#endif
#else
#define JXL_MEMORY_SANITIZER 0
#endif

#ifdef ADDRESS_SANITIZER
#define JXL_ADDRESS_SANITIZER 1
#elif defined(__has_feature)
#if __has_feature(address_sanitizer)
#define JXL_ADDRESS_SANITIZER 1
#else
#define JXL_ADDRESS_SANITIZER 0
#endif
#else
#define JXL_ADDRESS_SANITIZER 0
#endif

#ifdef THREAD_SANITIZER
#define JXL_THREAD_SANITIZER 1
#elif defined(__has_feature)
#if __has_feature(thread_sanitizer)
#define JXL_THREAD_SANITIZER 1
#else
#define JXL_THREAD_SANITIZER 0
#endif
#else
#define JXL_THREAD_SANITIZER 0
#endif

#if JXL_MEMORY_SANITIZER
#include <stdio.h>

#include <algorithm>
#include <string>
#include <vector>

#include "lib/jxl/base/status.h"
#include "sanitizer/msan_interface.h"
#endif

namespace jxl {
namespace msan {

#if JXL_MEMORY_SANITIZER

// Chosen so that kSanitizerSentinel is four copies of kSanitizerSentinelByte.
constexpr uint8_t kSanitizerSentinelByte = 0x48;
constexpr float kSanitizerSentinel = 205089.125f;

static JXL_INLINE JXL_MAYBE_UNUSED void PoisonMemory(const volatile void* m,
                                                     size_t size) {
  __msan_poison(m, size);
}

static JXL_INLINE JXL_MAYBE_UNUSED void UnpoisonMemory(const volatile void* m,
                                                       size_t size) {
  __msan_unpoison(m, size);
}

// Mark all the bytes of an image (including padding) as poisoned bytes.
static JXL_INLINE JXL_MAYBE_UNUSED void PoisonImage(const PlaneBase& im) {
  PoisonMemory(im.bytes(), im.bytes_per_row() * im.ysize());
}

template <typename T>
static JXL_INLINE JXL_MAYBE_UNUSED void PoisonImage(const Image3<T>& im) {
  PoisonImage(im.Plane(0));
  PoisonImage(im.Plane(1));
  PoisonImage(im.Plane(2));
}

// Print the uninitialized regions of an image.
template <typename T>
static JXL_INLINE JXL_MAYBE_UNUSED void PrintImageUninitialized(
    const Plane<T>& im) {
  fprintf(stderr, "Uninitialized regions for image of size %zux%zu:\n",
          im.xsize(), im.ysize());

  // A segment of uninitialized pixels in a row, in the format [first, second).
  typedef std::pair<size_t, size_t> PixelSegment;

  // Helper class to merge and print a list of rows of PixelSegment that may be
  // the same over big ranges of rows. This compacts the output to ranges of
  // rows like "[y0, y1): [x0, x1) [x2, x3)".
  class RowsMerger {
   public:
    // Add a new row the list of rows. If the row is the same as the previous
    // one it will be merged showing a range of rows [y0, y1), but if the new
    // row is different the current range of rows (if any) will be printed and a
    // new one will be started.
    void AddRow(size_t y, std::vector<PixelSegment>&& new_row) {
      if (start_y_ != -1 && new_row != segments_) {
        PrintRow(y);
      }
      if (new_row.empty()) {
        // Skip ranges with no uninitialized pixels.
        start_y_ = -1;
        segments_.clear();
        return;
      }
      if (start_y_ == -1) {
        start_y_ = y;
        segments_ = std::move(new_row);
      }
    }

    // Print the contents of the range of rows [start_y_, end_y) if any.
    void PrintRow(size_t end_y) {
      if (start_y_ == -1) return;
      if (segments_.empty()) {
        start_y_ = -1;
        return;
      }
      if (end_y - start_y_ > 1) {
        fprintf(stderr, " y=[%zd, %zu):", start_y_, end_y);
      } else {
        fprintf(stderr, " y=[%zd]:", start_y_);
      }
      for (const auto& seg : segments_) {
        if (seg.first + 1 == seg.second) {
          fprintf(stderr, " [%zd]", seg.first);
        } else {
          fprintf(stderr, " [%zd, %zu)", seg.first, seg.second);
        }
      }
      fprintf(stderr, "\n");
      start_y_ = -1;
    }

   private:
    std::vector<PixelSegment> segments_;
    // Row number of the first row in the range of rows that have |segments| as
    // the undefined segments.
    ssize_t start_y_ = -1;
  } rows_merger;

  class SegmentsMerger {
   public:
    void AddValue(size_t x) {
      if (row.empty() || row.back().second != x) {
        row.emplace_back(x, x + 1);
      } else {
        row.back().second = x + 1;
      }
    }

    std::vector<PixelSegment> row;
  };

  for (size_t y = 0; y < im.ysize(); y++) {
    auto* row = im.Row(y);
    SegmentsMerger seg_merger;
    size_t x = 0;
    while (x < im.xsize()) {
      intptr_t ret =
          __msan_test_shadow(row + x, (im.xsize() - x) * sizeof(row[0]));
      if (ret < 0) break;
      size_t next_x = x + ret / sizeof(row[0]);
      seg_merger.AddValue(next_x);
      x = next_x + 1;
    }
    rows_merger.AddRow(y, std::move(seg_merger.row));
  }
  rows_merger.PrintRow(im.ysize());
}

// Check that all the pixels in the provided rect of the image are initialized
// (not poisoned). If any of the values is poisoned it will abort.
template <typename T>
static JXL_INLINE JXL_MAYBE_UNUSED void CheckImageInitialized(
    const Plane<T>& im, const Rect& r, const char* message) {
  JXL_ASSERT(r.x0() <= im.xsize());
  JXL_ASSERT(r.x0() + r.xsize() <= im.xsize());
  JXL_ASSERT(r.y0() <= im.ysize());
  JXL_ASSERT(r.y0() + r.ysize() <= im.ysize());
  for (size_t y = r.y0(); y < r.y0() + r.ysize(); y++) {
    const auto* row = im.Row(y);
    intptr_t ret = __msan_test_shadow(row + r.x0(), sizeof(*row) * r.xsize());
    if (ret != -1) {
      JXL_DEBUG(1,
                "Checking an image of %zu x %zu, rect x0=%zu, y0=%zu, "
                "xsize=%zu, ysize=%zu",
                im.xsize(), im.ysize(), r.x0(), r.y0(), r.xsize(), r.ysize());
      size_t x = ret / sizeof(*row);
      JXL_DEBUG(1, "CheckImageInitialized failed at x=%zu, y=%zu: %s",
                r.x0() + x, y, message ? message : "");
      PrintImageUninitialized(im);
    }
    // This will report an error if memory is not initialized.
    __msan_check_mem_is_initialized(row + r.x0(), sizeof(*row) * r.xsize());
  }
}

template <typename T>
static JXL_INLINE JXL_MAYBE_UNUSED void CheckImageInitialized(
    const Image3<T>& im, const Rect& r, const char* message) {
  for (size_t c = 0; c < 3; c++) {
    std::string str_message(message);
    str_message += " c=" + std::to_string(c);
    CheckImageInitialized(im.Plane(c), r, str_message.c_str());
  }
}

#define JXL_CHECK_IMAGE_INITIALIZED(im, r) \
  ::jxl::msan::CheckImageInitialized(im, r, "im=" #im ", r=" #r);

#else  // JXL_MEMORY_SANITIZER

// In non-msan mode these functions don't use volatile since it is not needed
// for the empty functions.

static JXL_INLINE JXL_MAYBE_UNUSED void PoisonMemory(const void*, size_t) {}
static JXL_INLINE JXL_MAYBE_UNUSED void UnpoisonMemory(const void*, size_t) {}

static JXL_INLINE JXL_MAYBE_UNUSED void PoisonImage(const PlaneBase& im) {}
template <typename T>
static JXL_INLINE JXL_MAYBE_UNUSED void PoisonImage(const Plane<T>& im) {}

#define JXL_CHECK_IMAGE_INITIALIZED(im, r)

#endif

}  // namespace msan
}  // namespace jxl

#endif  // LIB_JXL_SANITIZERS_H_