Skip to content

Commit b0484ec

Browse files
rmacnak-googlecommit-bot@chromium.org
authored andcommitted
[vm] Check prefix.loadLibrary is called and returns before prefix members are used.
Restore checks against reloading a library with deferred prefixes. No loading is actually deferred. Bug: #26878 Bug: #41974 Change-Id: Iec2662de117453d596cca28dd9481a9751091ce9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/149613 Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Alexander Markov <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent 2a5c122 commit b0484ec

16 files changed

+183
-57
lines changed

runtime/lib/object.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ DEFINE_NATIVE_ENTRY(Type_equality, 0, 2) {
196196
return Bool::Get(type.IsEquivalent(other, TypeEquality::kSyntactical)).raw();
197197
}
198198

199+
DEFINE_NATIVE_ENTRY(LibraryPrefix_isLoaded, 0, 1) {
200+
const LibraryPrefix& prefix =
201+
LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0));
202+
return Bool::Get(prefix.is_loaded()).raw();
203+
}
204+
205+
DEFINE_NATIVE_ENTRY(LibraryPrefix_setLoaded, 0, 1) {
206+
const LibraryPrefix& prefix =
207+
LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0));
208+
prefix.set_is_loaded(true);
209+
return Instance::null();
210+
}
211+
199212
DEFINE_NATIVE_ENTRY(Internal_inquireIs64Bit, 0, 0) {
200213
#if defined(ARCH_IS_64_BIT)
201214
return Bool::True().raw();

runtime/vm/bootstrap_natives.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ namespace dart {
3131
V(AbstractType_toString, 1) \
3232
V(Type_getHashCode, 1) \
3333
V(Type_equality, 2) \
34+
V(LibraryPrefix_isLoaded, 1) \
35+
V(LibraryPrefix_setLoaded, 1) \
3436
V(Identical_comparison, 2) \
3537
V(Integer_bitAndFromInteger, 2) \
3638
V(Integer_bitOrFromInteger, 2) \

runtime/vm/clustered_snapshot.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,6 +3248,7 @@ class LibraryPrefixDeserializationCluster : public DeserializationCluster {
32483248
ReadFromTo(prefix);
32493249
prefix->ptr()->num_imports_ = d->Read<uint16_t>();
32503250
prefix->ptr()->is_deferred_load_ = d->Read<bool>();
3251+
prefix->ptr()->is_loaded_ = !prefix->ptr()->is_deferred_load_;
32513252
}
32523253
}
32533254
};

runtime/vm/compiler/backend/il.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,11 @@ ConstantInstr::ConstantInstr(const Object& value, TokenPosition token_pos)
10961096
// tables used for certain character classes are represented as TypedData,
10971097
// and so those values are also neither immutable (as there are no immutable
10981098
// TypedData values) or canonical.
1099-
ASSERT(value.IsTypeParameter() || value.IsArray() || value.IsTypedData());
1099+
//
1100+
// LibraryPrefixes are also never canonicalized since their equality is
1101+
// their identity.
1102+
ASSERT(value.IsTypeParameter() || value.IsArray() || value.IsTypedData() ||
1103+
value.IsLibraryPrefix());
11001104
}
11011105
#endif
11021106
}

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,9 @@ Fragment StreamingFlowGraphBuilder::BuildExpression(TokenPosition* position) {
13631363
case kInstantiation:
13641364
return BuildPartialTearoffInstantiation(position);
13651365
case kLoadLibrary:
1366+
return BuildLibraryPrefixAction(position, Symbols::LoadLibrary());
13661367
case kCheckLibraryIsLoaded:
1367-
ReadUInt(); // skip library index
1368-
return BuildFutureNullValue(position);
1368+
return BuildLibraryPrefixAction(position, Symbols::CheckLoaded());
13691369
case kConstStaticInvocation:
13701370
case kConstConstructorInvocation:
13711371
case kConstListLiteral:
@@ -4068,6 +4068,26 @@ Fragment StreamingFlowGraphBuilder::BuildPartialTearoffInstantiation(
40684068
return instructions;
40694069
}
40704070

4071+
Fragment StreamingFlowGraphBuilder::BuildLibraryPrefixAction(
4072+
TokenPosition* position,
4073+
const String& selector) {
4074+
const intptr_t dependency_index = ReadUInt();
4075+
const Library& current_library = Library::Handle(
4076+
Z, Class::Handle(Z, parsed_function()->function().origin()).library());
4077+
const Array& dependencies = Array::Handle(Z, current_library.dependencies());
4078+
const LibraryPrefix& prefix =
4079+
LibraryPrefix::CheckedZoneHandle(Z, dependencies.At(dependency_index));
4080+
const Function& function =
4081+
Function::ZoneHandle(Z, Library::Handle(Z, Library::CoreLibrary())
4082+
.LookupFunctionAllowPrivate(selector));
4083+
ASSERT(!function.IsNull());
4084+
Fragment instructions;
4085+
instructions += Constant(prefix);
4086+
instructions +=
4087+
StaticCall(TokenPosition::kNoSource, function, 1, ICData::kStatic);
4088+
return instructions;
4089+
}
4090+
40714091
Fragment StreamingFlowGraphBuilder::BuildExpressionStatement() {
40724092
Fragment instructions = BuildExpression(); // read expression.
40734093
instructions += Drop();

runtime/vm/compiler/frontend/kernel_binary_flowgraph.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ class StreamingFlowGraphBuilder : public KernelReaderHelper {
335335
Fragment BuildFutureNullValue(TokenPosition* position);
336336
Fragment BuildConstantExpression(TokenPosition* position, Tag tag);
337337
Fragment BuildPartialTearoffInstantiation(TokenPosition* position);
338+
Fragment BuildLibraryPrefixAction(TokenPosition* position,
339+
const String& selector);
338340

339341
Fragment BuildExpressionStatement();
340342
Fragment BuildBlock();

runtime/vm/compiler/runtime_offsets_extracted.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 4;
442442
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
443443
64;
444444
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 28;
445-
static constexpr dart::compiler::target::word Library_InstanceSize = 76;
445+
static constexpr dart::compiler::target::word Library_InstanceSize = 80;
446446
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 20;
447447
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 28;
448448
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -927,7 +927,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 8;
927927
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
928928
128;
929929
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 48;
930-
static constexpr dart::compiler::target::word Library_InstanceSize = 144;
930+
static constexpr dart::compiler::target::word Library_InstanceSize = 152;
931931
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 40;
932932
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 56;
933933
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -1404,7 +1404,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 4;
14041404
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
14051405
64;
14061406
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 28;
1407-
static constexpr dart::compiler::target::word Library_InstanceSize = 76;
1407+
static constexpr dart::compiler::target::word Library_InstanceSize = 80;
14081408
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 20;
14091409
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 28;
14101410
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -1890,7 +1890,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 8;
18901890
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
18911891
128;
18921892
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 48;
1893-
static constexpr dart::compiler::target::word Library_InstanceSize = 144;
1893+
static constexpr dart::compiler::target::word Library_InstanceSize = 152;
18941894
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 40;
18951895
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 56;
18961896
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -2366,7 +2366,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 4;
23662366
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
23672367
64;
23682368
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 28;
2369-
static constexpr dart::compiler::target::word Library_InstanceSize = 76;
2369+
static constexpr dart::compiler::target::word Library_InstanceSize = 80;
23702370
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 20;
23712371
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 28;
23722372
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -2845,7 +2845,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 8;
28452845
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
28462846
128;
28472847
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 48;
2848-
static constexpr dart::compiler::target::word Library_InstanceSize = 144;
2848+
static constexpr dart::compiler::target::word Library_InstanceSize = 152;
28492849
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 40;
28502850
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 56;
28512851
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -3316,7 +3316,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 4;
33163316
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
33173317
64;
33183318
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 28;
3319-
static constexpr dart::compiler::target::word Library_InstanceSize = 76;
3319+
static constexpr dart::compiler::target::word Library_InstanceSize = 80;
33203320
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 20;
33213321
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 28;
33223322
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -3796,7 +3796,7 @@ static constexpr dart::compiler::target::word Integer_InstanceSize = 8;
37963796
static constexpr dart::compiler::target::word KernelProgramInfo_InstanceSize =
37973797
128;
37983798
static constexpr dart::compiler::target::word LanguageError_InstanceSize = 48;
3799-
static constexpr dart::compiler::target::word Library_InstanceSize = 144;
3799+
static constexpr dart::compiler::target::word Library_InstanceSize = 152;
38003800
static constexpr dart::compiler::target::word LibraryPrefix_InstanceSize = 40;
38013801
static constexpr dart::compiler::target::word LinkedHashMap_InstanceSize = 56;
38023802
static constexpr dart::compiler::target::word LocalVarDescriptors_InstanceSize =
@@ -4307,7 +4307,7 @@ static constexpr dart::compiler::target::word
43074307
AOT_KernelProgramInfo_InstanceSize = 64;
43084308
static constexpr dart::compiler::target::word AOT_LanguageError_InstanceSize =
43094309
28;
4310-
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 72;
4310+
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 76;
43114311
static constexpr dart::compiler::target::word AOT_LibraryPrefix_InstanceSize =
43124312
20;
43134313
static constexpr dart::compiler::target::word AOT_LinkedHashMap_InstanceSize =
@@ -4833,7 +4833,7 @@ static constexpr dart::compiler::target::word
48334833
AOT_KernelProgramInfo_InstanceSize = 128;
48344834
static constexpr dart::compiler::target::word AOT_LanguageError_InstanceSize =
48354835
48;
4836-
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 136;
4836+
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 144;
48374837
static constexpr dart::compiler::target::word AOT_LibraryPrefix_InstanceSize =
48384838
40;
48394839
static constexpr dart::compiler::target::word AOT_LinkedHashMap_InstanceSize =
@@ -5363,7 +5363,7 @@ static constexpr dart::compiler::target::word
53635363
AOT_KernelProgramInfo_InstanceSize = 128;
53645364
static constexpr dart::compiler::target::word AOT_LanguageError_InstanceSize =
53655365
48;
5366-
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 136;
5366+
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 144;
53675367
static constexpr dart::compiler::target::word AOT_LibraryPrefix_InstanceSize =
53685368
40;
53695369
static constexpr dart::compiler::target::word AOT_LinkedHashMap_InstanceSize =
@@ -5880,7 +5880,7 @@ static constexpr dart::compiler::target::word
58805880
AOT_KernelProgramInfo_InstanceSize = 64;
58815881
static constexpr dart::compiler::target::word AOT_LanguageError_InstanceSize =
58825882
28;
5883-
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 72;
5883+
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 76;
58845884
static constexpr dart::compiler::target::word AOT_LibraryPrefix_InstanceSize =
58855885
20;
58865886
static constexpr dart::compiler::target::word AOT_LinkedHashMap_InstanceSize =
@@ -6399,7 +6399,7 @@ static constexpr dart::compiler::target::word
63996399
AOT_KernelProgramInfo_InstanceSize = 128;
64006400
static constexpr dart::compiler::target::word AOT_LanguageError_InstanceSize =
64016401
48;
6402-
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 136;
6402+
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 144;
64036403
static constexpr dart::compiler::target::word AOT_LibraryPrefix_InstanceSize =
64046404
40;
64056405
static constexpr dart::compiler::target::word AOT_LinkedHashMap_InstanceSize =
@@ -6922,7 +6922,7 @@ static constexpr dart::compiler::target::word
69226922
AOT_KernelProgramInfo_InstanceSize = 128;
69236923
static constexpr dart::compiler::target::word AOT_LanguageError_InstanceSize =
69246924
48;
6925-
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 136;
6925+
static constexpr dart::compiler::target::word AOT_Library_InstanceSize = 144;
69266926
static constexpr dart::compiler::target::word AOT_LibraryPrefix_InstanceSize =
69276927
40;
69286928
static constexpr dart::compiler::target::word AOT_LinkedHashMap_InstanceSize =

runtime/vm/isolate_reload.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ bool IsolateGroupReloadContext::Reload(bool force_reload,
738738

739739
// Ensure all functions on the stack have unoptimized code.
740740
// Deoptimize all code that had optimizing decisions that are dependent on
741-
// assumptions from field guards or CHA.
741+
// assumptions from field guards or CHA or deferred library prefixes.
742742
// TODO(johnmccutchan): Deoptimizing dependent code here (before the reload)
743743
// is paranoid. This likely can be moved to the commit phase.
744744
ForEachIsolate([&](Isolate* isolate) {
@@ -1312,6 +1312,8 @@ void IsolateReloadContext::DeoptimizeDependentCode() {
13121312
}
13131313

13141314
DeoptimizeTypeTestingStubs();
1315+
1316+
// TODO(rmacnak): Also call LibraryPrefix::InvalidateDependentCode.
13151317
}
13161318

13171319
void IsolateGroupReloadContext::CheckpointSharedClassTable() {

runtime/vm/kernel_loader.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ void KernelLoader::LoadLibraryImportsAndExports(Library* library,
13051305
LibraryPrefix& library_prefix = LibraryPrefix::Handle(Z);
13061306

13071307
const intptr_t deps_count = helper_.ReadListLength();
1308+
const Array& deps = Array::Handle(Array::New(deps_count));
13081309
for (intptr_t dep = 0; dep < deps_count; ++dep) {
13091310
LibraryDependencyHelper dependency_helper(&helper_);
13101311

@@ -1388,12 +1389,21 @@ void KernelLoader::LoadLibraryImportsAndExports(Library* library,
13881389
}
13891390
}
13901391
}
1392+
13911393
if (FLAG_enable_mirrors && dependency_helper.annotation_count_ > 0) {
13921394
ASSERT(annotations_kernel_offset > 0);
13931395
ns.AddMetadata(toplevel_class, TokenPosition::kNoSource,
13941396
annotations_kernel_offset);
13951397
}
1398+
1399+
if (prefix.IsNull()) {
1400+
deps.SetAt(dep, ns);
1401+
} else {
1402+
deps.SetAt(dep, library_prefix);
1403+
}
13961404
}
1405+
1406+
library->set_dependencies(deps);
13971407
}
13981408

13991409
void KernelLoader::LoadPreliminaryClass(ClassHelper* class_helper,

runtime/vm/object.cc

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11075,27 +11075,6 @@ void ClassDictionaryIterator::MoveToNextClass() {
1107511075
}
1107611076
}
1107711077

11078-
LibraryPrefixIterator::LibraryPrefixIterator(const Library& library)
11079-
: DictionaryIterator(library) {
11080-
Advance();
11081-
}
11082-
11083-
LibraryPrefixPtr LibraryPrefixIterator::GetNext() {
11084-
ASSERT(HasNext());
11085-
int ix = next_ix_++;
11086-
Object& obj = Object::Handle(array_.At(ix));
11087-
Advance();
11088-
return LibraryPrefix::Cast(obj).raw();
11089-
}
11090-
11091-
void LibraryPrefixIterator::Advance() {
11092-
Object& obj = Object::Handle(array_.At(next_ix_));
11093-
while (!obj.IsLibraryPrefix() && HasNext()) {
11094-
next_ix_++;
11095-
obj = array_.At(next_ix_);
11096-
}
11097-
}
11098-
1109911078
static void ReportTooManyImports(const Library& lib) {
1110011079
const String& url = String::Handle(lib.url());
1110111080
Report::MessageF(Report::kError, Script::Handle(lib.LookupScript(url)),
@@ -12069,6 +12048,10 @@ void Library::set_toplevel_class(const Class& value) const {
1206912048
StorePointer(&raw_ptr()->toplevel_class_, value.raw());
1207012049
}
1207112050

12051+
void Library::set_dependencies(const Array& deps) const {
12052+
StorePointer(&raw_ptr()->dependencies_, deps.raw());
12053+
}
12054+
1207212055
void Library::set_metadata(const GrowableObjectArray& value) const {
1207312056
StorePointer(&raw_ptr()->metadata_, value.raw());
1207412057
}
@@ -12937,6 +12920,7 @@ LibraryPrefixPtr LibraryPrefix::New(const String& name,
1293712920
result.set_num_imports(0);
1293812921
result.set_importer(importer);
1293912922
result.StoreNonPointer(&result.raw_ptr()->is_deferred_load_, deferred_load);
12923+
result.StoreNonPointer(&result.raw_ptr()->is_loaded_, !deferred_load);
1294012924
result.set_imports(Array::Handle(Array::New(kInitialSize)));
1294112925
result.AddImport(import);
1294212926
return result.raw();
@@ -12964,8 +12948,7 @@ void LibraryPrefix::set_importer(const Library& value) const {
1296412948

1296512949
const char* LibraryPrefix::ToCString() const {
1296612950
const String& prefix = String::Handle(name());
12967-
return OS::SCreate(Thread::Current()->zone(), "LibraryPrefix:'%s'",
12968-
prefix.ToCString());
12951+
return prefix.ToCString();
1296912952
}
1297012953

1297112954
void Namespace::set_metadata_field(const Field& value) const {

runtime/vm/object.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4543,7 +4543,6 @@ class DictionaryIterator : public ValueObject {
45434543
int next_ix_; // Index of next element.
45444544

45454545
friend class ClassDictionaryIterator;
4546-
friend class LibraryPrefixIterator;
45474546
DISALLOW_COPY_AND_ASSIGN(DictionaryIterator);
45484547
};
45494548

@@ -4574,16 +4573,6 @@ class ClassDictionaryIterator : public DictionaryIterator {
45744573
DISALLOW_COPY_AND_ASSIGN(ClassDictionaryIterator);
45754574
};
45764575

4577-
class LibraryPrefixIterator : public DictionaryIterator {
4578-
public:
4579-
explicit LibraryPrefixIterator(const Library& library);
4580-
LibraryPrefixPtr GetNext();
4581-
4582-
private:
4583-
void Advance();
4584-
DISALLOW_COPY_AND_ASSIGN(LibraryPrefixIterator);
4585-
};
4586-
45874576
class Library : public Object {
45884577
public:
45894578
StringPtr name() const { return raw_ptr()->name_; }
@@ -4738,6 +4727,9 @@ class Library : public Object {
47384727
NamespacePtr ImportAt(intptr_t index) const;
47394728
LibraryPtr ImportLibraryAt(intptr_t index) const;
47404729

4730+
ArrayPtr dependencies() const { return raw_ptr()->dependencies_; }
4731+
void set_dependencies(const Array& deps) const;
4732+
47414733
void DropDependenciesAndCaches() const;
47424734

47434735
// Resolving native methods for script loaded in the library.
@@ -7342,6 +7334,10 @@ class LibraryPrefix : public Instance {
73427334
void AddImport(const Namespace& import) const;
73437335

73447336
bool is_deferred_load() const { return raw_ptr()->is_deferred_load_; }
7337+
bool is_loaded() const { return raw_ptr()->is_loaded_; }
7338+
void set_is_loaded(bool value) const {
7339+
return StoreNonPointer(&raw_ptr()->is_loaded_, value);
7340+
}
73457341

73467342
static intptr_t InstanceSize() {
73477343
return RoundedAllocationSize(sizeof(LibraryPrefixLayout));

0 commit comments

Comments
 (0)