1 /*
2 * Copyright (c) 2003, 2018, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include "jvmti.h"
28 #include "agent_common.h"
29 #include "JVMTITools.h"
30
31 extern "C" {
32
33
34 #define PASSED 0
35 #define STATUS_FAILED 2
36
37 typedef struct {
38 const char *name;
39 const char *sig;
40 jboolean is_synthetic;
41 } field_info;
42
43 static jvmtiEnv *jvmti = NULL;
44 static jvmtiCapabilities caps;
45 static jint result = PASSED;
46 static jboolean printdump = JNI_FALSE;
47 static field_info fields[] = {
48 { "fld", "I", JNI_FALSE },
49 { "this$0", "Lnsk/jvmti/IsFieldSynthetic/isfldsin003a;", JNI_TRUE }
50 };
51
52 #ifdef STATIC_BUILD
Agent_OnLoad_isfldsin003(JavaVM * jvm,char * options,void * reserved)53 JNIEXPORT jint JNICALL Agent_OnLoad_isfldsin003(JavaVM *jvm, char *options, void *reserved) {
54 return Agent_Initialize(jvm, options, reserved);
55 }
Agent_OnAttach_isfldsin003(JavaVM * jvm,char * options,void * reserved)56 JNIEXPORT jint JNICALL Agent_OnAttach_isfldsin003(JavaVM *jvm, char *options, void *reserved) {
57 return Agent_Initialize(jvm, options, reserved);
58 }
JNI_OnLoad_isfldsin003(JavaVM * jvm,char * options,void * reserved)59 JNIEXPORT jint JNI_OnLoad_isfldsin003(JavaVM *jvm, char *options, void *reserved) {
60 return JNI_VERSION_1_8;
61 }
62 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)63 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
64 jint res;
65 jvmtiError err;
66
67 if (options != NULL && strcmp(options, "printdump") == 0) {
68 printdump = JNI_TRUE;
69 }
70
71 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
72 if (res != JNI_OK || jvmti == NULL) {
73 printf("Wrong result of a valid call to GetEnv!\n");
74 return JNI_ERR;
75 }
76
77 err = jvmti->GetPotentialCapabilities(&caps);
78 if (err != JVMTI_ERROR_NONE) {
79 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
80 TranslateError(err), err);
81 return JNI_ERR;
82 }
83
84 err = jvmti->AddCapabilities(&caps);
85 if (err != JVMTI_ERROR_NONE) {
86 printf("(AddCapabilities) unexpected error: %s (%d)\n",
87 TranslateError(err), err);
88 return JNI_ERR;
89 }
90
91 err = jvmti->GetCapabilities(&caps);
92 if (err != JVMTI_ERROR_NONE) {
93 printf("(GetCapabilities) unexpected error: %s (%d)\n",
94 TranslateError(err), err);
95 return JNI_ERR;
96 }
97
98 if (!caps.can_get_synthetic_attribute) {
99 printf("Warning: IsFieldSynthetic is not implemented\n");
100 }
101
102 return JNI_OK;
103 }
104
jbooleanToString(jboolean flag)105 char const *jbooleanToString(jboolean flag) {
106 return ((flag == JNI_TRUE) ? "true" : "false");
107 }
108
109 JNIEXPORT jint JNICALL
Java_nsk_jvmti_IsFieldSynthetic_isfldsin003a_check(JNIEnv * env,jclass cls,jclass clazz)110 Java_nsk_jvmti_IsFieldSynthetic_isfldsin003a_check(JNIEnv *env,
111 jclass cls, jclass clazz) {
112 jvmtiError err;
113 jfieldID fid;
114 jboolean isSynthetic;
115 size_t i;
116
117 if (jvmti == NULL) {
118 printf("JVMTI client was not properly loaded!\n");
119 return STATUS_FAILED;
120 }
121
122 for (i = 0; i < sizeof(fields) / sizeof(field_info); i++) {
123 fid = env->GetFieldID(clazz, fields[i].name, fields[i].sig);
124 if (fid == NULL) {
125 printf("(%" PRIuPTR ") cannot get field ID for %s:\"%s\"\n",
126 i, fields[i].name, fields[i].sig);
127 result = STATUS_FAILED;
128 continue;
129 }
130
131 err = jvmti->IsFieldSynthetic(clazz, fid, &isSynthetic);
132 if (err != JVMTI_ERROR_NONE) {
133 printf("(IsFieldSynthetic#%" PRIuPTR ") unexpected error: %s (%d)\n",
134 i, TranslateError(err), err);
135 result = STATUS_FAILED;
136 }
137
138 if (printdump == JNI_TRUE) {
139 printf(">>> %s:\"%s\", isSynthetic: %s\n",
140 fields[i].name, fields[i].sig,
141 jbooleanToString(isSynthetic));
142 }
143
144 if (isSynthetic != fields[i].is_synthetic) {
145 printf("%s:\"%s\"\n\t - isSynthetic expected: %s, actual: %s\n",
146 fields[i].name, fields[i].sig,
147 jbooleanToString(fields[i].is_synthetic),
148 jbooleanToString(isSynthetic));
149 result = STATUS_FAILED;
150 }
151 }
152
153 return result;
154 }
155
156 }
157