dart-sdk/runtime/vm/allocation_test.cc
Martin Kustermann 9bdd2ff1de [vm] Cleanup unused include, unnecessary fields on [Thread], rename mutator thread, ensure setjmp drains sticky error
Some cleanups factored out of a larger CL (which refactors enter/exit of threads):

  * remove unused `#include "vm/thread_registry.h"`
  * remove unused/unnecessary fields from [Thread] object
  * rename IsMutator() -> IsDartMutator()
  * make tests using setjmp() drain the sticky error
    => to ensure there's no sticky error on isolate shutdown

TEST=ci

Change-Id: I53935e8bd0628ab3768627d6d5e01c3f0e3a57ad
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/296582
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
2023-04-20 17:42:06 +00:00

159 lines
3.7 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/allocation.h"
#include "platform/assert.h"
#include "vm/longjump.h"
#include "vm/unit_test.h"
namespace dart {
class TestValueObject : public ValueObject {
public:
explicit TestValueObject(int* ptr) : ptr_(ptr) {
EXPECT_EQ(1, *ptr_);
*ptr_ = 2;
}
virtual ~TestValueObject() {
EXPECT_EQ(3, *ptr_);
*ptr_ = 4;
}
int value() const { return *ptr_; }
virtual int GetId() const { return 3; }
private:
int* ptr_;
};
class TestStackResource : public StackResource {
public:
explicit TestStackResource(int* ptr)
: StackResource(Thread::Current()), ptr_(ptr) {
EXPECT_EQ(1, *ptr_);
*ptr_ = 2;
}
~TestStackResource() {
EXPECT_EQ(6, *ptr_);
*ptr_ = 7;
}
int value() const { return *ptr_; }
virtual int GetId() const { return 3; }
private:
int* ptr_;
};
class TestStackedStackResource : public StackResource {
public:
explicit TestStackedStackResource(int* ptr)
: StackResource(Thread::Current()), ptr_(ptr) {
EXPECT_EQ(3, *ptr_);
*ptr_ = 4;
}
~TestStackedStackResource() {
EXPECT_EQ(5, *ptr_);
*ptr_ = 6;
}
int value() const { return *ptr_; }
private:
int* ptr_;
};
static void StackAllocatedDestructionHelper(int* ptr) {
TestValueObject stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
}
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction) {
int data = 1;
StackAllocatedDestructionHelper(&data);
EXPECT_EQ(4, data);
}
static void StackAllocatedLongJumpHelper(int* ptr, LongJumpScope* jump) {
TestValueObject stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
const Error& error = Error::Handle(LanguageError::New(
String::Handle(String::New("StackAllocatedLongJump"))));
jump->Jump(1, error);
UNREACHABLE();
}
ISOLATE_UNIT_TEST_CASE(StackAllocatedLongJump) {
LongJumpScope jump;
int data = 1;
if (setjmp(*jump.Set()) == 0) {
StackAllocatedLongJumpHelper(&data, &jump);
UNREACHABLE();
} else {
ASSERT(Error::Handle(thread->StealStickyError()).IsLanguageError());
}
EXPECT_EQ(3, data);
}
static void StackedStackResourceDestructionHelper(int* ptr) {
TestStackedStackResource stacked(ptr);
EXPECT_EQ(4, *ptr);
*ptr = 5;
}
static void StackResourceDestructionHelper(int* ptr) {
TestStackResource stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
StackedStackResourceDestructionHelper(ptr);
EXPECT_EQ(6, *ptr);
// Do not set data because the LongJump version does not return control here.
}
ISOLATE_UNIT_TEST_CASE(StackResourceDestruction) {
int data = 1;
StackResourceDestructionHelper(&data);
EXPECT_EQ(7, data);
}
static void StackedStackResourceLongJumpHelper(int* ptr, LongJumpScope* jump) {
TestStackedStackResource stacked(ptr);
EXPECT_EQ(4, *ptr);
*ptr = 5;
const Error& error = Error::Handle(LanguageError::New(
String::Handle(String::New("StackedStackResourceLongJump"))));
jump->Jump(1, error);
UNREACHABLE();
}
static void StackResourceLongJumpHelper(int* ptr, LongJumpScope* jump) {
TestStackResource stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
StackedStackResourceLongJumpHelper(ptr, jump);
UNREACHABLE();
}
ISOLATE_UNIT_TEST_CASE(StackResourceLongJump) {
LongJumpScope* base = Thread::Current()->long_jump_base();
{
LongJumpScope jump;
int data = 1;
if (setjmp(*jump.Set()) == 0) {
StackResourceLongJumpHelper(&data, &jump);
UNREACHABLE();
} else {
ASSERT(Error::Handle(thread->StealStickyError()).IsLanguageError());
}
EXPECT_EQ(7, data);
}
ASSERT(base == Thread::Current()->long_jump_base());
}
} // namespace dart