summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testWasmLEB128.cpp
blob: d39658e127b6081fecaba9ebdede7add30c5f4c1 (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
155
156
157
158
159
160
161
162
163
164
165
166
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdlib.h>

#include "jsapi-tests/tests.h"

#include "wasm/WasmBinaryFormat.h"

static bool WriteValidBytes(js::wasm::Encoder& encoder, bool* passed)
{
    *passed = false;
    if (!encoder.empty())
        return true;

    // These remain the same under LEB128 unsigned encoding
    if (!encoder.writeVarU32(0x0) ||
        !encoder.writeVarU32(0x1) ||
        !encoder.writeVarU32(0x42))
    {
        return false;
    }

    // 0x01 0x80
    if (!encoder.writeVarU32(0x80))
        return false;

    // 0x03 0x80
    if (!encoder.writeVarU32(0x180))
        return false;

    if (encoder.empty())
        return true;
    if (encoder.currentOffset() != 7)
        return true;
    *passed = true;
    return true;
}

BEGIN_TEST(testWasmLEB128_encoding)
{
    using namespace js;
    using namespace wasm;

    Bytes bytes;
    Encoder encoder(bytes);

    bool passed;
    if (!WriteValidBytes(encoder, &passed))
        return false;
    CHECK(passed);

    size_t i = 0;
    CHECK(bytes[i++] == 0x0);
    CHECK(bytes[i++] == 0x1);
    CHECK(bytes[i++] == 0x42);

    CHECK(bytes[i++] == 0x80);
    CHECK(bytes[i++] == 0x01);

    CHECK(bytes[i++] == 0x80);
    CHECK(bytes[i++] == 0x03);

    if (i + 1 < bytes.length())
        CHECK(bytes[i++] == 0x00);
    return true;
}
END_TEST(testWasmLEB128_encoding)

BEGIN_TEST(testWasmLEB128_valid_decoding)
{
    using namespace js;
    using namespace wasm;

    Bytes bytes;
    if (!bytes.append(0x0) || !bytes.append(0x1) || !bytes.append(0x42))
        return false;

    if (!bytes.append(0x80) || !bytes.append(0x01))
        return false;

    if (!bytes.append(0x80) || !bytes.append(0x03))
        return false;

    {
        // Fallible decoding
        Decoder decoder(bytes);
        uint32_t value;

        CHECK(decoder.readVarU32(&value) && value == 0x0);
        CHECK(decoder.readVarU32(&value) && value == 0x1);
        CHECK(decoder.readVarU32(&value) && value == 0x42);
        CHECK(decoder.readVarU32(&value) && value == 0x80);
        CHECK(decoder.readVarU32(&value) && value == 0x180);

        CHECK(decoder.done());
    }

    {
        // Infallible decoding
        Decoder decoder(bytes);
        uint32_t value;

        value = decoder.uncheckedReadVarU32();
        CHECK(value == 0x0);
        value = decoder.uncheckedReadVarU32();
        CHECK(value == 0x1);
        value = decoder.uncheckedReadVarU32();
        CHECK(value == 0x42);
        value = decoder.uncheckedReadVarU32();
        CHECK(value == 0x80);
        value = decoder.uncheckedReadVarU32();
        CHECK(value == 0x180);

        CHECK(decoder.done());
    }
    return true;
}
END_TEST(testWasmLEB128_valid_decoding)

BEGIN_TEST(testWasmLEB128_invalid_decoding)
{
    using namespace js;
    using namespace wasm;

    Bytes bytes;
    // Fill bits as per 28 encoded bits
    if (!bytes.append(0x80) || !bytes.append(0x80) || !bytes.append(0x80) || !bytes.append(0x80))
        return false;

    // Test last valid values
    if (!bytes.append(0x00))
        return false;

    for (uint8_t i = 0; i < 0x0F; i++) {
        bytes[4] = i;

        {
            Decoder decoder(bytes);
            uint32_t value;
            CHECK(decoder.readVarU32(&value));
            CHECK(value == uint32_t(i << 28));
            CHECK(decoder.done());
        }

        {
            Decoder decoder(bytes);
            uint32_t value = decoder.uncheckedReadVarU32();
            CHECK(value == uint32_t(i << 28));
            CHECK(decoder.done());
        }
    }

    // Test all invalid values of the same size
    for (uint8_t i = 0x10; i < 0xF0; i++) {
        bytes[4] = i;

        Decoder decoder(bytes);
        uint32_t value;
        CHECK(!decoder.readVarU32(&value));
    }

    return true;
}
END_TEST(testWasmLEB128_invalid_decoding)