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