map_anon.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file map_anon.h
  7. * \brief Headers for map_anon.c
  8. **/
  9. #ifndef TOR_MAP_ANON_H
  10. #define TOR_MAP_ANON_H
  11. #include "lib/malloc/malloc.h"
  12. #include <stddef.h>
  13. /**
  14. * When this flag is specified, try to prevent the mapping from being
  15. * swapped or dumped.
  16. *
  17. * In some operating systems, this flag is not implemented.
  18. */
  19. #define ANONMAP_PRIVATE (1u<<0)
  20. /**
  21. * When this flag is specified, try to prevent the mapping from being
  22. * inherited after a fork(). In some operating systems, trying to access it
  23. * afterwards will cause its contents to be zero. In others, trying to access
  24. * it afterwards will cause a crash.
  25. *
  26. * In some operating systems, this flag is not implemented at all.
  27. */
  28. #define ANONMAP_NOINHERIT (1u<<1)
  29. typedef enum {
  30. /** Possible value for inherit_result_out: the memory will be kept
  31. * by any child process. */
  32. INHERIT_RES_KEEP=0,
  33. /** Possible value for inherit_result_out: the memory will be dropped in the
  34. * child process. Attempting to access it will likely cause a segfault. */
  35. INHERIT_RES_DROP,
  36. /** Possible value for inherit_result_out: the memory will be cleared in
  37. * the child process. */
  38. INHERIT_RES_ZERO
  39. } inherit_res_t;
  40. /* Here we define the NOINHERIT_CAN_FAIL macro if and only if
  41. * it's possible that ANONMAP_NOINHERIT might yield inheritable memory.
  42. */
  43. #ifdef _WIN32
  44. /* Windows can't fork, so NOINHERIT is never needed. */
  45. #elif defined(HAVE_MINHERIT)
  46. /* minherit() will always have a working MAP_INHERIT_NONE or MAP_INHERIT_ZERO.
  47. * NOINHERIT should always work.
  48. */
  49. #elif defined(HAVE_MADVISE)
  50. /* madvise() sometimes has neither MADV_DONTFORK and MADV_WIPEONFORK.
  51. * We need to be ready for the possibility it failed.
  52. *
  53. * (Linux added DONTFORK in 2.6.16 and WIPEONFORK in 4.14. If we someday
  54. * require 2.6.16 or later, we can assume that DONTFORK will work.)
  55. */
  56. #define NOINHERIT_CAN_FAIL
  57. #else
  58. #define NOINHERIT_CAN_FAIL
  59. #endif /* defined(_WIN32) || ... */
  60. void *tor_mmap_anonymous(size_t sz, unsigned flags,
  61. inherit_res_t *inherit_result_out);
  62. void tor_munmap_anonymous(void *mapping, size_t sz);
  63. #endif /* !defined(TOR_MAP_ANON_H) */