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

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

Issue 10829444: Avoid trusting the length encoded in the Snapshot if there is an (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 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 | Annotate | Revision Log
« runtime/vm/isolate.cc ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | 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) 2012, the Dart project authors. Please see the AUTHORS file 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 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 "include/dart_debugger_api.h" 5 #include "include/dart_debugger_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_message.h" 10 #include "vm/dart_api_message.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 } 46 }
47 47
48 48
49 static uint8_t* zone_allocator( 49 static uint8_t* zone_allocator(
50 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 50 uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
51 Zone* zone = Isolate::Current()->current_zone(); 51 Zone* zone = Isolate::Current()->current_zone();
52 return zone->Realloc<uint8_t>(ptr, old_size, new_size); 52 return zone->Realloc<uint8_t>(ptr, old_size, new_size);
53 } 53 }
54 54
55 55
56 static Dart_CObject* DecodeMessage(uint8_t* message,
57 intptr_t length,
58 ReAlloc allocator) {
59 ApiMessageReader message_reader(message, length, allocator);
60 return message_reader.ReadMessage();
61 }
62
63
64 // Compare two Dart_CObject object graphs rooted in first and 56 // Compare two Dart_CObject object graphs rooted in first and
65 // second. The second graph will be destroyed by this operation no matter 57 // second. The second graph will be destroyed by this operation no matter
66 // whether the graphs are equal or not. 58 // whether the graphs are equal or not.
67 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { 59 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) {
68 // Return immediately if entering a cycle. 60 // Return immediately if entering a cycle.
69 if (second->type == Dart_CObject::kNumberOfTypes) return; 61 if (second->type == Dart_CObject::kNumberOfTypes) return;
70 62
71 EXPECT_NE(first, second); 63 EXPECT_NE(first, second);
72 EXPECT_EQ(first->type, second->type); 64 EXPECT_EQ(first->type, second->type);
73 switch (first->type) { 65 switch (first->type) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 default: 105 default:
114 EXPECT(false); 106 EXPECT(false);
115 } 107 }
116 } 108 }
117 109
118 110
119 static void CheckEncodeDecodeMessage(Dart_CObject* root) { 111 static void CheckEncodeDecodeMessage(Dart_CObject* root) {
120 // Encode and decode the message. 112 // Encode and decode the message.
121 uint8_t* buffer = NULL; 113 uint8_t* buffer = NULL;
122 ApiMessageWriter writer(&buffer, &malloc_allocator); 114 ApiMessageWriter writer(&buffer, &malloc_allocator);
123 writer.WriteCMessage(root); 115 intptr_t buffer_len = writer.WriteCMessage(root);
124 116
125 Dart_CObject* new_root = DecodeMessage(buffer + Snapshot::kHeaderSize, 117 // Create a snapshot object using the buffer.
126 writer.BytesWritten(), 118 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
127 &zone_allocator); 119 EXPECT_NOTNULL(snapshot);
120
121 ApiMessageReader api_reader(snapshot, &zone_allocator);
122 Dart_CObject* new_root = api_reader.ReadMessage();
128 123
129 // Check that the two messages are the same. 124 // Check that the two messages are the same.
130 CompareDartCObjects(root, new_root); 125 CompareDartCObjects(root, new_root);
131 } 126 }
132 127
133 TEST_CASE(SerializeNull) { 128 TEST_CASE(SerializeNull) {
134 Zone zone(Isolate::Current()); 129 Zone zone(Isolate::Current());
135 130
136 // Write snapshot with object content. 131 // Write snapshot with object content.
137 uint8_t* buffer; 132 uint8_t* buffer;
138 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 133 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
139 const Object& null_object = Object::Handle(); 134 const Object& null_object = Object::Handle();
140 writer.WriteObject(null_object.raw()); 135 writer.WriteObject(null_object.raw());
141 writer.FinalizeBuffer(); 136 intptr_t buffer_len = writer.FinalizeBuffer();
142 137
143 // Create a snapshot object using the buffer. 138 // Create a snapshot object using the buffer.
144 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 139 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
140 EXPECT_NOTNULL(snapshot);
145 141
146 // Read object back from the snapshot. 142 // Read object back from the snapshot.
147 SnapshotReader reader(snapshot, Isolate::Current()); 143 SnapshotReader reader(snapshot, Isolate::Current());
148 const Object& serialized_object = Object::Handle(reader.ReadObject()); 144 const Object& serialized_object = Object::Handle(reader.ReadObject());
149 EXPECT(Equals(null_object, serialized_object)); 145 EXPECT(Equals(null_object, serialized_object));
150 146
151 // Read object back from the snapshot into a C structure. 147 // Read object back from the snapshot into a C structure.
152 ApiNativeScope scope; 148 ApiNativeScope scope;
153 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 149 ApiMessageReader api_reader(snapshot, &zone_allocator);
154 writer.BytesWritten(), 150 Dart_CObject* root = api_reader.ReadMessage();
155 &zone_allocator);
156 EXPECT_NOTNULL(root); 151 EXPECT_NOTNULL(root);
157 EXPECT_EQ(Dart_CObject::kNull, root->type); 152 EXPECT_EQ(Dart_CObject::kNull, root->type);
158 CheckEncodeDecodeMessage(root); 153 CheckEncodeDecodeMessage(root);
159 } 154 }
160 155
161 156
162 TEST_CASE(SerializeSmi1) { 157 TEST_CASE(SerializeSmi1) {
163 Zone zone(Isolate::Current()); 158 Zone zone(Isolate::Current());
164 159
165 // Write snapshot with object content. 160 // Write snapshot with object content.
166 uint8_t* buffer; 161 uint8_t* buffer;
167 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 162 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
168 const Smi& smi = Smi::Handle(Smi::New(124)); 163 const Smi& smi = Smi::Handle(Smi::New(124));
169 writer.WriteObject(smi.raw()); 164 writer.WriteObject(smi.raw());
170 writer.FinalizeBuffer(); 165 intptr_t buffer_len = writer.FinalizeBuffer();
171 166
172 // Create a snapshot object using the buffer. 167 // Create a snapshot object using the buffer.
173 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 168 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
169 EXPECT_NOTNULL(snapshot);
174 170
175 // Read object back from the snapshot. 171 // Read object back from the snapshot.
176 SnapshotReader reader(snapshot, Isolate::Current()); 172 SnapshotReader reader(snapshot, Isolate::Current());
177 const Object& serialized_object = Object::Handle(reader.ReadObject()); 173 const Object& serialized_object = Object::Handle(reader.ReadObject());
178 EXPECT(Equals(smi, serialized_object)); 174 EXPECT(Equals(smi, serialized_object));
179 175
180 // Read object back from the snapshot into a C structure. 176 // Read object back from the snapshot into a C structure.
181 ApiNativeScope scope; 177 ApiNativeScope scope;
182 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 178 ApiMessageReader api_reader(snapshot, &zone_allocator);
183 writer.BytesWritten(), 179 Dart_CObject* root = api_reader.ReadMessage();
184 &zone_allocator);
185 EXPECT_NOTNULL(root); 180 EXPECT_NOTNULL(root);
186 EXPECT_EQ(Dart_CObject::kInt32, root->type); 181 EXPECT_EQ(Dart_CObject::kInt32, root->type);
187 EXPECT_EQ(smi.Value(), root->value.as_int32); 182 EXPECT_EQ(smi.Value(), root->value.as_int32);
188 CheckEncodeDecodeMessage(root); 183 CheckEncodeDecodeMessage(root);
189 } 184 }
190 185
191 186
192 TEST_CASE(SerializeSmi2) { 187 TEST_CASE(SerializeSmi2) {
193 Zone zone(Isolate::Current()); 188 Zone zone(Isolate::Current());
194 189
195 // Write snapshot with object content. 190 // Write snapshot with object content.
196 uint8_t* buffer; 191 uint8_t* buffer;
197 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 192 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
198 const Smi& smi = Smi::Handle(Smi::New(-1)); 193 const Smi& smi = Smi::Handle(Smi::New(-1));
199 writer.WriteObject(smi.raw()); 194 writer.WriteObject(smi.raw());
200 writer.FinalizeBuffer(); 195 intptr_t buffer_len = writer.FinalizeBuffer();
201 196
202 // Create a snapshot object using the buffer. 197 // Create a snapshot object using the buffer.
203 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 198 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
199 EXPECT_NOTNULL(snapshot);
204 200
205 // Read object back from the snapshot. 201 // Read object back from the snapshot.
206 SnapshotReader reader(snapshot, Isolate::Current()); 202 SnapshotReader reader(snapshot, Isolate::Current());
207 const Object& serialized_object = Object::Handle(reader.ReadObject()); 203 const Object& serialized_object = Object::Handle(reader.ReadObject());
208 EXPECT(Equals(smi, serialized_object)); 204 EXPECT(Equals(smi, serialized_object));
209 205
210 // Read object back from the snapshot into a C structure. 206 // Read object back from the snapshot into a C structure.
211 ApiNativeScope scope; 207 ApiNativeScope scope;
212 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 208 ApiMessageReader api_reader(snapshot, &zone_allocator);
213 writer.BytesWritten(), 209 Dart_CObject* root = api_reader.ReadMessage();
214 &zone_allocator);
215 EXPECT_NOTNULL(root); 210 EXPECT_NOTNULL(root);
216 EXPECT_EQ(Dart_CObject::kInt32, root->type); 211 EXPECT_EQ(Dart_CObject::kInt32, root->type);
217 EXPECT_EQ(smi.Value(), root->value.as_int32); 212 EXPECT_EQ(smi.Value(), root->value.as_int32);
218 CheckEncodeDecodeMessage(root); 213 CheckEncodeDecodeMessage(root);
219 } 214 }
220 215
221 216
222 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) { 217 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) {
223 // Write snapshot with object content. 218 // Write snapshot with object content.
224 uint8_t* buffer; 219 uint8_t* buffer;
225 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 220 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
226 writer.WriteObject(mint.raw()); 221 writer.WriteObject(mint.raw());
227 writer.FinalizeBuffer(); 222 intptr_t buffer_len = writer.FinalizeBuffer();
228 223
229 // Create a snapshot object using the buffer. 224 // Create a snapshot object using the buffer.
230 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 225 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
226 EXPECT_NOTNULL(snapshot);
231 227
232 // Read object back from the snapshot. 228 // Read object back from the snapshot.
233 SnapshotReader reader(snapshot, Isolate::Current()); 229 SnapshotReader reader(snapshot, Isolate::Current());
234 const Object& serialized_object = Object::Handle(reader.ReadObject()); 230 const Object& serialized_object = Object::Handle(reader.ReadObject());
235 EXPECT(serialized_object.IsMint()); 231 EXPECT(serialized_object.IsMint());
236 232
237 // Read object back from the snapshot into a C structure. 233 // Read object back from the snapshot into a C structure.
238 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 234 ApiMessageReader api_reader(snapshot, &zone_allocator);
239 writer.BytesWritten(), 235 Dart_CObject* root = api_reader.ReadMessage();
240 &zone_allocator);
241 EXPECT_NOTNULL(root); 236 EXPECT_NOTNULL(root);
242 CheckEncodeDecodeMessage(root); 237 CheckEncodeDecodeMessage(root);
243 return root; 238 return root;
244 } 239 }
245 240
246 241
247 void CheckMint(int64_t value) { 242 void CheckMint(int64_t value) {
248 Zone zone(Isolate::Current()); 243 Zone zone(Isolate::Current());
249 244
250 const Mint& mint = Mint::Handle(Mint::New(value)); 245 const Mint& mint = Mint::Handle(Mint::New(value));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 285
291 286
292 TEST_CASE(SerializeDouble) { 287 TEST_CASE(SerializeDouble) {
293 Zone zone(Isolate::Current()); 288 Zone zone(Isolate::Current());
294 289
295 // Write snapshot with object content. 290 // Write snapshot with object content.
296 uint8_t* buffer; 291 uint8_t* buffer;
297 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 292 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
298 const Double& dbl = Double::Handle(Double::New(101.29)); 293 const Double& dbl = Double::Handle(Double::New(101.29));
299 writer.WriteObject(dbl.raw()); 294 writer.WriteObject(dbl.raw());
300 writer.FinalizeBuffer(); 295 intptr_t buffer_len = writer.FinalizeBuffer();
301 296
302 // Create a snapshot object using the buffer. 297 // Create a snapshot object using the buffer.
303 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 298 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
299 EXPECT_NOTNULL(snapshot);
304 300
305 // Read object back from the snapshot. 301 // Read object back from the snapshot.
306 SnapshotReader reader(snapshot, Isolate::Current()); 302 SnapshotReader reader(snapshot, Isolate::Current());
307 const Object& serialized_object = Object::Handle(reader.ReadObject()); 303 const Object& serialized_object = Object::Handle(reader.ReadObject());
308 EXPECT(Equals(dbl, serialized_object)); 304 EXPECT(Equals(dbl, serialized_object));
309 305
310 // Read object back from the snapshot into a C structure. 306 // Read object back from the snapshot into a C structure.
311 ApiNativeScope scope; 307 ApiNativeScope scope;
312 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 308 ApiMessageReader api_reader(snapshot, &zone_allocator);
313 writer.BytesWritten(), 309 Dart_CObject* root = api_reader.ReadMessage();
314 &zone_allocator);
315 EXPECT_NOTNULL(root); 310 EXPECT_NOTNULL(root);
316 EXPECT_EQ(Dart_CObject::kDouble, root->type); 311 EXPECT_EQ(Dart_CObject::kDouble, root->type);
317 EXPECT_EQ(dbl.value(), root->value.as_double); 312 EXPECT_EQ(dbl.value(), root->value.as_double);
318 CheckEncodeDecodeMessage(root); 313 CheckEncodeDecodeMessage(root);
319 } 314 }
320 315
321 316
322 TEST_CASE(SerializeBool) { 317 TEST_CASE(SerializeBool) {
323 Zone zone(Isolate::Current()); 318 Zone zone(Isolate::Current());
324 319
325 // Write snapshot with object content. 320 // Write snapshot with object content.
326 uint8_t* buffer; 321 uint8_t* buffer;
327 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 322 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
328 const Bool& bool1 = Bool::Handle(Bool::True()); 323 const Bool& bool1 = Bool::Handle(Bool::True());
329 const Bool& bool2 = Bool::Handle(Bool::False()); 324 const Bool& bool2 = Bool::Handle(Bool::False());
330 writer.WriteObject(bool1.raw()); 325 writer.WriteObject(bool1.raw());
331 writer.WriteObject(bool2.raw()); 326 writer.WriteObject(bool2.raw());
332 writer.FinalizeBuffer(); 327 intptr_t buffer_len = writer.FinalizeBuffer();
333 328
334 // Create a snapshot object using the buffer. 329 // Create a snapshot object using the buffer.
335 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 330 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
331 EXPECT_NOTNULL(snapshot);
336 332
337 // Read object back from the snapshot. 333 // Read object back from the snapshot.
338 SnapshotReader reader(snapshot, Isolate::Current()); 334 SnapshotReader reader(snapshot, Isolate::Current());
339 EXPECT(Bool::True() == reader.ReadObject()); 335 EXPECT(Bool::True() == reader.ReadObject());
340 EXPECT(Bool::False() == reader.ReadObject()); 336 EXPECT(Bool::False() == reader.ReadObject());
341 } 337 }
342 338
343 339
344 TEST_CASE(SerializeTrue) { 340 TEST_CASE(SerializeTrue) {
345 Zone zone(Isolate::Current()); 341 Zone zone(Isolate::Current());
346 342
347 // Write snapshot with true object. 343 // Write snapshot with true object.
348 uint8_t* buffer; 344 uint8_t* buffer;
349 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 345 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
350 const Bool& bl = Bool::Handle(Bool::True()); 346 const Bool& bl = Bool::Handle(Bool::True());
351 writer.WriteObject(bl.raw()); 347 writer.WriteObject(bl.raw());
352 writer.FinalizeBuffer(); 348 intptr_t buffer_len = writer.FinalizeBuffer();
353 349
354 // Create a snapshot object using the buffer. 350 // Create a snapshot object using the buffer.
355 Snapshot::SetupFromBuffer(buffer); 351 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
352 EXPECT_NOTNULL(snapshot);
356 353
357 // Read object back from the snapshot into a C structure. 354 // Read object back from the snapshot into a C structure.
358 ApiNativeScope scope; 355 ApiNativeScope scope;
359 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 356 ApiMessageReader api_reader(snapshot, &zone_allocator);
360 writer.BytesWritten(), 357 Dart_CObject* root = api_reader.ReadMessage();
361 &zone_allocator);
362 EXPECT_NOTNULL(root); 358 EXPECT_NOTNULL(root);
363 EXPECT_EQ(Dart_CObject::kBool, root->type); 359 EXPECT_EQ(Dart_CObject::kBool, root->type);
364 EXPECT_EQ(true, root->value.as_bool); 360 EXPECT_EQ(true, root->value.as_bool);
365 CheckEncodeDecodeMessage(root); 361 CheckEncodeDecodeMessage(root);
366 } 362 }
367 363
368 364
369 TEST_CASE(SerializeFalse) { 365 TEST_CASE(SerializeFalse) {
370 Zone zone(Isolate::Current()); 366 Zone zone(Isolate::Current());
371 367
372 // Write snapshot with false object. 368 // Write snapshot with false object.
373 uint8_t* buffer; 369 uint8_t* buffer;
374 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 370 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
375 const Bool& bl = Bool::Handle(Bool::False()); 371 const Bool& bl = Bool::Handle(Bool::False());
376 writer.WriteObject(bl.raw()); 372 writer.WriteObject(bl.raw());
377 writer.FinalizeBuffer(); 373 intptr_t buffer_len = writer.FinalizeBuffer();
378 374
379 // Create a snapshot object using the buffer. 375 // Create a snapshot object using the buffer.
380 Snapshot::SetupFromBuffer(buffer); 376 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
377 EXPECT_NOTNULL(snapshot);
381 378
382 // Read object back from the snapshot into a C structure. 379 // Read object back from the snapshot into a C structure.
383 ApiNativeScope scope; 380 ApiNativeScope scope;
384 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 381 ApiMessageReader api_reader(snapshot, &zone_allocator);
385 writer.BytesWritten(), 382 Dart_CObject* root = api_reader.ReadMessage();
386 &zone_allocator);
387 EXPECT_NOTNULL(root); 383 EXPECT_NOTNULL(root);
388 EXPECT_EQ(Dart_CObject::kBool, root->type); 384 EXPECT_EQ(Dart_CObject::kBool, root->type);
389 EXPECT_EQ(false, root->value.as_bool); 385 EXPECT_EQ(false, root->value.as_bool);
390 CheckEncodeDecodeMessage(root); 386 CheckEncodeDecodeMessage(root);
391 } 387 }
392 388
393 389
394 static uword allocator(intptr_t size) { 390 static uword allocator(intptr_t size) {
395 return reinterpret_cast<uword>(malloc(size)); 391 return reinterpret_cast<uword>(malloc(size));
396 } 392 }
397 393
398 394
399 TEST_CASE(SerializeBigint) { 395 TEST_CASE(SerializeBigint) {
400 Zone zone(Isolate::Current()); 396 Zone zone(Isolate::Current());
401 397
402 // Write snapshot with object content. 398 // Write snapshot with object content.
403 uint8_t* buffer; 399 uint8_t* buffer;
404 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 400 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
405 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); 401 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff)));
406 writer.WriteObject(bigint.raw()); 402 writer.WriteObject(bigint.raw());
407 writer.FinalizeBuffer(); 403 intptr_t buffer_len = writer.FinalizeBuffer();
408 404
409 // Create a snapshot object using the buffer. 405 // Create a snapshot object using the buffer.
410 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 406 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
407 EXPECT_NOTNULL(snapshot);
411 408
412 // Read object back from the snapshot. 409 // Read object back from the snapshot.
413 SnapshotReader reader(snapshot, Isolate::Current()); 410 SnapshotReader reader(snapshot, Isolate::Current());
414 Bigint& obj = Bigint::Handle(); 411 Bigint& obj = Bigint::Handle();
415 obj ^= reader.ReadObject(); 412 obj ^= reader.ReadObject();
416 413
417 EXPECT_EQ(BigintOperations::ToMint(bigint), BigintOperations::ToMint(obj)); 414 EXPECT_EQ(BigintOperations::ToMint(bigint), BigintOperations::ToMint(obj));
418 415
419 // Read object back from the snapshot into a C structure. 416 // Read object back from the snapshot into a C structure.
420 ApiNativeScope scope; 417 ApiNativeScope scope;
421 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 418 ApiMessageReader api_reader(snapshot, &zone_allocator);
422 writer.BytesWritten(), 419 Dart_CObject* root = api_reader.ReadMessage();
423 &zone_allocator);
424 // Bigint not supported. 420 // Bigint not supported.
425 EXPECT_NOTNULL(root); 421 EXPECT_NOTNULL(root);
426 EXPECT_EQ(Dart_CObject::kBigint, root->type); 422 EXPECT_EQ(Dart_CObject::kBigint, root->type);
427 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint); 423 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint);
428 CheckEncodeDecodeMessage(root); 424 CheckEncodeDecodeMessage(root);
429 } 425 }
430 426
431 427
432 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { 428 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) {
433 // Write snapshot with object content. 429 // Write snapshot with object content.
434 uint8_t* buffer; 430 uint8_t* buffer;
435 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 431 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
436 writer.WriteObject(bigint.raw()); 432 writer.WriteObject(bigint.raw());
437 writer.FinalizeBuffer(); 433 intptr_t buffer_len = writer.FinalizeBuffer();
438 434
439 // Create a snapshot object using the buffer. 435 // Create a snapshot object using the buffer.
440 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 436 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
437 EXPECT_NOTNULL(snapshot);
441 438
442 // Read object back from the snapshot. 439 // Read object back from the snapshot.
443 SnapshotReader reader(snapshot, Isolate::Current()); 440 SnapshotReader reader(snapshot, Isolate::Current());
444 Bigint& serialized_bigint = Bigint::Handle(); 441 Bigint& serialized_bigint = Bigint::Handle();
445 serialized_bigint ^= reader.ReadObject(); 442 serialized_bigint ^= reader.ReadObject();
446 const char *str1 = BigintOperations::ToHexCString(bigint, allocator); 443 const char *str1 = BigintOperations::ToHexCString(bigint, allocator);
447 const char *str2 = 444 const char *str2 =
448 BigintOperations::ToHexCString(serialized_bigint, allocator); 445 BigintOperations::ToHexCString(serialized_bigint, allocator);
449 EXPECT_STREQ(str1, str2); 446 EXPECT_STREQ(str1, str2);
450 free(const_cast<char*>(str1)); 447 free(const_cast<char*>(str1));
451 free(const_cast<char*>(str2)); 448 free(const_cast<char*>(str2));
452 449
453 // Read object back from the snapshot into a C structure. 450 // Read object back from the snapshot into a C structure.
454 ApiNativeScope scope; 451 ApiNativeScope scope;
455 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 452 ApiMessageReader api_reader(snapshot, &zone_allocator);
456 writer.BytesWritten(), 453 Dart_CObject* root = api_reader.ReadMessage();
457 &zone_allocator);
458 // Bigint not supported. 454 // Bigint not supported.
459 EXPECT_NOTNULL(root); 455 EXPECT_NOTNULL(root);
460 CheckEncodeDecodeMessage(root); 456 CheckEncodeDecodeMessage(root);
461 return root; 457 return root;
462 } 458 }
463 459
464 460
465 void CheckBigint(const char* bigint_value) { 461 void CheckBigint(const char* bigint_value) {
466 Zone zone(Isolate::Current()); 462 Zone zone(Isolate::Current());
467 463
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 writer.WriteObject(Object::field_class()); 500 writer.WriteObject(Object::field_class());
505 writer.WriteObject(Object::token_stream_class()); 501 writer.WriteObject(Object::token_stream_class());
506 writer.WriteObject(Object::script_class()); 502 writer.WriteObject(Object::script_class());
507 writer.WriteObject(Object::library_class()); 503 writer.WriteObject(Object::library_class());
508 writer.WriteObject(Object::code_class()); 504 writer.WriteObject(Object::code_class());
509 writer.WriteObject(Object::instructions_class()); 505 writer.WriteObject(Object::instructions_class());
510 writer.WriteObject(Object::pc_descriptors_class()); 506 writer.WriteObject(Object::pc_descriptors_class());
511 writer.WriteObject(Object::exception_handlers_class()); 507 writer.WriteObject(Object::exception_handlers_class());
512 writer.WriteObject(Object::context_class()); 508 writer.WriteObject(Object::context_class());
513 writer.WriteObject(Object::context_scope_class()); 509 writer.WriteObject(Object::context_scope_class());
514 writer.FinalizeBuffer(); 510 intptr_t buffer_len = writer.FinalizeBuffer();
515 511
516 // Create a snapshot object using the buffer. 512 // Create a snapshot object using the buffer.
517 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 513 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
514 EXPECT_NOTNULL(snapshot);
518 515
519 // Read object back from the snapshot. 516 // Read object back from the snapshot.
520 SnapshotReader reader(snapshot, Isolate::Current()); 517 SnapshotReader reader(snapshot, Isolate::Current());
521 EXPECT(Object::class_class() == reader.ReadObject()); 518 EXPECT(Object::class_class() == reader.ReadObject());
522 EXPECT(Object::null_class() == reader.ReadObject()); 519 EXPECT(Object::null_class() == reader.ReadObject());
523 EXPECT(Object::type_class() == reader.ReadObject()); 520 EXPECT(Object::type_class() == reader.ReadObject());
524 EXPECT(Object::type_parameter_class() == reader.ReadObject()); 521 EXPECT(Object::type_parameter_class() == reader.ReadObject());
525 EXPECT(Object::type_arguments_class() == reader.ReadObject()); 522 EXPECT(Object::type_arguments_class() == reader.ReadObject());
526 EXPECT(Object::instantiated_type_arguments_class() == reader.ReadObject()); 523 EXPECT(Object::instantiated_type_arguments_class() == reader.ReadObject());
527 EXPECT(Object::function_class() == reader.ReadObject()); 524 EXPECT(Object::function_class() == reader.ReadObject());
(...skipping 14 matching lines...) Expand all
542 539
543 TEST_CASE(SerializeString) { 540 TEST_CASE(SerializeString) {
544 Zone zone(Isolate::Current()); 541 Zone zone(Isolate::Current());
545 542
546 // Write snapshot with object content. 543 // Write snapshot with object content.
547 uint8_t* buffer; 544 uint8_t* buffer;
548 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 545 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
549 static const char* cstr = "This string shall be serialized"; 546 static const char* cstr = "This string shall be serialized";
550 String& str = String::Handle(String::New(cstr)); 547 String& str = String::Handle(String::New(cstr));
551 writer.WriteObject(str.raw()); 548 writer.WriteObject(str.raw());
552 writer.FinalizeBuffer(); 549 intptr_t buffer_len = writer.FinalizeBuffer();
553 550
554 // Create a snapshot object using the buffer. 551 // Create a snapshot object using the buffer.
555 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 552 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
553 EXPECT_NOTNULL(snapshot);
556 554
557 // Read object back from the snapshot. 555 // Read object back from the snapshot.
558 SnapshotReader reader(snapshot, Isolate::Current()); 556 SnapshotReader reader(snapshot, Isolate::Current());
559 String& serialized_str = String::Handle(); 557 String& serialized_str = String::Handle();
560 serialized_str ^= reader.ReadObject(); 558 serialized_str ^= reader.ReadObject();
561 EXPECT(str.Equals(serialized_str)); 559 EXPECT(str.Equals(serialized_str));
562 560
563 // Read object back from the snapshot into a C structure. 561 // Read object back from the snapshot into a C structure.
564 ApiNativeScope scope; 562 ApiNativeScope scope;
565 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 563 ApiMessageReader api_reader(snapshot, &zone_allocator);
566 writer.BytesWritten(), 564 Dart_CObject* root = api_reader.ReadMessage();
567 &zone_allocator);
568 EXPECT_EQ(Dart_CObject::kString, root->type); 565 EXPECT_EQ(Dart_CObject::kString, root->type);
569 EXPECT_STREQ(cstr, root->value.as_string); 566 EXPECT_STREQ(cstr, root->value.as_string);
570 CheckEncodeDecodeMessage(root); 567 CheckEncodeDecodeMessage(root);
571 } 568 }
572 569
573 570
574 TEST_CASE(SerializeArray) { 571 TEST_CASE(SerializeArray) {
575 Zone zone(Isolate::Current()); 572 Zone zone(Isolate::Current());
576 573
577 // Write snapshot with object content. 574 // Write snapshot with object content.
578 uint8_t* buffer; 575 uint8_t* buffer;
579 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 576 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
580 const int kArrayLength = 10; 577 const int kArrayLength = 10;
581 Array& array = Array::Handle(Array::New(kArrayLength)); 578 Array& array = Array::Handle(Array::New(kArrayLength));
582 Smi& smi = Smi::Handle(); 579 Smi& smi = Smi::Handle();
583 for (int i = 0; i < kArrayLength; i++) { 580 for (int i = 0; i < kArrayLength; i++) {
584 smi ^= Smi::New(i); 581 smi ^= Smi::New(i);
585 array.SetAt(i, smi); 582 array.SetAt(i, smi);
586 } 583 }
587 writer.WriteObject(array.raw()); 584 writer.WriteObject(array.raw());
588 writer.FinalizeBuffer(); 585 intptr_t buffer_len = writer.FinalizeBuffer();
589 586
590 // Create a snapshot object using the buffer. 587 // Create a snapshot object using the buffer.
591 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 588 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
589 EXPECT_NOTNULL(snapshot);
592 590
593 // Read object back from the snapshot. 591 // Read object back from the snapshot.
594 SnapshotReader reader(snapshot, Isolate::Current()); 592 SnapshotReader reader(snapshot, Isolate::Current());
595 Array& serialized_array = Array::Handle(); 593 Array& serialized_array = Array::Handle();
596 serialized_array ^= reader.ReadObject(); 594 serialized_array ^= reader.ReadObject();
597 EXPECT(array.Equals(serialized_array)); 595 EXPECT(array.Equals(serialized_array));
598 596
599 // Read object back from the snapshot into a C structure. 597 // Read object back from the snapshot into a C structure.
600 ApiNativeScope scope; 598 ApiNativeScope scope;
601 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 599 ApiMessageReader api_reader(snapshot, &zone_allocator);
602 writer.BytesWritten(), 600 Dart_CObject* root = api_reader.ReadMessage();
603 &zone_allocator);
604 EXPECT_EQ(Dart_CObject::kArray, root->type); 601 EXPECT_EQ(Dart_CObject::kArray, root->type);
605 EXPECT_EQ(kArrayLength, root->value.as_array.length); 602 EXPECT_EQ(kArrayLength, root->value.as_array.length);
606 for (int i = 0; i < kArrayLength; i++) { 603 for (int i = 0; i < kArrayLength; i++) {
607 Dart_CObject* element = root->value.as_array.values[i]; 604 Dart_CObject* element = root->value.as_array.values[i];
608 EXPECT_EQ(Dart_CObject::kInt32, element->type); 605 EXPECT_EQ(Dart_CObject::kInt32, element->type);
609 EXPECT_EQ(i, element->value.as_int32); 606 EXPECT_EQ(i, element->value.as_int32);
610 } 607 }
611 CheckEncodeDecodeMessage(root); 608 CheckEncodeDecodeMessage(root);
612 } 609 }
613 610
614 611
615 TEST_CASE(SerializeEmptyArray) { 612 TEST_CASE(SerializeEmptyArray) {
616 Zone zone(Isolate::Current()); 613 Zone zone(Isolate::Current());
617 614
618 // Write snapshot with object content. 615 // Write snapshot with object content.
619 uint8_t* buffer; 616 uint8_t* buffer;
620 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 617 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
621 const int kArrayLength = 0; 618 const int kArrayLength = 0;
622 Array& array = Array::Handle(Array::New(kArrayLength)); 619 Array& array = Array::Handle(Array::New(kArrayLength));
623 writer.WriteObject(array.raw()); 620 writer.WriteObject(array.raw());
624 writer.FinalizeBuffer(); 621 intptr_t buffer_len = writer.FinalizeBuffer();
625 622
626 // Create a snapshot object using the buffer. 623 // Create a snapshot object using the buffer.
627 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 624 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
625 EXPECT_NOTNULL(snapshot);
628 626
629 // Read object back from the snapshot. 627 // Read object back from the snapshot.
630 SnapshotReader reader(snapshot, Isolate::Current()); 628 SnapshotReader reader(snapshot, Isolate::Current());
631 Array& serialized_array = Array::Handle(); 629 Array& serialized_array = Array::Handle();
632 serialized_array ^= reader.ReadObject(); 630 serialized_array ^= reader.ReadObject();
633 EXPECT(array.Equals(serialized_array)); 631 EXPECT(array.Equals(serialized_array));
634 632
635 // Read object back from the snapshot into a C structure. 633 // Read object back from the snapshot into a C structure.
636 ApiNativeScope scope; 634 ApiNativeScope scope;
637 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 635 ApiMessageReader api_reader(snapshot, &zone_allocator);
638 writer.BytesWritten(), 636 Dart_CObject* root = api_reader.ReadMessage();
639 &zone_allocator);
640 EXPECT_EQ(Dart_CObject::kArray, root->type); 637 EXPECT_EQ(Dart_CObject::kArray, root->type);
641 EXPECT_EQ(kArrayLength, root->value.as_array.length); 638 EXPECT_EQ(kArrayLength, root->value.as_array.length);
642 EXPECT(root->value.as_array.values == NULL); 639 EXPECT(root->value.as_array.values == NULL);
643 CheckEncodeDecodeMessage(root); 640 CheckEncodeDecodeMessage(root);
644 } 641 }
645 642
646 643
647 TEST_CASE(SerializeByteArray) { 644 TEST_CASE(SerializeByteArray) {
648 Zone zone(Isolate::Current()); 645 Zone zone(Isolate::Current());
649 646
650 // Write snapshot with object content. 647 // Write snapshot with object content.
651 uint8_t* buffer; 648 uint8_t* buffer;
652 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 649 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
653 const int kByteArrayLength = 256; 650 const int kByteArrayLength = 256;
654 Uint8Array& byte_array = 651 Uint8Array& byte_array =
655 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); 652 Uint8Array::Handle(Uint8Array::New(kByteArrayLength));
656 for (int i = 0; i < kByteArrayLength; i++) { 653 for (int i = 0; i < kByteArrayLength; i++) {
657 byte_array.SetAt(i, i); 654 byte_array.SetAt(i, i);
658 } 655 }
659 writer.WriteObject(byte_array.raw()); 656 writer.WriteObject(byte_array.raw());
660 writer.FinalizeBuffer(); 657 intptr_t buffer_len = writer.FinalizeBuffer();
661 658
662 // Create a snapshot object using the buffer. 659 // Create a snapshot object using the buffer.
663 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 660 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
661 EXPECT_NOTNULL(snapshot);
664 662
665 // Read object back from the snapshot. 663 // Read object back from the snapshot.
666 SnapshotReader reader(snapshot, Isolate::Current()); 664 SnapshotReader reader(snapshot, Isolate::Current());
667 ByteArray& serialized_byte_array = ByteArray::Handle(); 665 ByteArray& serialized_byte_array = ByteArray::Handle();
668 serialized_byte_array ^= reader.ReadObject(); 666 serialized_byte_array ^= reader.ReadObject();
669 EXPECT(serialized_byte_array.IsByteArray()); 667 EXPECT(serialized_byte_array.IsByteArray());
670 668
671 // Read object back from the snapshot into a C structure. 669 // Read object back from the snapshot into a C structure.
672 ApiNativeScope scope; 670 ApiNativeScope scope;
673 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 671 ApiMessageReader api_reader(snapshot, &zone_allocator);
674 writer.BytesWritten(), 672 Dart_CObject* root = api_reader.ReadMessage();
675 &zone_allocator);
676 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); 673 EXPECT_EQ(Dart_CObject::kUint8Array, root->type);
677 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); 674 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
678 for (int i = 0; i < kByteArrayLength; i++) { 675 for (int i = 0; i < kByteArrayLength; i++) {
679 EXPECT(root->value.as_byte_array.values[i] == i); 676 EXPECT(root->value.as_byte_array.values[i] == i);
680 } 677 }
681 CheckEncodeDecodeMessage(root); 678 CheckEncodeDecodeMessage(root);
682 } 679 }
683 680
684 681
685 TEST_CASE(SerializeEmptyByteArray) { 682 TEST_CASE(SerializeEmptyByteArray) {
686 Zone zone(Isolate::Current()); 683 Zone zone(Isolate::Current());
687 684
688 // Write snapshot with object content. 685 // Write snapshot with object content.
689 uint8_t* buffer; 686 uint8_t* buffer;
690 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 687 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
691 const int kByteArrayLength = 0; 688 const int kByteArrayLength = 0;
692 Uint8Array& byte_array = 689 Uint8Array& byte_array =
693 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); 690 Uint8Array::Handle(Uint8Array::New(kByteArrayLength));
694 writer.WriteObject(byte_array.raw()); 691 writer.WriteObject(byte_array.raw());
695 writer.FinalizeBuffer(); 692 intptr_t buffer_len = writer.FinalizeBuffer();
696 693
697 // Create a snapshot object using the buffer. 694 // Create a snapshot object using the buffer.
698 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 695 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
696 EXPECT_NOTNULL(snapshot);
699 697
700 // Read object back from the snapshot. 698 // Read object back from the snapshot.
701 SnapshotReader reader(snapshot, Isolate::Current()); 699 SnapshotReader reader(snapshot, Isolate::Current());
702 ByteArray& serialized_byte_array = ByteArray::Handle(); 700 ByteArray& serialized_byte_array = ByteArray::Handle();
703 serialized_byte_array ^= reader.ReadObject(); 701 serialized_byte_array ^= reader.ReadObject();
704 EXPECT(serialized_byte_array.IsByteArray()); 702 EXPECT(serialized_byte_array.IsByteArray());
705 703
706 // Read object back from the snapshot into a C structure. 704 // Read object back from the snapshot into a C structure.
707 ApiNativeScope scope; 705 ApiNativeScope scope;
708 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 706 ApiMessageReader api_reader(snapshot, &zone_allocator);
709 writer.BytesWritten(), 707 Dart_CObject* root = api_reader.ReadMessage();
710 &zone_allocator);
711 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); 708 EXPECT_EQ(Dart_CObject::kUint8Array, root->type);
712 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); 709 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
713 EXPECT(root->value.as_byte_array.values == NULL); 710 EXPECT(root->value.as_byte_array.values == NULL);
714 CheckEncodeDecodeMessage(root); 711 CheckEncodeDecodeMessage(root);
715 } 712 }
716 713
717 714
715 TEST_CASE(IllegalSnapshot_LengthMismatch) {
716 Zone zone(Isolate::Current());
717
718 // Write snapshot with object content.
719 uint8_t* buffer;
720 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
721 const Object& null_object = Object::Handle();
722 writer.WriteObject(null_object.raw());
723 intptr_t buffer_len = writer.FinalizeBuffer();
724
725 // Create a snapshot object using the wrong buffer length.
726 const Snapshot* snapshot =
727 Snapshot::SetupFromBuffer(buffer, buffer_len + sizeof(int32_t));
728 EXPECT(snapshot == NULL);
729
730 snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
731 EXPECT_NOTNULL(snapshot);
732 }
733
734
735 TEST_CASE(IllegalSnapshot_TooShort) {
736 Zone zone(Isolate::Current());
737
738 // Write snapshot with object content.
739 uint8_t* buffer;
740 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
741 const Object& null_object = Object::Handle();
742 writer.WriteObject(null_object.raw());
743 writer.FinalizeBuffer();
744
745 // Give the message an illegal size.
746 intptr_t snapshot_address = reinterpret_cast<intptr_t>(buffer);
747 intptr_t length_address = snapshot_address + Snapshot::length_offset();
748 int32_t* len_ptr = reinterpret_cast<int32_t*>(length_address);
749 *len_ptr = sizeof(int32_t);
750
751 // Check that SetupFromBuffer returns NULL because the buffer length
752 // is too short to possibly be valid.
753 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, sizeof(int32_t));
754 EXPECT(snapshot == NULL);
755 }
756
757
718 TEST_CASE(SerializeScript) { 758 TEST_CASE(SerializeScript) {
719 const char* kScriptChars = 759 const char* kScriptChars =
720 "class A {\n" 760 "class A {\n"
721 " static bar() { return 42; }\n" 761 " static bar() { return 42; }\n"
722 " static fly() { return 5; }\n" 762 " static fly() { return 5; }\n"
723 " static s1() { return 'this is a string in the source'; }\n" 763 " static s1() { return 'this is a string in the source'; }\n"
724 " static s2() { return 'this is a \"string\" in the source'; }\n" 764 " static s2() { return 'this is a \"string\" in the source'; }\n"
725 " static s3() { return 'this is a \\\'string\\\' in \"the\" source'; }\n" 765 " static s3() { return 'this is a \\\'string\\\' in \"the\" source'; }\n"
726 " static s4() { return 'this \"is\" a \"string\" in \"the\" source'; }\n" 766 " static s4() { return 'this \"is\" a \"string\" in \"the\" source'; }\n"
727 "}\n"; 767 "}\n";
728 768
729 String& url = String::Handle(String::New("dart-test:SerializeScript")); 769 String& url = String::Handle(String::New("dart-test:SerializeScript"));
730 String& source = String::Handle(String::New(kScriptChars)); 770 String& source = String::Handle(String::New(kScriptChars));
731 Script& script = Script::Handle(Script::New(url, 771 Script& script = Script::Handle(Script::New(url,
732 source, 772 source,
733 RawScript::kSourceTag)); 773 RawScript::kSourceTag));
734 const String& lib_url = String::Handle(Symbols::New("TestLib")); 774 const String& lib_url = String::Handle(Symbols::New("TestLib"));
735 Library& lib = Library::Handle(Library::New(lib_url)); 775 Library& lib = Library::Handle(Library::New(lib_url));
736 lib.Register(); 776 lib.Register();
737 EXPECT(CompilerTest::TestCompileScript(lib, script)); 777 EXPECT(CompilerTest::TestCompileScript(lib, script));
738 778
739 // Write snapshot with object content. 779 // Write snapshot with object content.
740 uint8_t* buffer; 780 uint8_t* buffer;
741 SnapshotWriter writer(Snapshot::kScript, &buffer, &malloc_allocator); 781 SnapshotWriter writer(Snapshot::kScript, &buffer, &malloc_allocator);
742 writer.WriteObject(script.raw()); 782 writer.WriteObject(script.raw());
743 writer.FinalizeBuffer(); 783 intptr_t buffer_len = writer.FinalizeBuffer();
744 784
745 // Create a snapshot object using the buffer. 785 // Create a snapshot object using the buffer.
746 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 786 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
787 EXPECT_NOTNULL(snapshot);
747 788
748 // Read object back from the snapshot. 789 // Read object back from the snapshot.
749 SnapshotReader reader(snapshot, Isolate::Current()); 790 SnapshotReader reader(snapshot, Isolate::Current());
750 Script& serialized_script = Script::Handle(); 791 Script& serialized_script = Script::Handle();
751 serialized_script ^= reader.ReadObject(); 792 serialized_script ^= reader.ReadObject();
752 793
753 // Check if the serialized script object matches the original script. 794 // Check if the serialized script object matches the original script.
754 String& expected_literal = String::Handle(); 795 String& expected_literal = String::Handle();
755 String& actual_literal = String::Handle(); 796 String& actual_literal = String::Handle();
756 String& str = String::Handle(); 797 String& str = String::Handle();
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1053 1094
1054 1095
1055 TEST_CASE(IntArrayMessage) { 1096 TEST_CASE(IntArrayMessage) {
1056 Zone zone(Isolate::Current()); 1097 Zone zone(Isolate::Current());
1057 uint8_t* buffer = NULL; 1098 uint8_t* buffer = NULL;
1058 ApiMessageWriter writer(&buffer, &zone_allocator); 1099 ApiMessageWriter writer(&buffer, &zone_allocator);
1059 1100
1060 static const int kArrayLength = 2; 1101 static const int kArrayLength = 2;
1061 intptr_t data[kArrayLength] = {1, 2}; 1102 intptr_t data[kArrayLength] = {1, 2};
1062 int len = kArrayLength; 1103 int len = kArrayLength;
1063 writer.WriteMessage(len, data); 1104 intptr_t buffer_len = writer.WriteMessage(len, data);
1105
1106 // Create a snapshot object using the buffer.
1107 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
1108 EXPECT_NOTNULL(snapshot);
1064 1109
1065 // Read object back from the snapshot into a C structure. 1110 // Read object back from the snapshot into a C structure.
1066 ApiNativeScope scope; 1111 ApiNativeScope scope;
1067 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1112 ApiMessageReader api_reader(snapshot, &zone_allocator);
1068 writer.BytesWritten(), 1113 Dart_CObject* root = api_reader.ReadMessage();
1069 &zone_allocator);
1070 EXPECT_EQ(Dart_CObject::kArray, root->type); 1114 EXPECT_EQ(Dart_CObject::kArray, root->type);
1071 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1115 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1072 for (int i = 0; i < kArrayLength; i++) { 1116 for (int i = 0; i < kArrayLength; i++) {
1073 Dart_CObject* element = root->value.as_array.values[i]; 1117 Dart_CObject* element = root->value.as_array.values[i];
1074 EXPECT_EQ(Dart_CObject::kInt32, element->type); 1118 EXPECT_EQ(Dart_CObject::kInt32, element->type);
1075 EXPECT_EQ(i + 1, element->value.as_int32); 1119 EXPECT_EQ(i + 1, element->value.as_int32);
1076 } 1120 }
1077 CheckEncodeDecodeMessage(root); 1121 CheckEncodeDecodeMessage(root);
1078 } 1122 }
1079 1123
1080 1124
1081 // Helper function to call a top level Dart function, serialize the 1125 // Helper function to call a top level Dart function, serialize the
1082 // result and deserialize the result into a Dart_CObject structure. 1126 // result and deserialize the result into a Dart_CObject structure.
1083 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib, 1127 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib,
1084 const char* dart_function) { 1128 const char* dart_function) {
1085 Dart_Handle result; 1129 Dart_Handle result;
1086 result = Dart_Invoke(lib, Dart_NewString(dart_function), 0, NULL); 1130 result = Dart_Invoke(lib, Dart_NewString(dart_function), 0, NULL);
1087 EXPECT_VALID(result); 1131 EXPECT_VALID(result);
1088 1132
1089 // Serialize the list into a message. 1133 // Serialize the list into a message.
1090 uint8_t* buffer; 1134 uint8_t* buffer;
1091 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1135 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
1092 const Object& list = Object::Handle(Api::UnwrapHandle(result)); 1136 const Object& list = Object::Handle(Api::UnwrapHandle(result));
1093 writer.WriteObject(list.raw()); 1137 writer.WriteObject(list.raw());
1094 writer.FinalizeBuffer(); 1138 intptr_t buffer_len = writer.FinalizeBuffer();
1139
1140 // Create a snapshot object using the buffer.
1141 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
1142 EXPECT_NOTNULL(snapshot);
1095 1143
1096 // Read object back from the snapshot into a C structure. 1144 // Read object back from the snapshot into a C structure.
1097 return DecodeMessage(buffer + Snapshot::kHeaderSize, 1145 ApiMessageReader api_reader(snapshot, &zone_allocator);
1098 writer.BytesWritten(), 1146 return api_reader.ReadMessage();
1099 &zone_allocator);
1100 } 1147 }
1101 1148
1102 1149
1103 UNIT_TEST_CASE(DartGeneratedMessages) { 1150 UNIT_TEST_CASE(DartGeneratedMessages) {
1104 static const char* kCustomIsolateScriptChars = 1151 static const char* kCustomIsolateScriptChars =
1105 "getSmi() {\n" 1152 "getSmi() {\n"
1106 " return 42;\n" 1153 " return 42;\n"
1107 "}\n" 1154 "}\n"
1108 "getBigint() {\n" 1155 "getBigint() {\n"
1109 " return -0x424242424242424242424242424242424242;\n" 1156 " return -0x424242424242424242424242424242424242;\n"
(...skipping 27 matching lines...) Expand all
1137 { 1184 {
1138 DARTSCOPE_NOCHECKS(isolate); 1185 DARTSCOPE_NOCHECKS(isolate);
1139 1186
1140 { 1187 {
1141 Zone zone(Isolate::Current()); 1188 Zone zone(Isolate::Current());
1142 uint8_t* buffer; 1189 uint8_t* buffer;
1143 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1190 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
1144 Smi& smi = Smi::Handle(); 1191 Smi& smi = Smi::Handle();
1145 smi ^= Api::UnwrapHandle(smi_result); 1192 smi ^= Api::UnwrapHandle(smi_result);
1146 writer.WriteObject(smi.raw()); 1193 writer.WriteObject(smi.raw());
1147 writer.FinalizeBuffer(); 1194 intptr_t buffer_len = writer.FinalizeBuffer();
1195
1196 // Create a snapshot object using the buffer.
1197 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
1198 EXPECT_NOTNULL(snapshot);
1148 1199
1149 // Read object back from the snapshot into a C structure. 1200 // Read object back from the snapshot into a C structure.
1150 ApiNativeScope scope; 1201 ApiNativeScope scope;
1151 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1202 ApiMessageReader api_reader(snapshot, &zone_allocator);
1152 writer.BytesWritten(), 1203 Dart_CObject* root = api_reader.ReadMessage();
1153 &zone_allocator);
1154 EXPECT_NOTNULL(root); 1204 EXPECT_NOTNULL(root);
1155 EXPECT_EQ(Dart_CObject::kInt32, root->type); 1205 EXPECT_EQ(Dart_CObject::kInt32, root->type);
1156 EXPECT_EQ(42, root->value.as_int32); 1206 EXPECT_EQ(42, root->value.as_int32);
1157 CheckEncodeDecodeMessage(root); 1207 CheckEncodeDecodeMessage(root);
1158 } 1208 }
1159 { 1209 {
1160 Zone zone(Isolate::Current()); 1210 Zone zone(Isolate::Current());
1161 uint8_t* buffer; 1211 uint8_t* buffer;
1162 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1212 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
1163 Bigint& bigint = Bigint::Handle(); 1213 Bigint& bigint = Bigint::Handle();
1164 bigint ^= Api::UnwrapHandle(bigint_result); 1214 bigint ^= Api::UnwrapHandle(bigint_result);
1165 writer.WriteObject(bigint.raw()); 1215 writer.WriteObject(bigint.raw());
1166 writer.FinalizeBuffer(); 1216 intptr_t buffer_len = writer.FinalizeBuffer();
1217
1218 // Create a snapshot object using the buffer.
1219 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
1220 EXPECT_NOTNULL(snapshot);
1167 1221
1168 // Read object back from the snapshot into a C structure. 1222 // Read object back from the snapshot into a C structure.
1169 ApiNativeScope scope; 1223 ApiNativeScope scope;
1170 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1224 ApiMessageReader api_reader(snapshot, &zone_allocator);
1171 writer.BytesWritten(), 1225 Dart_CObject* root = api_reader.ReadMessage();
1172 &zone_allocator);
1173 EXPECT_NOTNULL(root); 1226 EXPECT_NOTNULL(root);
1174 EXPECT_EQ(Dart_CObject::kBigint, root->type); 1227 EXPECT_EQ(Dart_CObject::kBigint, root->type);
1175 EXPECT_STREQ("-424242424242424242424242424242424242", 1228 EXPECT_STREQ("-424242424242424242424242424242424242",
1176 root->value.as_bigint); 1229 root->value.as_bigint);
1177 CheckEncodeDecodeMessage(root); 1230 CheckEncodeDecodeMessage(root);
1178 } 1231 }
1179 { 1232 {
1180 Zone zone(Isolate::Current()); 1233 Zone zone(Isolate::Current());
1181 uint8_t* buffer; 1234 uint8_t* buffer;
1182 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1235 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
1183 String& str = String::Handle(); 1236 String& str = String::Handle();
1184 str ^= Api::UnwrapHandle(string_result); 1237 str ^= Api::UnwrapHandle(string_result);
1185 writer.WriteObject(str.raw()); 1238 writer.WriteObject(str.raw());
1186 writer.FinalizeBuffer(); 1239 intptr_t buffer_len = writer.FinalizeBuffer();
1240
1241 // Create a snapshot object using the buffer.
1242 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer, buffer_len);
1243 EXPECT_NOTNULL(snapshot);
1187 1244
1188 // Read object back from the snapshot into a C structure. 1245 // Read object back from the snapshot into a C structure.
1189 ApiNativeScope scope; 1246 ApiNativeScope scope;
1190 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1247 ApiMessageReader api_reader(snapshot, &zone_allocator);
1191 writer.BytesWritten(), 1248 Dart_CObject* root = api_reader.ReadMessage();
1192 &zone_allocator);
1193 EXPECT_NOTNULL(root); 1249 EXPECT_NOTNULL(root);
1194 EXPECT_EQ(Dart_CObject::kString, root->type); 1250 EXPECT_EQ(Dart_CObject::kString, root->type);
1195 EXPECT_STREQ("Hello, world!", root->value.as_string); 1251 EXPECT_STREQ("Hello, world!", root->value.as_string);
1196 CheckEncodeDecodeMessage(root); 1252 CheckEncodeDecodeMessage(root);
1197 } 1253 }
1198 } 1254 }
1199 Dart_ExitScope(); 1255 Dart_ExitScope();
1200 Dart_ShutdownIsolate(); 1256 Dart_ShutdownIsolate();
1201 } 1257 }
1202 1258
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
1562 EXPECT(Dart_ErrorHasException(result)); 1618 EXPECT(Dart_ErrorHasException(result));
1563 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", 1619 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n",
1564 Dart_GetError(result)); 1620 Dart_GetError(result));
1565 1621
1566 Dart_ExitScope(); 1622 Dart_ExitScope();
1567 } 1623 }
1568 1624
1569 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1625 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1570 1626
1571 } // namespace dart 1627 } // namespace dart
OLDNEW
« runtime/vm/isolate.cc ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698