Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
kernel_binary.cc
Go to the documentation of this file.
1// Copyright (c) 2016, 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#if !defined(DART_PRECOMPILED_RUNTIME)
5
6#include "vm/kernel_binary.h"
7
8#include <memory>
9
10#include "platform/globals.h"
12#include "vm/dart_api_impl.h"
13#include "vm/flags.h"
14#include "vm/growable_array.h"
15#include "vm/kernel.h"
16#include "vm/object.h"
17#include "vm/os.h"
18#include "vm/version.h"
19
20namespace dart {
21
22namespace kernel {
23
24const char* Reader::TagName(Tag tag) {
25 switch (tag) {
26#define CASE(Name, value) \
27 case k##Name: \
28 return #Name;
30#undef CASE
31 default:
32 break;
33 }
34 return "Unknown";
35}
36
37TypedDataPtr Reader::ReadLineStartsData(intptr_t line_start_count) {
38 const intptr_t start_offset = offset();
39
40 // Choose representation between Uint16 and Uint32 typed data.
41 intptr_t max_start = 0;
42 for (intptr_t i = 0; i < line_start_count; ++i) {
43 const intptr_t delta = ReadUInt();
44 max_start += delta;
45 }
46
47 const intptr_t cid = (max_start <= kMaxUint16) ? kTypedDataUint16ArrayCid
48 : kTypedDataUint32ArrayCid;
49 const TypedData& line_starts_data =
51
52 set_offset(start_offset);
53 intptr_t current_start = 0;
54 for (intptr_t i = 0; i < line_start_count; ++i) {
55 const intptr_t delta = ReadUInt();
56 current_start += delta;
57 if (cid == kTypedDataUint16ArrayCid) {
58 line_starts_data.SetUint16(i << 1, static_cast<uint16_t>(current_start));
59 } else {
60 line_starts_data.SetUint32(i << 2, current_start);
61 }
62 }
63
64 return line_starts_data.ptr();
65}
66
68 "File size is too small to be a valid kernel file";
69const char* kKernelInvalidMagicIdentifier = "Invalid magic identifier";
71 "Invalid kernel binary format version";
73 "Invalid kernel binary: Indicated size is invalid";
74const char* kKernelInvalidSdkHash = "Invalid SDK hash";
75
76const int kSdkHashSizeInBytes = 10;
77const char* kSdkHashNull = "0000000000";
78
79bool IsValidSdkHash(const uint8_t* sdk_hash) {
81 memcmp(sdk_hash, kSdkHashNull, kSdkHashSizeInBytes) != 0 &&
82 memcmp(sdk_hash, Version::SdkHash(), kSdkHashSizeInBytes) != 0) {
83 return false;
84 }
85 return true;
86}
87
88std::unique_ptr<Program> Program::ReadFrom(Reader* reader, const char** error) {
89 if (reader->size() < 70) {
90 // A kernel file (v43) currently contains at least the following:
91 // * Magic number (32)
92 // * Kernel version (32)
93 // * SDK Hash (10 * 8)
94 // * List of problems (8)
95 // * Length of source map (32)
96 // * Length of canonical name table (8)
97 // * Metadata length (32)
98 // * Length of string table (8)
99 // * Length of constant table (8)
100 // * Component index (11 * 32)
101 //
102 // so is at least 74 bytes.
103 // (Technically it will also contain an empty entry in both source map and
104 // string table, taking up another 8 bytes.)
105 if (error != nullptr) {
107 }
108 return nullptr;
109 }
110
111 uint32_t magic = reader->ReadUInt32();
112 if (magic != kMagicProgramFile) {
113 if (error != nullptr) {
115 }
116 return nullptr;
117 }
118
119 const uint32_t format_version = reader->ReadUInt32();
120 if (format_version != kSupportedKernelFormatVersion) {
121 if (error != nullptr) {
123 }
124 return nullptr;
125 }
126
127 if (!IsValidSdkHash(reader->BufferAt(reader->offset()))) {
128 if (error != nullptr) {
130 }
131 return nullptr;
132 }
133 reader->set_offset(reader->offset() + kSdkHashSizeInBytes);
134
135 std::unique_ptr<Program> program(new Program(reader->typed_data()));
136
137 // Dill files can be concatenated (e.g. cat a.dill b.dill > c.dill). Find out
138 // if this dill contains more than one program.
139 int subprogram_count = 0;
140 reader->set_offset(reader->size() - 4);
141 while (reader->offset() > 0) {
142 intptr_t size = reader->ReadUInt32();
143 intptr_t start = reader->offset() - size;
144 if (start < 0 || size <= 0) {
145 if (error != nullptr) {
147 }
148 return nullptr;
149 }
150 ++subprogram_count;
151 if (subprogram_count > 1) break;
152 reader->set_offset(start - 4);
153 }
154 program->single_program_ = subprogram_count == 1;
155
156 // Read backwards at the end.
157 program->library_count_ = reader->ReadFromIndexNoReset(
158 reader->size_, LibraryCountFieldCountFromEnd, 1, 0);
159 intptr_t count_from_first_library_offset =
161 program->source_table_offset_ = reader->ReadFromIndexNoReset(
162 reader->size_,
163 LibraryCountFieldCountFromEnd + 1 + program->library_count_ + 1 +
164 count_from_first_library_offset,
165 1, 0);
166 program->constant_table_offset_ = reader->ReadUInt32();
167 reader->ReadUInt32(); // offset for constant table index.
168 program->name_table_offset_ = reader->ReadUInt32();
169 program->metadata_payloads_offset_ = reader->ReadUInt32();
170 program->metadata_mappings_offset_ = reader->ReadUInt32();
171 program->string_table_offset_ = reader->ReadUInt32();
172 // The below includes any 8-bit alignment; denotes the end of the previous
173 // block.
174 program->component_index_offset_ = reader->ReadUInt32();
175
176 program->main_method_reference_ = NameIndex(reader->ReadUInt32() - 1);
177 reader->ReadUInt32(); // Read and ignore NNBD compilation mode.
178
179 return program;
180}
181
182std::unique_ptr<Program> Program::ReadFromFile(
183 const char* script_uri,
184 const char** error /* = nullptr */) {
185 Thread* thread = Thread::Current();
186 auto isolate_group = thread->isolate_group();
187 if (script_uri == nullptr) {
188 return nullptr;
189 }
190 if (!isolate_group->HasTagHandler()) {
191 return nullptr;
192 }
193 std::unique_ptr<kernel::Program> kernel_program;
194
195 const String& uri = String::Handle(String::New(script_uri));
196 const Object& ret = Object::Handle(isolate_group->CallTagHandler(
197 Dart_kKernelTag, Object::null_object(), uri));
198 if (ret.IsExternalTypedData()) {
199 const auto& typed_data = ExternalTypedData::Cast(ret);
200 kernel_program = kernel::Program::ReadFromTypedData(typed_data);
201 return kernel_program;
202 } else if (error != nullptr) {
203 Api::Scope api_scope(thread);
204 Dart_Handle retval = Api::NewHandle(thread, ret.ptr());
205 {
206 TransitionVMToNative transition(thread);
207 *error = Dart_GetError(retval);
208 }
209 }
210 return kernel_program;
211}
212
213std::unique_ptr<Program> Program::ReadFromBuffer(const uint8_t* buffer,
214 intptr_t buffer_length,
215 const char** error) {
216 // Whoever called this method (e.g. embedder) has to ensure the buffer stays
217 // alive until the VM is done with the last usage (e.g. isolate shutdown).
219 kExternalTypedDataUint8ArrayCid, const_cast<uint8_t*>(buffer),
220 buffer_length, Heap::kNew));
221 kernel::Reader reader(binary);
222 return kernel::Program::ReadFrom(&reader, error);
223}
224
225std::unique_ptr<Program> Program::ReadFromTypedData(
226 const ExternalTypedData& typed_data,
227 const char** error) {
228 kernel::Reader reader(typed_data);
229 return kernel::Program::ReadFrom(&reader, error);
230}
231
232} // namespace kernel
233} // namespace dart
234#endif // !defined(DART_PRECOMPILED_RUNTIME)
static Dart_Handle NewHandle(Thread *thread, ObjectPtr raw)
static ExternalTypedDataPtr New(intptr_t class_id, uint8_t *data, intptr_t len, Heap::Space space=Heap::kNew, bool perform_eager_msan_initialization_check=true)
Definition object.cc:25705
@ kNew
Definition heap.h:38
@ kOld
Definition heap.h:39
ObjectPtr ptr() const
Definition object.h:332
static Object & Handle()
Definition object.h:407
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition object.cc:23777
static Thread * Current()
Definition thread.h:361
static TypedDataPtr New(intptr_t class_id, intptr_t len, Heap::Space space=Heap::kNew)
Definition object.cc:25666
static const char * SdkHash()
Definition version_in.cc:23
intptr_t offset() const
void set_offset(intptr_t offset)
TypedDataPtr ReadLineStartsData(intptr_t line_start_count)
static const char * TagName(Tag tag)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
@ Dart_kKernelTag
Definition dart_api.h:3342
static const uint8_t buffer[]
const uint8_t uint32_t uint32_t GError ** error
#define CASE(Arity, Mask, Name, Args, Result)
#define KERNEL_TAG_LIST(V)
bool IsValidSdkHash(const uint8_t *sdk_hash)
const char * kKernelInvalidMagicIdentifier
const char * kKernelInvalidSdkHash
const int kSdkHashSizeInBytes
const char * kKernelInvalidSizeIndicated
const char * kKernelInvalidBinaryFormatVersion
const char * kKernelInvalidFilesize
static const uint32_t kMagicProgramFile
static constexpr int SourceTableFieldCountFromFirstLibraryOffset
static constexpr int LibraryCountFieldCountFromEnd
static const uint32_t kSupportedKernelFormatVersion
const char * kSdkHashNull
DART_EXPORT const char * Dart_GetError(Dart_Handle handle)
const intptr_t cid
constexpr uint16_t kMaxUint16
Definition globals.h:481
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259