From owner-svn-src-all@FreeBSD.ORG Sun Aug 23 21:09:48 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 50F44106568C; Sun, 23 Aug 2009 21:09:48 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 3F2638FC17; Sun, 23 Aug 2009 21:09:48 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n7NL9k4r072429; Sun, 23 Aug 2009 21:09:46 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n7NL9khk072422; Sun, 23 Aug 2009 21:09:46 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <200908232109.n7NL9khk072422@svn.freebsd.org> From: Jilles Tjoelker Date: Sun, 23 Aug 2009 21:09:46 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r196483 - in head: bin/sh tools/regression/bin/sh/execution X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Aug 2009 21:09:48 -0000 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 #include +#include /* * 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'