1 /* src/vmcore/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 7246 2007-01-29 18:49:05Z twisti $
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 */
74 if (cls->flags & ACC_PUBLIC)
77 /* a class in the same package is always accessible */
78 if (SAME_PACKAGE(referer, cls))
81 /* a non-public class in another package is not accessible */
88 /* access_is_accessible_member *************************************************
90 Check if a field or method is accessible from a given class
93 referer..........the class containing the reference
94 declarer.........the class declaring the member
95 memberflags......the access flags of the member
98 true.............access permitted
99 false............access denied
102 This function only performs the checks listed in section 5.4.4.
103 "Access Control" of "The Java(TM) Virtual Machine Specification,
106 In particular a special condition for protected access with is
107 part of the verification process according to the spec is not
108 checked in this function.
110 *******************************************************************************/
112 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
118 /* public members are accessible */
119 if (memberflags & ACC_PUBLIC)
122 /* {declarer is not an interface} */
124 /* private members are only accessible by the class itself */
125 if (memberflags & ACC_PRIVATE)
126 return (referer == declarer);
128 /* {the member is protected or package private} */
130 /* protected and package private members are accessible in the same package */
131 if (SAME_PACKAGE(referer,declarer))
134 /* package private members are not accessible outside the package */
135 if (!(memberflags & ACC_PROTECTED))
138 /* {the member is protected and declarer is in another package} */
140 /* a necessary condition for access is that referer is a subclass of declarer */
141 assert((referer->state & CLASS_LINKED) && (declarer->state & CLASS_LINKED));
142 if (builtin_isanysubclass(referer,declarer))
149 /* access_check_caller *********************************************************
151 Check if the (indirect) caller has access rights to a member.
154 declarer.........the class declaring the member
155 memberflags......the access flags of the member
156 calldepth........number of callers to ignore
157 For example if the stacktrace looks like this:
159 java.lang.reflect.Method.invokeNative (Native Method)
160 [0] java.lang.reflect.Method.invoke (Method.java:329)
163 you must specify 1 so the access rights of <caller>
167 true.............access permitted
168 false............access denied, an exception has been thrown
170 *******************************************************************************/
172 bool access_check_caller(classinfo *declarer, s4 memberflags, s4 calldepth)
174 java_objectarray *oa;
175 classinfo *callerclass;
177 /* if everything is public, there is nothing to check */
179 if ((declarer->flags & ACC_PUBLIC) && (memberflags & ACC_PUBLIC))
182 /* get the caller's class */
184 oa = stacktrace_getClassContext();
189 assert(calldepth >= 0 && calldepth < oa->header.size);
191 callerclass = (classinfo *) oa->data[calldepth];
193 /* check access rights */
195 if (!access_is_accessible_class(callerclass, declarer)
196 || !access_is_accessible_member(callerclass, declarer, memberflags))
198 exceptions_throw_illegalaccessexception(callerclass);
209 * These are local overrides for various environment variables in Emacs.
210 * Please do not remove this and leave it at the end of the file, where
211 * Emacs will automagically detect them.
212 * ---------------------------------------------------------------------
215 * indent-tabs-mode: t
219 * vim:noexpandtab:sw=4:ts=4: