Bug 1467048 - Add a version of CorruptionCanary for statics. r=froydnj

This adds 'CorruptionCanaryForStatics', which as the name implies is suitable
for use in objects that are statically declared. It has a trivial destructor
which allows us to avoid the need for static constructors.

--HG--
extra : amend_source : 27f8eff9ead21fde9f5f5d17f16c322d2c995a27
This commit is contained in:
Eric Rahm 2018-06-06 14:50:17 -07:00
Родитель 9d8092fba3
Коммит 51f133db96
2 изменённых файлов: 31 добавлений и 16 удалений

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

@ -63,6 +63,34 @@ MOZ_END_EXTERN_C
namespace mozilla {
/**
* A version of CorruptionCanary that is suitable as a member of objects that
* are statically allocated.
*/
class CorruptionCanaryForStatics {
public:
constexpr CorruptionCanaryForStatics()
: mValue(kCanarySet)
{
}
// This is required to avoid static constructor bloat.
~CorruptionCanaryForStatics() = default;
void Check() const {
if (mValue != kCanarySet) {
MOZ_CRASH("Canary check failed, check lifetime");
}
}
protected:
uintptr_t mValue;
private:
static const uintptr_t kCanarySet = 0x0f0b0f0b;
};
/**
* This class is designed to cause crashes when various kinds of memory
* corruption are observed. For instance, let's say we have a class C where we
@ -79,27 +107,14 @@ namespace mozilla {
* consolidated at the point of a Check(), rather than scattered about at
* various uses of the corrupted memory.
*/
class CorruptionCanary {
class CorruptionCanary : public CorruptionCanaryForStatics {
public:
constexpr CorruptionCanary()
: mValue(kCanarySet)
{
}
constexpr CorruptionCanary() = default;
~CorruptionCanary() {
Check();
mValue = mozPoisonValue();
}
void Check() const {
if (mValue != kCanarySet) {
MOZ_CRASH("Canary check failed, check lifetime");
}
}
private:
static const uintptr_t kCanarySet = 0x0f0b0f0b;
uintptr_t mValue;
};
} // mozilla

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

@ -175,7 +175,7 @@ public:
private:
const char* const mLogName;
CorruptionCanary mCanary;
const CorruptionCanaryForStatics mCanary;
Atomic<LogModule*, ReleaseAcquire> mLog;
};