From 9472c1cec2291eb233962daa1a54cb8d21acd7a8 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 18 Jul 2022 10:49:58 +0200 Subject: [PATCH] Fixes memory issue --- src/lib.rs | 1 + .../wasm/export_registry/mod.rs | 6 ++---- src/script_implementations/wasm/script_resolver.rs | 11 +++++------ tests/common/library_loader.rs | 10 +++++----- tests/main.rs | 2 +- types.toml | 5 +++++ 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1eb342e..0964544 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ #![feature(is_some_with)] #![feature(core_ffi_c)] #![feature(new_uninit)] +#![feature(get_mut_unchecked)] //! PkmnLib //! PkmnLib is a full featured implementation of Pokemon. while currently focused on implementing diff --git a/src/script_implementations/wasm/export_registry/mod.rs b/src/script_implementations/wasm/export_registry/mod.rs index 1f704da..e001740 100644 --- a/src/script_implementations/wasm/export_registry/mod.rs +++ b/src/script_implementations/wasm/export_registry/mod.rs @@ -1,9 +1,7 @@ use core::ffi::c_char; -use std::borrow::Cow; use std::ffi::CString; -use std::mem::{align_of, size_of}; +use std::mem::align_of; -use wasmer::wasmparser::Data; use wasmer::{Exports, Function, Store}; use crate::dynamic_data::DynamicLibrary; @@ -82,7 +80,7 @@ fn const_string_get_hash(env: &WebAssemblyEnv, string_key: ExternRef) fn const_string_get_str(env: &WebAssemblyEnv, string_key: ExternRef) -> u32 { let string_key = string_key.value(env).str(); - let mut s: CString = CString::new(string_key.as_bytes()).unwrap(); + let s: CString = CString::new(string_key.as_bytes()).unwrap(); let wasm_string_ptr = env .resolver() .allocate_mem(string_key.len() as u32, align_of::() as u32); diff --git a/src/script_implementations/wasm/script_resolver.rs b/src/script_implementations/wasm/script_resolver.rs index af75f0c..a06f8b0 100644 --- a/src/script_implementations/wasm/script_resolver.rs +++ b/src/script_implementations/wasm/script_resolver.rs @@ -1,6 +1,4 @@ -use std::any::{Any, TypeId}; use std::fmt::{Debug, Formatter}; -use std::pin::Pin; use std::sync::Arc; use hashbrown::{HashMap, HashSet}; @@ -46,7 +44,7 @@ struct ScriptCapabilitiesKey { impl WebAssemblyScriptResolver { /// Instantiates a new WebAssemblyScriptResolver. - pub fn new() -> Self { + pub fn new() -> Box { let config = Cranelift::default(); let mut features = Features::new(); features.multi_value = true; @@ -54,7 +52,7 @@ impl WebAssemblyScriptResolver { let universal = Universal::new(config).features(features); let engine = universal.engine(); let store = Store::new(&engine); - Self { + let s = Self { engine, store, module: Default::default(), @@ -69,7 +67,8 @@ impl WebAssemblyScriptResolver { extern_ref_pointers: Default::default(), extern_ref_pointers_lookup: Default::default(), extern_ref_type_lookup: Default::default(), - } + }; + Box::new(s) } /// Load a compiled WASM module. @@ -146,7 +145,7 @@ impl WebAssemblyScriptResolver { /// its proper value, validates its type, and returns the value. pub fn get_extern_ref_value>(&self, index: u32) -> &T { let read_guard = self.extern_ref_pointers.read(); - let ptr = read_guard.get(index as usize).unwrap(); + let ptr = read_guard.get((index - 1) as usize).unwrap(); let expected_type_id = self.extern_ref_type_lookup.read()[&ptr]; if expected_type_id != T::id().0 { panic!("Extern ref was accessed with wrong type"); diff --git a/tests/common/library_loader.rs b/tests/common/library_loader.rs index 94ffe73..b0e4dcb 100644 --- a/tests/common/library_loader.rs +++ b/tests/common/library_loader.rs @@ -9,10 +9,10 @@ use project_root::get_project_root; use serde_json::Value; use pkmn_lib::defines::LevelInt; +use pkmn_lib::dynamic_data::DynamicLibrary; use pkmn_lib::dynamic_data::Gen7BattleStatCalculator; use pkmn_lib::dynamic_data::Gen7DamageLibrary; use pkmn_lib::dynamic_data::Gen7MiscLibrary; -use pkmn_lib::dynamic_data::{DynamicLibrary, EmptyScriptResolver}; use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver; use pkmn_lib::static_data::{ Ability, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, Item, @@ -34,14 +34,14 @@ pub fn load_library() -> DynamicLibrary { load_moves(&path, &mut data); load_species(&path, &mut data); let mut resolver = WebAssemblyScriptResolver::new(); - load_wasm(&path, &mut resolver); + load_wasm(&path, resolver.as_mut()); let dynamic = DynamicLibrary::new( data, Box::new(Gen7BattleStatCalculator {}), Box::new(Gen7DamageLibrary::new(false)), Box::new(Gen7MiscLibrary::new()), - Box::new(resolver), + resolver, ); dynamic } @@ -276,10 +276,10 @@ pub fn load_species(path: &String, library: &mut StaticData) { } fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) { - let mut file = File::open(path.to_string() + "gen7_scripts_rs.wasm").unwrap(); + let file = File::open(path.to_string() + "gen7_scripts_rs.wasm").unwrap(); let mut reader = BufReader::new(file); let mut buffer = Vec::new(); - reader.read_to_end(&mut buffer); + reader.read_to_end(&mut buffer).unwrap(); library.load_wasm_from_bytes(&buffer); } diff --git a/tests/main.rs b/tests/main.rs index 647e0bb..5f8c226 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -30,7 +30,7 @@ fn get_library<'a>() -> &'a DynamicLibrary { #[cfg_attr(miri, ignore)] fn validate_library_load() { let start_time = chrono::Utc::now(); - let lib = library_loader::load_library(); + library_loader::load_library(); let end_time = chrono::Utc::now(); println!("Built library in {} ms", (end_time - start_time).num_milliseconds()); } diff --git a/types.toml b/types.toml index 52ff811..58ae822 100644 --- a/types.toml +++ b/types.toml @@ -3,3 +3,8 @@ MoveData = 1 StringKey = 2 DynamicLibrary = 3 StaticData = 4 +DynamicLibrary = 0 +MoveLibrary = 1 +StaticData = 2 +MoveData = 3 +StringKey = 4