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

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

Issue 2449013003: Don't allocate on the Dart heap during Bigint::ToCString(). (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « runtime/vm/object.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 "platform/globals.h" 5 #include "platform/globals.h"
6 6
7 #include "include/dart_tools_api.h" 7 #include "include/dart_tools_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/class_finalizer.h" 9 #include "vm/class_finalizer.h"
10 #include "vm/clustered_snapshot.h" 10 #include "vm/clustered_snapshot.h"
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 ApiNativeScope scope; 370 ApiNativeScope scope;
371 ApiMessageReader api_reader(buffer, buffer_len); 371 ApiMessageReader api_reader(buffer, buffer_len);
372 Dart_CObject* root = api_reader.ReadMessage(); 372 Dart_CObject* root = api_reader.ReadMessage();
373 EXPECT_NOTNULL(root); 373 EXPECT_NOTNULL(root);
374 EXPECT_EQ(Dart_CObject_kBool, root->type); 374 EXPECT_EQ(Dart_CObject_kBool, root->type);
375 EXPECT_EQ(false, root->value.as_bool); 375 EXPECT_EQ(false, root->value.as_bool);
376 CheckEncodeDecodeMessage(root); 376 CheckEncodeDecodeMessage(root);
377 } 377 }
378 378
379 379
380 static uword allocator(intptr_t size) {
381 return reinterpret_cast<uword>(malloc(size));
382 }
383
384
385 TEST_CASE(SerializeCapability) { 380 TEST_CASE(SerializeCapability) {
386 // Write snapshot with object content. 381 // Write snapshot with object content.
387 const Capability& capability = Capability::Handle(Capability::New(12345)); 382 const Capability& capability = Capability::Handle(Capability::New(12345));
388 uint8_t* buffer; 383 uint8_t* buffer;
389 MessageWriter writer(&buffer, &zone_allocator, true); 384 MessageWriter writer(&buffer, &zone_allocator, true);
390 writer.WriteMessage(capability); 385 writer.WriteMessage(capability);
391 intptr_t buffer_len = writer.BytesWritten(); 386 intptr_t buffer_len = writer.BytesWritten();
392 387
393 // Read object back from the snapshot. 388 // Read object back from the snapshot.
394 MessageSnapshotReader reader(buffer, buffer_len, thread); 389 MessageSnapshotReader reader(buffer, buffer_len, thread);
(...skipping 23 matching lines...) Expand all
418 uint8_t* buffer; 413 uint8_t* buffer;
419 MessageWriter writer(&buffer, &zone_allocator, true); 414 MessageWriter writer(&buffer, &zone_allocator, true);
420 writer.WriteMessage(bigint); 415 writer.WriteMessage(bigint);
421 intptr_t buffer_len = writer.BytesWritten(); 416 intptr_t buffer_len = writer.BytesWritten();
422 417
423 // Read object back from the snapshot. 418 // Read object back from the snapshot.
424 MessageSnapshotReader reader(buffer, buffer_len, thread); 419 MessageSnapshotReader reader(buffer, buffer_len, thread);
425 Bigint& obj = Bigint::Handle(); 420 Bigint& obj = Bigint::Handle();
426 obj ^= reader.ReadObject(); 421 obj ^= reader.ReadObject();
427 422
428 EXPECT_STREQ(bigint.ToHexCString(allocator), obj.ToHexCString(allocator)); 423 Zone* zone = Thread::Current()->zone();
424 EXPECT_STREQ(bigint.ToHexCString(zone), obj.ToHexCString(zone));
429 425
430 // Read object back from the snapshot into a C structure. 426 // Read object back from the snapshot into a C structure.
431 ApiNativeScope scope; 427 ApiNativeScope scope;
432 ApiMessageReader api_reader(buffer, buffer_len); 428 ApiMessageReader api_reader(buffer, buffer_len);
433 Dart_CObject* root = api_reader.ReadMessage(); 429 Dart_CObject* root = api_reader.ReadMessage();
434 EXPECT_NOTNULL(root); 430 EXPECT_NOTNULL(root);
435 EXPECT_EQ(Dart_CObject_kBigint, root->type); 431 EXPECT_EQ(Dart_CObject_kBigint, root->type);
436 char* hex_value = TestCase::BigintToHexValue(root); 432 char* hex_value = TestCase::BigintToHexValue(root);
437 EXPECT_STREQ(cstr, hex_value); 433 EXPECT_STREQ(cstr, hex_value);
438 free(hex_value); 434 free(hex_value);
439 CheckEncodeDecodeMessage(root); 435 CheckEncodeDecodeMessage(root);
440 } 436 }
441 437
442 438
443 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { 439 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) {
444 // Write snapshot with object content. 440 // Write snapshot with object content.
445 uint8_t* buffer; 441 uint8_t* buffer;
446 MessageWriter writer(&buffer, &zone_allocator, true); 442 MessageWriter writer(&buffer, &zone_allocator, true);
447 writer.WriteMessage(bigint); 443 writer.WriteMessage(bigint);
448 intptr_t buffer_len = writer.BytesWritten(); 444 intptr_t buffer_len = writer.BytesWritten();
449 445
450 { 446 {
451 // Switch to a regular zone, where VM handle allocation is allowed. 447 // Switch to a regular zone, where VM handle allocation is allowed.
452 Thread* thread = Thread::Current(); 448 Thread* thread = Thread::Current();
453 StackZone zone(thread); 449 StackZone zone(thread);
454 // Read object back from the snapshot. 450 // Read object back from the snapshot.
455 MessageSnapshotReader reader(buffer, buffer_len, thread); 451 MessageSnapshotReader reader(buffer, buffer_len, thread);
456 Bigint& serialized_bigint = Bigint::Handle(); 452 Bigint& serialized_bigint = Bigint::Handle();
457 serialized_bigint ^= reader.ReadObject(); 453 serialized_bigint ^= reader.ReadObject();
458 const char* str1 = bigint.ToHexCString(allocator); 454 const char* str1 = bigint.ToHexCString(thread->zone());
459 const char* str2 = serialized_bigint.ToHexCString(allocator); 455 const char* str2 = serialized_bigint.ToHexCString(thread->zone());
460 EXPECT_STREQ(str1, str2); 456 EXPECT_STREQ(str1, str2);
461 free(const_cast<char*>(str1));
462 free(const_cast<char*>(str2));
463 } 457 }
464 458
465 // Read object back from the snapshot into a C structure. 459 // Read object back from the snapshot into a C structure.
466 ApiMessageReader api_reader(buffer, buffer_len); 460 ApiMessageReader api_reader(buffer, buffer_len);
467 Dart_CObject* root = api_reader.ReadMessage(); 461 Dart_CObject* root = api_reader.ReadMessage();
468 // Bigint not supported. 462 // Bigint not supported.
469 EXPECT_NOTNULL(root); 463 EXPECT_NOTNULL(root);
470 CheckEncodeDecodeMessage(root); 464 CheckEncodeDecodeMessage(root);
471 return root; 465 return root;
472 } 466 }
(...skipping 2545 matching lines...) Expand 10 before | Expand all | Expand 10 after
3018 StackZone zone(Thread::Current()); 3012 StackZone zone(Thread::Current());
3019 uint8_t* buffer; 3013 uint8_t* buffer;
3020 MessageWriter writer(&buffer, &zone_allocator, true); 3014 MessageWriter writer(&buffer, &zone_allocator, true);
3021 writer.WriteInlinedObjectHeader(kOmittedObjectId); 3015 writer.WriteInlinedObjectHeader(kOmittedObjectId);
3022 // For performance, we'd like single-byte headers when ids are omitted. 3016 // For performance, we'd like single-byte headers when ids are omitted.
3023 // If this starts failing, consider renumbering the snapshot ids. 3017 // If this starts failing, consider renumbering the snapshot ids.
3024 EXPECT_EQ(1, writer.BytesWritten()); 3018 EXPECT_EQ(1, writer.BytesWritten());
3025 } 3019 }
3026 3020
3027 } // namespace dart 3021 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698