diff options
| -rw-r--r-- | build/Android.common_build.mk | 2 | ||||
| -rw-r--r-- | runtime/arch/arm/quick_entrypoints_arm.S | 42 | ||||
| -rw-r--r-- | runtime/arch/x86/quick_entrypoints_x86.S | 43 | ||||
| -rw-r--r-- | runtime/common_runtime_test.cc | 20 | ||||
| -rw-r--r-- | runtime/java_vm_ext.cc | 31 | ||||
| -rw-r--r-- | test/136-daemon-jni-shutdown/daemon_jni_shutdown.cc | 14 | ||||
| -rw-r--r-- | test/570-checker-osr/osr.cc | 2 | ||||
| -rw-r--r-- | test/601-verifier-fails/expected.txt | 4 | ||||
| -rw-r--r-- | test/601-verifier-fails/info.txt | 18 | ||||
| -rw-r--r-- | test/601-verifier-fails/smali/construct.smali | 25 | ||||
| -rw-r--r-- | test/601-verifier-fails/smali/iget.smali | 25 | ||||
| -rw-r--r-- | test/601-verifier-fails/smali/iput.smali | 25 | ||||
| -rw-r--r-- | test/601-verifier-fails/smali/sput.smali | 23 | ||||
| -rw-r--r-- | test/601-verifier-fails/src/Main.java | 40 | ||||
| -rw-r--r-- | test/604-hot-static-interface/hot_static_interface.cc | 7 | ||||
| -rw-r--r-- | test/605-new-string-from-bytes/expected.txt | 0 | ||||
| -rw-r--r-- | test/605-new-string-from-bytes/info.txt | 2 | ||||
| -rw-r--r-- | test/605-new-string-from-bytes/src/Main.java | 45 |
18 files changed, 312 insertions, 56 deletions
diff --git a/build/Android.common_build.mk b/build/Android.common_build.mk index 0235a308f8..fc4dd55d67 100644 --- a/build/Android.common_build.mk +++ b/build/Android.common_build.mk @@ -347,7 +347,7 @@ ifeq ($(HOST_OS),linux) ifdef SANITIZE_TARGET art_target_non_debug_cflags += -Wframe-larger-than=6400 else - art_target_non_debug_cflags += -Wframe-larger-than=1728 + art_target_non_debug_cflags += -Wframe-larger-than=1736 endif endif endif diff --git a/runtime/arch/arm/quick_entrypoints_arm.S b/runtime/arch/arm/quick_entrypoints_arm.S index 1bba4f9b01..5209bb6ab6 100644 --- a/runtime/arch/arm/quick_entrypoints_arm.S +++ b/runtime/arch/arm/quick_entrypoints_arm.S @@ -88,6 +88,36 @@ #endif .endm + /* + * Macro that sets up the callee save frame to conform with + * Runtime::CreateCalleeSaveMethod(kRefsOnly) + * and preserves the value of rTemp2 at entry. + */ +.macro SETUP_REFS_ONLY_CALLEE_SAVE_FRAME_PRESERVE_RTEMP2 rTemp1, rTemp2 + push {r5-r8, r10-r11, lr} @ 7 words of callee saves + .cfi_adjust_cfa_offset 28 + .cfi_rel_offset r5, 0 + .cfi_rel_offset r6, 4 + .cfi_rel_offset r7, 8 + .cfi_rel_offset r8, 12 + .cfi_rel_offset r10, 16 + .cfi_rel_offset r11, 20 + .cfi_rel_offset lr, 24 + sub sp, #4 @ bottom word will hold Method* + .cfi_adjust_cfa_offset 4 + str \rTemp2, [sp, #0] @ save rTemp2 + RUNTIME_CURRENT2 \rTemp1, \rTemp2 @ Load Runtime::Current into rTemp1. + ldr \rTemp1, [\rTemp1, #RUNTIME_REFS_ONLY_CALLEE_SAVE_FRAME_OFFSET] @ rTemp1 is kRefsOnly Method*. + ldr \rTemp2, [sp, #0] @ restore rTemp2 + str \rTemp1, [sp, #0] @ Place Method* at bottom of stack. + str sp, [r9, #THREAD_TOP_QUICK_FRAME_OFFSET] @ Place sp in Thread::Current()->top_quick_frame. + + // Ugly compile-time check, but we only have the preprocessor. +#if (FRAME_SIZE_REFS_ONLY_CALLEE_SAVE != 28 + 4) +#error "REFS_ONLY_CALLEE_SAVE_FRAME(ARM) size not as expected." +#endif +.endm + .macro RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME add sp, #4 @ bottom word holds Method* .cfi_adjust_cfa_offset -4 @@ -831,23 +861,13 @@ END \name .macro FOUR_ARG_DOWNCALL name, entrypoint, return .extern \entrypoint ENTRY \name - sub sp, #12 @ alignment padding - .cfi_adjust_cfa_offset 12 - push {r3} @ Save r3 as is it used as a temp register in the - .cfi_adjust_cfa_offset 4 @ expansion of the SETUP_REFS_ONLY_CALLEE_SAVE_FRAME - .cfi_rel_offset r3, 0 @ macro below, which clobbers its arguments. - SETUP_REFS_ONLY_CALLEE_SAVE_FRAME r3, r12 @ save callee saves in case of GC - ldr r3, [sp, 32] @ restore r3 - .cfi_restore r3 - + SETUP_REFS_ONLY_CALLEE_SAVE_FRAME_PRESERVE_RTEMP2 r12, r3 @ save callee saves in case of GC str r9, [sp, #-16]! @ expand the frame and pass Thread::Current .cfi_adjust_cfa_offset 16 bl \entrypoint add sp, #16 @ strip the extra frame .cfi_adjust_cfa_offset -16 RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME - add sp, #16 @ pop r3 + padding - .cfi_adjust_cfa_offset -16 \return END \name .endm diff --git a/runtime/arch/x86/quick_entrypoints_x86.S b/runtime/arch/x86/quick_entrypoints_x86.S index 485da9fe33..2d7f664809 100644 --- a/runtime/arch/x86/quick_entrypoints_x86.S +++ b/runtime/arch/x86/quick_entrypoints_x86.S @@ -73,6 +73,38 @@ MACRO2(SETUP_REFS_ONLY_CALLEE_SAVE_FRAME, got_reg, temp_reg) #endif END_MACRO + /* + * Macro that sets up the callee save frame to conform with + * Runtime::CreateCalleeSaveMethod(kRefsOnly) + * and preserves the value of got_reg at entry. + */ +MACRO2(SETUP_REFS_ONLY_CALLEE_SAVE_FRAME_PRESERVE_GOT_REG, got_reg, temp_reg) + PUSH edi // Save callee saves (ebx is saved/restored by the upcall) + PUSH esi + PUSH ebp + pushl REG_VAR(got_reg) // Save got_reg + subl MACRO_LITERAL(8), %esp // Grow stack by 2 words. + CFI_ADJUST_CFA_OFFSET(8) + + SETUP_GOT_NOSAVE RAW_VAR(got_reg) + // Load Runtime::instance_ from GOT. + movl SYMBOL(_ZN3art7Runtime9instance_E)@GOT(REG_VAR(got_reg)), REG_VAR(temp_reg) + movl (REG_VAR(temp_reg)), REG_VAR(temp_reg) + // Push save all callee-save method. + pushl RUNTIME_REFS_ONLY_CALLEE_SAVE_FRAME_OFFSET(REG_VAR(temp_reg)) + CFI_ADJUST_CFA_OFFSET(4) + // Store esp as the top quick frame. + movl %esp, %fs:THREAD_TOP_QUICK_FRAME_OFFSET + // Restore got_reg. + movl 12(%esp), REG_VAR(got_reg) + + // Ugly compile-time check, but we only have the preprocessor. + // Last +4: implicit return address pushed on stack when caller made call. +#if (FRAME_SIZE_REFS_ONLY_CALLEE_SAVE != 3*4 + 16 + 4) +#error "REFS_ONLY_CALLEE_SAVE_FRAME(X86) size not as expected." +#endif +END_MACRO + MACRO0(RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME) addl MACRO_LITERAL(16), %esp // Unwind stack up to saved values CFI_ADJUST_CFA_OFFSET(-16) @@ -686,14 +718,7 @@ END_MACRO MACRO3(FOUR_ARG_DOWNCALL, c_name, cxx_name, return_macro) DEFINE_FUNCTION VAR(c_name) - subl MACRO_LITERAL(12), %esp // alignment padding - CFI_ADJUST_CFA_OFFSET(12) - PUSH ebx // Save ebx as the expansion of the - // SETUP_REFS_ONLY_CALLEE_SAVE_FRAME - // macro below clobbers it. - SETUP_REFS_ONLY_CALLEE_SAVE_FRAME ebx, ebx // save ref containing registers for GC - movl 28(%esp), %ebx // restore ebx - CFI_RESTORE_REG ebx + SETUP_REFS_ONLY_CALLEE_SAVE_FRAME_PRESERVE_GOT_REG ebx, ebx // save ref containing registers for GC // Outgoing argument set up subl MACRO_LITERAL(12), %esp // alignment padding @@ -708,8 +733,6 @@ MACRO3(FOUR_ARG_DOWNCALL, c_name, cxx_name, return_macro) addl MACRO_LITERAL(32), %esp // pop arguments CFI_ADJUST_CFA_OFFSET(-32) RESTORE_REFS_ONLY_CALLEE_SAVE_FRAME // restore frame up to return address - addl MACRO_LITERAL(16), %esp // pop ebx + padding - CFI_ADJUST_CFA_OFFSET(-16) CALL_MACRO(return_macro) // return or deliver exception END_FUNCTION VAR(c_name) END_MACRO diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc index 5bdb36cafc..f58af5a8da 100644 --- a/runtime/common_runtime_test.cc +++ b/runtime/common_runtime_test.cc @@ -418,6 +418,26 @@ void CommonRuntimeTestImpl::TearDown() { (*icu_cleanup_fn)(); Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test + + // Manually closing the JNI libraries. + // Runtime does not support repeatedly doing JNI->CreateVM, thus we need to manually clean up the + // dynamic linking loader so that gtests would not fail. + // Bug: 25785594 + if (runtime_->IsStarted()) { + { + // We retrieve the handle by calling dlopen on the library. To close it, we need to call + // dlclose twice, the first time to undo our dlopen and the second time to actually unload it. + // See man dlopen. + void* handle = dlopen("libjavacore.so", RTLD_LAZY); + dlclose(handle); + CHECK_EQ(0, dlclose(handle)); + } + { + void* handle = dlopen("libopenjdkd.so", RTLD_LAZY); + dlclose(handle); + CHECK_EQ(0, dlclose(handle)); + } + } } static std::string GetDexFileName(const std::string& jar_prefix, bool host) { diff --git a/runtime/java_vm_ext.cc b/runtime/java_vm_ext.cc index c2164126e1..d983a9fa19 100644 --- a/runtime/java_vm_ext.cc +++ b/runtime/java_vm_ext.cc @@ -74,10 +74,6 @@ class SharedLibrary { if (self != nullptr) { self->GetJniEnv()->DeleteWeakGlobalRef(class_loader_); } - - if (!needs_native_bridge_) { - android::CloseNativeLibrary(handle_); - } } jweak GetClassLoader() const { @@ -275,7 +271,8 @@ class Libraries { REQUIRES(!Locks::jni_libraries_lock_) SHARED_REQUIRES(Locks::mutator_lock_) { ScopedObjectAccessUnchecked soa(Thread::Current()); - std::vector<SharedLibrary*> unload_libraries; + typedef void (*JNI_OnUnloadFn)(JavaVM*, void*); + std::vector<JNI_OnUnloadFn> unload_functions; { MutexLock mu(soa.Self(), *Locks::jni_libraries_lock_); for (auto it = libraries_.begin(); it != libraries_.end(); ) { @@ -286,7 +283,15 @@ class Libraries { // the native libraries of the boot class loader. if (class_loader != nullptr && soa.Self()->IsJWeakCleared(class_loader)) { - unload_libraries.push_back(library); + void* const sym = library->FindSymbol("JNI_OnUnload", nullptr); + if (sym == nullptr) { + VLOG(jni) << "[No JNI_OnUnload found in \"" << library->GetPath() << "\"]"; + } else { + VLOG(jni) << "[JNI_OnUnload found for \"" << library->GetPath() << "\"]"; + JNI_OnUnloadFn jni_on_unload = reinterpret_cast<JNI_OnUnloadFn>(sym); + unload_functions.push_back(jni_on_unload); + } + delete library; it = libraries_.erase(it); } else { ++it; @@ -294,17 +299,9 @@ class Libraries { } } // Do this without holding the jni libraries lock to prevent possible deadlocks. - typedef void (*JNI_OnUnloadFn)(JavaVM*, void*); - for (auto library : unload_libraries) { - void* const sym = library->FindSymbol("JNI_OnUnload", nullptr); - if (sym == nullptr) { - VLOG(jni) << "[No JNI_OnUnload found in \"" << library->GetPath() << "\"]"; - } else { - VLOG(jni) << "[JNI_OnUnload found for \"" << library->GetPath() << "\"]: Calling..."; - JNI_OnUnloadFn jni_on_unload = reinterpret_cast<JNI_OnUnloadFn>(sym); - jni_on_unload(soa.Vm(), nullptr); - } - delete library; + for (JNI_OnUnloadFn fn : unload_functions) { + VLOG(jni) << "Calling JNI_OnUnload"; + (*fn)(soa.Vm(), nullptr); } } diff --git a/test/136-daemon-jni-shutdown/daemon_jni_shutdown.cc b/test/136-daemon-jni-shutdown/daemon_jni_shutdown.cc index b7293015cf..c9110a905d 100644 --- a/test/136-daemon-jni-shutdown/daemon_jni_shutdown.cc +++ b/test/136-daemon-jni-shutdown/daemon_jni_shutdown.cc @@ -27,20 +27,8 @@ namespace art { namespace { static volatile std::atomic<bool> vm_was_shutdown(false); -static const int kThreadCount = 4; - -static std::atomic<int> barrier_count(kThreadCount + 1); - -static void JniThreadBarrierWait() { - barrier_count--; - while (barrier_count.load() != 0) { - usleep(1000); - } -} extern "C" JNIEXPORT void JNICALL Java_Main_waitAndCallIntoJniEnv(JNIEnv* env, jclass) { - // Wait for all threads to enter JNI together. - JniThreadBarrierWait(); // Wait until the runtime is shutdown. while (!vm_was_shutdown.load()) { usleep(1000); @@ -52,8 +40,6 @@ extern "C" JNIEXPORT void JNICALL Java_Main_waitAndCallIntoJniEnv(JNIEnv* env, j // NO_RETURN does not work with extern "C" for target builds. extern "C" JNIEXPORT void JNICALL Java_Main_destroyJavaVMAndExit(JNIEnv* env, jclass) { - // Wait for all threads to enter JNI together. - JniThreadBarrierWait(); // Fake up the managed stack so we can detach. Thread* const self = Thread::Current(); self->SetTopOfStack(nullptr); diff --git a/test/570-checker-osr/osr.cc b/test/570-checker-osr/osr.cc index 2fa5800e5c..cf413bab71 100644 --- a/test/570-checker-osr/osr.cc +++ b/test/570-checker-osr/osr.cc @@ -136,7 +136,7 @@ class OsrCheckVisitor : public StackVisitor { if (m_name.compare(method_name_) == 0) { while (jit->GetCodeCache()->LookupOsrMethodHeader(m) == nullptr) { // Sleep to yield to the compiler thread. - sleep(0); + usleep(1000); // Will either ensure it's compiled or do the compilation itself. jit->CompileMethod(m, Thread::Current(), /* osr */ true); } diff --git a/test/601-verifier-fails/expected.txt b/test/601-verifier-fails/expected.txt new file mode 100644 index 0000000000..8399969a2d --- /dev/null +++ b/test/601-verifier-fails/expected.txt @@ -0,0 +1,4 @@ +passed A +passed B +passed C +passed D diff --git a/test/601-verifier-fails/info.txt b/test/601-verifier-fails/info.txt new file mode 100644 index 0000000000..f77de05ac7 --- /dev/null +++ b/test/601-verifier-fails/info.txt @@ -0,0 +1,18 @@ +The situations in these tests were discovered by running the mutating +dexfuzz on the DEX files of fuzzingly random generated Java test. + +(A) b/28908555: + soft verification failure (on the final field modification) should + not hide the hard verification failure (on the type mismatch) to + avoid compiler crash later on +(B) b/29070461: + hard verification failure (not calling super in constructor) should + bail immediately and not allow soft verification failures to pile up + behind it to avoid fatal message later on +(C) b/29068831: + access validation should occur prior to null reference check +(D) b/29126870: + soft verification failure (cannot access) should not hide the hard + verification failure (non-reference type) to avoid a compiler crash + later on + diff --git a/test/601-verifier-fails/smali/construct.smali b/test/601-verifier-fails/smali/construct.smali new file mode 100644 index 0000000000..417ced94fa --- /dev/null +++ b/test/601-verifier-fails/smali/construct.smali @@ -0,0 +1,25 @@ +# +# 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 public LB; +.super Ljava/lang/Object; + +.method public constructor <init>()V + .registers 1 + if-eqz v0, :bail + invoke-direct {v0}, LB;->append(Ljava/lang/String;)V +:bail + return-void +.end method diff --git a/test/601-verifier-fails/smali/iget.smali b/test/601-verifier-fails/smali/iget.smali new file mode 100644 index 0000000000..5c045e6b76 --- /dev/null +++ b/test/601-verifier-fails/smali/iget.smali @@ -0,0 +1,25 @@ +# +# 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 public LD; +.super Ljava/lang/Object; + +.method public constructor <init>()V + .registers 2 + invoke-direct {v1}, Ljava/lang/Object;-><init>()V + const v0, 2 + iget v1, v0, LMain;->privateField:I + return-void +.end method diff --git a/test/601-verifier-fails/smali/iput.smali b/test/601-verifier-fails/smali/iput.smali new file mode 100644 index 0000000000..bd8b9280c0 --- /dev/null +++ b/test/601-verifier-fails/smali/iput.smali @@ -0,0 +1,25 @@ +# +# 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 public LC; +.super Ljava/lang/Object; + +.method public constructor <init>()V + .registers 2 + invoke-direct {v1}, Ljava/lang/Object;-><init>()V + const v0, 0 + iput-object v0, v0, LMain;->staticPrivateField:Ljava/lang/String; + return-void +.end method diff --git a/test/601-verifier-fails/smali/sput.smali b/test/601-verifier-fails/smali/sput.smali new file mode 100644 index 0000000000..e8e56acf13 --- /dev/null +++ b/test/601-verifier-fails/smali/sput.smali @@ -0,0 +1,23 @@ +# +# 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 public LA; +.super Ljava/lang/Object; + +.method public foo(I)V +.registers 2 + sput v1, LMain;->staticFinalField:Ljava/lang/String; + return-void +.end method diff --git a/test/601-verifier-fails/src/Main.java b/test/601-verifier-fails/src/Main.java new file mode 100644 index 0000000000..a6a07fda79 --- /dev/null +++ b/test/601-verifier-fails/src/Main.java @@ -0,0 +1,40 @@ +/* + * 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. + */ + +public class Main { + + public static final String staticFinalField = null; + + private static String staticPrivateField = null; + + private int privateField = 0; + + private static void test(String name) throws Exception { + try { + Class<?> a = Class.forName(name); + a.newInstance(); + } catch (java.lang.LinkageError e) { + System.out.println("passed " + name); + } + } + + public static void main(String[] args) throws Exception { + test("A"); + test("B"); + test("C"); + test("D"); + } +} diff --git a/test/604-hot-static-interface/hot_static_interface.cc b/test/604-hot-static-interface/hot_static_interface.cc index 71877f5baa..475a11d351 100644 --- a/test/604-hot-static-interface/hot_static_interface.cc +++ b/test/604-hot-static-interface/hot_static_interface.cc @@ -17,6 +17,7 @@ #include "art_method.h" #include "jit/jit.h" #include "jit/jit_code_cache.h" +#include "jit/profiling_info.h" #include "oat_quick_method_header.h" #include "scoped_thread_state_change.h" #include "ScopedUtfChars.h" @@ -43,15 +44,17 @@ extern "C" JNIEXPORT void JNICALL Java_Main_waitUntilJitted(JNIEnv* env, jit::JitCodeCache* code_cache = jit->GetCodeCache(); OatQuickMethodHeader* header = nullptr; + // Make sure there is a profiling info, required by the compiler. + ProfilingInfo::Create(soa.Self(), method, /* retry_allocation */ true); while (true) { header = OatQuickMethodHeader::FromEntryPoint(method->GetEntryPointFromQuickCompiledCode()); if (code_cache->ContainsPc(header->GetCode())) { break; } else { // Sleep to yield to the compiler thread. - sleep(0); + usleep(1000); // Will either ensure it's compiled or do the compilation itself. - jit->CompileMethod(method, Thread::Current(), /* osr */ false); + jit->CompileMethod(method, soa.Self(), /* osr */ false); } } } diff --git a/test/605-new-string-from-bytes/expected.txt b/test/605-new-string-from-bytes/expected.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/605-new-string-from-bytes/expected.txt diff --git a/test/605-new-string-from-bytes/info.txt b/test/605-new-string-from-bytes/info.txt new file mode 100644 index 0000000000..be02c43bbd --- /dev/null +++ b/test/605-new-string-from-bytes/info.txt @@ -0,0 +1,2 @@ +Regression test for the newStringFromBytes entrypoint, +which used to wrongly setup the stack. diff --git a/test/605-new-string-from-bytes/src/Main.java b/test/605-new-string-from-bytes/src/Main.java new file mode 100644 index 0000000000..7dc0c15d99 --- /dev/null +++ b/test/605-new-string-from-bytes/src/Main.java @@ -0,0 +1,45 @@ +/* + * 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.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class Main { + + public static void main(String[] args) throws Exception { + Class c = Class.forName("java.lang.StringFactory"); + Method m = c.getDeclaredMethod("newStringFromBytes", byte[].class, int.class); + + // Loop over allocations to get more chances of doing GC while in the + // newStringFromBytes intrinsic. + for (int i = 0; i < 10; i++) { + try { + byte[] f = new byte[100000000]; + f[0] = (byte)i; + f[1] = (byte)i; + m.invoke(null, f, 0); + } catch (InvocationTargetException e) { + if (e.getCause() instanceof OutOfMemoryError) { + // Ignore, this is a stress test. + } else { + throw e; + } + } catch (OutOfMemoryError e) { + // Ignore, this is a stress test. + } + } + } +} |