rune/runtime/stack.rs
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
use core::array;
use core::convert::Infallible;
use core::fmt;
use core::mem::replace;
use core::slice;
use crate::alloc::alloc::Global;
use crate::alloc::prelude::*;
use crate::alloc::{self, Vec};
use crate::runtime::{InstAddress, Output, Value, VmErrorKind};
// This is a bit tricky. We know that `Value::empty()` is `Sync` but we can't
// convince Rust that is the case.
struct AssertSync<T>(T);
unsafe impl<T> Sync for AssertSync<T> {}
static EMPTY: AssertSync<Value> = AssertSync(Value::empty());
/// An error raised when accessing an address on the stack.
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[non_exhaustive]
pub struct StackError {
addr: InstAddress,
}
impl From<Infallible> for StackError {
#[inline]
fn from(value: Infallible) -> Self {
match value {}
}
}
impl fmt::Display for StackError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Tried to access out-of-bounds stack entry {}", self.addr)
}
}
impl core::error::Error for StackError {}
/// An error raised when accessing a slice on the stack.
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[non_exhaustive]
pub struct SliceError {
addr: InstAddress,
len: usize,
stack: usize,
}
impl fmt::Display for SliceError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Tried to access out-of-bounds stack slice {}-{} in 0-{}",
self.addr,
self.addr.offset() + self.len,
self.stack
)
}
}
impl core::error::Error for SliceError {}
pub(crate) enum Pair<'a> {
Same(&'a mut Value),
Pair(&'a mut Value, &'a Value),
}
/// Memory access.
pub trait Memory {
/// Get the slice at the given address with the given length.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::runtime::{Output, Memory, ToValue, VmResult, InstAddress};
///
/// fn sum(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// let mut number = 0;
///
/// for value in vm_try!(stack.slice_at(addr, args)) {
/// number += vm_try!(value.as_integer::<i64>());
/// }
///
/// out.store(stack, number);
/// VmResult::Ok(())
/// }
/// ```
fn slice_at(&self, addr: InstAddress, len: usize) -> Result<&[Value], SliceError>;
/// Access the given slice mutably.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::runtime::{Output, Memory, InstAddress, Value, VmResult};
///
/// fn drop_values(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// for value in vm_try!(stack.slice_at_mut(addr, args)) {
/// *value = Value::empty();
/// }
///
/// out.store(stack, ());
/// VmResult::Ok(())
/// }
/// ```
fn slice_at_mut(&mut self, addr: InstAddress, len: usize) -> Result<&mut [Value], SliceError>;
/// Get a value mutable at the given index from the stack bottom.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::Module;
/// use rune::runtime::{Output, Memory, VmResult, InstAddress};
///
/// fn add_one(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// let mut value = vm_try!(stack.at_mut(addr));
/// let number = vm_try!(value.as_integer::<i64>());
/// *value = vm_try!(rune::to_value(number + 1));
/// out.store(stack, ());
/// VmResult::Ok(())
/// }
/// ```
fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError>;
/// Get the slice at the given address with the given static length.
fn array_at<const N: usize>(&self, addr: InstAddress) -> Result<[&Value; N], SliceError>
where
Self: Sized,
{
let slice = self.slice_at(addr, N)?;
Ok(array::from_fn(|i| &slice[i]))
}
}
impl<M> Memory for &mut M
where
M: Memory + ?Sized,
{
#[inline]
fn slice_at(&self, addr: InstAddress, len: usize) -> Result<&[Value], SliceError> {
(**self).slice_at(addr, len)
}
#[inline]
fn slice_at_mut(&mut self, addr: InstAddress, len: usize) -> Result<&mut [Value], SliceError> {
(**self).slice_at_mut(addr, len)
}
#[inline]
fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError> {
(**self).at_mut(addr)
}
}
impl<const N: usize> Memory for [Value; N] {
fn slice_at(&self, addr: InstAddress, len: usize) -> Result<&[Value], SliceError> {
if len == 0 {
return Ok(&[]);
}
let start = addr.offset();
let Some(values) = start.checked_add(len).and_then(|end| self.get(start..end)) else {
return Err(SliceError {
addr,
len,
stack: N,
});
};
Ok(values)
}
fn slice_at_mut(&mut self, addr: InstAddress, len: usize) -> Result<&mut [Value], SliceError> {
if len == 0 {
return Ok(&mut []);
}
let start = addr.offset();
let Some(values) = start
.checked_add(len)
.and_then(|end| self.get_mut(start..end))
else {
return Err(SliceError {
addr,
len,
stack: N,
});
};
Ok(values)
}
#[inline]
fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError> {
let Some(value) = self.get_mut(addr.offset()) else {
return Err(StackError { addr });
};
Ok(value)
}
}
/// The stack of the virtual machine, where all values are stored.
#[derive(Default, Debug)]
pub struct Stack {
/// The current stack of values.
stack: Vec<Value>,
/// The top of the current stack frame.
///
/// It is not possible to interact with values below this stack frame.
top: usize,
}
impl Stack {
/// Construct a new stack.
pub(crate) const fn new() -> Self {
Self {
stack: Vec::new(),
top: 0,
}
}
/// Access the value at the given frame offset.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::Module;
/// use rune::runtime::{Output, Stack, VmResult, InstAddress};
///
/// fn add_one(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// let value = vm_try!(stack.at(addr).as_integer::<i64>());
/// out.store(stack, value + 1);
/// VmResult::Ok(())
/// }
/// ```
#[inline(always)]
pub fn at(&self, addr: InstAddress) -> &Value {
self.top
.checked_add(addr.offset())
.and_then(|n| self.stack.get(n))
.unwrap_or(&EMPTY.0)
}
/// Get a value mutable at the given index from the stack bottom.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::Module;
/// use rune::runtime::{Output, Stack, VmResult, InstAddress};
///
/// fn add_one(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// let mut value = vm_try!(stack.at_mut(addr));
/// let number = vm_try!(value.as_integer::<i64>());
/// *value = vm_try!(rune::to_value(number + 1));
/// out.store(stack, ());
/// VmResult::Ok(())
/// }
/// ```
pub fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError> {
self.top
.checked_add(addr.offset())
.and_then(|n| self.stack.get_mut(n))
.ok_or(StackError { addr })
}
/// Get the slice at the given address with the given length.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::Module;
/// use rune::runtime::{Output, Stack, ToValue, VmResult, InstAddress};
///
/// fn sum(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// let mut number = 0;
///
/// for value in vm_try!(stack.slice_at(addr, args)) {
/// number += vm_try!(value.as_integer::<i64>());
/// }
///
/// out.store(stack, number);
/// VmResult::Ok(())
/// }
/// ```
pub fn slice_at(&self, addr: InstAddress, len: usize) -> Result<&[Value], SliceError> {
let stack_len = self.stack.len();
if let Some(slice) = inner_slice_at(&self.stack, self.top, addr, len) {
return Ok(slice);
}
Err(slice_error(stack_len, self.top, addr, len))
}
/// Get the mutable slice at the given address with the given length.
///
/// # Examples
///
/// ```
/// use rune::vm_try;
/// use rune::Module;
/// use rune::runtime::{Output, Stack, VmResult, InstAddress};
///
/// fn sum(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> {
/// for value in vm_try!(stack.slice_at_mut(addr, args)) {
/// let number = vm_try!(value.as_integer::<i64>());
/// *value = vm_try!(rune::to_value(number + 1));
/// }
///
/// out.store(stack, ());
/// VmResult::Ok(())
/// }
/// ```
pub fn slice_at_mut(
&mut self,
addr: InstAddress,
len: usize,
) -> Result<&mut [Value], SliceError> {
let stack_len = self.stack.len();
if let Some(slice) = inner_slice_at_mut(&mut self.stack, self.top, addr, len) {
return Ok(slice);
}
Err(slice_error(stack_len, self.top, addr, len))
}
/// The current top address of the stack.
#[inline]
pub(crate) const fn addr(&self) -> InstAddress {
InstAddress::new(self.stack.len().saturating_sub(self.top))
}
/// Try to resize the stack with space for the given size.
pub(crate) fn resize(&mut self, size: usize) -> alloc::Result<()> {
if size == 0 {
return Ok(());
}
self.stack.try_resize_with(self.top + size, Value::empty)?;
Ok(())
}
/// Construct a new stack with the given capacity pre-allocated.
pub(crate) fn with_capacity(capacity: usize) -> alloc::Result<Self> {
Ok(Self {
stack: Vec::try_with_capacity(capacity)?,
top: 0,
})
}
/// Perform a raw access over the stack.
///
/// This ignores [top] and will just check that the given slice
/// index is within range.
///
/// [top]: Self::top()
#[cfg(feature = "cli")]
pub(crate) fn get<I>(&self, index: I) -> Option<&<I as slice::SliceIndex<[Value]>>::Output>
where
I: slice::SliceIndex<[Value]>,
{
self.stack.get(index)
}
/// Push a value onto the stack.
pub(crate) fn push<T>(&mut self, value: T) -> alloc::Result<()>
where
T: TryInto<Value, Error: Into<alloc::Error>>,
{
self.stack.try_push(value.try_into().map_err(Into::into)?)?;
Ok(())
}
/// Truncate the stack at the given address.
pub(crate) fn truncate(&mut self, addr: InstAddress) {
if let Some(len) = self.top.checked_add(addr.offset()) {
self.stack.truncate(len);
}
}
/// Drain the current stack down to the current stack bottom.
pub(crate) fn drain(&mut self) -> impl DoubleEndedIterator<Item = Value> + '_ {
self.stack.drain(self.top..)
}
/// Clear the current stack.
pub(crate) fn clear(&mut self) {
self.stack.clear();
self.top = 0;
}
/// Get the offset that corresponds to the bottom of the stack right now.
///
/// The stack is partitioned into call frames, and once we enter a call
/// frame the bottom of the stack corresponds to the bottom of the current
/// call frame.
#[cfg_attr(not(feature = "tracing"), allow(unused))]
pub(crate) const fn top(&self) -> usize {
self.top
}
/// Get the length of the stack.
#[cfg_attr(not(feature = "tracing"), allow(unused))]
pub(crate) const fn len(&self) -> usize {
self.stack.len()
}
/// Swap the value at position a with the value at position b.
pub(crate) fn swap(&mut self, a: InstAddress, b: InstAddress) -> Result<(), StackError> {
if a == b {
return Ok(());
}
let a = self
.top
.checked_add(a.offset())
.filter(|&n| n < self.stack.len())
.ok_or(StackError { addr: a })?;
let b = self
.top
.checked_add(b.offset())
.filter(|&n| n < self.stack.len())
.ok_or(StackError { addr: b })?;
self.stack.swap(a, b);
Ok(())
}
/// Modify stack top by subtracting the given count from it while checking
/// that it is in bounds of the stack.
///
/// This is used internally when returning from a call frame.
///
/// Returns the old stack top.
#[tracing::instrument(skip_all)]
pub(crate) fn swap_top(&mut self, addr: InstAddress, len: usize) -> Result<usize, VmErrorKind> {
let old_len = self.stack.len();
if len == 0 {
return Ok(replace(&mut self.top, old_len));
}
let Some(start) = self.top.checked_add(addr.offset()) else {
return Err(VmErrorKind::StackError {
error: StackError { addr },
});
};
let Some(new_len) = old_len.checked_add(len) else {
return Err(VmErrorKind::StackError {
error: StackError { addr },
});
};
if old_len < start + len {
return Err(VmErrorKind::StackError {
error: StackError { addr },
});
}
self.stack.try_reserve(len)?;
// SAFETY: We've ensured that the collection has space for the new
// values. It is also guaranteed to be non-overlapping.
unsafe {
let ptr = self.stack.as_mut_ptr();
let from = slice::from_raw_parts_mut(ptr.add(start), len);
for (value, n) in from.iter_mut().zip(old_len..) {
ptr.add(n).write(replace(value, Value::empty()));
}
self.stack.set_len(new_len);
}
Ok(replace(&mut self.top, old_len))
}
/// Pop the current stack top and modify it to a different one.
#[tracing::instrument(skip_all)]
pub(crate) fn pop_stack_top(&mut self, top: usize) {
tracing::trace!(stack = self.stack.len(), self.top);
self.stack.truncate(self.top);
self.top = top;
}
/// Copy the value at the given address to the output.
pub(crate) fn copy(&mut self, from: InstAddress, out: Output) -> Result<(), StackError> {
let Some(to) = out.as_addr() else {
return Ok(());
};
if from == to {
return Ok(());
}
let from = self.top.wrapping_add(from.offset());
let to = self.top.wrapping_add(to.offset());
if from.max(to) >= self.stack.len() {
return Err(StackError {
addr: InstAddress::new(from.max(to).wrapping_sub(self.top)),
});
}
// SAFETY: We've checked that both addresses are in-bound and different
// just above.
unsafe {
let ptr = self.stack.as_mut_ptr();
(*ptr.add(to)).clone_from(&*ptr.add(from).cast_const());
}
Ok(())
}
/// Get a pair of addresses.
pub(crate) fn pair(&mut self, a: InstAddress, b: InstAddress) -> Result<Pair<'_>, StackError> {
if a == b {
return Ok(Pair::Same(self.at_mut(a)?));
}
let a = self
.top
.checked_add(a.offset())
.filter(|&n| n < self.stack.len())
.ok_or(StackError { addr: a })?;
let b = self
.top
.checked_add(b.offset())
.filter(|&n| n < self.stack.len())
.ok_or(StackError { addr: b })?;
let pair = unsafe {
let ptr = self.stack.as_mut_ptr();
Pair::Pair(&mut *ptr.add(a), &*ptr.add(b).cast_const())
};
Ok(pair)
}
}
impl Memory for Stack {
#[inline]
fn slice_at(&self, addr: InstAddress, len: usize) -> Result<&[Value], SliceError> {
Stack::slice_at(self, addr, len)
}
#[inline]
fn slice_at_mut(&mut self, addr: InstAddress, len: usize) -> Result<&mut [Value], SliceError> {
Stack::slice_at_mut(self, addr, len)
}
#[inline]
fn at_mut(&mut self, addr: InstAddress) -> Result<&mut Value, StackError> {
Stack::at_mut(self, addr)
}
}
#[inline(always)]
fn inner_slice_at(values: &[Value], top: usize, addr: InstAddress, len: usize) -> Option<&[Value]> {
if len == 0 {
return Some(&[]);
}
let start = top.checked_add(addr.offset())?;
let end = start.checked_add(len)?;
values.get(start..end)
}
#[inline(always)]
fn inner_slice_at_mut(
values: &mut [Value],
top: usize,
addr: InstAddress,
len: usize,
) -> Option<&mut [Value]> {
if len == 0 {
return Some(&mut []);
}
let start = top.checked_add(addr.offset())?;
let end = start.checked_add(len)?;
values.get_mut(start..end)
}
#[inline(always)]
fn slice_error(stack: usize, bottom: usize, addr: InstAddress, len: usize) -> SliceError {
SliceError {
addr,
len,
stack: stack.saturating_sub(bottom),
}
}
impl TryClone for Stack {
fn try_clone(&self) -> alloc::Result<Self> {
Ok(Self {
stack: self.stack.try_clone()?,
top: self.top,
})
}
}
impl TryFromIteratorIn<Value, Global> for Stack {
#[inline]
fn try_from_iter_in<T: IntoIterator<Item = Value>>(
iter: T,
alloc: Global,
) -> alloc::Result<Self> {
Ok(Self {
stack: iter.into_iter().try_collect_in(alloc)?,
top: 0,
})
}
}
impl From<Vec<Value>> for Stack {
fn from(stack: Vec<Value>) -> Self {
Self { stack, top: 0 }
}
}