aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-05-05 12:27:17 +0900
committernsfisis <nsfisis@gmail.com>2024-05-05 14:12:32 +0900
commit27cd7cf8f816f1d1dd465c28a43fc0fcd25c48d6 (patch)
treef38f4eee869735a18c6df71058ed8ddc5ccbb635 /src
parent2f533445e47f4a0f7c94677f5fda3683a4f1fee7 (diff)
downloadphp-waddiwasi-27cd7cf8f816f1d1dd465c28a43fc0fcd25c48d6.tar.gz
php-waddiwasi-27cd7cf8f816f1d1dd465c28a43fc0fcd25c48d6.tar.zst
php-waddiwasi-27cd7cf8f816f1d1dd465c28a43fc0fcd25c48d6.zip
feat: limit call stack size
Diffstat (limited to 'src')
-rw-r--r--src/Execution/Stack.php8
-rw-r--r--src/Execution/StackOverflowException.php19
2 files changed, 27 insertions, 0 deletions
diff --git a/src/Execution/Stack.php b/src/Execution/Stack.php
index 424f8c6..6d2a2aa 100644
--- a/src/Execution/Stack.php
+++ b/src/Execution/Stack.php
@@ -30,6 +30,9 @@ final class Stack
public function pushFrame(Frame $frame): void
{
+ if ($this->getCallStackLimit() <= count($this->frames)) {
+ throw new StackOverflowException();
+ }
$this->push($frame);
$this->frames[] = $frame;
$this->currentFrame = $frame;
@@ -179,6 +182,11 @@ final class Stack
return $this->currentFrame;
}
+ public function getCallStackLimit(): int
+ {
+ return 1024;
+ }
+
private function push(int|float|Ref|Frame|Label $entry): void
{
$this->entries[] = $entry;
diff --git a/src/Execution/StackOverflowException.php b/src/Execution/StackOverflowException.php
new file mode 100644
index 0000000..92991e2
--- /dev/null
+++ b/src/Execution/StackOverflowException.php
@@ -0,0 +1,19 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution;
+
+use RuntimeException;
+use Throwable;
+
+final class StackOverflowException extends RuntimeException
+{
+ public function __construct(
+ string $message = 'Stack overflow',
+ int $code = 0,
+ Throwable $previous = null,
+ ) {
+ parent::__construct($message, $code, $previous);
+ }
+}