/
isolate_reload.h
410 lines (311 loc) · 12.2 KB
/
isolate_reload.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_ISOLATE_RELOAD_H_
#define RUNTIME_VM_ISOLATE_RELOAD_H_
#include "include/dart_tools_api.h"
#include "vm/globals.h"
#include "vm/growable_array.h"
#include "vm/hash_map.h"
#include "vm/log.h"
#include "vm/object.h"
DECLARE_FLAG(bool, trace_reload);
DECLARE_FLAG(bool, trace_reload_verbose);
// 'Trace Isolate Reload' TIR_Print
#if defined(_MSC_VER)
#define TIR_Print(format, ...) \
if (FLAG_trace_reload) Log::Current()->Print(format, __VA_ARGS__)
#else
#define TIR_Print(format, ...) \
if (FLAG_trace_reload) Log::Current()->Print(format, ##__VA_ARGS__)
#endif
// 'Verbose Trace Isolate Reload' VTIR_Print
#if defined(_MSC_VER)
#define VTIR_Print(format, ...) \
if (FLAG_trace_reload_verbose) Log::Current()->Print(format, __VA_ARGS__)
#else
#define VTIR_Print(format, ...) \
if (FLAG_trace_reload_verbose) Log::Current()->Print(format, ##__VA_ARGS__)
#endif
#if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
namespace dart {
class BitVector;
class GrowableObjectArray;
class Isolate;
class Library;
class ObjectPointerVisitor;
class ObjectStore;
class RawError;
class RawGrowableObjectArray;
class RawLibrary;
class RawObject;
class RawString;
class Script;
class UpdateClassesVisitor;
class InstanceMorpher : public ZoneAllocated {
public:
InstanceMorpher(Zone* zone, const Class& from, const Class& to);
virtual ~InstanceMorpher() {}
// Called on each instance that needs to be morphed.
RawInstance* Morph(const Instance& instance) const;
// Adds an object to be morphed.
void AddObject(RawObject* object) const;
// Create the morphed objects based on the before() list.
void CreateMorphedCopies() const;
// Dump the state of the morpher.
void Dump() const;
// Append the morper info to JSON array.
void AppendTo(JSONArray* array);
// Returns the list of objects that need to be morphed.
ZoneGrowableArray<const Instance*>* before() const { return before_; }
// Returns the list of morphed objects (matches order in before()).
ZoneGrowableArray<const Instance*>* after() const { return after_; }
// Returns the list of new fields.
ZoneGrowableArray<const Field*>* new_fields() const { return new_fields_; }
// Returns the cid associated with the from_ and to_ class.
intptr_t cid() const { return cid_; }
private:
const Class& from_;
const Class& to_;
ZoneGrowableArray<intptr_t> mapping_;
ZoneGrowableArray<const Instance*>* before_;
ZoneGrowableArray<const Instance*>* after_;
ZoneGrowableArray<const Field*>* new_fields_;
intptr_t cid_;
void ComputeMapping();
void DumpFormatFor(const Class& cls) const;
};
class ReasonForCancelling : public ZoneAllocated {
public:
explicit ReasonForCancelling(Zone* zone) {}
virtual ~ReasonForCancelling() {}
// Reports a reason for cancelling reload.
void Report(IsolateReloadContext* context);
// Conversion to a VM error object.
// Default implementation calls ToString.
virtual RawError* ToError();
// Conversion to a string object.
// Default implementation calls ToError.
virtual RawString* ToString();
// Append the reason to JSON array.
virtual void AppendTo(JSONArray* array);
// Concrete subclasses must override either ToError or ToString.
};
// Abstract class for also capturing the from_ and to_ class.
class ClassReasonForCancelling : public ReasonForCancelling {
public:
ClassReasonForCancelling(Zone* zone, const Class& from, const Class& to);
void AppendTo(JSONArray* array);
protected:
const Class& from_;
const Class& to_;
};
class IsolateReloadContext {
public:
explicit IsolateReloadContext(Isolate* isolate, JSONStream* js);
~IsolateReloadContext();
// If kernel_buffer is provided, the VM takes ownership when Reload is called.
void Reload(bool force_reload,
const char* root_script_url = NULL,
const char* packages_url = NULL,
const uint8_t* kernel_buffer = NULL,
intptr_t kernel_buffer_size = 0);
// All zone allocated objects must be allocated from this zone.
Zone* zone() const { return zone_; }
bool UseSavedClassTableForGC() const {
return saved_class_table_.load(std::memory_order_relaxed) != nullptr;
}
bool reload_skipped() const { return reload_skipped_; }
bool reload_aborted() const { return reload_aborted_; }
RawError* error() const;
int64_t reload_timestamp() const { return reload_timestamp_; }
static Dart_FileModifiedCallback file_modified_callback() {
return file_modified_callback_;
}
static void SetFileModifiedCallback(Dart_FileModifiedCallback callback) {
file_modified_callback_ = callback;
}
static bool IsSameField(const Field& a, const Field& b);
static bool IsSameLibrary(const Library& a_lib, const Library& b_lib);
static bool IsSameClass(const Class& a, const Class& b);
private:
RawLibrary* saved_root_library() const;
RawGrowableObjectArray* saved_libraries() const;
RawClass* FindOriginalClass(const Class& cls);
bool IsDirty(const Library& lib);
// Prefers old classes when we are in the middle of a reload.
RawClass* GetClassForHeapWalkAt(intptr_t cid);
intptr_t GetClassSizeForHeapWalkAt(intptr_t cid);
void DiscardSavedClassTable(bool is_rollback);
void RegisterClass(const Class& new_cls);
// Finds the library private key for |replacement_or_new| or return null
// if |replacement_or_new| is new.
RawString* FindLibraryPrivateKey(const Library& replacement_or_new);
int64_t start_time_micros() const { return start_time_micros_; }
// Tells whether there are reasons for cancelling the reload.
bool HasReasonsForCancelling() const {
return !reasons_to_cancel_reload_.is_empty();
}
// Record problem for this reload.
void AddReasonForCancelling(ReasonForCancelling* reason);
// Reports all reasons for cancelling reload.
void ReportReasonsForCancelling();
// Reports the details of a reload operation.
void ReportOnJSON(JSONStream* stream);
// Store morphing operation.
void AddInstanceMorpher(InstanceMorpher* morpher);
// Tells whether instance in the heap must be morphed.
bool HasInstanceMorphers() const { return !instance_morphers_.is_empty(); }
// NOTE: FinalizeLoading will be called *before* Reload() returns. This
// function will not be called if the embedder does not call
// Dart_FinalizeLoading.
void FinalizeLoading();
// NOTE: FinalizeFailedLoad will be called *before* Reload returns. This
// function will not be called if the embedder calls Dart_FinalizeLoading.
void FinalizeFailedLoad(const Error& error);
// Called by both FinalizeLoading and FinalizeFailedLoad.
void CommonFinalizeTail();
// Report back through the observatory channels.
void ReportError(const Error& error);
void ReportSuccess();
void set_saved_root_library(const Library& value);
void set_saved_libraries(const GrowableObjectArray& value);
void VisitObjectPointers(ObjectPointerVisitor* visitor);
Isolate* isolate() { return isolate_; }
ObjectStore* object_store();
void EnsuredUnoptimizedCodeForStack();
void DeoptimizeDependentCode();
void Checkpoint();
void CheckpointClasses();
bool ScriptModifiedSince(const Script& script, int64_t since);
void FindModifiedSources(Thread* thread,
bool force_reload,
Dart_SourceFile** modified_sources,
intptr_t* count,
const char* packages_url);
void CheckpointLibraries();
void MorphInstancesAndApplyNewClassTable();
bool ValidateReload();
void Rollback();
void RollbackClasses();
void RollbackLibraries();
#ifdef DEBUG
void VerifyMaps();
#endif
void Commit();
void PostCommit();
void ClearReplacedObjectBits();
// atomic_install:
void RunInvalidationVisitors();
void ResetUnoptimizedICsOnStack();
void ResetMegamorphicCaches();
void InvalidateWorld();
// The zone used for all reload related allocations.
Zone* zone_;
int64_t start_time_micros_;
int64_t reload_timestamp_;
Isolate* isolate_;
bool reload_skipped_;
bool reload_aborted_;
bool reload_finalized_;
JSONStream* js_;
intptr_t saved_num_cids_;
std::atomic<ClassAndSize*> saved_class_table_;
intptr_t num_saved_libs_;
intptr_t num_received_libs_;
intptr_t bytes_received_libs_;
intptr_t num_received_classes_;
intptr_t num_received_procedures_;
// Collect the necessary instance transformation for schema changes.
ZoneGrowableArray<InstanceMorpher*> instance_morphers_;
// Collects the reasons for cancelling the reload.
ZoneGrowableArray<ReasonForCancelling*> reasons_to_cancel_reload_;
// Required trait for the cid_mapper_;
struct MorpherTrait {
typedef InstanceMorpher* Value;
typedef intptr_t Key;
typedef InstanceMorpher* Pair;
static Key KeyOf(Pair kv) { return kv->cid(); }
static Value ValueOf(Pair kv) { return kv; }
static intptr_t Hashcode(Key key) { return key; }
static bool IsKeyEqual(Pair kv, Key key) { return kv->cid() == key; }
};
// Hash map from cid to InstanceMorpher.
DirectChainedHashMap<MorpherTrait> cid_mapper_;
struct LibraryInfo {
bool dirty;
};
MallocGrowableArray<LibraryInfo> library_infos_;
// A bit vector indicating which of the original libraries were modified.
BitVector* modified_libs_;
RawClass* OldClassOrNull(const Class& replacement_or_new);
RawLibrary* OldLibraryOrNull(const Library& replacement_or_new);
RawLibrary* OldLibraryOrNullBaseMoved(const Library& replacement_or_new);
void BuildLibraryMapping();
void BuildRemovedClassesSet();
void AddClassMapping(const Class& replacement_or_new, const Class& original);
void AddLibraryMapping(const Library& replacement_or_new,
const Library& original);
void AddStaticFieldMapping(const Field& old_field, const Field& new_field);
void AddBecomeMapping(const Object& old, const Object& neu);
void AddEnumBecomeMapping(const Object& old, const Object& neu);
void RebuildDirectSubclasses();
RawClass* MappedClass(const Class& replacement_or_new);
RawLibrary* MappedLibrary(const Library& replacement_or_new);
RawObject** from() { return reinterpret_cast<RawObject**>(&script_url_); }
RawString* script_url_;
RawError* error_;
RawArray* old_classes_set_storage_;
RawArray* class_map_storage_;
RawArray* removed_class_set_storage_;
RawArray* old_libraries_set_storage_;
RawArray* library_map_storage_;
RawArray* become_map_storage_;
RawGrowableObjectArray* become_enum_mappings_;
RawLibrary* saved_root_library_;
RawGrowableObjectArray* saved_libraries_;
RawString* root_url_prefix_;
RawString* old_root_url_prefix_;
RawObject** to() {
return reinterpret_cast<RawObject**>(&old_root_url_prefix_);
}
friend class Isolate;
friend class Class; // AddStaticFieldMapping, AddEnumBecomeMapping.
friend class Library;
friend class ObjectLocator;
friend class MarkFunctionsForRecompilation; // IsDirty.
friend class ReasonForCancelling;
static Dart_FileModifiedCallback file_modified_callback_;
};
class CallSiteResetter : public ValueObject {
public:
explicit CallSiteResetter(Zone* zone);
void ZeroEdgeCounters(const Function& function);
void ResetCaches(const Code& code);
void ResetCaches(const ObjectPool& pool);
void RebindStaticTargets(const Bytecode& code);
void Reset(const ICData& ic);
void ResetSwitchableCalls(const Code& code);
private:
Zone* zone_;
Instructions& instrs_;
ObjectPool& pool_;
Object& object_;
String& name_;
Class& new_cls_;
Library& new_lib_;
Function& new_function_;
Field& new_field_;
Array& entries_;
Function& old_target_;
Function& new_target_;
Function& caller_;
Array& args_desc_array_;
Array& ic_data_array_;
Array& edge_counters_;
PcDescriptors& descriptors_;
ICData& ic_data_;
};
} // namespace dart
#endif // !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
#endif // RUNTIME_VM_ISOLATE_RELOAD_H_