Skip to content

Commit

Permalink
Reland "[ VM / Mirrors ] Added type checking to enforce strong mode s…
Browse files Browse the repository at this point in the history
…emantics when using dart:mirrors"

Was reverted to temporarily fix issue #35009 for 2.1.

This reverts commit 65ffd0f7190f04dd0a8f94b7941e1ab706b0b084.

Change-Id: I71fe094b24f882cd942634557c9c43af06058842
Reviewed-on: https://dart-review.googlesource.com/c/83226
Reviewed-by: Siva Annamalai <asiva@google.com>
  • Loading branch information
bkonyi committed Nov 6, 2018
1 parent 9c95624 commit fbe3b1e
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 25 deletions.
7 changes: 7 additions & 0 deletions runtime/lib/mirrors.cc
Expand Up @@ -1434,6 +1434,13 @@ DEFINE_NATIVE_ENTRY(ClassMirror_invokeConstructor, 5) {
InvocationMirror::kMethod);
UNREACHABLE();
}
const Object& type_error =
Object::Handle(redirected_constructor.DoArgumentTypesMatch(
args, args_descriptor, type_arguments));
if (!type_error.IsNull()) {
Exceptions::PropagateError(Error::Cast(type_error));
UNREACHABLE();
}

Instance& new_object = Instance::Handle();
if (redirected_constructor.IsGenerativeConstructor()) {
Expand Down
1 change: 0 additions & 1 deletion runtime/tests/vm/vm.status
Expand Up @@ -17,7 +17,6 @@ cc/IsolateReload_PendingStaticCall_NSMToDefined: Fail, Crash # Issue 32981. Fail
cc/IsolateReload_PendingUnqualifiedCall_InstanceToStatic: Fail # Issue 32981
cc/IsolateReload_PendingUnqualifiedCall_StaticToInstance: Fail # Issue 32981
cc/IsolateReload_RunNewFieldInitializersWithGenerics: Fail # Issue 32299
cc/Profiler_InliningIntervalBoundry: Skip # Differences in ia32, debug, release
cc/SNPrint_BadArgs: Crash, Fail # These tests are expected to crash on all platforms.
dart/data_uri_import_test/none: SkipByDesign
dart/snapshot_version_test: Skip # This test is a Dart1 test (script snapshot)
Expand Down
92 changes: 92 additions & 0 deletions runtime/vm/dart_api_impl_test.cc
Expand Up @@ -4184,6 +4184,16 @@ TEST_CASE(DartAPI_SetField_FunnyValue) {
EXPECT_STREQ("myerror", Dart_GetError(result));
}

TEST_CASE(DartAPI_SetField_BadType) {
const char* kScriptChars = "int foo;\n";
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
Dart_Handle name = NewString("foo");
Dart_Handle result = Dart_SetField(lib, name, Dart_True());
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING("type 'bool' is not a subtype of type 'int' of 'foo'",
Dart_GetError(result));
}

void NativeFieldLookup(Dart_NativeArguments args) {
UNREACHABLE();
}
Expand Down Expand Up @@ -5168,6 +5178,88 @@ TEST_CASE(DartAPI_Invoke_FunnyArgs) {
EXPECT_STREQ("myerror", Dart_GetError(result));
}

TEST_CASE(DartAPI_Invoke_BadArgs) {
const char* kScriptChars =
"class BaseMethods {\n"
" inheritedMethod(int arg) => 'inherited $arg';\n"
" static nonInheritedMethod(int arg) => 'noninherited $arg';\n"
"}\n"
"\n"
"class Methods extends BaseMethods {\n"
" instanceMethod(int arg) => 'instance $arg';\n"
" _instanceMethod(int arg) => 'hidden instance $arg';\n"
" static staticMethod(int arg) => 'static $arg';\n"
" static _staticMethod(int arg) => 'hidden static $arg';\n"
"}\n"
"\n"
"topMethod(int arg) => 'top $arg';\n"
"_topMethod(int arg) => 'hidden top $arg';\n"
"\n"
"Methods test() {\n"
" return new Methods();\n"
"}\n";

#if defined(PRODUCT)
const char* error_msg =
"type '_OneByteString' is not a subtype of type 'int' of 'arg'";
#else
const char* error_msg =
"type 'String' is not a subtype of type 'int' of 'arg'";
#endif // defined(PRODUCT)

// Shared setup.
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
Dart_Handle type = Dart_GetType(lib, NewString("Methods"), 0, NULL);
EXPECT_VALID(type);
Dart_Handle instance = Dart_Invoke(lib, NewString("test"), 0, NULL);
EXPECT_VALID(instance);
Dart_Handle args[1];
args[0] = NewString("!!!");
Dart_Handle result;
Dart_Handle name;

// Instance method.
name = NewString("instanceMethod");
result = Dart_Invoke(instance, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));

name = PrivateLibName(lib, "_instanceMethod");
result = Dart_Invoke(instance, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));

// Inherited method.
name = NewString("inheritedMethod");
result = Dart_Invoke(instance, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));

// Static method.
name = NewString("staticMethod");
result = Dart_Invoke(type, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));

// Hidden static method.
name = NewString("_staticMethod");
result = Dart_Invoke(type, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));

// Top-Level method.
name = NewString("topMethod");
result = Dart_Invoke(lib, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));

// Hidden top-level method.
name = NewString("_topMethod");
result = Dart_Invoke(lib, name, 1, args);
EXPECT(Dart_IsError(result));
EXPECT_SUBSTRING(error_msg, Dart_GetError(result));
}

TEST_CASE(DartAPI_Invoke_Null) {
Dart_Handle result = Dart_Invoke(Dart_Null(), NewString("toString"), 0, NULL);
EXPECT_VALID(result);
Expand Down

0 comments on commit fbe3b1e

Please sign in to comment.