Debian Codegraph @

src/utils/read_source.c

raw
1 #define _GNU_SOURCE
2 #include "utils/read_source.h"
3 #include "utils/error.h"
4 #include "utils/logger.h"
5 #include <assert.h>
6 #include <errno.h>
7 #include <linux/prctl.h>
8 #include <linux/bpf_common.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/prctl.h>
16 #include <unistd.h>
17
18 #define GROW_LEN 0x4000
19 #define MAX_LINE_LEN 0x180
20 #define MAX_FILE_LEN 0x400000 // 4MiB
21
22 typedef enum
23 {
24 UNKNOWN,
25 UNIX,
26 WINDOWS,
27 MACOS,
28 } file_type_t;
29 static file_type_t file_type = UNKNOWN;
30
31 static char *source = NULL;
32 static uint32_t current = 0;
33 static uint32_t map_len = 0;
34
35 static void
36 clear_color (char *text, uint32_t line_len)
37 {
38 char *cursor = memchr (text, '\x1b', line_len);
39 if (!cursor)
40 return;
41 char *top = text + line_len; // excluding
42 char *colorstart = NULL;
43 char *clear = cursor;
44
45 for (; cursor < top; cursor++)
46 {
47 if (!colorstart && *cursor != '\x1b')
48 *clear++ = *cursor;
49 else if (!colorstart && *cursor == '\x1b')
50 colorstart = cursor;
51 else if (colorstart && *cursor == 'm')
52 colorstart = NULL;
53 // else skip
54 }
55 memset (clear, '\0', top - clear);
56 }
57
58 static char
59 detect_file_type (void)
60 {
61 char lf = '\n';
62 char *line_break = memchr (source, lf, current);
63 if (!line_break)
64 {
65 if (memchr (source, '\r', current)) // perhaps the file is from macos?
66 {
67 file_type = MACOS;
68 return '\r';
69 }
70 else
71 error ("%s", M_FOUND_SUS_NO_LF);
72 }
73 else
74 {
75 if (line_break != source && *(line_break - 1) == '\r')
76 file_type = WINDOWS;
77 else
78 file_type = UNIX;
79 }
80 return '\n';
81 }
82
83 static unsigned
84 process_source (void)
85 {
86 register char lf = detect_file_type ();
87
88 char *line_break;
89 char *line_start = source;
90 char *top = source + current;
91 uint32_t line_nr = 1;
92 while (true)
93 {
94 if (line_start + MAX_LINE_LEN <= top)
95 {
96 line_break = memchr (line_start, lf, MAX_LINE_LEN);
97 if (!line_break)
98 error (M_FOUND_SUS_LINE, line_nr, MAX_LINE_LEN);
99 }
100 else
101 {
102 // the rest space is less than MAX_LINE_LEN
103 line_break = memchr (line_start, lf, top - line_start);
104 if (!line_break)
105 break;
106 }
107
108 if (file_type == WINDOWS)
109 memcpy (line_break - 1, "\n", 2);
110 else
111 *line_break = '\n';
112
113 clear_color (line_start, line_break - line_start + 1);
114
115 line_nr++;
116 line_start = line_break + 1;
117 }
118
119 if (line_nr >= 4 * BPF_MAXINSNS)
120 error ("%s", M_LINES_TOO_MANY);
121 if (top == line_start)
122 return line_nr;
123 clear_color (line_start, top - line_start + 1);
124 return line_nr + 1;
125 }
126
127 static void
128 init_map (void)
129 {
130 source = mmap (NULL, GROW_LEN, PROT_READ | PROT_WRITE,
131 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
132 if (source == MAP_FAILED)
133 error ("mmap: %s", strerror (errno));
134
135 map_len = GROW_LEN;
136 }
137
138 static void
139 increase_map (void)
140 {
141 if (source == NULL)
142 {
143 init_map ();
144 return;
145 }
146
147 if (current + GROW_LEN <= map_len)
148 return;
149
150 source = mremap (source, map_len, map_len + GROW_LEN, MREMAP_MAYMOVE);
151 if (source == MAP_FAILED)
152 error ("mremap: %s", strerror (errno));
153
154 map_len += GROW_LEN;
155 }
156
157 unsigned
158 init_source (FILE *read_fp)
159 {
160 uint32_t read_len = 0;
161 int fd = fileno (read_fp); // give compiler some hint
162
163 do
164 {
165 increase_map ();
166 read_len = read (fd, source + current, GROW_LEN);
167 if (read_len == (uint32_t)-1)
168 error (M_READ_FAIL, strerror (errno));
169 current += read_len;
170 if (current > MAX_FILE_LEN)
171 error ("%s", M_FILE_TOO_LARGE);
172 }
173 while (read_len > 0); // reading via char device may get less than GROW_LEN
174
175 const char *found0;
176 if ((found0 = memchr (source, '\0', current)))
177 error (M_FOUND_SUS_ZERO, (unsigned long)(found0 - source));
178
179 if (current % GROW_LEN == 0 || current % GROW_LEN > GROW_LEN - 0x20)
180 increase_map (); // scanner expect a trailing \n, which may increase map
181
182 #ifdef PR_SET_VMA
183 prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, source, map_len, "asm source");
184 #endif
185
186 return process_source ();
187 }
188
189 void
190 free_source (void)
191 {
192 munmap (source, map_len);
193 }
194
195 char *
196 next_line (void)
197 {
198 static uint32_t cursor = 0;
199 if (cursor >= current)
200 return NULL;
201
202 char *read_ptr = source + cursor;
203
204 char *line_break = memchr (read_ptr, '\n', current - cursor);
205 if (!line_break)
206 {
207 // meet eof
208 source[current] = '\n';
209 cursor = current;
210 return read_ptr;
211 }
212
213 char *top = source + current; // give compiler some hint
214 do
215 line_break++;
216 while (line_break < top && (*line_break == '\0'));
217 cursor = line_break - source;
218
219 return read_ptr;
220 }