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

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
« no previous file with comments | « 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 18 matching lines...) Expand all
29 if (expected.IsDouble()) { 29 if (expected.IsDouble()) {
30 if (actual.IsDouble()) { 30 if (actual.IsDouble()) {
31 Double& dbl1 = Double::Handle(); 31 Double& dbl1 = Double::Handle();
32 Double& dbl2 = Double::Handle(); 32 Double& dbl2 = Double::Handle();
33 dbl1 ^= expected.raw(); 33 dbl1 ^= expected.raw();
34 dbl2 ^= actual.raw(); 34 dbl2 ^= actual.raw();
35 return dbl1.value() == dbl2.value(); 35 return dbl1.value() == dbl2.value();
36 } 36 }
37 return false; 37 return false;
38 } 38 }
39 if (expected.IsBool()) {
40 if (actual.IsBool()) {
41 Bool& bl1 = Bool::Handle();
42 Bool& bl2 = Bool::Handle();
43 bl1 ^= expected.raw();
44 bl2 ^= actual.raw();
45 return bl1.value() == bl2.value();
46 }
47 return false;
48 }
39 return false; 49 return false;
40 } 50 }
41 51
42 52
43 static uint8_t* malloc_allocator( 53 static uint8_t* malloc_allocator(
44 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 54 uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
45 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); 55 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size));
46 } 56 }
47 57
48 58
49 static uint8_t* zone_allocator( 59 static uint8_t* zone_allocator(
50 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 60 uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
51 Zone* zone = Isolate::Current()->current_zone(); 61 Zone* zone = Isolate::Current()->current_zone();
52 return zone->Realloc<uint8_t>(ptr, old_size, new_size); 62 return zone->Realloc<uint8_t>(ptr, old_size, new_size);
53 } 63 }
54 64
55 65
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 66 // Compare two Dart_CObject object graphs rooted in first and
65 // second. The second graph will be destroyed by this operation no matter 67 // second. The second graph will be destroyed by this operation no matter
66 // whether the graphs are equal or not. 68 // whether the graphs are equal or not.
67 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { 69 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) {
68 // Return immediately if entering a cycle. 70 // Return immediately if entering a cycle.
69 if (second->type == Dart_CObject::kNumberOfTypes) return; 71 if (second->type == Dart_CObject::kNumberOfTypes) return;
70 72
71 EXPECT_NE(first, second); 73 EXPECT_NE(first, second);
72 EXPECT_EQ(first->type, second->type); 74 EXPECT_EQ(first->type, second->type);
73 switch (first->type) { 75 switch (first->type) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 } 117 }
116 } 118 }
117 119
118 120
119 static void CheckEncodeDecodeMessage(Dart_CObject* root) { 121 static void CheckEncodeDecodeMessage(Dart_CObject* root) {
120 // Encode and decode the message. 122 // Encode and decode the message.
121 uint8_t* buffer = NULL; 123 uint8_t* buffer = NULL;
122 ApiMessageWriter writer(&buffer, &malloc_allocator); 124 ApiMessageWriter writer(&buffer, &malloc_allocator);
123 writer.WriteCMessage(root); 125 writer.WriteCMessage(root);
124 126
125 Dart_CObject* new_root = DecodeMessage(buffer + Snapshot::kHeaderSize, 127 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator);
126 writer.BytesWritten(), 128 Dart_CObject* new_root = api_reader.ReadMessage();
127 &zone_allocator);
128 129
129 // Check that the two messages are the same. 130 // Check that the two messages are the same.
130 CompareDartCObjects(root, new_root); 131 CompareDartCObjects(root, new_root);
131 } 132 }
132 133
133 TEST_CASE(SerializeNull) { 134 TEST_CASE(SerializeNull) {
134 Zone zone(Isolate::Current()); 135 Zone zone(Isolate::Current());
135 136
136 // Write snapshot with object content. 137 // Write snapshot with object content.
137 uint8_t* buffer; 138 uint8_t* buffer;
138 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 139 MessageWriter writer(&buffer, &zone_allocator);
139 const Object& null_object = Object::Handle(); 140 const Object& null_object = Object::Handle();
140 writer.WriteObject(null_object.raw()); 141 writer.WriteMessage(null_object);
141 writer.FinalizeBuffer(); 142 intptr_t buffer_len = writer.BytesWritten();
142
143 // Create a snapshot object using the buffer.
144 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
145 143
146 // Read object back from the snapshot. 144 // Read object back from the snapshot.
147 SnapshotReader reader(snapshot, Isolate::Current()); 145 SnapshotReader reader(buffer, buffer_len,
146 Snapshot::kMessage, Isolate::Current());
148 const Object& serialized_object = Object::Handle(reader.ReadObject()); 147 const Object& serialized_object = Object::Handle(reader.ReadObject());
149 EXPECT(Equals(null_object, serialized_object)); 148 EXPECT(Equals(null_object, serialized_object));
150 149
151 // Read object back from the snapshot into a C structure. 150 // Read object back from the snapshot into a C structure.
152 ApiNativeScope scope; 151 ApiNativeScope scope;
153 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 152 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
154 writer.BytesWritten(), 153 Dart_CObject* root = api_reader.ReadMessage();
155 &zone_allocator);
156 EXPECT_NOTNULL(root); 154 EXPECT_NOTNULL(root);
157 EXPECT_EQ(Dart_CObject::kNull, root->type); 155 EXPECT_EQ(Dart_CObject::kNull, root->type);
158 CheckEncodeDecodeMessage(root); 156 CheckEncodeDecodeMessage(root);
159 } 157 }
160 158
161 159
162 TEST_CASE(SerializeSmi1) { 160 TEST_CASE(SerializeSmi1) {
163 Zone zone(Isolate::Current()); 161 Zone zone(Isolate::Current());
164 162
165 // Write snapshot with object content. 163 // Write snapshot with object content.
166 uint8_t* buffer; 164 uint8_t* buffer;
167 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 165 MessageWriter writer(&buffer, &zone_allocator);
168 const Smi& smi = Smi::Handle(Smi::New(124)); 166 const Smi& smi = Smi::Handle(Smi::New(124));
169 writer.WriteObject(smi.raw()); 167 writer.WriteMessage(smi);
170 writer.FinalizeBuffer(); 168 intptr_t buffer_len = writer.BytesWritten();
171
172 // Create a snapshot object using the buffer.
173 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
174 169
175 // Read object back from the snapshot. 170 // Read object back from the snapshot.
176 SnapshotReader reader(snapshot, Isolate::Current()); 171 SnapshotReader reader(buffer, buffer_len,
172 Snapshot::kMessage, 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(buffer, buffer_len, &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 MessageWriter writer(&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.WriteMessage(smi);
200 writer.FinalizeBuffer(); 195 intptr_t buffer_len = writer.BytesWritten();
201
202 // Create a snapshot object using the buffer.
203 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
204 196
205 // Read object back from the snapshot. 197 // Read object back from the snapshot.
206 SnapshotReader reader(snapshot, Isolate::Current()); 198 SnapshotReader reader(buffer, buffer_len,
199 Snapshot::kMessage, Isolate::Current());
207 const Object& serialized_object = Object::Handle(reader.ReadObject()); 200 const Object& serialized_object = Object::Handle(reader.ReadObject());
208 EXPECT(Equals(smi, serialized_object)); 201 EXPECT(Equals(smi, serialized_object));
209 202
210 // Read object back from the snapshot into a C structure. 203 // Read object back from the snapshot into a C structure.
211 ApiNativeScope scope; 204 ApiNativeScope scope;
212 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 205 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
213 writer.BytesWritten(), 206 Dart_CObject* root = api_reader.ReadMessage();
214 &zone_allocator);
215 EXPECT_NOTNULL(root); 207 EXPECT_NOTNULL(root);
216 EXPECT_EQ(Dart_CObject::kInt32, root->type); 208 EXPECT_EQ(Dart_CObject::kInt32, root->type);
217 EXPECT_EQ(smi.Value(), root->value.as_int32); 209 EXPECT_EQ(smi.Value(), root->value.as_int32);
218 CheckEncodeDecodeMessage(root); 210 CheckEncodeDecodeMessage(root);
219 } 211 }
220 212
221 213
222 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) { 214 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) {
223 // Write snapshot with object content. 215 // Write snapshot with object content.
224 uint8_t* buffer; 216 uint8_t* buffer;
225 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 217 MessageWriter writer(&buffer, &zone_allocator);
226 writer.WriteObject(mint.raw()); 218 writer.WriteMessage(mint);
227 writer.FinalizeBuffer(); 219 intptr_t buffer_len = writer.BytesWritten();
228
229 // Create a snapshot object using the buffer.
230 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
231 220
232 // Read object back from the snapshot. 221 // Read object back from the snapshot.
233 SnapshotReader reader(snapshot, Isolate::Current()); 222 SnapshotReader reader(buffer, buffer_len,
223 Snapshot::kMessage, Isolate::Current());
234 const Object& serialized_object = Object::Handle(reader.ReadObject()); 224 const Object& serialized_object = Object::Handle(reader.ReadObject());
235 EXPECT(serialized_object.IsMint()); 225 EXPECT(serialized_object.IsMint());
236 226
237 // Read object back from the snapshot into a C structure. 227 // Read object back from the snapshot into a C structure.
238 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 228 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
239 writer.BytesWritten(), 229 Dart_CObject* root = api_reader.ReadMessage();
240 &zone_allocator);
241 EXPECT_NOTNULL(root); 230 EXPECT_NOTNULL(root);
242 CheckEncodeDecodeMessage(root); 231 CheckEncodeDecodeMessage(root);
243 return root; 232 return root;
244 } 233 }
245 234
246 235
247 void CheckMint(int64_t value) { 236 void CheckMint(int64_t value) {
248 Zone zone(Isolate::Current()); 237 Zone zone(Isolate::Current());
249 238
250 const Mint& mint = Mint::Handle(Mint::New(value)); 239 const Mint& mint = Mint::Handle(Mint::New(value));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 // Min negative mint + 1. 276 // Min negative mint + 1.
288 CheckMint(kMinInt64 + 1); 277 CheckMint(kMinInt64 + 1);
289 } 278 }
290 279
291 280
292 TEST_CASE(SerializeDouble) { 281 TEST_CASE(SerializeDouble) {
293 Zone zone(Isolate::Current()); 282 Zone zone(Isolate::Current());
294 283
295 // Write snapshot with object content. 284 // Write snapshot with object content.
296 uint8_t* buffer; 285 uint8_t* buffer;
297 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 286 MessageWriter writer(&buffer, &zone_allocator);
298 const Double& dbl = Double::Handle(Double::New(101.29)); 287 const Double& dbl = Double::Handle(Double::New(101.29));
299 writer.WriteObject(dbl.raw()); 288 writer.WriteMessage(dbl);
300 writer.FinalizeBuffer(); 289 intptr_t buffer_len = writer.BytesWritten();
301
302 // Create a snapshot object using the buffer.
303 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
304 290
305 // Read object back from the snapshot. 291 // Read object back from the snapshot.
306 SnapshotReader reader(snapshot, Isolate::Current()); 292 SnapshotReader reader(buffer, buffer_len,
293 Snapshot::kMessage, Isolate::Current());
307 const Object& serialized_object = Object::Handle(reader.ReadObject()); 294 const Object& serialized_object = Object::Handle(reader.ReadObject());
308 EXPECT(Equals(dbl, serialized_object)); 295 EXPECT(Equals(dbl, serialized_object));
309 296
310 // Read object back from the snapshot into a C structure. 297 // Read object back from the snapshot into a C structure.
311 ApiNativeScope scope; 298 ApiNativeScope scope;
312 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 299 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
313 writer.BytesWritten(), 300 Dart_CObject* root = api_reader.ReadMessage();
314 &zone_allocator);
315 EXPECT_NOTNULL(root); 301 EXPECT_NOTNULL(root);
316 EXPECT_EQ(Dart_CObject::kDouble, root->type); 302 EXPECT_EQ(Dart_CObject::kDouble, root->type);
317 EXPECT_EQ(dbl.value(), root->value.as_double); 303 EXPECT_EQ(dbl.value(), root->value.as_double);
318 CheckEncodeDecodeMessage(root); 304 CheckEncodeDecodeMessage(root);
319 } 305 }
320 306
321 307
322 TEST_CASE(SerializeBool) {
323 Zone zone(Isolate::Current());
324
325 // Write snapshot with object content.
326 uint8_t* buffer;
327 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
328 const Bool& bool1 = Bool::Handle(Bool::True());
329 const Bool& bool2 = Bool::Handle(Bool::False());
330 writer.WriteObject(bool1.raw());
331 writer.WriteObject(bool2.raw());
332 writer.FinalizeBuffer();
333
334 // Create a snapshot object using the buffer.
335 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
336
337 // Read object back from the snapshot.
338 SnapshotReader reader(snapshot, Isolate::Current());
339 EXPECT(Bool::True() == reader.ReadObject());
340 EXPECT(Bool::False() == reader.ReadObject());
341 }
342
343
344 TEST_CASE(SerializeTrue) { 308 TEST_CASE(SerializeTrue) {
345 Zone zone(Isolate::Current()); 309 Zone zone(Isolate::Current());
346 310
347 // Write snapshot with true object. 311 // Write snapshot with true object.
348 uint8_t* buffer; 312 uint8_t* buffer;
349 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 313 MessageWriter writer(&buffer, &zone_allocator);
350 const Bool& bl = Bool::Handle(Bool::True()); 314 const Bool& bl = Bool::Handle(Bool::True());
351 writer.WriteObject(bl.raw()); 315 writer.WriteMessage(bl);
352 writer.FinalizeBuffer(); 316 intptr_t buffer_len = writer.BytesWritten();
353 317
354 // Create a snapshot object using the buffer. 318 // Read object back from the snapshot.
355 Snapshot::SetupFromBuffer(buffer); 319 SnapshotReader reader(buffer, buffer_len,
320 Snapshot::kMessage, Isolate::Current());
321 const Object& serialized_object = Object::Handle(reader.ReadObject());
322 fprintf(stderr, "%s / %s\n", bl.ToCString(), serialized_object.ToCString());
323
324 EXPECT(Equals(bl, serialized_object));
356 325
357 // Read object back from the snapshot into a C structure. 326 // Read object back from the snapshot into a C structure.
358 ApiNativeScope scope; 327 ApiNativeScope scope;
359 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 328 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
360 writer.BytesWritten(), 329 Dart_CObject* root = api_reader.ReadMessage();
361 &zone_allocator);
362 EXPECT_NOTNULL(root); 330 EXPECT_NOTNULL(root);
363 EXPECT_EQ(Dart_CObject::kBool, root->type); 331 EXPECT_EQ(Dart_CObject::kBool, root->type);
364 EXPECT_EQ(true, root->value.as_bool); 332 EXPECT_EQ(true, root->value.as_bool);
365 CheckEncodeDecodeMessage(root); 333 CheckEncodeDecodeMessage(root);
366 } 334 }
367 335
368 336
369 TEST_CASE(SerializeFalse) { 337 TEST_CASE(SerializeFalse) {
370 Zone zone(Isolate::Current()); 338 Zone zone(Isolate::Current());
371 339
372 // Write snapshot with false object. 340 // Write snapshot with false object.
373 uint8_t* buffer; 341 uint8_t* buffer;
374 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 342 MessageWriter writer(&buffer, &zone_allocator);
375 const Bool& bl = Bool::Handle(Bool::False()); 343 const Bool& bl = Bool::Handle(Bool::False());
376 writer.WriteObject(bl.raw()); 344 writer.WriteMessage(bl);
377 writer.FinalizeBuffer(); 345 intptr_t buffer_len = writer.BytesWritten();
378 346
379 // Create a snapshot object using the buffer. 347 // Read object back from the snapshot.
380 Snapshot::SetupFromBuffer(buffer); 348 SnapshotReader reader(buffer, buffer_len,
349 Snapshot::kMessage, Isolate::Current());
350 const Object& serialized_object = Object::Handle(reader.ReadObject());
351 EXPECT(Equals(bl, serialized_object));
381 352
382 // Read object back from the snapshot into a C structure. 353 // Read object back from the snapshot into a C structure.
383 ApiNativeScope scope; 354 ApiNativeScope scope;
384 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 355 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
385 writer.BytesWritten(), 356 Dart_CObject* root = api_reader.ReadMessage();
386 &zone_allocator);
387 EXPECT_NOTNULL(root); 357 EXPECT_NOTNULL(root);
388 EXPECT_EQ(Dart_CObject::kBool, root->type); 358 EXPECT_EQ(Dart_CObject::kBool, root->type);
389 EXPECT_EQ(false, root->value.as_bool); 359 EXPECT_EQ(false, root->value.as_bool);
390 CheckEncodeDecodeMessage(root); 360 CheckEncodeDecodeMessage(root);
391 } 361 }
392 362
393 363
394 static uword allocator(intptr_t size) { 364 static uword allocator(intptr_t size) {
395 return reinterpret_cast<uword>(malloc(size)); 365 return reinterpret_cast<uword>(malloc(size));
396 } 366 }
397 367
398 368
399 TEST_CASE(SerializeBigint) { 369 TEST_CASE(SerializeBigint) {
400 Zone zone(Isolate::Current()); 370 Zone zone(Isolate::Current());
401 371
402 // Write snapshot with object content. 372 // Write snapshot with object content.
403 uint8_t* buffer; 373 uint8_t* buffer;
404 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 374 MessageWriter writer(&buffer, &zone_allocator);
405 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); 375 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff)));
406 writer.WriteObject(bigint.raw()); 376 writer.WriteMessage(bigint);
407 writer.FinalizeBuffer(); 377 intptr_t buffer_len = writer.BytesWritten();
408
409 // Create a snapshot object using the buffer.
410 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
411 378
412 // Read object back from the snapshot. 379 // Read object back from the snapshot.
413 SnapshotReader reader(snapshot, Isolate::Current()); 380 SnapshotReader reader(buffer, buffer_len,
381 Snapshot::kMessage, Isolate::Current());
414 Bigint& obj = Bigint::Handle(); 382 Bigint& obj = Bigint::Handle();
415 obj ^= reader.ReadObject(); 383 obj ^= reader.ReadObject();
416 384
417 EXPECT_EQ(BigintOperations::ToMint(bigint), BigintOperations::ToMint(obj)); 385 EXPECT_EQ(BigintOperations::ToMint(bigint), BigintOperations::ToMint(obj));
418 386
419 // Read object back from the snapshot into a C structure. 387 // Read object back from the snapshot into a C structure.
420 ApiNativeScope scope; 388 ApiNativeScope scope;
421 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 389 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
422 writer.BytesWritten(), 390 Dart_CObject* root = api_reader.ReadMessage();
423 &zone_allocator);
424 // Bigint not supported. 391 // Bigint not supported.
425 EXPECT_NOTNULL(root); 392 EXPECT_NOTNULL(root);
426 EXPECT_EQ(Dart_CObject::kBigint, root->type); 393 EXPECT_EQ(Dart_CObject::kBigint, root->type);
427 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint); 394 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint);
428 CheckEncodeDecodeMessage(root); 395 CheckEncodeDecodeMessage(root);
429 } 396 }
430 397
431 398
432 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { 399 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) {
433 // Write snapshot with object content. 400 // Write snapshot with object content.
434 uint8_t* buffer; 401 uint8_t* buffer;
435 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 402 MessageWriter writer(&buffer, &zone_allocator);
436 writer.WriteObject(bigint.raw()); 403 writer.WriteMessage(bigint);
437 writer.FinalizeBuffer(); 404 intptr_t buffer_len = writer.BytesWritten();
438
439 // Create a snapshot object using the buffer.
440 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
441 405
442 // Read object back from the snapshot. 406 // Read object back from the snapshot.
443 SnapshotReader reader(snapshot, Isolate::Current()); 407 SnapshotReader reader(buffer, buffer_len,
408 Snapshot::kMessage, Isolate::Current());
444 Bigint& serialized_bigint = Bigint::Handle(); 409 Bigint& serialized_bigint = Bigint::Handle();
445 serialized_bigint ^= reader.ReadObject(); 410 serialized_bigint ^= reader.ReadObject();
446 const char *str1 = BigintOperations::ToHexCString(bigint, allocator); 411 const char *str1 = BigintOperations::ToHexCString(bigint, allocator);
447 const char *str2 = 412 const char *str2 =
448 BigintOperations::ToHexCString(serialized_bigint, allocator); 413 BigintOperations::ToHexCString(serialized_bigint, allocator);
449 EXPECT_STREQ(str1, str2); 414 EXPECT_STREQ(str1, str2);
450 free(const_cast<char*>(str1)); 415 free(const_cast<char*>(str1));
451 free(const_cast<char*>(str2)); 416 free(const_cast<char*>(str2));
452 417
453 // Read object back from the snapshot into a C structure. 418 // Read object back from the snapshot into a C structure.
454 ApiNativeScope scope; 419 ApiNativeScope scope;
455 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 420 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
456 writer.BytesWritten(), 421 Dart_CObject* root = api_reader.ReadMessage();
457 &zone_allocator);
458 // Bigint not supported. 422 // Bigint not supported.
459 EXPECT_NOTNULL(root); 423 EXPECT_NOTNULL(root);
460 CheckEncodeDecodeMessage(root); 424 CheckEncodeDecodeMessage(root);
461 return root; 425 return root;
462 } 426 }
463 427
464 428
465 void CheckBigint(const char* bigint_value) { 429 void CheckBigint(const char* bigint_value) {
466 Zone zone(Isolate::Current()); 430 Zone zone(Isolate::Current());
467 431
(...skipping 16 matching lines...) Expand all
484 CheckBigint("0x1"); 448 CheckBigint("0x1");
485 CheckBigint("-0x1"); 449 CheckBigint("-0x1");
486 CheckBigint("0x11111111111111111111"); 450 CheckBigint("0x11111111111111111111");
487 CheckBigint("-0x11111111111111111111"); 451 CheckBigint("-0x11111111111111111111");
488 CheckBigint("0x9876543210987654321098765432109876543210"); 452 CheckBigint("0x9876543210987654321098765432109876543210");
489 CheckBigint("-0x9876543210987654321098765432109876543210"); 453 CheckBigint("-0x9876543210987654321098765432109876543210");
490 } 454 }
491 455
492 456
493 TEST_CASE(SerializeSingletons) { 457 TEST_CASE(SerializeSingletons) {
494 // Write snapshot with object content.
495 uint8_t* buffer; 458 uint8_t* buffer;
496 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); 459 {
497 writer.WriteObject(Object::class_class()); 460 Zone zone(Isolate::Current());
498 writer.WriteObject(Object::null_class()); 461 MessageWriter writer(&buffer, &zone_allocator);
499 writer.WriteObject(Object::type_class()); 462 writer.WriteMessage(Object::Handle(Object::class_class()));
500 writer.WriteObject(Object::type_parameter_class()); 463 SnapshotReader reader(buffer, writer.BytesWritten(),
501 writer.WriteObject(Object::type_arguments_class()); 464 Snapshot::kMessage, Isolate::Current());
502 writer.WriteObject(Object::instantiated_type_arguments_class()); 465 EXPECT(Object::class_class() == reader.ReadObject());
503 writer.WriteObject(Object::function_class()); 466 }
504 writer.WriteObject(Object::field_class()); 467 {
505 writer.WriteObject(Object::token_stream_class()); 468 Zone zone(Isolate::Current());
506 writer.WriteObject(Object::script_class()); 469 MessageWriter writer(&buffer, &zone_allocator);
507 writer.WriteObject(Object::library_class()); 470 writer.WriteMessage(Object::Handle(Object::null_class()));
508 writer.WriteObject(Object::code_class()); 471 SnapshotReader reader(buffer, writer.BytesWritten(),
509 writer.WriteObject(Object::instructions_class()); 472 Snapshot::kMessage, Isolate::Current());
510 writer.WriteObject(Object::pc_descriptors_class()); 473 EXPECT(Object::null_class() == reader.ReadObject());
511 writer.WriteObject(Object::exception_handlers_class()); 474 }
512 writer.WriteObject(Object::context_class()); 475 {
513 writer.WriteObject(Object::context_scope_class()); 476 Zone zone(Isolate::Current());
514 writer.FinalizeBuffer(); 477 MessageWriter writer(&buffer, &zone_allocator);
515 478 writer.WriteMessage(Object::Handle(Object::type_class()));
516 // Create a snapshot object using the buffer. 479 SnapshotReader reader(buffer, writer.BytesWritten(),
517 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 480 Snapshot::kMessage, Isolate::Current());
518 481 EXPECT(Object::type_class() == reader.ReadObject());
519 // Read object back from the snapshot. 482 }
520 SnapshotReader reader(snapshot, Isolate::Current()); 483 {
521 EXPECT(Object::class_class() == reader.ReadObject()); 484 Zone zone(Isolate::Current());
522 EXPECT(Object::null_class() == reader.ReadObject()); 485 MessageWriter writer(&buffer, &zone_allocator);
523 EXPECT(Object::type_class() == reader.ReadObject()); 486 writer.WriteMessage(Object::Handle(Object::type_parameter_class()));
524 EXPECT(Object::type_parameter_class() == reader.ReadObject()); 487 SnapshotReader reader(buffer, writer.BytesWritten(),
525 EXPECT(Object::type_arguments_class() == reader.ReadObject()); 488 Snapshot::kMessage, Isolate::Current());
526 EXPECT(Object::instantiated_type_arguments_class() == reader.ReadObject()); 489 EXPECT(Object::type_parameter_class() == reader.ReadObject());
527 EXPECT(Object::function_class() == reader.ReadObject()); 490 }
528 EXPECT(Object::field_class() == reader.ReadObject()); 491 {
529 EXPECT(Object::token_stream_class() == reader.ReadObject()); 492 Zone zone(Isolate::Current());
530 EXPECT(Object::script_class() == reader.ReadObject()); 493 MessageWriter writer(&buffer, &zone_allocator);
531 EXPECT(Object::library_class() == reader.ReadObject()); 494 writer.WriteMessage(Object::Handle(Object::type_arguments_class()));
532 EXPECT(Object::code_class() == reader.ReadObject()); 495 SnapshotReader reader(buffer, writer.BytesWritten(),
533 EXPECT(Object::instructions_class() == reader.ReadObject()); 496 Snapshot::kMessage, Isolate::Current());
534 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); 497 EXPECT(Object::type_arguments_class() == reader.ReadObject());
535 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); 498 }
536 EXPECT(Object::context_class() == reader.ReadObject()); 499 {
537 EXPECT(Object::context_scope_class() == reader.ReadObject()); 500 Zone zone(Isolate::Current());
538 501 MessageWriter writer(&buffer, &zone_allocator);
539 free(buffer); 502 writer.WriteMessage(
503 Object::Handle(Object::instantiated_type_arguments_class()));
504 SnapshotReader reader(buffer, writer.BytesWritten(),
505 Snapshot::kMessage, Isolate::Current());
506 EXPECT(Object::instantiated_type_arguments_class() == reader.ReadObject());
507 }
508 {
509 Zone zone(Isolate::Current());
510 MessageWriter writer(&buffer, &zone_allocator);
511 writer.WriteMessage(Object::Handle(Object::function_class()));
512 SnapshotReader reader(buffer, writer.BytesWritten(),
513 Snapshot::kMessage, Isolate::Current());
514 EXPECT(Object::function_class() == reader.ReadObject());
515 }
516 {
517 Zone zone(Isolate::Current());
518 MessageWriter writer(&buffer, &zone_allocator);
519 writer.WriteMessage(Object::Handle(Object::field_class()));
520 SnapshotReader reader(buffer, writer.BytesWritten(),
521 Snapshot::kMessage, Isolate::Current());
522 EXPECT(Object::field_class() == reader.ReadObject());
523 }
524 {
525 Zone zone(Isolate::Current());
526 MessageWriter writer(&buffer, &zone_allocator);
527 writer.WriteMessage(Object::Handle(Object::token_stream_class()));
528 SnapshotReader reader(buffer, writer.BytesWritten(),
529 Snapshot::kMessage, Isolate::Current());
530 EXPECT(Object::token_stream_class() == reader.ReadObject());
531 }
532 {
533 Zone zone(Isolate::Current());
534 MessageWriter writer(&buffer, &zone_allocator);
535 writer.WriteMessage(Object::Handle(Object::script_class()));
536 SnapshotReader reader(buffer, writer.BytesWritten(),
537 Snapshot::kMessage, Isolate::Current());
538 EXPECT(Object::script_class() == reader.ReadObject());
539 }
540 {
541 Zone zone(Isolate::Current());
542 MessageWriter writer(&buffer, &zone_allocator);
543 writer.WriteMessage(Object::Handle(Object::library_class()));
544 SnapshotReader reader(buffer, writer.BytesWritten(),
545 Snapshot::kMessage, Isolate::Current());
546 EXPECT(Object::library_class() == reader.ReadObject());
547 }
548 {
549 Zone zone(Isolate::Current());
550 MessageWriter writer(&buffer, &zone_allocator);
551 writer.WriteMessage(Object::Handle(Object::code_class()));
552 SnapshotReader reader(buffer, writer.BytesWritten(),
553 Snapshot::kMessage, Isolate::Current());
554 EXPECT(Object::code_class() == reader.ReadObject());
555 }
556 {
557 Zone zone(Isolate::Current());
558 MessageWriter writer(&buffer, &zone_allocator);
559 writer.WriteMessage(Object::Handle(Object::instructions_class()));
560 SnapshotReader reader(buffer, writer.BytesWritten(),
561 Snapshot::kMessage, Isolate::Current());
562 EXPECT(Object::instructions_class() == reader.ReadObject());
563 }
564 {
565 Zone zone(Isolate::Current());
566 MessageWriter writer(&buffer, &zone_allocator);
567 writer.WriteMessage(Object::Handle(Object::pc_descriptors_class()));
568 SnapshotReader reader(buffer, writer.BytesWritten(),
569 Snapshot::kMessage, Isolate::Current());
570 EXPECT(Object::pc_descriptors_class() == reader.ReadObject());
571 }
572 {
573 Zone zone(Isolate::Current());
574 MessageWriter writer(&buffer, &zone_allocator);
575 writer.WriteMessage(Object::Handle(Object::exception_handlers_class()));
576 SnapshotReader reader(buffer, writer.BytesWritten(),
577 Snapshot::kMessage, Isolate::Current());
578 EXPECT(Object::exception_handlers_class() == reader.ReadObject());
579 }
580 {
581 Zone zone(Isolate::Current());
582 MessageWriter writer(&buffer, &zone_allocator);
583 writer.WriteMessage(Object::Handle(Object::context_class()));
584 SnapshotReader reader(buffer, writer.BytesWritten(),
585 Snapshot::kMessage, Isolate::Current());
586 EXPECT(Object::context_class() == reader.ReadObject());
587 }
588 {
589 Zone zone(Isolate::Current());
590 MessageWriter writer(&buffer, &zone_allocator);
591 writer.WriteMessage(Object::Handle(Object::context_scope_class()));
592 SnapshotReader reader(buffer, writer.BytesWritten(),
593 Snapshot::kMessage, Isolate::Current());
594 EXPECT(Object::context_scope_class() == reader.ReadObject());
595 }
siva 2012/08/27 16:31:17 This has changed the model of the test itself. It
turnidge 2012/08/27 17:43:05 Yes, it has. I didn't think that putting them in
siva 2012/08/27 18:41:03 This test was trying to test both the singleton as
turnidge 2012/08/27 20:09:16 Ok, I've revert it to its original form. On 2012/
540 } 596 }
541 597
542 598
543 TEST_CASE(SerializeString) { 599 TEST_CASE(SerializeString) {
544 Zone zone(Isolate::Current()); 600 Zone zone(Isolate::Current());
545 601
546 // Write snapshot with object content. 602 // Write snapshot with object content.
547 uint8_t* buffer; 603 uint8_t* buffer;
548 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 604 MessageWriter writer(&buffer, &zone_allocator);
549 static const char* cstr = "This string shall be serialized"; 605 static const char* cstr = "This string shall be serialized";
550 String& str = String::Handle(String::New(cstr)); 606 String& str = String::Handle(String::New(cstr));
551 writer.WriteObject(str.raw()); 607 writer.WriteMessage(str);
552 writer.FinalizeBuffer(); 608 intptr_t buffer_len = writer.BytesWritten();
553
554 // Create a snapshot object using the buffer.
555 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
556 609
557 // Read object back from the snapshot. 610 // Read object back from the snapshot.
558 SnapshotReader reader(snapshot, Isolate::Current()); 611 SnapshotReader reader(buffer, buffer_len,
612 Snapshot::kMessage, Isolate::Current());
559 String& serialized_str = String::Handle(); 613 String& serialized_str = String::Handle();
560 serialized_str ^= reader.ReadObject(); 614 serialized_str ^= reader.ReadObject();
561 EXPECT(str.Equals(serialized_str)); 615 EXPECT(str.Equals(serialized_str));
562 616
563 // Read object back from the snapshot into a C structure. 617 // Read object back from the snapshot into a C structure.
564 ApiNativeScope scope; 618 ApiNativeScope scope;
565 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 619 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
566 writer.BytesWritten(), 620 Dart_CObject* root = api_reader.ReadMessage();
567 &zone_allocator);
568 EXPECT_EQ(Dart_CObject::kString, root->type); 621 EXPECT_EQ(Dart_CObject::kString, root->type);
569 EXPECT_STREQ(cstr, root->value.as_string); 622 EXPECT_STREQ(cstr, root->value.as_string);
570 CheckEncodeDecodeMessage(root); 623 CheckEncodeDecodeMessage(root);
571 } 624 }
572 625
573 626
574 TEST_CASE(SerializeArray) { 627 TEST_CASE(SerializeArray) {
575 Zone zone(Isolate::Current()); 628 Zone zone(Isolate::Current());
576 629
577 // Write snapshot with object content. 630 // Write snapshot with object content.
578 uint8_t* buffer; 631 uint8_t* buffer;
579 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 632 MessageWriter writer(&buffer, &zone_allocator);
580 const int kArrayLength = 10; 633 const int kArrayLength = 10;
581 Array& array = Array::Handle(Array::New(kArrayLength)); 634 Array& array = Array::Handle(Array::New(kArrayLength));
582 Smi& smi = Smi::Handle(); 635 Smi& smi = Smi::Handle();
583 for (int i = 0; i < kArrayLength; i++) { 636 for (int i = 0; i < kArrayLength; i++) {
584 smi ^= Smi::New(i); 637 smi ^= Smi::New(i);
585 array.SetAt(i, smi); 638 array.SetAt(i, smi);
586 } 639 }
587 writer.WriteObject(array.raw()); 640 writer.WriteMessage(array);
588 writer.FinalizeBuffer(); 641 intptr_t buffer_len = writer.BytesWritten();
589
590 // Create a snapshot object using the buffer.
591 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
592 642
593 // Read object back from the snapshot. 643 // Read object back from the snapshot.
594 SnapshotReader reader(snapshot, Isolate::Current()); 644 SnapshotReader reader(buffer, buffer_len,
645 Snapshot::kMessage, Isolate::Current());
595 Array& serialized_array = Array::Handle(); 646 Array& serialized_array = Array::Handle();
596 serialized_array ^= reader.ReadObject(); 647 serialized_array ^= reader.ReadObject();
597 EXPECT(array.Equals(serialized_array)); 648 EXPECT(array.Equals(serialized_array));
598 649
599 // Read object back from the snapshot into a C structure. 650 // Read object back from the snapshot into a C structure.
600 ApiNativeScope scope; 651 ApiNativeScope scope;
601 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 652 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
602 writer.BytesWritten(), 653 Dart_CObject* root = api_reader.ReadMessage();
603 &zone_allocator);
604 EXPECT_EQ(Dart_CObject::kArray, root->type); 654 EXPECT_EQ(Dart_CObject::kArray, root->type);
605 EXPECT_EQ(kArrayLength, root->value.as_array.length); 655 EXPECT_EQ(kArrayLength, root->value.as_array.length);
606 for (int i = 0; i < kArrayLength; i++) { 656 for (int i = 0; i < kArrayLength; i++) {
607 Dart_CObject* element = root->value.as_array.values[i]; 657 Dart_CObject* element = root->value.as_array.values[i];
608 EXPECT_EQ(Dart_CObject::kInt32, element->type); 658 EXPECT_EQ(Dart_CObject::kInt32, element->type);
609 EXPECT_EQ(i, element->value.as_int32); 659 EXPECT_EQ(i, element->value.as_int32);
610 } 660 }
611 CheckEncodeDecodeMessage(root); 661 CheckEncodeDecodeMessage(root);
612 } 662 }
613 663
614 664
615 TEST_CASE(SerializeEmptyArray) { 665 TEST_CASE(SerializeEmptyArray) {
616 Zone zone(Isolate::Current()); 666 Zone zone(Isolate::Current());
617 667
618 // Write snapshot with object content. 668 // Write snapshot with object content.
619 uint8_t* buffer; 669 uint8_t* buffer;
620 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 670 MessageWriter writer(&buffer, &zone_allocator);
621 const int kArrayLength = 0; 671 const int kArrayLength = 0;
622 Array& array = Array::Handle(Array::New(kArrayLength)); 672 Array& array = Array::Handle(Array::New(kArrayLength));
623 writer.WriteObject(array.raw()); 673 writer.WriteMessage(array);
624 writer.FinalizeBuffer(); 674 intptr_t buffer_len = writer.BytesWritten();
625
626 // Create a snapshot object using the buffer.
627 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
628 675
629 // Read object back from the snapshot. 676 // Read object back from the snapshot.
630 SnapshotReader reader(snapshot, Isolate::Current()); 677 SnapshotReader reader(buffer, buffer_len,
678 Snapshot::kMessage, Isolate::Current());
631 Array& serialized_array = Array::Handle(); 679 Array& serialized_array = Array::Handle();
632 serialized_array ^= reader.ReadObject(); 680 serialized_array ^= reader.ReadObject();
633 EXPECT(array.Equals(serialized_array)); 681 EXPECT(array.Equals(serialized_array));
634 682
635 // Read object back from the snapshot into a C structure. 683 // Read object back from the snapshot into a C structure.
636 ApiNativeScope scope; 684 ApiNativeScope scope;
637 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 685 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
638 writer.BytesWritten(), 686 Dart_CObject* root = api_reader.ReadMessage();
639 &zone_allocator);
640 EXPECT_EQ(Dart_CObject::kArray, root->type); 687 EXPECT_EQ(Dart_CObject::kArray, root->type);
641 EXPECT_EQ(kArrayLength, root->value.as_array.length); 688 EXPECT_EQ(kArrayLength, root->value.as_array.length);
642 EXPECT(root->value.as_array.values == NULL); 689 EXPECT(root->value.as_array.values == NULL);
643 CheckEncodeDecodeMessage(root); 690 CheckEncodeDecodeMessage(root);
644 } 691 }
645 692
646 693
647 TEST_CASE(SerializeByteArray) { 694 TEST_CASE(SerializeByteArray) {
648 Zone zone(Isolate::Current()); 695 Zone zone(Isolate::Current());
649 696
650 // Write snapshot with object content. 697 // Write snapshot with object content.
651 uint8_t* buffer; 698 uint8_t* buffer;
652 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 699 MessageWriter writer(&buffer, &zone_allocator);
653 const int kByteArrayLength = 256; 700 const int kByteArrayLength = 256;
654 Uint8Array& byte_array = 701 Uint8Array& byte_array =
655 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); 702 Uint8Array::Handle(Uint8Array::New(kByteArrayLength));
656 for (int i = 0; i < kByteArrayLength; i++) { 703 for (int i = 0; i < kByteArrayLength; i++) {
657 byte_array.SetAt(i, i); 704 byte_array.SetAt(i, i);
658 } 705 }
659 writer.WriteObject(byte_array.raw()); 706 writer.WriteMessage(byte_array);
660 writer.FinalizeBuffer(); 707 intptr_t buffer_len = writer.BytesWritten();
661
662 // Create a snapshot object using the buffer.
663 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
664 708
665 // Read object back from the snapshot. 709 // Read object back from the snapshot.
666 SnapshotReader reader(snapshot, Isolate::Current()); 710 SnapshotReader reader(buffer, buffer_len,
711 Snapshot::kMessage, Isolate::Current());
667 ByteArray& serialized_byte_array = ByteArray::Handle(); 712 ByteArray& serialized_byte_array = ByteArray::Handle();
668 serialized_byte_array ^= reader.ReadObject(); 713 serialized_byte_array ^= reader.ReadObject();
669 EXPECT(serialized_byte_array.IsByteArray()); 714 EXPECT(serialized_byte_array.IsByteArray());
670 715
671 // Read object back from the snapshot into a C structure. 716 // Read object back from the snapshot into a C structure.
672 ApiNativeScope scope; 717 ApiNativeScope scope;
673 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 718 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
674 writer.BytesWritten(), 719 Dart_CObject* root = api_reader.ReadMessage();
675 &zone_allocator);
676 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); 720 EXPECT_EQ(Dart_CObject::kUint8Array, root->type);
677 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); 721 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
678 for (int i = 0; i < kByteArrayLength; i++) { 722 for (int i = 0; i < kByteArrayLength; i++) {
679 EXPECT(root->value.as_byte_array.values[i] == i); 723 EXPECT(root->value.as_byte_array.values[i] == i);
680 } 724 }
681 CheckEncodeDecodeMessage(root); 725 CheckEncodeDecodeMessage(root);
682 } 726 }
683 727
684 728
685 TEST_CASE(SerializeEmptyByteArray) { 729 TEST_CASE(SerializeEmptyByteArray) {
686 Zone zone(Isolate::Current()); 730 Zone zone(Isolate::Current());
687 731
688 // Write snapshot with object content. 732 // Write snapshot with object content.
689 uint8_t* buffer; 733 uint8_t* buffer;
690 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 734 MessageWriter writer(&buffer, &zone_allocator);
691 const int kByteArrayLength = 0; 735 const int kByteArrayLength = 0;
692 Uint8Array& byte_array = 736 Uint8Array& byte_array =
693 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); 737 Uint8Array::Handle(Uint8Array::New(kByteArrayLength));
694 writer.WriteObject(byte_array.raw()); 738 writer.WriteMessage(byte_array);
695 writer.FinalizeBuffer(); 739 intptr_t buffer_len = writer.BytesWritten();
696
697 // Create a snapshot object using the buffer.
698 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
699 740
700 // Read object back from the snapshot. 741 // Read object back from the snapshot.
701 SnapshotReader reader(snapshot, Isolate::Current()); 742 SnapshotReader reader(buffer, buffer_len,
743 Snapshot::kMessage, Isolate::Current());
702 ByteArray& serialized_byte_array = ByteArray::Handle(); 744 ByteArray& serialized_byte_array = ByteArray::Handle();
703 serialized_byte_array ^= reader.ReadObject(); 745 serialized_byte_array ^= reader.ReadObject();
704 EXPECT(serialized_byte_array.IsByteArray()); 746 EXPECT(serialized_byte_array.IsByteArray());
705 747
706 // Read object back from the snapshot into a C structure. 748 // Read object back from the snapshot into a C structure.
707 ApiNativeScope scope; 749 ApiNativeScope scope;
708 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 750 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
709 writer.BytesWritten(), 751 Dart_CObject* root = api_reader.ReadMessage();
710 &zone_allocator);
711 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); 752 EXPECT_EQ(Dart_CObject::kUint8Array, root->type);
712 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); 753 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
713 EXPECT(root->value.as_byte_array.values == NULL); 754 EXPECT(root->value.as_byte_array.values == NULL);
714 CheckEncodeDecodeMessage(root); 755 CheckEncodeDecodeMessage(root);
715 } 756 }
716 757
717 758
759 class TestSnapshotWriter : public SnapshotWriter {
760 public:
761 TestSnapshotWriter(uint8_t** buffer, ReAlloc alloc)
762 : SnapshotWriter(Snapshot::kScript, buffer, alloc) {
763 ASSERT(buffer != NULL);
764 ASSERT(alloc != NULL);
765 }
766 ~TestSnapshotWriter() { }
767
768 // Writes just a script object
769 void WriteScript(const Script& script) {
770 WriteObject(script.raw());
771 UnmarkAll();
772 }
773
774 private:
775 DISALLOW_COPY_AND_ASSIGN(TestSnapshotWriter);
776 };
777
778
718 TEST_CASE(SerializeScript) { 779 TEST_CASE(SerializeScript) {
719 const char* kScriptChars = 780 const char* kScriptChars =
720 "class A {\n" 781 "class A {\n"
721 " static bar() { return 42; }\n" 782 " static bar() { return 42; }\n"
722 " static fly() { return 5; }\n" 783 " static fly() { return 5; }\n"
723 " static s1() { return 'this is a string in the source'; }\n" 784 " static s1() { return 'this is a string in the source'; }\n"
724 " static s2() { return 'this is a \"string\" in the source'; }\n" 785 " static s2() { return 'this is a \"string\" in the source'; }\n"
725 " static s3() { return 'this is a \\\'string\\\' in \"the\" source'; }\n" 786 " static s3() { return 'this is a \\\'string\\\' in \"the\" source'; }\n"
726 " static s4() { return 'this \"is\" a \"string\" in \"the\" source'; }\n" 787 " static s4() { return 'this \"is\" a \"string\" in \"the\" source'; }\n"
727 "}\n"; 788 "}\n";
728 789
729 String& url = String::Handle(String::New("dart-test:SerializeScript")); 790 String& url = String::Handle(String::New("dart-test:SerializeScript"));
730 String& source = String::Handle(String::New(kScriptChars)); 791 String& source = String::Handle(String::New(kScriptChars));
731 Script& script = Script::Handle(Script::New(url, 792 Script& script = Script::Handle(Script::New(url,
732 source, 793 source,
733 RawScript::kSourceTag)); 794 RawScript::kSourceTag));
734 const String& lib_url = String::Handle(Symbols::New("TestLib")); 795 const String& lib_url = String::Handle(Symbols::New("TestLib"));
735 Library& lib = Library::Handle(Library::New(lib_url)); 796 Library& lib = Library::Handle(Library::New(lib_url));
736 lib.Register(); 797 lib.Register();
737 EXPECT(CompilerTest::TestCompileScript(lib, script)); 798 EXPECT(CompilerTest::TestCompileScript(lib, script));
738 799
739 // Write snapshot with object content. 800 // Write snapshot with script content.
740 uint8_t* buffer; 801 uint8_t* buffer;
741 SnapshotWriter writer(Snapshot::kScript, &buffer, &malloc_allocator); 802 TestSnapshotWriter writer(&buffer, &malloc_allocator);
742 writer.WriteObject(script.raw()); 803 writer.WriteScript(script);
743 writer.FinalizeBuffer();
744
745 // Create a snapshot object using the buffer.
746 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
747 804
748 // Read object back from the snapshot. 805 // Read object back from the snapshot.
749 SnapshotReader reader(snapshot, Isolate::Current()); 806 SnapshotReader reader(buffer, writer.BytesWritten(),
807 Snapshot::kScript, Isolate::Current());
750 Script& serialized_script = Script::Handle(); 808 Script& serialized_script = Script::Handle();
751 serialized_script ^= reader.ReadObject(); 809 serialized_script ^= reader.ReadObject();
752 810
753 // Check if the serialized script object matches the original script. 811 // Check if the serialized script object matches the original script.
754 String& expected_literal = String::Handle(); 812 String& expected_literal = String::Handle();
755 String& actual_literal = String::Handle(); 813 String& actual_literal = String::Handle();
756 String& str = String::Handle(); 814 String& str = String::Handle();
757 str ^= serialized_script.url(); 815 str ^= serialized_script.url();
758 EXPECT(url.Equals(str)); 816 EXPECT(url.Equals(str));
759 817
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 894
837 // Create a test library and Load up a test script in it. 895 // Create a test library and Load up a test script in it.
838 TestCase::LoadTestScript(kScriptChars, NULL); 896 TestCase::LoadTestScript(kScriptChars, NULL);
839 timer1.Stop(); 897 timer1.Stop();
840 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); 898 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime());
841 899
842 // Write snapshot with object content. 900 // Write snapshot with object content.
843 Isolate* isolate = Isolate::Current(); 901 Isolate* isolate = Isolate::Current();
844 Zone zone(isolate); 902 Zone zone(isolate);
845 HandleScope scope(isolate); 903 HandleScope scope(isolate);
846 SnapshotWriter writer(Snapshot::kFull, &buffer, &malloc_allocator); 904 FullSnapshotWriter writer(&buffer, &malloc_allocator);
847 writer.WriteFullSnapshot(); 905 writer.WriteFullSnapshot();
848 } 906 }
849 907
850 // Now Create another isolate using the snapshot and execute a method 908 // Now Create another isolate using the snapshot and execute a method
851 // from the script. 909 // from the script.
852 Timer timer2(true, "Snapshot_test"); 910 Timer timer2(true, "Snapshot_test");
853 timer2.Start(); 911 timer2.Start();
854 TestCase::CreateTestIsolateFromSnapshot(buffer); 912 TestCase::CreateTestIsolateFromSnapshot(buffer);
855 { 913 {
856 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. 914 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 Zone zone(isolate); 948 Zone zone(isolate);
891 HandleScope scope(isolate); 949 HandleScope scope(isolate);
892 950
893 // Create a test library and Load up a test script in it. 951 // Create a test library and Load up a test script in it.
894 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 952 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
895 ClassFinalizer::FinalizePendingClasses(); 953 ClassFinalizer::FinalizePendingClasses();
896 timer1.Stop(); 954 timer1.Stop();
897 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); 955 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime());
898 956
899 // Write snapshot with object content. 957 // Write snapshot with object content.
900 SnapshotWriter writer(Snapshot::kFull, &buffer, &malloc_allocator); 958 FullSnapshotWriter writer(&buffer, &malloc_allocator);
901 writer.WriteFullSnapshot(); 959 writer.WriteFullSnapshot();
902 960
903 // Invoke a function which returns an object. 961 // Invoke a function which returns an object.
904 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("FieldsTest")); 962 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("FieldsTest"));
905 Dart_Handle result = Dart_Invoke(cls, Dart_NewString("testMain"), 0, NULL); 963 Dart_Handle result = Dart_Invoke(cls, Dart_NewString("testMain"), 0, NULL);
906 EXPECT_VALID(result); 964 EXPECT_VALID(result);
907 } 965 }
908 966
909 // Now Create another isolate using the snapshot and execute a method 967 // Now Create another isolate using the snapshot and execute a method
910 // from the script. 968 // from the script.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 uint8_t* buffer = NULL; 1115 uint8_t* buffer = NULL;
1058 ApiMessageWriter writer(&buffer, &zone_allocator); 1116 ApiMessageWriter writer(&buffer, &zone_allocator);
1059 1117
1060 static const int kArrayLength = 2; 1118 static const int kArrayLength = 2;
1061 intptr_t data[kArrayLength] = {1, 2}; 1119 intptr_t data[kArrayLength] = {1, 2};
1062 int len = kArrayLength; 1120 int len = kArrayLength;
1063 writer.WriteMessage(len, data); 1121 writer.WriteMessage(len, data);
1064 1122
1065 // Read object back from the snapshot into a C structure. 1123 // Read object back from the snapshot into a C structure.
1066 ApiNativeScope scope; 1124 ApiNativeScope scope;
1067 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1125 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator);
1068 writer.BytesWritten(), 1126 Dart_CObject* root = api_reader.ReadMessage();
1069 &zone_allocator);
1070 EXPECT_EQ(Dart_CObject::kArray, root->type); 1127 EXPECT_EQ(Dart_CObject::kArray, root->type);
1071 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1128 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1072 for (int i = 0; i < kArrayLength; i++) { 1129 for (int i = 0; i < kArrayLength; i++) {
1073 Dart_CObject* element = root->value.as_array.values[i]; 1130 Dart_CObject* element = root->value.as_array.values[i];
1074 EXPECT_EQ(Dart_CObject::kInt32, element->type); 1131 EXPECT_EQ(Dart_CObject::kInt32, element->type);
1075 EXPECT_EQ(i + 1, element->value.as_int32); 1132 EXPECT_EQ(i + 1, element->value.as_int32);
1076 } 1133 }
1077 CheckEncodeDecodeMessage(root); 1134 CheckEncodeDecodeMessage(root);
1078 } 1135 }
1079 1136
1080 1137
1081 // Helper function to call a top level Dart function, serialize the 1138 // Helper function to call a top level Dart function, serialize the
1082 // result and deserialize the result into a Dart_CObject structure. 1139 // result and deserialize the result into a Dart_CObject structure.
1083 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib, 1140 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib,
1084 const char* dart_function) { 1141 const char* dart_function) {
1085 Dart_Handle result; 1142 Dart_Handle result;
1086 result = Dart_Invoke(lib, Dart_NewString(dart_function), 0, NULL); 1143 result = Dart_Invoke(lib, Dart_NewString(dart_function), 0, NULL);
1087 EXPECT_VALID(result); 1144 EXPECT_VALID(result);
1088 1145
1089 // Serialize the list into a message. 1146 // Serialize the list into a message.
1090 uint8_t* buffer; 1147 uint8_t* buffer;
1091 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1148 MessageWriter writer(&buffer, &zone_allocator);
1092 const Object& list = Object::Handle(Api::UnwrapHandle(result)); 1149 const Object& list = Object::Handle(Api::UnwrapHandle(result));
1093 writer.WriteObject(list.raw()); 1150 writer.WriteMessage(list);
1094 writer.FinalizeBuffer(); 1151 intptr_t buffer_len = writer.BytesWritten();
1095 1152
1096 // Read object back from the snapshot into a C structure. 1153 // Read object back from the snapshot into a C structure.
1097 return DecodeMessage(buffer + Snapshot::kHeaderSize, 1154 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1098 writer.BytesWritten(), 1155 return api_reader.ReadMessage();
1099 &zone_allocator);
1100 } 1156 }
1101 1157
1102 1158
1103 UNIT_TEST_CASE(DartGeneratedMessages) { 1159 UNIT_TEST_CASE(DartGeneratedMessages) {
1104 static const char* kCustomIsolateScriptChars = 1160 static const char* kCustomIsolateScriptChars =
1105 "getSmi() {\n" 1161 "getSmi() {\n"
1106 " return 42;\n" 1162 " return 42;\n"
1107 "}\n" 1163 "}\n"
1108 "getBigint() {\n" 1164 "getBigint() {\n"
1109 " return -0x424242424242424242424242424242424242;\n" 1165 " return -0x424242424242424242424242424242424242;\n"
(...skipping 23 matching lines...) Expand all
1133 string_result = Dart_Invoke(lib, Dart_NewString("getString"), 0, NULL); 1189 string_result = Dart_Invoke(lib, Dart_NewString("getString"), 0, NULL);
1134 EXPECT_VALID(string_result); 1190 EXPECT_VALID(string_result);
1135 EXPECT(Dart_IsString(string_result)); 1191 EXPECT(Dart_IsString(string_result));
1136 1192
1137 { 1193 {
1138 DARTSCOPE_NOCHECKS(isolate); 1194 DARTSCOPE_NOCHECKS(isolate);
1139 1195
1140 { 1196 {
1141 Zone zone(Isolate::Current()); 1197 Zone zone(Isolate::Current());
1142 uint8_t* buffer; 1198 uint8_t* buffer;
1143 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1199 MessageWriter writer(&buffer, &zone_allocator);
1144 Smi& smi = Smi::Handle(); 1200 Smi& smi = Smi::Handle();
1145 smi ^= Api::UnwrapHandle(smi_result); 1201 smi ^= Api::UnwrapHandle(smi_result);
1146 writer.WriteObject(smi.raw()); 1202 writer.WriteMessage(smi);
1147 writer.FinalizeBuffer(); 1203 intptr_t buffer_len = writer.BytesWritten();
1148 1204
1149 // Read object back from the snapshot into a C structure. 1205 // Read object back from the snapshot into a C structure.
1150 ApiNativeScope scope; 1206 ApiNativeScope scope;
1151 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1207 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1152 writer.BytesWritten(), 1208 Dart_CObject* root = api_reader.ReadMessage();
1153 &zone_allocator);
1154 EXPECT_NOTNULL(root); 1209 EXPECT_NOTNULL(root);
1155 EXPECT_EQ(Dart_CObject::kInt32, root->type); 1210 EXPECT_EQ(Dart_CObject::kInt32, root->type);
1156 EXPECT_EQ(42, root->value.as_int32); 1211 EXPECT_EQ(42, root->value.as_int32);
1157 CheckEncodeDecodeMessage(root); 1212 CheckEncodeDecodeMessage(root);
1158 } 1213 }
1159 { 1214 {
1160 Zone zone(Isolate::Current()); 1215 Zone zone(Isolate::Current());
1161 uint8_t* buffer; 1216 uint8_t* buffer;
1162 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1217 MessageWriter writer(&buffer, &zone_allocator);
1163 Bigint& bigint = Bigint::Handle(); 1218 Bigint& bigint = Bigint::Handle();
1164 bigint ^= Api::UnwrapHandle(bigint_result); 1219 bigint ^= Api::UnwrapHandle(bigint_result);
1165 writer.WriteObject(bigint.raw()); 1220 writer.WriteMessage(bigint);
1166 writer.FinalizeBuffer(); 1221 intptr_t buffer_len = writer.BytesWritten();
1167 1222
1168 // Read object back from the snapshot into a C structure. 1223 // Read object back from the snapshot into a C structure.
1169 ApiNativeScope scope; 1224 ApiNativeScope scope;
1170 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1225 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1171 writer.BytesWritten(), 1226 Dart_CObject* root = api_reader.ReadMessage();
1172 &zone_allocator);
1173 EXPECT_NOTNULL(root); 1227 EXPECT_NOTNULL(root);
1174 EXPECT_EQ(Dart_CObject::kBigint, root->type); 1228 EXPECT_EQ(Dart_CObject::kBigint, root->type);
1175 EXPECT_STREQ("-424242424242424242424242424242424242", 1229 EXPECT_STREQ("-424242424242424242424242424242424242",
1176 root->value.as_bigint); 1230 root->value.as_bigint);
1177 CheckEncodeDecodeMessage(root); 1231 CheckEncodeDecodeMessage(root);
1178 } 1232 }
1179 { 1233 {
1180 Zone zone(Isolate::Current()); 1234 Zone zone(Isolate::Current());
1181 uint8_t* buffer; 1235 uint8_t* buffer;
1182 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 1236 MessageWriter writer(&buffer, &zone_allocator);
1183 String& str = String::Handle(); 1237 String& str = String::Handle();
1184 str ^= Api::UnwrapHandle(string_result); 1238 str ^= Api::UnwrapHandle(string_result);
1185 writer.WriteObject(str.raw()); 1239 writer.WriteMessage(str);
1186 writer.FinalizeBuffer(); 1240 intptr_t buffer_len = writer.BytesWritten();
1187 1241
1188 // Read object back from the snapshot into a C structure. 1242 // Read object back from the snapshot into a C structure.
1189 ApiNativeScope scope; 1243 ApiNativeScope scope;
1190 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1244 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1191 writer.BytesWritten(), 1245 Dart_CObject* root = api_reader.ReadMessage();
1192 &zone_allocator);
1193 EXPECT_NOTNULL(root); 1246 EXPECT_NOTNULL(root);
1194 EXPECT_EQ(Dart_CObject::kString, root->type); 1247 EXPECT_EQ(Dart_CObject::kString, root->type);
1195 EXPECT_STREQ("Hello, world!", root->value.as_string); 1248 EXPECT_STREQ("Hello, world!", root->value.as_string);
1196 CheckEncodeDecodeMessage(root); 1249 CheckEncodeDecodeMessage(root);
1197 } 1250 }
1198 } 1251 }
1199 Dart_ExitScope(); 1252 Dart_ExitScope();
1200 Dart_ShutdownIsolate(); 1253 Dart_ShutdownIsolate();
1201 } 1254 }
1202 1255
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
1562 EXPECT(Dart_ErrorHasException(result)); 1615 EXPECT(Dart_ErrorHasException(result));
1563 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", 1616 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n",
1564 Dart_GetError(result)); 1617 Dart_GetError(result));
1565 1618
1566 Dart_ExitScope(); 1619 Dart_ExitScope();
1567 } 1620 }
1568 1621
1569 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1622 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1570 1623
1571 } // namespace dart 1624 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698