mmapio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* mmapio.c -- File views using mmap.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <errno.h>
  29. #include <sys/types.h>
  30. #include <sys/mman.h>
  31. #include <unistd.h>
  32. #include "backtrace.h"
  33. #include "internal.h"
  34. #ifndef HAVE_DECL_GETPAGESIZE
  35. extern int getpagesize (void);
  36. #endif
  37. #ifndef MAP_FAILED
  38. #define MAP_FAILED ((void *)-1)
  39. #endif
  40. /* This file implements file views and memory allocation when mmap is
  41. available. */
  42. /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. */
  43. int
  44. backtrace_get_view (struct backtrace_state *state ATTRIBUTE_UNUSED,
  45. int descriptor, off_t offset, uint64_t size,
  46. backtrace_error_callback error_callback,
  47. void *data, struct backtrace_view *view)
  48. {
  49. size_t pagesize;
  50. unsigned int inpage;
  51. off_t pageoff;
  52. void *map;
  53. if ((uint64_t) (size_t) size != size)
  54. {
  55. error_callback (data, "file size too large", 0);
  56. return 0;
  57. }
  58. pagesize = getpagesize ();
  59. inpage = offset % pagesize;
  60. pageoff = offset - inpage;
  61. size += inpage;
  62. size = (size + (pagesize - 1)) & ~ (pagesize - 1);
  63. map = mmap (NULL, size, PROT_READ, MAP_PRIVATE, descriptor, pageoff);
  64. if (map == MAP_FAILED)
  65. {
  66. error_callback (data, "mmap", errno);
  67. return 0;
  68. }
  69. view->data = (char *) map + inpage;
  70. view->base = map;
  71. view->len = size;
  72. return 1;
  73. }
  74. /* Release a view read by backtrace_get_view. */
  75. void
  76. backtrace_release_view (struct backtrace_state *state ATTRIBUTE_UNUSED,
  77. struct backtrace_view *view,
  78. backtrace_error_callback error_callback,
  79. void *data)
  80. {
  81. union {
  82. const void *cv;
  83. void *v;
  84. } const_cast;
  85. const_cast.cv = view->base;
  86. if (munmap (const_cast.v, view->len) < 0)
  87. error_callback (data, "munmap", errno);
  88. }