Bug 1744604 - Part 2: Be less restrictive with the non-memmovable static analysis, r=andi

This change adds an exception for stl types which implement
`std::is_trivially_move_constructible` and `std::is_trivially_destructible`, as
these are generally stable parts of the standard library type definition which
may be relied on by downstream code.

Differential Revision: https://phabricator.services.mozilla.com/D132996
This commit is contained in:
Nika Layzell 2021-12-14 16:49:17 +00:00
Родитель ab7c298175
Коммит 2e21209147
2 изменённых файлов: 81 добавлений и 51 удалений

Просмотреть файл

@ -23,56 +23,37 @@ protected:
VisitFlags &ToVisit) const override {
// Annotate everything in ::std, with a few exceptions; see bug
// 1201314 for discussion.
if (getDeclarationNamespace(D) == "std") {
if (getDeclarationNamespace(D) != "std") {
return "";
}
StringRef Name = getNameChecked(D);
// If the type has a trivial move constructor and destructor, it is safe to
// memmove, and we don't need to visit any fields.
auto RD = dyn_cast<CXXRecordDecl>(D);
if (RD && RD->isCompleteDefinition() &&
(RD->hasTrivialMoveConstructor() ||
(!RD->hasMoveConstructor() && RD->hasTrivialCopyConstructor())) &&
RD->hasTrivialDestructor()) {
ToVisit = VISIT_NONE;
return "";
}
// This doesn't check that it's really ::std::pair and not
// ::std::something_else::pair, but should be good enough.
StringRef Name = getNameChecked(D);
if (isNameExcepted(Name.data())) {
// If we're an excepted name, stop traversing within the type further,
// and only check template arguments for foreign types.
ToVisit = VISIT_TMPL_ARGS;
return "";
}
return "it is an stl-provided type not guaranteed to be memmove-able";
}
return "";
}
private:
bool isNameExcepted(const char *Name) const {
static std::unordered_set<std::string> NamesSet = {
{"pair"},
{"atomic"},
// libstdc++ specific names
{"__atomic_base"},
{"atomic_bool"},
{"__cxx_atomic_impl"},
{"__cxx_atomic_base_impl"},
{"__pair_base"},
// MSVCRT specific names
{"_Atomic_impl"},
{"_Atomic_base"},
{"_Atomic_bool"},
{"_Atomic_char"},
{"_Atomic_schar"},
{"_Atomic_uchar"},
{"_Atomic_char16_t"},
{"_Atomic_char32_t"},
{"_Atomic_wchar_t"},
{"_Atomic_short"},
{"_Atomic_ushort"},
{"_Atomic_int"},
{"_Atomic_uint"},
{"_Atomic_long"},
{"_Atomic_ulong"},
{"_Atomic_llong"},
{"_Atomic_ullong"},
{"_Atomic_address"},
// MSVCRT 2019
{"_Atomic_integral"},
{"_Atomic_integral_facade"},
{"_Atomic_padded"},
{"_Atomic_pointer"},
{"_Atomic_storage"}};
return NamesSet.find(Name) != NamesSet.end();
bool isNameExcepted(StringRef Name) const {
return Name == "pair" || Name == "atomic";
}
};

Просмотреть файл

@ -1,21 +1,70 @@
#define MOZ_NEEDS_MEMMOVABLE_TYPE __attribute__((annotate("moz_needs_memmovable_type")))
template<class T>
class MOZ_NEEDS_MEMMOVABLE_TYPE Mover { T mForceInst; }; // expected-error-re 4 {{Cannot instantiate 'Mover<{{.*}}>' with non-memmovable template argument '{{.*}}'}}
class MOZ_NEEDS_MEMMOVABLE_TYPE Mover { T mForceInst; }; // expected-error-re 9 {{Cannot instantiate 'Mover<{{.*}}>' with non-memmovable template argument '{{.*}}'}}
namespace std {
// In theory defining things in std:: like this invokes undefined
// behavior, but in practice it's good enough for this test case.
template<class C> class basic_string { }; // expected-note 2 {{'std::basic_string<char>' is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}} expected-note {{'std::string' (aka 'basic_string<char>') is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}}
template<class C> class basic_string { // expected-note 2 {{'std::basic_string<char>' is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}} expected-note {{'std::string' (aka 'basic_string<char>') is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}}
public:
basic_string();
basic_string(const basic_string&);
basic_string(basic_string&&);
basic_string& operator=(const basic_string&);
basic_string& operator=(basic_string&&);
~basic_string();
};
typedef basic_string<char> string;
template<class T, class U> class pair { T mT; U mU; }; // expected-note-re {{std::pair<bool, std::basic_string<char>{{ ?}}>' is a non-memmove()able type because member 'mU' is a non-memmove()able type 'std::basic_string<char>'}}
class arbitrary_name { }; // expected-note {{'std::arbitrary_name' is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}}
template<class T, class U> class pair { T mT; U mU; }; // expected-note-re 4 {{'std::pair<bool, {{.*}}>' is a non-memmove()able type because it has a template argument non-memmove()able type '{{.*}}'}}
struct has_nontrivial_dtor { // expected-note 2 {{'std::has_nontrivial_dtor' is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}}
has_nontrivial_dtor() = default;
~has_nontrivial_dtor();
};
struct has_nontrivial_copy { // expected-note 2 {{'std::has_nontrivial_copy' is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}}
has_nontrivial_copy() = default;
has_nontrivial_copy(const has_nontrivial_copy&);
has_nontrivial_copy& operator=(const has_nontrivial_copy&);
};
struct has_nontrivial_move { // expected-note 2 {{'std::has_nontrivial_move' is a non-memmove()able type because it is an stl-provided type not guaranteed to be memmove-able}}
has_nontrivial_move() = default;
has_nontrivial_move(const has_nontrivial_move&);
has_nontrivial_move& operator=(const has_nontrivial_move&);
};
struct has_trivial_dtor {
has_trivial_dtor() = default;
~has_trivial_dtor() = default;
};
struct has_trivial_copy {
has_trivial_copy() = default;
has_trivial_copy(const has_trivial_copy&) = default;
has_trivial_copy& operator=(const has_trivial_copy&) = default;
};
struct has_trivial_move {
has_trivial_move() = default;
has_trivial_move(const has_trivial_move&) = default;
has_trivial_move& operator=(const has_trivial_move&) = default;
};
}
class HasString { std::string m; }; // expected-note {{'HasString' is a non-memmove()able type because member 'm' is a non-memmove()able type 'std::string' (aka 'basic_string<char>')}}
static Mover<std::string> bad; // expected-note-re {{instantiation of 'Mover<std::basic_string<char>{{ ?}}>' requested here}}
static Mover<HasString> bad_mem; // expected-note {{instantiation of 'Mover<HasString>' requested here}}
static Mover<std::arbitrary_name> assumed_bad; // expected-note {{instantiation of 'Mover<std::arbitrary_name>' requested here}}
static Mover<std::pair<bool, int>> good;
static Mover<std::pair<bool, std::string>> not_good; // expected-note-re {{instantiation of 'Mover<std::pair<bool, std::basic_string<char>{{ ?}}>{{ ?}}>' requested here}}
static Mover<std::has_nontrivial_dtor> nontrivial_dtor; // expected-note {{instantiation of 'Mover<std::has_nontrivial_dtor>' requested here}}
static Mover<std::has_nontrivial_copy> nontrivial_copy; // expected-note {{instantiation of 'Mover<std::has_nontrivial_copy>' requested here}}
static Mover<std::has_nontrivial_move> nontrivial_move; // expected-note {{instantiation of 'Mover<std::has_nontrivial_move>' requested here}}
static Mover<std::has_trivial_dtor> trivial_dtor;
static Mover<std::has_trivial_copy> trivial_copy;
static Mover<std::has_trivial_move> trivial_move;
static Mover<std::pair<bool, std::has_nontrivial_dtor>> pair_nontrivial_dtor; // expected-note {{instantiation of 'Mover<std::pair<bool, std::has_nontrivial_dtor>>' requested here}}
static Mover<std::pair<bool, std::has_nontrivial_copy>> pair_nontrivial_copy; // expected-note {{instantiation of 'Mover<std::pair<bool, std::has_nontrivial_copy>>' requested here}}
static Mover<std::pair<bool, std::has_nontrivial_move>> pair_nontrivial_move; // expected-note {{instantiation of 'Mover<std::pair<bool, std::has_nontrivial_move>>' requested here}}
static Mover<std::pair<bool, std::has_trivial_dtor>> pair_trivial_dtor;
static Mover<std::pair<bool, std::has_trivial_copy>> pair_trivial_copy;
static Mover<std::pair<bool, std::has_trivial_move>> pair_trivial_move;