1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
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. |
4 | |
5 | #include "vm/bootstrap.h" |
6 | |
7 | #include <memory> |
8 | |
9 | #include "include/dart_api.h" |
10 | |
11 | #include "vm/class_finalizer.h" |
12 | #include "vm/compiler/jit/compiler.h" |
13 | #include "vm/dart_api_impl.h" |
14 | #if !defined(DART_PRECOMPILED_RUNTIME) |
15 | #include "vm/kernel.h" |
16 | #include "vm/kernel_loader.h" |
17 | #endif |
18 | #include "vm/longjump.h" |
19 | #include "vm/object.h" |
20 | #include "vm/object_store.h" |
21 | #include "vm/symbols.h" |
22 | |
23 | namespace dart { |
24 | |
25 | struct BootstrapLibProps { |
26 | ObjectStore::BootstrapLibraryId index; |
27 | const char* uri; |
28 | }; |
29 | |
30 | enum { kPathsUriOffset = 0, kPathsSourceOffset = 1, kPathsEntryLength = 2 }; |
31 | |
32 | #if !defined(DART_PRECOMPILED_RUNTIME) |
33 | #define MAKE_PROPERTIES(CamelName, name) \ |
34 | {ObjectStore::k##CamelName, "dart:" #name}, |
35 | |
36 | static const BootstrapLibProps bootstrap_libraries[] = { |
37 | FOR_EACH_BOOTSTRAP_LIBRARY(MAKE_PROPERTIES)}; |
38 | |
39 | #undef MAKE_PROPERTIES |
40 | |
41 | static const intptr_t kBootstrapLibraryCount = ARRAY_SIZE(bootstrap_libraries); |
42 | static void Finish(Thread* thread) { |
43 | Bootstrap::SetupNativeResolver(); |
44 | if (!ClassFinalizer::ProcessPendingClasses()) { |
45 | FATAL("Error in class finalization during bootstrapping." ); |
46 | } |
47 | |
48 | // Eagerly compile the _Closure class as it is the class of all closure |
49 | // instances. This allows us to just finalize function types without going |
50 | // through the hoops of trying to compile their scope class. |
51 | ObjectStore* object_store = thread->isolate()->object_store(); |
52 | Zone* zone = thread->zone(); |
53 | Class& cls = Class::Handle(zone, object_store->closure_class()); |
54 | ClassFinalizer::LoadClassMembers(cls); |
55 | |
56 | // Make sure _Closure fields are not marked as unboxing candidates |
57 | // as they are accessed with plain loads. |
58 | const Array& fields = Array::Handle(zone, cls.fields()); |
59 | Field& field = Field::Handle(zone); |
60 | for (intptr_t i = 0; i < fields.Length(); ++i) { |
61 | field ^= fields.At(i); |
62 | field.set_is_unboxing_candidate(false); |
63 | } |
64 | // _Closure._hash field should be explicitly marked as nullable because |
65 | // VM creates instances of _Closure without compiling its constructors, |
66 | // so it won't get nullability info from a constructor. |
67 | field ^= fields.At(fields.Length() - 1); |
68 | // Note that UserVisibleName depends on --show-internal-names. |
69 | ASSERT(strncmp(field.UserVisibleNameCString(), "_hash" , 5) == 0); |
70 | field.RecordStore(Object::null_object()); |
71 | |
72 | #if defined(DEBUG) |
73 | // Verify that closure field offsets are identical in Dart and C++. |
74 | ASSERT(fields.Length() == 6); |
75 | field ^= fields.At(0); |
76 | ASSERT(field.HostOffset() == Closure::instantiator_type_arguments_offset()); |
77 | field ^= fields.At(1); |
78 | ASSERT(field.HostOffset() == Closure::function_type_arguments_offset()); |
79 | field ^= fields.At(2); |
80 | ASSERT(field.HostOffset() == Closure::delayed_type_arguments_offset()); |
81 | field ^= fields.At(3); |
82 | ASSERT(field.HostOffset() == Closure::function_offset()); |
83 | field ^= fields.At(4); |
84 | ASSERT(field.HostOffset() == Closure::context_offset()); |
85 | field ^= fields.At(5); |
86 | ASSERT(field.HostOffset() == Closure::hash_offset()); |
87 | #endif // defined(DEBUG) |
88 | |
89 | // Eagerly compile Bool class, bool constants are used from within compiler. |
90 | cls = object_store->bool_class(); |
91 | ClassFinalizer::LoadClassMembers(cls); |
92 | } |
93 | |
94 | static ErrorPtr BootstrapFromKernel(Thread* thread, |
95 | const uint8_t* kernel_buffer, |
96 | intptr_t kernel_buffer_size) { |
97 | Zone* zone = thread->zone(); |
98 | const char* error = nullptr; |
99 | std::unique_ptr<kernel::Program> program = kernel::Program::ReadFromBuffer( |
100 | kernel_buffer, kernel_buffer_size, &error); |
101 | if (program == nullptr) { |
102 | const intptr_t kMessageBufferSize = 512; |
103 | char message_buffer[kMessageBufferSize]; |
104 | Utils::SNPrint(message_buffer, kMessageBufferSize, |
105 | "Can't load Kernel binary: %s." , error); |
106 | const String& msg = String::Handle(String::New(message_buffer, Heap::kOld)); |
107 | return ApiError::New(msg, Heap::kOld); |
108 | } |
109 | |
110 | LongJumpScope jump; |
111 | if (setjmp(*jump.Set()) == 0) { |
112 | kernel::KernelLoader loader(program.get(), /*uri_to_source_table=*/nullptr); |
113 | |
114 | Isolate* isolate = thread->isolate(); |
115 | |
116 | if (isolate->obfuscate()) { |
117 | loader.ReadObfuscationProhibitions(); |
118 | } |
119 | |
120 | // Load the bootstrap libraries in order (see object_store.h). |
121 | Library& library = Library::Handle(zone); |
122 | for (intptr_t i = 0; i < kBootstrapLibraryCount; ++i) { |
123 | ObjectStore::BootstrapLibraryId id = bootstrap_libraries[i].index; |
124 | library = isolate->object_store()->bootstrap_library(id); |
125 | loader.LoadLibrary(library); |
126 | } |
127 | |
128 | // Finish bootstrapping, including class finalization. |
129 | Finish(thread); |
130 | |
131 | // The platform binary may contain other libraries (e.g., dart:_builtin or |
132 | // dart:io) that will not be bundled with application. Load them now. |
133 | const Object& result = Object::Handle(zone, loader.LoadProgram()); |
134 | program.reset(); |
135 | if (result.IsError()) { |
136 | return Error::Cast(result).raw(); |
137 | } |
138 | |
139 | // The builtin library should be registered with the VM. |
140 | const auto& dart_builtin = |
141 | String::Handle(zone, String::New("dart:_builtin" )); |
142 | library = Library::LookupLibrary(thread, dart_builtin); |
143 | isolate->object_store()->set_builtin_library(library); |
144 | |
145 | if (FLAG_precompiled_mode) { |
146 | loader.ReadLoadingUnits(); |
147 | } |
148 | |
149 | return Error::null(); |
150 | } |
151 | |
152 | // Either class finalization failed or we caught a compile-time error. |
153 | // In both cases sticky error would be set. |
154 | return Thread::Current()->StealStickyError(); |
155 | } |
156 | |
157 | ErrorPtr Bootstrap::DoBootstrapping(const uint8_t* kernel_buffer, |
158 | intptr_t kernel_buffer_size) { |
159 | Thread* thread = Thread::Current(); |
160 | Isolate* isolate = thread->isolate(); |
161 | Zone* zone = thread->zone(); |
162 | String& uri = String::Handle(zone); |
163 | Library& lib = Library::Handle(zone); |
164 | |
165 | HANDLESCOPE(thread); |
166 | |
167 | // Ensure there are library objects for all the bootstrap libraries. |
168 | for (intptr_t i = 0; i < kBootstrapLibraryCount; ++i) { |
169 | ObjectStore::BootstrapLibraryId id = bootstrap_libraries[i].index; |
170 | uri = Symbols::New(thread, bootstrap_libraries[i].uri); |
171 | lib = isolate->object_store()->bootstrap_library(id); |
172 | ASSERT(lib.raw() == Library::LookupLibrary(thread, uri)); |
173 | if (lib.IsNull()) { |
174 | lib = Library::NewLibraryHelper(uri, false); |
175 | lib.SetLoadRequested(); |
176 | lib.Register(thread); |
177 | isolate->object_store()->set_bootstrap_library(id, lib); |
178 | } |
179 | } |
180 | |
181 | return BootstrapFromKernel(thread, kernel_buffer, kernel_buffer_size); |
182 | } |
183 | #else |
184 | ErrorPtr Bootstrap::DoBootstrapping(const uint8_t* kernel_buffer, |
185 | intptr_t kernel_buffer_size) { |
186 | UNREACHABLE(); |
187 | return Error::null(); |
188 | } |
189 | #endif |
190 | |
191 | } // namespace dart |
192 | |