aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/WebAssembly/Validation/ValidationStack.php
blob: 53b47a7777290a1ac3a04af1305f9d24d8463434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

declare(strict_types=1);

namespace Nsfisis\Waddiwasi\WebAssembly\Validation;

use Nsfisis\Waddiwasi\WebAssembly\Structure\Instructions\Instrs;
use Nsfisis\Waddiwasi\WebAssembly\Structure\Types\ValType;
use function array_pop;
use function array_reverse;
use function array_slice;
use function assert;
use function count;

final class ValidationStack
{
    /**
     * @var list<?ValType>
     */
    private array $values = [];

    /**
     * @var list<ControlFrame>
     */
    private array $controls = [];

    public function __construct(
    ) {
    }

    public function pushVal(?ValType $type): void
    {
        $this->values[] = $type;
    }

    /**
     * @param list<?ValType> $types
     */
    public function pushVals(array $types): void
    {
        foreach ($types as $type) {
            $this->pushVal($type);
        }
    }

    public function popVal(): ?ValType
    {
        // Check stack underflow.
        $currentCtrl = $this->getCurrentControl();
        if (count($this->values) === $currentCtrl->height) {
            // If the current control frame is marked as unreachable, the stack behaves polymorphically.
            if ($currentCtrl->unreachable) {
                return null;
            }
            throw new ValidationFailureException('Value stack underflow');
        }

        return array_pop($this->values);
    }

    public function popValOf(?ValType $expect): ?ValType
    {
        $actual = $this->popVal();
        if ($actual !== $expect && $actual !== null && $expect !== null) {
            throw new ValidationFailureException('Type mismatch');
        }
        return $actual;
    }

    /**
     * @param list<ValType> $types
     * @return list<?ValType>
     */
    public function popVals(array $types): array
    {
        $popped = [];
        foreach (array_reverse($types) as $type) {
            $popped[] = $this->popValOf($type);
        }
        return array_reverse($popped);
    }

    /**
     * @param class-string $opcode
     * @param list<ValType> $in
     * @param list<ValType> $out
     */
    public function pushCtrl(string $opcode, array $in, array $out): void
    {
        $ctrl = new ControlFrame($opcode, $in, $out, count($this->values), false);
        $this->controls[] = $ctrl;
        $this->pushVals($in);
    }

    public function popCtrl(): ControlFrame
    {
        if ($this->controlDepth() === 0) {
            throw new ValidationFailureException('Control stack underflow');
        }
        $ctrl = $this->getCurrentControl();
        $this->popVals($ctrl->endTypes);
        if (count($this->values) !== $ctrl->height) {
            throw new ValidationFailureException('Invalid stack height');
        }
        array_pop($this->controls);
        return $ctrl;
    }

    /**
     * @return list<ValType>
     */
    public function labelTypes(ControlFrame $ctrl): array
    {
        return $ctrl->opcode === Instrs\Control\Loop::class ? $ctrl->startTypes : $ctrl->endTypes;
    }

    public function unreachable(): void
    {
        $currentCtrl = $this->getCurrentControl();
        $this->values = array_slice($this->values, 0, $currentCtrl->height);
        assert(count($this->controls) !== 0);
        array_pop($this->controls);
        $this->controls[] = new ControlFrame(
            $currentCtrl->opcode,
            $currentCtrl->startTypes,
            $currentCtrl->endTypes,
            $currentCtrl->height,
            true,
        );
    }

    public function controlDepth(): int
    {
        return count($this->controls);
    }

    public function getControl(int $index): ?ControlFrame
    {
        $absIndex = count($this->controls) - 1 - $index;
        if ($absIndex < 0) {
            return null;
        }
        return $this->controls[$absIndex] ?? null;
    }

    public function getCurrentControl(): ControlFrame
    {
        if ($this->controlDepth() === 0) {
            throw new ValidationFailureException('No control frame available');
        }
        assert(count($this->controls) !== 0);
        return $this->controls[array_key_last($this->controls)];
    }
}