001/* A parser for MoML (modeling markup language) 002 003 Copyright (c) 2006 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.moml.test; 029 030import ptolemy.kernel.CompositeEntity; 031import ptolemy.moml.MoMLParser; 032 033////////////////////////////////////////////////////////////////////////// 034//// MoMLParserLeak 035 036/** 037 Leak memory in MoMLParser by throwing an Exception. 038 <p> Under Java 1.4, run this with: 039 <pre> 040 java -Xrunhprof:depth=15 -classpath "$PTII;." ptolemy.moml.test.MoMLParserLeak 041 </pre> 042 and then look in java.hprof.txt. 043 044 @author Christopher Brooks 045 @version $Id$ 046 @since Ptolemy II 5.2 047 @Pt.ProposedRating Red (cxh) 048 @Pt.AcceptedRating Red (cxh) 049 */ 050public class MoMLParserLeak { 051 052 /** Attempt to leak code by parsing MoML that looks for a class 053 * that does not exist. 054 * @return the top level that would be found if the class that 055 * does not exist was present 056 * @exception Exception If the class does not exist. 057 */ 058 public static CompositeEntity leak() throws Exception { 059 //MoMLParser parser = new MoMLParser(); 060 CompositeEntity toplevel = (CompositeEntity) parser 061 .parse("<?xml version=\"1.0\" standalone=\"no\"?>\n" 062 + "<!DOCTYPE entity PUBLIC \"-//UC Berkeley//DTD MoML 1//EN\"\n" 063 + "\"http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd\">\n" 064 + "<entity name=\"top\" class=\"ptolemy.kernel.CompositeEntity\">\n" 065 + "<entity name=\"myRamp\" class=\"ptolemy.actor.lib.Ramp\"/>\n" 066 + "<entity name=\"notaclass\" class=\"Not.A.Class\"/>\n" 067 + "</entity>\n"); 068 return toplevel; 069 } 070 071 /** Attempt to leak code by parsing MoML that looks for a class 072 * that does not exist. 073 * @param args Ignored. 074 * @exception Exception if there is a problem parsing 075 */ 076 public static void main(String[] args) throws Exception { 077 parser = new MoMLParser(); 078 //CompositeEntity toptop = new CompositeEntity(); 079 //parser.setContext(toptop); 080 try { 081 CompositeEntity toplevel = leak(); 082 toplevel.setContainer(null); 083 toplevel = null; 084 // If we don't set parser to null or otherwise force it to 085 // go out of scope, then we leak memory. 086 System.out.println("Setting parser to null"); 087 parser = null; 088 } catch (Exception ex) { 089 // If we don't gc here, then references to Ramp might exist 090 // if if we _don't_ throw an exception. 091 System.gc(); 092 System.out.println("Sleeping for 2 seconds for any possible gc."); 093 try { 094 Thread.sleep(2000); 095 } catch (InterruptedException e) { 096 } 097 ex.printStackTrace(); 098 throw ex; 099 } 100 } 101 102 /** Note that the parser is what actually leaks here. */ 103 public static MoMLParser parser; 104}