Always make a copy of a string when adding it to StringsStorage

Otherwise the string passed as const char* may be disposed and we will end up with a dangling pointer.

Also changed StringsStorage::GetCopy so that a copy is not created if the string is already in the cache.

BUG=None
R=alph@chromium.org, svenpanne@chromium.org

Review URL: https://codereview.chromium.org/27627006

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17260 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
yurys@chromium.org 2013-10-18 08:56:14 +00:00
parent fd45ddcc15
commit ed7dea41a9
3 changed files with 54 additions and 37 deletions

View File

@ -33,16 +33,6 @@
namespace v8 { namespace v8 {
namespace internal { namespace internal {
const char* StringsStorage::GetFunctionName(Name* name) {
return GetFunctionName(GetName(name));
}
const char* StringsStorage::GetFunctionName(const char* name) {
return strlen(name) > 0 ? name : ProfileGenerator::kAnonymousFunctionName;
}
CodeEntry::CodeEntry(Logger::LogEventsAndTags tag, CodeEntry::CodeEntry(Logger::LogEventsAndTags tag,
const char* name, const char* name,
const char* name_prefix, const char* name_prefix,

View File

@ -41,6 +41,12 @@ namespace v8 {
namespace internal { namespace internal {
bool StringsStorage::StringsMatch(void* key1, void* key2) {
return strcmp(reinterpret_cast<char*>(key1),
reinterpret_cast<char*>(key2)) == 0;
}
StringsStorage::StringsStorage(Heap* heap) StringsStorage::StringsStorage(Heap* heap)
: hash_seed_(heap->HashSeed()), names_(StringsMatch) { : hash_seed_(heap->HashSeed()), names_(StringsMatch) {
} }
@ -57,12 +63,15 @@ StringsStorage::~StringsStorage() {
const char* StringsStorage::GetCopy(const char* src) { const char* StringsStorage::GetCopy(const char* src) {
int len = static_cast<int>(strlen(src)); int len = static_cast<int>(strlen(src));
Vector<char> dst = Vector<char>::New(len + 1); HashMap::Entry* entry = GetEntry(src, len);
OS::StrNCpy(dst, src, len); if (entry->value == NULL) {
dst[len] = '\0'; Vector<char> dst = Vector<char>::New(len + 1);
uint32_t hash = OS::StrNCpy(dst, src, len);
StringHasher::HashSequentialString(dst.start(), len, hash_seed_); dst[len] = '\0';
return AddOrDisposeString(dst.start(), hash); entry->key = dst.start();
entry->value = entry->key;
}
return reinterpret_cast<const char*>(entry->value);
} }
@ -75,15 +84,16 @@ const char* StringsStorage::GetFormatted(const char* format, ...) {
} }
const char* StringsStorage::AddOrDisposeString(char* str, uint32_t hash) { const char* StringsStorage::AddOrDisposeString(char* str, int len) {
HashMap::Entry* cache_entry = names_.Lookup(str, hash, true); HashMap::Entry* entry = GetEntry(str, len);
if (cache_entry->value == NULL) { if (entry->value == NULL) {
// New entry added. // New entry added.
cache_entry->value = str; entry->key = str;
entry->value = str;
} else { } else {
DeleteArray(str); DeleteArray(str);
} }
return reinterpret_cast<const char*>(cache_entry->value); return reinterpret_cast<const char*>(entry->value);
} }
@ -92,11 +102,9 @@ const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
int len = OS::VSNPrintF(str, format, args); int len = OS::VSNPrintF(str, format, args);
if (len == -1) { if (len == -1) {
DeleteArray(str.start()); DeleteArray(str.start());
return format; return GetCopy(format);
} }
uint32_t hash = StringHasher::HashSequentialString( return AddOrDisposeString(str.start(), len);
str.start(), len, hash_seed_);
return AddOrDisposeString(str.start(), hash);
} }
@ -104,11 +112,11 @@ const char* StringsStorage::GetName(Name* name) {
if (name->IsString()) { if (name->IsString()) {
String* str = String::cast(name); String* str = String::cast(name);
int length = Min(kMaxNameSize, str->length()); int length = Min(kMaxNameSize, str->length());
int actual_length = 0;
SmartArrayPointer<char> data = SmartArrayPointer<char> data =
str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length); str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length,
uint32_t hash = StringHasher::HashSequentialString( &actual_length);
*data, length, name->GetHeap()->HashSeed()); return AddOrDisposeString(data.Detach(), actual_length);
return AddOrDisposeString(data.Detach(), hash);
} else if (name->IsSymbol()) { } else if (name->IsSymbol()) {
return "<symbol>"; return "<symbol>";
} }
@ -121,6 +129,21 @@ const char* StringsStorage::GetName(int index) {
} }
const char* StringsStorage::GetFunctionName(Name* name) {
return BeautifyFunctionName(GetName(name));
}
const char* StringsStorage::GetFunctionName(const char* name) {
return BeautifyFunctionName(GetCopy(name));
}
const char* StringsStorage::BeautifyFunctionName(const char* name) {
return (*name == 0) ? ProfileGenerator::kAnonymousFunctionName : name;
}
size_t StringsStorage::GetUsedMemorySize() const { size_t StringsStorage::GetUsedMemorySize() const {
size_t size = sizeof(*this); size_t size = sizeof(*this);
size += sizeof(HashMap::Entry) * names_.capacity(); size += sizeof(HashMap::Entry) * names_.capacity();
@ -131,6 +154,12 @@ size_t StringsStorage::GetUsedMemorySize() const {
} }
HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
return names_.Lookup(const_cast<char*>(str), hash, true);
}
const char* const CodeEntry::kEmptyNamePrefix = ""; const char* const CodeEntry::kEmptyNamePrefix = "";
const char* const CodeEntry::kEmptyResourceName = ""; const char* const CodeEntry::kEmptyResourceName = "";
const char* const CodeEntry::kEmptyBailoutReason = ""; const char* const CodeEntry::kEmptyBailoutReason = "";

View File

@ -49,20 +49,18 @@ class StringsStorage {
const char* GetVFormatted(const char* format, va_list args); const char* GetVFormatted(const char* format, va_list args);
const char* GetName(Name* name); const char* GetName(Name* name);
const char* GetName(int index); const char* GetName(int index);
inline const char* GetFunctionName(Name* name); const char* GetFunctionName(Name* name);
inline const char* GetFunctionName(const char* name); const char* GetFunctionName(const char* name);
size_t GetUsedMemorySize() const; size_t GetUsedMemorySize() const;
private: private:
static const int kMaxNameSize = 1024; static const int kMaxNameSize = 1024;
static bool StringsMatch(void* key1, void* key2) { static bool StringsMatch(void* key1, void* key2);
return strcmp(reinterpret_cast<char*>(key1), const char* BeautifyFunctionName(const char* name);
reinterpret_cast<char*>(key2)) == 0; const char* AddOrDisposeString(char* str, int len);
} HashMap::Entry* GetEntry(const char* str, int len);
const char* AddOrDisposeString(char* str, uint32_t hash);
// Mapping of strings by String::Hash to const char* strings.
uint32_t hash_seed_; uint32_t hash_seed_;
HashMap names_; HashMap names_;