xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/include/k5-platform.h (revision 03100a6332bd4edc7a53091fcf7c9a7131bcdaa7)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * k5-platform.h
10  *
11  * Copyright 2003, 2004 Massachusetts Institute of Technology.
12  * All Rights Reserved.
13  *
14  * Export of this software from the United States of America may
15  *   require a specific license from the United States Government.
16  *   It is the responsibility of any person or organization contemplating
17  *   export to obtain such a license before exporting.
18  *
19  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20  * distribute this software and its documentation for any purpose and
21  * without fee is hereby granted, provided that the above copyright
22  * notice appear in all copies and that both that copyright notice and
23  * this permission notice appear in supporting documentation, and that
24  * the name of M.I.T. not be used in advertising or publicity pertaining
25  * to distribution of the software without specific, written prior
26  * permission.	Furthermore if you modify this software you must label
27  * your software as modified software and not distribute it in such a
28  * fashion that it might be confused with the original M.I.T. software.
29  * M.I.T. makes no representations about the suitability of
30  * this software for any purpose.  It is provided "as is" without express
31  * or implied warranty.
32  *
33  *
34  * Some platform-dependent definitions to sync up the C support level.
35  * Some to a C99-ish level, some related utility code.
36  *
37  * Currently:
38  * + make "static inline" work
39  * + 64-bit types and load/store code
40  * + SIZE_MAX
41  * + shared library init/fini hooks
42  */
43 
44 #ifndef K5_PLATFORM_H
45 #define K5_PLATFORM_H
46 
47 #ifndef _KERNEL
48 #include "autoconf.h"
49 
50 /* Initialization and finalization function support for libraries.
51 
52    At top level, before the functions are defined or even declared:
53    MAKE_INIT_FUNCTION(init_fn);
54    MAKE_FINI_FUNCTION(fini_fn);
55    int init_fn(void) { ... }
56    void fini_fn(void) { if (INITIALIZER_RAN(init_fn)) ... }
57 
58    In code, in the same file:
59    err = CALL_INIT_FUNCTION(init_fn);
60 
61    To trigger or verify the initializer invocation from another file,
62    an additional function must be created.
63 
64    The init_fn and fini_fn names should be chosen such that any
65    exported names staring with those names, and optionally followed by
66    additional characters, fits in with any namespace constraints on
67    the library in question.
68 
69 
70    Implementation outline:
71 
72    Windows: MAKE_FINI_FUNCTION creates a symbol with a magic name that
73    is sought at library build time, and code is added to invoke the
74    function when the library is unloaded.  MAKE_INIT_FUNCTION does
75    likewise, but the function is invoked when the library is loaded,
76    and an extra variable is declared to hold an error code and a "yes
77    the initializer ran" flag.  CALL_INIT_FUNCTION blows up if the flag
78    isn't set, otherwise returns the error code.
79 
80    UNIX: MAKE_INIT_FUNCTION creates and initializes a variable with a
81    name derived from the function name, containing a k5_once_t
82    (pthread_once_t or int), an error code, and a pointer to the
83    function.  The function itself is declared static, but the
84    associated variable has external linkage.  CALL_INIT_FUNCTION
85    ensures thath the function is called exactly once (pthread_once or
86    just check the flag) and returns the stored error code (or the
87    pthread_once error).
88 
89    UNIX, with compiler support: MAKE_FINI_FUNCTION declares the
90    function as a destructor, and the run time linker support or
91    whatever will cause it to be invoked when the library is unloaded,
92    the program ends, etc.
93 
94    UNIX, with linker support: MAKE_FINI_FUNCTION creates a symbol with
95    a magic name that is sought at library build time, and linker
96    options are used to mark it as a finalization function for the
97    library.  The symbol must be exported.
98 
99    UNIX, no library finalization support: The finalization function
100    never runs, and we leak memory.  Tough.
101 
102 
103 
104    For maximum flexibility in defining the macros, the function name
105    parameter should be a simple name, not even a macro defined as
106    another name.  The function should have a unique name, and should
107    conform to whatever namespace is used by the library in question.
108 
109    If the macro expansion needs the function to have been declared, it
110    must include a declaration.  If it is not necessary for the symbol
111    name to be exported from the object file, the macro should declare
112    it as "static".  Hence the signature must exactly match "void
113    foo(void)".  (ANSI C allows a static declaration followed by a
114    non-static one; the result is internal linkage.)  The macro
115    expansion has to come before the function, because gcc apparently
116    won't act on "__attribute__((constructor))" if it comes after the
117    function definition.
118 
119    This is going to be compiler- and environment-specific, and may
120    require some support at library build time, and/or "asm"
121    statements.
122 
123    It's okay for this code to require that the library be built
124    with the same compiler and compiler options throughout, but
125    we shouldn't require that the library and application use the
126    same compiler.
127 
128    For static libraries, we don't really care about cleanup too much,
129    since it's all memory handling and mutex allocation which will all
130    be cleaned up when the program exits.  Thus, it's okay if gcc-built
131    static libraries don't play nicely with cc-built executables when
132    it comes to static constructors, just as long as it doesn't cause
133    linking to fail.
134 
135    For dynamic libraries on UNIX, we'll use pthread_once-type support
136    to do delayed initialization, so if finalization can't be made to
137    work, we'll only have memory leaks in a load/use/unload cycle.  If
138    anyone (like, say, the OS vendor) complains about this, they can
139    tell us how to get a shared library finalization function invoked
140    automatically.  */
141 
142 /* Helper macros.  */
143 
144 # define JOIN__2_2(A,B) A ## _ ## _ ## B
145 # define JOIN__2(A,B) JOIN__2_2(A,B)
146 
147 /* XXX Should test USE_LINKER_INIT_OPTION early, and if it's set,
148    always provide a function by the expected name, even if we're
149    delaying initialization.  */
150 
151 #if defined(DELAY_INITIALIZER)
152 
153 /* Run the initialization code during program execution, at the latest
154    possible moment.  This means multiple threads may be active.  */
155 # include "k5-thread.h"
156 typedef struct { k5_once_t once; int error, did_run; void (*fn)(void); } k5_init_t;
157 # ifdef USE_LINKER_INIT_OPTION
158 #  define MAYBE_DUMMY_INIT(NAME)		\
159 	void JOIN__2(NAME, auxinit) () { }
160 # else
161 #  define MAYBE_DUMMY_INIT(NAME)
162 # endif
163 # define MAKE_INIT_FUNCTION(NAME)				\
164 	static int NAME(void);					\
165 	MAYBE_DUMMY_INIT(NAME)					\
166 	/* forward declaration for use in initializer */	\
167 	static void JOIN__2(NAME, aux) (void);			\
168 	static k5_init_t JOIN__2(NAME, once) =			\
169 		{ K5_ONCE_INIT, 0, 0, JOIN__2(NAME, aux) };	\
170 	static void JOIN__2(NAME, aux) (void)			\
171 	{							\
172 	    JOIN__2(NAME, once).did_run = 1;			\
173 	    JOIN__2(NAME, once).error = NAME();			\
174 	}							\
175 	/* so ';' following macro use won't get error */	\
176 	static int NAME(void)
177 # define CALL_INIT_FUNCTION(NAME)	\
178 	k5_call_init_function(& JOIN__2(NAME, once))
179 # ifdef __GNUC__
180 /* Do it in macro form so we get the file/line of the invocation if
181    the assertion fails.  */
182 #  define k5_call_init_function(I)					\
183 	(__extension__ ({						\
184 		k5_init_t *k5int_i = (I);				\
185 		int k5int_err = k5_once(&k5int_i->once, k5int_i->fn);	\
186 		(k5int_err						\
187 		 ? k5int_err						\
188 		 : (assert(k5int_i->did_run != 0), k5int_i->error));	\
189 	    }))
190 # else /* __GNUC__ */
191 static int k5_call_init_function(k5_init_t *i)
192 {
193     int err;
194     err = k5_once(&i->once, i->fn);
195     if (err)
196 	return err;
197     assert (i->did_run != 0);
198     return i->error;
199 }
200 # endif  /* __GNUC__ */
201 /* This should be called in finalization only, so we shouldn't have
202    multiple active threads mucking around in our library at this
203    point.  So ignore the once_t object and just look at the flag.
204 
205    XXX Could we have problems with memory coherence between
206    processors if we don't invoke mutex/once routines?  */
207 # define INITIALIZER_RAN(NAME)	\
208 	(JOIN__2(NAME, once).did_run && JOIN__2(NAME, once).error == 0)
209 
210 # define PROGRAM_EXITING()		(0)
211 
212 #elif defined(__GNUC__) && !defined(_WIN32) && defined(CONSTRUCTOR_ATTR_WORKS)
213 
214 /* Run initializer at load time, via GCC/C++ hook magic.  */
215 
216 # ifdef USE_LINKER_INIT_OPTION
217 #  define MAYBE_DUMMY_INIT(NAME)		\
218 	void JOIN__2(NAME, auxinit) () { }
219 # else
220 #  define MAYBE_DUMMY_INIT(NAME)
221 # endif
222 
223 typedef struct { int error; unsigned char did_run; } k5_init_t;
224 # define MAKE_INIT_FUNCTION(NAME)		\
225 	MAYBE_DUMMY_INIT(NAME)			\
226 	static k5_init_t JOIN__2(NAME, ran)	\
227 		= { 0, 2 };			\
228 	static void JOIN__2(NAME, aux)(void)	\
229 	    __attribute__((constructor));	\
230 	static int NAME(void);			\
231 	static void JOIN__2(NAME, aux)(void)	\
232 	{					\
233 	    JOIN__2(NAME, ran).error = NAME();	\
234 	    JOIN__2(NAME, ran).did_run = 3;	\
235 	}					\
236 	static int NAME(void)
237 # define CALL_INIT_FUNCTION(NAME)		\
238 	(JOIN__2(NAME, ran).did_run == 3	\
239 	 ? JOIN__2(NAME, ran).error		\
240 	 : (abort(),0))
241 # define INITIALIZER_RAN(NAME)	(JOIN__2(NAME,ran).did_run == 3 && JOIN__2(NAME, ran).error == 0)
242 
243 #elif defined(USE_LINKER_INIT_OPTION) || defined(_WIN32)
244 
245 /* Run initializer at load time, via linker magic, or in the
246    case of WIN32, win_glue.c hard-coded knowledge.  */
247 typedef struct { int error; unsigned char did_run; } k5_init_t;
248 # define MAKE_INIT_FUNCTION(NAME)		\
249 	static k5_init_t JOIN__2(NAME, ran)	\
250 		= { 0, 2 };			\
251 	static int NAME(void);			\
252 	void JOIN__2(NAME, auxinit)()		\
253 	{					\
254 	    JOIN__2(NAME, ran).error = NAME();	\
255 	    JOIN__2(NAME, ran).did_run = 3;	\
256 	}					\
257 	static int NAME(void)
258 # define CALL_INIT_FUNCTION(NAME)		\
259 	(JOIN__2(NAME, ran).did_run == 3	\
260 	 ? JOIN__2(NAME, ran).error		\
261 	 : (abort(),0))
262 # define INITIALIZER_RAN(NAME)	\
263 	(JOIN__2(NAME, ran).error == 0)
264 
265 # define PROGRAM_EXITING()		(0)
266 
267 #else
268 
269 # error "Don't know how to do load-time initializers for this configuration."
270 
271 # define PROGRAM_EXITING()		(0)
272 
273 #endif
274 
275 
276 
277 #if defined(USE_LINKER_FINI_OPTION) || defined(_WIN32)
278 /* If we're told the linker option will be used, it doesn't really
279    matter what compiler we're using.  Do it the same way
280    regardless.  */
281 
282 # define MAKE_FINI_FUNCTION(NAME)	\
283 	void NAME(void)
284 
285 #elif defined(__GNUC__) && defined(DESTRUCTOR_ATTR_WORKS)
286 /* If we're using gcc, if the C++ support works, the compiler should
287    build executables and shared libraries that support the use of
288    static constructors and destructors.  The C compiler supports a
289    function attribute that makes use of the same facility as C++.
290 
291    XXX How do we know if the C++ support actually works?  */
292 # define MAKE_FINI_FUNCTION(NAME)	\
293 	static void NAME(void) __attribute__((destructor))
294 
295 #elif !defined(SHARED)
296 
297 /* In this case, we just don't care about finalization.
298 
299    The code will still define the function, but we won't do anything
300    with it.  Annoying: This may generate unused-function warnings.  */
301 
302 # define MAKE_FINI_FUNCTION(NAME)	\
303 	static void NAME(void)
304 
305 #else  /* DELAY_INITIALIZER */
306 
307 # error "Don't know how to do unload-time finalization for this configuration."
308 
309 #endif /* DELAY_INITIALIZER */
310 
311 #endif /* !_KERNEL */
312 
313 
314 /* 64-bit support: krb5_ui_8 and krb5_int64.
315 
316    This should move to krb5.h eventually, but without the namespace
317    pollution from the autoconf macros.  */
318 #if defined(HAVE_STDINT_H) || defined(HAVE_INTTYPES_H)
319 # ifdef HAVE_STDINT_H
320 #  include <stdint.h>
321 # endif
322 # ifdef HAVE_INTTYPES_H
323 #  include <inttypes.h>
324 # endif
325 # define INT64_TYPE int64_t
326 # define UINT64_TYPE uint64_t
327 #elif defined(_WIN32)
328 # define INT64_TYPE signed __int64
329 # define UINT64_TYPE unsigned __int64
330 #else /* not Windows, and neither stdint.h nor inttypes.h */
331 # define INT64_TYPE signed long long
332 # define UINT64_TYPE unsigned long long
333 #endif
334 
335 #ifndef _KERNEL
336 #include <limits.h>
337 #endif /* !_KERNEL */
338 #ifndef SIZE_MAX
339 # define SIZE_MAX ((size_t)((size_t)0 - 1))
340 #endif
341 
342 
343 /* Read and write integer values as (unaligned) octet strings in
344    specific byte orders.
345 
346    Add per-platform optimizations later if needed.  (E.g., maybe x86
347    unaligned word stores and gcc/asm instructions for byte swaps,
348    etc.)  */
349 
350 static  void
351 store_16_be (unsigned int val, unsigned char *p)
352 {
353     p[0] = (val >>  8) & 0xff;
354     p[1] = (val      ) & 0xff;
355 }
356 static  void
357 store_16_le (unsigned int val, unsigned char *p)
358 {
359     p[1] = (val >>  8) & 0xff;
360     p[0] = (val      ) & 0xff;
361 }
362 static  void
363 store_32_be (unsigned int val, unsigned char *p)
364 {
365     p[0] = (val >> 24) & 0xff;
366     p[1] = (val >> 16) & 0xff;
367     p[2] = (val >>  8) & 0xff;
368     p[3] = (val      ) & 0xff;
369 }
370 static  void
371 store_32_le (unsigned int val, unsigned char *p)
372 {
373     p[3] = (val >> 24) & 0xff;
374     p[2] = (val >> 16) & 0xff;
375     p[1] = (val >>  8) & 0xff;
376     p[0] = (val      ) & 0xff;
377 }
378 static  void
379 store_64_be (UINT64_TYPE val, unsigned char *p)
380 {
381     p[0] = (unsigned char)((val >> 56) & 0xff);
382     p[1] = (unsigned char)((val >> 48) & 0xff);
383     p[2] = (unsigned char)((val >> 40) & 0xff);
384     p[3] = (unsigned char)((val >> 32) & 0xff);
385     p[4] = (unsigned char)((val >> 24) & 0xff);
386     p[5] = (unsigned char)((val >> 16) & 0xff);
387     p[6] = (unsigned char)((val >>  8) & 0xff);
388     p[7] = (unsigned char)((val      ) & 0xff);
389 }
390 static  void
391 store_64_le (UINT64_TYPE val, unsigned char *p)
392 {
393     p[7] = (unsigned char)((val >> 56) & 0xff);
394     p[6] = (unsigned char)((val >> 48) & 0xff);
395     p[5] = (unsigned char)((val >> 40) & 0xff);
396     p[4] = (unsigned char)((val >> 32) & 0xff);
397     p[3] = (unsigned char)((val >> 24) & 0xff);
398     p[2] = (unsigned char)((val >> 16) & 0xff);
399     p[1] = (unsigned char)((val >>  8) & 0xff);
400     p[0] = (unsigned char)((val      ) & 0xff);
401 }
402 static  unsigned short
403 load_16_be (unsigned char *p)
404 {
405     return (p[1] | (p[0] << 8));
406 }
407 static  unsigned short
408 load_16_le (unsigned char *p)
409 {
410     return (p[0] | (p[1] << 8));
411 }
412 static  unsigned int
413 load_32_be (unsigned char *p)
414 {
415     return (p[3] | (p[2] << 8) | (p[1] << 16) | (p[0] << 24));
416 }
417 static  unsigned int
418 load_32_le (unsigned char *p)
419 {
420     return (p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
421 }
422 static  UINT64_TYPE
423 load_64_be (unsigned char *p)
424 {
425     return ((UINT64_TYPE)load_32_be(p) << 32) | load_32_be(p+4);
426 }
427 static  UINT64_TYPE
428 load_64_le (unsigned char *p)
429 {
430     return ((UINT64_TYPE)load_32_le(p+4) << 32) | load_32_le(p);
431 }
432 
433 
434 /* Make the interfaces to getpwnam and getpwuid consistent.
435    Model the wrappers on the POSIX thread-safe versions, but
436    use the unsafe system versions if the safe ones don't exist
437    or we can't figure out their interfaces.  */
438 /* SUNW15resync - just have Solaris relevant ones */
439 
440 #define k5_getpwnam_r(NAME, REC, BUF, BUFSIZE, OUT)  \
441          (*(OUT) = getpwnam_r(NAME,REC,BUF,BUFSIZE), *(OUT) == NULL ? -1 : 0)
442 
443 #define k5_getpwuid_r(UID, REC, BUF, BUFSIZE, OUT)  \
444         (*(OUT) = getpwuid_r(UID,REC,BUF,BUFSIZE), *(OUT) == NULL ? -1 : 0)
445 
446 
447 #endif /* K5_PLATFORM_H */
448