Fix various memory leaks in unit tests detected by a new version of ASAN.

R=zra@google.com

Review-Url: https://codereview.chromium.org/2620413002 .
This commit is contained in:
Ryan Macnak 2017-01-11 11:12:40 -08:00
parent 443f2a0672
commit cbf3eaeb90
7 changed files with 40 additions and 11 deletions

View file

@ -96,9 +96,8 @@ void Benchmark::RunAll(const char* executable) {
Dart_Isolate Benchmark::CreateIsolate(const uint8_t* buffer) {
bin::IsolateData* isolate_data = new bin::IsolateData(NULL, NULL, NULL);
char* err = NULL;
isolate_ = Dart_CreateIsolate(NULL, NULL, buffer, NULL, isolate_data, &err);
isolate_ = Dart_CreateIsolate(NULL, NULL, buffer, NULL, NULL, &err);
EXPECT(isolate_ != NULL);
free(err);
return isolate_;
@ -520,6 +519,9 @@ BENCHMARK_SIZE(CoreSnapshotSize) {
const Snapshot* snapshot = Snapshot::SetupFromBuffer(isolate_snapshot_buffer);
ASSERT(snapshot->kind() == Snapshot::kCore);
benchmark->set_score(snapshot->length());
free(vm_isolate_snapshot_buffer);
free(isolate_snapshot_buffer);
}
@ -554,6 +556,9 @@ BENCHMARK_SIZE(StandaloneSnapshotSize) {
const Snapshot* snapshot = Snapshot::SetupFromBuffer(isolate_snapshot_buffer);
ASSERT(snapshot->kind() == Snapshot::kCore);
benchmark->set_score(snapshot->length());
free(vm_isolate_snapshot_buffer);
free(isolate_snapshot_buffer);
}

View file

@ -102,10 +102,13 @@ VM_TEST_CASE(CompileFunctionOnHelperThread) {
ASSERT(isolate->background_compiler() != NULL);
isolate->background_compiler()->CompileOptimized(func);
Monitor* m = new Monitor();
MonitorLocker ml(m);
while (!func.HasOptimizedCode()) {
ml.WaitWithSafepointCheck(thread, 1);
{
MonitorLocker ml(m);
while (!func.HasOptimizedCode()) {
ml.WaitWithSafepointCheck(thread, 1);
}
}
delete m;
BackgroundCompiler::Stop(isolate);
}

View file

@ -405,6 +405,7 @@ UNIT_TEST_CASE(MessageHandler_Run) {
handler_peer.decrement_live_ports();
EXPECT(!handler.HasLivePorts());
PortMap::ClosePorts(&handler);
delete[] ports;
}
} // namespace dart

View file

@ -16,7 +16,9 @@ namespace dart {
// --- Message sending/receiving from native code ---
static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
static uint8_t* malloc_allocator(uint8_t* ptr,
intptr_t old_size,
intptr_t new_size) {
void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
return reinterpret_cast<uint8_t*>(new_ptr);
}
@ -47,10 +49,13 @@ class IsolateSaver {
static bool PostCObjectHelper(Dart_Port port_id, Dart_CObject* message) {
uint8_t* buffer = NULL;
ApiMessageWriter writer(&buffer, allocator);
ApiMessageWriter writer(&buffer, malloc_allocator);
bool success = writer.WriteCMessage(message);
if (!success) return success;
if (!success) {
free(buffer);
return success;
}
// Post the message at the given port.
return PortMap::PostMessage(new Message(

View file

@ -139,6 +139,8 @@ static void CheckEncodeDecodeMessage(Dart_CObject* root) {
// Check that the two messages are the same.
CompareDartCObjects(root, new_root);
free(buffer);
}
@ -147,6 +149,7 @@ static void ExpectEncodeFail(Dart_CObject* root) {
ApiMessageWriter writer(&buffer, &malloc_allocator);
const bool result = writer.WriteCMessage(root);
EXPECT_EQ(false, result);
free(buffer);
}
@ -1653,6 +1656,7 @@ UNIT_TEST_CASE(MismatchedSnapshotKinds) {
EXPECT(isolate == NULL);
EXPECT(error != NULL);
EXPECT_SUBSTRING("got 'script', expected 'core'", error);
free(error);
}
{

View file

@ -91,7 +91,7 @@ class TimelineTestHelper : public AllStatic {
event->Complete();
}
static void Clear(TimelineEventEndlessRecorder* recorder) {
static void Clear(TimelineEventRecorder* recorder) {
ASSERT(recorder != NULL);
recorder->Clear();
}
@ -343,6 +343,8 @@ TEST_CASE(TimelineEventCallbackRecorderBasic) {
EXPECT_EQ(0, recorder->CountFor(TimelineEvent::kAsyncEnd));
event->Complete();
EXPECT_EQ(1, recorder->CountFor(TimelineEvent::kAsyncEnd));
delete recorder;
}
@ -466,6 +468,9 @@ TEST_CASE(TimelineAnalysis_ThreadBlockCount) {
EXPECT(LabelMatch(it.Next(), "F"));
EXPECT(!it.HasNext());
}
TimelineTestHelper::Clear(recorder);
delete recorder;
}
@ -502,6 +507,9 @@ TEST_CASE(TimelineRingRecorderJSONOrder) {
const char* alpha = strstr(js.ToCString(), "Alpha");
const char* beta = strstr(js.ToCString(), "Beta");
EXPECT(alpha < beta);
TimelineTestHelper::Clear(recorder);
delete recorder;
}
@ -674,6 +682,8 @@ TEST_CASE(TimelinePauses_Basic) {
EXPECT_EQ(8, pauses.MaxExclusiveTime("a"));
}
TimelineTestHelper::Clear(recorder);
delete recorder;
}
@ -884,6 +894,8 @@ TEST_CASE(TimelinePauses_BeginEnd) {
EXPECT(pauses.has_error());
}
TimelineTestHelper::Clear(recorder);
delete recorder;
}
#endif // !PRODUCT

View file

@ -52,10 +52,9 @@ void TestCaseBase::RunAll() {
Dart_Isolate TestCase::CreateIsolate(const uint8_t* buffer, const char* name) {
bin::IsolateData* isolate_data = new bin::IsolateData(name, NULL, NULL);
char* err;
Dart_Isolate isolate =
Dart_CreateIsolate(name, NULL, buffer, NULL, isolate_data, &err);
Dart_CreateIsolate(name, NULL, buffer, NULL, NULL, &err);
if (isolate == NULL) {
OS::Print("Creation of isolate failed '%s'\n", err);
free(err);