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
use itertools::{Either, Itertools};
use twizzler_abi::{
    object::{MAX_SIZE, NULLPAGE_SIZE},
    syscall::{
        sys_object_create, BackingType, LifetimeType, ObjectCreate, ObjectCreateFlags, ObjectSource,
    },
};
use twizzler_runtime_api::MapFlags;

use super::{Backing, LoadDirective, LoadFlags};
use crate::{DynlinkError, DynlinkErrorKind};

pub struct Engine;

fn within_object(slot: usize, addr: usize) -> bool {
    addr >= slot * MAX_SIZE + NULLPAGE_SIZE && addr < (slot + 1) * MAX_SIZE - NULLPAGE_SIZE * 2
}

/// Load segments according to Twizzler requirements. Helper function for implementing a
/// ContextEngine.
pub fn load_segments(src: &Backing, ld: &[LoadDirective]) -> Result<Vec<Backing>, DynlinkError> {
    let create_spec = ObjectCreate::new(
        BackingType::Normal,
        LifetimeType::Volatile,
        None,
        ObjectCreateFlags::empty(),
    );

    let build_copy_cmd = |directive: &LoadDirective| {
        if !within_object(
            if directive.load_flags.contains(LoadFlags::TARGETS_DATA) {
                1
            } else {
                0
            },
            directive.vaddr,
        ) || directive.memsz > MAX_SIZE - NULLPAGE_SIZE * 2
            || directive.offset > MAX_SIZE - NULLPAGE_SIZE * 2
            || directive.filesz > directive.memsz
        {
            return Err(DynlinkError::new(DynlinkErrorKind::LoadDirectiveFail {
                dir: *directive,
            }));
        }

        if directive.filesz != directive.memsz {
            todo!()
        }

        let src_start = (NULLPAGE_SIZE + directive.offset) & !(directive.align - 1);
        let dest_start = directive.vaddr & !(directive.align - 1);
        let len = (directive.vaddr - dest_start) + directive.filesz;

        if !directive.load_flags.contains(LoadFlags::TARGETS_DATA) {
            // Ensure we can direct-map the object for the text directives.
            if src_start != dest_start || directive.filesz != directive.memsz {
                // TODO: check len too.
                return Err(DynlinkError::new(DynlinkErrorKind::LoadDirectiveFail {
                    dir: *directive,
                }));
            }
        }

        Ok(ObjectSource::new_copy(
            src.obj.id,
            (src_start % MAX_SIZE) as u64,
            (dest_start % MAX_SIZE) as u64,
            len,
        ))
    };

    let ld = ld.to_vec();
    let (data_cmds, text_cmds): (Vec<_>, Vec<_>) = ld.into_iter().partition_map(|directive| {
        if directive.load_flags.contains(LoadFlags::TARGETS_DATA) {
            Either::Left(build_copy_cmd(&directive))
        } else {
            Either::Right(build_copy_cmd(&directive))
        }
    });

    let data_cmds = DynlinkError::collect(DynlinkErrorKind::NewBackingFail, data_cmds)?;
    let text_cmds = DynlinkError::collect(DynlinkErrorKind::NewBackingFail, text_cmds)?;

    let data_id = sys_object_create(create_spec, &data_cmds, &[])
        .map_err(|_| DynlinkErrorKind::NewBackingFail)?;

    let text_id = sys_object_create(create_spec, &text_cmds, &[])
        .map_err(|_| DynlinkErrorKind::NewBackingFail)?;

    let runtime = twizzler_runtime_api::get_runtime();

    let (text_handle, data_handle) = runtime
        .map_two_objects(
            text_id,
            MapFlags::READ | MapFlags::EXEC,
            data_id,
            MapFlags::READ | MapFlags::WRITE,
        )
        .map_err(|_| DynlinkErrorKind::NewBackingFail)?;

    if data_handle.start as usize != text_handle.start as usize + MAX_SIZE {
        tracing::error!(
            "internal runtime error: failed to map text and data adjacent and in-order ({:p} {:p})",
            text_handle.start,
            data_handle.start
        );
        return Err(DynlinkErrorKind::NewBackingFail.into());
    }

    Ok(vec![Backing::new(text_handle), Backing::new(data_handle)])
}