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

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, 3 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
« no previous file with comments | « runtime/vm/isolate.h ('k') | runtime/vm/json_stream.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 908 matching lines...) Expand 10 before | Expand all | Expand 10 after
1617 // Visit the tag table which is stored in the isolate. 1628 // Visit the tag table which is stored in the isolate.
1618 visitor->VisitPointer(reinterpret_cast<RawObject**>(&tag_table_)); 1629 visitor->VisitPointer(reinterpret_cast<RawObject**>(&tag_table_));
1619 1630
1620 // Visit array of closures pending precompilation. 1631 // Visit array of closures pending precompilation.
1621 visitor->VisitPointer(reinterpret_cast<RawObject**>(&collected_closures_)); 1632 visitor->VisitPointer(reinterpret_cast<RawObject**>(&collected_closures_));
1622 1633
1623 // Visit the deoptimized code array which is stored in the isolate. 1634 // Visit the deoptimized code array which is stored in the isolate.
1624 visitor->VisitPointer( 1635 visitor->VisitPointer(
1625 reinterpret_cast<RawObject**>(&deoptimized_code_array_)); 1636 reinterpret_cast<RawObject**>(&deoptimized_code_array_));
1626 1637
1638 // Visit the pending service extension calls.
1639 visitor->VisitPointer(
1640 reinterpret_cast<RawObject**>(&pending_extension_calls_));
1641
1627 // Visit objects in the debugger. 1642 // Visit objects in the debugger.
1628 debugger()->VisitObjectPointers(visitor); 1643 debugger()->VisitObjectPointers(visitor);
1629 1644
1630 // Visit objects that are being used for deoptimization. 1645 // Visit objects that are being used for deoptimization.
1631 if (deopt_context() != NULL) { 1646 if (deopt_context() != NULL) {
1632 deopt_context()->VisitObjectPointers(visitor); 1647 deopt_context()->VisitObjectPointers(visitor);
1633 } 1648 }
1634 1649
1635 // Visit objects in thread registry (e.g., Dart stack, handles in zones). 1650 // Visit objects in thread registry (e.g., Dart stack, handles in zones).
1636 thread_registry()->VisitObjectPointers(visitor, validate_frames); 1651 thread_registry()->VisitObjectPointers(visitor, validate_frames);
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1838 if (deoptimized_code.IsNull()) { 1853 if (deoptimized_code.IsNull()) {
1839 // Not tracking deoptimized code. 1854 // Not tracking deoptimized code.
1840 return; 1855 return;
1841 } 1856 }
1842 // TODO(johnmccutchan): Scan this array and the isolate's profile before 1857 // TODO(johnmccutchan): Scan this array and the isolate's profile before
1843 // old space GC and remove the keep_code flag. 1858 // old space GC and remove the keep_code flag.
1844 deoptimized_code.Add(code); 1859 deoptimized_code.Add(code);
1845 } 1860 }
1846 1861
1847 1862
1863 void Isolate::set_pending_extension_calls(const GrowableObjectArray& value) {
1864 pending_extension_calls_ = value.raw();
1865 }
1866
1867
1868 void Isolate::set_registered_extension_handlers(
1869 const GrowableObjectArray& value) {
1870 registered_extension_handlers_ = value.raw();
1871 }
1872
1873
1874 RawObject* Isolate::InvokePendingExtensionCalls() {
1875 GrowableObjectArray& calls =
1876 GrowableObjectArray::Handle(GetAndClearPendingExtensionCalls());
1877 if (calls.IsNull()) {
1878 return Object::null();
1879 }
1880 // Grab run function.
1881 const Library& developer_lib = Library::Handle(Library::DeveloperLibrary());
1882 ASSERT(!developer_lib.IsNull());
1883 const Function& run_extension = Function::Handle(
1884 developer_lib.LookupLocalFunction(Symbols::_runExtension()));
1885 ASSERT(!run_extension.IsNull());
1886
1887 const Array& arguments =
1888 Array::Handle(Array::New(kPendingEntrySize, Heap::kNew));
1889 Object& result = Object::Handle();
1890 String& method_name = String::Handle();
1891 Instance& closure = Instance::Handle();
1892 Array& parameter_keys = Array::Handle();
1893 Array& parameter_values = Array::Handle();
1894 Instance& reply_port = Instance::Handle();
1895 Instance& id = Instance::Handle();
1896 for (intptr_t i = 0; i < calls.Length(); i += kPendingEntrySize) {
1897 // Grab arguments for call.
1898 closure ^= calls.At(i + kPendingHandlerIndex);
1899 ASSERT(!closure.IsNull());
1900 arguments.SetAt(kPendingHandlerIndex, closure);
1901 method_name ^= calls.At(i + kPendingMethodNameIndex);
1902 ASSERT(!method_name.IsNull());
1903 arguments.SetAt(kPendingMethodNameIndex, method_name);
1904 parameter_keys ^= calls.At(i + kPendingKeysIndex);
1905 ASSERT(!parameter_keys.IsNull());
1906 arguments.SetAt(kPendingKeysIndex, parameter_keys);
1907 parameter_values ^= calls.At(i + kPendingValuesIndex);
1908 ASSERT(!parameter_values.IsNull());
1909 arguments.SetAt(kPendingValuesIndex, parameter_values);
1910 reply_port ^= calls.At(i + kPendingReplyPortIndex);
1911 ASSERT(!reply_port.IsNull());
1912 arguments.SetAt(kPendingReplyPortIndex, reply_port);
1913 id ^= calls.At(i + kPendingIdIndex);
1914 arguments.SetAt(kPendingIdIndex, id);
1915
1916 result = DartEntry::InvokeFunction(run_extension, arguments);
1917 if (result.IsError()) {
1918 if (result.IsUnwindError()) {
1919 // Propagate the unwind error. Remaining service extension calls
1920 // are dropped.
1921 return result.raw();
1922 } else {
1923 // Send error back over the protocol.
1924 Service::PostError(method_name,
1925 parameter_keys,
1926 parameter_values,
1927 reply_port,
1928 id,
1929 Error::Cast(result));
1930 }
1931 }
1932 result = DartLibraryCalls::DrainMicrotaskQueue();
1933 if (result.IsError()) {
1934 return result.raw();
1935 }
1936 }
1937 return Object::null();
1938 }
1939
1940
1941 RawGrowableObjectArray* Isolate::GetAndClearPendingExtensionCalls() {
1942 RawGrowableObjectArray* r = pending_extension_calls_;
1943 pending_extension_calls_ = GrowableObjectArray::null();
1944 return r;
1945 }
1946
1947
1948 void Isolate::AppendExtensionCall(const Instance& closure,
1949 const String& method_name,
1950 const Array& parameter_keys,
1951 const Array& parameter_values,
1952 const Instance& reply_port,
1953 const Instance& id) {
1954 GrowableObjectArray& calls =
1955 GrowableObjectArray::Handle(pending_extension_calls());
1956 if (calls.IsNull()) {
1957 calls ^= GrowableObjectArray::New(Heap::kOld);
Ivan Posva 2015/08/27 06:15:29 Why in old space?
Cutch 2015/08/27 13:27:49 Done.
1958 ASSERT(!calls.IsNull());
1959 set_pending_extension_calls(calls);
1960 }
1961 ASSERT(kPendingHandlerIndex == 0);
1962 calls.Add(closure, Heap::kOld);
Ivan Posva 2015/08/27 06:15:29 ditto here and below. Why old?
Cutch 2015/08/27 13:27:49 Done.
1963 ASSERT(kPendingMethodNameIndex == 1);
1964 calls.Add(method_name, Heap::kOld);
1965 ASSERT(kPendingKeysIndex == 2);
1966 calls.Add(parameter_keys, Heap::kOld);
1967 ASSERT(kPendingValuesIndex == 3);
1968 calls.Add(parameter_values, Heap::kOld);
1969 ASSERT(kPendingReplyPortIndex == 4);
1970 calls.Add(reply_port, Heap::kOld);
1971 ASSERT(kPendingIdIndex == 5);
1972 calls.Add(id, Heap::kOld);
1973 }
1974
1975
1976 void Isolate::RegisterExtensionHandler(const String& name,
Ivan Posva 2015/08/27 06:15:29 These are really VM service extension handlers, co
Cutch 2015/08/27 13:27:49 Done.
1977 const Instance& closure) {
1978 GrowableObjectArray& handlers =
1979 GrowableObjectArray::Handle(registered_extension_handlers());
1980 if (handlers.IsNull()) {
1981 handlers ^= GrowableObjectArray::New(Heap::kOld);
1982 set_registered_extension_handlers(handlers);
1983 }
1984 #if defined(DEBUG)
1985 {
1986 // Sanity check.
1987 const Instance& existing_handler =
1988 Instance::Handle(LookupExtensionHandler(name));
1989 ASSERT(existing_handler.IsNull());
1990 }
1991 #endif
1992 ASSERT(kRegisteredNameIndex == 0);
1993 handlers.Add(name, Heap::kOld);
1994 ASSERT(kRegisteredHandlerIndex == 1);
1995 handlers.Add(closure, Heap::kOld);
1996 }
1997
1998
1999 RawInstance* Isolate::LookupExtensionHandler(const String& name) {
Ivan Posva 2015/08/27 06:15:29 Why isn't the extension list not maintained in the
Cutch 2015/08/27 13:27:49 Because doing that in Dart code could lead to inco
Ivan Posva 2015/08/27 18:12:26 Many thanks for the explanation. Please add it as
Cutch 2015/08/27 18:23:29 Done.
2000 const GrowableObjectArray& handlers =
2001 GrowableObjectArray::Handle(registered_extension_handlers());
2002 if (handlers.IsNull()) {
2003 return Instance::null();
2004 }
2005 String& handler_name = String::Handle();
2006 for (intptr_t i = 0; i < handlers.Length(); i += kRegisteredEntrySize) {
2007 handler_name ^= handlers.At(i + kRegisteredNameIndex);
2008 ASSERT(!handler_name.IsNull());
2009 if (handler_name.Equals(name)) {
2010 return Instance::RawCast(handlers.At(i + kRegisteredHandlerIndex));
2011 }
2012 }
2013 return Instance::null();
2014 }
2015
2016
1848 void Isolate::WakePauseEventHandler(Dart_Isolate isolate) { 2017 void Isolate::WakePauseEventHandler(Dart_Isolate isolate) {
1849 Isolate* iso = reinterpret_cast<Isolate*>(isolate); 2018 Isolate* iso = reinterpret_cast<Isolate*>(isolate);
1850 MonitorLocker ml(iso->pause_loop_monitor_); 2019 MonitorLocker ml(iso->pause_loop_monitor_);
1851 ml.Notify(); 2020 ml.Notify();
1852 } 2021 }
1853 2022
1854 2023
1855 void Isolate::PauseEventHandler() { 2024 void Isolate::PauseEventHandler() {
1856 // We are stealing a pause event (like a breakpoint) from the 2025 // We are stealing a pause event (like a breakpoint) from the
1857 // embedder. We don't know what kind of thread we are on -- it 2026 // 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
2153 serialized_message_, serialized_message_len_); 2322 serialized_message_, serialized_message_len_);
2154 } 2323 }
2155 2324
2156 2325
2157 void IsolateSpawnState::Cleanup() { 2326 void IsolateSpawnState::Cleanup() {
2158 SwitchIsolateScope switch_scope(I); 2327 SwitchIsolateScope switch_scope(I);
2159 Dart::ShutdownIsolate(); 2328 Dart::ShutdownIsolate();
2160 } 2329 }
2161 2330
2162 } // namespace dart 2331 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/isolate.h ('k') | runtime/vm/json_stream.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698