1/*
2 * Copyright (C) 2017-2018 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#include "config.h"
27#include "ObjectInitializationScope.h"
28
29#include "JSCInlines.h"
30#include "JSObject.h"
31#include "Operations.h"
32
33namespace JSC {
34
35#ifndef NDEBUG
36ObjectInitializationScope::ObjectInitializationScope(VM& vm)
37 : m_vm(vm)
38 , m_disallowGC(false)
39 , m_disallowVMReentry(false)
40{
41}
42
43ObjectInitializationScope::~ObjectInitializationScope()
44{
45 if (!m_object)
46 return;
47 verifyPropertiesAreInitialized(m_object);
48}
49
50void ObjectInitializationScope::notifyAllocated(JSObject* object, bool wasCreatedUninitialized)
51{
52 if (wasCreatedUninitialized) {
53 m_disallowGC.enable();
54 m_disallowVMReentry.enable();
55 m_object = object;
56 } else
57 verifyPropertiesAreInitialized(object);
58}
59
60void ObjectInitializationScope::notifyInitialized(JSObject* object)
61{
62 if (m_object) {
63 m_disallowGC.disable();
64 m_disallowVMReentry.disable();
65 m_object = nullptr;
66 }
67 verifyPropertiesAreInitialized(object);
68}
69
70void ObjectInitializationScope::verifyPropertiesAreInitialized(JSObject* object)
71{
72 Butterfly* butterfly = object->butterfly();
73 Structure* structure = object->structure(m_vm);
74 IndexingType indexingType = structure->indexingType();
75 unsigned vectorLength = butterfly->vectorLength();
76 if (UNLIKELY(hasUndecided(indexingType)) || !hasIndexedProperties(indexingType)) {
77 // Nothing to verify.
78 } else if (LIKELY(!hasAnyArrayStorage(indexingType))) {
79 auto data = butterfly->contiguous().data();
80 for (unsigned i = 0; i < vectorLength; ++i) {
81 if (isScribbledValue(data[i].get())) {
82 dataLogLn("Found scribbled value at i = ", i);
83 ASSERT_NOT_REACHED();
84 }
85 }
86 } else {
87 ArrayStorage* storage = butterfly->arrayStorage();
88 for (unsigned i = 0; i < vectorLength; ++i) {
89 if (isScribbledValue(storage->m_vector[i].get())) {
90 dataLogLn("Found scribbled value at i = ", i);
91 ASSERT_NOT_REACHED();
92 }
93 }
94 }
95
96 auto isSafeEmptyValueForGCScanning = [] (JSValue value) {
97#if USE(JSVALUE64)
98 return !value;
99#else
100 return !value || !JSValue::encode(value);
101#endif
102 };
103
104 for (int64_t i = 0; i < static_cast<int64_t>(structure->outOfLineCapacity()); i++) {
105 // We rely on properties past the last offset be zero for concurrent GC.
106 if (i + firstOutOfLineOffset > structure->lastOffset())
107 ASSERT(isSafeEmptyValueForGCScanning(butterfly->propertyStorage()[-i - 1].get()));
108 else if (isScribbledValue(butterfly->propertyStorage()[-i - 1].get())) {
109 dataLogLn("Found scribbled property at i = ", -i - 1);
110 ASSERT_NOT_REACHED();
111 }
112 }
113}
114#endif
115
116} // namespace JSC
117