2013-06-26 23:37:14 +08:00
|
|
|
//===-- sanitizer_suppressions.h --------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-06-26 23:37:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-02-21 01:41:59 +08:00
|
|
|
// Suppression parsing/matching code.
|
2013-06-26 23:37:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_SUPPRESSIONS_H
|
|
|
|
#define SANITIZER_SUPPRESSIONS_H
|
|
|
|
|
|
|
|
#include "sanitizer_common.h"
|
2015-09-03 19:20:46 +08:00
|
|
|
#include "sanitizer_atomic.h"
|
2013-06-26 23:37:14 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
struct Suppression {
|
2015-12-10 02:48:10 +08:00
|
|
|
Suppression() { internal_memset(this, 0, sizeof(*this)); }
|
2015-02-21 01:41:59 +08:00
|
|
|
const char *type;
|
2013-06-26 23:37:14 +08:00
|
|
|
char *templ;
|
2015-09-03 19:20:46 +08:00
|
|
|
atomic_uint32_t hit_count;
|
2013-06-28 22:38:31 +08:00
|
|
|
uptr weight;
|
2013-06-26 23:37:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class SuppressionContext {
|
|
|
|
public:
|
2015-02-21 01:41:59 +08:00
|
|
|
// Create new SuppressionContext capable of parsing given suppression types.
|
|
|
|
SuppressionContext(const char *supprression_types[],
|
|
|
|
int suppression_types_num);
|
|
|
|
|
|
|
|
void ParseFromFile(const char *filename);
|
2013-06-26 23:37:14 +08:00
|
|
|
void Parse(const char *str);
|
2015-02-21 01:41:59 +08:00
|
|
|
|
|
|
|
bool Match(const char *str, const char *type, Suppression **s);
|
2013-10-03 21:37:17 +08:00
|
|
|
uptr SuppressionCount() const;
|
2015-02-21 01:41:59 +08:00
|
|
|
bool HasSuppressionType(const char *type) const;
|
2013-10-03 21:37:17 +08:00
|
|
|
const Suppression *SuppressionAt(uptr i) const;
|
2013-06-26 23:37:14 +08:00
|
|
|
void GetMatched(InternalMmapVector<Suppression *> *matched);
|
|
|
|
|
|
|
|
private:
|
[UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour
Summary:
Quote from http://eel.is/c++draft/expr.add#4:
```
4 When an expression J that has integral type is added to or subtracted
from an expression P of pointer type, the result has the type of P.
(4.1) If P evaluates to a null pointer value and J evaluates to 0,
the result is a null pointer value.
(4.2) Otherwise, if P points to an array element i of an array object x with n
elements ([dcl.array]), the expressions P + J and J + P
(where J has the value j) point to the (possibly-hypothetical) array
element i+j of x if 0≤i+j≤n and the expression P - J points to the
(possibly-hypothetical) array element i−j of x if 0≤i−j≤n.
(4.3) Otherwise, the behavior is undefined.
```
Therefore, as per the standard, applying non-zero offset to `nullptr`
(or making non-`nullptr` a `nullptr`, by subtracting pointer's integral value
from the pointer itself) is undefined behavior. (*if* `nullptr` is not defined,
i.e. e.g. `-fno-delete-null-pointer-checks` was *not* specified.)
To make things more fun, in C (6.5.6p8), applying *any* offset to null pointer
is undefined, although Clang front-end pessimizes the code by not lowering
that info, so this UB is "harmless".
Since rL369789 (D66608 `[InstCombine] icmp eq/ne (gep inbounds P, Idx..), null -> icmp eq/ne P, null`)
LLVM middle-end uses those guarantees for transformations.
If the source contains such UB's, said code may now be miscompiled.
Such miscompilations were already observed:
* https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190826/687838.html
* https://github.com/google/filament/pull/1566
Surprisingly, UBSan does not catch those issues
... until now. This diff teaches UBSan about these UB's.
`getelementpointer inbounds` is a pretty frequent instruction,
so this does have a measurable impact on performance;
I've addressed most of the obvious missing folds (and thus decreased the performance impact by ~5%),
and then re-performed some performance measurements using my [[ https://github.com/darktable-org/rawspeed | RawSpeed ]] benchmark:
(all measurements done with LLVM ToT, the sanitizer never fired.)
* no sanitization vs. existing check: average `+21.62%` slowdown
* existing check vs. check after this patch: average `22.04%` slowdown
* no sanitization vs. this patch: average `48.42%` slowdown
Reviewers: vsk, filcab, rsmith, aaron.ballman, vitalybuka, rjmccall, #sanitizers
Reviewed By: rsmith
Subscribers: kristof.beyls, nickdesaulniers, nikic, ychen, dtzWill, xbolva00, dberris, arphaman, rupprecht, reames, regehr, llvm-commits, cfe-commits
Tags: #clang, #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D67122
llvm-svn: 374293
2019-10-10 17:25:02 +08:00
|
|
|
static const int kMaxSuppressionTypes = 64;
|
2015-02-21 01:41:59 +08:00
|
|
|
const char **const suppression_types_;
|
|
|
|
const int suppression_types_num_;
|
|
|
|
|
2013-06-26 23:37:14 +08:00
|
|
|
InternalMmapVector<Suppression> suppressions_;
|
2015-02-21 01:41:59 +08:00
|
|
|
bool has_suppression_type_[kMaxSuppressionTypes];
|
2013-06-26 23:37:14 +08:00
|
|
|
bool can_parse_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_SUPPRESSIONS_H
|