Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14695

Re: Segfault on recursive trap/kill

Path csiph.com!goblin3!goblin.stu.neva.ru!panix!usenet.stanford.edu!not-for-mail
From Valentin Bajrami <valentin.bajrami@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Segfault on recursive trap/kill
Date Sun, 7 Oct 2018 08:52:25 +0200
Lines 183
Approved bug-bash@gnu.org
Message-ID <mailman.1785.1538895917.1284.bug-bash@gnu.org> (permalink)
References <8736tj3llu.fsf@gnu.org> <25389056-9fcf-1d31-36d8-13098769a43a@case.edu> <874ldy1vka.fsf@gnu.org> <20181006210450465282080@bob.proulx.com> <87y3baxpy4.fsf@gnu.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding quoted-printable
X-Trace usenet.stanford.edu 1538895918 13957 208.118.235.17 (7 Oct 2018 07:05:18 GMT)
X-Complaints-To action@cs.stanford.edu
Cc bug-bash@gnu.org
To Mike Gerwitz <mtg@gnu.org>
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=JdMH5u44BTSZ2JOREgow/wmYWw7ybmsxFCcmDpbLKkw=; b=udYMvtYbDm2HBMzNna/xuheS6pl8/Tpgox1PcvhMejC3i+TztjEpNPn4DjrJLul1ks n6pekElXLKQsd0W3CzvgEPsg/hA9mLdWQNdB8K3ovvdxEaeUMTv+khNxwBppzUuS9UJv VYV4q2eAXgbJoBql99XaFVNol4aLq34sqM58YsvhIq4RX+b+lURg1MdZM2LhfxJKQe4Y BR8xBbYf3VknwWkibMUAJ4m125clCzz+kYGDoPU/lu/d2i0OX2Ev/FDaipf/E6KvZB+d W1nS/AzOIi9yx7OVA4k9hTH+Xe7oA4y6vH2WOQu0HpzbA5JeoEFxG+evLv8mAQlEjmVU Ld4g==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=JdMH5u44BTSZ2JOREgow/wmYWw7ybmsxFCcmDpbLKkw=; b=H9xO9KMLbfw9egISWf2op9U2TID542/7/5uGx8KZwQkZKzqFZgRR699D8mSIW/636D JiXEIDUHGcIr1BQtTHZVP8dDAzDN4Y8L2jTdq2wjdupwDoK8fn1JvsaR+Fn6pIgz9hDV 7Hx+VXgkOazwIRQ9D9eQuHLehrMv/pDelX97IfHeV98FceSDBfSNaPLOe0nQAodK6ZTe ecNQYIlIMU8ol+NsRLjERYOT9aiX1NUi70IH1Ijj50iQ+7X36fy49yhhDT9Sp9bQl8Xv Pf4CC5HpYk50tMSHKP00rASU1QM4vDYoSDRZX7St/FdzFSRLwwgUero3qMQRNwBtWxGg W0qw==
X-Gm-Message-State ABuFfojwWG3Pg0UhGk9Zd7/fjzvQXFpPZSRMFAjgv7l116Uhg8Myl4fr 2IToSzjN3MuvKnvWIGVb2iE/YV5xjhg3p7bi7huqew==
X-Google-Smtp-Source ACcGV618uDtuNNKmKDYQC/5JvWwV92KnNtemdx2bZ724NRGtwdUtsRTyXDXqHr6Pfh4tICb7dMS9bauv3QhOit6LVbc=
X-Received by 2002:a02:a087:: with SMTP id g7-v6mr14224231jah.92.1538895158216; Sat, 06 Oct 2018 23:52:38 -0700 (PDT)
In-Reply-To <87y3baxpy4.fsf@gnu.org>
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 2607:f8b0:4864:20::12b
X-Content-Filtered-By Mailman/MimeDel 2.1.21
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14695

Show key headers only | View raw


Hi Mike,

As earlier expained, you are calling foo function recursively. To mitigate
this behaviour you simple set FUNCNEST=<N> foo() { foo; }; foo where N
denotes the number of nested functios to be called.


Op zo 7 okt. 2018 07:57 schreef Mike Gerwitz <mtg@gnu.org>:

> Hey, Bob!
>
> On Sat, Oct 06, 2018 at 22:44:17 -0600, Bob Proulx wrote:
> > Let me give the discussion this way and I think you will be
> > convinced. :-)
>
> Well, thanks for taking the time for such a long reply. :)
>
> > How is your example any different from a C program?  Or Perl, Python,
> > Ruby, and so forth?  All of those also allow infinite recursion and
> > the kernel will terminate them with a segfault.  Because all of those
> > also allow infinite recursion.  A program that executes an infinite
> > recursion would use infinite stack space.  But real machines have a
> > finite amount of stack available and therefore die when the stack is
> > exceeded.
>
> I expect this behavior when writing in C, certainly.  But in languages
> where the user does not deal with memory management, I'm used to a more
> graceful abort when the stack gets out of control.  A segfault means
> something to a C hacker.  It means very little to users who are
> unfamiliar with the concepts that you were describing.
>
> I don't have enough experience with Perl, Python, or Ruby to know how
> they handle stack issues.  But, out of interest, I gave it a try:
>
>   $ perl -e 'sub foo() { foo(); }; foo()'
>   Out of memory!
>
>   $ python <<< 'def foo():
>   >  foo()
>   >foo()' |& tail -n1
>   RuntimeError: maximum recursion depth exceeded
>
>   $ ruby -e 'def foo()
>   >   foo()
>   > end
>   > foo()'
>   -e:2: stack level too deep (SystemStackError)
>
> Some languages I'm more familiar with:
>
>   $ node -e '(function foo() { foo(); })()'
>   [eval]:1
>   (function foo() { foo(); })()
>                ^
>
>   RangeError: Maximum call stack size exceeded
>
>   $ php -r 'function foo() { foo(); } foo();'
>
>   Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to
>   allocate 262144 bytes) in Command line code on line 1
>
>   $ guile -e '(let x () (+ (x)))'
>   allocate_stack failed: Cannot allocate memory
>   Warning: Unwind-only `stack-overflow' exception; skipping pre-unwind
> handler.
>
>   $ emacs --batch --eval '(message (defun foo () (foo)) (foo))'
>   Lisp nesting exceeds ‘max-lisp-eval-depth’
>
> And so on.
>
> I understand that in C you usually don't manage your own stack and,
> consequently, you can't say that it falls under "memory management" in
> the sense of malloc(3) and brk(2) and such.  But C programmers are aware
> of the mechanisms behind the stack (or at least better be) and won't be
> surprised when they get a segfault in this situation.
>
> But if one of my coworkers who knows some web programming and not much
> about system programming gets a segfault, that's not a friendly
> error.  If Bash instead said something like the above languages, then
> that would be useful.
>
> When I first saw the error, I didn't know that my trap was
> recursing.  My immediate reaction was "shit, I found a bug".  Once I saw
> it was the trap, I _assumed_ it was just exhausting the stack, but I
> wanted to report it regardless, just in case; I didn't have the time to
> dig deeper, and even so, I wasn't sure if it was intended behavior to
> just let the kernel handle it.
>
>
> > This following complete C program recurses infinitely.  Or at least
> > until the stack is exhausted.  At which time it triggers a segfault
> > because it tries to use memory beyond the page mapped stack.
>
> [...]
>
> > Would you say that is a bug in the C language?  A bug in gcc that
> > compiled it?  A bug in the Unix/Linux kernel for memory management
> > that trapped the error?  The parent shell that reported the exit code
> > of the program?  Or in the program source code?  I am hoping that we
> > will all agree that it is a bug in the program source code and not
> > either gcc or the kernel. :-)
>
> I agree, yes.
>
> > Shell script code is program source code.  Infinite loops or infinite
> > recursion are bugs in the shell script source code not the interpreter
> > that is executing the code as written.
>
> I also agree.  But the context is very different.  Shell is a very,
> very high-level language.
>
> > This feels to me to be related to The Halting Problem.
>
> Knowing in advance whether there may be a problem certainly is, but we
> don't need to do that; we'd just need to detect it at runtime to provide
> a more useful error message.
>
> > Other shells are also fun to check:
> >
> >   $ dash -c 'trap "kill 0" TERM; kill 0'
> >   Segmentation fault
> >
> >   $ ash -c 'trap "kill 0" TERM; kill 0'
> >   Segmentation fault
> >
> >   $ mksh -c 'trap "kill 0" TERM; kill 0'
> >   Segmentation fault
>
> Heh, interesting!
>
> >   $ ksh93 -c 'trap "kill 0" TERM; kill 0'
> >   $ echo $?
> >   0
>
> This is not the behavior I'd want.
>
> >   $ posh -c 'trap "kill 0" TERM; kill 0'
> >   Terminated
> >   Terminated
> >   Terminated
> >   ...
> >   Terminated
> >   ^C
>
> :x
>
> > This finds what look like bugs in posh and ksh93.
>
> That's a fair assessment.
>
> >> it's just that most users assume that a segfault represents a
> >> problem with the program
> >
> > Yes.  And here it indicates a bug too.  It is indicating a bug in the
> > shell program code which sets up the infinite recursion.  Programs
> > should avoid doing that. :-)
>
> Indeed they should, but inevitably, such bugs do happen, and mine was a
> particularly common pitfall: I was trying to set the variable from
> within a subprocess.  But it wasn't in a subprocss when I originally
> wrote it.
>
> > The proper way for a program to terminate itself upon catching a
> > signal is to set the signal handler back to the default value and then
> > send the signal to itself so that it will be terminated as a result of
> > the signal and therefore the exit status will be set correctly.
>
> That's good advice.  Thank you.
>
> >   https://www.cons.org/cracauer/sigint.html
>
> Thanks.
>
> > Hope this helps!
>
> There was useful information, yes.  I hope I was able to further clarify
> my concerns as well.
>
> --
> Mike Gerwitz
>

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Segfault on recursive trap/kill Valentin Bajrami <valentin.bajrami@gmail.com> - 2018-10-07 08:52 +0200

csiph-web