70696c0161
we had incorrect usage of setstackmark()/popstackmark() There was an ancient idiom (imported from CSRG in 1993) where code can do: setstackmark(&smark); loop until whatever condition { /* do lots of code */ popstackmark(&smark); } popstackmark(&smark); The 1st (inner) popstackmark() resets the stack, conserving memory, The 2nd one is needed just in case the "whatever condition" was never true, and the first one was never executed. This is (was) safe as all popstackmark() did was reset the stack. That could be done over and over again with no harm. That is, until 2000 when a fix from FreeBSD for another problem was imported. That connected all the stack marks as a list (so they can be located). That caused the problem, as the idiom was not changed, now there is this list of marks, and popstackmark() was removing an entry. It rarely (never?) caused any problems as the idiom was rarely used (the shell used to do loops like above, mostly, without the inner popstackmark()). Further, the stack mark list is only ever used when a memory block is realloc'd. That is, until last weekend - with the recent set of changes. Part of that copied code from FreeBSD introduced the idiom above into more functions - functions used much more, and with a greater possibility of stack marks being set on blocks that are realloc'd and so cause the problem. In the FreeBSD code, they changed the idiom, and always do a setstackmark() immediately after the inner popstackmark(). But not for reasons related to a list of stack marks, as in the intervening period, FreeBSD deleted that, but for another reason. We do not have their issue, and I did not believe that their updated idiom was needed (I did some analysis of exactly this issue - just missed the important part!), and just continued using the old one. Hence Patrick's core dump.... The solution used here is to split popstackmark() into 2 halves, popstackmark() continues to do what it has (recently) done, but is now implemented as a call of (a new func) rststackmark() which does all the original work of popstackmark - but not removing the entry from the stack mark list (which remains in popstackmark()). Then in the idiom above, the inner popstackmark() turns into a call of rststackmark() so the stack is reset, but the stack mark list is unchanged. Tail recursion elimination makes this essentially free.
80 lines
3.0 KiB
C
80 lines
3.0 KiB
C
/* $NetBSD: memalloc.h,v 1.18 2018/08/22 20:08:54 kre Exp $ */
|
|
|
|
/*-
|
|
* Copyright (c) 1991, 1993
|
|
* The Regents of the University of California. All rights reserved.
|
|
*
|
|
* This code is derived from software contributed to Berkeley by
|
|
* Kenneth Almquist.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
* @(#)memalloc.h 8.2 (Berkeley) 5/4/95
|
|
*/
|
|
|
|
struct stackmark {
|
|
struct stack_block *stackp;
|
|
char *stacknxt;
|
|
int stacknleft;
|
|
int sstrnleft;
|
|
struct stackmark *marknext;
|
|
};
|
|
|
|
|
|
extern char *stacknxt;
|
|
extern int stacknleft;
|
|
extern int sstrnleft;
|
|
extern int herefd;
|
|
|
|
pointer ckmalloc(size_t);
|
|
pointer ckrealloc(pointer, int);
|
|
char *savestr(const char *);
|
|
pointer stalloc(int);
|
|
void stunalloc(pointer);
|
|
void setstackmark(struct stackmark *);
|
|
void popstackmark(struct stackmark *);
|
|
void rststackmark(struct stackmark *);
|
|
void growstackblock(void);
|
|
void grabstackblock(int);
|
|
char *growstackstr(void);
|
|
char *makestrspace(void);
|
|
void ungrabstackstr(char *, char *);
|
|
|
|
|
|
|
|
#define stackblock() stacknxt
|
|
#define stackblocksize() stacknleft
|
|
#define STARTSTACKSTR(p) p = stackblock(), sstrnleft = stackblocksize()
|
|
#define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c)))
|
|
#define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(); }
|
|
#define USTPUTC(c, p) (--sstrnleft, *p++ = (c))
|
|
#define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), sstrnleft++, *p = '\0') : (*p = '\0'))
|
|
#define STUNPUTC(p) (++sstrnleft, --p)
|
|
#define STTOPC(p) p[-1]
|
|
#define STADJUST(amount, p) (p += (amount), sstrnleft -= (amount))
|
|
#define grabstackstr(p) stalloc((p) - stackblock())
|
|
|
|
#define ckfree(p) free((pointer)(p))
|