Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -verify %s -o %t.report
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -DEMULATE_LIBSTDCPP -verify %s -o %t.report

// We do NOT model libcxx03 implementation, but the analyzer should still
// not crash.
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -DEMULATE_LIBCXX03 -verify %s -o %t.report
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -analyzer-checker=core,debug.ExprInspection -DEMULATE_LIBCXX03 -DEMULATE_LIBSTDCPP -verify %s -o %t.report
// RUN: rm -rf %t.report

void clang_analyzer_eval(bool);

// Faking std::call_once implementation.
namespace std {

// Fake std::function implementation.
template <typename>
class function;
class function_base {
 public:
  long field;
};
template <typename R, typename... P>
class function<R(P...)> : function_base {
 public:
   R operator()(P...) const {

     // Read from a super-class necessary to reproduce a crash.
     bool a = field;
   }
};

#ifndef EMULATE_LIBSTDCPP
typedef struct once_flag_s {
  unsigned long __state_ = 0;
} once_flag;
#else
typedef struct once_flag_s {
  int _M_once = 0;
} once_flag;
#endif

#ifndef EMULATE_LIBCXX03
template <class Callable, class... Args>
void call_once(once_flag &o, Callable&& func, Args&&... args) {};
#else
template <class Callable, class... Args> // libcxx03 call_once
void call_once(once_flag &o, Callable func, Args&&... args) {};
#endif

} // namespace std

// Check with Lambdas.
void test_called_warning() {
  std::once_flag g_initialize;
  int z;

  std::call_once(g_initialize, [&] {
    int *x = nullptr;
#ifndef EMULATE_LIBCXX03
    int y = *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
#endif
    z = 200;
  });
}

void test_called_on_path_inside_no_warning() {
  std::once_flag g_initialize;

  int *x = nullptr;
  int y = 100;
  int z;

  std::call_once(g_initialize, [&] {
    z = 200;
    x = &z;
  });

#ifndef EMULATE_LIBCXX03
  *x = 100; // no-warning
  clang_analyzer_eval(z == 100); // expected-warning{{TRUE}}
#endif
}

void test_called_on_path_no_warning() {
  std::once_flag g_initialize;

  int *x = nullptr;
  int y = 100;

  std::call_once(g_initialize, [&] {
    x = &y;
  });

#ifndef EMULATE_LIBCXX03
  *x = 100; // no-warning
#else
  *x = 100; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
#endif
}

void test_called_on_path_warning() {
  std::once_flag g_initialize;

  int y = 100;
  int *x = &y;

  std::call_once(g_initialize, [&] {
    x = nullptr;
  });

#ifndef EMULATE_LIBCXX03
  *x = 100; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
#endif
}

void test_called_once_warning() {
  std::once_flag g_initialize;

  int *x = nullptr;
  int y = 100;

  std::call_once(g_initialize, [&] {
    x = nullptr;
  });

  std::call_once(g_initialize, [&] {
    x = &y;
  });

#ifndef EMULATE_LIBCXX03
  *x = 100; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
#endif
}

void test_called_once_no_warning() {
  std::once_flag g_initialize;

  int *x = nullptr;
  int y = 100;

  std::call_once(g_initialize, [&] {
    x = &y;
  });

  std::call_once(g_initialize, [&] {
    x = nullptr;
  });

#ifndef EMULATE_LIBCXX03
  *x = 100; // no-warning
#endif
}

static int global = 0;
void funcPointer() {
  global = 1;
}

void test_func_pointers() {
  static std::once_flag flag;
  std::call_once(flag, &funcPointer);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(global == 1); // expected-warning{{TRUE}}
#endif
}

template <class _Fp>
class function; // undefined
template <class _Rp, class... _ArgTypes>
struct function<_Rp(_ArgTypes...)> {
  _Rp operator()(_ArgTypes...) const {};
  template <class _Fp>
  function(_Fp) {};
};

// Note: currently we do not support calls to std::function,
// but the analyzer should not crash either.
void test_function_objects_warning() {
  int x = 0;
  int *y = &x;

  std::once_flag flag;

  function<void()> func = [&]() {
    y = nullptr;
  };

  std::call_once(flag, func);

  func();
  int z = *y;
}

void test_param_passing_lambda() {
  std::once_flag flag;
  int x = 120;
  int y = 0;

  std::call_once(flag, [&](int p) {
    y = p;
  },
                 x);

#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(y == 120); // expected-warning{{TRUE}}
#endif
}

void test_param_passing_lambda_false() {
  std::once_flag flag;
  int x = 120;

  std::call_once(flag, [&](int p) {
    x = 0;
  },
                 x);

#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(x == 120); // expected-warning{{FALSE}}
#endif
}

void test_param_passing_stored_lambda() {
  std::once_flag flag;
  int x = 120;
  int y = 0;

  auto lambda = [&](int p) {
    y = p;
  };

  std::call_once(flag, lambda, x);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(y == 120); // expected-warning{{TRUE}}
#endif
}

void test_multiparam_passing_lambda() {
  std::once_flag flag;
  int x = 120;

  std::call_once(flag, [&](int a, int b, int c) {
    x = a + b + c;
  },
                 1, 2, 3);

#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(x == 120); // expected-warning{{FALSE}}
  clang_analyzer_eval(x == 6); // expected-warning{{TRUE}}
#endif
}

static int global2 = 0;
void test_param_passing_lambda_global() {
  std::once_flag flag;
  global2 = 0;
  std::call_once(flag, [&](int a, int b, int c) {
    global2 = a + b + c;
  },
                 1, 2, 3);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(global2 == 6); // expected-warning{{TRUE}}
#endif
}

static int global3 = 0;
void funcptr(int a, int b, int c) {
  global3 = a + b + c;
}

void test_param_passing_funcptr() {
  std::once_flag flag;
  global3 = 0;

  std::call_once(flag, &funcptr, 1, 2, 3);

#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(global3 == 6); // expected-warning{{TRUE}}
#endif
}

void test_blocks() {
  global3 = 0;
  std::once_flag flag;
  std::call_once(flag, ^{
    global3 = 120;
  });
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(global3 == 120); // expected-warning{{TRUE}}
#endif
}

int call_once() {
  return 5;
}

void test_non_std_call_once() {
  int x = call_once();
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(x == 5); // expected-warning{{TRUE}}
#endif
}

namespace std {
template <typename d, typename e>
void call_once(d, e);
}
void g();
void test_no_segfault_on_different_impl() {
#ifndef EMULATE_LIBCXX03
  std::call_once(g, false); // no-warning
#endif
}

void test_lambda_refcapture() {
  static std::once_flag flag;
  int a = 6;
  std::call_once(flag, [&](int &a) { a = 42; }, a);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(a == 42); // expected-warning{{TRUE}}
#endif
}

void test_lambda_refcapture2() {
  static std::once_flag flag;
  int a = 6;
  std::call_once(flag, [=](int &a) { a = 42; }, a);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(a == 42); // expected-warning{{TRUE}}
#endif
}

void test_lambda_fail_refcapture() {
  static std::once_flag flag;
  int a = 6;
  std::call_once(flag, [=](int a) { a = 42; }, a);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(a == 42); // expected-warning{{FALSE}}
#endif
}

void mutator(int &param) {
  param = 42;
}
void test_reftypes_funcptr() {
  static std::once_flag flag;
  int a = 6;
  std::call_once(flag, &mutator, a);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(a == 42); // expected-warning{{TRUE}}
#endif
}

void fail_mutator(int param) {
  param = 42;
}
void test_mutator_noref() {
  static std::once_flag flag;
  int a = 6;
  std::call_once(flag, &fail_mutator, a);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(a == 42); // expected-warning{{FALSE}}
#endif
}

// Function is implicitly treated as a function pointer
// even when an ampersand is not explicitly set.
void callbackn(int &param) {
  param = 42;
}
void test_implicit_funcptr() {
  int x = 0;
  static std::once_flag flagn;

  std::call_once(flagn, callbackn, x);
#ifndef EMULATE_LIBCXX03
  clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
#endif
}

int param_passed(int *x) {
  return *x; // no-warning, as std::function is not working yet.
}

void callback_taking_func_ok(std::function<void(int*)> &innerCallback) {
  innerCallback(nullptr);
}

// The provided callback expects an std::function, but instead a pointer
// to a C++ function is provided.
void callback_with_implicit_cast_ok() {
  std::once_flag flag;
  call_once(flag, callback_taking_func_ok, &param_passed);
}

void callback_taking_func(std::function<void()> &innerCallback) {
  innerCallback();
}

// The provided callback expects an std::function, but instead a C function
// name is provided, and C++ implicitly auto-constructs a pointer from it.
void callback_with_implicit_cast() {
  std::once_flag flag;
  call_once(flag, callback_taking_func, callback_with_implicit_cast);
}

std::once_flag another_once_flag;
typedef void (*my_callback_t)(int *);
my_callback_t callback;
int global_int;

void rdar40270582() {
  call_once(another_once_flag, callback, &global_int);
}