1use anyhow::Result;
2use bitflags::bitflags;
3use num_bigint::BigInt;
4#[cfg(feature = "tracing")]
5use tracing::instrument;
6use tycho_types::cell::*;
7use tycho_types::error::Error;
8
9use crate::cont::{
10 AgainCont, ArgContExt, ControlData, ControlRegs, ExcQuitCont, OrdCont, QuitCont, RcCont,
11 RepeatCont, UntilCont, WhileCont,
12};
13use crate::dispatch::DispatchTable;
14use crate::error::{VmException, VmResult};
15use crate::gas::{GasConsumer, GasParams, LibraryProvider, NoLibraries, ParentGasConsumer};
16use crate::instr::{codepage, codepage0};
17use crate::saferc::SafeRc;
18use crate::smc_info::{SmcInfo, VmVersion};
19use crate::stack::{RcStackValue, Stack};
20use crate::util::OwnedCellSlice;
21
22#[derive(Default)]
24pub struct VmStateBuilder<'a> {
25 pub code: Option<OwnedCellSlice>,
26 pub data: Option<Cell>,
27 pub stack: SafeRc<Stack>,
28 pub libraries: Option<&'a dyn LibraryProvider>,
29 pub c7: Option<SafeRc<Vec<RcStackValue>>>,
30 pub gas: GasParams,
31 pub init_selector: InitSelectorParams,
32 pub version: Option<VmVersion>,
33 pub modifiers: BehaviourModifiers,
34 pub debug: Option<&'a mut dyn std::fmt::Write>,
35}
36
37impl<'a> VmStateBuilder<'a> {
38 pub fn new() -> Self {
39 Self::default()
40 }
41
42 pub fn build(mut self) -> VmState<'a> {
43 static NO_LIBRARIES: NoLibraries = NoLibraries;
44
45 let quit0 = QUIT0.with(SafeRc::clone);
46 let quit1 = QUIT1.with(SafeRc::clone);
47 let cp = codepage0();
48
49 let (code, throw_on_code_access) = match self.code {
50 Some(code) => (code, false),
51 None => (Default::default(), true),
52 };
53
54 let c3 = match self.init_selector {
55 InitSelectorParams::None => QUIT11.with(SafeRc::clone).into_dyn_cont(),
56 InitSelectorParams::UseCode { push0 } => {
57 if push0 {
58 vm_log_trace!("implicit PUSH 0 at start");
59 SafeRc::make_mut(&mut self.stack)
60 .items
61 .push(Stack::make_zero());
62 }
63 SafeRc::from(OrdCont::simple(code.clone(), cp.id()))
64 }
65 };
66
67 VmState {
68 cr: ControlRegs {
69 c: [
70 Some(quit0.clone().into_dyn_cont()),
71 Some(quit1.clone().into_dyn_cont()),
72 Some(EXC_QUIT.with(SafeRc::clone).into_dyn_cont()),
73 Some(c3),
74 ],
75 d: [
76 Some(self.data.unwrap_or_default()),
77 Some(Cell::empty_cell()),
78 ],
79 c7: Some(self.c7.unwrap_or_default()),
80 },
81 code,
82 throw_on_code_access,
83 stack: self.stack,
84 committed_state: None,
85 steps: 0,
86 quit0,
87 quit1,
88 gas: GasConsumer::with_libraries(self.gas, self.libraries.unwrap_or(&NO_LIBRARIES)),
89 cp,
90 debug: self.debug,
91 modifiers: self.modifiers,
92 version: self.version.unwrap_or(VmState::DEFAULT_VERSION),
93 parent: None,
94 }
95 }
96
97 pub fn with_libraries<T: LibraryProvider>(mut self, libraries: &'a T) -> Self {
98 self.libraries = Some(libraries);
99 self
100 }
101
102 pub fn with_gas(mut self, gas: GasParams) -> Self {
103 self.gas = gas;
104 self
105 }
106
107 pub fn with_debug<T: std::fmt::Write>(mut self, stderr: &'a mut T) -> Self {
108 self.debug = Some(stderr);
109 self
110 }
111
112 pub fn with_code<T: IntoCode>(mut self, code: T) -> Self {
113 self.code = code.into_code().ok();
114 self
115 }
116
117 pub fn with_data(mut self, data: Cell) -> Self {
118 self.data = Some(data);
119 self
120 }
121
122 pub fn with_init_selector(mut self, push0: bool) -> Self {
123 self.init_selector = InitSelectorParams::UseCode { push0 };
124 self
125 }
126
127 pub fn with_stack<I: IntoIterator<Item = RcStackValue>>(mut self, values: I) -> Self {
128 self.stack = SafeRc::new(values.into_iter().collect());
129 self
130 }
131
132 pub fn with_raw_stack(mut self, stack: SafeRc<Stack>) -> Self {
133 self.stack = stack;
134 self
135 }
136
137 pub fn with_smc_info<T: SmcInfo>(mut self, info: T) -> Self {
138 if self.version.is_none() {
139 self.version = Some(info.version());
140 }
141 self.c7 = Some(info.build_c7());
142 self
143 }
144
145 pub fn with_modifiers(mut self, modifiers: BehaviourModifiers) -> Self {
146 self.modifiers = modifiers;
147 self
148 }
149
150 pub fn with_version(mut self, version: VmVersion) -> Self {
151 self.version = Some(version);
152 self
153 }
154}
155
156pub trait IntoCode {
158 fn into_code(self) -> Result<OwnedCellSlice, Error>;
159}
160
161impl<T: IntoCode> IntoCode for Option<T> {
162 fn into_code(self) -> Result<OwnedCellSlice, Error> {
163 match self {
164 Some(code) => code.into_code(),
165 None => Err(Error::CellUnderflow),
166 }
167 }
168}
169
170impl IntoCode for CellSliceParts {
171 #[inline]
172 fn into_code(self) -> Result<OwnedCellSlice, Error> {
173 Ok(OwnedCellSlice::from(self))
174 }
175}
176
177impl IntoCode for OwnedCellSlice {
178 #[inline]
179 fn into_code(self) -> Result<OwnedCellSlice, Error> {
180 Ok(self)
181 }
182}
183
184impl IntoCode for Cell {
185 fn into_code(mut self) -> Result<OwnedCellSlice, Error> {
186 let descriptor = self.descriptor();
187 if descriptor.is_exotic() {
188 if descriptor.is_library() {
189 self = CellBuilder::build_from(self).unwrap();
191 } else {
192 return Err(Error::UnexpectedExoticCell);
194 }
195 }
196
197 Ok(OwnedCellSlice::new_allow_exotic(self))
198 }
199}
200
201#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
203pub enum InitSelectorParams {
204 #[default]
205 None,
206 UseCode {
207 push0: bool,
208 },
209}
210
211pub struct VmState<'a> {
213 pub code: OwnedCellSlice,
214 pub throw_on_code_access: bool,
215 pub stack: SafeRc<Stack>,
216 pub cr: ControlRegs,
217 pub committed_state: Option<CommittedState>,
218 pub steps: u64,
219 pub quit0: SafeRc<QuitCont>,
220 pub quit1: SafeRc<QuitCont>,
221 pub gas: GasConsumer<'a>,
222 pub cp: &'static DispatchTable,
223 pub debug: Option<&'a mut dyn std::fmt::Write>,
224 pub modifiers: BehaviourModifiers,
225 pub version: VmVersion,
226 pub parent: Option<Box<ParentVmState<'a>>>,
227}
228
229pub struct ParentVmState<'a> {
231 pub code: OwnedCellSlice,
233 pub stack: SafeRc<Stack>,
235 pub cr: ControlRegs,
237 pub committed_state: Option<CommittedState>,
239 pub steps: u64,
241 pub quit0: SafeRc<QuitCont>,
243 pub quit1: SafeRc<QuitCont>,
245 pub gas: ParentGasConsumer<'a>,
247 pub cp: &'static DispatchTable,
249
250 pub return_data: bool,
252 pub return_actions: bool,
254 pub return_gas: bool,
256 pub return_values: Option<u32>,
260
261 pub parent: Option<Box<ParentVmState<'a>>>,
263}
264
265impl<'a> VmState<'a> {
266 pub const DEFAULT_VERSION: VmVersion = VmVersion::LATEST_TON;
267
268 pub const MAX_DATA_DEPTH: u16 = 512;
269
270 thread_local! {
271 static EMPTY_STACK: SafeRc<Stack> = SafeRc::new(Default::default());
272 }
273
274 pub fn builder() -> VmStateBuilder<'a> {
275 VmStateBuilder::default()
276 }
277
278 #[cfg_attr(
279 feature = "tracing",
280 instrument(
281 level = "trace",
282 name = "vm_step",
283 fields(n = self.steps),
284 skip_all,
285 )
286 )]
287 pub fn step(&mut self) -> VmResult<i32> {
288 #[cfg(feature = "tracing")]
289 if self
290 .modifiers
291 .log_mask
292 .intersects(VmLogMask::DUMP_STACK.union(VmLogMask::DUMP_STACK_VERBOSE))
293 {
294 vm_log_stack!(
295 self.stack,
296 self.modifiers
297 .log_mask
298 .contains(VmLogMask::DUMP_STACK_VERBOSE)
299 );
300 }
301
302 self.steps += 1;
303 if !self.code.range().is_data_empty() {
304 #[cfg(feature = "tracing")]
305 if self.modifiers.log_mask.contains(VmLogMask::EXEC_LOCATION) {
306 let Size { bits, refs } = self.code.range().offset();
307 vm_log_exec_location!(self.code.cell(), bits, refs);
308 }
309
310 self.cp.dispatch(self)
311 } else if !self.code.range().is_refs_empty() {
312 vm_log_op!("implicit JMPREF");
313
314 let next_cell = self.code.apply().get_reference_cloned(0)?;
315
316 #[cfg(feature = "tracing")]
317 if self.modifiers.log_mask.contains(VmLogMask::EXEC_LOCATION) {
318 vm_log_exec_location!(next_cell, 0u16, 0u8);
319 }
320
321 self.gas.try_consume_implicit_jmpref_gas()?;
322 let code = self.gas.load_cell_as_slice(next_cell, LoadMode::Full)?;
323
324 let cont = SafeRc::from(OrdCont::simple(code, self.cp.id()));
325 self.jump(cont)
326 } else {
327 vm_log_op!("implicit RET");
328
329 self.gas.try_consume_implicit_ret_gas()?;
330 self.ret()
331 }
332 }
333
334 pub fn run(&mut self) -> i32 {
335 if self.throw_on_code_access {
336 return VmException::Fatal as u8 as i32;
338 }
339
340 let mut res = 0;
341 loop {
342 res = match self.restore_parent(!res) {
343 Ok(()) => self.run_inner(),
344 Err(OutOfGas) => {
345 self.steps += 1;
346 self.throw_out_of_gas()
347 }
348 };
349
350 if self.parent.is_none() {
351 #[cfg(feature = "tracing")]
352 if self.modifiers.log_mask.contains(VmLogMask::DUMP_C5)
353 && let Some(committed) = &self.committed_state
354 {
355 vm_log_c5!(committed.c5.as_ref());
356 }
357 break res;
358 }
359 }
360 }
361
362 fn run_inner(&mut self) -> i32 {
363 let mut res = 0;
364 while res == 0 {
365 let step_res = self.step();
366
367 #[cfg(feature = "tracing")]
368 if self.modifiers.log_mask.contains(VmLogMask::GAS_REMAINING) {
369 vm_log_gas_remaining!(self.gas.remaining());
370 }
371
372 #[cfg(feature = "tracing")]
373 if self.modifiers.log_mask.contains(VmLogMask::GAS_CONSUMED) {
374 vm_log_gas_consumed!(self.gas.consumed());
375 }
376
377 res = match step_res {
378 Ok(res) => res,
379 Err(e) if e.is_out_of_gas() => {
380 self.steps += 1;
381 self.throw_out_of_gas()
382 }
383 Err(e) => {
384 let exception = e.as_exception();
385 vm_log_trace!("handling exception {exception:?}: {e:?}");
386
387 self.steps += 1;
388 match self.throw_exception(exception as i32) {
389 Ok(res) => res,
390 Err(e) if e.is_out_of_gas() => {
391 self.steps += 1;
392 self.throw_out_of_gas()
393 }
394 Err(e) => {
395 vm_log_trace!("double exception {exception:?}: {e:?}");
396 return exception.as_exit_code();
397 }
398 }
399 }
400 };
401 }
402
403 if res | 1 == -1 && !self.try_commit() {
405 vm_log_trace!("automatic commit failed");
406 self.stack = SafeRc::new(Stack {
407 items: vec![Stack::make_zero()],
408 });
409 return VmException::CellOverflow.as_exit_code();
410 }
411
412 res
413 }
414
415 pub fn try_commit(&mut self) -> bool {
416 if let (Some(c4), Some(c5)) = (&self.cr.d[0], &self.cr.d[1])
417 && c4.level() == 0
418 && c5.level() == 0
419 && c4.repr_depth() <= Self::MAX_DATA_DEPTH
420 && c5.repr_depth() <= Self::MAX_DATA_DEPTH
421 {
422 self.committed_state = Some(CommittedState {
423 c4: c4.clone(),
424 c5: c5.clone(),
425 });
426 return true;
427 }
428
429 false
430 }
431
432 pub fn force_commit(&mut self) -> Result<(), Error> {
433 if self.try_commit() {
434 Ok(())
435 } else {
436 Err(Error::CellOverflow)
437 }
438 }
439
440 pub fn take_stack(&mut self) -> SafeRc<Stack> {
441 std::mem::replace(&mut self.stack, Self::EMPTY_STACK.with(SafeRc::clone))
442 }
443
444 pub fn ref_to_cont(&mut self, code: Cell) -> VmResult<RcCont> {
445 let code = self.gas.load_cell_as_slice(code, LoadMode::Full)?;
446 Ok(SafeRc::from(OrdCont::simple(code, self.cp.id())))
447 }
448
449 pub fn c1_envelope_if(&mut self, cond: bool, cont: RcCont, save: bool) -> RcCont {
450 if cond {
451 self.c1_envelope(cont, save)
452 } else {
453 cont
454 }
455 }
456
457 pub fn c1_envelope(&mut self, mut cont: RcCont, save: bool) -> RcCont {
458 if save {
459 if cont.get_control_data().is_none() {
460 let mut c = ArgContExt {
461 data: Default::default(),
462 ext: cont,
463 };
464 c.data.save.define_c0(&self.cr.c[0]);
465 c.data.save.define_c1(&self.cr.c[1]);
466
467 cont = SafeRc::from(c);
468 } else {
469 let cont = SafeRc::make_mut(&mut cont);
470 if let Some(data) = cont.get_control_data_mut() {
471 data.save.define_c0(&self.cr.c[0]);
472 data.save.define_c1(&self.cr.c[1]);
473 }
474 }
475 }
476 self.cr.c[1] = Some(cont.clone());
477 cont
478 }
479
480 pub fn c1_save_set(&mut self) {
481 let [c0, c1, ..] = &mut self.cr.c;
482
483 if let Some(c0) = c0 {
484 if c0.get_control_data().is_none() {
485 let mut c = ArgContExt {
486 data: Default::default(),
487 ext: c0.clone(),
488 };
489 c.data.save.define_c1(c1);
490 *c0 = SafeRc::from(c);
491 } else {
492 let c0 = SafeRc::make_mut(c0);
493 if let Some(data) = c0.get_control_data_mut() {
494 data.save.define_c1(c1);
495 }
496 }
497 }
498
499 c1.clone_from(c0);
500 }
501
502 pub fn extract_cc(
503 &mut self,
504 mode: SaveCr,
505 stack_copy: Option<u16>,
506 nargs: Option<u16>,
507 ) -> VmResult<RcCont> {
508 let new_stack = match stack_copy {
509 Some(0) => None,
510 Some(n) if (n as usize) != self.stack.depth() => {
511 let stack = ok!(SafeRc::make_mut(&mut self.stack)
512 .split_top(n as _)
513 .map(Some));
514 self.gas.try_consume_stack_gas(stack.as_ref())?;
515 stack
516 }
517 _ => Some(self.take_stack()),
518 };
519
520 let mut res = OrdCont {
521 code: std::mem::take(&mut self.code),
522 data: ControlData {
523 nargs,
524 stack: Some(self.take_stack()),
525 save: Default::default(),
526 cp: Some(self.cp.id()),
527 },
528 };
529 if let Some(new_stack) = new_stack {
530 self.stack = new_stack;
531 }
532
533 if mode.contains(SaveCr::C0) {
534 res.data.save.c[0] = self.cr.c[0].replace(self.quit0.clone().into_dyn_cont());
535 }
536 if mode.contains(SaveCr::C1) {
537 res.data.save.c[1] = self.cr.c[1].replace(self.quit1.clone().into_dyn_cont());
538 }
539 if mode.contains(SaveCr::C2) {
540 res.data.save.c[2] = self.cr.c[2].take();
541 }
542
543 Ok(SafeRc::from(res))
544 }
545
546 pub fn throw_exception(&mut self, n: i32) -> VmResult<i32> {
547 self.stack = SafeRc::new(Stack {
548 items: vec![Stack::make_zero(), SafeRc::new_dyn_value(BigInt::from(n))],
549 });
550 self.code = Default::default();
551 self.gas.try_consume_exception_gas()?;
552 let Some(c2) = self.cr.c[2].clone() else {
553 vm_bail!(InvalidOpcode);
554 };
555 self.jump(c2)
556 }
557
558 pub fn throw_exception_with_arg(&mut self, n: i32, arg: RcStackValue) -> VmResult<i32> {
559 self.stack = SafeRc::new(Stack {
560 items: vec![arg, SafeRc::new_dyn_value(BigInt::from(n))],
561 });
562 self.code = Default::default();
563 self.gas.try_consume_exception_gas()?;
564 let Some(c2) = self.cr.c[2].clone() else {
565 vm_bail!(InvalidOpcode);
566 };
567 self.jump(c2)
568 }
569
570 pub fn throw_out_of_gas(&mut self) -> i32 {
571 let consumed = self.gas.consumed();
572 vm_log_trace!(
573 "out of gas: consumed={consumed}, limit={}",
574 self.gas.limit(),
575 );
576 self.stack = SafeRc::new(Stack {
577 items: vec![SafeRc::new_dyn_value(BigInt::from(consumed))],
578 });
579
580 VmException::OutOfGas as u8 as i32
582 }
583
584 pub fn call(&mut self, cont: RcCont) -> VmResult<i32> {
585 if let Some(control_data) = cont.get_control_data() {
586 if control_data.save.c[0].is_some() {
587 return self.jump(cont);
589 }
590 if control_data.stack.is_some() || control_data.nargs.is_some() {
591 return self.call_ext(cont, None, None);
594 }
595 }
596
597 let mut ret = OrdCont::simple(std::mem::take(&mut self.code), self.cp.id());
599 ret.data.save.c[0] = self.cr.c[0].take();
600 self.cr.c[0] = Some(SafeRc::from(ret));
601
602 self.do_jump_to(cont)
604 }
605
606 pub fn call_ext(
607 &mut self,
608 mut cont: RcCont,
609 pass_args: Option<u16>,
610 ret_args: Option<u16>,
611 ) -> VmResult<i32> {
612 let (new_stack, c0) = if let Some(control_data) = cont.get_control_data() {
613 if control_data.save.c[0].is_some() {
614 return self.jump_ext(cont, pass_args);
616 }
617
618 let current_depth = self.stack.depth();
619 vm_ensure!(
620 pass_args.unwrap_or_default() as usize <= current_depth
621 && control_data.nargs.unwrap_or_default() as usize <= current_depth,
622 StackUnderflow(std::cmp::max(
623 pass_args.unwrap_or_default(),
624 control_data.nargs.unwrap_or_default()
625 ) as _)
626 );
627
628 if let Some(pass_args) = pass_args {
629 vm_ensure!(
630 control_data.nargs.unwrap_or_default() <= pass_args,
631 StackUnderflow(pass_args as _)
632 );
633 }
634
635 let old_c0 = self.cr.c[0].take();
636 self.cr.preclear(&control_data.save);
637
638 let (copy, skip) = match (pass_args, control_data.nargs) {
639 (Some(pass_args), Some(copy)) => (Some(copy as usize), (pass_args - copy) as usize),
640 (Some(pass_args), None) => (Some(pass_args as usize), 0),
641 _ => (None, 0),
642 };
643
644 let new_stack = match SafeRc::get_mut(&mut cont) {
645 Some(cont) => cont
646 .get_control_data_mut()
647 .and_then(|control_data| control_data.stack.take()),
648 None => cont
649 .get_control_data()
650 .and_then(|control_data| control_data.stack.clone()),
651 };
652
653 let new_stack = match new_stack {
654 Some(mut new_stack) if !new_stack.items.is_empty() => {
655 let copy = copy.unwrap_or(current_depth);
656
657 let current_stack = SafeRc::make_mut(&mut self.stack);
658 ok!(SafeRc::make_mut(&mut new_stack).move_from_stack(current_stack, copy));
659 ok!(current_stack.pop_many(skip));
660
661 self.gas.try_consume_stack_gas(Some(&new_stack))?;
662
663 new_stack
664 }
665 _ => {
666 if let Some(copy) = copy {
667 let new_stack =
668 ok!(SafeRc::make_mut(&mut self.stack).split_top_ext(copy, skip));
669
670 self.gas.try_consume_stack_gas(Some(&new_stack))?;
671
672 new_stack
673 } else {
674 self.take_stack()
675 }
676 }
677 };
678
679 (new_stack, old_c0)
680 } else {
681 let new_stack = if let Some(pass_args) = pass_args {
683 let new_stack = ok!(SafeRc::make_mut(&mut self.stack).split_top(pass_args as _));
684 self.gas.try_consume_stack_gas(Some(&new_stack))?;
685 new_stack
686 } else {
687 self.take_stack()
688 };
689
690 (new_stack, self.cr.c[0].take())
691 };
692
693 let mut ret = OrdCont {
695 code: std::mem::take(&mut self.code),
696 data: ControlData {
697 save: Default::default(),
698 nargs: ret_args,
699 stack: Some(std::mem::replace(&mut self.stack, new_stack)),
700 cp: Some(self.cp.id()),
701 },
702 };
703 ret.data.save.c[0] = c0;
704 self.cr.c[0] = Some(SafeRc::from(ret));
705
706 self.do_jump_to(cont)
707 }
708
709 pub fn jump(&mut self, cont: RcCont) -> VmResult<i32> {
710 if let Some(cont_data) = cont.get_control_data()
711 && (cont_data.stack.is_some() || cont_data.nargs.is_some())
712 {
713 return self.jump_ext(cont, None);
715 }
716
717 self.do_jump_to(cont)
723 }
724
725 pub fn jump_ext(&mut self, cont: RcCont, pass_args: Option<u16>) -> VmResult<i32> {
726 let cont = ok!(self.adjust_jump_cont(cont, pass_args));
730
731 self.do_jump_to(cont)
733 }
734
735 fn adjust_jump_cont(&mut self, mut cont: RcCont, pass_args: Option<u16>) -> VmResult<RcCont> {
736 if let Some(control_data) = cont.get_control_data() {
737 let current_depth = self.stack.depth();
752 vm_ensure!(
753 pass_args.unwrap_or_default() as usize <= current_depth
754 && control_data.nargs.unwrap_or_default() as usize <= current_depth,
755 StackUnderflow(std::cmp::max(
756 pass_args.unwrap_or_default(),
757 control_data.nargs.unwrap_or_default()
758 ) as usize)
759 );
760
761 if let Some(pass_args) = pass_args {
762 vm_ensure!(
763 control_data.nargs.unwrap_or_default() <= pass_args,
764 StackUnderflow(pass_args as usize)
765 );
766 }
767
768 self.preclear_cr(&control_data.save);
770
771 let next_depth = control_data
773 .nargs
774 .or(pass_args)
775 .map(|n| n as usize)
776 .unwrap_or(current_depth);
777
778 let cont_stack = match SafeRc::get_mut(&mut cont) {
780 None => cont
781 .get_control_data()
782 .and_then(|control_data| control_data.stack.clone()),
783 Some(cont) => cont
784 .get_control_data_mut()
785 .and_then(|control_data| control_data.stack.take()),
786 };
787
788 match cont_stack {
789 Some(mut cont_stack) if !cont_stack.items.is_empty() => {
791 ok!(SafeRc::make_mut(&mut cont_stack)
793 .move_from_stack(SafeRc::make_mut(&mut self.stack), next_depth));
794 self.gas.try_consume_stack_gas(Some(&cont_stack))?;
795
796 self.stack = cont_stack;
797 }
798 _ if next_depth < current_depth => {
800 ok!(SafeRc::make_mut(&mut self.stack).drop_bottom(current_depth - next_depth));
801 self.gas.try_consume_stack_depth_gas(next_depth as _)?;
802 }
803 _ => {}
805 }
806 } else if let Some(pass_args) = pass_args {
807 let Some(depth_diff) = self.stack.depth().checked_sub(pass_args as _) else {
809 vm_bail!(StackUnderflow(pass_args as _));
810 };
811
812 if depth_diff > 0 {
813 ok!(SafeRc::make_mut(&mut self.stack).drop_bottom(depth_diff));
815 self.gas.try_consume_stack_depth_gas(pass_args as _)?;
816 }
817 }
818
819 Ok(cont)
820 }
821
822 fn do_jump_to(&mut self, mut cont: RcCont) -> VmResult<i32> {
823 let mut exit_code = 0;
824 let mut count = 0;
825 while let Some(next) = ok!(SafeRc::into_inner(cont).jump(self, &mut exit_code)) {
826 cont = next;
827 count += 1;
828
829 if count > GasConsumer::FREE_NESTED_CONT_JUMP {
831 self.gas.try_consume(1)?;
832 }
833
834 if let Some(cont_data) = cont.get_control_data()
835 && (cont_data.stack.is_some() || cont_data.nargs.is_some())
836 {
837 cont = ok!(self.adjust_jump_cont(cont, None));
839 }
840 }
841
842 Ok(exit_code)
843 }
844
845 pub fn ret(&mut self) -> VmResult<i32> {
846 let cont = ok!(self.take_c0());
847 self.jump(cont)
848 }
849
850 pub fn ret_ext(&mut self, ret_args: Option<u16>) -> VmResult<i32> {
851 let cont = ok!(self.take_c0());
852 self.jump_ext(cont, ret_args)
853 }
854
855 pub fn ret_alt(&mut self) -> VmResult<i32> {
856 let cont = ok!(self.take_c1());
857 self.jump(cont)
858 }
859
860 pub fn ret_alt_ext(&mut self, ret_args: Option<u16>) -> VmResult<i32> {
861 let cont = ok!(self.take_c1());
862 self.jump_ext(cont, ret_args)
863 }
864
865 pub fn repeat(&mut self, body: RcCont, after: RcCont, n: u32) -> VmResult<i32> {
866 self.jump(if n == 0 {
867 drop(body);
868 after
869 } else {
870 SafeRc::from(RepeatCont {
871 count: n as _,
872 body,
873 after,
874 })
875 })
876 }
877
878 pub fn until(&mut self, body: RcCont, after: RcCont) -> VmResult<i32> {
879 if !body.has_c0() {
880 self.cr.c[0] = Some(SafeRc::from(UntilCont {
881 body: body.clone(),
882 after,
883 }))
884 }
885 self.jump(body)
886 }
887
888 pub fn loop_while(&mut self, cond: RcCont, body: RcCont, after: RcCont) -> VmResult<i32> {
889 if !cond.has_c0() {
890 self.cr.c[0] = Some(SafeRc::from(WhileCont {
891 check_cond: true,
892 cond: cond.clone(),
893 body,
894 after,
895 }));
896 }
897 self.jump(cond)
898 }
899
900 pub fn again(&mut self, body: RcCont) -> VmResult<i32> {
901 self.jump(SafeRc::from(AgainCont { body }))
902 }
903
904 pub fn adjust_cr(&mut self, save: &ControlRegs) {
905 self.cr.merge(save)
906 }
907
908 pub fn preclear_cr(&mut self, save: &ControlRegs) {
909 self.cr.preclear(save)
910 }
911
912 pub fn set_c0(&mut self, cont: RcCont) {
913 self.cr.c[0] = Some(cont);
914 }
915
916 pub fn set_code(&mut self, code: OwnedCellSlice, cp: u16) -> VmResult<()> {
917 self.code = code;
918 self.force_cp(cp)
919 }
920
921 pub fn force_cp(&mut self, cp: u16) -> VmResult<()> {
922 let Some(cp) = codepage(cp) else {
923 vm_bail!(InvalidOpcode);
924 };
925 self.cp = cp;
926 Ok(())
927 }
928
929 fn take_c0(&mut self) -> VmResult<RcCont> {
930 let Some(cont) = self.cr.c[0].replace(self.quit0.clone().into_dyn_cont()) else {
931 vm_bail!(InvalidOpcode);
932 };
933 Ok(cont)
934 }
935
936 fn take_c1(&mut self) -> VmResult<RcCont> {
937 let Some(cont) = self.cr.c[1].replace(self.quit1.clone().into_dyn_cont()) else {
938 vm_bail!(InvalidOpcode);
939 };
940 Ok(cont)
941 }
942
943 fn restore_parent(&mut self, mut res: i32) -> Result<(), OutOfGas> {
944 let Some(parent) = self.parent.take() else {
945 return Ok(());
946 };
947
948 let steps = self.steps;
949
950 self.code = parent.code;
952 let child_stack = std::mem::replace(&mut self.stack, parent.stack);
953 self.cr = parent.cr;
954 let child_committed_state =
955 std::mem::replace(&mut self.committed_state, parent.committed_state);
956 self.steps += parent.steps;
957 self.quit0 = parent.quit0;
958 self.quit1 = parent.quit1;
959 let child_gas = self.gas.restore(parent.gas);
960 self.cp = parent.cp;
961 self.parent = parent.parent;
962
963 vm_log_trace!(
964 "child vm finished: res={res}, steps={steps}, gas={}",
965 child_gas.gas_consumed
966 );
967
968 let amount = std::cmp::min(
976 child_gas.gas_consumed,
977 child_gas.gas_limit.saturating_add(1),
978 );
979 if self.gas.try_consume(amount).is_err() {
980 return Err(OutOfGas);
981 }
982
983 let stack = SafeRc::make_mut(&mut self.stack);
984 let stack = &mut stack.items;
985
986 let returned_values = parent.return_values;
987 let returned_values = if res == 0 || res == 1 {
988 match returned_values {
989 Some(n) if child_stack.depth() < n as usize => {
990 res = VmException::StackUnderflow.as_exit_code();
991 stack.push(Stack::make_zero());
992 0
993 }
994 Some(n) => n as usize,
995 None => child_stack.depth(),
996 }
997 } else {
998 std::cmp::min(child_stack.depth(), 1)
999 };
1000
1001 let gas = &mut self.gas;
1002 if gas.try_consume_stack_depth_gas(returned_values).is_err() {
1003 return Err(OutOfGas);
1004 }
1005
1006 stack.extend_from_slice(&child_stack.items[child_stack.depth() - returned_values..]);
1007
1008 stack.push(SafeRc::new_dyn_value(BigInt::from(res)));
1009
1010 let (committed_c4, committed_c5) = match child_committed_state {
1011 Some(CommittedState { c4, c5 }) => (Some(c4), Some(c5)),
1012 None => (None, None),
1013 };
1014 if parent.return_data {
1015 stack.push(match committed_c4 {
1016 None => Stack::make_null(),
1017 Some(cell) => SafeRc::new_dyn_value(cell),
1018 })
1019 }
1020 if parent.return_actions {
1021 stack.push(match committed_c5 {
1022 None => Stack::make_null(),
1023 Some(cell) => SafeRc::new_dyn_value(cell),
1024 })
1025 }
1026 if parent.return_gas {
1027 stack.push(SafeRc::new_dyn_value(BigInt::from(child_gas.gas_consumed)));
1028 }
1029
1030 Ok(())
1031 }
1032}
1033
1034struct OutOfGas;
1035
1036#[derive(Default, Debug, Clone, Copy)]
1038pub struct BehaviourModifiers {
1039 pub stop_on_accept: bool,
1040 pub chksig_always_succeed: bool,
1041 pub signature_with_id: Option<i32>,
1042 #[cfg(feature = "tracing")]
1043 pub log_mask: VmLogMask,
1044}
1045
1046#[cfg(feature = "tracing")]
1047bitflags! {
1048 #[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
1050 pub struct VmLogMask: u8 {
1051 const MESSAGE = 1 << 0;
1052 const DUMP_STACK = 1 << 1;
1053 const EXEC_LOCATION = 1 << 2;
1054 const GAS_REMAINING = 1 << 3;
1055 const GAS_CONSUMED = 1 << 4;
1056 const DUMP_STACK_VERBOSE = 1 << 5;
1057 const DUMP_C5 = 1 << 6;
1058
1059 const FULL = 0b111111;
1060 }
1061}
1062
1063pub struct CommittedState {
1065 pub c4: Cell,
1067 pub c5: Cell,
1069}
1070
1071bitflags! {
1072 pub struct SaveCr: u8 {
1074 const NONE = 0;
1075
1076 const C0 = 1;
1077 const C1 = 1 << 1;
1078 const C2 = 1 << 2;
1079
1080 const C0_C1 = SaveCr::C0.bits() | SaveCr::C1.bits();
1081 const FULL = SaveCr::C0_C1.bits() | SaveCr::C2.bits();
1082 }
1083}
1084
1085thread_local! {
1086 pub(crate) static QUIT0: SafeRc<QuitCont> = SafeRc::new(QuitCont { exit_code: 0 });
1087 pub(crate) static QUIT1: SafeRc<QuitCont> = SafeRc::new(QuitCont { exit_code: 1 });
1088 pub(crate) static QUIT11: SafeRc<QuitCont> = SafeRc::new(QuitCont { exit_code: 11 });
1089 pub(crate) static EXC_QUIT: SafeRc<ExcQuitCont> = SafeRc::new(ExcQuitCont);
1090}