* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / vm / access.c
1 /* vm/access.c - checking access rights
2
3    Copyright (C) 1996-2005, 2006 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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Edwin Steiner
28
29    Changes:
30
31    $Id: access.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35 #include <assert.h>
36
37 #include "config.h"
38 #include "vm/types.h"
39
40 #include "vm/access.h"
41 #include "vm/builtin.h"
42 #include "vm/class.h"
43
44
45 /****************************************************************************/
46 /* DEBUG HELPERS                                                            */
47 /****************************************************************************/
48
49 #ifdef NDEBUG
50 #define ACCESS_DEBUG
51 #endif
52
53 #ifdef ACCESS_DEBUG
54 #define ACCESS_ASSERT(cond)  assert(cond)
55 #else
56 #define ACCESS_ASSERT(cond)
57 #endif
58
59 /****************************************************************************/
60 /* ACCESS CHECKS                                                            */
61 /****************************************************************************/
62
63 /* access_is_accessible_class **************************************************
64  
65    Check if a class is accessible from another class
66   
67    IN:
68        referer..........the class containing the reference
69        cls..............the result of resolving the reference
70   
71    RETURN VALUE:
72        true.............access permitted
73        false............access denied
74    
75    NOTE:
76        This function performs the checks listed in section 5.4.4.
77            "Access Control" of "The Java(TM) Virtual Machine Specification,
78            Second Edition".
79
80 *******************************************************************************/
81
82 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
83 {
84         ACCESS_ASSERT(referer);
85         ACCESS_ASSERT(cls);
86
87         /* public classes are always accessible */
88         if (cls->flags & ACC_PUBLIC)
89                 return true;
90
91         /* a class in the same package is always accessible */
92         if (SAME_PACKAGE(referer, cls))
93                 return true;
94
95         /* a non-public class in another package is not accessible */
96         return false;
97 }
98
99
100
101
102 /* access_is_accessible_member *************************************************
103  
104    Check if a field or method is accessible from a given class
105   
106    IN:
107        referer..........the class containing the reference
108        declarer.........the class declaring the member
109        memberflags......the access flags of the member
110   
111    RETURN VALUE:
112        true.............access permitted
113        false............access denied
114
115    NOTE:
116        This function only performs the checks listed in section 5.4.4.
117            "Access Control" of "The Java(TM) Virtual Machine Specification,
118            Second Edition".
119
120            In particular a special condition for protected access with is
121            part of the verification process according to the spec is not
122            checked in this function.
123    
124 *******************************************************************************/
125
126 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
127                                                                  s4 memberflags)
128 {
129         ACCESS_ASSERT(referer);
130         ACCESS_ASSERT(declarer);
131         
132         /* public members are accessible */
133         if (memberflags & ACC_PUBLIC)
134                 return true;
135
136         /* {declarer is not an interface} */
137
138         /* private members are only accessible by the class itself */
139         if (memberflags & ACC_PRIVATE)
140                 return (referer == declarer);
141
142         /* {the member is protected or package private} */
143
144         /* protected and package private members are accessible in the same package */
145         if (SAME_PACKAGE(referer,declarer))
146                 return true;
147
148         /* package private members are not accessible outside the package */
149         if (!(memberflags & ACC_PROTECTED))
150                 return false;
151
152         /* {the member is protected and declarer is in another package} */
153
154         /* a necessary condition for access is that referer is a subclass of declarer */
155         ACCESS_ASSERT(referer->linked && declarer->linked);
156         if (builtin_isanysubclass(referer,declarer))
157                 return true;
158
159         return false;
160 }
161
162
163 /*
164  * These are local overrides for various environment variables in Emacs.
165  * Please do not remove this and leave it at the end of the file, where
166  * Emacs will automagically detect them.
167  * ---------------------------------------------------------------------
168  * Local variables:
169  * mode: c
170  * indent-tabs-mode: t
171  * c-basic-offset: 4
172  * tab-width: 4
173  * End:
174  * vim:noexpandtab:sw=4:ts=4:
175  */
176