Skip to content

Commit

Permalink
Add tests for literals (#83)
Browse files Browse the repository at this point in the history
* prep for tests

* tests

* lints

* fix valid large negative test

* fix literal tests/code

* tiny lint

* lint

---------

Co-authored-by: brock elmore <[email protected]>
  • Loading branch information
plotchy and brockelmore authored Jul 10, 2024
1 parent 36c8878 commit 44ec8f1
Show file tree
Hide file tree
Showing 10 changed files with 932 additions and 93 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ debug = true

[profile.dev]
# opt-level = 1 # Enable some optimizations like tail call
inline = true
# inline = true

[profile.bench]
debug = true
Expand Down
23 changes: 23 additions & 0 deletions crates/graph/src/nodes/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,29 @@ impl Concrete {
}
}

pub fn needed_size(&self) -> Option<u16> {
match self {
Concrete::Uint(_, val) => Some(((32 - (val.leading_zeros() / 8)) * 8).max(8) as u16),
Concrete::Int(_, val) => {
let unsigned = val.unsigned_abs();
Some(((32 - (unsigned.leading_zeros() / 8)) * 8).max(8) as u16)
}
_ => None,
}
}

pub fn fit_size(self) -> Self {
if let Some(needed) = self.needed_size() {
match self {
Concrete::Uint(_, val) => Concrete::Uint(needed, val),
Concrete::Int(_, val) => Concrete::Int(needed, val),
_ => self,
}
} else {
self
}
}

/// If its `Concrete::Uint`, gets the value
pub fn uint_val(&self) -> Option<U256> {
match self {
Expand Down
20 changes: 1 addition & 19 deletions crates/graph/src/range/solc_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,12 @@ impl SolcRange {
| c @ Concrete::Int(_, _)
| c @ Concrete::Bool(_)
| c @ Concrete::Address(_)
| c @ Concrete::String(_)
| c @ Concrete::Bytes(_, _) => Some(SolcRange::new(
Elem::Concrete(RangeConcrete::new(c.clone(), Loc::Implicit)),
Elem::Concrete(RangeConcrete::new(c, Loc::Implicit)),
vec![],
)),
Concrete::String(s) => {
let val = s
.chars()
.enumerate()
.map(|(i, v)| {
let idx = Elem::from(Concrete::from(U256::from(i)));
let mut bytes = [0x00; 32];
v.encode_utf8(&mut bytes[..]);
let v = Elem::from(Concrete::Bytes(1, H256::from(bytes)));
(idx, v)
})
.collect::<BTreeMap<_, _>>();
let r = Elem::ConcreteDyn(RangeDyn::new(
Elem::from(Concrete::from(U256::from(s.len()))),
val,
Loc::Implicit,
));
Some(SolcRange::new(r.clone(), r, vec![]))
}
Concrete::DynBytes(b) => {
let val = b
.iter()
Expand Down
6 changes: 3 additions & 3 deletions crates/graph/src/solvers/dl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@ impl DLSolver {
added_atoms.push((*dyn_elem).clone());
self.graph_map.insert((*dyn_elem).clone(), idx);
if let Some(dep) = dep {
if !self.var_to_atom_idx.contains_key(&dep) {
self.var_to_atom_idx.entry(dep).or_insert_with(|| {
added_deps.push(dep);
self.var_to_atom_idx.insert(dep, idx);
}
idx
});
}
idx
};
Expand Down
1 change: 1 addition & 0 deletions crates/graph/src/var_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ impl VarType {
}

pub fn range(&self, analyzer: &impl GraphBackend) -> Result<Option<SolcRange>, GraphError> {
println!("here: {:?}", self);
match self {
Self::User(_, Some(range)) => Ok(Some(range.clone())),
Self::BuiltIn(_, Some(range)) => Ok(Some(range.clone())),
Expand Down
4 changes: 2 additions & 2 deletions crates/pyrometer/src/graph_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,10 @@ pub fn arena_mermaid_node(
format!("{indent}{}{{{{\"{}\"}}}}", idx.index(), arena_idx)
}
ArenaNode::ELEM(label) => {
format!("{indent}{}(\"{}\")", idx.index(), label.replace("\"", "'"))
format!("{indent}{}(\"{}\")", idx.index(), label.replace('"', "'"))
}
ArenaNode::CVAR(label) => {
format!("{indent}{}(\"{}\")", idx.index(), label.replace("\"", "'"))
format!("{indent}{}(\"{}\")", idx.index(), label.replace('"', "'"))
}
};

Expand Down
4 changes: 4 additions & 0 deletions crates/solc-expressions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ tracing.workspace = true
tracing-subscriber.workspace = true

keccak-hash = "0.10.0"

[dev-dependencies]
pyrometer.workspace = true
eyre = "0.6"
4 changes: 3 additions & 1 deletion crates/solc-expressions/src/context_builder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ pub trait ExpressionParser:
HexNumberLiteral(loc, b, _unit) => self.hex_num_literal(ctx, *loc, b, false),
HexLiteral(hexes) => self.hex_literals(ctx, hexes),
RationalNumberLiteral(loc, integer, fraction, exp, unit) => {
self.rational_number_literal(arena, ctx, *loc, integer, fraction, exp, unit)
self.rational_number_literal(arena, ctx, *loc, integer, fraction, exp, unit, false)
}
Negate(_loc, expr) => match &**expr {
NumberLiteral(loc, int, exp, unit) => {
self.number_literal(ctx, *loc, int, exp, true, unit)
}
HexNumberLiteral(loc, b, _unit) => self.hex_num_literal(ctx, *loc, b, true),
RationalNumberLiteral(loc, integer, fraction, exp, unit) => self
.rational_number_literal(arena, ctx, *loc, integer, fraction, exp, unit, true),
e => {
self.parse_ctx_expr(arena, e, ctx)?;
self.apply_to_edges(ctx, e.loc(), arena, &|analyzer, arena, ctx, loc| {
Expand Down
Loading

0 comments on commit 44ec8f1

Please sign in to comment.