diff --git a/runtime/vm/code_descriptors_test.cc b/runtime/vm/code_descriptors_test.cc index c5faf2ac17c..8c192b5705c 100644 --- a/runtime/vm/code_descriptors_test.cc +++ b/runtime/vm/code_descriptors_test.cc @@ -36,7 +36,7 @@ static Dart_NativeFunction native_resolver(Dart_Handle name, bool* auto_setup_scope) { ASSERT(auto_setup_scope); *auto_setup_scope = false; - return reinterpret_cast(&NativeFunc); + return NativeFunc; } TEST_CASE(StackMapGC) { diff --git a/runtime/vm/compiler/backend/redundancy_elimination_test.cc b/runtime/vm/compiler/backend/redundancy_elimination_test.cc index 74cf38ec22a..01c3fbd3c62 100644 --- a/runtime/vm/compiler/backend/redundancy_elimination_test.cc +++ b/runtime/vm/compiler/backend/redundancy_elimination_test.cc @@ -33,7 +33,7 @@ static Dart_NativeFunction NoopNativeLookup(Dart_Handle name, bool* auto_setup_scope) { ASSERT(auto_setup_scope != nullptr); *auto_setup_scope = false; - return reinterpret_cast(&NoopNative); + return NoopNative; } // Flatten all non-captured LocalVariables from the given scope and its children diff --git a/runtime/vm/dart_api_impl.cc b/runtime/vm/dart_api_impl.cc index e867ce4f3aa..e2840babd9a 100644 --- a/runtime/vm/dart_api_impl.cc +++ b/runtime/vm/dart_api_impl.cc @@ -653,23 +653,29 @@ bool Api::GetNativeFieldsOfArgument(NativeArguments* arguments, intptr_t* field_values) { NoSafepointScope no_safepoint_scope; ObjectPtr raw_obj = arguments->NativeArgAt(arg_index); - if (raw_obj->IsHeapObject()) { - intptr_t cid = raw_obj->GetClassId(); - if (cid >= kNumPredefinedCids) { - TypedDataPtr native_fields = *reinterpret_cast( - ObjectLayout::ToAddr(raw_obj) + sizeof(ObjectLayout)); - if (native_fields == TypedData::null()) { - memset(field_values, 0, (num_fields * sizeof(field_values[0]))); - } else if (num_fields == Smi::Value(native_fields->ptr()->length_)) { - intptr_t* native_values = - bit_cast(native_fields->ptr()->data()); - memmove(field_values, native_values, - (num_fields * sizeof(field_values[0]))); - } - return true; - } + intptr_t cid = raw_obj->GetClassIdMayBeSmi(); + int class_num_fields = arguments->thread() + ->isolate() + ->class_table() + ->At(cid) + ->ptr() + ->num_native_fields_; + if (num_fields != class_num_fields) { + // No native fields or mismatched native field count. + return false; } - return false; + TypedDataPtr native_fields = *reinterpret_cast( + ObjectLayout::ToAddr(raw_obj) + sizeof(ObjectLayout)); + if (native_fields == TypedData::null()) { + // Native fields not initialized. + memset(field_values, 0, (num_fields * sizeof(field_values[0]))); + return true; + } + ASSERT(class_num_fields == Smi::Value(native_fields->ptr()->length_)); + intptr_t* native_values = + reinterpret_cast(native_fields->ptr()->data()); + memmove(field_values, native_values, (num_fields * sizeof(field_values[0]))); + return true; } void Api::SetWeakHandleReturnValue(NativeArguments* args, diff --git a/runtime/vm/dart_api_impl_test.cc b/runtime/vm/dart_api_impl_test.cc index 2c2a4eb4336..08b0c7943b0 100644 --- a/runtime/vm/dart_api_impl_test.cc +++ b/runtime/vm/dart_api_impl_test.cc @@ -538,7 +538,7 @@ static Dart_NativeFunction CurrentStackTraceNativeLookup( bool* auto_setup_scope) { ASSERT(auto_setup_scope != NULL); *auto_setup_scope = true; - return reinterpret_cast(&CurrentStackTraceNative); + return CurrentStackTraceNative; } TEST_CASE(DartAPI_CurrentStackTraceInfo) { @@ -687,7 +687,7 @@ static Dart_NativeFunction PropagateError_native_lookup( bool* auto_setup_scope) { ASSERT(auto_setup_scope != NULL); *auto_setup_scope = true; - return reinterpret_cast(&PropagateErrorNative); + return PropagateErrorNative; } TEST_CASE(DartAPI_PropagateCompileTimeError) { @@ -4995,7 +4995,7 @@ static Dart_NativeFunction native_field_lookup(Dart_Handle name, bool* auto_setup_scope) { ASSERT(auto_setup_scope != NULL); *auto_setup_scope = false; - return reinterpret_cast(&NativeFieldLookup); + return NativeFieldLookup; } TEST_CASE(DartAPI_InjectNativeFields2) { @@ -5139,6 +5139,15 @@ void TestNativeFieldsAccess_access(Dart_NativeArguments args) { EXPECT_EQ(0, field_values[1]); } +void TestNativeFieldsAccess_invalidAccess(Dart_NativeArguments args) { + intptr_t field_values[kTestNumNativeFields]; + Dart_Handle result = Dart_GetNativeFieldsOfArgument( + args, 0, kTestNumNativeFields, field_values); + EXPECT_ERROR(result, + "Dart_GetNativeFieldsOfArgument: " + "expected 0 'num_fields' but was passed in 2"); +} + static Dart_NativeFunction TestNativeFieldsAccess_lookup(Dart_Handle name, int argument_count, bool* auto_scope) { @@ -5152,10 +5161,12 @@ static Dart_NativeFunction TestNativeFieldsAccess_lookup(Dart_Handle name, const char* function_name = obj.ToCString(); ASSERT(function_name != NULL); if (strcmp(function_name, "TestNativeFieldsAccess_init") == 0) { - return reinterpret_cast(&TestNativeFieldsAccess_init); + return TestNativeFieldsAccess_init; } else if (strcmp(function_name, "TestNativeFieldsAccess_access") == 0) { - return reinterpret_cast( - &TestNativeFieldsAccess_access); + return TestNativeFieldsAccess_access; + } else if (strcmp(function_name, "TestNativeFieldsAccess_invalidAccess") == + 0) { + return TestNativeFieldsAccess_invalidAccess; } else { return NULL; } @@ -5178,10 +5189,15 @@ TEST_CASE(DartAPI_TestNativeFieldsAccess) { " int%s accessNativeFlds(int%s i) native " "'TestNativeFieldsAccess_access';\n" "}\n" + "class NoNativeFields {\n" + " int neitherATypedDataNorNull = 0;\n" + " invalidAccess() native 'TestNativeFieldsAccess_invalidAccess';\n" + "}\n" "NativeFields testMain() {\n" " NativeFields obj = new NativeFields(10, 20);\n" " obj.initNativeFlds();\n" " obj.accessNativeFlds(null);\n" + " new NoNativeFields().invalidAccess();\n" " return obj;\n" "}\n", nullable_tag, nullable_tag, nullable_tag, nullable_tag), @@ -6394,7 +6410,7 @@ static Dart_NativeFunction native_lookup(Dart_Handle name, bool* auto_setup_scope) { ASSERT(auto_setup_scope != NULL); *auto_setup_scope = true; - return reinterpret_cast(&ExceptionNative); + return ExceptionNative; } TEST_CASE(DartAPI_ThrowException) { @@ -6557,9 +6573,9 @@ static Dart_NativeFunction native_args_lookup(Dart_Handle name, const char* function_name = obj.ToCString(); ASSERT(function_name != NULL); if (strcmp(function_name, "NativeArgument_Create") == 0) { - return reinterpret_cast(&NativeArgumentCreate); + return NativeArgumentCreate; } else if (strcmp(function_name, "NativeArgument_Access") == 0) { - return reinterpret_cast(&NativeArgumentAccess); + return NativeArgumentAccess; } return NULL; } @@ -6618,7 +6634,7 @@ static Dart_NativeFunction gnac_lookup(Dart_Handle name, bool* auto_setup_scope) { ASSERT(auto_setup_scope != NULL); *auto_setup_scope = true; - return reinterpret_cast(&NativeArgumentCounter); + return NativeArgumentCounter; } TEST_CASE(DartAPI_GetNativeArgumentCount) { diff --git a/runtime/vm/raw_object.h b/runtime/vm/raw_object.h index 499dba8ac12..ccf4d45393e 100644 --- a/runtime/vm/raw_object.h +++ b/runtime/vm/raw_object.h @@ -806,6 +806,7 @@ class ClassLayout : public ObjectLayout { friend class SnapshotReader; friend class InstanceSerializationCluster; friend class CidRewriteVisitor; + friend class Api; }; class PatchClassLayout : public ObjectLayout {