1 /*
   2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_GLOBALDEFINITIONS_GCC_HPP
  26 #define SHARE_VM_UTILITIES_GLOBALDEFINITIONS_GCC_HPP
  27 
  28 #include "prims/jni.h"
  29 
  30 // This file holds compiler-dependent includes,
  31 // globally used constants & types, class (forward)
  32 // declarations and a few frequently used utility functions.
  33 
  34 #include <ctype.h>
  35 #include <string.h>
  36 #include <stdarg.h>
  37 #include <stddef.h>
  38 #include <stdio.h>
  39 #include <stdlib.h>
  40 #include <wchar.h>
  41 
  42 #ifdef SOLARIS
  43 #include <ieeefp.h>
  44 #endif // SOLARIS
  45 
  46 #include <math.h>
  47 #include <time.h>
  48 #include <fcntl.h>
  49 #include <dlfcn.h>
  50 #include <pthread.h>
  51 
  52 #ifdef SOLARIS
  53 #include <thread.h>
  54 #endif // SOLARIS
  55 
  56 #include <limits.h>
  57 #include <errno.h>
  58 
  59 #ifdef SOLARIS
  60 #include <sys/trap.h>
  61 #include <sys/regset.h>
  62 #include <sys/procset.h>
  63 #include <ucontext.h>
  64 #include <setjmp.h>
  65 #endif // SOLARIS
  66 
  67 # ifdef SOLARIS_MUTATOR_LIBTHREAD
  68 # include <sys/procfs.h>
  69 # endif
  70 
  71 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
  72 #ifndef __STDC_LIMIT_MACROS
  73 #define __STDC_LIMIT_MACROS
  74 #endif // __STDC_LIMIT_MACROS
  75 #include <inttypes.h>
  76 #include <signal.h>
  77 #ifndef __OpenBSD__
  78 #include <ucontext.h>
  79 #endif
  80 #ifdef __APPLE__
  81   #include <AvailabilityMacros.h>
  82   #include <mach/mach.h>
  83 #endif
  84 #include <sys/time.h>
  85 #endif // LINUX || _ALLBSD_SOURCE
  86 
  87 // 4810578: varargs unsafe on 32-bit integer/64-bit pointer architectures
  88 // When __cplusplus is defined, NULL is defined as 0 (32-bit constant) in
  89 // system header files.  On 32-bit architectures, there is no problem.
  90 // On 64-bit architectures, defining NULL as a 32-bit constant can cause
  91 // problems with varargs functions: C++ integral promotion rules say for
  92 // varargs, we pass the argument 0 as an int.  So, if NULL was passed to a
  93 // varargs function it will remain 32-bits.  Depending on the calling
  94 // convention of the machine, if the argument is passed on the stack then
  95 // only 32-bits of the "NULL" pointer may be initialized to zero.  The
  96 // other 32-bits will be garbage.  If the varargs function is expecting a
  97 // pointer when it extracts the argument, then we have a problem.
  98 //
  99 // Solution: For 64-bit architectures, redefine NULL as 64-bit constant 0.
 100 //
 101 // Note: this fix doesn't work well on Linux because NULL will be overwritten
 102 // whenever a system header file is included. Linux handles NULL correctly
 103 // through a special type '__null'.
 104 #ifdef SOLARIS
 105   #ifdef _LP64
 106     #undef NULL
 107     #define NULL 0L
 108   #else
 109     #ifndef NULL
 110       #define NULL 0
 111     #endif
 112   #endif
 113 #endif
 114 
 115 // NULL vs NULL_WORD:
 116 // On Linux NULL is defined as a special type '__null'. Assigning __null to
 117 // integer variable will cause gcc warning. Use NULL_WORD in places where a
 118 // pointer is stored as integer value.  On some platforms, sizeof(intptr_t) >
 119 // sizeof(void*), so here we want something which is integer type, but has the
 120 // same size as a pointer.
 121 #ifdef __GNUC__
 122   #ifdef _LP64
 123     #define NULL_WORD  0L
 124   #else
 125     // Cast 0 to intptr_t rather than int32_t since they are not the same type
 126     // on platforms such as Mac OS X.
 127     #define NULL_WORD  ((intptr_t)0)
 128   #endif
 129 #else
 130   #define NULL_WORD  NULL
 131 #endif
 132 
 133 #if !defined(LINUX) && !defined(_ALLBSD_SOURCE)
 134 // Compiler-specific primitive types
 135 typedef unsigned short     uint16_t;
 136 #ifndef _UINT32_T
 137 #define _UINT32_T
 138 typedef unsigned int       uint32_t;
 139 #endif // _UINT32_T
 140 
 141 #if !defined(_SYS_INT_TYPES_H)
 142 #ifndef _UINT64_T
 143 #define _UINT64_T
 144 typedef unsigned long long uint64_t;
 145 #endif // _UINT64_T
 146 // %%%% how to access definition of intptr_t portably in 5.5 onward?
 147 typedef int                     intptr_t;
 148 typedef unsigned int            uintptr_t;
 149 // If this gets an error, figure out a symbol XXX that implies the
 150 // prior definition of intptr_t, and add "&& !defined(XXX)" above.
 151 #endif // _SYS_INT_TYPES_H
 152 
 153 #endif // !LINUX && !_ALLBSD_SOURCE
 154 
 155 // Additional Java basic types
 156 
 157 typedef uint8_t  jubyte;
 158 typedef uint16_t jushort;
 159 typedef uint32_t juint;
 160 typedef uint64_t julong;
 161 
 162 
 163 //----------------------------------------------------------------------------------------------------
 164 // Constant for jlong (specifying an long long canstant is C++ compiler specific)
 165 
 166 // Build a 64bit integer constant
 167 #define CONST64(x)  (x ## LL)
 168 #define UCONST64(x) (x ## ULL)
 169 
 170 const jlong min_jlong = CONST64(0x8000000000000000);
 171 const jlong max_jlong = CONST64(0x7fffffffffffffff);
 172 
 173 
 174 #ifdef SOLARIS
 175 //----------------------------------------------------------------------------------------------------
 176 // ANSI C++ fixes
 177 // NOTE:In the ANSI committee's continuing attempt to make each version
 178 // of C++ incompatible with the previous version, you can no longer cast
 179 // pointers to functions without specifying linkage unless you want to get
 180 // warnings.
 181 //
 182 // This also means that pointers to functions can no longer be "hidden"
 183 // in opaque types like void * because at the invokation point warnings
 184 // will be generated. While this makes perfect sense from a type safety
 185 // point of view it causes a lot of warnings on old code using C header
 186 // files. Here are some typedefs to make the job of silencing warnings
 187 // a bit easier.
 188 //
 189 // The final kick in the teeth is that you can only have extern "C" linkage
 190 // specified at file scope. So these typedefs are here rather than in the
 191 // .hpp for the class (os:Solaris usually) that needs them.
 192 
 193 extern "C" {
 194    typedef int (*int_fnP_thread_t_iP_uP_stack_tP_gregset_t)(thread_t, int*, unsigned *, stack_t*, gregset_t);
 195    typedef int (*int_fnP_thread_t_i_gregset_t)(thread_t, int, gregset_t);
 196    typedef int (*int_fnP_thread_t_i)(thread_t, int);
 197    typedef int (*int_fnP_thread_t)(thread_t);
 198 
 199    typedef int (*int_fnP_cond_tP_mutex_tP_timestruc_tP)(cond_t *cv, mutex_t *mx, timestruc_t *abst);
 200    typedef int (*int_fnP_cond_tP_mutex_tP)(cond_t *cv, mutex_t *mx);
 201 
 202    // typedef for missing API in libc
 203    typedef int (*int_fnP_mutex_tP_i_vP)(mutex_t *, int, void *);
 204    typedef int (*int_fnP_mutex_tP)(mutex_t *);
 205    typedef int (*int_fnP_cond_tP_i_vP)(cond_t *cv, int scope, void *arg);
 206    typedef int (*int_fnP_cond_tP)(cond_t *cv);
 207 };
 208 #endif // SOLARIS
 209 
 210 //----------------------------------------------------------------------------------------------------
 211 // Debugging
 212 
 213 #define DEBUG_EXCEPTION ::abort();
 214 
 215 #ifdef ARM32
 216 #ifdef SOLARIS
 217 #define BREAKPOINT __asm__ volatile (".long 0xe1200070")
 218 #else
 219 #define BREAKPOINT __asm__ volatile (".long 0xe7f001f0")
 220 #endif
 221 #else
 222 extern "C" void breakpoint();
 223 #define BREAKPOINT ::breakpoint()
 224 #endif
 225 
 226 // checking for nanness
 227 #ifdef SOLARIS
 228 #ifdef SPARC
 229 inline int g_isnan(float  f) { return isnanf(f); }
 230 #else
 231 // isnanf() broken on Intel Solaris use isnand()
 232 inline int g_isnan(float  f) { return isnand(f); }
 233 #endif
 234 inline int g_isnan(double f) { return isnand(f); }
 235 #elif defined(__APPLE__)
 236 inline int g_isnan(double f) { return isnan(f); }
 237 #elif defined(LINUX)
 238 // Linux libc without BSD extensions not required
 239 // to have isnanf, but should have isnan macro
 240 inline int g_isnan(float  f) { return isnan(f); }
 241 inline int g_isnan(double f) { return isnan(f); }
 242 #elif defined(_ALLBSD_SOURCE)
 243 inline int g_isnan(float  f) { return isnanf(f); }
 244 inline int g_isnan(double f) { return isnan(f); }
 245 #else
 246 #error "missing platform-specific definition here"
 247 #endif
 248 
 249 // GCC 4.3 does not allow 0.0/0.0 to produce a NAN value
 250 #if (__GNUC__ == 4) && (__GNUC_MINOR__ > 2)
 251 #define CAN_USE_NAN_DEFINE 1
 252 #endif
 253 
 254 
 255 // Checking for finiteness
 256 
 257 #if defined(LINUX)
 258 // Linux libc without BSD extensions have no finite, but has isfinite
 259 inline int g_isfinite(jfloat  f)                 { return isfinite(f); }
 260 inline int g_isfinite(jdouble f)                 { return isfinite(f); }
 261 #else
 262 inline int g_isfinite(jfloat  f)                 { return finite(f); }
 263 inline int g_isfinite(jdouble f)                 { return finite(f); }
 264 #endif
 265 
 266 
 267 // Wide characters
 268 
 269 inline int wcslen(const jchar* x) { return wcslen((const wchar_t*)x); }
 270 
 271 
 272 // Portability macros
 273 #define PRAGMA_INTERFACE             #pragma interface
 274 #define PRAGMA_IMPLEMENTATION        #pragma implementation
 275 #define VALUE_OBJ_CLASS_SPEC
 276 
 277 #ifndef ATTRIBUTE_PRINTF
 278 // Diagnostic pragmas like the ones defined below in PRAGMA_FORMAT_NONLITERAL_IGNORED
 279 // were only introduced in GCC 4.2. Because we have no other possibility to ignore
 280 // these warnings for older versions of GCC, we simply don't decorate our printf-style
 281 // functions with __attribute__(format) in that case.
 282 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) || (__GNUC__ > 4)
 283 #define ATTRIBUTE_PRINTF(fmt,vargs)  __attribute__((format(printf, fmt, vargs)))
 284 #else
 285 #define ATTRIBUTE_PRINTF(fmt,vargs)
 286 #endif
 287 #endif
 288 
 289 #define PRAGMA_FORMAT_NONLITERAL_IGNORED _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \
 290                                          _Pragma("GCC diagnostic ignored \"-Wformat-security\"")
 291 #define PRAGMA_FORMAT_IGNORED _Pragma("GCC diagnostic ignored \"-Wformat\"")
 292 
 293 #if defined(__clang_major__) && \
 294       (__clang_major__ >= 4 || \
 295       (__clang_major__ >= 3 && __clang_minor__ >= 1)) || \
 296     ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4)
 297 // Tested to work with clang version 3.1 and better.
 298 #define PRAGMA_DIAG_PUSH             _Pragma("GCC diagnostic push")
 299 #define PRAGMA_DIAG_POP              _Pragma("GCC diagnostic pop")
 300 #define PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL
 301 #define PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL PRAGMA_FORMAT_NONLITERAL_IGNORED
 302 
 303 // Hack to deal with gcc yammering about non-security format stuff
 304 #else
 305 // Old versions of gcc don't do push/pop, also do not cope with this pragma within a function
 306 // One method does so much varied printing that it is decorated with both internal and external
 307 // versions of the macro-pragma to obtain better checking with newer compilers.
 308 #define PRAGMA_DIAG_PUSH
 309 #define PRAGMA_DIAG_POP
 310 #define PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL PRAGMA_FORMAT_NONLITERAL_IGNORED
 311 #define PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
 312 #endif
 313 
 314 #ifndef __clang_major__
 315 #define PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC _Pragma("GCC diagnostic ignored \"-Wformat\"") _Pragma("GCC diagnostic error \"-Wformat-nonliteral\"") _Pragma("GCC diagnostic error \"-Wformat-security\"")
 316 #endif
 317 
 318 #if (__GNUC__ == 2) && (__GNUC_MINOR__ < 95)
 319 #define TEMPLATE_TABLE_BUG
 320 #endif
 321 #if (__GNUC__ == 2) && (__GNUC_MINOR__ >= 96)
 322 #define CONST_SDM_BUG
 323 #endif
 324 
 325 // Formatting.
 326 #ifdef _LP64
 327 #define FORMAT64_MODIFIER "l"
 328 #else // !_LP64
 329 #define FORMAT64_MODIFIER "ll"
 330 #endif // _LP64
 331 
 332 // HACK: gcc warns about applying offsetof() to non-POD object or calculating
 333 //       offset directly when base address is NULL. Use 16 to get around the
 334 //       warning. gcc-3.4 has an option -Wno-invalid-offsetof to suppress
 335 //       this warning.
 336 #define offset_of(klass,field) (size_t)((intx)&(((klass*)16)->field) - 16)
 337 
 338 #ifdef offsetof
 339 # undef offsetof
 340 #endif
 341 #define offsetof(klass,field) offset_of(klass,field)
 342 
 343 #if defined(_LP64) && defined(__APPLE__)
 344 #define JLONG_FORMAT           "%ld"
 345 #endif // _LP64 && __APPLE__
 346 
 347 #endif // SHARE_VM_UTILITIES_GLOBALDEFINITIONS_GCC_HPP