src/os/aix/vm/porting_aix.cpp

Wed, 31 Jan 2018 19:24:57 -0500

author
dbuck
date
Wed, 31 Jan 2018 19:24:57 -0500
changeset 9289
427b2fb1944f
parent 0
f90c822e73f8
permissions
-rw-r--r--

8189170: Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM
Reviewed-by: dcubed

     1 /*
     2  * Copyright 2012, 2013 SAP AG. 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  */
    25 #include "asm/assembler.hpp"
    26 #include "loadlib_aix.hpp"
    27 #include "porting_aix.hpp"
    28 #include "utilities/debug.hpp"
    30 #include <demangle.h>
    31 #include <sys/debug.h>
    33 //////////////////////////////////
    34 // Provide implementation for dladdr based on LoadedLibraries pool and
    35 // traceback table scan (see getFuncName).
    37 // Search traceback table in stack,
    38 // return procedure name from trace back table.
    39 #define MAX_FUNC_SEARCH_LEN 0x10000
    40 // Any PC below this value is considered toast.
    41 #define MINIMUM_VALUE_FOR_PC ((unsigned int*)0x1024)
    43 #define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
    45 // Align a pointer without having to cast.
    46 inline char* align_ptr_up(char* ptr, intptr_t alignment) {
    47   return (char*) align_size_up((intptr_t)ptr, alignment);
    48 }
    50 // Trace if verbose to tty.
    51 // I use these now instead of the Xtrace system because the latter is
    52 // not available at init time, hence worthless. Until we fix this, all
    53 // tracing here is done with -XX:+Verbose.
    54 #define trcVerbose(fmt, ...) { \
    55   if (Verbose) { \
    56     fprintf(stderr, fmt, ##__VA_ARGS__); \
    57     fputc('\n', stderr); fflush(stderr); \
    58   } \
    59 }
    60 #define ERRBYE(s) { trcVerbose(s); return -1; }
    62 // Unfortunately, the interface of dladdr makes the implementator
    63 // responsible for maintaining memory for function name/library
    64 // name. I guess this is because most OS's keep those values as part
    65 // of the mapped executable image ready to use. On AIX, this doesn't
    66 // work, so I have to keep the returned strings. For now, I do this in
    67 // a primitive string map. Should this turn out to be a performance
    68 // problem, a better hashmap has to be used.
    69 class fixed_strings {
    70   struct node {
    71     char* v;
    72     node* next;
    73   };
    75   node* first;
    77   public:
    79   fixed_strings() : first(0) {}
    80   ~fixed_strings() {
    81     node* n = first;
    82     while (n) {
    83       node* p = n;
    84       n = n->next;
    85       free(p->v);
    86       delete p;
    87     }
    88   }
    90   char* intern(const char* s) {
    91     for (node* n = first; n; n = n->next) {
    92       if (strcmp(n->v, s) == 0) {
    93         return n->v;
    94       }
    95     }
    96     node* p = new node;
    97     p->v = strdup(s);
    98     p->next = first;
    99     first = p;
   100     return p->v;
   101   }
   102 };
   104 static fixed_strings dladdr_fixed_strings;
   106 // Given a code pointer, returns the function name and the displacement.
   107 // Function looks for the traceback table at the end of the function.
   108 extern "C" int getFuncName(
   109     codeptr_t pc,                    // [in] program counter
   110     char* p_name, size_t namelen,    // [out] optional: function name ("" if not available)
   111     int* p_displacement,             // [out] optional: displacement (-1 if not available)
   112     const struct tbtable** p_tb,     // [out] optional: ptr to traceback table to get further
   113                                      //                 information (NULL if not available)
   114     char* p_errmsg, size_t errmsglen // [out] optional: user provided buffer for error messages
   115   ) {
   116   struct tbtable* tb = 0;
   117   unsigned int searchcount = 0;
   119   // initialize output parameters
   120   if (p_name && namelen > 0) {
   121     *p_name = '\0';
   122   }
   123   if (p_errmsg && errmsglen > 0) {
   124     *p_errmsg = '\0';
   125   }
   126   if (p_displacement) {
   127     *p_displacement = -1;
   128   }
   129   if (p_tb) {
   130     *p_tb = NULL;
   131   }
   133   // weed out obvious bogus states
   134   if (pc < MINIMUM_VALUE_FOR_PC) {
   135     ERRBYE("invalid program counter");
   136   }
   138   codeptr_t pc2 = pc;
   140   // make sure the pointer is word aligned.
   141   pc2 = (codeptr_t) align_ptr_up((char*)pc2, 4);
   143   // Find start of traceback table.
   144   // (starts after code, is marked by word-aligned (32bit) zeros)
   145   while ((*pc2 != NULL) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {
   146     pc2++;
   147   }
   148   if (*pc2 != 0) {
   149     ERRBYE("could not find traceback table within 5000 bytes of program counter");
   150   }
   151   //
   152   // Set up addressability to the traceback table
   153   //
   154   tb = (struct tbtable*) (pc2 + 1);
   156   // Is this really a traceback table? No way to be sure but
   157   // some indicators we can check.
   158   if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
   159     // Language specifiers, go from 0 (C) to 14 (Objective C).
   160     // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.
   161     ERRBYE("not a traceback table");
   162   }
   164   // Existence of fields in the tbtable extension are contingent upon
   165   // specific fields in the base table.  Check for their existence so
   166   // that we can address the function name if it exists.
   167   pc2 = (codeptr_t) tb +
   168     sizeof(struct tbtable_short)/sizeof(int);
   169   if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)
   170     pc2++;
   172   if (tb->tb.has_tboff == TRUE) {
   174     // I want to know the displacement
   175     const unsigned int tb_offset = *pc2;
   176     codeptr_t start_of_procedure =
   177     (codeptr_t)(((char*)tb) - 4 - tb_offset);  // (-4 to omit leading 0000)
   179     // Weed out the cases where we did find the wrong traceback table.
   180     if (pc < start_of_procedure) {
   181       ERRBYE("could not find (the real) traceback table within 5000 bytes of program counter");
   182     }
   184     // return the displacement
   185     if (p_displacement) {
   186       (*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);
   187     }
   189     pc2++;
   190   } else {
   191     // return -1 for displacement
   192     if (p_displacement) {
   193       (*p_displacement) = -1;
   194     }
   195   }
   197   if (tb->tb.int_hndl == TRUE)
   198     pc2++;
   200   if (tb->tb.has_ctl == TRUE)
   201     pc2 += (*pc2) + 1; // don't care
   203   //
   204   // return function name if it exists.
   205   //
   206   if (p_name && namelen > 0) {
   207     if (tb->tb.name_present) {
   208       char buf[256];
   209       const short l = MIN2<short>(*((short*)pc2), sizeof(buf) - 1);
   210       memcpy(buf, (char*)pc2 + sizeof(short), l);
   211       buf[l] = '\0';
   213       p_name[0] = '\0';
   215       // If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h).
   216       char* rest;
   217       Name* const name = Demangle(buf, rest);
   218       if (name) {
   219         const char* const demangled_name = name->Text();
   220         if (demangled_name) {
   221           strncpy(p_name, demangled_name, namelen-1);
   222           p_name[namelen-1] = '\0';
   223         }
   224         delete name;
   225       }
   227       // Fallback: if demangling did not work, just provide the unmangled name.
   228       if (p_name[0] == '\0') {
   229         strncpy(p_name, buf, namelen-1);
   230         p_name[namelen-1] = '\0';
   231       }
   233     } else {
   234       strncpy(p_name, "<nameless function>", namelen-1);
   235       p_name[namelen-1] = '\0';
   236     }
   237   }
   238   // Return traceback table, if user wants it.
   239   if (p_tb) {
   240     (*p_tb) = tb;
   241   }
   243   return 0;
   244 }
   246 // Special implementation of dladdr for Aix based on LoadedLibraries
   247 // Note: dladdr returns non-zero for ok, 0 for error!
   248 // Note: dladdr is not posix, but a non-standard GNU extension. So this tries to
   249 //   fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr)
   250 // Note: addr may be both an AIX function descriptor or a real code pointer
   251 //   to the entry of a function.
   252 extern "C"
   253 int dladdr(void* addr, Dl_info* info) {
   255   if (!addr) {
   256     return 0;
   257   }
   259   assert(info, "");
   261   int rc = 0;
   263   const char* const ZEROSTRING = "";
   265   // Always return a string, even if a "" one. Linux dladdr manpage
   266   // does not say anything about returning NULL
   267   info->dli_fname = ZEROSTRING;
   268   info->dli_sname = ZEROSTRING;
   269   info->dli_saddr = NULL;
   271   address p = (address) addr;
   272   const LoadedLibraryModule* lib = NULL;
   274   enum { noclue, code, data } type = noclue;
   276   trcVerbose("dladdr(%p)...", p);
   278   // Note: input address may be a function. I accept both a pointer to
   279   // the entry of a function and a pointer to the function decriptor.
   280   // (see ppc64 ABI)
   281   lib = LoadedLibraries::find_for_text_address(p);
   282   if (lib) {
   283     type = code;
   284   }
   286   if (!lib) {
   287     // Not a pointer into any text segment. Is it a function descriptor?
   288     const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;
   289     p = pfd->entry();
   290     if (p) {
   291       lib = LoadedLibraries::find_for_text_address(p);
   292       if (lib) {
   293         type = code;
   294       }
   295     }
   296   }
   298   if (!lib) {
   299     // Neither direct code pointer nor function descriptor. A data ptr?
   300     p = (address)addr;
   301     lib = LoadedLibraries::find_for_data_address(p);
   302     if (lib) {
   303       type = data;
   304     }
   305   }
   307   // If we did find the shared library this address belongs to (either
   308   // code or data segment) resolve library path and, if possible, the
   309   // symbol name.
   310   if (lib) {
   311     const char* const interned_libpath =
   312       dladdr_fixed_strings.intern(lib->get_fullpath());
   313     if (interned_libpath) {
   314       info->dli_fname = interned_libpath;
   315     }
   317     if (type == code) {
   319       // For code symbols resolve function name and displacement. Use
   320       // displacement to calc start of function.
   321       char funcname[256] = "";
   322       int displacement = 0;
   324       if (getFuncName((codeptr_t) p, funcname, sizeof(funcname), &displacement,
   325                       NULL, NULL, 0) == 0) {
   326         if (funcname[0] != '\0') {
   327           const char* const interned = dladdr_fixed_strings.intern(funcname);
   328           info->dli_sname = interned;
   329           trcVerbose("... function name: %s ...", interned);
   330         }
   332         // From the displacement calculate the start of the function.
   333         if (displacement != -1) {
   334           info->dli_saddr = p - displacement;
   335         } else {
   336           info->dli_saddr = p;
   337         }
   338       } else {
   340         // No traceback table found. Just assume the pointer is it.
   341         info->dli_saddr = p;
   343       }
   345     } else if (type == data) {
   347       // For data symbols.
   348       info->dli_saddr = p;
   350     } else {
   351       ShouldNotReachHere();
   352     }
   354     rc = 1; // success: return 1 [sic]
   356   }
   358   // sanity checks.
   359   if (rc) {
   360     assert(info->dli_fname, "");
   361     assert(info->dli_sname, "");
   362     assert(info->dli_saddr, "");
   363   }
   365   return rc; // error: return 0 [sic]
   367 }

mercurial