|
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | 5 | #include "vm/dart_api_impl.h" |
| 6 | + |
| 7 | +#include <thread> // NOLINT(build/c++11) |
| 8 | + |
6 | 9 | #include "bin/builtin.h" |
7 | 10 | #include "bin/dartutils.h" |
8 | 11 | #include "include/dart_api.h" |
|
19 | 22 | #include "vm/flags.h" |
20 | 23 | #include "vm/heap/verifier.h" |
21 | 24 | #include "vm/lockers.h" |
| 25 | +#include "vm/native_message_handler.h" |
22 | 26 | #include "vm/timeline.h" |
23 | 27 | #include "vm/unit_test.h" |
24 | 28 |
|
@@ -213,6 +217,110 @@ TEST_CASE(Dart_KillIsolatePriority) { |
213 | 217 | EXPECT(interrupted); |
214 | 218 | } |
215 | 219 |
|
| 220 | +TEST_CASE(DartAPI_IsolateOwnership) { |
| 221 | + Dart_Handle lib = TestCase::LoadTestScript("", nullptr); |
| 222 | + EXPECT_VALID(lib); |
| 223 | + |
| 224 | + Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 225 | + |
| 226 | + NativeMessageHandler dummy_handler("test", nullptr, 1); |
| 227 | + Dart_Port dummy_port = PortMap::CreatePort(&dummy_handler); |
| 228 | + |
| 229 | + Dart_Port port = Dart_GetMainPortId(); |
| 230 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 231 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 232 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 233 | + |
| 234 | + Dart_SetCurrentThreadOwnsIsolate(); |
| 235 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 236 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 237 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 238 | + |
| 239 | + Dart_ExitIsolate(); |
| 240 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 241 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 242 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 243 | + |
| 244 | + Dart_Port other_port; |
| 245 | + std::thread([port, dummy_port, isolate, &other_port]() { |
| 246 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 247 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 248 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 249 | + |
| 250 | + char* error = nullptr; |
| 251 | + Dart_CreateIsolateInGroup(isolate, "other", nullptr, nullptr, nullptr, |
| 252 | + &error); |
| 253 | + EXPECT_EQ(nullptr, error); |
| 254 | + other_port = Dart_GetMainPortId(); |
| 255 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(other_port)); |
| 256 | + |
| 257 | + Dart_SetCurrentThreadOwnsIsolate(); |
| 258 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 259 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(other_port)); |
| 260 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 261 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 262 | + |
| 263 | + Dart_ShutdownIsolate(); |
| 264 | + }).join(); |
| 265 | + |
| 266 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 267 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(other_port)); |
| 268 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 269 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 270 | + |
| 271 | + Dart_EnterIsolate(isolate); |
| 272 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 273 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(other_port)); |
| 274 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(dummy_port)); |
| 275 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(ILLEGAL_PORT)); |
| 276 | + |
| 277 | + Dart_ExitIsolate(); |
| 278 | + PortMap::ClosePort(dummy_port); |
| 279 | + Dart_EnterIsolate(isolate); |
| 280 | +} |
| 281 | + |
| 282 | +TEST_CASE_WITH_EXPECTATION(DartAPI_IsolateOwnership_SetWhenAlreadyOwned, |
| 283 | + "Crash") { |
| 284 | + Dart_Handle lib = TestCase::LoadTestScript("", nullptr); |
| 285 | + EXPECT_VALID(lib); |
| 286 | + |
| 287 | + Dart_Port port = Dart_GetMainPortId(); |
| 288 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 289 | + |
| 290 | + Dart_SetCurrentThreadOwnsIsolate(); |
| 291 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 292 | + |
| 293 | + // Causes an assertion failure, because this thread already owns the isolate. |
| 294 | + Dart_SetCurrentThreadOwnsIsolate(); |
| 295 | +} |
| 296 | + |
| 297 | +TEST_CASE_WITH_EXPECTATION( |
| 298 | + DartAPI_IsolateOwnership_EnterIsolateOwnedByOtherThread, |
| 299 | + "Crash") { |
| 300 | + Dart_Handle lib = TestCase::LoadTestScript("", nullptr); |
| 301 | + EXPECT_VALID(lib); |
| 302 | + |
| 303 | + Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 304 | + |
| 305 | + Dart_Port port = Dart_GetMainPortId(); |
| 306 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 307 | + |
| 308 | + Dart_SetCurrentThreadOwnsIsolate(); |
| 309 | + EXPECT_EQ(true, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 310 | + |
| 311 | + Dart_ExitIsolate(); |
| 312 | + |
| 313 | + std::thread([isolate, port]() { |
| 314 | + EXPECT_EQ(false, Dart_GetCurrentThreadOwnsIsolate(port)); |
| 315 | + |
| 316 | + // Causes an assertion failure, because the isolate is already owned by |
| 317 | + // another thread. |
| 318 | + Dart_EnterIsolate(isolate); |
| 319 | + }).join(); |
| 320 | + |
| 321 | + Dart_EnterIsolate(isolate); |
| 322 | +} |
| 323 | + |
216 | 324 | TEST_CASE(DartAPI_ErrorHandleBasics) { |
217 | 325 | const char* kScriptChars = |
218 | 326 | "@pragma('vm:entry-point', 'call')\n" |
|
0 commit comments