From ae83ac248f092bf899e675aaa4e04d2ac375e498 Mon Sep 17 00:00:00 2001 From: Matt Gilbride Date: Tue, 24 Sep 2024 22:48:38 +0000 Subject: Add `DynamicInstrumentationManagerService` Adds a new service with a single operation `getExecutableMethodFileOffsets`. The operation allows the caller to fetch information about the native executable of a given method. The operation's access control is limited to the UprobeStats module. Given a method in the form of fully qualified class name, method name, and fully qualified parameter list, the operation returns information from the ODEX file associated with that method. If the method isn't precompiled, the operation returns null. However, ART can be enhanced to support returning information about JIT compiled methods in the future. Bug: 372925025 Test: DynamicInstrumentationManagerServiceTests, ExecutableMethodFileOffsetsTest Flag: com.android.art.flags.executable_method_file_offsets Change-Id: I1f2dc3780d1bd2a682c1fd3ec41e5c8d73e96fc2 --- Android.bp | 1 + core/api/system-current.txt | 1 + core/java/Android.bp | 12 ++ core/java/android/content/Context.java | 6 + .../ExecutableMethodFileOffsets.aidl | 37 +++++ .../IDynamicInstrumentationManager.aidl | 33 ++++ .../os/instrumentation/MethodDescriptor.aidl | 37 +++++ .../android/os/instrumentation/TargetProcess.aidl | 29 ++++ core/res/Android.bp | 1 + core/res/AndroidManifest.xml | 10 ++ data/etc/platform.xml | 2 + native/android/Android.bp | 2 + native/android/dynamic_instrumentation_manager.cpp | 173 +++++++++++++++++++++ .../android/dynamic_instrumentation_manager.h | 132 ++++++++++++++++ native/android/libandroid.map.txt | 9 ++ packages/Shell/AndroidManifest.xml | 3 + services/core/Android.bp | 1 + .../DynamicInstrumentationManagerService.java | 135 ++++++++++++++++ services/java/com/android/server/SystemServer.java | 8 + .../Android.bp | 44 ++++++ .../AndroidManifest.xml | 27 ++++ .../TEST_MAPPING | 7 + .../src/com/android/server/TestAbstractClass.java | 24 +++ .../com/android/server/TestAbstractClassImpl.java | 23 +++ .../src/com/android/server/TestClass.java | 40 +++++ .../src/com/android/server/TestInterface.java | 33 ++++ .../src/com/android/server/TestInterfaceImpl.java | 23 +++ .../instrumentation/ParseMethodDescriptorTest.java | 143 +++++++++++++++++ 28 files changed, 996 insertions(+) create mode 100644 core/java/android/os/instrumentation/ExecutableMethodFileOffsets.aidl create mode 100644 core/java/android/os/instrumentation/IDynamicInstrumentationManager.aidl create mode 100644 core/java/android/os/instrumentation/MethodDescriptor.aidl create mode 100644 core/java/android/os/instrumentation/TargetProcess.aidl create mode 100644 native/android/dynamic_instrumentation_manager.cpp create mode 100644 native/android/include_platform/android/dynamic_instrumentation_manager.h create mode 100644 services/core/java/com/android/server/os/instrumentation/DynamicInstrumentationManagerService.java create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/Android.bp create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/AndroidManifest.xml create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/TEST_MAPPING create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClass.java create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClassImpl.java create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestClass.java create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterface.java create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterfaceImpl.java create mode 100644 services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/os/instrumentation/ParseMethodDescriptorTest.java diff --git a/Android.bp b/Android.bp index 26d0d65f329c..2024c8afb5be 100644 --- a/Android.bp +++ b/Android.bp @@ -399,6 +399,7 @@ java_defaults { "com.android.sysprop.foldlockbehavior", "com.android.sysprop.view", "framework-internal-utils", + "dynamic_instrumentation_manager_aidl-java", // If MimeMap ever becomes its own APEX, then this dependency would need to be removed // in favor of an API stubs dependency in java_library "framework" below. "mimemap", diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 2a01ca082832..2535d2dda003 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -138,6 +138,7 @@ package android { field public static final String DISABLE_SYSTEM_SOUND_EFFECTS = "android.permission.DISABLE_SYSTEM_SOUND_EFFECTS"; field public static final String DISPATCH_PROVISIONING_MESSAGE = "android.permission.DISPATCH_PROVISIONING_MESSAGE"; field public static final String DOMAIN_VERIFICATION_AGENT = "android.permission.DOMAIN_VERIFICATION_AGENT"; + field @FlaggedApi("com.android.art.flags.executable_method_file_offsets") public static final String DYNAMIC_INSTRUMENTATION = "android.permission.DYNAMIC_INSTRUMENTATION"; field @FlaggedApi("com.android.window.flags.untrusted_embedding_any_app_permission") public static final String EMBED_ANY_APP_IN_UNTRUSTED_MODE = "android.permission.EMBED_ANY_APP_IN_UNTRUSTED_MODE"; field @FlaggedApi("android.content.pm.emergency_install_permission") public static final String EMERGENCY_INSTALL_PACKAGES = "android.permission.EMERGENCY_INSTALL_PACKAGES"; field public static final String ENTER_CAR_MODE_PRIORITIZED = "android.permission.ENTER_CAR_MODE_PRIORITIZED"; diff --git a/core/java/Android.bp b/core/java/Android.bp index 9875efe04361..81ed11ae9af0 100644 --- a/core/java/Android.bp +++ b/core/java/Android.bp @@ -28,6 +28,7 @@ filegroup { exclude_srcs: [ "android/os/*MessageQueue/**/*.java", "android/ranging/**/*.java", + ":dynamic_instrumentation_manager_aidl_sources", ], visibility: ["//frameworks/base"], } @@ -119,6 +120,17 @@ filegroup { srcs: ["android/tracing/TraceReportParams.aidl"], } +filegroup { + name: "dynamic_instrumentation_manager_aidl_sources", + srcs: ["android/os/instrumentation/*.aidl"], +} + +aidl_interface { + name: "dynamic_instrumentation_manager_aidl", + srcs: [":dynamic_instrumentation_manager_aidl_sources"], + unstable: true, +} + filegroup { name: "framework-internal-display-sources", srcs: ["com/android/internal/display/BrightnessSynchronizer.java"], diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 186f7b3e111c..6086f2455a31 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -6801,6 +6801,12 @@ public abstract class Context { @FlaggedApi(android.media.tv.flags.Flags.FLAG_MEDIA_QUALITY_FW) public static final String MEDIA_QUALITY_SERVICE = "media_quality"; + /** + * Service to perform operations needed for dynamic instrumentation. + * @hide + */ + public static final String DYNAMIC_INSTRUMENTATION_SERVICE = "dynamic_instrumentation"; + /** * Determine whether the given permission is allowed for a particular * process and user ID running in the system. diff --git a/core/java/android/os/instrumentation/ExecutableMethodFileOffsets.aidl b/core/java/android/os/instrumentation/ExecutableMethodFileOffsets.aidl new file mode 100644 index 000000000000..dbe54891b0f2 --- /dev/null +++ b/core/java/android/os/instrumentation/ExecutableMethodFileOffsets.aidl @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.os.instrumentation; + +/** + * Represents the location of the code for a compiled method within a process' + * memory. + * {@hide} + */ +@JavaDerive(toString=true) +parcelable ExecutableMethodFileOffsets { + /** + * The OS path of the containing file (could be virtual). + */ + @utf8InCpp String containerPath; + /** + * The offset of the containing file within the process' memory. + */ + long containerOffset; + /** + * The offset of the method within the containing file. + */ + long methodOffset; +} diff --git a/core/java/android/os/instrumentation/IDynamicInstrumentationManager.aidl b/core/java/android/os/instrumentation/IDynamicInstrumentationManager.aidl new file mode 100644 index 000000000000..c45c51d15cc9 --- /dev/null +++ b/core/java/android/os/instrumentation/IDynamicInstrumentationManager.aidl @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os.instrumentation; + +import android.os.instrumentation.ExecutableMethodFileOffsets; +import android.os.instrumentation.MethodDescriptor; +import android.os.instrumentation.TargetProcess; + +/** + * System private API for managing the dynamic attachment of instrumentation. + * + * {@hide} + */ +interface IDynamicInstrumentationManager { + /** Provides ART metadata about the described compiled method within the target process */ + @PermissionManuallyEnforced + @nullable ExecutableMethodFileOffsets getExecutableMethodFileOffsets( + in TargetProcess targetProcess, in MethodDescriptor methodDescriptor); +} diff --git a/core/java/android/os/instrumentation/MethodDescriptor.aidl b/core/java/android/os/instrumentation/MethodDescriptor.aidl new file mode 100644 index 000000000000..055d0ecb66e4 --- /dev/null +++ b/core/java/android/os/instrumentation/MethodDescriptor.aidl @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os.instrumentation; + +/** + * Represents a JVM method, where class fields that make up its signature. + * {@hide} + */ +@JavaDerive(toString=true) +parcelable MethodDescriptor { + /** + * Fully qualified class in reverse.domain.Naming + */ + @utf8InCpp String fullyQualifiedClassName; + /** + * Name of the method. + */ + @utf8InCpp String methodName; + /** + * Fully qualified types of method parameters, or string representations if primitive e.g. "int". + */ + @utf8InCpp String[] fullyQualifiedParameters; +} diff --git a/core/java/android/os/instrumentation/TargetProcess.aidl b/core/java/android/os/instrumentation/TargetProcess.aidl new file mode 100644 index 000000000000..e90780d07ef2 --- /dev/null +++ b/core/java/android/os/instrumentation/TargetProcess.aidl @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.os.instrumentation; + +/** + * Addresses a process that would run on the device. + * Helps disambiguate targeted processes in cases of pid re-use. + * {@hide} + */ +@JavaDerive(toString=true) +parcelable TargetProcess { + int uid; + int pid; + @utf8InCpp String processName; +} diff --git a/core/res/Android.bp b/core/res/Android.bp index 66c2e12f7cdf..cdf88f7ee33c 100644 --- a/core/res/Android.bp +++ b/core/res/Android.bp @@ -171,6 +171,7 @@ android_app { "android.security.flags-aconfig", "com.android.hardware.input.input-aconfig", "aconfig_trade_in_mode_flags", + "art-aconfig-flags", "ranging_aconfig_flags", ], } diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 5913992004b8..a79ad4aa4b89 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -8482,6 +8482,16 @@ android:protectionLevel="signature|privileged" android:featureFlag="com.android.tradeinmode.flags.enable_trade_in_mode" /> + + + + + + javaClass = classLoader.loadClass(descriptor.fullyQualifiedClassName); + Class[] parameters = new Class[descriptor.fullyQualifiedParameters.length]; + for (int i = 0; i < descriptor.fullyQualifiedParameters.length; i++) { + String typeName = descriptor.fullyQualifiedParameters[i]; + boolean isArrayType = typeName.endsWith("[]"); + if (isArrayType) { + typeName = typeName.substring(0, typeName.length() - 2); + } + switch (typeName) { + case "boolean": + parameters[i] = isArrayType ? boolean.class.arrayType() : boolean.class; + break; + case "byte": + parameters[i] = isArrayType ? byte.class.arrayType() : byte.class; + break; + case "char": + parameters[i] = isArrayType ? char.class.arrayType() : char.class; + break; + case "short": + parameters[i] = isArrayType ? short.class.arrayType() : short.class; + break; + case "int": + parameters[i] = isArrayType ? int.class.arrayType() : int.class; + break; + case "long": + parameters[i] = isArrayType ? long.class.arrayType() : long.class; + break; + case "float": + parameters[i] = isArrayType ? float.class.arrayType() : float.class; + break; + case "double": + parameters[i] = isArrayType ? double.class.arrayType() : double.class; + break; + default: + parameters[i] = isArrayType ? classLoader.loadClass(typeName).arrayType() + : classLoader.loadClass(typeName); + } + } + + return javaClass.getDeclaredMethod(descriptor.methodName, parameters); + } catch (ClassNotFoundException | NoSuchMethodException e) { + throw new IllegalArgumentException( + "The specified method cannot be found. Is this descriptor valid? " + + descriptor, e); + } + } +} diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index 3805c02d1bb9..221b8481a30c 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -205,6 +205,7 @@ import com.android.server.os.BugreportManagerService; import com.android.server.os.DeviceIdentifiersPolicyService; import com.android.server.os.NativeTombstoneManagerService; import com.android.server.os.SchedulingPolicyService; +import com.android.server.os.instrumentation.DynamicInstrumentationManagerService; import com.android.server.pdb.PersistentDataBlockService; import com.android.server.people.PeopleService; import com.android.server.permission.access.AccessCheckingService; @@ -2890,6 +2891,13 @@ public final class SystemServer implements Dumpable { mSystemServiceManager.startService(TracingServiceProxy.class); t.traceEnd(); + // UprobeStats DynamicInstrumentationManager + if (com.android.art.flags.Flags.executableMethodFileOffsets()) { + t.traceBegin("StartDynamicInstrumentationManager"); + mSystemServiceManager.startService(DynamicInstrumentationManagerService.class); + t.traceEnd(); + } + // It is now time to start up the app processes... t.traceBegin("MakeLockSettingsServiceReady"); diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/Android.bp b/services/tests/DynamicInstrumentationManagerServiceTests/Android.bp new file mode 100644 index 000000000000..2c2e5fdb68d9 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/Android.bp @@ -0,0 +1,44 @@ +// Copyright (C) 2024 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], + default_team: "trendy_team_system_performance", +} + +android_test { + name: "DynamicInstrumentationManagerServiceTests", + srcs: ["src/**/*.java"], + + static_libs: [ + "androidx.test.core", + "androidx.test.runner", + "hamcrest-library", + "platform-test-annotations", + "services.core", + "testables", + "truth", + ], + + certificate: "platform", + platform_apis: true, + test_suites: [ + "device-tests", + ], +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/AndroidManifest.xml b/services/tests/DynamicInstrumentationManagerServiceTests/AndroidManifest.xml new file mode 100644 index 000000000000..4913d706a72d --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/TEST_MAPPING b/services/tests/DynamicInstrumentationManagerServiceTests/TEST_MAPPING new file mode 100644 index 000000000000..33defed0eae6 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "postsubmit": [ + { + "name": "DynamicInstrumentationManagerServiceTests" + } + ] +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClass.java b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClass.java new file mode 100644 index 000000000000..04073fab2059 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClass.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server; + +public abstract class TestAbstractClass { + abstract void abstractMethod(); + + void concreteMethod() { + } +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClassImpl.java b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClassImpl.java new file mode 100644 index 000000000000..2c25e7a52f73 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestAbstractClassImpl.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server; + +public class TestAbstractClassImpl extends TestAbstractClass { + @Override + void abstractMethod() { + } +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestClass.java b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestClass.java new file mode 100644 index 000000000000..085f5953f0e5 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestClass.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server; + +public class TestClass { + void primitiveParams(boolean a, boolean[] b, byte c, byte[] d, char e, char[] f, short g, + short[] h, int i, int[] j, long k, long[] l, float m, float[] n, double o, double[] p) { + } + + void classParams(String a, String[] b) { + } + + private void privateMethod() { + } + + /** + * docs! + */ + public void publicMethod() { + } + + private static class InnerClass { + private void innerMethod(InnerClass arg, InnerClass[] argArray) { + } + } +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterface.java b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterface.java new file mode 100644 index 000000000000..7af4f254ab1c --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterface.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server; + +/** + * docs! + */ +public interface TestInterface { + /** + * docs! + */ + void interfaceMethod(); + + /** + * docs! + */ + default void defaultMethod() { + } +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterfaceImpl.java b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterfaceImpl.java new file mode 100644 index 000000000000..53aecbc08939 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/TestInterfaceImpl.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server; + +public class TestInterfaceImpl implements TestInterface { + @Override + public void interfaceMethod() { + } +} diff --git a/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/os/instrumentation/ParseMethodDescriptorTest.java b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/os/instrumentation/ParseMethodDescriptorTest.java new file mode 100644 index 000000000000..5492ba6b9dd1 --- /dev/null +++ b/services/tests/DynamicInstrumentationManagerServiceTests/src/com/android/server/os/instrumentation/ParseMethodDescriptorTest.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.os.instrumentation; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import android.os.instrumentation.MethodDescriptor; +import android.platform.test.annotations.Presubmit; + +import androidx.test.filters.SmallTest; + +import com.android.server.TestAbstractClass; +import com.android.server.TestAbstractClassImpl; +import com.android.server.TestClass; +import com.android.server.TestInterface; +import com.android.server.TestInterfaceImpl; + +import org.junit.Test; + +import java.lang.reflect.Method; + + +/** + * Test class for + * {@link DynamicInstrumentationManagerService#parseMethodDescriptor(ClassLoader, + * MethodDescriptor)}. + *

+ * Build/Install/Run: + * atest FrameworksMockingServicesTests:ParseMethodDescriptorTest + */ +@Presubmit +@SmallTest +public class ParseMethodDescriptorTest { + private static final String[] PRIMITIVE_PARAMS = new String[]{ + "boolean", "boolean[]", "byte", "byte[]", "char", "char[]", "short", "short[]", "int", + "int[]", "long", "long[]", "float", "float[]", "double", "double[]"}; + private static final String[] CLASS_PARAMS = + new String[]{"java.lang.String", "java.lang.String[]"}; + + @Test + public void primitiveParams() { + assertNotNull(parseMethodDescriptor(TestClass.class.getName(), "primitiveParams", + PRIMITIVE_PARAMS)); + } + + @Test + public void classParams() { + assertNotNull( + parseMethodDescriptor(TestClass.class.getName(), "classParams", CLASS_PARAMS)); + } + + @Test + public void publicMethod() { + assertNotNull( + parseMethodDescriptor(TestClass.class.getName(), "publicMethod")); + } + + @Test + public void privateMethod() { + assertNotNull( + parseMethodDescriptor(TestClass.class.getName(), "privateMethod")); + } + + @Test + public void innerClass() { + assertNotNull( + parseMethodDescriptor(TestClass.class.getName() + "$InnerClass", "innerMethod", + new String[]{TestClass.class.getName() + "$InnerClass", + TestClass.class.getName() + "$InnerClass[]"})); + } + + @Test + public void interface_concreteMethod() { + assertNotNull( + parseMethodDescriptor(TestInterfaceImpl.class.getName(), "interfaceMethod")); + } + + @Test + public void interface_defaultMethod() { + assertNotNull( + parseMethodDescriptor(TestInterface.class.getName(), "defaultMethod")); + } + + @Test + public void abstractClassImpl_abstractMethod() { + assertNotNull( + parseMethodDescriptor(TestAbstractClassImpl.class.getName(), "abstractMethod")); + } + + @Test + public void abstractClass_concreteMethod() { + assertNotNull( + parseMethodDescriptor(TestAbstractClass.class.getName(), "concreteMethod")); + } + + @Test + public void notFound_illegalArgumentException() { + assertThrows(IllegalArgumentException.class, () -> parseMethodDescriptor("foo", "bar")); + assertThrows(IllegalArgumentException.class, + () -> parseMethodDescriptor(TestClass.class.getName(), "bar")); + assertThrows(IllegalArgumentException.class, + () -> parseMethodDescriptor(TestClass.class.getName(), "primitiveParams", + new String[]{"int"})); + } + + private Method parseMethodDescriptor(String fqcn, String methodName) { + return DynamicInstrumentationManagerService.parseMethodDescriptor( + getClass().getClassLoader(), + getMethodDescriptor(fqcn, methodName, new String[]{})); + } + + private Method parseMethodDescriptor(String fqcn, String methodName, String[] fqParameters) { + return DynamicInstrumentationManagerService.parseMethodDescriptor( + getClass().getClassLoader(), + getMethodDescriptor(fqcn, methodName, fqParameters)); + } + + private MethodDescriptor getMethodDescriptor(String fqcn, String methodName, + String[] fqParameters) { + MethodDescriptor methodDescriptor = new MethodDescriptor(); + methodDescriptor.fullyQualifiedClassName = fqcn; + methodDescriptor.methodName = methodName; + methodDescriptor.fullyQualifiedParameters = fqParameters; + return methodDescriptor; + } + + +} -- cgit v1.2.3-59-g8ed1b