harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r545554 [13/13] - in /harmony/enhanced/buildtest/branches/2.0/tests/reliability: ./ run/ src/ src/java/ src/java/org/ src/java/org/apache/ src/java/org/apache/harmony/ src/java/org/apache/harmony/test/ src/java/org/apache/harmony/test/relia...
Date Fri, 08 Jun 2007 16:35:10 GMT
Added: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeResourceOrientedTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeResourceOrientedTest.java?view=auto&rev=545554
==============================================================================
--- harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeResourceOrientedTest.java (added)
+++ harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeResourceOrientedTest.java Fri Jun  8 09:34:52 2007
@@ -0,0 +1,499 @@
+/*
+ *
+ * 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.
+ */
+
+/**
+ * @author Aleksey Ignatenko
+ * @version $Revision: 1.0 $
+ */
+
+package org.apache.harmony.test.reliability.vm.finalization;
+
+import org.apache.harmony.test.reliability.share.Test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.ref.*;
+import java.util.ArrayList;
+import java.util.Random;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+/*
+ * Goal: check VM finalization subsystem 
+ * The test relies that finalization is not garanteed by spec
+ * Accepted parameters:
+ * param[0] - number of threads to start
+ * param[1] - path to folder where class files are located - to pack them in jar file
+ * param[2] - path to folder where temporary jar file are to be located
+
+ * The test consists of 5 parts executed consequently
+ * 1 part - IOFinalizerActivities
+ *   - the test case finds class files of reliability suit and transfers them 
+ *   into 1 jar file in Tmp directory in finalizer. Execution is done in params[0] threads 
+ * 2 part - RefFinalizerActivities
+ *   - the test case builds list of entries with different reference types: weak, phantom, soft, solid
+ *   - operations with ReferenceQueue are done for phantom refs in finilizer
+ *   - Plus running through the list and creating additional not tracked list 
+ * 3 part - SimplethreaddingActivities
+ *   - the test starts threads in finalizer
+ * 4 part - in the parts 1-3 finalize sections were instrumented to save reference to finalizer thread.
+ *   The test gets some info about finalizer thread.   
+ */
+
+public class FinalizeResourceOrientedTest extends Test {
+    volatile static boolean failed = false;
+    public final static int NUMBER_OF_THREADS_OR_INSTANCES = 10;
+    public final static int NUMBER_OF_CHAIN_OBJECTS = 100;
+    public static int numberOfThreads = NUMBER_OF_THREADS_OR_INSTANCES;
+    public static int numberOfObjects = NUMBER_OF_THREADS_OR_INSTANCES;
+    public static int numberOfChainObjects = NUMBER_OF_CHAIN_OBJECTS;
+    static Thread finThread = null;
+    public static String classFolder = System.getProperty("java.class.path");
+    public static String tmpFolder = System.getProperty("java.io.tmpdir");;
+    
+    synchronized public static void reportFinalizeThread(Thread t){
+        if (finThread == null){
+            finThread = t;
+        }
+    }
+    
+    public static void setFailed(){
+        failed = true;
+    }
+    
+    public static void commenterror(String msg){
+        log.add(msg);
+    }
+    
+    public static void main(String[] params){
+        System.exit(new FinalizeResourceOrientedTest().test(params));
+    }
+
+    public int test(String []params){
+        parseParams(params);
+        
+        // Stage 1: launch IOFinalizerActivities test
+        try{
+            for (int i=0; i<numberOfObjects; i++){
+                IOFinalizerActivities act = new IOFinalizerActivities();
+                act.Test();
+            }            
+        }catch(Throwable t){
+            failed = true;
+        }
+        
+        System.gc();
+        System.runFinalization();
+        
+        if (failed){
+            return fail("rootcause is in IOFinalizerActivities");
+        }
+        
+        
+        // Stage 2: launch RefFinalizerActivities test
+        RefFinalizerActivities[] thrds = new RefFinalizerActivities[numberOfThreads];
+        for (int i=0; i<thrds.length; i++ ){
+            thrds[i] = new RefFinalizerActivities();
+            thrds[i].start();
+        }
+
+        System.gc();
+        System.runFinalization();
+        
+        for (int i=0; i<thrds.length; i++ ){
+            try {
+                thrds[i].join();
+                thrds[i].hintnull();
+                thrds[i] = null;
+            } catch (InterruptedException e) {
+                failed = true;
+                log.add(e);
+            }
+            System.gc();
+            System.runFinalization();
+            
+        }
+        thrds = null;
+        if (failed){
+            return fail("rootcause is in RefFinalizerActivities");
+        }
+
+        // Stage 3: launch SimplethreaddingActivities test
+        for (int i = 0; i < numberOfObjects; i++){
+            SimplethreaddingLaunchActivities sta = new SimplethreaddingLaunchActivities();
+            sta = null;
+        }
+        System.gc();
+        System.runFinalization();
+        
+
+        // Stage 4: test finalizer thread if any
+        while (finThread != null){
+            try{
+                finThread.checkAccess();
+            } catch (SecurityException e){
+                break;
+            }
+            finThread.getName();
+            finThread.getThreadGroup();
+            finThread.getState();
+            break;
+        }
+        
+        
+        // Final stage: results
+        if (failed){
+            return fail("rootcause is RestoreObjectsFinalizerActivities");
+        }
+        
+        
+        return pass("OK");
+    }
+    
+    public void parseParams(String[] params) {
+
+        if (params.length >= 1) {
+            numberOfThreads = Integer.parseInt(params[0]);
+        }
+        if (params.length >= 2) {
+            classFolder = params[1];
+        }
+        if (params.length >= 3) {
+            tmpFolder = params[2];
+        }
+    }
+    
+}
+
+    /////////////////// Functional classes section /////////////////////////////
+
+class IOFinalizerActivities {
+
+    public void Test(){
+        // scan class files in classFolder if any
+        ArrayList<File> clFiles = new ArrayList<File>();
+        String dir = FinalizeResourceOrientedTest.classFolder;
+        scanClassFiles(dir, clFiles);
+        clFiles = null;
+    }
+    
+    protected void finalize(){
+        FinalizeResourceOrientedTest.reportFinalizeThread(Thread.currentThread());
+        ArrayList<File> classFiles = new ArrayList<File>();
+        String workDir = FinalizeResourceOrientedTest.classFolder;
+        scanClassFiles(workDir, classFiles);
+        if (classFiles.size() == 0){
+            FinalizeResourceOrientedTest.commenterror("failed to scan class files in " + workDir);
+            FinalizeResourceOrientedTest.setFailed();
+            return;
+        }
+        
+        // Create temporary file for processing
+        File tmpFileToProcess = null;;
+        String tmpDir = FinalizeResourceOrientedTest.tmpFolder;
+        if (tmpDir.length() == 0){
+            tmpDir = FinalizeResourceOrientedTest.classFolder;
+        }
+        tmpDir += "goldenfiles";
+        File tmpDirFolder = new File(tmpDir);
+        // create this dir if there is no such
+        tmpDirFolder.mkdir();
+        
+        // generate jar file name
+        String postfix = "" + this.hashCode() + ".jar";
+        ZipOutputStream zo = null;
+        // create jar file
+        try {
+            tmpFileToProcess = File.createTempFile("JAR_", postfix, tmpDirFolder);
+            //tmpFileToProcess.deleteOnExit();
+            zo = new ZipOutputStream(new FileOutputStream(tmpFileToProcess));
+        } catch (IOException e1) {
+            FinalizeResourceOrientedTest.commenterror("failed to create jar file " + tmpFileToProcess.getAbsolutePath());
+            FinalizeResourceOrientedTest.setFailed();
+            return;
+        }
+    
+        // flush class files into one jar file 
+        byte buf[] = new byte[2048];
+        for (int i = 0; i<classFiles.size();i++){
+            FileInputStream fis = null;
+            try {
+                fis = new FileInputStream(classFiles.get(i));
+                try{
+                    zo.putNextEntry(new ZipEntry(classFiles.get(i).getName()));
+                } catch (java.util.zip.ZipException ze){
+                    // Expected, e.g. equal names
+                    fis.close();
+                    continue;
+                }
+                int nBytes = 0;
+                while((nBytes = fis.read(buf)) > 0){
+                    zo.write(buf, 0, nBytes);
+                }
+            } catch (IOException e) {
+                FinalizeResourceOrientedTest.commenterror("Unexpected error during conversion into jar file: " + e.getMessage());
+                FinalizeResourceOrientedTest.setFailed();
+                return;
+            }finally{
+                try {
+                    zo.closeEntry();
+                    fis.close();
+                } catch (Throwable e) {
+                    // Expected in case of unknown exception
+                }
+            }
+        }
+        try {
+            zo.close();
+            tmpFileToProcess.delete();
+        } catch (IOException e) {
+            FinalizeResourceOrientedTest.commenterror("Unexpected error when closing/deleting jar file: " + e.getMessage());
+            FinalizeResourceOrientedTest.setFailed();
+            return;
+        }
+    }
+    
+    // scan class files in test folder
+    void scanClassFiles(String dir, ArrayList<File> classFiles){
+        File root = new File(dir);
+        File[] files = root.listFiles();
+        for (int i=0; i< files.length; i++){
+            if (files[i].getName().endsWith(".class")){
+                synchronized(this){
+                    classFiles.add(files[i]);
+                }
+            }
+            if (files[i].isDirectory()){
+                scanClassFiles(files[i].getAbsolutePath(), classFiles);
+            }
+        }
+        files = null;
+        root = null;
+    }
+
+}
+
+    // Primitives to work with in finalization  
+
+interface Entry{
+    void nullRef();
+    Object getRef();
+    boolean isPhantom();
+    ReferenceQueue getQue();
+}
+
+class WeakRefEntry implements Entry{
+    WeakRefEntry(Entry obj){
+        ref = new WeakReference(obj);
+    }
+    public void nullRef(){
+        ref = null;
+    }
+    public boolean isPhantom(){
+        return false;
+    }
+    public ReferenceQueue getQue(){
+        return null;
+    }
+    public WeakReference ref = null;
+    protected void fialize(){
+        FinalizeResourceOrientedTest.reportFinalizeThread(Thread.currentThread());
+        RefFinalizerActivities.weakCount++;
+    }
+    public Object getRef() {
+        return ref;
+    }
+}
+
+class SoftRefEntry implements Entry{
+    SoftRefEntry(Entry obj){
+        ref = new SoftReference(obj);
+    }
+    public void nullRef(){
+        ref = null;
+    }
+    public Object getRef() {
+        return ref;
+    }
+    public boolean isPhantom(){
+        return false;
+    }
+    public ReferenceQueue getQue(){
+        return null;
+    }
+    public SoftReference ref = null;
+    protected void fialize(){
+        FinalizeResourceOrientedTest.reportFinalizeThread(Thread.currentThread());
+        RefFinalizerActivities.softCount++;
+    }
+}
+
+class PhantomRefEntry implements Entry{
+    PhantomRefEntry(Entry obj){
+        ref = new PhantomReference(obj, refQue);
+    }
+    public void nullRef(){
+        ref = null;
+    }
+    public Object getRef() {
+        return ref;
+    }
+    public boolean isPhantom(){
+        return true;
+    }
+    public ReferenceQueue getQue(){
+        return refQue;
+    }
+    public PhantomReference ref = null;
+    static ReferenceQueue refQue = new ReferenceQueue();
+    protected void fialize(){
+        FinalizeResourceOrientedTest.reportFinalizeThread(Thread.currentThread());
+        RefFinalizerActivities.phantomCount++;
+    }
+
+}
+
+class solidRefEntry implements Entry{
+    solidRefEntry(Entry obj){
+        ref = obj;
+    }
+    public void nullRef(){
+        ref = null;
+    }
+    public Object getRef() {
+        return ref;
+    }
+    public boolean isPhantom(){
+        return false;
+    }
+    public ReferenceQueue getQue(){
+        return null;
+    }
+    public Object ref = null;
+    protected void fialize(){
+        FinalizeResourceOrientedTest.reportFinalizeThread(Thread.currentThread());
+        RefFinalizerActivities.solidCount++;
+    }
+}
+
+    // building ref connections with freeing in finillize
+class RefFinalizerActivities extends Thread{
+
+    public static int weakCount = 0;
+    public static int softCount = 0;
+    public static int phantomCount = 0;
+    public static int solidCount = 0;
+    
+    Random rnd = new Random();
+    Entry first = null;// first element of the list
+    Entry last = null; // last  element of the list    
+    Object refs[] = new Object[FinalizeResourceOrientedTest.numberOfChainObjects];
+
+    void hintnull(){
+        first = null;
+        last=null;
+    }
+    
+    public void run(){
+        // create list of mixed references
+        for (int i = 0; i < FinalizeResourceOrientedTest.numberOfChainObjects; i++){
+            last = newRandomEntry(last);
+            if (i == 0){
+                first = last;
+            }
+        }
+    }
+    
+    protected void finalize(){
+        FinalizeResourceOrientedTest.reportFinalizeThread(Thread.currentThread());
+        // null all created refs
+        Entry ce = first;
+        while (ce != null){
+            Entry e = null;
+            synchronized(this){            
+                e = (Entry)ce.getRef();
+            }
+            // process ReferenceQueue
+            ReferenceQueue rq = null;
+            if ((rq = ce.getQue()) != null){
+                synchronized(ce){                
+                    rq.poll();
+                }
+                try {
+                    while (rq.remove(1) != null);
+                } catch (InterruptedException e1) {
+                }
+            }
+            ce.nullRef();
+            ce = e;
+        }
+        first = last = null;
+
+        int n_fin = weakCount + softCount + phantomCount + solidCount;
+        if (n_fin > FinalizeResourceOrientedTest.numberOfChainObjects){
+            FinalizeResourceOrientedTest.setFailed();
+            FinalizeResourceOrientedTest.commenterror("Conflict: number of finilized objects " + n_fin + 
+                " > than number of created objects " + FinalizeResourceOrientedTest.numberOfChainObjects);
+        }
+        
+        // create new list - no refs nulling - GC is to check reachability for these objects
+        synchronized (this){
+            for (int i = 0; i < FinalizeResourceOrientedTest.numberOfChainObjects; i++){
+                last = newRandomEntry(last);
+                if (i == 0){
+                    first = last;
+                }
+            }
+        }
+        
+    }
+
+    // creates Entry with different type of Ref
+    Entry newRandomEntry(Entry obj){
+        int cs = rnd.nextInt(4);
+        Entry entry = null;
+        switch (cs){
+            case 0:
+                entry = new WeakRefEntry(obj);
+                break;
+            case 1:
+                entry = new SoftRefEntry(obj);
+                break;
+            case 2:
+                entry = new PhantomRefEntry(obj);
+                break;
+            case 3:
+                entry = new solidRefEntry(obj);
+                break;
+            default:
+                entry = new WeakRefEntry(obj);
+                break;
+        }
+        return entry;
+    }
+    
+}
+
+
+    // launching new threads in finalizer
+class SimplethreaddingLaunchActivities {
+    protected void finalize(){
+        // starting threads from existing tests above
+        new RefFinalizerActivities().start();
+    }
+}

Propchange: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeResourceOrientedTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeThrowRestoreTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeThrowRestoreTest.java?view=auto&rev=545554
==============================================================================
--- harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeThrowRestoreTest.java (added)
+++ harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeThrowRestoreTest.java Fri Jun  8 09:34:52 2007
@@ -0,0 +1,353 @@
+/*
+ * Copyright 2007 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+/**
+ * @author Aleksey Ignatenko
+ * @version $Revision: 1.0 $
+ */
+
+package org.apache.harmony.test.reliability.vm.finalization;
+
+import org.apache.harmony.test.reliability.share.Test;
+
+import java.util.ArrayList;
+
+/*
+ * Goal: check VM finalization subsystem 
+ * The test relies that finalization is not garanteed by spec
+ * Accepted parameters:
+ * param[0] - number of threads to run in test
+
+ * The test consists of 3 parts executed consequently
+ * 1 part - ThrowingFinalizerActivities
+ *  - the test starts param[0] threads and checks that throwed exceptions in finilizer interrupt it w/o any 
+ *    additional exceptions throwing, the check is done fo 7 subclasses of Throwable:
+ *    both for j.l.Error and j.l.Exception types
+ * 2 part - RestoreObjectsFinalizerActivities
+ *  - the test starts param[0] threads and checks that single object finalizer is 
+ *    executed only once, check in MT environment.
+ * 3 part - SimplethreaddingActivities
+ *  - the test starts 1,2 threads in finalizer   
+ *  
+ */
+
+public class FinalizeThrowRestoreTest extends Test {
+    volatile static boolean failed = false;
+    public final static int NUMBER_OF_THREADS_OR_INSTANCES = 10;
+    public final static int NUMBER_OF_CHAIN_OBJECTS = 100;
+    public static int numberOfThreads = NUMBER_OF_THREADS_OR_INSTANCES;
+    public static int numberOfObjects = NUMBER_OF_THREADS_OR_INSTANCES;
+    public static int numberOfChainObjects = NUMBER_OF_CHAIN_OBJECTS;
+    public static int numberOfThreadsLaunchedTotal = 0;
+    static int counter = 0;
+    static final int NUMBER_ITERATION_CLEAR_FINALIZERS = 5;
+    
+    public static void setFailed(){
+        failed = true;
+    }
+    
+    public static void commenterror(String msg){
+        log.add(msg);
+    }
+    
+    public static void main(String[] params){
+        System.exit(new FinalizeThrowRestoreTest().test(params));
+    }
+
+    public int test(String []params){
+        parseParams(params);
+        RestoreObjectsFinalizerActivities.finalizeCouner = 0;
+        
+        counter++;
+        if (counter%NUMBER_ITERATION_CLEAR_FINALIZERS == 0){
+            RestoreObjectsFinalizerActivities.saveVals.clear();
+        }
+        
+        // Stage 1: launch ThrowingFinalizerActivities test
+        ThrowingFinalizerActivities[] thrds2 = new ThrowingFinalizerActivities[numberOfThreads];
+        for (int i=0; i<thrds2.length; i++ ){
+            thrds2[i] = new ThrowingFinalizerActivities();
+            thrds2[i].start();
+        }
+
+        System.gc();
+        System.runFinalization();
+        
+        for (int i=0; i<thrds2.length; i++ ){
+            try {
+                thrds2[i].join();
+                thrds2[i] = null;
+            } catch (InterruptedException e) {
+                failed = true;
+                log.add(e);
+            }
+            System.gc();
+            System.runFinalization();
+            
+        }
+        thrds2 = null;
+        
+        if (failed){
+            return fail("rootcause is ThrowingFinalizerActivities");
+        }
+
+        // Stage 2: launch RestoreObjectsFinalizerActivities test
+        Thread[] thrds3 = new Thread[numberOfThreads];
+        for (int i=0; i<thrds3.length; i++ ){
+            thrds3[i] = new RestoreObjectsFinalizerActivities();
+            thrds3[i].start();
+        }
+        numberOfThreadsLaunchedTotal += thrds3.length; 
+
+        System.gc();
+        System.runFinalization();
+        
+        for (int i=0; i<thrds3.length; i++ ){
+            try {
+                thrds3[i].join();
+                thrds3[i] = null;
+            } catch (InterruptedException e) {
+                failed = true;
+                log.add(e);
+            }
+            System.gc();
+            System.runFinalization();
+            
+        }
+        thrds3 = null;
+        
+        // check that number of executed finalizer <= number of threads
+        if (RestoreObjectsFinalizerActivities.getFinalizeCounter() > numberOfThreadsLaunchedTotal*RestoreObjectsFinalizerActivities.NUMBER_OF_OBJECTS_TO_CREATE_PER_THREAD){
+            return fail("rootcause is RestoreObjectsFinalizerActivities: some finalizers were executed more than once!");
+        }
+        
+        // Stage 3: launch SimplethreaddingActivities test
+        for (int i = 0; i < numberOfObjects; i++){
+            SimplethreaddingActivities sta = new SimplethreaddingActivities();
+            sta = null;
+        }
+        System.gc();
+        System.runFinalization();
+
+        // Final stage: results
+        if (failed){
+            return fail("rootcause is RestoreObjectsFinalizerActivities");
+        }
+        
+        
+        return pass("OK");
+    }
+    
+    public void parseParams(String[] params) {
+
+        if (params.length >= 1) {
+            numberOfThreads = Integer.parseInt(params[0]);
+        }
+    }
+    
+}
+
+    /////////////////// Functional classes section /////////////////////////////
+
+
+    // Prinitives for throwing exceptions from finilize method
+
+interface Exc{
+    public int getRes();
+}
+
+class throwArithmeticException extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize(){
+        // devision by zero
+        for (int i = 100; true; i--){
+            int z = 50;
+            float val = z/i;
+            res = val;
+        }
+    }
+}
+
+class throwArrayStoreException extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize(){
+        // ArrayStoreException
+        Object str[] = new String[10];
+        str[0] = new Float(3.53);
+        FinalizeThrowRestoreTest.setFailed();
+    }
+}
+
+class throwRuntimeException extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize(){
+        throw new RuntimeException();
+    }
+}
+
+class throwInternalError extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize(){
+        throw new InternalError();
+    }
+}
+
+class throwIndexOutOfBoundsException extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize(){
+        char [] a = new char[100];
+        for (int i = 0; i< 1000; i++){
+            a[i] = 'a';
+        }
+        FinalizeThrowRestoreTest.setFailed();
+    }
+}
+
+class throwNegativeArraySizeException extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize() {
+        int []a = new int[-100];
+        a[0] = 100;
+        FinalizeThrowRestoreTest.setFailed();
+    }
+}
+
+class throwError extends Exception implements Exc{
+    float res = 0;
+    public int getRes(){
+        return (int)res;
+    }
+    public void finalize() {
+        throw new Error();
+    }
+}
+
+    // Create different  - nothing is to be catched 
+class ThrowingFinalizerActivities extends Thread{
+    Exc xc = null;
+    public void run(){
+        try {
+            throw new throwArithmeticException();
+        }catch (Throwable e){
+        }
+        try {
+            throw new throwArrayStoreException();
+        }catch (Throwable e){
+        } 
+        try {
+            throw new throwRuntimeException();
+        }catch (Throwable e){
+        }
+        try {
+            throw new throwInternalError();
+        }catch (Throwable e){
+        }
+        try {
+            throw new throwIndexOutOfBoundsException();
+        }catch (Throwable e){
+        }
+        try {
+            throw new throwNegativeArraySizeException();
+        }catch (Throwable e){
+        }
+        try{
+            throw new throwError();
+        }catch (Throwable e){
+        }
+        
+        System.gc();
+        System.runFinalization();
+    }
+    protected void finalize(){
+        // emulate NPE
+        xc.getRes();
+    }
+}
+
+    //restoring objects from finilizer and checking the same finalizer is not called twice
+class Finalizator{
+    // restorereference to object in finalizer 
+    protected void finalize(){
+        RestoreObjectsFinalizerActivities.RestoreToSaved(this);
+    }
+}
+
+class RestoreObjectsFinalizerActivities extends Thread{
+    public static final int NUMBER_OF_OBJECTS_TO_CREATE_PER_THREAD = 100;
+    // create objects
+    RestoreObjectsFinalizerActivities(){
+        for (int i=0; i< NUMBER_OF_OBJECTS_TO_CREATE_PER_THREAD; i++){
+            new Finalizator();
+        }
+    }
+    static int finalizeCouner = 0;
+    public static ArrayList saveVals = new ArrayList();
+    public static void RestoreToSaved(Object obj){
+        synchronized(saveVals){
+            finalizeCouner++;
+            saveVals.add(obj);
+        }
+    }
+    public static int getFinalizeCounter(){
+        return finalizeCouner;
+    }
+    public void run(){
+        
+        // delete previously created objects from savevals
+        for (int i=0; i < NUMBER_OF_OBJECTS_TO_CREATE_PER_THREAD; i++){
+            Object obj = null;
+            synchronized(saveVals) {
+                if (saveVals.size() > 0){
+                    obj = saveVals.get(0);
+                    saveVals.remove(0);
+                }else{
+                    break;
+                }
+            }
+            obj = null;
+        }
+        
+    }
+}
+
+
+    // launching new threads in finalizer
+class SimplethreaddingActivities {
+    protected void finalize(){
+        // starting threads from existing tests above
+        new RestoreObjectsFinalizerActivities().start();
+        new ThrowingFinalizerActivities().start();
+    }
+}

Propchange: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/finalization/FinalizeThrowRestoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/gc/GCTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/gc/GCTest.java?view=auto&rev=545554
==============================================================================
--- harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/gc/GCTest.java (added)
+++ harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/gc/GCTest.java Fri Jun  8 09:34:52 2007
@@ -0,0 +1,359 @@
+/*
+ * Copyright 2007 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+/**
+ * @author Aleksey Ignatenko
+ * @version $Revision: 1.0 $
+ */
+
+package org.apache.harmony.test.reliability.vm.gc;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Random;
+import java.util.WeakHashMap;
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.Exchanger;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.lang.ref.PhantomReference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
+import java.lang.ref.WeakReference;
+import java.nio.*;
+
+import org.apache.harmony.test.reliability.share.Test;
+
+/*
+ * Goal: Test GC - create chains of various types of references. Try to fragment java heap.
+ *  passed parameters:
+ *  parameter[0] - number of threads to start
+ *  
+ *  The test Thread does the following:
+ *  * creates numberOfThreads  
+ *  * in every thread 3 consequent steps:
+ *  1. Array of different objects is constructured - objects are chosen by Random
+ *     in emitObject function; after objects were constructed thread starts to exchange 
+ *     them to other objects from other threads 
+ *  2. The same as 1, but array is WeakHashMap, therefore exchanged refs are weak
+ *  3. Every thread constructs 5 kind of cycled object chains - 1 consists of mixed refrences,
+ *     1 - of solid, and 3 - of weak, phantom, sof references correspondently
+ *  
+ *  Additionally test checks synchronization code of bootstrap classloader (the same classes are 
+ *  loaded at the same time by different threads)
+ *    
+ *  */
+
+public class GCTest extends Test{
+    static volatile boolean failed = false;
+    static final int NUMBER_OF_THREADS = 32;
+    static int numberOfThreads = NUMBER_OF_THREADS;
+    static CyclicBarrier cb;
+    
+    public static void main(String[] params){
+        System.exit(new GCTest().test(params));
+    }
+    
+    public int test(String[] params){
+        parseParams(params);
+        cb = new CyclicBarrier(numberOfThreads);
+        
+        Thread[] thrds = new Thread[numberOfThreads];
+        for (int i=0; i< numberOfThreads; i++){
+            thrds[i] = new ObjectEmitterThread();
+            thrds[i].start();
+        }
+        
+        for (int i=0; i< numberOfThreads; i++){
+            try {
+                thrds[i].join();
+            } catch (InterruptedException e) {
+                failed = true;
+            }
+        }
+        
+        // force gc to see results of the test
+        System.gc();
+        
+        if (failed == true){
+            return fail("FAILED");
+        }
+        return pass("OK");
+    }
+    
+    public void parseParams(String[] params) {
+
+        if (params.length >= 1) {
+            numberOfThreads = Integer.parseInt(params[0]);
+        }
+    }
+}
+
+class ObjectEmitterThread extends Thread{
+    static final int ARRAY_SIZE_LIMIT = 777;
+    static final int NUMBER_OF_EMMITED_OBJECTS = 1024;
+    static Random rm = new Random();
+    static Exchanger exc = new Exchanger();
+    
+    public void run (){
+        // 1 step: emit objects of different size and nature
+        try {
+            GCTest.cb.await();            
+        } catch (InterruptedException e) {
+            GCTest.log.add("Failed to wait all starting threads, stage 1!");
+            GCTest.failed = true;
+        } catch (BrokenBarrierException e) {
+            GCTest.log.add("Failed to wait all starting threads, stage 1!");
+            GCTest.failed = true;
+        }
+        
+        CopyOnWriteArrayList<Object> arr = new CopyOnWriteArrayList<Object>();
+            for (int i=0; i<rm.nextInt(NUMBER_OF_EMMITED_OBJECTS)+1; i++){
+                try{
+                    arr.add(emitObject());
+                }catch(Throwable e){
+                    GCTest.log.add("Failed to create object");
+                    GCTest.failed = true;
+                    return;
+                }
+            }
+
+        // try to exchange with refs with other threads
+        for (int i=0; i< arr.size(); i++){
+            try {
+                Object ret = exc.exchange(arr.get(i), rm.nextInt(50)+1, TimeUnit.MILLISECONDS);
+                arr.set(i, ret);
+            } catch (InterruptedException e) {
+                GCTest.log.add("Failed due thread was interrupted, stage 1!");
+                GCTest.failed = true;
+            } catch (TimeoutException e) {
+                // Excpected
+            }
+        }
+        
+        // hint GC to erase every second element from Array
+        for (int i=0; i< arr.size(); i++){
+            if (i%2 == 0){
+                arr.set(i, null);
+            }
+        }
+        
+        try {
+            GCTest.cb.await();            
+        } catch (InterruptedException e) {
+            GCTest.log.add("Failed to wait all starting threads, stage 1!");
+            GCTest.failed = true;
+        } catch (BrokenBarrierException e) {
+            GCTest.log.add("Failed to wait all starting threads, stage 1!");
+            GCTest.failed = true;
+        }
+        
+        // 2 step: emit objects of different size and nature + store weak refs   
+        WeakHashMap<Object, Object> whm = new WeakHashMap<Object, Object>();
+        for (int i=0; i<rm.nextInt(rm.nextInt(1024) + 1); i++){
+            try{
+                whm.put(emitObject(), null);
+            }catch(Throwable e){
+                GCTest.log.add("Failed to create object!");
+                GCTest.failed = true;
+                return;
+            }
+        }
+        // try to exchange with refs with other threads
+        Iterator it = whm.entrySet().iterator();
+        while(it.hasNext()){
+            Map.Entry en = (Map.Entry) it.next();
+            try {
+                Object ret = exc.exchange(en.getKey(), rm.nextInt(50)+1, TimeUnit.MILLISECONDS);
+                whm.put(ret, null);
+            } catch (InterruptedException e) {
+                GCTest.log.add("Failed due thread was interrupted, stage 1!");
+                GCTest.failed = true;
+            } catch (TimeoutException e) {
+                // Excpected
+            }
+        }
+        
+        try {
+            GCTest.cb.await();            
+        } catch (Throwable e) {
+            GCTest.log.add("Failed to wait all starting threads, stage 1!");
+            GCTest.failed = true;
+        } 
+ 
+        // 3 step: construct various chains of objects 
+        Entry first = null;
+        // start 5 cases: 1 mixed refs chain, 4 - set: solid, weak, phantom, 
+        for (int j = -1; j<4; j++){
+            Entry e = first = newEntry(first, j);
+            for (int i=0; i<rm.nextInt(rm.nextInt(1024) + 1); i++){
+                first = newEntry(first, j);
+            }
+            e.ref = first;
+        }
+
+    }
+
+    class Entry{
+        Object ref = null;
+        Object obj = null;
+    }
+    
+    static ReferenceQueue rQue = new ReferenceQueue();
+    Entry newEntry(Object next, int caseForce){
+        Entry ret = new Entry();
+        ret.obj = emitObject();
+        int choice = 0;
+        if (caseForce < 0){
+            choice = rm.nextInt(4);
+        }else{
+            choice = caseForce;
+        }
+        
+        switch(choice){
+            case 0:
+                ret.ref = new WeakReference(next);
+                break;
+            case 1:
+                ret.ref = new SoftReference(next);
+                break;
+            case 2:
+                ret.ref = new PhantomReference(next, rQue);
+                break;
+            case 3:
+                ret.ref = next;
+                break;
+            
+        }
+        return ret;
+    }
+    
+    Object emitObject() throws OutOfMemoryError{
+        Object ret = null;
+        switch(rm.nextInt(32)){
+                // array types
+            case 0:
+                ret = new int[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 1:
+                ret = new char[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 2:
+                ret = new byte[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 3:
+                ret = new long[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 4:
+                ret = new float[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 5:
+                ret = new double[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 6:
+                ret = new boolean[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 7:
+                ret = new String[rm.nextInt(ARRAY_SIZE_LIMIT)];
+                break;
+            case 8:
+                ret = new char[rm.nextInt(ARRAY_SIZE_LIMIT)].toString();
+                break;
+                // simple types
+            case 9: 
+                ret = new Boolean(true);
+                break;
+            case 10:
+                ret = new Byte((byte) rm.nextInt(32));
+                break;
+            case 11:
+                ret = new Character('a');
+                break;
+            case 12:
+                ret = new Double("3.54");
+                break;
+            case 13:
+                ret = new Character('a');
+                break;
+            case 14:
+                ret = new Float("3.54");
+                break;
+            case 15:
+                ret = new Integer(rm.nextInt());
+                break;
+            case 16:
+                ret = new Long(rm.nextLong());
+                break;
+                // big  chunks of memory, upto: 32K, 64K, 128K, 256K, 512K
+            case 17:
+                ret = new byte[rm.nextInt(32*1024)];
+                break;
+            case 18:
+                ret = new byte[rm.nextInt(64*1024)];
+                break;
+            case 19:
+                ret = new byte[rm.nextInt(128*1024)];
+                break;
+            case 20:
+                ret = new byte[rm.nextInt(256*1024)];
+                break;
+            case 21:
+                ret = new byte[rm.nextInt(512*1024)];
+                break;
+                // Objects which could relate to native memory allocations
+                // we do not make too big chunks for direct allocations
+                // to avoid issues with native heap fragmentation
+            case 22:
+                ret = ByteBuffer.allocateDirect(rm.nextInt(64));
+                break;
+            case 23:
+                ret = ByteBuffer.allocateDirect(rm.nextInt(128));
+                break;
+            case 24:
+                ret = ByteBuffer.allocate(rm.nextInt(256));
+                break;
+            case 25:
+                ret = ByteBuffer.allocate(rm.nextInt(512));
+                break;
+            case 26:
+                ret = CharBuffer.allocate(rm.nextInt(64));
+                break;
+            case 27:
+                ret = CharBuffer.allocate(rm.nextInt(128));
+                break;
+            case 28:
+                ret = CharBuffer.allocate(rm.nextInt(256));
+                break;
+            case 29:
+                ret = CharBuffer.allocate(rm.nextInt(512));
+                break;
+            case 30:
+                ret = IntBuffer.allocate(rm.nextInt(64));
+                break;
+            case 31:
+                ret = IntBuffer.allocate(rm.nextInt(128));
+                break;
+            default:
+                ret = new byte[1];
+        }
+        return null;
+    }
+    
+}
+

Propchange: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/gc/GCTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackFramesRestoreTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackFramesRestoreTest.java?view=auto&rev=545554
==============================================================================
--- harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackFramesRestoreTest.java (added)
+++ harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackFramesRestoreTest.java Fri Jun  8 09:34:52 2007
@@ -0,0 +1,230 @@
+/*
+ * Copyright 2007 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+/**
+ * @author Aleksey Ignatenko
+ * @version $Revision: 1.0 $
+ */
+
+package org.apache.harmony.test.reliability.vm.stack;
+
+import org.apache.harmony.test.reliability.share.Test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+
+/*
+ * Goal: check that Throwable object saves, kepps and restores (prints) stack trace in case 
+ * class unloading happened for methods which have frames saved as stack trace in Throwable 
+ * Accepted parameters:
+ * param[0] - number Of threads to launch
+ * param[1] - folder where loaded class is located
+
+ * The test does:
+ * 1. Process Throables (*) every NUMBER_OF_CYCLE_TO_CHECK_THROWABLES Test is called 
+ * by reliability suit
+ * (*) - call getStackTrace function + read some info about StackTraceElements
+ * 2. Starts param[0] number of threads, every thread does:
+ *    a. Loads TestClass class with userdefined classloader CustomLoaderSimple
+ *    b. Creates instance of the loaded class
+ *    c. Calls "testFunction" of TestClass object via Reflection
+ *    d. testFunction in its case calls StackFramesRestoreTest.reportThrowable function;
+ * 
+ * - When GC processed between iterations class unloading is to happen, assuming class 
+ *   unloading released class related data the test tries to read info about unloaded
+ *   method.
+ *   
+ * - The test is not to hang, crash.
+ */
+
+public class StackFramesRestoreTest extends Test {
+    volatile static boolean failed = false;
+    final static int NUMBER_OF_THREADS_OR_INSTANCES = 10;
+    static int numberOfThreads = NUMBER_OF_THREADS_OR_INSTANCES;
+    static String classFolder = System.getProperty("java.class.path");
+    static final int NUMBER_OF_CYCLE_TO_CHECK_THROWABLES = 3;
+    
+    static ArrayList<Throwable> tSaveList = new ArrayList<Throwable>();
+    static int internalCycle = 0;
+    
+    public static void setFailed(){
+        failed = true;
+    }
+    
+    public static void commenterror(String msg){
+        log.add(msg);
+    }
+    
+    public static void main(String[] params){
+        System.exit(new StackFramesRestoreTest().test(params));
+    }
+    
+    public static synchronized void reportThrowable(){
+        Throwable t = new Throwable();
+        t.fillInStackTrace();
+        tSaveList.add(t);
+    }
+    
+    public static void reportThrowableByCatch(){
+        try{
+            int a = 0;
+            int b = 10;
+            a = b;
+            int t = 10/(b-a);// thrown exception
+        }catch(Throwable e){
+            e.fillInStackTrace();
+            tSaveList.add(e);
+        }
+    }
+
+    public int test(String []params){
+        parseParams(params);
+
+        // increment internalCycle counter to control how often we iterate through 
+        // tSaveList collection of saved Throwable
+        internalCycle++;
+        if (internalCycle%NUMBER_OF_CYCLE_TO_CHECK_THROWABLES == 0){
+            for (int i = 0; i < tSaveList.size(); i++){
+                Throwable t = tSaveList.get(i);
+                StackTraceElement[] els = t.getStackTrace();
+                for (int j = 0; j< els.length; j++){
+                    els[j].getFileName();
+                    els[j].getLineNumber();
+                    els[j].getMethodName();
+                    els[j].getClassName();
+                }
+            }
+            tSaveList.clear();            
+        }
+        
+        String packageName = this.getClass().getPackage().getName();
+        String classPath = File.separator + packageName.replace(".", File.separator) + 
+            File.separator + "TestClass.class";
+        String filePath = classFolder + classPath;
+        
+        Thread thrds[] = new Thread[numberOfThreads];
+        for (int i = 0; i < numberOfThreads; i++){
+            thrds[i] = new ThreadLoader(filePath);
+            thrds[i].start();
+        }
+
+        for (int i = 0; i < numberOfThreads; i++){
+            try {
+                thrds[i].join();
+            } catch (InterruptedException e) {
+                log.add("Launching thread was interrupted");
+                failed = true;
+            }
+        }        
+        if (failed){
+            return fail("FAILED");
+        }
+        
+        return pass("OK");
+    }
+    
+    public void parseParams(String[] params) {
+
+        if (params.length >= 1) {
+            numberOfThreads = Integer.parseInt(params[0]);
+        }
+        if (params.length >= 2) {
+            classFolder = params[1];
+        }
+    }
+    
+}
+
+class ThreadLoader extends Thread{
+    String filePath = "";
+    ThreadLoader(String file){
+        filePath = file;
+    }
+    public void run(){
+        CustomLoaderSimple cl = new CustomLoaderSimple(filePath);
+        Class cls = null;
+        try {
+            cls = cl.loadClass("SOME_NAME");
+        } catch (ClassNotFoundException e) {
+            StackFramesRestoreTest.commenterror("Failed to load class " + filePath);
+            StackFramesRestoreTest.setFailed();
+            return;
+        }
+        
+        Object obj = null;
+        try {
+            new TestClass();
+            obj = cls.newInstance();
+        } catch (Throwable e1) {
+            StackFramesRestoreTest.commenterror("Failed to create object of class " + filePath);
+            StackFramesRestoreTest.setFailed();
+            return;
+        } 
+        
+        Method m = null;
+        try {
+            m = cls.getMethod("testFunction", null);
+        } catch (Throwable e) {
+            StackFramesRestoreTest.commenterror("Failed to invoke testFunction in class " + filePath);
+            StackFramesRestoreTest.setFailed();
+            return;            
+        } 
+        
+        try {
+            m.invoke(obj, null);
+        } catch (Throwable e) {
+            StackFramesRestoreTest.commenterror("Failed to invoke testFunction function");
+            StackFramesRestoreTest.setFailed();
+            return;
+        } 
+    }
+}
+
+
+class CustomLoaderSimple extends ClassLoader {
+    String classFilePath = null;
+
+    CustomLoaderSimple(String filePath){
+        classFilePath = filePath;
+    }
+
+    private byte[] loadBinary(String className){
+        byte[] b = null;
+        try {
+            FileInputStream fis = new FileInputStream(classFilePath);
+            b = new byte[fis.available()];
+            fis.read(b);
+            fis.close();
+        } catch (Exception e) {
+            return null;
+        }
+        
+        return b;
+    }
+    
+    protected Class<?> findClass(String name) throws ClassNotFoundException {
+        byte[] b = loadBinary(name);
+        if (b == null){
+            throw new ClassNotFoundException();
+        }
+        return defineClass(b, 0, b.length);
+    }
+}
+

Propchange: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackFramesRestoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackUnwindTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackUnwindTest.java?view=auto&rev=545554
==============================================================================
--- harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackUnwindTest.java (added)
+++ harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackUnwindTest.java Fri Jun  8 09:34:52 2007
@@ -0,0 +1,521 @@
+/*
+ * Copyright 2007 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+/**
+ * @author Aleksey Ignatenko
+ * @version $Revision: 1.0 $
+ */
+
+package org.apache.harmony.test.reliability.vm.stack;
+
+import org.apache.harmony.test.reliability.share.Test;
+import java.util.Random;
+
+/*
+ * Goal: check that stack is unwinded properly when exception is thrown from jitted code. The idea 
+ * is that exception is thrown from differently compiled methods: compiled, optimized, inlined. 
+ * Accepted parameters:
+ * param[0] - number of threads to launch
+ * param[1] - number of methods to call iterativly by every thread
+
+ * The test does:
+ * 1. Creates 3 MethodNClass objects
+ * 2. Launches TestHotInline to hint JIT to recompile TestHotInline function with optimizer
+ *    and inline InlinedMethod in Method3Class 
+ * 3. Starts param[0] number of threads, every thread does:
+ *    a. makes some simple operations
+ *    b. calls StackUnwindTest.intersectMethodCall() function 
+ *    c. intersectMethodCall function calls 1 of 4 functions of 1 of instantiated classes MethodNClass 
+ *       chosen randomly
+ *    d. when number of functions called is equal to numberOfIterations, an exception is thrown,
+ *       it to be catched by Thread.
+ *
+ * No hangs, crashes or fails are expected.
+ * 
+ * Note: TestHotInline supposes that when function is called with a very big internal cycle, then it is to be recompiled 
+ *       with optimizing JIT. Thereto boolean flag hotMode is used. 
+ *
+ */
+
+public class StackUnwindTest extends Test {
+    volatile static boolean failed = false;
+    final static int NUMBER_OF_THREADS = 100;
+    static int numberOfThreads = NUMBER_OF_THREADS;
+    static Random rm = new Random();
+    static volatile boolean hotMode = false; 
+    static int NUMBER_OF_CYCLE_RUNS_TO_MAKE_METHOD_HOT = 151000;
+    static int NUMBER_OF_ITERATIONS = 1000;
+    static int numberOfIterations = NUMBER_OF_ITERATIONS; 
+    static MethodBase[] mBase = new MethodBase[3];
+    static public Thread mainthread = null;
+    
+    public static void setFailed(){
+        failed = true;
+    }
+    
+    public static void commenterror(String msg){
+        log.add(msg);
+    }
+    
+    public static void main(String[] params){
+        System.exit(new StackUnwindTest().test(params));
+    }
+    
+    public int test(String []params){
+        parseParams(params);
+        mainthread = Thread.currentThread();
+
+        // create objects to work on
+        mBase[0] = new Method1Class();
+        mBase[1] = new Method2Class();
+        mBase[2] = new Method3Class();
+        
+        // "burn" methods - make JIT recompile these methods with optimization
+        // hotMode flag is used in methods to run special code to make JIT recompile 
+        // this method with optimizer
+        hotMode = true;
+        mBase[0].TestHotInline();
+        mBase[1].TestHotInline();
+        mBase[2].TestHotInline();
+        hotMode = false;
+
+        Thread thrds[] = new Thread[numberOfThreads];
+        for (int i=0; i< numberOfThreads; i++){
+            thrds[i] = new ThreadExecutor(rm.nextInt(NUMBER_OF_ITERATIONS) + 1);
+            thrds[i].start();
+        }
+
+        for (int i=0; i< numberOfThreads; i++){
+            try {
+                thrds[i].join();
+            } catch (InterruptedException e) {
+                failed = true;
+                log.add("Thread " + thrds[i].getId() + " was interrupted");
+            }
+        }
+
+        if (failed){
+            return fail("FAILED");
+        }
+        
+        return pass("OK");
+    }
+    
+    public void parseParams(String[] params) {
+
+        if (params.length >= 1) {
+            numberOfThreads = Integer.parseInt(params[0]);
+        }
+        if (params.length >= 2) {
+            numberOfIterations = Integer.parseInt(params[1]);
+        }
+        
+    }
+
+    public static void intersectMethodCall(){
+        MethodBase obj = null;
+        switch (rm.nextInt(3)){
+            case 0:
+                obj = mBase[0];
+                break;
+            case 1:
+                obj = mBase[1];
+                break;
+            case 2:
+                obj = mBase[2];
+                break;
+        }
+        
+        switch (rm.nextInt(4)){
+            case 0:
+                obj.Test1();
+                break;
+            case 1:
+                obj.Test2();
+                break;
+            case 2:
+                obj.Test3();
+                break;
+            case 3:
+                obj.TestHotInline();
+                break;
+        }
+        
+    }
+}
+
+
+    // Methods for manipulations on stack
+
+interface MethodBase{
+    public void Test1();
+    public void Test2();
+    public void Test3();
+    public void TestHotInline();
+}
+
+    // extend the class of Exception to avoid inlining 
+class Method1Class extends Exception implements MethodBase{
+    public void Test1(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+    
+    public void Test2(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+
+    public void Test3(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+    
+    public void TestHotInline(){
+        if (StackUnwindTest.hotMode){
+            int i = StackUnwindTest.NUMBER_OF_CYCLE_RUNS_TO_MAKE_METHOD_HOT;
+            while (i > 0){
+                // do something lightweight
+                if (i == StackUnwindTest.NUMBER_OF_CYCLE_RUNS_TO_MAKE_METHOD_HOT/2){
+                    i--;
+                    continue;
+                }
+                i--;
+            }
+            return;
+        }
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+
+}
+
+    //extend the class of Exception to avoid inlining
+class Method2Class extends Exception implements MethodBase{
+    public void Test1(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+    
+    public void Test2(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+
+    public void Test3(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+    public void TestHotInline(){
+        if (StackUnwindTest.hotMode){
+            int i = StackUnwindTest.NUMBER_OF_CYCLE_RUNS_TO_MAKE_METHOD_HOT;
+            while (i > 0){
+                // do something lightweight
+                if (i == StackUnwindTest.NUMBER_OF_CYCLE_RUNS_TO_MAKE_METHOD_HOT/2){
+                    i--;
+                    continue;
+                }
+                i--;
+            }
+            return;
+        }
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+    
+}
+
+class Method3Class implements MethodBase{
+    public void Test1(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        Test2();
+    }
+    
+    public void Test2(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        Test3();
+    }
+
+    public void Test3(){
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        // do something
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+    
+    public void TestHotInline(){
+        if (StackUnwindTest.hotMode){
+            int i = StackUnwindTest.NUMBER_OF_CYCLE_RUNS_TO_MAKE_METHOD_HOT;
+            while (i > 0){
+                // try to hink JIT to compile optimized and inline InlinedMethod 
+                InlinedMethod();
+                i--;
+            }
+            return;
+        }
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;        
+        StackUnwindTest.intersectMethodCall();
+    }
+    
+    void InlinedMethod(){
+        // simple actions
+        int a = 10;
+        int b = 20;
+        int c = 50;
+        
+        c = a+b;
+        c -= a;
+        
+        double h = Math.cos(c);
+
+        c = a;
+        
+        if (Thread.currentThread() == StackUnwindTest.mainthread){
+            return;
+        }
+        ((ThreadExecutor)(Thread.currentThread())).stopIterationNumber--;
+        
+        if (((ThreadExecutor)(Thread.currentThread())).stopIterationNumber < 0){
+            // force exception here
+            c = (a-b)/(c-a);
+        }
+        
+        b *= a*b*c*h; 
+        
+        StackUnwindTest.intersectMethodCall();
+    }
+
+}
+
+class ThreadExecutor extends Thread{
+    int stopIterationNumber = 1;
+    ThreadExecutor(int i){
+        stopIterationNumber = i;
+    }
+    public void run(){
+        try{
+            StackUnwindTest.intersectMethodCall();
+        }catch(Throwable e){
+            // Expected
+            //e.printStackTrace();
+            StackTraceElement[] ste = e.getStackTrace();
+            for (int i = 0; i<ste.length; i++){
+                ste[i].getMethodName();
+                ste[i].getLineNumber();
+                ste[i].getClassName();
+            }
+        }
+    }
+}
+

Propchange: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/StackUnwindTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/TestClass.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/TestClass.java?view=auto&rev=545554
==============================================================================
--- harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/TestClass.java (added)
+++ harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/TestClass.java Fri Jun  8 09:34:52 2007
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2007 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+/*
+ * TestClass is class which is loaded by userdefined classloader in StackFramesRestoreTest test 
+*/
+package org.apache.harmony.test.reliability.vm.stack;
+
+public class TestClass{
+
+    public TestClass(){
+        val = 0;
+    }
+    
+    int val;
+    public static void testFunction(){
+        StackFramesRestoreTest.reportThrowable();
+    }
+}

Propchange: harmony/enhanced/buildtest/branches/2.0/tests/reliability/src/java/org/apache/harmony/test/reliability/vm/stack/TestClass.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message