message: Test and fix limit to Pages calculation
diff: @@ -67,15 +67,38 @@ pub fn make_runtime_store(memory_limit: Size) -> Store {
  fn make_store_with_engine(engine: &dyn Engine, memory_limit: Option<Size>) -> Store {
  match memory_limit {
  Some(limit) => {
  - let capped = std::cmp::max(limit.0, MAX_WASM_MEMORY);
  + let base = BaseTunables::for_target(&Target::default());
  + let tunables = LimitingTunables::new(base, limit_to_pages(limit));
  + Store::new_with_tunables(engine, tunables)
  + }
  + None => Store::new(engine),
  + }
  +}
  +
  +fn limit_to_pages(limit: Size) -> Pages {
  + let capped = std::cmp::min(limit.0, MAX_WASM_MEMORY);
  // round down to ensure the limit is less than or equal to the config
  let pages: u32 = (capped / WASM_PAGE_SIZE)
  .try_into()
  .expect("Value must be <= 4 GiB/64KiB, i.e. fit in uint32. This is a bug.");
  - let base = BaseTunables::for_target(&Target::default());
  - let tunables = LimitingTunables::new(base, Pages(pages));
  - Store::new_with_tunables(engine, tunables)
  + Pages(pages)
  }
  - None => Store::new(engine),
  +
  +#[cfg(test)]
  +mod tests {
  + use super::*;
  +
  + #[test]
  + fn limit_to_pages_works() {
  + // rounds down
  + assert_eq!(limit_to_pages(Size(0)), Pages(0));
  + assert_eq!(limit_to_pages(Size(1)), Pages(0));
  + assert_eq!(limit_to_pages(Size::kibi(63)), Pages(0));
  + assert_eq!(limit_to_pages(Size::kibi(64)), Pages(1));
  + assert_eq!(limit_to_pages(Size::kibi(65)), Pages(1));
  + // caps at 4 GiB
  + assert_eq!(limit_to_pages(Size::gibi(3)), Pages(49152));
  + assert_eq!(limit_to_pages(Size::gibi(4)), Pages(65536));
  + assert_eq!(limit_to_pages(Size::gibi(5)), Pages(65536));
  }
  }
  