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

Side by Side Diff: src/heap.cc

Issue 347503003: Move invalid string length check to the factory. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: addressed comments Created 6 years, 6 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 | « src/heap.h ('k') | src/heap-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/accessors.h" 7 #include "src/accessors.h"
8 #include "src/api.h" 8 #include "src/api.h"
9 #include "src/base/once.h" 9 #include "src/base/once.h"
10 #include "src/bootstrapper.h" 10 #include "src/bootstrapper.h"
(...skipping 3732 matching lines...) Expand 10 before | Expand all | Expand 10 after
3743 { AllocationResult allocation = CopyFixedArray(properties); 3743 { AllocationResult allocation = CopyFixedArray(properties);
3744 if (!allocation.To(&prop)) return allocation; 3744 if (!allocation.To(&prop)) return allocation;
3745 } 3745 }
3746 JSObject::cast(clone)->set_properties(prop, wb_mode); 3746 JSObject::cast(clone)->set_properties(prop, wb_mode);
3747 } 3747 }
3748 // Return the new clone. 3748 // Return the new clone.
3749 return clone; 3749 return clone;
3750 } 3750 }
3751 3751
3752 3752
3753 AllocationResult Heap::AllocateStringFromUtf8Slow(Vector<const char> string,
3754 int non_ascii_start,
3755 PretenureFlag pretenure) {
3756 // Continue counting the number of characters in the UTF-8 string, starting
3757 // from the first non-ascii character or word.
3758 Access<UnicodeCache::Utf8Decoder>
3759 decoder(isolate_->unicode_cache()->utf8_decoder());
3760 decoder->Reset(string.start() + non_ascii_start,
3761 string.length() - non_ascii_start);
3762 int utf16_length = decoder->Utf16Length();
3763 ASSERT(utf16_length > 0);
3764 // Allocate string.
3765 HeapObject* result;
3766 {
3767 int chars = non_ascii_start + utf16_length;
3768 AllocationResult allocation = AllocateRawTwoByteString(chars, pretenure);
3769 if (!allocation.To(&result) || result->IsException()) {
3770 return allocation;
3771 }
3772 }
3773 // Copy ascii portion.
3774 uint16_t* data = SeqTwoByteString::cast(result)->GetChars();
3775 if (non_ascii_start != 0) {
3776 const char* ascii_data = string.start();
3777 for (int i = 0; i < non_ascii_start; i++) {
3778 *data++ = *ascii_data++;
3779 }
3780 }
3781 // Now write the remainder.
3782 decoder->WriteUtf16(data, utf16_length);
3783 return result;
3784 }
3785
3786
3787 AllocationResult Heap::AllocateStringFromTwoByte(Vector<const uc16> string,
3788 PretenureFlag pretenure) {
3789 // Check if the string is an ASCII string.
3790 HeapObject* result;
3791 int length = string.length();
3792 const uc16* start = string.start();
3793
3794 if (String::IsOneByte(start, length)) {
3795 AllocationResult allocation = AllocateRawOneByteString(length, pretenure);
3796 if (!allocation.To(&result) || result->IsException()) {
3797 return allocation;
3798 }
3799 CopyChars(SeqOneByteString::cast(result)->GetChars(), start, length);
3800 } else { // It's not a one byte string.
3801 AllocationResult allocation = AllocateRawTwoByteString(length, pretenure);
3802 if (!allocation.To(&result) || result->IsException()) {
3803 return allocation;
3804 }
3805 CopyChars(SeqTwoByteString::cast(result)->GetChars(), start, length);
3806 }
3807 return result;
3808 }
3809
3810
3811 static inline void WriteOneByteData(Vector<const char> vector, 3753 static inline void WriteOneByteData(Vector<const char> vector,
3812 uint8_t* chars, 3754 uint8_t* chars,
3813 int len) { 3755 int len) {
3814 // Only works for ascii. 3756 // Only works for ascii.
3815 ASSERT(vector.length() == len); 3757 ASSERT(vector.length() == len);
3816 MemCopy(chars, vector.start(), len); 3758 MemCopy(chars, vector.start(), len);
3817 } 3759 }
3818 3760
3819 static inline void WriteTwoByteData(Vector<const char> vector, 3761 static inline void WriteTwoByteData(Vector<const char> vector,
3820 uint16_t* chars, 3762 uint16_t* chars,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
3857 3799
3858 3800
3859 template<bool is_one_byte, typename T> 3801 template<bool is_one_byte, typename T>
3860 AllocationResult Heap::AllocateInternalizedStringImpl( 3802 AllocationResult Heap::AllocateInternalizedStringImpl(
3861 T t, int chars, uint32_t hash_field) { 3803 T t, int chars, uint32_t hash_field) {
3862 ASSERT(chars >= 0); 3804 ASSERT(chars >= 0);
3863 // Compute map and object size. 3805 // Compute map and object size.
3864 int size; 3806 int size;
3865 Map* map; 3807 Map* map;
3866 3808
3867 if (chars < 0 || chars > String::kMaxLength) { 3809 ASSERT_LE(0, chars);
3868 return isolate()->ThrowInvalidStringLength(); 3810 ASSERT_GE(String::kMaxLength, chars);
3869 }
3870 if (is_one_byte) { 3811 if (is_one_byte) {
3871 map = ascii_internalized_string_map(); 3812 map = ascii_internalized_string_map();
3872 size = SeqOneByteString::SizeFor(chars); 3813 size = SeqOneByteString::SizeFor(chars);
3873 } else { 3814 } else {
3874 map = internalized_string_map(); 3815 map = internalized_string_map();
3875 size = SeqTwoByteString::SizeFor(chars); 3816 size = SeqTwoByteString::SizeFor(chars);
3876 } 3817 }
3877 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, TENURED); 3818 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, TENURED);
3878 3819
3879 // Allocate string. 3820 // Allocate string.
(...skipping 26 matching lines...) Expand all
3906 template 3847 template
3907 AllocationResult Heap::AllocateInternalizedStringImpl<false>( 3848 AllocationResult Heap::AllocateInternalizedStringImpl<false>(
3908 String*, int, uint32_t); 3849 String*, int, uint32_t);
3909 template 3850 template
3910 AllocationResult Heap::AllocateInternalizedStringImpl<false>( 3851 AllocationResult Heap::AllocateInternalizedStringImpl<false>(
3911 Vector<const char>, int, uint32_t); 3852 Vector<const char>, int, uint32_t);
3912 3853
3913 3854
3914 AllocationResult Heap::AllocateRawOneByteString(int length, 3855 AllocationResult Heap::AllocateRawOneByteString(int length,
3915 PretenureFlag pretenure) { 3856 PretenureFlag pretenure) {
3916 if (length < 0 || length > String::kMaxLength) { 3857 ASSERT_LE(0, length);
3917 return isolate()->ThrowInvalidStringLength(); 3858 ASSERT_GE(String::kMaxLength, length);
3918 }
3919 int size = SeqOneByteString::SizeFor(length); 3859 int size = SeqOneByteString::SizeFor(length);
3920 ASSERT(size <= SeqOneByteString::kMaxSize); 3860 ASSERT(size <= SeqOneByteString::kMaxSize);
3921 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, pretenure); 3861 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, pretenure);
3922 3862
3923 HeapObject* result; 3863 HeapObject* result;
3924 { AllocationResult allocation = AllocateRaw(size, space, OLD_DATA_SPACE); 3864 { AllocationResult allocation = AllocateRaw(size, space, OLD_DATA_SPACE);
3925 if (!allocation.To(&result)) return allocation; 3865 if (!allocation.To(&result)) return allocation;
3926 } 3866 }
3927 3867
3928 // Partially initialize the object. 3868 // Partially initialize the object.
3929 result->set_map_no_write_barrier(ascii_string_map()); 3869 result->set_map_no_write_barrier(ascii_string_map());
3930 String::cast(result)->set_length(length); 3870 String::cast(result)->set_length(length);
3931 String::cast(result)->set_hash_field(String::kEmptyHashField); 3871 String::cast(result)->set_hash_field(String::kEmptyHashField);
3932 ASSERT_EQ(size, HeapObject::cast(result)->Size()); 3872 ASSERT_EQ(size, HeapObject::cast(result)->Size());
3933 3873
3934 return result; 3874 return result;
3935 } 3875 }
3936 3876
3937 3877
3938 AllocationResult Heap::AllocateRawTwoByteString(int length, 3878 AllocationResult Heap::AllocateRawTwoByteString(int length,
3939 PretenureFlag pretenure) { 3879 PretenureFlag pretenure) {
3940 if (length < 0 || length > String::kMaxLength) { 3880 ASSERT_LE(0, length);
3941 return isolate()->ThrowInvalidStringLength(); 3881 ASSERT_GE(String::kMaxLength, length);
3942 }
3943 int size = SeqTwoByteString::SizeFor(length); 3882 int size = SeqTwoByteString::SizeFor(length);
3944 ASSERT(size <= SeqTwoByteString::kMaxSize); 3883 ASSERT(size <= SeqTwoByteString::kMaxSize);
3945 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, pretenure); 3884 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, pretenure);
3946 3885
3947 HeapObject* result; 3886 HeapObject* result;
3948 { AllocationResult allocation = AllocateRaw(size, space, OLD_DATA_SPACE); 3887 { AllocationResult allocation = AllocateRaw(size, space, OLD_DATA_SPACE);
3949 if (!allocation.To(&result)) return allocation; 3888 if (!allocation.To(&result)) return allocation;
3950 } 3889 }
3951 3890
3952 // Partially initialize the object. 3891 // Partially initialize the object.
(...skipping 2480 matching lines...) Expand 10 before | Expand all | Expand 10 after
6433 static_cast<int>(object_sizes_last_time_[index])); 6372 static_cast<int>(object_sizes_last_time_[index]));
6434 CODE_AGE_LIST_COMPLETE(ADJUST_LAST_TIME_OBJECT_COUNT) 6373 CODE_AGE_LIST_COMPLETE(ADJUST_LAST_TIME_OBJECT_COUNT)
6435 #undef ADJUST_LAST_TIME_OBJECT_COUNT 6374 #undef ADJUST_LAST_TIME_OBJECT_COUNT
6436 6375
6437 MemCopy(object_counts_last_time_, object_counts_, sizeof(object_counts_)); 6376 MemCopy(object_counts_last_time_, object_counts_, sizeof(object_counts_));
6438 MemCopy(object_sizes_last_time_, object_sizes_, sizeof(object_sizes_)); 6377 MemCopy(object_sizes_last_time_, object_sizes_, sizeof(object_sizes_));
6439 ClearObjectStats(); 6378 ClearObjectStats();
6440 } 6379 }
6441 6380
6442 } } // namespace v8::internal 6381 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/heap.h ('k') | src/heap-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698