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
//! This module define the required structures to emit custom
//! Sections in a `Compilation`.
//!
//! The functions that access a custom [`CustomSection`] would need
//! to emit a custom relocation: `RelocationTarget::CustomSection`, so
//! it can be patched later by the engine (native or JIT).

use crate::lib::std::vec::Vec;
use crate::Relocation;
use wasmer_types::entity::entity_impl;

/// Index type of a Section defined inside a WebAssembly `Compilation`.
#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
#[archive_attr(derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug))]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct SectionIndex(u32);

entity_impl!(SectionIndex);

entity_impl!(ArchivedSectionIndex);

/// Custom section Protection.
///
/// Determines how a custom section may be used.
#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Copy, Clone, PartialEq, Eq)]
pub enum CustomSectionProtection {
    /// A custom section with read permission.
    Read,

    /// A custom section with read and execute permissions.
    ReadExecute,
}

/// A Section for a `Compilation`.
///
/// This is used so compilers can store arbitrary information
/// in the emitted module.
#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq)]
pub struct CustomSection {
    /// Memory protection that applies to this section.
    pub protection: CustomSectionProtection,

    /// The bytes corresponding to this section.
    ///
    /// > Note: These bytes have to be at-least 8-byte aligned
    /// > (the start of the memory pointer).
    /// > We might need to create another field for alignment in case it's
    /// > needed in the future.
    pub bytes: SectionBody,

    /// Relocations that apply to this custom section.
    pub relocations: Vec<Relocation>,
}

/// See [`CustomSection`].
///
/// Note that this does not reference the relocation data.
#[derive(Clone, Copy)]
pub struct CustomSectionRef<'a> {
    /// See [`CustomSection::protection`].
    pub protection: CustomSectionProtection,

    /// See [`CustomSection::bytes`].
    pub bytes: &'a [u8],
}

impl<'a> From<&'a CustomSection> for CustomSectionRef<'a> {
    fn from(section: &'a CustomSection) -> Self {
        CustomSectionRef {
            protection: section.protection.clone(),
            bytes: section.bytes.as_slice(),
        }
    }
}

impl<'a> From<&'a ArchivedCustomSection> for CustomSectionRef<'a> {
    fn from(section: &'a ArchivedCustomSection) -> Self {
        CustomSectionRef {
            protection: Result::<_, std::convert::Infallible>::unwrap(
                rkyv::Deserialize::deserialize(&section.protection, &mut rkyv::Infallible),
            ),
            bytes: &section.bytes.0[..],
        }
    }
}

/// The bytes in the section.
#[derive(
    rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, PartialEq, Eq, Default,
)]
pub struct SectionBody(Vec<u8>);

impl SectionBody {
    /// Create a new section body with the given contents.
    pub fn new_with_vec(contents: Vec<u8>) -> Self {
        Self(contents)
    }

    /// Returns a raw pointer to the section's buffer.
    pub fn as_ptr(&self) -> *const u8 {
        self.0.as_ptr()
    }

    /// Returns the length of this section in bytes.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Dereferences into the section's buffer.
    pub fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }

    /// Returns whether or not the section body is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}