Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118777 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16682 invoked from network); 7 Oct 2022 13:32:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Oct 2022 13:32:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 194B01804F7 for ; Fri, 7 Oct 2022 06:32:27 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 7 Oct 2022 06:32:23 -0700 (PDT) Received: by mail-wm1-f51.google.com with SMTP id l8so2905703wmi.2 for ; Fri, 07 Oct 2022 06:32:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:to:subject:from:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=QT8Ufr1N2G1joUFxC+LDRW+h7khKqnYkMV6aIwpW/Fk=; b=CKyKlLo6CPzxYGeJ4+OtZuN4ntjMSVDq2AVVbooaNgGs39hi+Z9jtz2KUX/bGmycNq PQY3LYDgd8ZjBrNcXWRLPDgBaPGAS0WjoCTinBK7izp4xfYYYfxyezGUHstWIKVmNOs2 rhI7xszMuv3B056iDjdmTkMh4TkLvHgK5V33sUF/9eJWJ3PGYriYw67J+oAgkZa//p2k qLtNIw3fTgeUCLgBWG+EY1kQdYDDj4Jnn2MU1MgkO8QielHPI9SqL1fY6Qz/W5CorXCC H0WbMQO4ACwbp7FS/bWAeFpllXklqNNDy/JmpzYBWTA5Wh3tFt8d71hwZ8G5a5MfAXbI r/2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:to:subject:from:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=QT8Ufr1N2G1joUFxC+LDRW+h7khKqnYkMV6aIwpW/Fk=; b=Ig1Ynr9nUmWsVN/Mt6kODi2XnGsTypqQIpD5cvWNF6L0IlW1z6jkrm37QmffoR7XeR sC/p3o3eZvcqPKXEkBG6x1zY7R0tRwKKA8G0mTWGbgQE6RleTSCyrgCc62nrht5oGVFx lCI0nLzoHEd+Ho1l4R7oFbthFTKl6z+WSLUOdyMzGNBxHIaZOGCYhzwVC2BfMlRSg6Eu FT29zPtSgtIElsQTg9E4DLDCI00mj6pgvN9fj/BZ4jsYtLGsJNTIS1lNGCJ4AO1imVV7 DqiHWQTFFPzJHfLH01awDs1J3WVjgfq/MPzTy283Pm1ry+8EuoGc4cnnYBE5ezoDer8s Kd0g== X-Gm-Message-State: ACrzQf2p06gUTvFpdGNnV+PG3wcb6fkdwNW3BG56guVuTxwL1WcYYQup tlz4saS4FyeEK1D/PeqT9VOLlpglj4Ljxg== X-Google-Smtp-Source: AMsMyM7fXB6POzs8WW8P8IaLgZnQz2X19/BK8M+/GOHnk1fCLQlGCEKS49Ine7/Kja7WpKl9n5tVsQ== X-Received: by 2002:a05:600c:4e4d:b0:3c2:5978:d730 with SMTP id e13-20020a05600c4e4d00b003c25978d730mr3283441wmq.168.1665149540989; Fri, 07 Oct 2022 06:32:20 -0700 (PDT) Received: from ?IPV6:2a01:cb04:4b6:6600:f8a3:746b:2868:d3a3? (2a01cb0404b66600f8a3746b2868d3a3.ipv6.abo.wanadoo.fr. [2a01:cb04:4b6:6600:f8a3:746b:2868:d3a3]) by smtp.gmail.com with ESMTPSA id v127-20020a1cac85000000b003b505d26776sm7687612wme.5.2022.10.07.06.32.19 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 07 Oct 2022 06:32:20 -0700 (PDT) Message-ID: <8392afe2-3f5b-5811-64fc-a38f40f9af7a@gmail.com> Date: Fri, 7 Oct 2022 15:32:19 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Content-Language: en-US To: PHP Internals List Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Preventing stack overflows From: arnaud.lb@gmail.com (Arnaud Le Blanc) Hi internals, I would like to propose a way to detect stack overflows before they happen, with the goal of improving debugability. Stack overflows are caused by excessive stack usage, typically due to deep recursions. Recursion in PHP doesn't use recursion internally, but some constructs still do, and it is not always possible to avoid without dramatically increasing complexity or degrading performances. Some examples of these constructs can be found at [1][2][3]. Programs that overflow the stack are terminated with a SIGSEGV, and the user is left with no hint as to which code is causing the issue. This makes debugging difficult. Xdebug makes debugging easier by throwing an exception when the function call nesting level exceeds a certain limit [4], which is very useful. However, this can be overly limiting because this does not discriminate between the calls that actually use the stack and those that do not. Nikita proposed an other solution a while ago [5] that limits in terms of VM reentrances, so that only the constructs that actually cause internal recursion count towards the limit. One issue is that not all VM frames will consume the same amount of stack, and the maximum stack size depends on the system, so it's hard to determine a good default value for the limit that is not overly limiting. I would like to propose an alternative [6] that limits in terms of stack size. One issue is that it increases the internals complexity a bit, but this would make debugging easier without limiting recursion too much compared to what the system would allow. Any thoughts? [1] https://bugs.php.net/bug.php?id=64196 [2] https://bugs.php.net/bug.php?id=75509 [3] https://github.com/php/php-src/issues/8315 [4] https://xdebug.org/docs/develop#max_nesting_level [5] https://externals.io/message/108348 [6] https://github.com/php/php-src/pull/9104