1 /*
2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Copyright 2003 Wily Technology, Inc.
28  */
29 
30 #include    <stdlib.h>
31 #include    <stdio.h>
32 
33 #include    "JPLISAssert.h"
34 #include    "Utilities.h"
35 #include    "JavaExceptions.h"
36 
37 /*
38  *  This module provides various simple JNI and JVMTI utility functionality.
39  */
40 
41 void *
allocate(jvmtiEnv * jvmtienv,size_t bytecount)42 allocate(jvmtiEnv * jvmtienv, size_t bytecount) {
43     void *          resultBuffer    = NULL;
44     jvmtiError      error           = JVMTI_ERROR_NONE;
45 
46     error = (*jvmtienv)->Allocate(jvmtienv,
47                                   bytecount,
48                                   (unsigned char**) &resultBuffer);
49     /* may be called from any phase */
50     jplis_assert(error == JVMTI_ERROR_NONE);
51     if ( error != JVMTI_ERROR_NONE ) {
52         resultBuffer = NULL;
53     }
54     return resultBuffer;
55 }
56 
57 /**
58  * Convenience method that deallocates memory.
59  * Throws assert on error.
60  * JVMTI Deallocate can only fail due to internal error, that is, this
61  * agent has done something wrong or JVMTI has done something wrong.  These
62  * errors aren't interesting to a JPLIS agent and so are not returned.
63  */
64 void
deallocate(jvmtiEnv * jvmtienv,void * buffer)65 deallocate(jvmtiEnv * jvmtienv, void * buffer) {
66     jvmtiError  error = JVMTI_ERROR_NONE;
67 
68     error = (*jvmtienv)->Deallocate(jvmtienv,
69                                     (unsigned char*)buffer);
70     /* may be called from any phase */
71     jplis_assert_msg(error == JVMTI_ERROR_NONE, "Can't deallocate memory");
72     return;
73 }
74 
75 /**
76  *  Returns whether the passed exception is an instance of the given classname
77  *  Clears any JNI exceptions before returning
78  */
79 jboolean
isInstanceofClassName(JNIEnv * jnienv,jobject instance,const char * className)80 isInstanceofClassName(  JNIEnv *        jnienv,
81                         jobject         instance,
82                         const char *    className) {
83     jboolean    isInstanceof        = JNI_FALSE;
84     jboolean    errorOutstanding    = JNI_FALSE;
85     jclass      classHandle         = NULL;
86 
87     jplis_assert(isSafeForJNICalls(jnienv));
88 
89     /* get an instance of unchecked exception for instanceof comparison */
90     classHandle = (*jnienv)->FindClass(jnienv, className);
91     errorOutstanding = checkForAndClearThrowable(jnienv);
92     jplis_assert(!errorOutstanding);
93 
94     if (!errorOutstanding) {
95         isInstanceof = (*jnienv)->IsInstanceOf(jnienv, instance, classHandle);
96         errorOutstanding = checkForAndClearThrowable(jnienv);
97         jplis_assert(!errorOutstanding);
98     }
99 
100     jplis_assert(isSafeForJNICalls(jnienv));
101     return isInstanceof;
102 }
103 
104 /* We don't come back from this
105 */
106 void
abortJVM(JNIEnv * jnienv,const char * message)107 abortJVM(   JNIEnv *        jnienv,
108             const char *    message) {
109     (*jnienv)->FatalError(jnienv, message);
110 }
111