1 /*
   2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2016 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "os_aix.inline.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/os.hpp"
  34 #include "runtime/perfMemory.hpp"
  35 #include "services/memTracker.hpp"
  36 #include "utilities/exceptions.hpp"
  37 
  38 // put OS-includes here
  39 # include <sys/types.h>
  40 # include <sys/mman.h>
  41 # include <errno.h>
  42 # include <stdio.h>
  43 # include <unistd.h>
  44 # include <sys/stat.h>
  45 # include <signal.h>
  46 # include <pwd.h>
  47 
  48 static char* backing_store_file_name = NULL;  // name of the backing store
  49                                               // file, if successfully created.
  50 
  51 // Standard Memory Implementation Details
  52 
  53 // create the PerfData memory region in standard memory.
  54 //
  55 static char* create_standard_memory(size_t size) {
  56 
  57   // allocate an aligned chuck of memory
  58   char* mapAddress = os::reserve_memory(size);
  59 
  60   if (mapAddress == NULL) {
  61     return NULL;
  62   }
  63 
  64   // commit memory
  65   if (!os::commit_memory(mapAddress, size, !ExecMem)) {
  66     if (PrintMiscellaneous && Verbose) {
  67       warning("Could not commit PerfData memory\n");
  68     }
  69     os::release_memory(mapAddress, size);
  70     return NULL;
  71   }
  72 
  73   return mapAddress;
  74 }
  75 
  76 // delete the PerfData memory region
  77 //
  78 static void delete_standard_memory(char* addr, size_t size) {
  79 
  80   // there are no persistent external resources to cleanup for standard
  81   // memory. since DestroyJavaVM does not support unloading of the JVM,
  82   // cleanup of the memory resource is not performed. The memory will be
  83   // reclaimed by the OS upon termination of the process.
  84   //
  85   return;
  86 }
  87 
  88 // save the specified memory region to the given file
  89 //
  90 // Note: this function might be called from signal handler (by os::abort()),
  91 // don't allocate heap memory.
  92 //
  93 static void save_memory_to_file(char* addr, size_t size) {
  94 
  95   const char* destfile = PerfMemory::get_perfdata_file_path();
  96   assert(destfile[0] != '\0', "invalid PerfData file path");
  97 
  98   int result;
  99 
 100   RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
 101               result);;
 102   if (result == OS_ERR) {
 103     if (PrintMiscellaneous && Verbose) {
 104       warning("Could not create Perfdata save file: %s: %s\n",
 105               destfile, os::strerror(errno));
 106     }
 107   } else {
 108     int fd = result;
 109 
 110     for (size_t remaining = size; remaining > 0;) {
 111 
 112       RESTARTABLE(::write(fd, addr, remaining), result);
 113       if (result == OS_ERR) {
 114         if (PrintMiscellaneous && Verbose) {
 115           warning("Could not write Perfdata save file: %s: %s\n",
 116                   destfile, os::strerror(errno));
 117         }
 118         break;
 119       }
 120 
 121       remaining -= (size_t)result;
 122       addr += result;
 123     }
 124 
 125     result = ::close(fd);
 126     if (PrintMiscellaneous && Verbose) {
 127       if (result == OS_ERR) {
 128         warning("Could not close %s: %s\n", destfile, os::strerror(errno));
 129       }
 130     }
 131   }
 132   FREE_C_HEAP_ARRAY(char, destfile);
 133 }
 134 
 135 
 136 // Shared Memory Implementation Details
 137 
 138 // Note: the solaris and linux shared memory implementation uses the mmap
 139 // interface with a backing store file to implement named shared memory.
 140 // Using the file system as the name space for shared memory allows a
 141 // common name space to be supported across a variety of platforms. It
 142 // also provides a name space that Java applications can deal with through
 143 // simple file apis.
 144 //
 145 // The solaris and linux implementations store the backing store file in
 146 // a user specific temporary directory located in the /tmp file system,
 147 // which is always a local file system and is sometimes a RAM based file
 148 // system.
 149 
 150 // return the user specific temporary directory name.
 151 //
 152 // the caller is expected to free the allocated memory.
 153 //
 154 static char* get_user_tmp_dir(const char* user) {
 155 
 156   const char* tmpdir = os::get_temp_directory();
 157   const char* perfdir = PERFDATA_NAME;
 158   size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
 159   char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
 160 
 161   // construct the path name to user specific tmp directory
 162   snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
 163 
 164   return dirname;
 165 }
 166 
 167 // convert the given file name into a process id. if the file
 168 // does not meet the file naming constraints, return 0.
 169 //
 170 static pid_t filename_to_pid(const char* filename) {
 171 
 172   // a filename that doesn't begin with a digit is not a
 173   // candidate for conversion.
 174   //
 175   if (!isdigit(*filename)) {
 176     return 0;
 177   }
 178 
 179   // check if file name can be converted to an integer without
 180   // any leftover characters.
 181   //
 182   char* remainder = NULL;
 183   errno = 0;
 184   pid_t pid = (pid_t)strtol(filename, &remainder, 10);
 185 
 186   if (errno != 0) {
 187     return 0;
 188   }
 189 
 190   // check for left over characters. If any, then the filename is
 191   // not a candidate for conversion.
 192   //
 193   if (remainder != NULL && *remainder != '\0') {
 194     return 0;
 195   }
 196 
 197   // successful conversion, return the pid
 198   return pid;
 199 }
 200 
 201 // Check if the given statbuf is considered a secure directory for
 202 // the backing store files. Returns true if the directory is considered
 203 // a secure location. Returns false if the statbuf is a symbolic link or
 204 // if an error occurred.
 205 //
 206 static bool is_statbuf_secure(struct stat *statp) {
 207   if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) {
 208     // The path represents a link or some non-directory file type,
 209     // which is not what we expected. Declare it insecure.
 210     //
 211     return false;
 212   }
 213   // We have an existing directory, check if the permissions are safe.
 214   //
 215   if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) {
 216     // The directory is open for writing and could be subjected
 217     // to a symlink or a hard link attack. Declare it insecure.
 218     //
 219     return false;
 220   }
 221   // If user is not root then see if the uid of the directory matches the effective uid of the process.
 222   uid_t euid = geteuid();
 223   if ((euid != 0) && (statp->st_uid != euid)) {
 224     // The directory was not created by this user, declare it insecure.
 225     //
 226     return false;
 227   }
 228   return true;
 229 }
 230 
 231 
 232 // Check if the given path is considered a secure directory for
 233 // the backing store files. Returns true if the directory exists
 234 // and is considered a secure location. Returns false if the path
 235 // is a symbolic link or if an error occurred.
 236 //
 237 static bool is_directory_secure(const char* path) {
 238   struct stat statbuf;
 239   int result = 0;
 240 
 241   RESTARTABLE(::lstat(path, &statbuf), result);
 242   if (result == OS_ERR) {
 243     return false;
 244   }
 245 
 246   // The path exists, see if it is secure.
 247   return is_statbuf_secure(&statbuf);
 248 }
 249 
 250 // (Taken over from Solaris to support the O_NOFOLLOW case on AIX.)
 251 // Check if the given directory file descriptor is considered a secure
 252 // directory for the backing store files. Returns true if the directory
 253 // exists and is considered a secure location. Returns false if the path
 254 // is a symbolic link or if an error occurred.
 255 static bool is_dirfd_secure(int dir_fd) {
 256   struct stat statbuf;
 257   int result = 0;
 258 
 259   RESTARTABLE(::fstat(dir_fd, &statbuf), result);
 260   if (result == OS_ERR) {
 261     return false;
 262   }
 263 
 264   // The path exists, now check its mode.
 265   return is_statbuf_secure(&statbuf);
 266 }
 267 
 268 
 269 // Check to make sure fd1 and fd2 are referencing the same file system object.
 270 static bool is_same_fsobject(int fd1, int fd2) {
 271   struct stat statbuf1;
 272   struct stat statbuf2;
 273   int result = 0;
 274 
 275   RESTARTABLE(::fstat(fd1, &statbuf1), result);
 276   if (result == OS_ERR) {
 277     return false;
 278   }
 279   RESTARTABLE(::fstat(fd2, &statbuf2), result);
 280   if (result == OS_ERR) {
 281     return false;
 282   }
 283 
 284   if ((statbuf1.st_ino == statbuf2.st_ino) &&
 285       (statbuf1.st_dev == statbuf2.st_dev)) {
 286     return true;
 287   } else {
 288     return false;
 289   }
 290 }
 291 
 292 // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1.
 293 // We use the jdk6 implementation here.
 294 #ifndef O_NOFOLLOW
 295 // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour
 296 // was done in jdk 5/6 hotspot by Oracle this way
 297 static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) {
 298   struct stat orig_st;
 299   struct stat new_st;
 300   bool create;
 301   int error;
 302   int fd;
 303   int result;
 304 
 305   create = false;
 306 
 307   RESTARTABLE(::lstat(path, &orig_st), result);
 308 
 309   if (result == OS_ERR) {
 310     if (errno == ENOENT && (oflag & O_CREAT) != 0) {
 311       // File doesn't exist, but_we want to create it, add O_EXCL flag
 312       // to make sure no-one creates it (or a symlink) before us
 313       // This works as we expect with symlinks, from posix man page:
 314       // 'If O_EXCL  and  O_CREAT  are set, and path names a symbolic
 315       // link, open() shall fail and set errno to [EEXIST]'.
 316       oflag |= O_EXCL;
 317       create = true;
 318     } else {
 319       // File doesn't exist, and we are not creating it.
 320       return OS_ERR;
 321     }
 322   } else {
 323     // lstat success, check if existing file is a link.
 324     if ((orig_st.st_mode & S_IFMT) == S_IFLNK)  {
 325       // File is a symlink.
 326       errno = ELOOP;
 327       return OS_ERR;
 328     }
 329   }
 330 
 331   if (use_mode == true) {
 332     RESTARTABLE(::open(path, oflag, mode), fd);
 333   } else {
 334     RESTARTABLE(::open(path, oflag), fd);
 335   }
 336 
 337   if (fd == OS_ERR) {
 338     return fd;
 339   }
 340 
 341   // Can't do inode checks on before/after if we created the file.
 342   if (create == false) {
 343     RESTARTABLE(::fstat(fd, &new_st), result);
 344     if (result == OS_ERR) {
 345       // Keep errno from fstat, in case close also fails.
 346       error = errno;
 347       ::close(fd);
 348       errno = error;
 349       return OS_ERR;
 350     }
 351 
 352     if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) {
 353       // File was tampered with during race window.
 354       ::close(fd);
 355       errno = EEXIST;
 356       if (PrintMiscellaneous && Verbose) {
 357         warning("possible file tampering attempt detected when opening %s", path);
 358       }
 359       return OS_ERR;
 360     }
 361   }
 362 
 363   return fd;
 364 }
 365 
 366 static int open_o_nofollow(const char* path, int oflag, mode_t mode) {
 367   return open_o_nofollow_impl(path, oflag, mode, true);
 368 }
 369 
 370 static int open_o_nofollow(const char* path, int oflag) {
 371   return open_o_nofollow_impl(path, oflag, 0, false);
 372 }
 373 #endif
 374 
 375 // Open the directory of the given path and validate it.
 376 // Return a DIR * of the open directory.
 377 static DIR *open_directory_secure(const char* dirname) {
 378   // Open the directory using open() so that it can be verified
 379   // to be secure by calling is_dirfd_secure(), opendir() and then check
 380   // to see if they are the same file system object.  This method does not
 381   // introduce a window of opportunity for the directory to be attacked that
 382   // calling opendir() and is_directory_secure() does.
 383   int result;
 384   DIR *dirp = NULL;
 385 
 386   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
 387   // so provide a workaround in this case.
 388 #ifdef O_NOFOLLOW
 389   RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
 390 #else
 391   // workaround (jdk6 coding)
 392   result = open_o_nofollow(dirname, O_RDONLY);
 393 #endif
 394 
 395   if (result == OS_ERR) {
 396     // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
 397     if (PrintMiscellaneous && Verbose) {
 398       if (errno == ELOOP) {
 399         warning("directory %s is a symlink and is not secure\n", dirname);
 400       } else {
 401         warning("could not open directory %s: %s\n", dirname, os::strerror(errno));
 402       }
 403     }
 404     return dirp;
 405   }
 406   int fd = result;
 407 
 408   // Determine if the open directory is secure.
 409   if (!is_dirfd_secure(fd)) {
 410     // The directory is not a secure directory.
 411     os::close(fd);
 412     return dirp;
 413   }
 414 
 415   // Open the directory.
 416   dirp = ::opendir(dirname);
 417   if (dirp == NULL) {
 418     // The directory doesn't exist, close fd and return.
 419     os::close(fd);
 420     return dirp;
 421   }
 422 
 423   // Check to make sure fd and dirp are referencing the same file system object.
 424   if (!is_same_fsobject(fd, dirp->dd_fd)) {
 425     // The directory is not secure.
 426     os::close(fd);
 427     os::closedir(dirp);
 428     dirp = NULL;
 429     return dirp;
 430   }
 431 
 432   // Close initial open now that we know directory is secure
 433   os::close(fd);
 434 
 435   return dirp;
 436 }
 437 
 438 // NOTE: The code below uses fchdir(), open() and unlink() because
 439 // fdopendir(), openat() and unlinkat() are not supported on all
 440 // versions.  Once the support for fdopendir(), openat() and unlinkat()
 441 // is available on all supported versions the code can be changed
 442 // to use these functions.
 443 
 444 // Open the directory of the given path, validate it and set the
 445 // current working directory to it.
 446 // Return a DIR * of the open directory and the saved cwd fd.
 447 //
 448 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
 449 
 450   // Open the directory.
 451   DIR* dirp = open_directory_secure(dirname);
 452   if (dirp == NULL) {
 453     // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 454     return dirp;
 455   }
 456   int fd = dirp->dd_fd;
 457 
 458   // Open a fd to the cwd and save it off.
 459   int result;
 460   RESTARTABLE(::open(".", O_RDONLY), result);
 461   if (result == OS_ERR) {
 462     *saved_cwd_fd = -1;
 463   } else {
 464     *saved_cwd_fd = result;
 465   }
 466 
 467   // Set the current directory to dirname by using the fd of the directory and
 468   // handle errors, otherwise shared memory files will be created in cwd.
 469   result = fchdir(fd);
 470   if (result == OS_ERR) {
 471     if (PrintMiscellaneous && Verbose) {
 472       warning("could not change to directory %s", dirname);
 473     }
 474     if (*saved_cwd_fd != -1) {
 475       ::close(*saved_cwd_fd);
 476       *saved_cwd_fd = -1;
 477     }
 478     // Close the directory.
 479     os::closedir(dirp);
 480     return NULL;
 481   } else {
 482     return dirp;
 483   }
 484 }
 485 
 486 // Close the directory and restore the current working directory.
 487 //
 488 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
 489 
 490   int result;
 491   // If we have a saved cwd change back to it and close the fd.
 492   if (saved_cwd_fd != -1) {
 493     result = fchdir(saved_cwd_fd);
 494     ::close(saved_cwd_fd);
 495   }
 496 
 497   // Close the directory.
 498   os::closedir(dirp);
 499 }
 500 
 501 // Check if the given file descriptor is considered a secure.
 502 static bool is_file_secure(int fd, const char *filename) {
 503 
 504   int result;
 505   struct stat statbuf;
 506 
 507   // Determine if the file is secure.
 508   RESTARTABLE(::fstat(fd, &statbuf), result);
 509   if (result == OS_ERR) {
 510     if (PrintMiscellaneous && Verbose) {
 511       warning("fstat failed on %s: %s\n", filename, os::strerror(errno));
 512     }
 513     return false;
 514   }
 515   if (statbuf.st_nlink > 1) {
 516     // A file with multiple links is not expected.
 517     if (PrintMiscellaneous && Verbose) {
 518       warning("file %s has multiple links\n", filename);
 519     }
 520     return false;
 521   }
 522   return true;
 523 }
 524 
 525 // Return the user name for the given user id.
 526 //
 527 // The caller is expected to free the allocated memory.
 528 static char* get_user_name(uid_t uid) {
 529 
 530   struct passwd pwent;
 531 
 532   // Determine the max pwbuf size from sysconf, and hardcode
 533   // a default if this not available through sysconf.
 534   long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
 535   if (bufsize == -1)
 536     bufsize = 1024;
 537 
 538   char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
 539 
 540   struct passwd* p;
 541   int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
 542 
 543   if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
 544     if (PrintMiscellaneous && Verbose) {
 545       if (result != 0) {
 546         warning("Could not retrieve passwd entry: %s\n",
 547                 os::strerror(result));
 548       }
 549       else if (p == NULL) {
 550         // this check is added to protect against an observed problem
 551         // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
 552         // indicating success, but has p == NULL. This was observed when
 553         // inserting a file descriptor exhaustion fault prior to the call
 554         // getpwuid_r() call. In this case, error is set to the appropriate
 555         // error condition, but this is undocumented behavior. This check
 556         // is safe under any condition, but the use of errno in the output
 557         // message may result in an erroneous message.
 558         // Bug Id 89052 was opened with RedHat.
 559         //
 560         warning("Could not retrieve passwd entry: %s\n",
 561                 os::strerror(errno));
 562       }
 563       else {
 564         warning("Could not determine user name: %s\n",
 565                 p->pw_name == NULL ? "pw_name = NULL" :
 566                                      "pw_name zero length");
 567       }
 568     }
 569     FREE_C_HEAP_ARRAY(char, pwbuf);
 570     return NULL;
 571   }
 572 
 573   char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
 574   strcpy(user_name, p->pw_name);
 575 
 576   FREE_C_HEAP_ARRAY(char, pwbuf);
 577   return user_name;
 578 }
 579 
 580 // return the name of the user that owns the process identified by vmid.
 581 //
 582 // This method uses a slow directory search algorithm to find the backing
 583 // store file for the specified vmid and returns the user name, as determined
 584 // by the user name suffix of the hsperfdata_<username> directory name.
 585 //
 586 // the caller is expected to free the allocated memory.
 587 //
 588 static char* get_user_name_slow(int vmid, TRAPS) {
 589 
 590   // short circuit the directory search if the process doesn't even exist.
 591   if (kill(vmid, 0) == OS_ERR) {
 592     if (errno == ESRCH) {
 593       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 594                   "Process not found");
 595     }
 596     else /* EPERM */ {
 597       THROW_MSG_0(vmSymbols::java_io_IOException(), os::strerror(errno));
 598     }
 599   }
 600 
 601   // directory search
 602   char* oldest_user = NULL;
 603   time_t oldest_ctime = 0;
 604 
 605   const char* tmpdirname = os::get_temp_directory();
 606 
 607   DIR* tmpdirp = os::opendir(tmpdirname);
 608 
 609   if (tmpdirp == NULL) {
 610     return NULL;
 611   }
 612 
 613   // for each entry in the directory that matches the pattern hsperfdata_*,
 614   // open the directory and check if the file for the given vmid exists.
 615   // The file with the expected name and the latest creation date is used
 616   // to determine the user name for the process id.
 617   //
 618   struct dirent* dentry;
 619   char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
 620   errno = 0;
 621   while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
 622 
 623     // check if the directory entry is a hsperfdata file
 624     if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
 625       continue;
 626     }
 627 
 628     char* usrdir_name = NEW_C_HEAP_ARRAY(char,
 629                               strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
 630     strcpy(usrdir_name, tmpdirname);
 631     strcat(usrdir_name, "/");
 632     strcat(usrdir_name, dentry->d_name);
 633 
 634     // Open the user directory.
 635     DIR* subdirp = open_directory_secure(usrdir_name);
 636 
 637     if (subdirp == NULL) {
 638       FREE_C_HEAP_ARRAY(char, usrdir_name);
 639       continue;
 640     }
 641 
 642     // Since we don't create the backing store files in directories
 643     // pointed to by symbolic links, we also don't follow them when
 644     // looking for the files. We check for a symbolic link after the
 645     // call to opendir in order to eliminate a small window where the
 646     // symlink can be exploited.
 647     //
 648     if (!is_directory_secure(usrdir_name)) {
 649       FREE_C_HEAP_ARRAY(char, usrdir_name);
 650       os::closedir(subdirp);
 651       continue;
 652     }
 653 
 654     struct dirent* udentry;
 655     char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
 656     errno = 0;
 657     while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
 658 
 659       if (filename_to_pid(udentry->d_name) == vmid) {
 660         struct stat statbuf;
 661         int result;
 662 
 663         char* filename = NEW_C_HEAP_ARRAY(char,
 664                             strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
 665 
 666         strcpy(filename, usrdir_name);
 667         strcat(filename, "/");
 668         strcat(filename, udentry->d_name);
 669 
 670         // don't follow symbolic links for the file
 671         RESTARTABLE(::lstat(filename, &statbuf), result);
 672         if (result == OS_ERR) {
 673            FREE_C_HEAP_ARRAY(char, filename);
 674            continue;
 675         }
 676 
 677         // skip over files that are not regular files.
 678         if (!S_ISREG(statbuf.st_mode)) {
 679           FREE_C_HEAP_ARRAY(char, filename);
 680           continue;
 681         }
 682 
 683         // compare and save filename with latest creation time
 684         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
 685 
 686           if (statbuf.st_ctime > oldest_ctime) {
 687             char* user = strchr(dentry->d_name, '_') + 1;
 688 
 689             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user);
 690             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
 691 
 692             strcpy(oldest_user, user);
 693             oldest_ctime = statbuf.st_ctime;
 694           }
 695         }
 696 
 697         FREE_C_HEAP_ARRAY(char, filename);
 698       }
 699     }
 700     os::closedir(subdirp);
 701     FREE_C_HEAP_ARRAY(char, udbuf);
 702     FREE_C_HEAP_ARRAY(char, usrdir_name);
 703   }
 704   os::closedir(tmpdirp);
 705   FREE_C_HEAP_ARRAY(char, tdbuf);
 706 
 707   return(oldest_user);
 708 }
 709 
 710 // return the name of the user that owns the JVM indicated by the given vmid.
 711 //
 712 static char* get_user_name(int vmid, TRAPS) {
 713   return get_user_name_slow(vmid, THREAD);
 714 }
 715 
 716 // return the file name of the backing store file for the named
 717 // shared memory region for the given user name and vmid.
 718 //
 719 // the caller is expected to free the allocated memory.
 720 //
 721 static char* get_sharedmem_filename(const char* dirname, int vmid) {
 722 
 723   // add 2 for the file separator and a null terminator.
 724   size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
 725 
 726   char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
 727   snprintf(name, nbytes, "%s/%d", dirname, vmid);
 728 
 729   return name;
 730 }
 731 
 732 
 733 // remove file
 734 //
 735 // this method removes the file specified by the given path
 736 //
 737 static void remove_file(const char* path) {
 738 
 739   int result;
 740 
 741   // if the file is a directory, the following unlink will fail. since
 742   // we don't expect to find directories in the user temp directory, we
 743   // won't try to handle this situation. even if accidentially or
 744   // maliciously planted, the directory's presence won't hurt anything.
 745   //
 746   RESTARTABLE(::unlink(path), result);
 747   if (PrintMiscellaneous && Verbose && result == OS_ERR) {
 748     if (errno != ENOENT) {
 749       warning("Could not unlink shared memory backing"
 750               " store file %s : %s\n", path, os::strerror(errno));
 751     }
 752   }
 753 }
 754 
 755 // Cleanup stale shared memory resources
 756 //
 757 // This method attempts to remove all stale shared memory files in
 758 // the named user temporary directory. It scans the named directory
 759 // for files matching the pattern ^$[0-9]*$. For each file found, the
 760 // process id is extracted from the file name and a test is run to
 761 // determine if the process is alive. If the process is not alive,
 762 // any stale file resources are removed.
 763 static void cleanup_sharedmem_resources(const char* dirname) {
 764 
 765   int saved_cwd_fd;
 766   // Open the directory.
 767   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 768   if (dirp == NULL) {
 769      // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 770     return;
 771   }
 772 
 773   // For each entry in the directory that matches the expected file
 774   // name pattern, determine if the file resources are stale and if
 775   // so, remove the file resources. Note, instrumented HotSpot processes
 776   // for this user may start and/or terminate during this search and
 777   // remove or create new files in this directory. The behavior of this
 778   // loop under these conditions is dependent upon the implementation of
 779   // opendir/readdir.
 780   struct dirent* entry;
 781   char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
 782 
 783   errno = 0;
 784   while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
 785 
 786     pid_t pid = filename_to_pid(entry->d_name);
 787 
 788     if (pid == 0) {
 789 
 790       if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
 791 
 792         // Attempt to remove all unexpected files, except "." and "..".
 793         unlink(entry->d_name);
 794       }
 795 
 796       errno = 0;
 797       continue;
 798     }
 799 
 800     // We now have a file name that converts to a valid integer
 801     // that could represent a process id . if this process id
 802     // matches the current process id or the process is not running,
 803     // then remove the stale file resources.
 804     //
 805     // Process liveness is detected by sending signal number 0 to
 806     // the process id (see kill(2)). if kill determines that the
 807     // process does not exist, then the file resources are removed.
 808     // if kill determines that that we don't have permission to
 809     // signal the process, then the file resources are assumed to
 810     // be stale and are removed because the resources for such a
 811     // process should be in a different user specific directory.
 812     if ((pid == os::current_process_id()) ||
 813         (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
 814 
 815         unlink(entry->d_name);
 816     }
 817     errno = 0;
 818   }
 819 
 820   // Close the directory and reset the current working directory.
 821   close_directory_secure_cwd(dirp, saved_cwd_fd);
 822 
 823   FREE_C_HEAP_ARRAY(char, dbuf);
 824 }
 825 
 826 // Make the user specific temporary directory. Returns true if
 827 // the directory exists and is secure upon return. Returns false
 828 // if the directory exists but is either a symlink, is otherwise
 829 // insecure, or if an error occurred.
 830 static bool make_user_tmp_dir(const char* dirname) {
 831 
 832   // Create the directory with 0755 permissions. note that the directory
 833   // will be owned by euid::egid, which may not be the same as uid::gid.
 834   if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
 835     if (errno == EEXIST) {
 836       // The directory already exists and was probably created by another
 837       // JVM instance. However, this could also be the result of a
 838       // deliberate symlink. Verify that the existing directory is safe.
 839       if (!is_directory_secure(dirname)) {
 840         // Directory is not secure.
 841         if (PrintMiscellaneous && Verbose) {
 842           warning("%s directory is insecure\n", dirname);
 843         }
 844         return false;
 845       }
 846     }
 847     else {
 848       // we encountered some other failure while attempting
 849       // to create the directory
 850       //
 851       if (PrintMiscellaneous && Verbose) {
 852         warning("could not create directory %s: %s\n",
 853                 dirname, os::strerror(errno));
 854       }
 855       return false;
 856     }
 857   }
 858   return true;
 859 }
 860 
 861 // create the shared memory file resources
 862 //
 863 // This method creates the shared memory file with the given size
 864 // This method also creates the user specific temporary directory, if
 865 // it does not yet exist.
 866 //
 867 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
 868 
 869   // make the user temporary directory
 870   if (!make_user_tmp_dir(dirname)) {
 871     // could not make/find the directory or the found directory
 872     // was not secure
 873     return -1;
 874   }
 875 
 876   int saved_cwd_fd;
 877   // Open the directory and set the current working directory to it.
 878   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 879   if (dirp == NULL) {
 880     // Directory doesn't exist or is insecure, so cannot create shared
 881     // memory file.
 882     return -1;
 883   }
 884 
 885   // Open the filename in the current directory.
 886   // Cannot use O_TRUNC here; truncation of an existing file has to happen
 887   // after the is_file_secure() check below.
 888   int result;
 889 
 890   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
 891   // so provide a workaround in this case.
 892 #ifdef O_NOFOLLOW
 893   RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
 894 #else
 895   // workaround function (jdk6 code)
 896   result = open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
 897 #endif
 898 
 899   if (result == OS_ERR) {
 900     if (PrintMiscellaneous && Verbose) {
 901       if (errno == ELOOP) {
 902         warning("file %s is a symlink and is not secure\n", filename);
 903       } else {
 904         warning("could not create file %s: %s\n", filename, os::strerror(errno));
 905       }
 906     }
 907     // Close the directory and reset the current working directory.
 908     close_directory_secure_cwd(dirp, saved_cwd_fd);
 909 
 910     return -1;
 911   }
 912   // Close the directory and reset the current working directory.
 913   close_directory_secure_cwd(dirp, saved_cwd_fd);
 914 
 915   // save the file descriptor
 916   int fd = result;
 917 
 918   // Check to see if the file is secure.
 919   if (!is_file_secure(fd, filename)) {
 920     ::close(fd);
 921     return -1;
 922   }
 923 
 924   // Truncate the file to get rid of any existing data.
 925   RESTARTABLE(::ftruncate(fd, (off_t)0), result);
 926   if (result == OS_ERR) {
 927     if (PrintMiscellaneous && Verbose) {
 928       warning("could not truncate shared memory file: %s\n", os::strerror(errno));
 929     }
 930     ::close(fd);
 931     return -1;
 932   }
 933   // set the file size
 934   RESTARTABLE(::ftruncate(fd, (off_t)size), result);
 935   if (result == OS_ERR) {
 936     if (PrintMiscellaneous && Verbose) {
 937       warning("could not set shared memory file size: %s\n", os::strerror(errno));
 938     }
 939     ::close(fd);
 940     return -1;
 941   }
 942 
 943   return fd;
 944 }
 945 
 946 // open the shared memory file for the given user and vmid. returns
 947 // the file descriptor for the open file or -1 if the file could not
 948 // be opened.
 949 //
 950 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
 951 
 952   // open the file
 953   int result;
 954   // provide a workaround in case no O_NOFOLLOW is defined at buildtime
 955 #ifdef O_NOFOLLOW
 956   RESTARTABLE(::open(filename, oflags), result);
 957 #else
 958   result = open_o_nofollow(filename, oflags);
 959 #endif
 960   if (result == OS_ERR) {
 961     if (errno == ENOENT) {
 962       THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 963                  "Process not found", OS_ERR);
 964     }
 965     else if (errno == EACCES) {
 966       THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 967                  "Permission denied", OS_ERR);
 968     }
 969     else {
 970       THROW_MSG_(vmSymbols::java_io_IOException(),
 971                  os::strerror(errno), OS_ERR);
 972     }
 973   }
 974   int fd = result;
 975 
 976   // Check to see if the file is secure.
 977   if (!is_file_secure(fd, filename)) {
 978     ::close(fd);
 979     return -1;
 980   }
 981 
 982   return fd;
 983 }
 984 
 985 // create a named shared memory region. returns the address of the
 986 // memory region on success or NULL on failure. A return value of
 987 // NULL will ultimately disable the shared memory feature.
 988 //
 989 // On AIX, the name space for shared memory objects
 990 // is the file system name space.
 991 //
 992 // A monitoring application attaching to a JVM does not need to know
 993 // the file system name of the shared memory object. However, it may
 994 // be convenient for applications to discover the existence of newly
 995 // created and terminating JVMs by watching the file system name space
 996 // for files being created or removed.
 997 //
 998 static char* mmap_create_shared(size_t size) {
 999 
1000   int result;
1001   int fd;
1002   char* mapAddress;
1003 
1004   int vmid = os::current_process_id();
1005 
1006   char* user_name = get_user_name(geteuid());
1007 
1008   if (user_name == NULL)
1009     return NULL;
1010 
1011   char* dirname = get_user_tmp_dir(user_name);
1012   char* filename = get_sharedmem_filename(dirname, vmid);
1013 
1014   // get the short filename.
1015   char* short_filename = strrchr(filename, '/');
1016   if (short_filename == NULL) {
1017     short_filename = filename;
1018   } else {
1019     short_filename++;
1020   }
1021 
1022   // cleanup any stale shared memory files
1023   cleanup_sharedmem_resources(dirname);
1024 
1025   assert(((size > 0) && (size % os::vm_page_size() == 0)),
1026          "unexpected PerfMemory region size");
1027 
1028   fd = create_sharedmem_resources(dirname, short_filename, size);
1029 
1030   FREE_C_HEAP_ARRAY(char, user_name);
1031   FREE_C_HEAP_ARRAY(char, dirname);
1032 
1033   if (fd == -1) {
1034     FREE_C_HEAP_ARRAY(char, filename);
1035     return NULL;
1036   }
1037 
1038   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1039 
1040   result = ::close(fd);
1041   assert(result != OS_ERR, "could not close file");
1042 
1043   if (mapAddress == MAP_FAILED) {
1044     if (PrintMiscellaneous && Verbose) {
1045       warning("mmap failed -  %s\n", os::strerror(errno));
1046     }
1047     remove_file(filename);
1048     FREE_C_HEAP_ARRAY(char, filename);
1049     return NULL;
1050   }
1051 
1052   // save the file name for use in delete_shared_memory()
1053   backing_store_file_name = filename;
1054 
1055   // clear the shared memory region
1056   (void)::memset((void*) mapAddress, 0, size);
1057 
1058   // It does not go through os api, the operation has to record from here.
1059   MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1060 
1061   return mapAddress;
1062 }
1063 
1064 // release a named shared memory region
1065 //
1066 static void unmap_shared(char* addr, size_t bytes) {
1067   // Do not rely on os::reserve_memory/os::release_memory to use mmap.
1068   // Use os::reserve_memory/os::release_memory for PerfDisableSharedMem=1, mmap/munmap for PerfDisableSharedMem=0
1069   if (::munmap(addr, bytes) == -1) {
1070     warning("perfmemory: munmap failed (%d)\n", errno);
1071   }
1072 }
1073 
1074 // create the PerfData memory region in shared memory.
1075 //
1076 static char* create_shared_memory(size_t size) {
1077 
1078   // create the shared memory region.
1079   return mmap_create_shared(size);
1080 }
1081 
1082 // delete the shared PerfData memory region
1083 //
1084 static void delete_shared_memory(char* addr, size_t size) {
1085 
1086   // cleanup the persistent shared memory resources. since DestroyJavaVM does
1087   // not support unloading of the JVM, unmapping of the memory resource is
1088   // not performed. The memory will be reclaimed by the OS upon termination of
1089   // the process. The backing store file is deleted from the file system.
1090 
1091   assert(!PerfDisableSharedMem, "shouldn't be here");
1092 
1093   if (backing_store_file_name != NULL) {
1094     remove_file(backing_store_file_name);
1095     // Don't.. Free heap memory could deadlock os::abort() if it is called
1096     // from signal handler. OS will reclaim the heap memory.
1097     // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
1098     backing_store_file_name = NULL;
1099   }
1100 }
1101 
1102 // return the size of the file for the given file descriptor
1103 // or 0 if it is not a valid size for a shared memory file
1104 //
1105 static size_t sharedmem_filesize(int fd, TRAPS) {
1106 
1107   struct stat statbuf;
1108   int result;
1109 
1110   RESTARTABLE(::fstat(fd, &statbuf), result);
1111   if (result == OS_ERR) {
1112     if (PrintMiscellaneous && Verbose) {
1113       warning("fstat failed: %s\n", os::strerror(errno));
1114     }
1115     THROW_MSG_0(vmSymbols::java_io_IOException(),
1116                 "Could not determine PerfMemory size");
1117   }
1118 
1119   if ((statbuf.st_size == 0) ||
1120      ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
1121     THROW_MSG_0(vmSymbols::java_lang_Exception(),
1122                 "Invalid PerfMemory size");
1123   }
1124 
1125   return (size_t)statbuf.st_size;
1126 }
1127 
1128 // attach to a named shared memory region.
1129 //
1130 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
1131 
1132   char* mapAddress;
1133   int result;
1134   int fd;
1135   size_t size = 0;
1136   const char* luser = NULL;
1137 
1138   int mmap_prot;
1139   int file_flags;
1140 
1141   ResourceMark rm;
1142 
1143   // map the high level access mode to the appropriate permission
1144   // constructs for the file and the shared memory mapping.
1145   if (mode == PerfMemory::PERF_MODE_RO) {
1146     mmap_prot = PROT_READ;
1147   // No O_NOFOLLOW defined at buildtime, and it is not documented for open.
1148 #ifdef O_NOFOLLOW
1149     file_flags = O_RDONLY | O_NOFOLLOW;
1150 #else
1151     file_flags = O_RDONLY;
1152 #endif
1153   }
1154   else if (mode == PerfMemory::PERF_MODE_RW) {
1155 #ifdef LATER
1156     mmap_prot = PROT_READ | PROT_WRITE;
1157     file_flags = O_RDWR | O_NOFOLLOW;
1158 #else
1159     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1160               "Unsupported access mode");
1161 #endif
1162   }
1163   else {
1164     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1165               "Illegal access mode");
1166   }
1167 
1168   if (user == NULL || strlen(user) == 0) {
1169     luser = get_user_name(vmid, CHECK);
1170   }
1171   else {
1172     luser = user;
1173   }
1174 
1175   if (luser == NULL) {
1176     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1177               "Could not map vmid to user Name");
1178   }
1179 
1180   char* dirname = get_user_tmp_dir(luser);
1181 
1182   // since we don't follow symbolic links when creating the backing
1183   // store file, we don't follow them when attaching either.
1184   //
1185   if (!is_directory_secure(dirname)) {
1186     FREE_C_HEAP_ARRAY(char, dirname);
1187     if (luser != user) {
1188       FREE_C_HEAP_ARRAY(char, luser);
1189     }
1190     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1191               "Process not found");
1192   }
1193 
1194   char* filename = get_sharedmem_filename(dirname, vmid);
1195 
1196   // copy heap memory to resource memory. the open_sharedmem_file
1197   // method below need to use the filename, but could throw an
1198   // exception. using a resource array prevents the leak that
1199   // would otherwise occur.
1200   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1201   strcpy(rfilename, filename);
1202 
1203   // free the c heap resources that are no longer needed
1204   if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
1205   FREE_C_HEAP_ARRAY(char, dirname);
1206   FREE_C_HEAP_ARRAY(char, filename);
1207 
1208   // open the shared memory file for the give vmid
1209   fd = open_sharedmem_file(rfilename, file_flags, THREAD);
1210 
1211   if (fd == OS_ERR) {
1212     return;
1213   }
1214 
1215   if (HAS_PENDING_EXCEPTION) {
1216     ::close(fd);
1217     return;
1218   }
1219 
1220   if (*sizep == 0) {
1221     size = sharedmem_filesize(fd, CHECK);
1222   } else {
1223     size = *sizep;
1224   }
1225 
1226   assert(size > 0, "unexpected size <= 0");
1227 
1228   mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
1229 
1230   result = ::close(fd);
1231   assert(result != OS_ERR, "could not close file");
1232 
1233   if (mapAddress == MAP_FAILED) {
1234     if (PrintMiscellaneous && Verbose) {
1235       warning("mmap failed: %s\n", os::strerror(errno));
1236     }
1237     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
1238               "Could not map PerfMemory");
1239   }
1240 
1241   // it does not go through os api, the operation has to record from here.
1242   MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1243 
1244   *addr = mapAddress;
1245   *sizep = size;
1246 
1247   if (PerfTraceMemOps) {
1248     tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
1249                INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress));
1250   }
1251 }
1252 
1253 
1254 
1255 
1256 // create the PerfData memory region
1257 //
1258 // This method creates the memory region used to store performance
1259 // data for the JVM. The memory may be created in standard or
1260 // shared memory.
1261 //
1262 void PerfMemory::create_memory_region(size_t size) {
1263 
1264   if (PerfDisableSharedMem) {
1265     // do not share the memory for the performance data.
1266     _start = create_standard_memory(size);
1267   }
1268   else {
1269     _start = create_shared_memory(size);
1270     if (_start == NULL) {
1271 
1272       // creation of the shared memory region failed, attempt
1273       // to create a contiguous, non-shared memory region instead.
1274       //
1275       if (PrintMiscellaneous && Verbose) {
1276         warning("Reverting to non-shared PerfMemory region.\n");
1277       }
1278       PerfDisableSharedMem = true;
1279       _start = create_standard_memory(size);
1280     }
1281   }
1282 
1283   if (_start != NULL) _capacity = size;
1284 
1285 }
1286 
1287 // delete the PerfData memory region
1288 //
1289 // This method deletes the memory region used to store performance
1290 // data for the JVM. The memory region indicated by the <address, size>
1291 // tuple will be inaccessible after a call to this method.
1292 //
1293 void PerfMemory::delete_memory_region() {
1294 
1295   assert((start() != NULL && capacity() > 0), "verify proper state");
1296 
1297   // If user specifies PerfDataSaveFile, it will save the performance data
1298   // to the specified file name no matter whether PerfDataSaveToFile is specified
1299   // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
1300   // -XX:+PerfDataSaveToFile.
1301   if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
1302     save_memory_to_file(start(), capacity());
1303   }
1304 
1305   if (PerfDisableSharedMem) {
1306     delete_standard_memory(start(), capacity());
1307   }
1308   else {
1309     delete_shared_memory(start(), capacity());
1310   }
1311 }
1312 
1313 // attach to the PerfData memory region for another JVM
1314 //
1315 // This method returns an <address, size> tuple that points to
1316 // a memory buffer that is kept reasonably synchronized with
1317 // the PerfData memory region for the indicated JVM. This
1318 // buffer may be kept in synchronization via shared memory
1319 // or some other mechanism that keeps the buffer updated.
1320 //
1321 // If the JVM chooses not to support the attachability feature,
1322 // this method should throw an UnsupportedOperation exception.
1323 //
1324 // This implementation utilizes named shared memory to map
1325 // the indicated process's PerfData memory region into this JVMs
1326 // address space.
1327 //
1328 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
1329 
1330   if (vmid == 0 || vmid == os::current_process_id()) {
1331      *addrp = start();
1332      *sizep = capacity();
1333      return;
1334   }
1335 
1336   mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
1337 }
1338 
1339 // detach from the PerfData memory region of another JVM
1340 //
1341 // This method detaches the PerfData memory region of another
1342 // JVM, specified as an <address, size> tuple of a buffer
1343 // in this process's address space. This method may perform
1344 // arbitrary actions to accomplish the detachment. The memory
1345 // region specified by <address, size> will be inaccessible after
1346 // a call to this method.
1347 //
1348 // If the JVM chooses not to support the attachability feature,
1349 // this method should throw an UnsupportedOperation exception.
1350 //
1351 // This implementation utilizes named shared memory to detach
1352 // the indicated process's PerfData memory region from this
1353 // process's address space.
1354 //
1355 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
1356 
1357   assert(addr != 0, "address sanity check");
1358   assert(bytes > 0, "capacity sanity check");
1359 
1360   if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
1361     // prevent accidental detachment of this process's PerfMemory region
1362     return;
1363   }
1364 
1365   unmap_shared(addr, bytes);
1366 }