2019-03-07 14:43:43 +00:00
|
|
|
// Copyright 2019 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/handles/handles.h"
|
2019-08-29 14:37:43 +00:00
|
|
|
#include "src/objects/foreign-inl.h"
|
|
|
|
#include "src/objects/managed.h"
|
2019-03-07 14:43:43 +00:00
|
|
|
#include "src/objects/maybe-object.h"
|
|
|
|
#include "src/objects/object-macros.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2019-08-29 14:37:43 +00:00
|
|
|
// ------- Test simple argument evaluation order problems ---------
|
|
|
|
|
2019-03-07 14:43:43 +00:00
|
|
|
Handle<Object> CauseGC(Handle<Object> obj, Isolate* isolate) {
|
|
|
|
isolate->heap()->CollectGarbage(OLD_SPACE, GarbageCollectionReason::kTesting);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:37:43 +00:00
|
|
|
Object CauseGCRaw(Object obj, Isolate* isolate) {
|
|
|
|
isolate->heap()->CollectGarbage(OLD_SPACE, GarbageCollectionReason::kTesting);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
Managed<Smi> CauseGCManaged(int i, Isolate* isolate) {
|
|
|
|
isolate->heap()->CollectGarbage(OLD_SPACE, GarbageCollectionReason::kTesting);
|
|
|
|
|
|
|
|
return Managed<Smi>::cast(Smi::FromInt(i));
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:43:43 +00:00
|
|
|
void TwoArgumentsFunction(Object a, Object b) {
|
2019-08-29 14:37:43 +00:00
|
|
|
a.Print();
|
|
|
|
b.Print();
|
2019-03-07 14:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestTwoArguments(Isolate* isolate) {
|
|
|
|
Handle<JSObject> obj1 = isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
Handle<JSObject> obj2 = isolate->factory()->NewJSObjectWithNullProto();
|
2019-08-29 14:37:43 +00:00
|
|
|
// Should cause warning.
|
2019-03-07 14:43:43 +00:00
|
|
|
TwoArgumentsFunction(*CauseGC(obj1, isolate), *CauseGC(obj2, isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwoSizeTArgumentsFunction(size_t a, size_t b) {
|
|
|
|
USE(a);
|
|
|
|
USE(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestTwoSizeTArguments(Isolate* isolate) {
|
|
|
|
Handle<JSObject> obj1 = isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
Handle<JSObject> obj2 = isolate->factory()->NewJSObjectWithNullProto();
|
2019-08-29 14:37:43 +00:00
|
|
|
// Should cause warning.
|
2019-03-07 14:43:43 +00:00
|
|
|
TwoSizeTArgumentsFunction(sizeof(*CauseGC(obj1, isolate)),
|
|
|
|
sizeof(*CauseGC(obj2, isolate)));
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:37:43 +00:00
|
|
|
// --------- Test problems with method arguments ----------
|
|
|
|
|
2019-03-07 14:43:43 +00:00
|
|
|
class SomeObject : public Object {
|
|
|
|
public:
|
2019-08-29 14:37:43 +00:00
|
|
|
void Method(Object a) { a.Print(); }
|
2019-03-07 14:43:43 +00:00
|
|
|
|
2019-03-13 09:47:24 +00:00
|
|
|
SomeObject& operator=(const Object& b) {
|
|
|
|
this->Print();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:43:43 +00:00
|
|
|
DECL_CAST(SomeObject)
|
|
|
|
|
|
|
|
OBJECT_CONSTRUCTORS(SomeObject, Object);
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestMethodCall(Isolate* isolate) {
|
|
|
|
SomeObject obj;
|
|
|
|
Handle<SomeObject> so = handle(obj, isolate);
|
|
|
|
Handle<JSObject> obj1 = isolate->factory()->NewJSObjectWithNullProto();
|
2019-08-29 14:37:43 +00:00
|
|
|
// Should cause warning.
|
2019-03-07 14:43:43 +00:00
|
|
|
so->Method(*CauseGC(obj1, isolate));
|
2019-08-29 14:37:43 +00:00
|
|
|
// Should cause warning.
|
|
|
|
so->Method(CauseGCRaw(*obj1, isolate));
|
2019-03-07 14:43:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:47:24 +00:00
|
|
|
void TestOperatorCall(Isolate* isolate) {
|
|
|
|
SomeObject obj;
|
|
|
|
Handle<JSObject> obj1 = isolate->factory()->NewJSObjectWithNullProto();
|
2019-08-29 14:37:43 +00:00
|
|
|
// Should not cause warning.
|
2019-03-13 09:47:24 +00:00
|
|
|
obj = *CauseGC(obj1, isolate);
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:37:43 +00:00
|
|
|
// --------- Test for templated sub-classes of Object ----------
|
|
|
|
|
|
|
|
void TestFollowingTemplates(Isolate* isolate) {
|
|
|
|
// Should cause warning.
|
|
|
|
CauseGCManaged(42, isolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------- Test for correctly resolving virtual methods ----------
|
|
|
|
|
|
|
|
class BaseObject {
|
|
|
|
public:
|
|
|
|
virtual Handle<Object> VirtualCauseGC(Handle<Object> obj, Isolate* isolate) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DerivedObject : public BaseObject {
|
|
|
|
public:
|
|
|
|
Handle<Object> VirtualCauseGC(Handle<Object> obj, Isolate* isolate) override {
|
|
|
|
isolate->heap()->CollectGarbage(OLD_SPACE,
|
|
|
|
GarbageCollectionReason::kTesting);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestFollowingVirtualFunctions(Isolate* isolate) {
|
|
|
|
DerivedObject derived;
|
|
|
|
BaseObject* base = &derived;
|
|
|
|
Handle<JSObject> obj1 = isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
|
|
|
|
SomeObject so;
|
|
|
|
Handle<SomeObject> so_handle = handle(so, isolate);
|
|
|
|
// Should cause warning.
|
|
|
|
so_handle->Method(*derived.VirtualCauseGC(obj1, isolate));
|
|
|
|
// Should cause warning.
|
|
|
|
so_handle->Method(*base->VirtualCauseGC(obj1, isolate));
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:06:29 +00:00
|
|
|
// --------- Test for correctly resolving static methods ----------
|
|
|
|
|
|
|
|
class SomeClass {
|
|
|
|
public:
|
|
|
|
static Handle<Object> StaticCauseGC(Handle<Object> obj, Isolate* isolate) {
|
|
|
|
isolate->heap()->CollectGarbage(OLD_SPACE,
|
|
|
|
GarbageCollectionReason::kTesting);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestFollowingStaticFunctions(Isolate* isolate) {
|
|
|
|
SomeObject so;
|
|
|
|
Handle<SomeObject> so_handle = handle(so, isolate);
|
|
|
|
|
|
|
|
Handle<JSObject> obj1 = isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
// Should cause warning.
|
|
|
|
so_handle->Method(*SomeClass::StaticCauseGC(obj1, isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------- Test basic dead variable analysis ----------
|
|
|
|
|
|
|
|
void TestDeadVarAnalysis(Isolate* isolate) {
|
|
|
|
JSObject raw_obj = *isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
CauseGCRaw(raw_obj, isolate);
|
|
|
|
|
|
|
|
// Should cause warning.
|
|
|
|
raw_obj.Print();
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:15:58 +00:00
|
|
|
void TestGuardedDeadVarAnalysis(Isolate* isolate) {
|
|
|
|
JSObject raw_obj = *isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
|
|
|
|
// Note: having DisallowHeapAllocation with the same function as CauseGC
|
|
|
|
// normally doesn't make sense, but we want to test whether the gurads
|
|
|
|
// are recognized by GCMole.
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
CauseGCRaw(raw_obj, isolate);
|
|
|
|
|
|
|
|
// Shouldn't cause warning.
|
|
|
|
raw_obj.Print();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGuardedDeadVarAnalysisNotOnStack(Isolate* isolate) {
|
|
|
|
JSObject raw_obj = *isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
|
|
|
|
// {DisallowHeapAccess} has a {DisallowHeapAllocation} embedded as a member
|
|
|
|
// field, so both are treated equally by gcmole.
|
|
|
|
DisallowHeapAccess no_gc;
|
|
|
|
CauseGCRaw(raw_obj, isolate);
|
|
|
|
|
|
|
|
// Shouldn't cause warning.
|
|
|
|
raw_obj.Print();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGuardedDeadVarAnalysisNested(JSObject raw_obj, Isolate* isolate) {
|
|
|
|
CauseGCRaw(raw_obj, isolate);
|
|
|
|
|
|
|
|
// Shouldn't cause warning.
|
|
|
|
raw_obj.Print();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGuardedDeadVarAnalysisCaller(Isolate* isolate) {
|
|
|
|
DisallowHeapAccess no_gc;
|
|
|
|
JSObject raw_obj = *isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
|
|
|
|
TestGuardedDeadVarAnalysisNested(raw_obj, isolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject GuardedAllocation(Isolate* isolate) {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
return *isolate->factory()->NewJSObjectWithNullProto();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestNestedDeadVarAnalysis(Isolate* isolate) {
|
|
|
|
JSObject raw_obj = GuardedAllocation(isolate);
|
|
|
|
CauseGCRaw(raw_obj, isolate);
|
|
|
|
|
|
|
|
// Should cause warning.
|
|
|
|
raw_obj.Print();
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:43:43 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|