[RFC,v4,2/3] grep: make PCRE2 aware of custom allocator
diff mbox series

Message ID 20190807213945.10464-3-carenas@gmail.com
State New
Headers show
Series
  • grep: no leaks or crashes (windows testing needed)
Related show

Commit Message

Carlo Arenas Aug. 7, 2019, 9:39 p.m. UTC
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
USE_NED_ALLOCATOR.  The problem was made visible when an attempt to
avoid a leak in a data structure that is created by the library was
passed to NED's free for disposal triggering a segfault in Windows.

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,log} 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 (currently only
relevant when using both NED and PCRE2)

Move the logic to decide if a general context will be needed to an
earlier phase so it will only be done once per pattern (instead of
at least once per worker thread) avoiding then the need for locking.

This change does the minimum change required to hopefully solve the
crash, with the rest of the users of it added later.

Helped-by: René Scharfe <l.s.r@web.de>
Reported-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
V4:
* use xmalloc instead as suggested by René and Junio
* "fix" for regression in t7816 as reported by René

 builtin/grep.c |  1 +
 builtin/log.c  |  1 +
 grep.c         | 62 ++++++++++++++++++++++++++++++++++++++++++++------
 grep.h         |  1 +
 4 files changed, 58 insertions(+), 7 deletions(-)

Comments

Junio C Hamano Aug. 7, 2019, 10:28 p.m. UTC | #1
Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:

As we already have such an ifdef block here...

> +#ifdef USE_LIBPCRE2
> +static pcre2_general_context *pcre2_global_context;
> +
> +#ifdef USE_NED_ALLOCATOR
> +static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
> +{
> +	return xmalloc(size); /* will use nedalloc underneath */
> +}
> +
> +static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
> +{
> +	return free(pointer);
> +}
> +#endif
> +#endif

... perhaps an ugly ifdef block like this one ...

> +static struct grep_pat *create_grep_pat(struct grep_opt *opt,
> +					const char *pat, size_t patlen,
>  					const char *origin, int no,
>  					enum grep_pat_token t,
>  					enum grep_header_field field)
>  {
>  	struct grep_pat *p = xcalloc(1, sizeof(*p));
> +
> +#if defined(USE_LIBPCRE2) && defined(USE_NED_ALLOCATOR)
> +	/* BUG: this is technically not needed if we will do UTF matching
> +	 *      but UTF locale detection is currently broken  */
> +	/* SMELL: opt shouldn't be needed at this level but it is used
> +	 *        here to keep the way we were detecting the need for
> +	 *        the chartable consistent and until it can be refactored
> +	 *        properly.  the NULL check is needed as a workaround
> +	 *        for segfaulting in t7810.102 and should also go */
> +	/* TODO: has_non_ascii doesn't support NUL in pattern */
> +	if (!pcre2_global_context && opt && opt->ignore_case &&
> +		has_non_ascii(pat))
> +		pcre2_global_context = pcre2_general_context_create(
> +					pcre2_malloc, pcre2_free, NULL);
> +#endif
> +

... can be abstracted away by *not* having the #if/#endif here and
instead have a line that looks like this:

	setup_pcre2_as_needed(opt, pat);

which implementation can be prepared near the top, close to where
pcre2_malloc/free are defined (or not) above, i.e.

	#if defined(USE_LIBPCRE2) && defined(USE_NED_ALLOCATOR)
	static void setup_pcre2_as_needed(struct grep_opt *opt,	const char *pat)
	{
		... the above one ...
	}
	#else
	#define setup_pcre2_as_needed(ignore1, ignore2) /* nothing */
	#endif

The conditional code in grep_destroy() can be eliminated in a
similar fashion, i.e.

	void grep_destroy(void)
	{
		cleanup_pcre2_as_needed();
	}

with

	#ifdef USE_LIBPCRE2
	static void cleanup_pcre2_as_needed(void)
	{
		pcre2_general_context_free(pcre2_global_context);
	}
	#else
	#define cleanup_pcre2_as_needed() /* nothing */
	#endif

near the top (the beneral idea is to "call" a helper function whose
name describes what the call is for in a more general terms, and let
only the implementation details be hidden inside ifdef blocks).

Also, 

	/* 
	 * our multi-line comments are supposed to be formatted like
	 * this, with opening and closing slash-asterisk and asterisk-slash
	 * on their own lines.
	 */

Patch
diff mbox series

diff --git a/builtin/grep.c b/builtin/grep.c
index 560051784e..e49c20df60 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -1145,5 +1145,6 @@  int cmd_grep(int argc, const char **argv, const char *prefix)
 		run_pager(&opt, prefix);
 	clear_pathspec(&pathspec);
 	free_grep_patterns(&opt);
+	grep_destroy();
 	return !hit;
 }
diff --git a/builtin/log.c b/builtin/log.c
index 1cf9e37736..139b8770e7 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -2146,6 +2146,7 @@  int cmd_cherry(int argc, const char **argv, const char *prefix)
 		list = list->next;
 	}
 
+	grep_destroy();
 	free_patch_ids(&ids);
 	return 0;
 }
diff --git a/grep.c b/grep.c
index 0154998695..8ee982e2e8 100644
--- a/grep.c
+++ b/grep.c
@@ -16,6 +16,22 @@  static int grep_source_is_binary(struct grep_source *gs,
 
 static struct grep_opt grep_defaults;
 
+#ifdef USE_LIBPCRE2
+static pcre2_general_context *pcre2_global_context;
+
+#ifdef USE_NED_ALLOCATOR
+static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
+{
+	return xmalloc(size); /* will use nedalloc underneath */
+}
+
+static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
+{
+	return free(pointer);
+}
+#endif
+#endif
+
 static const char *color_grep_slots[] = {
 	[GREP_COLOR_CONTEXT]	    = "context",
 	[GREP_COLOR_FILENAME]	    = "filename",
@@ -153,6 +169,7 @@  int grep_config(const char *var, const char *value, void *cb)
  *
  * If using PCRE make sure that the library is configured
  * to use the right allocator (ex: NED)
+ * if any object is created it should be cleaned up in grep_destroy()
  */
 void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
 {
@@ -188,6 +205,13 @@  void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
 		color_set(opt->colors[i], def->colors[i]);
 }
 
+void grep_destroy(void)
+{
+#ifdef USE_LIBPCRE2
+	pcre2_general_context_free(pcre2_global_context);
+#endif
+}
+
 static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
 {
 	/*
@@ -254,12 +278,29 @@  void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_o
 		grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
 }
 
-static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
+static struct grep_pat *create_grep_pat(struct grep_opt *opt,
+					const char *pat, size_t patlen,
 					const char *origin, int no,
 					enum grep_pat_token t,
 					enum grep_header_field field)
 {
 	struct grep_pat *p = xcalloc(1, sizeof(*p));
+
+#if defined(USE_LIBPCRE2) && defined(USE_NED_ALLOCATOR)
+	/* BUG: this is technically not needed if we will do UTF matching
+	 *      but UTF locale detection is currently broken  */
+	/* SMELL: opt shouldn't be needed at this level but it is used
+	 *        here to keep the way we were detecting the need for
+	 *        the chartable consistent and until it can be refactored
+	 *        properly.  the NULL check is needed as a workaround
+	 *        for segfaulting in t7810.102 and should also go */
+	/* TODO: has_non_ascii doesn't support NUL in pattern */
+	if (!pcre2_global_context && opt && opt->ignore_case &&
+		has_non_ascii(pat))
+		pcre2_global_context = pcre2_general_context_create(
+					pcre2_malloc, pcre2_free, NULL);
+#endif
+
 	p->pattern = xmemdupz(pat, patlen);
 	p->patternlen = patlen;
 	p->origin = origin;
@@ -291,8 +332,10 @@  static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
 			}
 			if (!nl)
 				break;
-			new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
-						  p->no, p->token, p->field);
+
+			new_pat = create_grep_pat(NULL, nl + 1, len - 1,
+						p->origin, p->no, p->token,
+						p->field);
 			new_pat->next = p->next;
 			if (!p->next)
 				*tail = &new_pat->next;
@@ -309,8 +352,8 @@  static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
 void append_header_grep_pattern(struct grep_opt *opt,
 				enum grep_header_field field, const char *pat)
 {
-	struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
-					     GREP_PATTERN_HEAD, field);
+	struct grep_pat *p = create_grep_pat(opt, pat, strlen(pat), "header",
+					 0, GREP_PATTERN_HEAD, field);
 	if (field == GREP_HEADER_REFLOG)
 		opt->use_reflog_filter = 1;
 	do_append_grep_pat(&opt->header_tail, p);
@@ -325,7 +368,7 @@  void append_grep_pattern(struct grep_opt *opt, const char *pat,
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
 		     const char *origin, int no, enum grep_pat_token t)
 {
-	struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
+	struct grep_pat *p = create_grep_pat(opt, pat, patlen, origin, no, t, 0);
 	do_append_grep_pat(&opt->pattern_tail, p);
 }
 
@@ -507,9 +550,14 @@  static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 
 	p->pcre2_compile_context = NULL;
 
+	/* pcre2_global_context is initialized in append_grep_pattern */
 	if (opt->ignore_case) {
 		if (has_non_ascii(p->pattern)) {
-			character_tables = pcre2_maketables(NULL);
+#ifdef USE_NED_ALLOCATOR
+			if (!pcre2_global_context)
+				BUG("pcre2_global_context uninitialized");
+#endif
+			character_tables = pcre2_maketables(pcre2_global_context);
 			p->pcre2_compile_context = pcre2_compile_context_create(NULL);
 			pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
 		}
diff --git a/grep.h b/grep.h
index 1875880f37..526c2db9ef 100644
--- a/grep.h
+++ b/grep.h
@@ -189,6 +189,7 @@  struct grep_opt {
 void init_grep_defaults(struct repository *);
 int grep_config(const char *var, const char *value, void *);
 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
+void grep_destroy(void);
 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);