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

Side by Side Diff: runtime/vm/isolate.cc

Issue 1299493007: Rework service extensions to be safe (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 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 (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/isolate.h" 5 #include "vm/isolate.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "platform/json.h" 9 #include "platform/json.h"
10 #include "vm/code_observers.h" 10 #include "vm/code_observers.h"
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 } else { 485 } else {
486 const Object& result = Object::Handle(zone, 486 const Object& result = Object::Handle(zone,
487 DartLibraryCalls::HandleMessage(msg_handler, msg)); 487 DartLibraryCalls::HandleMessage(msg_handler, msg));
488 if (result.IsError()) { 488 if (result.IsError()) {
489 success = ProcessUnhandledException(Error::Cast(result)); 489 success = ProcessUnhandledException(Error::Cast(result));
490 } else { 490 } else {
491 ASSERT(result.IsNull()); 491 ASSERT(result.IsNull());
492 } 492 }
493 } 493 }
494 delete message; 494 delete message;
495 if (success) {
496 const Object& result =
497 Object::Handle(zone, I->InvokePendingExtensionCalls());
498 if (result.IsError()) {
499 success = ProcessUnhandledException(Error::Cast(result));
500 } else {
501 ASSERT(result.IsNull());
502 }
503 }
495 return success; 504 return success;
496 } 505 }
497 506
498 507
499 void IsolateMessageHandler::NotifyPauseOnStart() { 508 void IsolateMessageHandler::NotifyPauseOnStart() {
500 if (Service::debug_stream.enabled()) { 509 if (Service::debug_stream.enabled()) {
501 StartIsolateScope start_isolate(isolate()); 510 StartIsolateScope start_isolate(isolate());
502 StackZone zone(I); 511 StackZone zone(I);
503 HandleScope handle_scope(I); 512 HandleScope handle_scope(I);
504 ServiceEvent pause_event(isolate(), ServiceEvent::kPauseStart); 513 ServiceEvent pause_event(isolate(), ServiceEvent::kPauseStart);
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 last_allocationprofile_accumulator_reset_timestamp_(0), 698 last_allocationprofile_accumulator_reset_timestamp_(0),
690 last_allocationprofile_gc_timestamp_(0), 699 last_allocationprofile_gc_timestamp_(0),
691 object_id_ring_(NULL), 700 object_id_ring_(NULL),
692 trace_buffer_(NULL), 701 trace_buffer_(NULL),
693 profiler_data_(NULL), 702 profiler_data_(NULL),
694 tag_table_(GrowableObjectArray::null()), 703 tag_table_(GrowableObjectArray::null()),
695 current_tag_(UserTag::null()), 704 current_tag_(UserTag::null()),
696 default_tag_(UserTag::null()), 705 default_tag_(UserTag::null()),
697 collected_closures_(GrowableObjectArray::null()), 706 collected_closures_(GrowableObjectArray::null()),
698 deoptimized_code_array_(GrowableObjectArray::null()), 707 deoptimized_code_array_(GrowableObjectArray::null()),
708 pending_extension_calls_(GrowableObjectArray::null()),
709 registered_extension_handlers_(GrowableObjectArray::null()),
699 metrics_list_head_(NULL), 710 metrics_list_head_(NULL),
700 compilation_allowed_(true), 711 compilation_allowed_(true),
701 cha_(NULL), 712 cha_(NULL),
702 next_(NULL), 713 next_(NULL),
703 pause_loop_monitor_(NULL), 714 pause_loop_monitor_(NULL),
704 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_INITIALIZERS) 715 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_INITIALIZERS)
705 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_INIT) 716 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_INIT)
706 reusable_handles_() { 717 reusable_handles_() {
707 flags_.CopyFrom(api_flags); 718 flags_.CopyFrom(api_flags);
708 set_vm_tag(VMTag::kEmbedderTagId); 719 set_vm_tag(VMTag::kEmbedderTagId);
(...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after
1613 // Visit the tag table which is stored in the isolate. 1624 // Visit the tag table which is stored in the isolate.
1614 visitor->VisitPointer(reinterpret_cast<RawObject**>(&tag_table_)); 1625 visitor->VisitPointer(reinterpret_cast<RawObject**>(&tag_table_));
1615 1626
1616 // Visit array of closures pending precompilation. 1627 // Visit array of closures pending precompilation.
1617 visitor->VisitPointer(reinterpret_cast<RawObject**>(&collected_closures_)); 1628 visitor->VisitPointer(reinterpret_cast<RawObject**>(&collected_closures_));
1618 1629
1619 // Visit the deoptimized code array which is stored in the isolate. 1630 // Visit the deoptimized code array which is stored in the isolate.
1620 visitor->VisitPointer( 1631 visitor->VisitPointer(
1621 reinterpret_cast<RawObject**>(&deoptimized_code_array_)); 1632 reinterpret_cast<RawObject**>(&deoptimized_code_array_));
1622 1633
1634 // Visit the pending service extension calls.
1635 visitor->VisitPointer(
1636 reinterpret_cast<RawObject**>(&pending_extension_calls_));
1637
1623 // Visit objects in the debugger. 1638 // Visit objects in the debugger.
1624 debugger()->VisitObjectPointers(visitor); 1639 debugger()->VisitObjectPointers(visitor);
1625 1640
1626 // Visit objects that are being used for deoptimization. 1641 // Visit objects that are being used for deoptimization.
1627 if (deopt_context() != NULL) { 1642 if (deopt_context() != NULL) {
1628 deopt_context()->VisitObjectPointers(visitor); 1643 deopt_context()->VisitObjectPointers(visitor);
1629 } 1644 }
1630 1645
1631 // Visit objects in thread registry (e.g., Dart stack, handles in zones). 1646 // Visit objects in thread registry (e.g., Dart stack, handles in zones).
1632 thread_registry()->VisitObjectPointers(visitor, validate_frames); 1647 thread_registry()->VisitObjectPointers(visitor, validate_frames);
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 if (deoptimized_code.IsNull()) { 1842 if (deoptimized_code.IsNull()) {
1828 // Not tracking deoptimized code. 1843 // Not tracking deoptimized code.
1829 return; 1844 return;
1830 } 1845 }
1831 // TODO(johnmccutchan): Scan this array and the isolate's profile before 1846 // TODO(johnmccutchan): Scan this array and the isolate's profile before
1832 // old space GC and remove the keep_code flag. 1847 // old space GC and remove the keep_code flag.
1833 deoptimized_code.Add(code); 1848 deoptimized_code.Add(code);
1834 } 1849 }
1835 1850
1836 1851
1852 void Isolate::set_pending_extension_calls(const GrowableObjectArray& value) {
1853 pending_extension_calls_ = value.raw();
1854 }
1855
1856
1857 void Isolate::set_registered_extension_handlers(
1858 const GrowableObjectArray& value) {
1859 registered_extension_handlers_ = value.raw();
1860 }
1861
1862
1863 RawObject* Isolate::InvokePendingExtensionCalls() {
1864 GrowableObjectArray& calls =
1865 GrowableObjectArray::Handle(GetAndClearPendingExtensionCalls());
1866 if (calls.IsNull()) {
1867 return Object::null();
1868 }
1869 // Grab run function.
1870 const Library& developer_lib = Library::Handle(Library::DeveloperLibrary());
1871 ASSERT(!developer_lib.IsNull());
1872 const Function& run_extension = Function::Handle(
1873 developer_lib.LookupLocalFunction(Symbols::_runExtension()));
1874 ASSERT(!run_extension.IsNull());
1875
1876 const Array& arguments =
1877 Array::Handle(Array::New(kPendingEntrySize, Heap::kNew));
1878 Object& result = Object::Handle();
1879 String& method_name = String::Handle();
1880 Instance& closure = Instance::Handle();
1881 Array& parameter_keys = Array::Handle();
1882 Array& parameter_values = Array::Handle();
1883 Instance& reply_port = Instance::Handle();
1884 Instance& id = Instance::Handle();
1885 for (intptr_t i = 0; i < calls.Length(); i += kPendingEntrySize) {
1886 // Grab arguments for call.
1887 closure ^= calls.At(i + kPendingHandlerIndex);
1888 ASSERT(!closure.IsNull());
1889 arguments.SetAt(kPendingHandlerIndex, closure);
1890 method_name ^= calls.At(i + kPendingMethodNameIndex);
1891 ASSERT(!method_name.IsNull());
1892 arguments.SetAt(kPendingMethodNameIndex, method_name);
1893 parameter_keys ^= calls.At(i + kPendingKeysIndex);
1894 ASSERT(!parameter_keys.IsNull());
1895 arguments.SetAt(kPendingKeysIndex, parameter_keys);
1896 parameter_values ^= calls.At(i + kPendingValuesIndex);
1897 ASSERT(!parameter_values.IsNull());
1898 arguments.SetAt(kPendingValuesIndex, parameter_values);
1899 reply_port ^= calls.At(i + kPendingReplyPortIndex);
1900 ASSERT(!reply_port.IsNull());
1901 arguments.SetAt(kPendingReplyPortIndex, reply_port);
1902 id ^= calls.At(i + kPendingIdIndex);
1903 arguments.SetAt(kPendingIdIndex, id);
1904
1905 result = DartEntry::InvokeFunction(run_extension, arguments);
1906 if (result.IsError()) {
1907 if (result.IsUnwindError()) {
1908 // Propagate the unwind error. Remaining service extension calls
1909 // are dropped.
1910 return result.raw();
1911 } else {
1912 // Send error back over the protocol.
1913 Service::PostError(method_name,
1914 parameter_keys,
1915 parameter_values,
1916 reply_port,
1917 id,
1918 Error::Cast(result));
1919 }
1920 }
1921 result = DartLibraryCalls::DrainMicrotaskQueue();
1922 if (result.IsError()) {
1923 return result.raw();
1924 }
1925 }
1926 return Object::null();
1927 }
1928
1929
1930 RawGrowableObjectArray* Isolate::GetAndClearPendingExtensionCalls() {
1931 RawGrowableObjectArray* r = pending_extension_calls_;
1932 pending_extension_calls_ = GrowableObjectArray::null();
1933 return r;
1934 }
1935
1936
1937 void Isolate::AppendExtensionCall(const Instance& closure,
1938 const String& method_name,
1939 const Array& parameter_keys,
1940 const Array& parameter_values,
1941 const Instance& reply_port,
1942 const Instance& id) {
1943 GrowableObjectArray& calls =
1944 GrowableObjectArray::Handle(pending_extension_calls());
1945 if (calls.IsNull()) {
1946 calls ^= GrowableObjectArray::New(Heap::kOld);
1947 ASSERT(!calls.IsNull());
1948 set_pending_extension_calls(calls);
1949 }
1950 ASSERT(kPendingHandlerIndex == 0);
1951 calls.Add(closure, Heap::kOld);
1952 ASSERT(kPendingMethodNameIndex == 1);
1953 calls.Add(method_name, Heap::kOld);
1954 ASSERT(kPendingKeysIndex == 2);
1955 calls.Add(parameter_keys, Heap::kOld);
1956 ASSERT(kPendingValuesIndex == 3);
1957 calls.Add(parameter_values, Heap::kOld);
1958 ASSERT(kPendingReplyPortIndex == 4);
1959 calls.Add(reply_port, Heap::kOld);
1960 ASSERT(kPendingIdIndex == 5);
1961 calls.Add(id, Heap::kOld);
1962 }
1963
1964
1965 void Isolate::RegisterExtensionHandler(const String& name,
1966 const Instance& closure) {
1967 GrowableObjectArray& handlers =
1968 GrowableObjectArray::Handle(registered_extension_handlers());
1969 if (handlers.IsNull()) {
1970 handlers ^= GrowableObjectArray::New(Heap::kOld);
1971 set_registered_extension_handlers(handlers);
1972 }
1973 #if defined(DEBUG)
1974 {
1975 // Sanity check.
1976 const Instance& existing_handler =
1977 Instance::Handle(LookupExtensionHandler(name));
1978 ASSERT(existing_handler.IsNull());
1979 }
1980 #endif
1981 ASSERT(kRegisteredNameIndex == 0);
1982 handlers.Add(name, Heap::kOld);
1983 ASSERT(kRegisteredHandlerIndex == 1);
1984 handlers.Add(closure, Heap::kOld);
1985 }
1986
1987
1988 RawInstance* Isolate::LookupExtensionHandler(const String& name) {
1989 const GrowableObjectArray& handlers =
1990 GrowableObjectArray::Handle(registered_extension_handlers());
1991 if (handlers.IsNull()) {
1992 return Instance::null();
1993 }
1994 String& handler_name = String::Handle();
1995 for (intptr_t i = 0; i < handlers.Length(); i += kRegisteredEntrySize) {
1996 handler_name ^= handlers.At(i + kRegisteredNameIndex);
1997 ASSERT(!handler_name.IsNull());
1998 if (handler_name.Equals(name)) {
1999 return Instance::RawCast(handlers.At(i + kRegisteredHandlerIndex));
2000 }
2001 }
2002 return Instance::null();
2003 }
2004
2005
1837 void Isolate::WakePauseEventHandler(Dart_Isolate isolate) { 2006 void Isolate::WakePauseEventHandler(Dart_Isolate isolate) {
1838 Isolate* iso = reinterpret_cast<Isolate*>(isolate); 2007 Isolate* iso = reinterpret_cast<Isolate*>(isolate);
1839 MonitorLocker ml(iso->pause_loop_monitor_); 2008 MonitorLocker ml(iso->pause_loop_monitor_);
1840 ml.Notify(); 2009 ml.Notify();
1841 } 2010 }
1842 2011
1843 2012
1844 void Isolate::PauseEventHandler() { 2013 void Isolate::PauseEventHandler() {
1845 // We are stealing a pause event (like a breakpoint) from the 2014 // We are stealing a pause event (like a breakpoint) from the
1846 // embedder. We don't know what kind of thread we are on -- it 2015 // embedder. We don't know what kind of thread we are on -- it
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
2142 serialized_message_, serialized_message_len_); 2311 serialized_message_, serialized_message_len_);
2143 } 2312 }
2144 2313
2145 2314
2146 void IsolateSpawnState::Cleanup() { 2315 void IsolateSpawnState::Cleanup() {
2147 SwitchIsolateScope switch_scope(I); 2316 SwitchIsolateScope switch_scope(I);
2148 Dart::ShutdownIsolate(); 2317 Dart::ShutdownIsolate();
2149 } 2318 }
2150 2319
2151 } // namespace dart 2320 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698