@matheusbsbs / matheus-bagliano / commits / 513f2b0bbd
Fork 0

grep: make PCRE2 aware of custom allocator

94da9193a6 (grep: add support for PCRE v2, 2017-06-01) didn't include a way to override the system allocator, and so it is incompatible with custom allocators (e.g. nedmalloc). This problem became obvious when we tried to plug a memory leak by `free()`ing a data structure allocated by PCRE2, triggering a segfault in Windows (where we use nedmalloc by default). PCRE2 requires the use of a general context to override the allocator and therefore, there is a lot more code needed than in PCRE1, including a couple of wrapper functions. Extend the grep API with a "destructor" that could be called to cleanup any objects that were created and used globally. Update `builtin/grep.c` to use that new API, but any other future users should make sure to have matching `grep_init()`/`grep_destroy()` calls if they are using the pattern matching functionality. Move some of the logic that was before done per thread (in the workers) into an earlier phase to avoid degrading performance, but as the use of PCRE2 with custom allocators is better understood it is expected more of its functions will be instructed to use the custom allocator as well as was done in the original code[1] this work was based on. [1] https://public-inbox.org/git/3397e6797f872aedd18c6d795f4976e1c579514b.1565005867.git.gitgitgadget@gmail.com/ Reported-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlo Marcelo Arenas Belón committed Oct 16, 2019 at 12:10 UTC 513f2b0bbd47015640723a10210c527839e8946a
3 files changed +35 -1
builtin/grep.c
+1
@@ -1145,5 +1145,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1145 run_pager(&opt, prefix);
1146 clear_pathspec(&pathspec);
1147 free_grep_patterns(&opt);
1148 + grep_destroy();
1149 return !hit;
1150 }
grep.c
+33 -1
@@ -16,6 +16,20 @@ static int grep_source_is_binary(struct grep_source *gs,
16
17 static struct grep_opt grep_defaults;
18
19 +#ifdef USE_LIBPCRE2
20 +static pcre2_general_context *pcre2_global_context;
21 +
22 +static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
23 +{
24 + return malloc(size);
25 +}
26 +
27 +static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
28 +{
29 + return free(pointer);
30 +}
31 +#endif
32 +
33 static const char *color_grep_slots[] = {
34 [GREP_COLOR_CONTEXT] = "context",
35 [GREP_COLOR_FILENAME] = "filename",
@@ -153,12 +167,20 @@ int grep_config(const char *var, const char *value, void *cb)
167 *
168 * If using PCRE, make sure that the library is configured
169 * to use the same allocator as Git (e.g. nedmalloc on Windows).
170 + *
171 + * Any allocated memory needs to be released in grep_destroy().
172 */
173 void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
174 {
175 struct grep_opt *def = &grep_defaults;
176 int i;
177
178 +#if defined(USE_LIBPCRE2)
179 + if (!pcre2_global_context)
180 + pcre2_global_context = pcre2_general_context_create(
181 + pcre2_malloc, pcre2_free, NULL);
182 +#endif
183 +
184 #ifdef USE_LIBPCRE1
185 pcre_malloc = malloc;
186 pcre_free = free;
@@ -186,6 +208,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
208 color_set(opt->colors[i], def->colors[i]);
209 }
210
211 +void grep_destroy(void)
212 +{
213 +#ifdef USE_LIBPCRE2
214 + pcre2_general_context_free(pcre2_global_context);
215 +#endif
216 +}
217 +
218 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
219 {
220 /*
@@ -505,9 +534,12 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
534
535 p->pcre2_compile_context = NULL;
536
537 + /* pcre2_global_context is initialized in append_grep_pattern */
538 if (opt->ignore_case) {
539 if (has_non_ascii(p->pattern)) {
510 - character_tables = pcre2_maketables(NULL);
540 + if (!pcre2_global_context)
541 + BUG("pcre2_global_context uninitialized");
542 + character_tables = pcre2_maketables(pcre2_global_context);
543 p->pcre2_compile_context = pcre2_compile_context_create(NULL);
544 pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
545 }
grep.h
+1
@@ -189,6 +189,7 @@ struct grep_opt {
189 void init_grep_defaults(struct repository *);
190 int grep_config(const char *var, const char *value, void *);
191 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
192 +void grep_destroy(void);
193 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
194
195 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);