aboutsummaryrefslogtreecommitdiffstats
path: root/memcheck/memcheck_malloc_map.h
blob: 1c25738f6550c93cb283e328fa62bb53f37dc103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright (C) 2007-2010 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/

/*
 * Contains declarations of structures and routines that implement a red-black
 * tree (a map) of memory blocks allocated by the guest system. The map is
 * organized in such a way, that each entry in the map describes a virtual
 * address range that belongs to a memory block allocated in the guest's space.
 * The range includes block's suffix and prefix, as well as block returned to
 * malloc's caller. Map considers two blocks to be equal if their address ranges
 * intersect in any part. Allocation descriptor maps are instantiated one per
 * each process running on the guest system.
 */

#ifndef QEMU_MEMCHECK_MEMCHECK_MALLOC_MAP_H
#define QEMU_MEMCHECK_MEMCHECK_MALLOC_MAP_H

#include "sys-tree.h"
#include "memcheck_common.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Allocation descriptors map. */
typedef struct AllocMap {
    /* Head of the map. */
    struct AllocMapEntry*   rbh_root;
} AllocMap;

// =============================================================================
// Map API
// =============================================================================

/* Initializes allocation descriptors map.
 * Param:
 *  map - Allocation descriptors map to initialize.
 */
void allocmap_init(AllocMap* map);

/* Inserts new (or replaces existing) entry in the allocation descriptors map.
 * Insertion, or replacement is controlled by the value, passed to this routine
 * with 'replaced' parameter. If this parameter is NULL, insertion will fail if
 * a matching entry already exists in the map. If 'replaced' parameter is not
 * NULL, and a matching entry exists in the map, content of the existing entry
 * will be copied to the descriptor, addressed by 'replace' parameter, existing
 * entry will be removed from the map, and new entry will be inserted.
 * Param:
 *  map - Allocation descriptors map where to insert new, or replace existing
 *      entry.
 *  desc - Allocation descriptor to insert to the map.
 *  replaced - If not NULL, upon return from this routine contains descriptor
 *      that has been replaced in the map with the new entry. Note that if this
 *      routine returns with value other than RBT_MAP_RESULT_ENTRY_REPLACED,
 *      content of the 'replaced' buffer is not defined, as no replacement has
 *      actually occurred.
 * Return
 *  See RBTMapResult for the return codes.
 */
RBTMapResult allocmap_insert(AllocMap* map,
                             const MallocDescEx* desc,
                             MallocDescEx* replaced);

/* Finds an entry in the allocation descriptors map that matches the given
 * address.
 * Param:
 *  map - Allocation descriptors map where to search for an entry.
 *  address - Virtual address in the guest's user space to find matching
 *      entry for. Entry matches the address, if address is contained within
 *      allocated memory range (including guarding areas), as defined by the
 *      memory allocation descriptor for that entry.
 *  block_size - Size of the block, beginning with 'address'.
 * Return:
 *  Pointer to the allocation descriptor found in a map entry, or NULL if no
 *  matching entry has been found in the map.
 */
MallocDescEx* allocmap_find(const AllocMap* map,
                            target_ulong address,
                            uint32_t block_size);

/* Pulls (finds and removes) an entry from the allocation descriptors map that
 * matches the given address.
 * Param:
 *  map - Allocation descriptors map where to search for an entry.
 *  address - Virtual address in the guest's user space to find matching
 *      entry for. Entry matches the address, if address is contained within
 *      allocated memory range (including guarding areas), as defined by the
 *      memory allocation descriptor for that entry.
 *  pulled - Upon successful return contains allocation descriptor data pulled
 *      from the map.
 * Return:
 *  Zero if an allocation descriptor that matches the given address has
 *  been pulled, or 1 if no matching entry has been found in the map.
 */
int allocmap_pull(AllocMap* map, target_ulong address, MallocDescEx* pulled);

/* Pulls (removes) an entry from the head of the allocation descriptors map.
 * Param:
 *  map - Allocation descriptors map where to pull an entry from.
 *  pulled - Upon successful return contains allocation descriptor data pulled
 *      from the head of the map.
 * Return:
 *  Zero if an allocation descriptor has been pulled from the head of the map,
 *  or 1 if map is empty.
 */
int allocmap_pull_first(AllocMap* map, MallocDescEx* pulled);

/* Copies content of one memory allocation descriptors map to another.
 * Param:
 *  to - Map where to copy entries to.
 *  from - Map where to copy entries from.
 *  set_flags - Flags that should be set in the copied entry's 'flags' field.
 *  celar_flags - Flags that should be cleared in the copied entry's 'flags'
 *  field.
 * Return:
 *  Zero on success, or -1 on error.
 */
int allocmap_copy(AllocMap* to,
                  const AllocMap* from,
                  uint32_t set_flags,
                  uint32_t clear_flags);

/* Empties the map.
 * Param:
 *  map - Map to empty.
 * Return:
 *  Number of entries removed from the map.
 */
int allocmap_empty(AllocMap* map);

#ifdef __cplusplus
};  /* end of extern "C" */
#endif

#endif  // QEMU_MEMCHECK_MEMCHECK_MALLOC_MAP_H