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