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
25 $Id: access.c 7441 2007-03-02 23:13:10Z michi $
36 #include "vm/access.h"
37 #include "vm/builtin.h"
38 #include "vm/exceptions.h"
40 #include "vm/jit/stacktrace.h"
42 #include "vmcore/class.h"
45 /****************************************************************************/
47 /****************************************************************************/
49 /* access_is_accessible_class **************************************************
51 Check if a class is accessible from another class
54 referer..........the class containing the reference
55 cls..............the result of resolving the reference
58 true.............access permitted
59 false............access denied
62 This function performs the checks listed in section 5.4.4.
63 "Access Control" of "The Java(TM) Virtual Machine Specification,
66 *******************************************************************************/
68 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
73 /* public classes are always accessible */
75 if (cls->flags & ACC_PUBLIC)
78 /* a class in the same package is always accessible */
80 if (SAME_PACKAGE(referer, cls))
83 /* a non-public class in another package is not accessible */
89 /* access_is_accessible_member *************************************************
91 Check if a field or method is accessible from a given class
94 referer..........the class containing the reference
95 declarer.........the class declaring the member
96 memberflags......the access flags of the member
99 true.............access permitted
100 false............access denied
103 This function only performs the checks listed in section 5.4.4.
104 "Access Control" of "The Java(TM) Virtual Machine Specification,
107 In particular a special condition for protected access with is
108 part of the verification process according to the spec is not
109 checked in this function.
111 *******************************************************************************/
113 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
119 /* public members are accessible */
121 if (memberflags & ACC_PUBLIC)
124 /* {declarer is not an interface} */
126 /* private members are only accessible by the class itself */
128 if (memberflags & ACC_PRIVATE)
129 return (referer == declarer);
131 /* {the member is protected or package private} */
133 /* protected and package private members are accessible in the
136 if (SAME_PACKAGE(referer, declarer))
139 /* package private members are not accessible outside the package */
141 if (!(memberflags & ACC_PROTECTED))
144 /* {the member is protected and declarer is in another package} */
146 /* a necessary condition for access is that referer is a subclass
149 assert((referer->state & CLASS_LINKED) && (declarer->state & CLASS_LINKED));
151 if (builtin_isanysubclass(referer, declarer))
158 /* access_check_member *********************************************************
160 Check if the (indirect) caller has access rights to a member.
163 declarer.........the class declaring the member
164 memberflags......the access flags of the member
165 calldepth........number of callers to ignore
166 For example if the stacktrace looks like this:
168 java.lang.reflect.Method.invokeNative (Native Method)
169 [0] java.lang.reflect.Method.invoke (Method.java:329)
172 you must specify 1 so the access rights of <caller>
176 true.............access permitted
177 false............access denied, an exception has been thrown
179 *******************************************************************************/
181 bool access_check_member(classinfo *declarer, s4 memberflags, s4 calldepth)
183 java_objectarray *oa;
184 classinfo *callerclass;
186 /* if everything is public, there is nothing to check */
188 if ((declarer->flags & ACC_PUBLIC) && (memberflags & ACC_PUBLIC))
191 /* get the caller's class */
193 oa = stacktrace_getClassContext();
198 assert(calldepth >= 0 && calldepth < oa->header.size);
200 callerclass = (classinfo *) oa->data[calldepth];
202 /* check access rights */
204 if (!access_is_accessible_member(callerclass, declarer, memberflags)) {
205 exceptions_throw_illegalaccessexception(callerclass);
216 * These are local overrides for various environment variables in Emacs.
217 * Please do not remove this and leave it at the end of the file, where
218 * Emacs will automagically detect them.
219 * ---------------------------------------------------------------------
222 * indent-tabs-mode: t
226 * vim:noexpandtab:sw=4:ts=4: