diff options
| author | 2017-03-27 13:02:41 -0700 | |
|---|---|---|
| committer | 2017-03-28 09:38:42 -0700 | |
| commit | 37aa4c99260985e67cd466397e58927e7a0d2871 (patch) | |
| tree | c563b378aedd88dbac5b4732ce62f6ebc85ba4d1 | |
| parent | 1082e0e46ddfae2ed9cc3d0a3d6025e7335ac239 (diff) | |
Add test to verify dex file passed to agent
Bug: 31455788
Test: ./test.py --host -j40
Change-Id: Ibd75e21d6d9f19d9bb125a43a0f54262724e894c
| -rw-r--r-- | test/983-source-transform-verify/expected.txt | 1 | ||||
| -rw-r--r-- | test/983-source-transform-verify/info.txt | 1 | ||||
| -rwxr-xr-x | test/983-source-transform-verify/run | 17 | ||||
| -rw-r--r-- | test/983-source-transform-verify/source_transform.cc | 98 | ||||
| -rw-r--r-- | test/983-source-transform-verify/source_transform.h | 30 | ||||
| -rw-r--r-- | test/983-source-transform-verify/src/Main.java | 34 | ||||
| -rw-r--r-- | test/983-source-transform-verify/src/Transform.java | 28 | ||||
| -rw-r--r-- | test/Android.bp | 1 | ||||
| -rw-r--r-- | test/ti-agent/common_load.cc | 2 |
9 files changed, 212 insertions, 0 deletions
diff --git a/test/983-source-transform-verify/expected.txt b/test/983-source-transform-verify/expected.txt new file mode 100644 index 0000000000..85746f39f5 --- /dev/null +++ b/test/983-source-transform-verify/expected.txt @@ -0,0 +1 @@ +Dex file hook for Transform diff --git a/test/983-source-transform-verify/info.txt b/test/983-source-transform-verify/info.txt new file mode 100644 index 0000000000..875a5f6ec1 --- /dev/null +++ b/test/983-source-transform-verify/info.txt @@ -0,0 +1 @@ +Tests basic functions in the jvmti plugin. diff --git a/test/983-source-transform-verify/run b/test/983-source-transform-verify/run new file mode 100755 index 0000000000..c6e62ae6cd --- /dev/null +++ b/test/983-source-transform-verify/run @@ -0,0 +1,17 @@ +#!/bin/bash +# +# Copyright 2016 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. + +./default-run "$@" --jvmti diff --git a/test/983-source-transform-verify/source_transform.cc b/test/983-source-transform-verify/source_transform.cc new file mode 100644 index 0000000000..21ba7a307a --- /dev/null +++ b/test/983-source-transform-verify/source_transform.cc @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2017 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. + */ + +#include <inttypes.h> +#include <stdio.h> +#include <string.h> + +#include <iostream> +#include <vector> + +#include "android-base/stringprintf.h" + +#include "base/logging.h" +#include "base/macros.h" +#include "dex_file.h" +#include "jit/jit.h" +#include "jni.h" +#include "native_stack_dump.h" +#include "jvmti.h" +#include "runtime.h" +#include "scoped_thread_state_change-inl.h" +#include "thread-inl.h" +#include "thread_list.h" + +#include "ti-agent/common_helper.h" +#include "ti-agent/common_load.h" + +namespace art { +namespace Test983SourceTransformVerify { + +// The hook we are using. +void JNICALL CheckDexFileHook(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED, + JNIEnv* jni_env ATTRIBUTE_UNUSED, + jclass class_being_redefined, + jobject loader ATTRIBUTE_UNUSED, + const char* name, + jobject protection_domain ATTRIBUTE_UNUSED, + jint class_data_len, + const unsigned char* class_data, + jint* new_class_data_len ATTRIBUTE_UNUSED, + unsigned char** new_class_data ATTRIBUTE_UNUSED) { + if (class_being_redefined == nullptr) { + // Something got loaded concurrently. Just ignore it for now. + return; + } + std::cout << "Dex file hook for " << name << std::endl; + if (IsJVM()) { + return; + } + std::string error; + std::unique_ptr<const DexFile> dex(DexFile::Open(class_data, + class_data_len, + "fake_location.dex", + 0, + nullptr, + true, + true, + &error)); + if (dex.get() == nullptr) { + std::cout << "Failed to verify dex file for " << name << " because " << error << std::endl; + } +} + +// Get all capabilities except those related to retransformation. +jint OnLoad(JavaVM* vm, + char* options ATTRIBUTE_UNUSED, + void* reserved ATTRIBUTE_UNUSED) { + if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { + printf("Unable to get jvmti env!\n"); + return 1; + } + SetAllCapabilities(jvmti_env); + jvmtiEventCallbacks cb; + memset(&cb, 0, sizeof(cb)); + cb.ClassFileLoadHook = CheckDexFileHook; + if (jvmti_env->SetEventCallbacks(&cb, sizeof(cb)) != JVMTI_ERROR_NONE) { + printf("Unable to set class file load hook cb!\n"); + return 1; + } + return 0; +} + + +} // namespace Test983SourceTransformVerify +} // namespace art diff --git a/test/983-source-transform-verify/source_transform.h b/test/983-source-transform-verify/source_transform.h new file mode 100644 index 0000000000..db9415aec1 --- /dev/null +++ b/test/983-source-transform-verify/source_transform.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017 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. + */ + +#ifndef ART_TEST_983_SOURCE_TRANSFORM_VERIFY_SOURCE_TRANSFORM_H_ +#define ART_TEST_983_SOURCE_TRANSFORM_VERIFY_SOURCE_TRANSFORM_H_ + +#include <jni.h> + +namespace art { +namespace Test983SourceTransformVerify { + +jint OnLoad(JavaVM* vm, char* options, void* reserved); + +} // namespace Test983SourceTransformVerify +} // namespace art + +#endif // ART_TEST_983_SOURCE_TRANSFORM_VERIFY_SOURCE_TRANSFORM_H_ diff --git a/test/983-source-transform-verify/src/Main.java b/test/983-source-transform-verify/src/Main.java new file mode 100644 index 0000000000..35fb945209 --- /dev/null +++ b/test/983-source-transform-verify/src/Main.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2016 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. + */ + +import java.util.Base64; +public class Main { + + public static void main(String[] args) { + doTest(); + } + + public static void doTest() { + Transform abc = new Transform(); + enableCommonRetransformation(true); + doCommonClassRetransformation(Transform.class); + enableCommonRetransformation(false); + } + + // Transforms the class + private static native void doCommonClassRetransformation(Class<?>... target); + private static native void enableCommonRetransformation(boolean enable); +} diff --git a/test/983-source-transform-verify/src/Transform.java b/test/983-source-transform-verify/src/Transform.java new file mode 100644 index 0000000000..8e8af355da --- /dev/null +++ b/test/983-source-transform-verify/src/Transform.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 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. + */ + +class Transform { + public void sayHi() { + // Use lower 'h' to make sure the string will have a different string id + // than the transformation (the transformation code is the same except + // the actual printed String, which was making the test inacurately passing + // in JIT mode when loading the string from the dex cache, as the string ids + // of the two different strings were the same). + // We know the string ids will be different because lexicographically: + // "Goodbye" < "LTransform;" < "hello". + System.out.println("hello"); + } +} diff --git a/test/Android.bp b/test/Android.bp index 4ebfd7429a..ec40acf279 100644 --- a/test/Android.bp +++ b/test/Android.bp @@ -276,6 +276,7 @@ art_cc_defaults { "944-transform-classloaders/classloader.cc", "945-obsolete-native/obsolete_native.cc", "980-redefine-object/redefine_object.cc", + "983-source-transform-verify/source_transform.cc", ], shared_libs: [ "libbase", diff --git a/test/ti-agent/common_load.cc b/test/ti-agent/common_load.cc index 68c7d50dbc..303e8244da 100644 --- a/test/ti-agent/common_load.cc +++ b/test/ti-agent/common_load.cc @@ -27,6 +27,7 @@ #include "901-hello-ti-agent/basics.h" #include "909-attach-agent/attach.h" #include "936-search-onload/search_onload.h" +#include "983-source-transform-verify/source_transform.h" namespace art { @@ -123,6 +124,7 @@ static AgentLib agents[] = { { "945-obsolete-native", common_redefine::OnLoad, nullptr }, { "981-dedup-original-dex", common_retransform::OnLoad, nullptr }, { "982-ok-no-retransform", common_retransform::OnLoad, nullptr }, + { "983-source-transform-verify", Test983SourceTransformVerify::OnLoad, nullptr }, }; static AgentLib* FindAgent(char* name) { |