1/*
2 * Copyright (C) 2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "JSCJSValue.h"
29#include <wtf/Gigacage.h>
30#include <wtf/Lock.h>
31
32namespace JSC {
33
34class JSCell;
35class VM;
36
37namespace Integrity {
38
39enum class AuditLevel {
40 None,
41 Minimal,
42 Full,
43 Random,
44};
45
46#ifdef NDEBUG
47static constexpr AuditLevel DefaultAuditLevel = AuditLevel::None;
48#else
49static constexpr AuditLevel DefaultAuditLevel = AuditLevel::Random;
50#endif
51
52class Random {
53public:
54 Random(VM&);
55
56 ALWAYS_INLINE bool shouldAudit(VM&);
57
58private:
59 JS_EXPORT_PRIVATE bool reloadAndCheckShouldAuditSlow(VM&);
60
61 uint64_t m_triggerBits;
62 Lock m_lock;
63
64 // The top bit is reserved as a termination bit. Hence, the number of
65 // trigger bits is always 1 less than will fit in m_triggerBits.
66 static constexpr int numberOfTriggerBits = (sizeof(m_triggerBits) * CHAR_BIT) - 1;
67};
68
69ALWAYS_INLINE void auditCellRandomly(VM&, JSCell*);
70ALWAYS_INLINE void auditCellMinimally(VM&, JSCell*);
71JS_EXPORT_PRIVATE void auditCellMinimallySlow(VM&, JSCell*);
72JS_EXPORT_PRIVATE void auditCellFully(VM&, JSCell*);
73
74template<AuditLevel = AuditLevel::Random, typename T>
75ALWAYS_INLINE void auditCell(VM&, T) { }
76
77template<AuditLevel auditLevel = DefaultAuditLevel>
78ALWAYS_INLINE void auditCell(VM& vm, JSCell* cell)
79{
80 switch (auditLevel) {
81 case AuditLevel::None:
82 return;
83 case AuditLevel::Minimal:
84 return auditCellMinimally(vm, cell);
85 case AuditLevel::Full:
86 return auditCellFully(vm, cell);
87 case AuditLevel::Random:
88 return auditCellRandomly(vm, cell);
89 }
90}
91
92template<AuditLevel auditLevel = DefaultAuditLevel>
93ALWAYS_INLINE void auditCell(VM& vm, JSValue value)
94{
95 if (auditLevel == AuditLevel::None)
96 return;
97
98 if (value.isCell())
99 auditCell<auditLevel>(vm, value.asCell());
100}
101
102} // namespace Integrity
103
104} // namespace JSC
105