Line data Source code
1 : /* Support for scanning buffers for a NUL terminator (for C-style strings).
2 : Copyright (C) 2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_ANALYZER_STRING_SCAN_H
22 : #define GCC_ANALYZER_STRING_SCAN_H
23 :
24 : #include "analyzer/concrete-ranges.h"
25 :
26 : namespace ana {
27 :
28 : /* A class representing the result of a scan through a bit_vector,
29 : bit_contents, or constant at some start offset looking for the first of:
30 : - NUL terminator (if any), or
31 : - an access that will trigger a diagnostic (if any), e.g. uninitialized, or
32 : - exhausting the domain of the input
33 :
34 : Records what happened, and the offset at which it happened. The
35 : offset is relative to the start of the buffer, rather than the given
36 : start offset, i.e. they are both in the same "coordinate system".
37 : For exhausted data, this will be the index of one past the last
38 : valid element, i.e. the size of the bit_vector/constant. */
39 :
40 11821 : struct string_scan
41 : {
42 : using byte_offset_type = concrete_byte_offset;
43 :
44 : /* A enum to discriminate the different kinds of result. */
45 : enum class state
46 : {
47 : /* Found a NUL terminator.
48 : Result offset is that of the NUL (which could be that
49 : of the start_byte_offset).
50 : . +-----+-----+-----+-----+-----+-----+
51 : . | 'a' | 'b' | 'c' | 'd' | NUL | NUL |
52 : . +-----+-----+-----+-----+-----+-----+
53 : . ^ ^
54 : . | result offset
55 : . start_byte_offset. */
56 : found_nul,
57 :
58 : /* Found a poisoned byte (e.g. uninitialized)
59 : Result offset is that of the problematic byte.
60 : . +-----+-----+-----+-----+------+-----+
61 : . | 'a' | 'b' | 'c' | 'd' |uninit| NUL |
62 : . +-----+-----+-----+-----+------+-----+
63 : . ^ ^
64 : . | result offset
65 : . start_byte_offset. */
66 : found_poison,
67 :
68 : /* Didn't find any terminators or problems.
69 : Result offset is one past the end of the domain of the bit_vector
70 : or constant, which is its size in bytes.
71 : . +-----+-----+-----+-----+-----+-----+......+
72 : . | | | | | | | ..
73 : . +-0---+-----+-----+-----+-----+-----+.size.+
74 : . ^ ^
75 : . start_byte_offset result offset. */
76 : beyond_domain,
77 :
78 : /* This object can't be scanned for some reason.
79 : The result offset is where the problem occurred
80 : (often the start byte offset). */
81 : cannot_scan
82 : };
83 :
84 11878 : string_scan (byte_offset_type byte_offset,
85 : enum state state)
86 3790 : : m_byte_offset (byte_offset),
87 11878 : m_state (state),
88 3638 : m_poison_kind (poison_kind::other)
89 : {
90 : }
91 :
92 457 : string_scan (byte_offset_type byte_offset,
93 : enum poison_kind poison_kind_)
94 914 : : m_byte_offset (byte_offset),
95 457 : m_state (state::found_poison),
96 457 : m_poison_kind (poison_kind_)
97 : {
98 : }
99 :
100 : static string_scan
101 4203 : found_nul (byte_offset_type byte_offset)
102 : {
103 4203 : return string_scan (byte_offset, state::found_nul);
104 : }
105 : static string_scan
106 457 : found_poison (byte_offset_type byte_offset,
107 : enum poison_kind poison_kind_)
108 : {
109 457 : return string_scan (byte_offset, poison_kind_);
110 : }
111 : static string_scan
112 979 : beyond_domain (byte_offset_type byte_offset)
113 : {
114 : // byte offset == offset of 1 past the end of the buffer == buffer size
115 979 : return string_scan (byte_offset, state::beyond_domain);
116 : }
117 : static string_scan
118 3210 : cannot_scan (byte_offset_type byte_offset)
119 : {
120 3210 : return string_scan (byte_offset, state::cannot_scan);
121 : }
122 :
123 : bool
124 760 : operator== (const string_scan &other) const
125 : {
126 760 : return (m_byte_offset == other.m_byte_offset
127 760 : && m_state == other.m_state);
128 : }
129 :
130 : void dump_to_pp (pretty_printer *pp) const;
131 : void dump () const;
132 :
133 : static const char *
134 : state_to_string (enum state s);
135 :
136 : byte_offset_type m_byte_offset;
137 : enum state m_state;
138 : enum poison_kind m_poison_kind;
139 : };
140 :
141 : } // namespace ana
142 :
143 : #endif /* GCC_ANALYZER_STRING_SCAN_H */
|