v8/test/unittests/wasm/string-builder-unittest.cc
Jakob Kummerow 0460e63f33 [wasm] Fix/improve StringBuilder buffer growth
This includes several changes:
- avoid a very-unlikely-but-theoretically-possible OOB write
- avoid a somewhat-likely memory leak
- grow the buffer less aggressively for medium-length strings

Change-Id: I877f43d7e2e7cd4778ba8c7c7525ba988301f750
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3771900
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81808}
2022-07-19 13:02:36 +00:00

44 lines
1.3 KiB
C++

// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/wasm/string-builder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8::internal::wasm {
namespace string_builder_unittest {
TEST(StringBuilder, Simple) {
StringBuilder sb;
sb << "foo"
<< "bar" << -42 << "\n";
EXPECT_STREQ(std::string(sb.start(), sb.length()).c_str(), "foobar-42\n");
}
TEST(StringBuilder, DontLeak) {
// Should be bigger than StringBuilder::kStackSize = 256.
constexpr size_t kMoreThanStackBufferSize = 300;
StringBuilder sb;
const char* on_stack = sb.start();
sb.allocate(kMoreThanStackBufferSize);
const char* on_heap = sb.start();
// If this fails, then kMoreThanStackBufferSize was too small.
ASSERT_NE(on_stack, on_heap);
// Still don't leak on further growth.
sb.allocate(kMoreThanStackBufferSize * 4);
}
TEST(StringBuilder, SuperLongStrings) {
// Should be bigger than StringBuilder::kChunkSize = 1024 * 1024.
constexpr size_t kMoreThanChunkSize = 2 * 1024 * 1024;
StringBuilder sb;
char* s = sb.allocate(kMoreThanChunkSize);
for (size_t i = 0; i < kMoreThanChunkSize; i++) {
s[i] = 'a';
}
}
} // namespace string_builder_unittest
} // namespace v8::internal::wasm