1// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "include/dart_api.h"
6
7#include "platform/globals.h"
8
9#include "vm/compiler/backend/slot.h"
10#include "vm/compiler/compiler_state.h"
11#include "vm/object.h"
12#include "vm/parser.h"
13#include "vm/symbols.h"
14#include "vm/unit_test.h"
15
16namespace dart {
17
18// This is a regression test for b/121271056: there might be a race between
19// background compiler and mutator where mutator changes guarded state of
20// the field after Slot was created from it. A situation is possible where we
21// have a clone of a field with its guarded state set to unknown, however
22// Slot::Get for this field returns a Slot created from the previous clone of
23// the same field with a known guarded state. In this case we must add *old*
24// clone from which the Slot was created to guarded fields and not the new
25// clone, because new clone has no guarded state to begin with and thus
26// ParsedFunction::AddToGuardedFields(...) would simply ignore it.
27// Such slots with inconsistent guarded state that are not in the current
28// list of guarded fields arise due to unsuccessful inlining attempts.
29// If we built and discard the graph, then guarded fields associated with
30// that graph are also discarded. However the slot itself stays behind in
31// the global cache.
32// Adding old clone would lead to correct rejection of the compilation
33// attempt because Slot type information is different from the current guarded
34// state of the field.
35TEST_CASE(SlotFromGuardedField) {
36 if (!FLAG_use_field_guards) {
37 return;
38 }
39
40 TransitionNativeToVM transition(thread);
41 Zone* zone = thread->zone();
42
43 // Setup: create dummy class, function and a field.
44 const Class& dummy_class = Class::Handle(Class::New(
45 Library::Handle(), String::Handle(Symbols::New(thread, "DummyClass")),
46 Script::Handle(), TokenPosition::kNoSource));
47 dummy_class.set_is_synthesized_class();
48
49 const Function& dummy_function = Function::ZoneHandle(
50 Function::New(String::Handle(Symbols::New(thread, "foo")),
51 FunctionLayout::kRegularFunction, false, false, false,
52 false, false, dummy_class, TokenPosition::kMinSource));
53
54 const Field& field = Field::Handle(
55 Field::New(String::Handle(Symbols::New(thread, "field")),
56 /*is_static=*/false, /*is_final=*/false, /*is_const=*/false,
57 /*is_reflectable=*/true, /*is_late=*/false, dummy_class,
58 Object::dynamic_type(), TokenPosition::kMinSource,
59 TokenPosition::kMinSource));
60
61 // Set non-trivial guarded state on the field.
62 field.set_guarded_cid(kSmiCid);
63 field.set_is_nullable(false);
64
65 // Enter compiler state.
66 CompilerState compiler_state(thread, /*is_aot=*/false);
67
68 const Field& field_clone_1 = Field::ZoneHandle(field.CloneFromOriginal());
69 const Field& field_clone_2 = Field::ZoneHandle(field.CloneFromOriginal());
70
71 // Check that Slot::Get() returns correctly canonicalized and configured
72 // slot that matches properties of the field.
73 ParsedFunction* parsed_function =
74 new (zone) ParsedFunction(thread, dummy_function);
75 const Slot& slot1 = Slot::Get(field_clone_1, parsed_function);
76 const Slot& slot2 = Slot::Get(field_clone_2, parsed_function);
77 EXPECT_EQ(&slot1, &slot2);
78 EXPECT(slot1.is_guarded_field());
79 EXPECT(!slot1.is_nullable());
80 EXPECT_EQ(kSmiCid, slot1.nullable_cid());
81
82 // Check that the field was added (once) to the list of guarded fields.
83 EXPECT_EQ(1, parsed_function->guarded_fields()->length());
84 EXPECT_EQ(parsed_function->guarded_fields()->At(0)->raw(),
85 field_clone_1.raw());
86
87 // Change the guarded state of the field to "unknown" - emulating concurrent
88 // modification of the guarded state in mutator) and create a new clone of
89 // the field.
90 field.set_guarded_cid(kDynamicCid);
91 field.set_is_nullable(true);
92 const Field& field_clone_3 = Field::ZoneHandle(field.CloneFromOriginal());
93
94 // Slot::Get must return the same slot and add the field from which it
95 // was created to the guarded fields list.
96 ParsedFunction* parsed_function2 =
97 new (zone) ParsedFunction(thread, dummy_function);
98 const Slot& slot3 = Slot::Get(field_clone_3, parsed_function2);
99 EXPECT_EQ(&slot1, &slot3);
100 EXPECT_EQ(1, parsed_function2->guarded_fields()->length());
101 EXPECT_EQ(parsed_function2->guarded_fields()->At(0)->raw(),
102 field_clone_1.raw());
103}
104
105} // namespace dart
106