Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: src/runtime/runtime-object.cc

Issue 1168093002: [strong] Implement strong mode restrictions on property access (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: slim down, improve tests Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/bootstrapper.h" 8 #include "src/bootstrapper.h"
9 #include "src/debug.h" 9 #include "src/debug.h"
10 #include "src/messages.h" 10 #include "src/messages.h"
11 #include "src/runtime/runtime.h" 11 #include "src/runtime/runtime.h"
12 #include "src/runtime/runtime-utils.h" 12 #include "src/runtime/runtime-utils.h"
13 13
14 namespace v8 { 14 namespace v8 {
15 namespace internal { 15 namespace internal {
16 16
17 // Returns a single character string where first character equals 17 // Returns a single character string where first character equals
18 // string->Get(index). 18 // string->Get(index).
19 static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { 19 static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) {
20 DCHECK_LT(index, static_cast<uint32_t>(string->length())); 20 DCHECK_LT(index, static_cast<uint32_t>(string->length()));
21 Factory* factory = string->GetIsolate()->factory(); 21 Factory* factory = string->GetIsolate()->factory();
22 return factory->LookupSingleCharacterStringFromCode( 22 return factory->LookupSingleCharacterStringFromCode(
23 String::Flatten(string)->Get(index)); 23 String::Flatten(string)->Get(index));
24 } 24 }
25 25
26 26
27 MaybeHandle<Object> Runtime::GetElementOrCharAt(Isolate* isolate, 27 MaybeHandle<Object> Runtime::GetElementOrCharAt(Isolate* isolate,
28 Handle<Object> object, 28 Handle<Object> object,
29 uint32_t index) { 29 uint32_t index,
30 LanguageMode language_mode) {
30 // Handle [] indexing on Strings 31 // Handle [] indexing on Strings
31 if (object->IsString() && 32 if (object->IsString() &&
32 index < static_cast<uint32_t>(String::cast(*object)->length())) { 33 index < static_cast<uint32_t>(String::cast(*object)->length())) {
33 Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); 34 Handle<Object> result = GetCharAt(Handle<String>::cast(object), index);
34 if (!result->IsUndefined()) return result; 35 if (!result->IsUndefined()) return result;
35 } 36 }
36 37
37 return Object::GetElement(isolate, object, index); 38 return Object::GetElement(isolate, object, index, language_mode);
38 } 39 }
39 40
40 41
41 MaybeHandle<Name> Runtime::ToName(Isolate* isolate, Handle<Object> key) { 42 MaybeHandle<Name> Runtime::ToName(Isolate* isolate, Handle<Object> key) {
42 if (key->IsName()) { 43 if (key->IsName()) {
43 return Handle<Name>::cast(key); 44 return Handle<Name>::cast(key);
44 } else { 45 } else {
45 Handle<Object> converted; 46 Handle<Object> converted;
46 ASSIGN_RETURN_ON_EXCEPTION(isolate, converted, 47 ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
47 Execution::ToString(isolate, key), Name); 48 Execution::ToString(isolate, key), Name);
48 return Handle<Name>::cast(converted); 49 return Handle<Name>::cast(converted);
49 } 50 }
50 } 51 }
51 52
52 53
53 MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate, 54 MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate,
54 Handle<Object> object, 55 Handle<Object> object,
55 Handle<Object> key) { 56 Handle<Object> key,
57 LanguageMode language_mode) {
56 if (object->IsUndefined() || object->IsNull()) { 58 if (object->IsUndefined() || object->IsNull()) {
57 THROW_NEW_ERROR( 59 THROW_NEW_ERROR(
58 isolate, 60 isolate,
59 NewTypeError(MessageTemplate::kNonObjectPropertyLoad, key, object), 61 NewTypeError(MessageTemplate::kNonObjectPropertyLoad, key, object),
60 Object); 62 Object);
61 } 63 }
62 64
63 // Check if the given key is an array index. 65 // Check if the given key is an array index.
64 uint32_t index = 0; 66 uint32_t index = 0;
65 if (key->ToArrayIndex(&index)) { 67 if (key->ToArrayIndex(&index)) {
66 return GetElementOrCharAt(isolate, object, index); 68 return GetElementOrCharAt(isolate, object, index, language_mode);
67 } 69 }
68 70
69 // Convert the key to a name - possibly by calling back into JavaScript. 71 // Convert the key to a name - possibly by calling back into JavaScript.
70 Handle<Name> name; 72 Handle<Name> name;
71 ASSIGN_RETURN_ON_EXCEPTION(isolate, name, ToName(isolate, key), Object); 73 ASSIGN_RETURN_ON_EXCEPTION(isolate, name, ToName(isolate, key), Object);
72 74
73 // Check if the name is trivially convertible to an index and get 75 // Check if the name is trivially convertible to an index and get
74 // the element if so. 76 // the element if so.
75 if (name->AsArrayIndex(&index)) { 77 if (name->AsArrayIndex(&index)) {
76 return GetElementOrCharAt(isolate, object, index); 78 return GetElementOrCharAt(isolate, object, index);
77 } else { 79 } else {
78 return Object::GetProperty(object, name); 80 return Object::GetProperty(object, name, language_mode);
79 } 81 }
80 } 82 }
81 83
82 84
85 MUST_USE_RESULT static MaybeHandle<Object> TransitionElements(
86 Handle<Object> object, ElementsKind to_kind, Isolate* isolate) {
87 HandleScope scope(isolate);
88 if (!object->IsJSObject()) {
89 isolate->ThrowIllegalOperation();
90 return MaybeHandle<Object>();
91 }
92 ElementsKind from_kind =
93 Handle<JSObject>::cast(object)->map()->elements_kind();
94 if (Map::IsValidElementsTransition(from_kind, to_kind)) {
95 JSObject::TransitionElementsKind(Handle<JSObject>::cast(object), to_kind);
96 return object;
97 }
98 isolate->ThrowIllegalOperation();
99 return MaybeHandle<Object>();
100 }
101
102
103 MaybeHandle<Object> Runtime::KeyedGetObjectProperty(
104 Isolate* isolate, Handle<Object> receiver_obj, Handle<Object> key_obj,
105 LanguageMode language_mode) {
106 // Fast cases for getting named properties of the receiver JSObject
107 // itself.
108 //
109 // The global proxy objects has to be excluded since LookupOwn on
110 // the global proxy object can return a valid result even though the
111 // global proxy object never has properties. This is the case
112 // because the global proxy object forwards everything to its hidden
113 // prototype including own lookups.
114 //
115 // Additionally, we need to make sure that we do not cache results
116 // for objects that require access checks.
117 if (receiver_obj->IsJSObject()) {
118 if (!receiver_obj->IsJSGlobalProxy() &&
119 !receiver_obj->IsAccessCheckNeeded() && key_obj->IsName()) {
120 DisallowHeapAllocation no_allocation;
121 Handle<JSObject> receiver = Handle<JSObject>::cast(receiver_obj);
122 Handle<Name> key = Handle<Name>::cast(key_obj);
123 if (receiver->IsGlobalObject()) {
124 // Attempt dictionary lookup.
125 GlobalDictionary* dictionary = receiver->global_dictionary();
126 int entry = dictionary->FindEntry(key);
127 if (entry != GlobalDictionary::kNotFound) {
128 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
129 PropertyCell* cell = PropertyCell::cast(dictionary->ValueAt(entry));
130 if (cell->property_details().type() == DATA) {
131 Object* value = cell->value();
132 if (!value->IsTheHole()) return Handle<Object>(value, isolate);
133 // If value is the hole (meaning, absent) do the general lookup.
134 }
135 }
136 } else if (!receiver->HasFastProperties()) {
137 // Attempt dictionary lookup.
138 NameDictionary* dictionary = receiver->property_dictionary();
139 int entry = dictionary->FindEntry(key);
140 if ((entry != NameDictionary::kNotFound) &&
141 (dictionary->DetailsAt(entry).type() == DATA)) {
142 Object* value = dictionary->ValueAt(entry);
143 return Handle<Object>(value, isolate);
144 }
145 }
146 } else if (key_obj->IsSmi()) {
147 // JSObject without a name key. If the key is a Smi, check for a
148 // definite out-of-bounds access to elements, which is a strong indicator
149 // that subsequent accesses will also call the runtime. Proactively
150 // transition elements to FAST_*_ELEMENTS to avoid excessive boxing of
151 // doubles for those future calls in the case that the elements would
152 // become FAST_DOUBLE_ELEMENTS.
153 Handle<JSObject> js_object = Handle<JSObject>::cast(receiver_obj);
154 ElementsKind elements_kind = js_object->GetElementsKind();
155 if (IsFastDoubleElementsKind(elements_kind)) {
156 Handle<Smi> key = Handle<Smi>::cast(key_obj);
157 if (key->value() >= js_object->elements()->length()) {
158 if (IsFastHoleyElementsKind(elements_kind)) {
159 elements_kind = FAST_HOLEY_ELEMENTS;
160 } else {
161 elements_kind = FAST_ELEMENTS;
162 }
163 RETURN_ON_EXCEPTION(
164 isolate, TransitionElements(js_object, elements_kind, isolate),
165 Object);
166 }
167 } else {
168 DCHECK(IsFastSmiOrObjectElementsKind(elements_kind) ||
169 !IsFastElementsKind(elements_kind));
170 }
171 }
172 } else if (receiver_obj->IsString() && key_obj->IsSmi()) {
173 // Fast case for string indexing using [] with a smi index.
174 Handle<String> str = Handle<String>::cast(receiver_obj);
175 int index = Handle<Smi>::cast(key_obj)->value();
176 if (index >= 0 && index < str->length()) {
177 return GetCharAt(str, index);
178 }
179 }
180
181 // Fall back to GetObjectProperty.
182 return GetObjectProperty(isolate, receiver_obj, key_obj, language_mode);
183 }
184
185
83 MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate, 186 MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate,
84 Handle<Object> object, 187 Handle<Object> object,
85 Handle<Object> key, 188 Handle<Object> key,
86 Handle<Object> value, 189 Handle<Object> value,
87 LanguageMode language_mode) { 190 LanguageMode language_mode) {
88 if (object->IsUndefined() || object->IsNull()) { 191 if (object->IsUndefined() || object->IsNull()) {
89 THROW_NEW_ERROR( 192 THROW_NEW_ERROR(
90 isolate, 193 isolate,
91 NewTypeError(MessageTemplate::kNonObjectPropertyStore, key, object), 194 NewTypeError(MessageTemplate::kNonObjectPropertyStore, key, object),
92 Object); 195 Object);
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 !object->map()->is_observed() && !object->IsJSProxy()); 595 !object->map()->is_observed() && !object->IsJSProxy());
493 596
494 Handle<Object> result; 597 Handle<Object> result;
495 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Seal(object)); 598 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Seal(object));
496 return *result; 599 return *result;
497 } 600 }
498 601
499 602
500 RUNTIME_FUNCTION(Runtime_GetProperty) { 603 RUNTIME_FUNCTION(Runtime_GetProperty) {
501 HandleScope scope(isolate); 604 HandleScope scope(isolate);
502 DCHECK(args.length() == 2); 605 DCHECK(args.length() == 3);
503 606
504 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); 607 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
505 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); 608 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
609 CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode, 2);
610
506 Handle<Object> result; 611 Handle<Object> result;
507 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 612 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
508 isolate, result, Runtime::GetObjectProperty(isolate, object, key)); 613 isolate, result,
614 Runtime::GetObjectProperty(isolate, object, key, language_mode));
509 return *result; 615 return *result;
510 } 616 }
511 617
512 618
513 MUST_USE_RESULT static MaybeHandle<Object> TransitionElements(
514 Handle<Object> object, ElementsKind to_kind, Isolate* isolate) {
515 HandleScope scope(isolate);
516 if (!object->IsJSObject()) {
517 isolate->ThrowIllegalOperation();
518 return MaybeHandle<Object>();
519 }
520 ElementsKind from_kind =
521 Handle<JSObject>::cast(object)->map()->elements_kind();
522 if (Map::IsValidElementsTransition(from_kind, to_kind)) {
523 JSObject::TransitionElementsKind(Handle<JSObject>::cast(object), to_kind);
524 return object;
525 }
526 isolate->ThrowIllegalOperation();
527 return MaybeHandle<Object>();
528 }
529
530
531 // KeyedGetProperty is called from KeyedLoadIC::GenerateGeneric.
532 RUNTIME_FUNCTION(Runtime_KeyedGetProperty) { 619 RUNTIME_FUNCTION(Runtime_KeyedGetProperty) {
533 HandleScope scope(isolate); 620 HandleScope scope(isolate);
534 DCHECK(args.length() == 2); 621 DCHECK(args.length() == 3);
535 622
536 CONVERT_ARG_HANDLE_CHECKED(Object, receiver_obj, 0); 623 CONVERT_ARG_HANDLE_CHECKED(Object, receiver_obj, 0);
537 CONVERT_ARG_HANDLE_CHECKED(Object, key_obj, 1); 624 CONVERT_ARG_HANDLE_CHECKED(Object, key_obj, 1);
625 CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode, 2);
538 626
539 // Fast cases for getting named properties of the receiver JSObject
540 // itself.
541 //
542 // The global proxy objects has to be excluded since LookupOwn on
543 // the global proxy object can return a valid result even though the
544 // global proxy object never has properties. This is the case
545 // because the global proxy object forwards everything to its hidden
546 // prototype including own lookups.
547 //
548 // Additionally, we need to make sure that we do not cache results
549 // for objects that require access checks.
550 if (receiver_obj->IsJSObject()) {
551 if (!receiver_obj->IsJSGlobalProxy() &&
552 !receiver_obj->IsAccessCheckNeeded() && key_obj->IsName()) {
553 DisallowHeapAllocation no_allocation;
554 Handle<JSObject> receiver = Handle<JSObject>::cast(receiver_obj);
555 Handle<Name> key = Handle<Name>::cast(key_obj);
556 if (receiver->IsGlobalObject()) {
557 // Attempt dictionary lookup.
558 GlobalDictionary* dictionary = receiver->global_dictionary();
559 int entry = dictionary->FindEntry(key);
560 if (entry != GlobalDictionary::kNotFound) {
561 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
562 PropertyCell* cell = PropertyCell::cast(dictionary->ValueAt(entry));
563 if (cell->property_details().type() == DATA) {
564 Object* value = cell->value();
565 if (!value->IsTheHole()) return value;
566 // If value is the hole (meaning, absent) do the general lookup.
567 }
568 }
569 } else if (!receiver->HasFastProperties()) {
570 // Attempt dictionary lookup.
571 NameDictionary* dictionary = receiver->property_dictionary();
572 int entry = dictionary->FindEntry(key);
573 if ((entry != NameDictionary::kNotFound) &&
574 (dictionary->DetailsAt(entry).type() == DATA)) {
575 Object* value = dictionary->ValueAt(entry);
576 return value;
577 }
578 }
579 } else if (key_obj->IsSmi()) {
580 // JSObject without a name key. If the key is a Smi, check for a
581 // definite out-of-bounds access to elements, which is a strong indicator
582 // that subsequent accesses will also call the runtime. Proactively
583 // transition elements to FAST_*_ELEMENTS to avoid excessive boxing of
584 // doubles for those future calls in the case that the elements would
585 // become FAST_DOUBLE_ELEMENTS.
586 Handle<JSObject> js_object = Handle<JSObject>::cast(receiver_obj);
587 ElementsKind elements_kind = js_object->GetElementsKind();
588 if (IsFastDoubleElementsKind(elements_kind)) {
589 Handle<Smi> key = Handle<Smi>::cast(key_obj);
590 if (key->value() >= js_object->elements()->length()) {
591 if (IsFastHoleyElementsKind(elements_kind)) {
592 elements_kind = FAST_HOLEY_ELEMENTS;
593 } else {
594 elements_kind = FAST_ELEMENTS;
595 }
596 RETURN_FAILURE_ON_EXCEPTION(
597 isolate, TransitionElements(js_object, elements_kind, isolate));
598 }
599 } else {
600 DCHECK(IsFastSmiOrObjectElementsKind(elements_kind) ||
601 !IsFastElementsKind(elements_kind));
602 }
603 }
604 } else if (receiver_obj->IsString() && key_obj->IsSmi()) {
605 // Fast case for string indexing using [] with a smi index.
606 Handle<String> str = Handle<String>::cast(receiver_obj);
607 int index = args.smi_at(1);
608 if (index >= 0 && index < str->length()) {
609 return *GetCharAt(str, index);
610 }
611 }
612
613 // Fall back to GetObjectProperty.
614 Handle<Object> result; 627 Handle<Object> result;
615 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 628 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
616 isolate, result, 629 isolate, result, Runtime::KeyedGetObjectProperty(isolate, receiver_obj,
617 Runtime::GetObjectProperty(isolate, receiver_obj, key_obj)); 630 key_obj, language_mode));
618 return *result; 631 return *result;
619 } 632 }
620 633
621 634
622 RUNTIME_FUNCTION(Runtime_AddNamedProperty) { 635 RUNTIME_FUNCTION(Runtime_AddNamedProperty) {
623 HandleScope scope(isolate); 636 HandleScope scope(isolate);
624 RUNTIME_ASSERT(args.length() == 4); 637 RUNTIME_ASSERT(args.length() == 4);
625 638
626 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); 639 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
627 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1); 640 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
(...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after
1542 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3); 1555 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1543 1556
1544 RETURN_FAILURE_ON_EXCEPTION( 1557 RETURN_FAILURE_ON_EXCEPTION(
1545 isolate, 1558 isolate,
1546 JSObject::DefineAccessor(object, name, isolate->factory()->null_value(), 1559 JSObject::DefineAccessor(object, name, isolate->factory()->null_value(),
1547 setter, attrs)); 1560 setter, attrs));
1548 return isolate->heap()->undefined_value(); 1561 return isolate->heap()->undefined_value();
1549 } 1562 }
1550 } // namespace internal 1563 } // namespace internal
1551 } // namespace v8 1564 } // namespace v8
OLDNEW
« src/lookup.h ('K') | « src/runtime/runtime-debug.cc ('k') | src/x64/code-stubs-x64.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698