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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Module for pages which size can be different for different runtime versions.

use numerated::{interval::Interval, iterators::IntervalIterator, Bound, Numerated, OptionBound};
use std::{cmp::Ordering, marker::PhantomData, mem, num::NonZeroU32};

/// Size number for dyn-size pages.
pub trait SizeNumber: Copy + Ord + Eq {
    const SIZE_NO: usize;
}

const WASM_SIZE_NO: usize = 0;
const GEAR_SIZE_NO: usize = 1;

/// Amount of different page sizes.
///
/// NOTE: Must be in connect with current runtime.
/// If runtime wanna to reduce or increase amount of pages with different size,
/// then we must add here support for old runtimes that used 2 sizes,
/// and add support for new runtimes that uses some other amount of sizes.
pub(crate) const SIZES_AMOUNT: usize = 2;

/// Size number for wasm pages.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct WasmSizeNo;

impl SizeNumber for WasmSizeNo {
    const SIZE_NO: usize = WASM_SIZE_NO;
}

/// Size number for gear pages.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct GearSizeNo;

impl SizeNumber for GearSizeNo {
    const SIZE_NO: usize = GEAR_SIZE_NO;
}

/// Context where dynamic size pages store their sizes
pub trait SizeManager {
    /// Returns non-zero size of page.
    fn size_non_zero<S: SizeNumber>(&self) -> NonZeroU32;
}

#[cfg(test)]
impl SizeManager for u32 {
    fn size_non_zero<S: SizeNumber>(&self) -> NonZeroU32 {
        NonZeroU32::new(*self).expect("Size cannot be zero")
    }
}

pub type PagesAmount<S> = OptionBound<Page<S>>;

pub trait PagesAmountTrait<S: SizeNumber>: Bound<Page<S>> {
    fn upper<M: SizeManager>(ctx: &M) -> u32 {
        Page::<S>::max_value(ctx)
            .raw
            .checked_add(1)
            .unwrap_or_else(|| unreachable!("Page size == 1 byte is restricted"))
    }
    fn new<M: SizeManager>(ctx: &M, raw: u32) -> Option<Self> {
        let page = match raw.cmp(&Self::upper(ctx)) {
            Ordering::Less => Some(Page::from_raw(raw)),
            Ordering::Equal => None,
            Ordering::Greater => return None,
        };
        Some(page.into())
    }
    fn offset<M: SizeManager>(&self, ctx: &M) -> usize {
        const _: () = assert!(mem::size_of::<usize>() > mem::size_of::<u32>());
        let raw = self.unbound().map(|p| p.raw).unwrap_or(Self::upper(ctx));
        (raw as usize)
            .checked_mul(Page::<S>::size(ctx) as usize)
            .unwrap_or_else(|| {
                unreachable!("changing page size during program execution is restricted")
            })
    }
    fn convert<M: SizeManager, S1: SizeNumber>(self, ctx: &M) -> PagesAmount<S1> {
        self.unbound().map(|p| p.to_page::<_, S1>(ctx)).into()
    }
}

impl<S: SizeNumber> PagesAmountTrait<S> for PagesAmount<S> {}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default, derive_more::Into)]
pub struct Page<S: SizeNumber> {
    raw: u32,
    _phantom: PhantomData<S>,
}

impl<S: SizeNumber> Page<S> {
    fn from_raw(raw: u32) -> Self {
        Page {
            raw,
            _phantom: PhantomData,
        }
    }

    /// Returns raw page number.
    pub fn raw(&self) -> u32 {
        self.raw
    }

    /// Page size. Guaranteed to be > 0.
    pub fn size<M: SizeManager>(ctx: &M) -> u32 {
        ctx.size_non_zero::<S>().into()
    }

    /// Returns max possible page number for 32-bits address space.
    pub fn max_value<M: SizeManager>(ctx: &M) -> Self {
        Self::from_raw(u32::MAX / Self::size(ctx))
    }

    /// Creates page from raw number with specific context and checks that page number is valid.
    /// Returns None if page number is invalid.
    pub fn new<M: SizeManager>(ctx: &M, raw: u32) -> Option<Self> {
        let page_size = Self::size(ctx);
        let page_begin = raw.checked_mul(page_size)?;

        // Check that the last page byte has index less or equal then u32::MAX
        let last_byte_offset = page_size - 1;
        page_begin.checked_add(last_byte_offset)?;

        Some(Page::from_raw(raw))
    }

    /// Returns offset of page.
    pub fn offset<M: SizeManager>(&self, ctx: &M) -> u32 {
        self.raw
            .checked_mul(Self::size(ctx))
            .unwrap_or_else(|| unreachable!("`self` page size has been changed - it's restricted"))
    }

    /// Returns offset of end of page.
    pub fn end_offset<M: SizeManager>(&self, ctx: &M) -> u32 {
        let size = Self::size(ctx);
        self.raw
            .checked_mul(size)
            .and_then(|offset| offset.checked_add(size - 1))
            .unwrap_or_else(|| unreachable!("`self` page size has been changed - it's restricted"))
    }

    /// Creates page from offset.
    pub fn from_offset<M: SizeManager>(ctx: &M, offset: u32) -> Self {
        Self::from_raw(offset / Self::size(ctx))
    }

    /// Returns page with other size `S1`, which contains `self` start byte.
    pub fn to_page<M: SizeManager, S1: SizeNumber>(self, ctx: &M) -> Page<S1> {
        Page::<S1>::from_offset(ctx, self.offset(ctx))
    }

    /// Returns [IntervalIterator] created from `self..end`, if `end >= self`.
    pub fn to_end_interval<M: SizeManager, I: Into<PagesAmount<S>>>(
        self,
        ctx: &M,
        end: I,
    ) -> Option<IntervalIterator<Page<S>>> {
        match Into::<PagesAmount<S>>::into(end).unbound() {
            Some(end) => Interval::try_from_range(self..end).map(Into::into).ok(),
            None => (self..=Self::max_value(ctx)).try_into().ok(),
        }
    }
}

impl<S: SizeNumber> Numerated for Page<S> {
    type Distance = u32;
    type Bound = PagesAmount<S>;

    fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self> {
        self.raw.checked_add(num).and_then(|r| {
            r.enclosed_by(&self.raw, &other.raw)
                .then_some(Self::from_raw(r))
        })
    }

    fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self> {
        self.raw.checked_sub(num).and_then(|r| {
            r.enclosed_by(&self.raw, &other.raw)
                .then_some(Self::from_raw(r))
        })
    }

    fn distance(self, other: Self) -> Self::Distance {
        self.raw.abs_diff(other.raw)
    }
}

pub type GearPage = Page<GearSizeNo>;
pub type WasmPage = Page<WasmSizeNo>;
pub type WasmPagesAmount = PagesAmount<WasmSizeNo>;
pub type GearPagesAmount = PagesAmount<GearSizeNo>;

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use std::fmt::Debug;

    #[derive(Debug, Clone, Copy)]
    pub struct PageSizeManager(pub [u32; 2]);

    impl SizeManager for PageSizeManager {
        fn size_non_zero<S: SizeNumber>(&self) -> NonZeroU32 {
            NonZeroU32::new(self.0[S::SIZE_NO]).expect("Size cannot be zero")
        }
    }

    impl Default for PageSizeManager {
        fn default() -> Self {
            PageSizeManager([0x10000, 0x4000])
        }
    }

    #[test]
    fn raw() {
        let page = GearPage::from_raw(0x100);
        assert_eq!(page.raw(), 0x100);
    }

    #[test]
    fn size() {
        let ctx = PageSizeManager::default();
        assert_eq!(GearPage::size(&ctx), 0x4000);
        assert_eq!(WasmPage::size(&ctx), 0x10000);
    }

    #[test]
    fn max_value() {
        let ctx = PageSizeManager::default();
        assert_eq!(GearPage::max_value(&ctx).raw(), 0x3FFFF);
        assert_eq!(WasmPage::max_value(&ctx).raw(), 0xFFFF);
    }

    #[test]
    fn new() {
        let ctx = PageSizeManager::default();
        assert_eq!(
            GearPage::new(&ctx, 0x3FFFF),
            Some(GearPage::from_raw(0x3FFFF))
        );
        assert_eq!(GearPage::new(&ctx, 0x40000), None);
        assert_eq!(
            WasmPage::new(&ctx, 0xFFFF),
            Some(WasmPage::from_raw(0xFFFF))
        );
        assert_eq!(WasmPage::new(&ctx, 0x10000), None);
    }

    #[test]
    fn offset() {
        let ctx = PageSizeManager::default();
        let page = GearPage::from_raw(0x100);
        assert_eq!(page.offset(&ctx), 0x100 * ctx.0[GearSizeNo::SIZE_NO]);
    }

    #[test]
    fn end_offset() {
        let ctx = PageSizeManager::default();
        let page = GearPage::from_raw(0x100);
        assert_eq!(
            page.end_offset(&ctx),
            0x100 * ctx.0[GearSizeNo::SIZE_NO] + (ctx.0[GearSizeNo::SIZE_NO] - 1)
        );
    }

    #[test]
    fn from_offset() {
        let ctx = PageSizeManager::default();
        let page = GearPage::from_offset(&ctx, 0x100 * ctx.0[GearSizeNo::SIZE_NO]);
        assert_eq!(page.raw(), 0x100);
    }

    #[test]
    fn to_page() {
        let ctx = PageSizeManager::default();
        let page = GearPage::from_raw(0x400);
        let page1 = page.to_page::<_, WasmSizeNo>(&ctx);
        assert_eq!(page1.raw(), 0x100);
    }

    #[test]
    fn to_end_interval() {
        let ctx = PageSizeManager::default();
        let page = GearPage::from_raw(0x100);
        let interval: Vec<_> = page
            .to_end_interval(&ctx, GearPage::from_raw(0x200))
            .unwrap()
            .collect();
        assert_eq!(
            interval,
            (0x100..0x200).map(GearPage::from_raw).collect::<Vec<_>>()
        );
    }

    #[test]
    fn pages_amount_upper() {
        let ctx = PageSizeManager::default();
        assert_eq!(GearPagesAmount::upper(&ctx), 0x40000);
        assert_eq!(WasmPagesAmount::upper(&ctx), 0x10000);
    }

    #[test]
    fn pages_amount_new() {
        let ctx = PageSizeManager::default();
        assert_eq!(
            GearPagesAmount::new(&ctx, 0x3FFFF),
            Some(GearPage::from_raw(0x3FFFF).into())
        );
        assert_eq!(GearPagesAmount::new(&ctx, 0x40000), Some(None.into()));
        assert_eq!(GearPagesAmount::new(&ctx, 0x40001), None);
        assert_eq!(
            WasmPagesAmount::new(&ctx, 0xFFFF),
            Some(WasmPage::from_raw(0xFFFF).into())
        );
        assert_eq!(WasmPagesAmount::new(&ctx, 0x10000), Some(None.into()));
        assert_eq!(WasmPagesAmount::new(&ctx, 0x10001), None);
    }

    #[test]
    fn pages_amount_offset() {
        let ctx = PageSizeManager::default();
        let page = GearPage::from_raw(0x100);
        assert_eq!(
            GearPagesAmount::offset(&page.into(), &ctx),
            0x100 * ctx.0[GearSizeNo::SIZE_NO] as usize
        );
    }

    #[test]
    fn pages_amount_convert() {
        let ctx = PageSizeManager::default();
        let page1: GearPagesAmount = GearPage::from_raw(0x400).into();
        let page2: WasmPagesAmount = page1.convert(&ctx);
        assert_eq!(page2, Some(WasmPage::from_raw(0x100)));
    }
}

#[cfg(test)]
mod property_tests {
    use super::{tests::PageSizeManager, *};
    use numerated::mock;
    use proptest::{
        prelude::{any, Arbitrary},
        proptest,
        strategy::{BoxedStrategy, Strategy},
        test_runner::Config as ProptestConfig,
    };
    use std::fmt::Debug;

    impl<S: SizeNumber + Debug + 'static> Arbitrary for Page<S> {
        type Parameters = PageSizeManager;
        type Strategy = BoxedStrategy<Page<S>>;

        fn arbitrary_with(ctx: Self::Parameters) -> Self::Strategy {
            (0..Page::<S>::max_value(&ctx).raw)
                .prop_map(Page::from_raw)
                .boxed()
        }
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(1024))]

        #[test]
        fn gear_page_numerated(x in any::<GearPage>(), y in any::<GearPage>()) {
            mock::test_numerated(x, y);
        }

        #[test]
        fn wasm_page_numerated(x in any::<WasmPage>(), y in any::<WasmPage>()) {
            mock::test_numerated(x, y);
        }
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(64))]

        #[test]
        fn gear_page_tree((initial, actions) in mock::tree_actions::<GearPage>(0..128, 2..8)) {
            mock::test_tree(initial, actions);
        }

        #[test]
        fn wasm_page_tree((initial, actions) in mock::tree_actions::<WasmPage>(0..128, 10..20)) {
            mock::test_tree(initial, actions);
        }
    }
}