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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

#![forbid(unsafe_code)]

use bytecode_verifier::{cyclic_dependencies, dependencies, verify_module};
use move_binary_format::{access::ModuleAccess, file_format::CompiledModule};
use move_command_line_common::files::{
    extension_equals, find_filenames, MOVE_COMPILED_EXTENSION, MOVE_EXTENSION,
};
use move_lang::{compiled_unit::AnnotatedCompiledUnit, shared::AddressBytes, Compiler};
use move_symbol_pool::Symbol;
use once_cell::sync::Lazy;
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    fs::File,
    io::{Read, Write},
    path::{Path, PathBuf},
};

pub mod natives;
pub mod release;

const MODULES_DIR: &str = "modules";

/// The output path under which compiled files will be put
pub const COMPILED_OUTPUT_PATH: &str = "compiled";

pub fn path_in_crate<S>(relative: S) -> PathBuf
where
    S: Into<String>,
{
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push(relative.into());
    path
}

pub fn diem_stdlib_modules_full_path() -> String {
    format!("{}/{}", env!("CARGO_MANIFEST_DIR"), MODULES_DIR)
}

pub fn diem_stdlib_files_no_dependencies() -> Vec<String> {
    let path = path_in_crate(MODULES_DIR);
    find_filenames(&[path], |p| extension_equals(p, MOVE_EXTENSION)).unwrap()
}

pub fn diem_stdlib_files() -> Vec<String> {
    let mut files = move_stdlib::move_stdlib_files();
    files.extend(diem_stdlib_files_no_dependencies());
    files
}

// TODO: This will be declared in the package once those are in
pub fn diem_framework_named_addresses() -> BTreeMap<String, AddressBytes> {
    let mapping = [
        ("Std", "0x1"),
        ("DiemFramework", "0x1"),
        ("DiemRoot", "0xA550C18"),
        ("CurrencyInfo", "0xA550C18"),
        ("TreasuryCompliance", "0xB1E55ED"),
        ("VMReserved", "0x0"),
    ];
    mapping
        .iter()
        .map(|(name, addr)| (name.to_string(), AddressBytes::parse_str(addr).unwrap()))
        .collect()
}

pub fn stdlib_bytecode_files() -> Vec<String> {
    let path = path_in_crate(COMPILED_OUTPUT_PATH);
    let names = diem_stdlib_files();
    let res: Vec<String> =
        find_filenames(&[path], |p| extension_equals(p, MOVE_COMPILED_EXTENSION))
            .unwrap()
            .into_iter()
            .filter(|s| {
                let path = Path::new(s);
                for name in &names {
                    let suffix = "_".to_owned()
                        + Path::new(name)
                            .with_extension(MOVE_COMPILED_EXTENSION)
                            .file_name()
                            .unwrap()
                            .to_str()
                            .unwrap();
                    if path
                        .file_name()
                        .and_then(|f| f.to_str())
                        .map_or(false, |s| s.ends_with(&suffix))
                    {
                        return true;
                    }
                }
                false
            })
            .collect();
    assert!(
        !res.is_empty(),
        "Unexpected: no stdlib bytecode files found"
    );
    res
}

pub(crate) fn build_stdlib() -> BTreeMap<Symbol, CompiledModule> {
    let (_files, compiled_units) = Compiler::new(&diem_stdlib_files(), &[])
        .set_named_address_values(diem_framework_named_addresses())
        .build_and_report()
        .unwrap();
    let mut modules = BTreeMap::new();
    for compiled_unit in compiled_units {
        match compiled_unit {
            AnnotatedCompiledUnit::Module(annot_unit) => {
                verify_module(&annot_unit.named_module.module)
                    .expect("stdlib module failed to verify");
                dependencies::verify_module(&annot_unit.named_module.module, modules.values())
                    .expect("stdlib module dependency failed to verify");
                modules.insert(annot_unit.named_module.name, annot_unit.named_module.module);
            }
            AnnotatedCompiledUnit::Script(_) => panic!("Unexpected Script in stdlib"),
        }
    }
    let modules_by_id: BTreeMap<_, _> = modules
        .values()
        .map(|module| (module.self_id(), module))
        .collect();
    for module in modules_by_id.values() {
        cyclic_dependencies::verify_module(
            module,
            |module_id| {
                Ok(modules_by_id
                    .get(module_id)
                    .expect("missing module in stdlib")
                    .immediate_dependencies())
            },
            |module_id| {
                Ok(modules_by_id
                    .get(module_id)
                    .expect("missing module in stdlib")
                    .immediate_friends())
            },
        )
        .expect("stdlib module has cyclic dependencies");
    }
    modules
}

static MODULES: Lazy<Vec<CompiledModule>> = Lazy::new(|| {
    build_stdlib()
        .into_iter()
        .map(|(_key, value)| value)
        .collect()
});

static MODULE_BLOBS: Lazy<Vec<Vec<u8>>> = Lazy::new(|| {
    MODULES
        .iter()
        .map(|module| {
            let mut bytes = vec![];
            module.serialize(&mut bytes).unwrap();
            bytes
        })
        .collect()
});

pub fn modules() -> &'static [CompiledModule] {
    &MODULES
}

pub fn module_blobs() -> &'static [Vec<u8>] {
    &MODULE_BLOBS
}

fn save_binary(path: &Path, binary: &[u8]) -> bool {
    if path.exists() {
        let mut bytes = vec![];
        File::open(path).unwrap().read_to_end(&mut bytes).unwrap();
        if Sha256::digest(binary) == Sha256::digest(&bytes) {
            return false;
        }
    }
    File::create(path).unwrap().write_all(binary).unwrap();
    true
}