vm_runtime.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. cell::{Cell, RefCell},
  20. sync::Arc,
  21. };
  22. use darkfi_sdk::{crypto::ContractId, entrypoint};
  23. use darkfi_serial::serialize;
  24. use log::{debug, error, info};
  25. use wasmer::{
  26. imports, wasmparser::Operator, AsStoreRef, CompilerConfig, Function, FunctionEnv, Instance,
  27. Memory, MemoryView, Module, Pages, Store, Value, WASM_PAGE_SIZE,
  28. };
  29. use wasmer_compiler_singlepass::Singlepass;
  30. use wasmer_middlewares::{
  31. metering::{get_remaining_points, MeteringPoints},
  32. Metering,
  33. };
  34. use super::{import, import::db::DbHandle, memory::MemoryManipulation};
  35. use crate::{blockchain::BlockchainOverlayPtr, Error, Result};
  36. /// Name of the wasm linear memory in our guest module
  37. const MEMORY: &str = "memory";
  38. /// Gas limit for a contract
  39. const GAS_LIMIT: u64 = 200000000;
  40. /// The hardcoded db name for the zkas circuits database tree
  41. pub const SMART_CONTRACT_ZKAS_DB_NAME: &str = "_zkas";
  42. #[derive(Clone, Copy, PartialEq)]
  43. pub enum ContractSection {
  44. /// Setup function of a contract
  45. Deploy,
  46. /// Entrypoint function of a contract
  47. Exec,
  48. /// Apply function of a contract
  49. Update,
  50. /// Metadata
  51. Metadata,
  52. /// Placeholder state before any initialization
  53. Null,
  54. }
  55. impl ContractSection {
  56. pub fn name(&self) -> &str {
  57. match self {
  58. Self::Deploy => "__initialize",
  59. Self::Exec => "__entrypoint",
  60. Self::Update => "__update",
  61. Self::Metadata => "__metadata",
  62. Self::Null => unreachable!(),
  63. }
  64. }
  65. }
  66. /// The wasm vm runtime instantiated for every smart contract that runs.
  67. pub struct Env {
  68. /// Blockchain overlay access
  69. pub blockchain: BlockchainOverlayPtr,
  70. /// Overlay tree handles used with `db_*`
  71. pub db_handles: RefCell<Vec<DbHandle>>,
  72. /// The contract ID being executed
  73. pub contract_id: ContractId,
  74. /// The compiled wasm bincode being executed,
  75. pub contract_bincode: Vec<u8>,
  76. /// The contract section being executed
  77. pub contract_section: ContractSection,
  78. /// State update produced by a smart contract function call
  79. pub contract_return_data: Cell<Option<Vec<u8>>>,
  80. /// Logs produced by the contract
  81. pub logs: RefCell<Vec<String>>,
  82. /// Direct memory access to the VM
  83. pub memory: Option<Memory>,
  84. /// Object store for transferring memory from the host to VM
  85. pub objects: RefCell<Vec<Vec<u8>>>,
  86. }
  87. impl Env {
  88. /// Provide safe access to the memory
  89. /// (it must be initialized before it can be used)
  90. ///
  91. /// // ctx: FunctionEnvMut<Env>
  92. /// let env = ctx.data();
  93. /// let memory = env.memory_view(&ctx);
  94. ///
  95. pub fn memory_view<'a>(&'a self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
  96. self.memory().view(store)
  97. }
  98. /// Get memory, that needs to have been set fist
  99. pub fn memory(&self) -> &Memory {
  100. self.memory.as_ref().unwrap()
  101. }
  102. }
  103. pub struct Runtime {
  104. pub instance: Instance,
  105. pub store: Store,
  106. pub ctx: FunctionEnv<Env>,
  107. }
  108. impl Runtime {
  109. /// Create a new wasm runtime instance that contains the given wasm module.
  110. pub fn new(
  111. wasm_bytes: &[u8],
  112. blockchain: BlockchainOverlayPtr,
  113. contract_id: ContractId,
  114. ) -> Result<Self> {
  115. info!(target: "runtime::vm_runtime", "Instantiating a new runtime");
  116. // TODO: Add necessary operators
  117. // This function will be called for each `Operator` encountered during
  118. // the wasm module execution. It should return the cost of the operator
  119. // that it received as its first argument.
  120. // https://docs.rs/wasmparser/latest/wasmparser/enum.Operator.html
  121. let cost_function = |operator: &Operator| -> u64 {
  122. match operator {
  123. Operator::LocalGet { .. } => 1,
  124. Operator::I32Const { .. } => 1,
  125. Operator::I32Add { .. } => 2,
  126. _ => 0,
  127. }
  128. };
  129. // `Metering` needs to be conigured with a limit and a cost function.
  130. // For each `Operator`, the metering middleware will call the cost
  131. // function and subtract the cost from the remaining points.
  132. let metering = Arc::new(Metering::new(GAS_LIMIT, cost_function));
  133. // Define the compiler and middleware, engine, and store
  134. let mut compiler_config = Singlepass::new();
  135. compiler_config.push_middleware(metering);
  136. let mut store = Store::new(compiler_config);
  137. debug!(target: "runtime::vm_runtime", "Compiling module");
  138. let module = Module::new(&store, wasm_bytes)?;
  139. // Initialize data
  140. let db_handles = RefCell::new(vec![]);
  141. let logs = RefCell::new(vec![]);
  142. debug!(target: "runtime::vm_runtime", "Importing functions");
  143. let ctx = FunctionEnv::new(
  144. &mut store,
  145. Env {
  146. blockchain,
  147. db_handles,
  148. contract_id,
  149. contract_bincode: wasm_bytes.to_vec(),
  150. contract_section: ContractSection::Null,
  151. contract_return_data: Cell::new(None),
  152. logs,
  153. memory: None,
  154. objects: RefCell::new(vec![]),
  155. },
  156. );
  157. let imports = imports! {
  158. "env" => {
  159. "drk_log_" => Function::new_typed_with_env(
  160. &mut store,
  161. &ctx,
  162. import::util::drk_log,
  163. ),
  164. "set_return_data_" => Function::new_typed_with_env(
  165. &mut store,
  166. &ctx,
  167. import::util::set_return_data,
  168. ),
  169. "db_init_" => Function::new_typed_with_env(
  170. &mut store,
  171. &ctx,
  172. import::db::db_init,
  173. ),
  174. "db_lookup_" => Function::new_typed_with_env(
  175. &mut store,
  176. &ctx,
  177. import::db::db_lookup,
  178. ),
  179. "db_get_" => Function::new_typed_with_env(
  180. &mut store,
  181. &ctx,
  182. import::db::db_get,
  183. ),
  184. "db_contains_key_" => Function::new_typed_with_env(
  185. &mut store,
  186. &ctx,
  187. import::db::db_contains_key,
  188. ),
  189. "db_set_" => Function::new_typed_with_env(
  190. &mut store,
  191. &ctx,
  192. import::db::db_set,
  193. ),
  194. "db_del_" => Function::new_typed_with_env(
  195. &mut store,
  196. &ctx,
  197. import::db::db_del,
  198. ),
  199. "zkas_db_set_" => Function::new_typed_with_env(
  200. &mut store,
  201. &ctx,
  202. import::db::zkas_db_set,
  203. ),
  204. "put_object_bytes_" => Function::new_typed_with_env(
  205. &mut store,
  206. &ctx,
  207. import::util::put_object_bytes,
  208. ),
  209. "get_object_bytes_" => Function::new_typed_with_env(
  210. &mut store,
  211. &ctx,
  212. import::util::get_object_bytes,
  213. ),
  214. "get_object_size_" => Function::new_typed_with_env(
  215. &mut store,
  216. &ctx,
  217. import::util::get_object_size,
  218. ),
  219. "merkle_add_" => Function::new_typed_with_env(
  220. &mut store,
  221. &ctx,
  222. import::merkle::merkle_add,
  223. ),
  224. }
  225. };
  226. debug!(target: "runtime::vm_runtime", "Instantiating module");
  227. let instance = Instance::new(&mut store, &module, &imports)?;
  228. let mut env_mut = ctx.as_mut(&mut store);
  229. env_mut.memory = Some(instance.exports.get_with_generics(MEMORY)?);
  230. Ok(Self { instance, store, ctx })
  231. }
  232. fn call(&mut self, section: ContractSection, payload: &[u8]) -> Result<Vec<u8>> {
  233. debug!(target: "runtime::vm_runtime", "Calling {} method", section.name());
  234. let mut env_mut = self.ctx.as_mut(&mut self.store);
  235. env_mut.contract_section = section;
  236. assert!(env_mut.contract_return_data.take().is_none());
  237. env_mut.contract_return_data.set(None);
  238. // Clear the logs
  239. let _ = env_mut.logs.take();
  240. // Serialize the payload for the format the wasm runtime is expecting.
  241. let payload = Self::serialize_payload(&env_mut.contract_id, payload);
  242. // Allocate enough memory for the payload and copy it into the memory.
  243. let pages_required = payload.len() / WASM_PAGE_SIZE + 1;
  244. self.set_memory_page_size(pages_required as u32)?;
  245. self.copy_to_memory(&payload)?;
  246. debug!(target: "runtime::vm_runtime", "Getting {} function", section.name());
  247. let entrypoint = self.instance.exports.get_function(section.name())?;
  248. debug!(target: "runtime::vm_runtime", "Executing wasm");
  249. let ret = match entrypoint.call(&mut self.store, &[Value::I32(0_i32)]) {
  250. Ok(retvals) => {
  251. self.print_logs();
  252. debug!(target: "runtime::vm_runtime", "{}", self.gas_info());
  253. retvals
  254. }
  255. Err(e) => {
  256. self.print_logs();
  257. debug!(target: "runtime::vm_runtime", "{}", self.gas_info());
  258. // WasmerRuntimeError panics are handled here. Return from run() immediately.
  259. error!(target: "runtime::vm_runtime", "Wasmer Runtime Error: {:#?}", e);
  260. return Err(e.into())
  261. }
  262. };
  263. debug!(target: "runtime::vm_runtime", "wasm executed successfully");
  264. debug!(target: "runtime::vm_runtime", "Contract returned: {:?}", ret[0]);
  265. let mut env_mut = self.ctx.as_mut(&mut self.store);
  266. env_mut.contract_section = ContractSection::Null;
  267. let retdata = match env_mut.contract_return_data.take() {
  268. Some(retdata) => retdata,
  269. None => Vec::new(),
  270. };
  271. let retval = match ret[0] {
  272. Value::I64(v) => v,
  273. _ => unreachable!("Got unexpected result from ret: {:?}", ret),
  274. };
  275. match retval {
  276. entrypoint::SUCCESS => Ok(retdata),
  277. // FIXME: we should be able to see the error returned from the contract
  278. // We can put sdk::Error inside of this.
  279. _ => {
  280. let err = darkfi_sdk::error::ContractError::from(retval);
  281. Err(Error::ContractError(err))
  282. }
  283. }
  284. }
  285. /// This function runs when a smart contract is initially deployed, or re-deployed.
  286. /// The runtime will look for an `INITIALIZE` symbol in the wasm code, and execute
  287. /// it if found. Optionally, it is possible to pass in a payload for any kind of special
  288. /// instructions the developer wants to manage in the initialize function.
  289. /// This process is supposed to set up the overlay trees for storing the smart contract
  290. /// state, and it can create, delete, modify, read, and write to databases it's allowed to.
  291. /// The permissions for this are handled by the `ContractId` in the overlay db API so we
  292. /// assume that the contract is only able to do write operations on its own overlay trees.
  293. pub fn deploy(&mut self, payload: &[u8]) -> Result<()> {
  294. info!(target: "runtime::vm_runtime", "[wasm-runtime] Running deploy");
  295. // Scoped for borrows
  296. {
  297. let env_mut = self.ctx.as_mut(&mut self.store);
  298. // We always want to have the zkas db as index 0 in db handles and batches when
  299. // deploying.
  300. let contracts = &env_mut.blockchain.lock().unwrap().contracts;
  301. let zkas_tree_handle =
  302. match contracts.lookup(&env_mut.contract_id, SMART_CONTRACT_ZKAS_DB_NAME) {
  303. Ok(v) => v,
  304. Err(_) => {
  305. // FIXME: All this is deploy code is "vulnerable" and able to init a
  306. // tree regardless of execution success. We can easily delete the db
  307. // if execution fails though, and we should charge gas for db_init.
  308. // and perhaps also for the zkas database in this specific case.
  309. contracts.init(&env_mut.contract_id, SMART_CONTRACT_ZKAS_DB_NAME)?
  310. }
  311. };
  312. let mut db_handles = env_mut.db_handles.borrow_mut();
  313. db_handles.push(DbHandle::new(env_mut.contract_id, zkas_tree_handle));
  314. }
  315. debug!(target: "runtime::vm_runtime", "[wasm-runtime] payload: {:?}", payload);
  316. let _ = self.call(ContractSection::Deploy, payload)?;
  317. // Update the wasm bincode in the WasmStore
  318. let env_mut = self.ctx.as_mut(&mut self.store);
  319. env_mut
  320. .blockchain
  321. .lock()
  322. .unwrap()
  323. .wasm_bincode
  324. .insert(env_mut.contract_id, &env_mut.contract_bincode)?;
  325. Ok(())
  326. }
  327. /// This funcion runs when someone wants to execute a smart contract.
  328. /// The runtime will look for an `ENTRYPOINT` symbol in the wasm code, and
  329. /// execute it if found. A payload is also passed as an instruction that can
  330. /// be used inside the vm by the runtime.
  331. pub fn exec(&mut self, payload: &[u8]) -> Result<Vec<u8>> {
  332. debug!(target: "runtime::vm_runtime", "exec: {:?}", payload);
  333. self.call(ContractSection::Exec, payload)
  334. }
  335. /// This function runs after successful execution of `exec` and tries to
  336. /// apply the state change to the overlay databases.
  337. /// The runtime will lok for an `UPDATE` symbol in the wasm code, and execute
  338. /// it if found. The function does not take an arbitrary payload, but just takes
  339. /// a state update from `env` and passes it into the wasm runtime.
  340. pub fn apply(&mut self, update: &[u8]) -> Result<()> {
  341. debug!(target: "runtime::vm_runtime", "apply: {:?}", update);
  342. let _ = self.call(ContractSection::Update, update)?;
  343. Ok(())
  344. }
  345. /// This function runs first in the entire scheme of executing a smart contract.
  346. /// It is supposed to correctly extract public inputs for any ZK proofs included
  347. /// in the contract calls, and also extract the public keys used to verify the
  348. /// call/transaction signatures.
  349. pub fn metadata(&mut self, payload: &[u8]) -> Result<Vec<u8>> {
  350. self.call(ContractSection::Metadata, payload)
  351. }
  352. fn print_logs(&self) {
  353. let logs = self.ctx.as_ref(&self.store).logs.borrow();
  354. for msg in logs.iter() {
  355. debug!(target: "runtime::vm_runtime", "Contract log: {}", msg);
  356. }
  357. }
  358. fn gas_info(&mut self) -> String {
  359. let remaining_points = get_remaining_points(&mut self.store, &self.instance);
  360. match remaining_points {
  361. MeteringPoints::Remaining(rem) => {
  362. format!("Gas used: {}/{}", GAS_LIMIT - rem, GAS_LIMIT)
  363. }
  364. MeteringPoints::Exhausted => {
  365. format!("Gas fully exhausted: {}/{}", GAS_LIMIT + 1, GAS_LIMIT)
  366. }
  367. }
  368. }
  369. /// Set the memory page size
  370. fn set_memory_page_size(&mut self, pages: u32) -> Result<Pages> {
  371. // Grab memory by value
  372. let memory = self.take_memory();
  373. // Modify the memory
  374. let ret = memory.grow(&mut self.store, Pages(pages))?;
  375. // Replace the memory back again
  376. self.ctx.as_mut(&mut self.store).memory = Some(memory);
  377. Ok(ret)
  378. }
  379. /// Take Memory by value. Needed to modify the Memory object
  380. /// Will panic if memory isn't set.
  381. fn take_memory(&mut self) -> Memory {
  382. let env_memory = &mut self.ctx.as_mut(&mut self.store).memory;
  383. let memory = env_memory.take();
  384. memory.expect("memory should be set")
  385. }
  386. /// Copy payload to the start of the memory
  387. fn copy_to_memory(&self, payload: &[u8]) -> Result<()> {
  388. // TODO: Maybe should write to first zero memory and return the pointer/offset?
  389. // Get the memory view
  390. let env = self.ctx.as_ref(&self.store);
  391. let memory_view = env.memory_view(&self.store);
  392. memory_view.write_slice(payload, 0)
  393. }
  394. /// Serialize contract payload to the format accepted by the runtime functions.
  395. /// We keep the same payload as a slice of bytes, and prepend it with a ContractId,
  396. /// and then a little-endian u64 to tell the payload's length.
  397. fn serialize_payload(cid: &ContractId, payload: &[u8]) -> Vec<u8> {
  398. let ser_cid = serialize(cid);
  399. let payload_len = payload.len();
  400. let mut out = Vec::with_capacity(ser_cid.len() + 8 + payload_len);
  401. out.extend_from_slice(&ser_cid);
  402. out.extend_from_slice(&(payload_len as u64).to_le_bytes());
  403. out.extend_from_slice(payload);
  404. out
  405. }
  406. }