1 | /*
|
---|
2 | * Copyright (C) 2019-2022 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 "Integrity.h"
|
---|
29 | #include "JSCJSValue.h"
|
---|
30 | #include "StructureID.h"
|
---|
31 | #include "VM.h"
|
---|
32 | #include "VMInspector.h"
|
---|
33 | #include <wtf/Atomics.h>
|
---|
34 | #include <wtf/Gigacage.h>
|
---|
35 |
|
---|
36 | namespace JSC {
|
---|
37 | namespace Integrity {
|
---|
38 |
|
---|
39 | ALWAYS_INLINE bool Random::shouldAudit(VM& vm)
|
---|
40 | {
|
---|
41 | // If auditing is enabled, then the top bit of m_triggerBits is always set
|
---|
42 | // to 1 on reload. When this top bit reaches the bottom, it does not
|
---|
43 | // indicate that we should trigger an audit but rather that we've shifted
|
---|
44 | // out all the available trigger bits and hence, need to reload. Instead,
|
---|
45 | // reloadAndCheckShouldAuditSlow() will return whether we actually need to
|
---|
46 | // trigger an audit this turn.
|
---|
47 | //
|
---|
48 | // This function can be called concurrently from different threads and can
|
---|
49 | // be racy. For that reason, we intentionally do not write back to
|
---|
50 | // m_triggerBits if newTriggerBits is null. This ensures that if
|
---|
51 | // Options::randomIntegrityAuditRate() is non-zero, then m_triggerBits will
|
---|
52 | // always have at least 1 bit to trigger a reload.
|
---|
53 |
|
---|
54 | uint64_t newTriggerBits = m_triggerBits;
|
---|
55 | bool shouldAudit = newTriggerBits & 1;
|
---|
56 | newTriggerBits = newTriggerBits >> 1;
|
---|
57 | if (LIKELY(!shouldAudit)) {
|
---|
58 | m_triggerBits = newTriggerBits;
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (!newTriggerBits)
|
---|
63 | return reloadAndCheckShouldAuditSlow(vm);
|
---|
64 |
|
---|
65 | m_triggerBits = newTriggerBits;
|
---|
66 | return true;
|
---|
67 | }
|
---|
68 |
|
---|
69 | template<AuditLevel auditLevel>
|
---|
70 | ALWAYS_INLINE void auditCell(VM& vm, JSValue value)
|
---|
71 | {
|
---|
72 | if constexpr (auditLevel == AuditLevel::None)
|
---|
73 | return;
|
---|
74 |
|
---|
75 | if (value.isCell())
|
---|
76 | auditCell<auditLevel>(vm, value.asCell());
|
---|
77 | }
|
---|
78 |
|
---|
79 | ALWAYS_INLINE void auditCellMinimally(VM& vm, JSCell* cell)
|
---|
80 | {
|
---|
81 | if (UNLIKELY(Gigacage::contains(cell)))
|
---|
82 | auditCellMinimallySlow(vm, cell);
|
---|
83 | }
|
---|
84 |
|
---|
85 | ALWAYS_INLINE void auditCellRandomly(VM& vm, JSCell* cell)
|
---|
86 | {
|
---|
87 | if (UNLIKELY(vm.integrityRandom().shouldAudit(vm)))
|
---|
88 | auditCellFully(vm, cell);
|
---|
89 | }
|
---|
90 |
|
---|
91 | ALWAYS_INLINE void auditCellFully(VM& vm, JSCell* cell)
|
---|
92 | {
|
---|
93 | #if USE(JSVALUE64)
|
---|
94 | doAudit(vm, cell);
|
---|
95 | #else
|
---|
96 | auditCellMinimally(vm, cell);
|
---|
97 | #endif
|
---|
98 | }
|
---|
99 |
|
---|
100 | ALWAYS_INLINE void auditStructureID(StructureID structureID)
|
---|
101 | {
|
---|
102 | UNUSED_PARAM(structureID);
|
---|
103 | #if CPU(ADDRESS64) && !ENABLE(STRUCTURE_ID_WITH_SHIFT)
|
---|
104 | ASSERT(static_cast<uintptr_t>(structureID.bits()) <= structureHeapAddressSize + StructureID::nukedStructureIDBit);
|
---|
105 | #endif
|
---|
106 | #if ENABLE(EXTRA_INTEGRITY_CHECKS) || ASSERT_ENABLED
|
---|
107 | Structure* structure = structureID.tryDecode();
|
---|
108 | IA_ASSERT(structure, "structureID.bits 0x%x", structureID.bits());
|
---|
109 | // structure should be pointing to readable memory. Force a read.
|
---|
110 | WTF::opaque(*bitwise_cast<uintptr_t*>(structure));
|
---|
111 | #endif
|
---|
112 | }
|
---|
113 |
|
---|
114 | #if USE(JSVALUE64)
|
---|
115 |
|
---|
116 | JS_EXPORT_PRIVATE VM* doAuditSlow(VM*);
|
---|
117 |
|
---|
118 | ALWAYS_INLINE VM* doAudit(VM* vm)
|
---|
119 | {
|
---|
120 | if (UNLIKELY(!VMInspector::isValidVM(vm)))
|
---|
121 | return doAuditSlow(vm);
|
---|
122 | return vm;
|
---|
123 | }
|
---|
124 |
|
---|
125 | #endif // USE(JSVALUE64)
|
---|
126 |
|
---|
127 | } // namespace Integrity
|
---|
128 | } // namespace JSC
|
---|