Skip to content

Commit

Permalink
Merge pull request #207 from urbit/as/motes
Browse files Browse the repository at this point in the history
Add motes and `NockStack` allocation safety
  • Loading branch information
matthew-levan authored Feb 8, 2024
2 parents 0f5863d + 077af35 commit 7372095
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 283 deletions.
10 changes: 5 additions & 5 deletions DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ bazel-bin/pkg/vere/urbit -F zod
#### Pills

Ares development and testing, unlike regular development and ship operation, currently requires careful control over what pill is used to launch a ship. Currently, there are several pills available in `resources/pills/`:
* baby.pill: an extremely minimal Arvo-shaped core and Hoon standard library (`~wicdev-wisryt` [streamed a
- **baby.pill**: an extremely minimal Arvo-shaped core and Hoon standard library (`~wicdev-wisryt` [streamed a
video of its development](https://youtu.be/fOVhCx1a-9A))
* toddler.pill: a slightly more complex Arvo and Hoon than `baby`, which runs slow recursive operations for testing jets
* azimuth.pill: a pill that processes an Azimuth snapshot
* full.pill: the complete Urbit `v2.11` pill
* slim.pill: a slimmed down version of the Urbit `v2.11` pill that has had every desk and agent not necessary for booting to dojo removed
- **toddler.pill**: a slightly more complex Arvo and Hoon than `baby`, which runs slow recursive operations for testing jets
- **azimuth.pill**: a pill that processes an Azimuth snapshot
- **full.pill**: the complete Urbit `v2.11` pill
- **slim.pill**: a slimmed down version of the Urbit `v2.11` pill that has had every desk and agent not necessary for booting to dojo removed

More information on the pills used by Ares can be found [here](https://github.com/urbit/ares/blob/status/docs/pills.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/pills.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Ares development and testing, unlike regular development and ship operation, cur

## Example: `baby.pill`

`baby.pill` is an extremely minimal Arvo-shaped core and Hoon standard library equipped with `%sham` jets needed to run it. `~wicdev-wisryt`` [streamed a video](https://youtu.be/fOVhCx1a-9A) of its development. You can find the source Hoon for `baby.pill` in `resources/pills/src/baby/baby.hoon`, and the limited version of Hoon that it uses in `resources/pills/src/baby/cradle.hoon`. A pre-compiled `baby.pill` is already available at `resources/pills/baby.pill`. However, the steps to compile it yourself are documented below.
`baby.pill` is an extremely minimal Arvo-shaped core and Hoon standard library equipped with `%sham` jets needed to run it. `~wicdev-wisryt` [streamed a video](https://youtu.be/fOVhCx1a-9A) of its development. You can find the source Hoon for `baby.pill` in `resources/pills/src/baby/baby.hoon`, and the limited version of Hoon that it uses in `resources/pills/src/baby/cradle.hoon`. A pre-compiled `baby.pill` is already available at `resources/pills/baby.pill`. However, the steps to compile it yourself are documented below.

1. Boot a fake `zod` using an ordinary Urbit executable (not the one you created
to run Ares as serf)
Expand Down
112 changes: 62 additions & 50 deletions rust/ares/src/interpreter.rs

Large diffs are not rendered by default.

27 changes: 16 additions & 11 deletions rust/ares/src/jets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod serial;
pub mod sort;
pub mod tree;

use crate::interpreter::{Context, Error};
use crate::interpreter::{Context, Error, Mote};
use crate::jets::bits::*;
use crate::jets::cold::Cold;
use crate::jets::form::*;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl From<Error> for JetErr {

impl From<noun::Error> for JetErr {
fn from(_err: noun::Error) -> Self {
Self::Fail(Error::Deterministic(D(0)))
Self::Fail(Error::Deterministic(Mote::Exit, D(0)))
}
}

Expand Down Expand Up @@ -190,6 +190,9 @@ pub mod util {
use bitvec::prelude::{BitSlice, Lsb0};
use std::result;

pub const BAIL_EXIT: JetErr = JetErr::Fail(Error::Deterministic(Mote::Exit, D(0)));
pub const BAIL_FAIL: JetErr = JetErr::Fail(Error::NonDeterministic(Mote::Fail, D(0)));

/**
* Address-based size checks.
* Currently, only addresses indexable by the first 48 bits are reachable by
Expand All @@ -201,21 +204,20 @@ pub mod util {
pub fn checked_add(a: usize, b: usize) -> result::Result<usize, JetErr> {
a.checked_add(b)
.filter(|x| x <= &MAX_BIT_LENGTH)
.ok_or(JetErr::Fail(Error::NonDeterministic(D(0))))
.ok_or(BAIL_FAIL)
}

/// Performs subtraction that returns None on Noun size overflow
pub fn checked_sub(a: usize, b: usize) -> result::Result<usize, JetErr> {
a.checked_sub(b)
.ok_or(JetErr::Fail(Error::NonDeterministic(D(0))))
a.checked_sub(b).ok_or(BAIL_FAIL)
}

pub fn checked_left_shift(bloq: usize, a: usize) -> result::Result<usize, JetErr> {
let res = a << bloq;

// Catch overflow
if (res >> bloq) < a || res > MAX_BIT_LENGTH {
Err(JetErr::Fail(Error::NonDeterministic(D(0))))
Err(BAIL_FAIL)
} else {
Ok(res)
}
Expand All @@ -233,14 +235,14 @@ pub mod util {

pub fn slot(noun: Noun, axis: u64) -> Result {
noun.slot(axis)
.map_err(|_e| JetErr::Fail(Error::Deterministic(D(0))))
.map_err(|_e| JetErr::Fail(Error::Deterministic(Mote::Exit, D(0))))
}

/// Extract a bloq and check that it's computable by the current system
pub fn bloq(a: Noun) -> result::Result<usize, JetErr> {
let bloq = a.as_direct()?.data() as usize;
if bloq >= 47 {
Err(JetErr::Fail(Error::NonDeterministic(D(0))))
Err(BAIL_FAIL)
} else {
Ok(bloq)
}
Expand Down Expand Up @@ -380,10 +382,13 @@ pub mod util {
match (actual_err, expected_err) {
(Error::ScryBlocked(mut actual), Error::ScryBlocked(mut expected))
| (Error::ScryCrashed(mut actual), Error::ScryCrashed(mut expected))
| (Error::Deterministic(mut actual), Error::Deterministic(mut expected))
| (
Error::NonDeterministic(mut actual),
Error::NonDeterministic(mut expected),
Error::Deterministic(_, mut actual),
Error::Deterministic(_, mut expected),
)
| (
Error::NonDeterministic(_, mut actual),
Error::NonDeterministic(_, mut expected),
) => unsafe {
assert!(unifying_equality(
&mut context.stack,
Expand Down
6 changes: 3 additions & 3 deletions rust/ares/src/jets/bits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** Bit arithmetic & logic jets
*/
use crate::interpreter::{Context, Error};
use crate::interpreter::Context;
use crate::jets::util::*;
use crate::jets::{JetErr, Result};
use crate::jets::Result;
use crate::noun::{IndirectAtom, Noun, D};
use std::cmp;

Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn jet_rev(context: &mut Context, subject: Noun) -> Result {
let boz = slot(arg, 2)?.as_atom()?.as_direct()?.data();

if boz >= 64 {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_EXIT);
}

let boz = boz as usize;
Expand Down
25 changes: 12 additions & 13 deletions rust/ares/src/jets/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** Text processing jets
*/
use crate::interpreter::{interpret, Context, Error};
use crate::jets::util::slot;
use crate::jets::{JetErr, Result};
use crate::interpreter::{interpret, Context};
use crate::jets::util::{slot, BAIL_FAIL};
use crate::jets::Result;
use crate::noun::{Cell, Noun, D, T};
use bitvec::order::Lsb0;
use bitvec::slice::BitSlice;
Expand Down Expand Up @@ -82,7 +82,7 @@ pub fn jet_turn(context: &mut Context, subject: Noun) -> Result {
}
} else {
if unsafe { !list.raw_equals(D(0)) } {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_FAIL);
}
unsafe {
*dest = D(0);
Expand All @@ -106,7 +106,7 @@ pub fn jet_turn(context: &mut Context, subject: Noun) -> Result {
}
} else {
if unsafe { !list.raw_equals(D(0)) } {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_FAIL);
}
unsafe {
*dest = D(0);
Expand All @@ -118,7 +118,7 @@ pub fn jet_turn(context: &mut Context, subject: Noun) -> Result {
}

pub mod util {
use crate::interpreter::Error;
use crate::jets::util::BAIL_EXIT;
use crate::jets::{JetErr, Result};
use crate::mem::NockStack;
use crate::noun::{Cell, Noun, D, T};
Expand Down Expand Up @@ -149,7 +149,7 @@ pub mod util {
if atom.as_bitslice().first_one().is_none() {
break;
} else {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_EXIT);
}
}
let cell = list.as_cell()?;
Expand Down Expand Up @@ -191,9 +191,8 @@ pub mod util {
#[cfg(test)]
mod tests {
use super::*;
use crate::interpreter::Error;
use crate::jets::util::test::{assert_jet, assert_jet_err, init_context};
use crate::jets::JetErr;
use crate::jets::util::BAIL_EXIT;
use crate::noun::{D, T};

#[test]
Expand Down Expand Up @@ -228,9 +227,9 @@ mod tests {
);
assert_jet(c, jet_flop, sam, res);

assert_jet_err(c, jet_flop, D(1), JetErr::Fail(Error::Deterministic(D(0))));
assert_jet_err(c, jet_flop, D(1), BAIL_EXIT);
let sam = T(&mut c.stack, &[D(1), D(2), D(3)]);
assert_jet_err(c, jet_flop, sam, JetErr::Fail(Error::Deterministic(D(0))));
assert_jet_err(c, jet_flop, sam, BAIL_EXIT);
}

#[test]
Expand All @@ -242,9 +241,9 @@ mod tests {
assert_jet(c, jet_lent, sam, D(3));
let sam = T(&mut c.stack, &[D(3), D(2), D(1), D(0)]);
assert_jet(c, jet_lent, sam, D(3));
assert_jet_err(c, jet_lent, D(1), JetErr::Fail(Error::Deterministic(D(0))));
assert_jet_err(c, jet_lent, D(1), BAIL_EXIT);
let sam = T(&mut c.stack, &[D(3), D(2), D(1)]);
assert_jet_err(c, jet_lent, sam, JetErr::Fail(Error::Deterministic(D(0))));
assert_jet_err(c, jet_lent, sam, BAIL_EXIT);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions rust/ares/src/jets/lock/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ pub fn jet_sivc_de(context: &mut Context, subject: Noun) -> Result {
}

mod util {
use crate::interpreter::Error;
use crate::jets::bits::util::met;
use crate::jets::list;
use crate::jets::util::BAIL_FAIL;
use crate::jets::{JetErr, Result};
use crate::mem::NockStack;
use crate::noun::{Atom, IndirectAtom, Noun, D, T};
Expand Down Expand Up @@ -219,7 +219,7 @@ mod util {
unsafe {
let txt_len = match len.as_direct() {
Ok(direct) => direct.data() as usize,
Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))),
Err(_) => return Err(BAIL_FAIL),
};

let iv_bytes = &mut [0u8; 16];
Expand Down
14 changes: 7 additions & 7 deletions rust/ares/src/jets/lock/ed.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::interpreter::{Context, Error};
use crate::interpreter::Context;
use crate::jets::bits::util::met;
use crate::jets::util::slot;
use crate::jets::util::{slot, BAIL_EXIT};
use crate::jets::{JetErr, Result};
use crate::mem::NockStack;
use crate::noun::{IndirectAtom, Noun, D, NO, YES};
use crate::noun::{IndirectAtom, Noun, NO, YES};
use ares_crypto::ed25519::{ac_ed_puck, ac_ed_shar, ac_ed_sign, ac_ed_veri};

crate::gdb!();
Expand All @@ -14,7 +14,7 @@ pub fn jet_puck(context: &mut Context, subject: Noun) -> Result {

let sed_len = met(3, sed);
if sed_len > 32 {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_EXIT);
}

unsafe {
Expand All @@ -35,7 +35,7 @@ pub fn jet_shar(context: &mut Context, subject: Noun) -> Result {

if met(3, sec_key) > 32 {
// sek is size checked by +puck via +suck
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_EXIT);
}
if met(3, pub_key) > 32 {
// pub is not size checked in Hoon, but it must be 32 bytes or less for
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn jet_sign(context: &mut Context, subject: Noun) -> Result {
let sed_bytes = sed.as_bytes();
let sed_len = sed_bytes.len();
if sed_len > 32 {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
return Err(BAIL_EXIT);
};
let seed = &mut [0u8; 32];
seed[0..sed_len].copy_from_slice(sed_bytes);
Expand Down Expand Up @@ -168,7 +168,7 @@ mod tests {
&mut c.stack,
&ubig!(_0xfb099b0acc4d1ce37f9982a2ed331245e0cdfdf6979364b7676a142b8233e53b),
);
assert_jet_err(c, jet_shar, sam, JetErr::Fail(Error::Deterministic(D(0))));
assert_jet_err(c, jet_shar, sam, BAIL_EXIT);
}

#[test]
Expand Down
Loading

0 comments on commit 7372095

Please sign in to comment.