Reviewers: Michael Starzinger,

Message:
PTAL.

This makes sure that ASCII strings only have characters in the range 0-127.

Description:
Ensure integrity of ASCII strings.


BUG=v8:2128
TEST=


Please review this at http://codereview.chromium.org/10407090/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/d8.h
  M src/d8.cc
  M src/heap-inl.h
  M src/heap.cc
  M src/objects-debug.cc
  M src/objects.h


Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index ddd4100fa8209cb9d6075c466229a05eaa869ecd..3f0b2245f4e00e002dd7ab086c57a76ed2c13f7b 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -834,8 +834,6 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() {
   global_template->Set(String::New("print"), FunctionTemplate::New(Print));
   global_template->Set(String::New("write"), FunctionTemplate::New(Write));
   global_template->Set(String::New("read"), FunctionTemplate::New(Read));
-  global_template->Set(String::New("readbinary"),
-                       FunctionTemplate::New(ReadBinary));
   global_template->Set(String::New("readbuffer"),
                        FunctionTemplate::New(ReadBuffer));
   global_template->Set(String::New("readline"),
@@ -1056,23 +1054,6 @@ static char* ReadChars(const char* name, int* size_out) {
 }


-Handle<Value> Shell::ReadBinary(const Arguments& args) {
-  String::Utf8Value filename(args[0]);
-  int size;
-  if (*filename == NULL) {
-    return ThrowException(String::New("Error loading file"));
-  }
-  char* chars = ReadChars(*filename, &size);
-  if (chars == NULL) {
-    return ThrowException(String::New("Error reading file"));
-  }
-  // We skip checking the string for UTF8 characters and use it raw as
-  // backing store for the external string with 8-bit characters.
-  BinaryResource* resource = new BinaryResource(chars, size);
-  return String::NewExternal(resource);
-}
-
-
 Handle<Value> Shell::ReadBuffer(const Arguments& args) {
   String::Utf8Value filename(args[0]);
   int length;
Index: src/d8.h
diff --git a/src/d8.h b/src/d8.h
index 23fdebcaf4043c018e49fc95d183ebe19c89cef8..b315086fcfdb762e86176832744c8c999c71e06b 100644
--- a/src/d8.h
+++ b/src/d8.h
@@ -307,7 +307,6 @@ class Shell : public i::AllStatic {
   static Handle<Value> EnableProfiler(const Arguments& args);
   static Handle<Value> DisableProfiler(const Arguments& args);
   static Handle<Value> Read(const Arguments& args);
-  static Handle<Value> ReadBinary(const Arguments& args);
   static Handle<Value> ReadBuffer(const Arguments& args);
   static Handle<String> ReadFromStdin();
   static Handle<Value> ReadLine(const Arguments& args) {
Index: src/heap-inl.h
diff --git a/src/heap-inl.h b/src/heap-inl.h
index e12895a28348e011bf0bdf9cddc6c4e06f94d7cb..6c0baedd0b74bee1cd0b186a0f3f2f7189a38ebc 100644
--- a/src/heap-inl.h
+++ b/src/heap-inl.h
@@ -595,12 +595,24 @@ void ExternalStringTable::Iterate(ObjectVisitor* v) {
 void ExternalStringTable::Verify() {
 #ifdef DEBUG
   for (int i = 0; i < new_space_strings_.length(); ++i) {
-    ASSERT(heap_->InNewSpace(new_space_strings_[i]));
-    ASSERT(new_space_strings_[i] != HEAP->raw_unchecked_the_hole_value());
+    Object* obj = Object::cast(new_space_strings_[i]);
+    // TODO(yangguo): check that the object is indeed an external string.
+    ASSERT(heap_->InNewSpace(obj));
+    ASSERT(obj != HEAP->raw_unchecked_the_hole_value());
+    if (obj->IsExternalAsciiString()) {
+      ExternalAsciiString* string = ExternalAsciiString::cast(obj);
+      ASSERT(String::IsAscii(string->GetChars(), string->length()));
+    }
   }
   for (int i = 0; i < old_space_strings_.length(); ++i) {
-    ASSERT(!heap_->InNewSpace(old_space_strings_[i]));
-    ASSERT(old_space_strings_[i] != HEAP->raw_unchecked_the_hole_value());
+    Object* obj = Object::cast(new_space_strings_[i]);
+    // TODO(yangguo): check that the object is indeed an external string.
+    ASSERT(!heap_->InNewSpace(obj));
+    ASSERT(obj != HEAP->raw_unchecked_the_hole_value());
+    if (obj->IsExternalAsciiString()) {
+      ExternalAsciiString* string = ExternalAsciiString::cast(obj);
+      ASSERT(String::IsAscii(string->GetChars(), string->length()));
+    }
   }
 #endif
 }
Index: src/heap.cc
diff --git a/src/heap.cc b/src/heap.cc
index d3c7f0a9b01ef48cc751b8e392cc06f277b7d754..c311b9a5675ef017d8cbcbe733a7daac9da245e7 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -3326,6 +3326,8 @@ MaybeObject* Heap::AllocateExternalStringFromAscii(
     return Failure::OutOfMemoryException();
   }

+  ASSERT(String::IsAscii(resource->data(), length));
+
   Map* map = external_ascii_string_map();
   Object* result;
   { MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
@@ -4490,6 +4492,18 @@ MaybeObject* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) {
   String::cast(result)->set_length(length);
   String::cast(result)->set_hash_field(String::kEmptyHashField);
   ASSERT_EQ(size, HeapObject::cast(result)->Size());
+
+#ifdef DEBUG
+  if (FLAG_verify_heap) {
+ // Initialize string's content to ensure ASCII-ness (character range 0-127)
+    // as required when verifying the heap.
+    char* dest = SeqAsciiString::cast(result)->GetChars();
+    for (int i = 0; i < length; i++) {
+      dest[i] = 0x0F;
+    }
+  }
+#endif  // DEBUG
+
   return result;
 }

Index: src/objects-debug.cc
diff --git a/src/objects-debug.cc b/src/objects-debug.cc
index 9006abde1b88114f9ab57f8554993d5b042f2bf4..89b25bdbba10ffca3f6cce8da465ebbe072d854a 100644
--- a/src/objects-debug.cc
+++ b/src/objects-debug.cc
@@ -458,10 +458,17 @@ void String::StringVerify() {
     ConsString::cast(this)->ConsStringVerify();
   } else if (IsSlicedString()) {
     SlicedString::cast(this)->SlicedStringVerify();
+  } else if (IsSeqAsciiString()) {
+    SeqAsciiString::cast(this)->SeqAsciiStringVerify();
   }
 }


+void SeqAsciiString::SeqAsciiStringVerify() {
+  CHECK(String::IsAscii(GetChars(), length()));
+}
+
+
 void ConsString::ConsStringVerify() {
   CHECK(this->first()->IsString());
   CHECK(this->second() == GetHeap()->empty_string() ||
Index: src/objects.h
diff --git a/src/objects.h b/src/objects.h
index 4fd29ad56227d368d2ebb0773c566f6cba3909b0..421a5314b017294188aaa836293f7e36c9d3e020 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -7242,6 +7242,10 @@ class SeqAsciiString: public SeqString {
                                                       unsigned* offset,
                                                       unsigned chars);

+#ifdef DEBUG
+  void SeqAsciiStringVerify();
+#endif
+
  private:
   DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
 };


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to