Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Aug 2009 21:20:02 GMT
From:      dfilter@FreeBSD.ORG (dfilter service)
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/137640: commit references a PR
Message-ID:  <200908232120.n7NLK2sD075852@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/137640; it has been noted by GNATS.

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/137640: commit references a PR
Date: Sun, 23 Aug 2009 21:10:09 +0000 (UTC)

 Author: jilles
 Date: Sun Aug 23 21:09:46 2009
 New Revision: 196483
 URL: http://svn.freebsd.org/changeset/base/196483
 
 Log:
   sh: Fix crash when undefining or redefining a currently executing function.
   
   Add a reference count to function definitions.
   Memory may leak if multiple SIGINTs arrive in interactive mode,
   this will be fixed later by changing SIGINT handling.
   
   PR:		bin/137640
 
 Added:
   head/tools/regression/bin/sh/execution/func1.0   (contents, props changed)
 Modified:
   head/bin/sh/eval.c
   head/bin/sh/exec.c
   head/bin/sh/exec.h
   head/bin/sh/mknodes.c
   head/bin/sh/nodes.c.pat
 
 Modified: head/bin/sh/eval.c
 ==============================================================================
 --- head/bin/sh/eval.c	Sun Aug 23 21:00:21 2009	(r196482)
 +++ head/bin/sh/eval.c	Sun Aug 23 21:09:46 2009	(r196483)
 @@ -785,6 +785,7 @@ evalcommand(union node *cmd, int flags, 
  		INTOFF;
  		savelocalvars = localvars;
  		localvars = NULL;
 +		reffunc(cmdentry.u.func);
  		INTON;
  		savehandler = handler;
  		if (setjmp(jmploc.loc)) {
 @@ -794,6 +795,7 @@ evalcommand(union node *cmd, int flags, 
  				freeparam(&shellparam);
  				shellparam = saveparam;
  			}
 +			unreffunc(cmdentry.u.func);
  			poplocalvars();
  			localvars = savelocalvars;
  			handler = savehandler;
 @@ -805,11 +807,12 @@ evalcommand(union node *cmd, int flags, 
  		funcnest++;
  		exitstatus = oexitstatus;
  		if (flags & EV_TESTED)
 -			evaltree(cmdentry.u.func, EV_TESTED);
 +			evaltree(&cmdentry.u.func->n, EV_TESTED);
  		else
 -			evaltree(cmdentry.u.func, 0);
 +			evaltree(&cmdentry.u.func->n, 0);
  		funcnest--;
  		INTOFF;
 +		unreffunc(cmdentry.u.func);
  		poplocalvars();
  		localvars = savelocalvars;
  		freeparam(&shellparam);
 
 Modified: head/bin/sh/exec.c
 ==============================================================================
 --- head/bin/sh/exec.c	Sun Aug 23 21:00:21 2009	(r196482)
 +++ head/bin/sh/exec.c	Sun Aug 23 21:09:46 2009	(r196483)
 @@ -286,7 +286,7 @@ printentry(struct tblentry *cmdp, int ve
  		out1fmt("function %s", cmdp->cmdname);
  		if (verbose) {
  			INTOFF;
 -			name = commandtext(cmdp->param.func);
 +			name = commandtext(&cmdp->param.func->n);
  			out1c(' ');
  			out1str(name);
  			ckfree(name);
 @@ -583,7 +583,7 @@ deletefuncs(void)
  		while ((cmdp = *pp) != NULL) {
  			if (cmdp->cmdtype == CMDFUNCTION) {
  				*pp = cmdp->next;
 -				freefunc(cmdp->param.func);
 +				unreffunc(cmdp->param.func);
  				ckfree(cmdp);
  			} else {
  				pp = &cmdp->next;
 @@ -670,7 +670,7 @@ addcmdentry(char *name, struct cmdentry 
  	INTOFF;
  	cmdp = cmdlookup(name, 1);
  	if (cmdp->cmdtype == CMDFUNCTION) {
 -		freefunc(cmdp->param.func);
 +		unreffunc(cmdp->param.func);
  	}
  	cmdp->cmdtype = entry->cmdtype;
  	cmdp->param = entry->u;
 @@ -705,7 +705,7 @@ unsetfunc(char *name)
  	struct tblentry *cmdp;
  
  	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
 -		freefunc(cmdp->param.func);
 +		unreffunc(cmdp->param.func);
  		delete_cmd_entry();
  		return (0);
  	}
 
 Modified: head/bin/sh/exec.h
 ==============================================================================
 --- head/bin/sh/exec.h	Sun Aug 23 21:00:21 2009	(r196482)
 +++ head/bin/sh/exec.h	Sun Aug 23 21:09:46 2009	(r196483)
 @@ -46,11 +46,12 @@ enum {
  	TYPECMD_TYPE		/* type */
  };
  
 +union node;
  struct cmdentry {
  	int cmdtype;
  	union param {
  		int index;
 -		union node *func;
 +		struct funcdef *func;
  	} u;
  	int special;
  };
 
 Modified: head/bin/sh/mknodes.c
 ==============================================================================
 --- head/bin/sh/mknodes.c	Sun Aug 23 21:00:21 2009	(r196482)
 +++ head/bin/sh/mknodes.c	Sun Aug 23 21:09:46 2009	(r196483)
 @@ -248,8 +248,13 @@ output(char *file)
  	fputs("\tstruct nodelist *next;\n", hfile);
  	fputs("\tunion node *n;\n", hfile);
  	fputs("};\n\n\n", hfile);
 -	fputs("union node *copyfunc(union node *);\n", hfile);
 -	fputs("void freefunc(union node *);\n", hfile);
 +	fputs("struct funcdef {\n", hfile);
 +	fputs("\tunsigned int refcount;\n", hfile);
 +	fputs("\tunion node n;\n", hfile);
 +	fputs("};\n\n\n", hfile);
 +	fputs("struct funcdef *copyfunc(union node *);\n", hfile);
 +	fputs("void reffunc(struct funcdef *);\n", hfile);
 +	fputs("void unreffunc(struct funcdef *);\n", hfile);
  
  	fputs(writer, cfile);
  	while (fgets(line, sizeof line, patfile) != NULL) {
 
 Modified: head/bin/sh/nodes.c.pat
 ==============================================================================
 --- head/bin/sh/nodes.c.pat	Sun Aug 23 21:00:21 2009	(r196482)
 +++ head/bin/sh/nodes.c.pat	Sun Aug 23 21:09:46 2009	(r196483)
 @@ -35,6 +35,7 @@
  
  #include <sys/param.h>
  #include <stdlib.h>
 +#include <stddef.h>
  /*
   * Routine for dealing with parsed shell commands.
   */
 @@ -65,17 +66,22 @@ STATIC char *nodesavestr(char *);
   * Make a copy of a parse tree.
   */
  
 -union node *
 +struct funcdef *
  copyfunc(union node *n)
  {
 +	struct funcdef *fn;
 +
  	if (n == NULL)
  		return NULL;
 -	funcblocksize = 0;
 +	funcblocksize = offsetof(struct funcdef, n);
  	funcstringsize = 0;
  	calcsize(n);
 -	funcblock = ckmalloc(funcblocksize + funcstringsize);
 -	funcstring = (char *)funcblock + funcblocksize;
 -	return copynode(n);
 +	fn = ckmalloc(funcblocksize + funcstringsize);
 +	fn->refcount = 1;
 +	funcblock = (char *)fn + offsetof(struct funcdef, n);
 +	funcstring = (char *)fn + funcblocksize;
 +	copynode(n);
 +	return fn;
  }
  
  
 @@ -144,14 +150,25 @@ nodesavestr(char *s)
  }
  
  
 +void
 +reffunc(struct funcdef *fn)
 +{
 +	fn->refcount++;
 +}
 +
  
  /*
 - * Free a parse tree.
 + * Decrement the reference count of a function definition, freeing it
 + * if it falls to 0.
   */
  
  void
 -freefunc(union node *n)
 +unreffunc(struct funcdef *fn)
  {
 -	if (n)
 -		ckfree(n);
 +	if (fn) {
 +		fn->refcount--;
 +		if (fn->refcount > 0)
 +			return;
 +		ckfree(fn);
 +	}
  }
 
 Added: head/tools/regression/bin/sh/execution/func1.0
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ head/tools/regression/bin/sh/execution/func1.0	Sun Aug 23 21:09:46 2009	(r196483)
 @@ -0,0 +1,4 @@
 +# $FreeBSD$
 +
 +MALLOC_OPTIONS=J sh -c 'g() { g() { :; }; :; }; g' &&
 +MALLOC_OPTIONS=J sh -c 'g() { unset -f g; :; }; g'
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200908232120.n7NLK2sD075852>