forked from mirrors/gecko-dev
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:
parent
9d8092fba3
commit
51f133db96
2 changed files with 31 additions and 16 deletions
|
|
@ -63,6 +63,34 @@ MOZ_END_EXTERN_C
|
||||||
|
|
||||||
namespace mozilla {
|
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
|
* 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
|
* 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
|
* consolidated at the point of a Check(), rather than scattered about at
|
||||||
* various uses of the corrupted memory.
|
* various uses of the corrupted memory.
|
||||||
*/
|
*/
|
||||||
class CorruptionCanary {
|
class CorruptionCanary : public CorruptionCanaryForStatics {
|
||||||
public:
|
public:
|
||||||
constexpr CorruptionCanary()
|
constexpr CorruptionCanary() = default;
|
||||||
: mValue(kCanarySet)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~CorruptionCanary() {
|
~CorruptionCanary() {
|
||||||
Check();
|
Check();
|
||||||
mValue = mozPoisonValue();
|
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
|
} // mozilla
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* const mLogName;
|
const char* const mLogName;
|
||||||
CorruptionCanary mCanary;
|
const CorruptionCanaryForStatics mCanary;
|
||||||
Atomic<LogModule*, ReleaseAcquire> mLog;
|
Atomic<LogModule*, ReleaseAcquire> mLog;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue