Commit 185389fa authored by Leszek Swirski's avatar Leszek Swirski Committed by Commit Bot

[string] Fix casting around external internalized string thinning

When an external string matches and internalized external string, we
want to deduplicate them. As part of that de-duplication, we check if
either a) the internalized string's resource is null (it's freshly
created because the original string was in new space) or b) they point
to the same data (in which case we don't want to dispose of the data).

However, when doing these checks we cast both the initial and the
internalized external string to the same type (one or two byte). So,
if a two-byte string finds a one-byte internalized string with
equivalent data, this cast will fail.

Since we only care about the external string resource being null or
equal to another during the above deduplication, the solution is
casting first to the more general ExternalString type, comparing
resources by address, and only casting to the more specific type
when needed (and we know that the types have to match by other
construction).

Change-Id: Id34a02eb1900d8aa492c030488afaffd0d035454
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2315987
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Auto-Submit: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69024}
parent c3e65121
...@@ -72,19 +72,19 @@ Handle<String> String::SlowFlatten(Isolate* isolate, Handle<ConsString> cons, ...@@ -72,19 +72,19 @@ Handle<String> String::SlowFlatten(Isolate* isolate, Handle<ConsString> cons,
namespace { namespace {
template <class StringClass> template <class StringClass>
void MigrateExternalStringResource(Isolate* isolate, String from, String to) { void MigrateExternalStringResource(Isolate* isolate, ExternalString from,
StringClass to) {
Address to_resource_address = to.resource_as_address();
if (to_resource_address == kNullAddress) {
StringClass cast_from = StringClass::cast(from); StringClass cast_from = StringClass::cast(from);
StringClass cast_to = StringClass::cast(to);
const typename StringClass::Resource* to_resource = cast_to.resource();
if (to_resource == nullptr) {
// |to| is a just-created internalized copy of |from|. Migrate the resource. // |to| is a just-created internalized copy of |from|. Migrate the resource.
cast_to.SetResource(isolate, cast_from.resource()); to.SetResource(isolate, cast_from.resource());
// Zap |from|'s resource pointer to reflect the fact that |from| has // Zap |from|'s resource pointer to reflect the fact that |from| has
// relinquished ownership of its resource. // relinquished ownership of its resource.
isolate->heap()->UpdateExternalString( isolate->heap()->UpdateExternalString(
from, ExternalString::cast(from).ExternalPayloadSize(), 0); from, ExternalString::cast(from).ExternalPayloadSize(), 0);
cast_from.SetResource(isolate, nullptr); cast_from.SetResource(isolate, nullptr);
} else if (to_resource != cast_from.resource()) { } else if (to_resource_address != from.resource_as_address()) {
// |to| already existed and has its own resource. Finalize |from|. // |to| already existed and has its own resource. Finalize |from|.
isolate->heap()->FinalizeExternalString(from); isolate->heap()->FinalizeExternalString(from);
} }
...@@ -99,11 +99,11 @@ void String::MakeThin(Isolate* isolate, String internalized) { ...@@ -99,11 +99,11 @@ void String::MakeThin(Isolate* isolate, String internalized) {
if (this->IsExternalString()) { if (this->IsExternalString()) {
if (internalized.IsExternalOneByteString()) { if (internalized.IsExternalOneByteString()) {
MigrateExternalStringResource<ExternalOneByteString>(isolate, *this, MigrateExternalStringResource(isolate, ExternalString::cast(*this),
internalized); ExternalOneByteString::cast(internalized));
} else if (internalized.IsExternalTwoByteString()) { } else if (internalized.IsExternalTwoByteString()) {
MigrateExternalStringResource<ExternalTwoByteString>(isolate, *this, MigrateExternalStringResource(isolate, ExternalString::cast(*this),
internalized); ExternalTwoByteString::cast(internalized));
} else { } else {
// If the external string is duped into an existing non-external // If the external string is duped into an existing non-external
// internalized string, free its resource (it's about to be rewritten // internalized string, free its resource (it's about to be rewritten
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment