blob: 81dcb06cfbf50bc88f3b3d1f3204ca44ad11b82a [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_allocator:
Jamie Garside558e1442020-03-27 17:05:55 +00002
Chenghan Zhou091e3122020-08-03 17:52:34 -04003------------
4pw_allocator
5------------
Jamie Garside558e1442020-03-27 17:05:55 +00006
7This module provides various building blocks
8for a dynamic allocator. This is composed of the following parts:
9
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080010- ``block``: An implementation of a linked list of memory blocks, supporting
11 splitting and merging of blocks.
12- ``freelist``: A freelist, suitable for fast lookups of available memory chunks
13 (i.e. ``block`` s).
Jamie Garside558e1442020-03-27 17:05:55 +000014
Chenghan Zhouea0f7ad2020-07-29 18:20:37 -040015Heap Integrity Check
16====================
Rob Mohrde9cc1b2021-05-21 15:58:44 -070017The ``Block`` class provides two check functions:
Chenghan Zhouea0f7ad2020-07-29 18:20:37 -040018
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080019- ``bool Block::IsValid()``: Returns ``true`` is the given block is valid and
20 ``false`` otherwise.
21- ``void Block::CrashIfInvalid()``: Crash the program and output the reason why
22 the check fails using ``PW_DCHECK``.
Chenghan Zhouea0f7ad2020-07-29 18:20:37 -040023
24Heap Poisoning
25==============
26
27By default, this module disables heap poisoning since it requires extra space.
28User can enable heap poisoning by enabling the ``pw_allocator_POISON_HEAP``
29build arg.
30
31.. code:: sh
32
33 $ gn args out
34 # Modify and save the args file to use heap poison.
35 pw_allocator_POISON_HEAP = true
36
37When heap poisoning is enabled, ``pw_allocator`` will add ``sizeof(void*)``
38bytes before and after the usable space of each ``Block``, and paint the space
Rob Mohrde9cc1b2021-05-21 15:58:44 -070039with a hard-coded randomized pattern. During each check, ``pw_allocator``
Chenghan Zhouea0f7ad2020-07-29 18:20:37 -040040will check if the painted space still remains the pattern, and return ``false``
41if the pattern is damaged.
42
Chenghan Zhou091e3122020-08-03 17:52:34 -040043Heap Visualizer
44===============
45
46Functionality
47-------------
48
49``pw_allocator`` supplies a pw command ``pw heap-viewer`` to help visualize
50the state of the heap at the end of a dump file. The heap is represented by
51ASCII characters, where each character represents 4 bytes in the heap.
52
53.. image:: doc_resources/pw_allocator_heap_visualizer_demo.png
54
55Usage
56-----
57
58The heap visualizer can be launched from a shell using the Pigweed environment.
59
60.. code:: sh
61
62 $ pw heap-viewer --dump-file <directory of dump file> --heap-low-address
63 <hex address of heap lower address> --heap-high-address <hex address of heap
64 lower address> [options]
65
66The required arguments are:
67
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080068- ``--dump-file`` is the path of a file that contains ``malloc/free``
69 information. Each line in the dump file represents a ``malloc/free`` call.
70 ``malloc`` is represented as ``m <size> <memory address>`` and ``free`` is
71 represented as ``f <memory address>``. For example, a dump file should look
72 like:
Chenghan Zhou091e3122020-08-03 17:52:34 -040073
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080074 .. code:: sh
Chenghan Zhou091e3122020-08-03 17:52:34 -040075
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080076 m 20 0x20004450 # malloc 20 bytes, the pointer is 0x20004450
77 m 8 0x2000447c # malloc 8 bytes, the pointer is 0x2000447c
78 f 0x2000447c # free the pointer at 0x2000447c
79 ...
Chenghan Zhou091e3122020-08-03 17:52:34 -040080
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080081 Any line not formatted as the above will be ignored.
Chenghan Zhou091e3122020-08-03 17:52:34 -040082
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080083- ``--heap-low-address`` is the start of the heap. For example:
Chenghan Zhou091e3122020-08-03 17:52:34 -040084
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080085 .. code:: sh
Chenghan Zhou091e3122020-08-03 17:52:34 -040086
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080087 --heap-low-address 0x20004440
Chenghan Zhou091e3122020-08-03 17:52:34 -040088
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080089- ``--heap-high-address`` is the end of the heap. For example:
Chenghan Zhou091e3122020-08-03 17:52:34 -040090
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080091 .. code:: sh
Chenghan Zhou091e3122020-08-03 17:52:34 -040092
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080093 --heap-high-address 0x20006040
Chenghan Zhou091e3122020-08-03 17:52:34 -040094
95Options include the following:
96
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -080097- ``--poison-enable``: If heap poisoning is enabled during the
98 allocation or not. The value is ``False`` if the option is not specified and
99 ``True`` otherwise.
Chenghan Zhou091e3122020-08-03 17:52:34 -0400100
Anthony DiGirolamo46ae10a2021-11-30 15:57:02 -0800101- ``--pointer-size <integer of pointer size>``: The size of a pointer on the
102 machine where ``malloc/free`` is called. The default value is ``4``.
Chenghan Zhou091e3122020-08-03 17:52:34 -0400103
Jamie Garside558e1442020-03-27 17:05:55 +0000104Note, this module, and its documentation, is currently incomplete and
105experimental.