1 /* src/vm/access.c - checking access rights
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "mm/memory.h"
37 #include "native/llni.h"
39 #include "vm/access.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
43 #include "vm/jit/stacktrace.h"
45 #include "vmcore/class.h"
46 #include "vmcore/field.h"
47 #include "vmcore/method.h"
50 /* access_is_accessible_class **************************************************
52 Check if a class is accessible from another class
55 referer..........the class containing the reference
56 cls..............the result of resolving the reference
59 true.............access permitted
60 false............access denied
63 This function performs the checks listed in section 5.4.4.
64 "Access Control" of "The Java(TM) Virtual Machine Specification,
67 *******************************************************************************/
69 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
74 /* Public classes are always accessible. */
76 if (cls->flags & ACC_PUBLIC)
79 /* A class in the same package is always accessible. */
81 if (SAME_PACKAGE(referer, cls))
84 #if defined(WITH_CLASSPATH_SUN)
85 /* Code for Sun's OpenJDK (see
86 hotspot/src/share/vm/runtime/reflection.cpp
87 (Reflection::verify_class_access)): Allow all accesses from
88 sun/reflect/MagicAccessorImpl subclasses to succeed
91 /* NOTE: This check must be before checks that could return
94 if (class_issubclass(referer, class_sun_reflect_MagicAccessorImpl))
98 /* A non-public class in another package is not accessible. */
104 /* access_is_accessible_member *************************************************
106 Check if a field or method is accessible from a given class
109 referer..........the class containing the reference
110 declarer.........the class declaring the member
111 memberflags......the access flags of the member
114 true.............access permitted
115 false............access denied
118 This function only performs the checks listed in section 5.4.4.
119 "Access Control" of "The Java(TM) Virtual Machine Specification,
122 In particular a special condition for protected access with is
123 part of the verification process according to the spec is not
124 checked in this function.
126 *******************************************************************************/
128 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
134 /* Public members are accessible. */
136 if (memberflags & ACC_PUBLIC)
139 #if defined(WITH_CLASSPATH_SUN)
140 /* Code for Sun's OpenJDK (see
141 hotspot/src/share/vm/runtime/reflection.cpp
142 (Reflection::verify_class_access)): Allow all accesses from
143 sun/reflect/MagicAccessorImpl subclasses to succeed
146 /* NOTE: This check must be before checks that could return
149 if (class_issubclass(referer, class_sun_reflect_MagicAccessorImpl))
153 /* {declarer is not an interface} */
155 /* private members are only accessible by the class itself */
157 if (memberflags & ACC_PRIVATE)
158 return (referer == declarer);
160 /* {the member is protected or package private} */
162 /* protected and package private members are accessible in the
165 if (SAME_PACKAGE(referer, declarer))
168 /* package private members are not accessible outside the package */
170 if (!(memberflags & ACC_PROTECTED))
173 /* {the member is protected and declarer is in another package} */
175 /* a necessary condition for access is that referer is a subclass
178 assert((referer->state & CLASS_LINKED) && (declarer->state & CLASS_LINKED));
180 if (class_isanysubclass(referer, declarer))
187 /* access_check_field **********************************************************
189 Check if the (indirect) caller has access rights to the specified
193 f................the field to check
194 calldepth........number of callers to ignore
195 For example if the stacktrace looks like this:
197 java.lang.reflect.Method.invokeNative (Native Method)
198 [0] java.lang.reflect.Method.invoke (Method.java:329)
201 you must specify 1 so the access rights of <caller>
205 true.............access permitted
206 false............access denied, an exception has been thrown
208 *******************************************************************************/
210 bool access_check_field(fieldinfo *f, s4 calldepth)
212 java_handle_objectarray_t *oa;
213 classinfo *callerclass;
218 /* if everything is public, there is nothing to check */
220 if ((f->class->flags & ACC_PUBLIC) && (f->flags & ACC_PUBLIC))
223 /* get the caller's class */
225 oa = stacktrace_getClassContext();
230 assert(calldepth >= 0 && calldepth < LLNI_array_size(oa));
232 callerclass = (classinfo *) oa->data[calldepth];
234 /* check access rights */
236 if (!access_is_accessible_member(callerclass, f->class, f->flags)) {
238 utf_bytes(f->class->name) +
241 strlen(" not accessible from ") +
242 utf_bytes(callerclass->name) +
245 msg = MNEW(char, msglen);
247 utf_copy_classname(msg, f->class->name);
249 utf_cat_classname(msg, f->name);
250 strcat(msg, " not accessible from ");
251 utf_cat_classname(msg, callerclass->name);
253 u = utf_new_char(msg);
255 MFREE(msg, char, msglen);
257 exceptions_throw_illegalaccessexception(u);
268 /* access_check_method *********************************************************
270 Check if the (indirect) caller has access rights to the specified
274 m................the method to check
275 calldepth........number of callers to ignore
276 For example if the stacktrace looks like this:
278 java.lang.reflect.Method.invokeNative (Native Method)
279 [0] java.lang.reflect.Method.invoke (Method.java:329)
282 you must specify 1 so the access rights of <caller>
286 true.............access permitted
287 false............access denied, an exception has been thrown
289 *******************************************************************************/
291 bool access_check_method(methodinfo *m, s4 calldepth)
293 java_handle_objectarray_t *oa;
294 classinfo *callerclass;
299 /* if everything is public, there is nothing to check */
301 if ((m->class->flags & ACC_PUBLIC) && (m->flags & ACC_PUBLIC))
304 /* get the caller's class */
306 oa = stacktrace_getClassContext();
311 assert(calldepth >= 0 && calldepth < LLNI_array_size(oa));
313 callerclass = (classinfo *) oa->data[calldepth];
315 /* check access rights */
317 if (!access_is_accessible_member(callerclass, m->class, m->flags)) {
319 utf_bytes(m->class->name) +
322 utf_bytes(m->descriptor) +
323 strlen(" not accessible from ") +
324 utf_bytes(callerclass->name) +
327 msg = MNEW(char, msglen);
329 utf_copy_classname(msg, m->class->name);
331 utf_cat_classname(msg, m->name);
332 utf_cat_classname(msg, m->descriptor);
333 strcat(msg, " not accessible from ");
334 utf_cat_classname(msg, callerclass->name);
336 u = utf_new_char(msg);
338 MFREE(msg, char, msglen);
340 exceptions_throw_illegalaccessexception(u);
352 * These are local overrides for various environment variables in Emacs.
353 * Please do not remove this and leave it at the end of the file, where
354 * Emacs will automagically detect them.
355 * ---------------------------------------------------------------------
358 * indent-tabs-mode: t
362 * vim:noexpandtab:sw=4:ts=4: