Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when Yul mapping fails; print friendly message when fe panics #327

Merged
merged 3 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ stringreader = "0.1"
# This fork supports concurrent compilation, which is required for Rust tests.
solc = { git = "https://github.com/g-r-a-n-t/solc-rust", optional = true }
maplit = "1.0.2"
once_cell = "1.5.2"

[dev-dependencies]
evm-runtime = "0.18"
Expand Down
27 changes: 26 additions & 1 deletion compiler/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
//! Errors returned by the compilers and ABI builder.

use fe_parser::tokenizer::TokenizeError;
use serde::export::Formatter;
use once_cell::sync::Lazy;
use std::fmt::Formatter;
use std::panic;

const BUG_REPORT_URL: &str = "https://github.com/ethereum/fe/issues/new";
static DEFAULT_PANIC_HOOK: Lazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
Lazy::new(|| {
let hook = panic::take_hook();
panic::set_hook(Box::new(|info| report_ice(info)));
hook
});

pub fn install_compiler_panic_hook() {
Lazy::force(&DEFAULT_PANIC_HOOK);
}

/// Errors can either be an object or static reference.
#[derive(Debug)]
Expand Down Expand Up @@ -78,3 +92,14 @@ impl<'a> From<ethabi::Error> for CompileError {
CompileError::str(&format!("ethabi error: {}", e))
}
}

fn report_ice(info: &panic::PanicInfo) {
(*DEFAULT_PANIC_HOOK)(info);

eprintln!();
eprintln!("You've hit an internal compiler error. This is a bug in the Fe compiler.");
eprintln!("Fe is still under heavy development, and isn't yet ready for production use.");
eprintln!();
eprintln!("If you would, please report this bug at the following URL:");
eprintln!(" {}", BUG_REPORT_URL);
}
2 changes: 1 addition & 1 deletion compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn compile(
.map_err(|error| CompileError::str(&error.format_user(src)))?;

// compile to yul
let yul_contracts = yul::compile(&context, &lowered_fe_module)?;
let yul_contracts = yul::compile(&context, &lowered_fe_module);

// compile to bytecode if required
#[cfg(feature = "solc-backend")]
Expand Down
100 changes: 47 additions & 53 deletions compiler/src/yul/mappers/assignments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::errors::CompileError;
use crate::yul::mappers::expressions;
use crate::yul::operations::data as data_operations;
use fe_analyzer::namespace::types::FixedSize;
Expand All @@ -12,10 +11,7 @@ use std::convert::TryFrom;
use yultsur::*;

/// Builds a Yul statement from a Fe assignment.
pub fn assign(
context: &Context,
stmt: &Node<fe::FuncStmt>,
) -> Result<yul::Statement, CompileError> {
pub fn assign(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
if let fe::FuncStmt::Assign { targets, value } = &stmt.kind {
if targets.len() > 1 {
unimplemented!("multiple assignment targets")
Expand All @@ -26,65 +22,63 @@ pub fn assign(
context.get_expression(first_target),
context.get_expression(value),
) {
let target = expressions::expr(&context, first_target)?;
let value = expressions::expr(&context, value)?;
let target = expressions::expr(&context, first_target);
let value = expressions::expr(&context, value);

let typ = FixedSize::try_from(target_attributes.typ.to_owned())
.map_err(|_| CompileError::static_str("invalid attributes"))?;

return Ok(
match (
value_attributes.final_location(),
target_attributes.final_location(),
) {
(Location::Memory, Location::Storage { .. }) => {
data_operations::mcopys(typ, target, value)
}
(Location::Memory, Location::Value) => {
let target = expr_as_ident(target)?;
let value = data_operations::mload(typ, value);
statement! { [target] := [value] }
}
(Location::Memory, Location::Memory) => {
let target = expr_as_ident(target)?;
statement! { [target] := [value] }
}
(Location::Storage { .. }, Location::Storage { .. }) => {
data_operations::scopys(typ, target, value)
}
(Location::Storage { .. }, Location::Value) => {
let target = expr_as_ident(target)?;
let value = data_operations::sload(typ, value);
statement! { [target] := [value] }
}
(Location::Storage { .. }, Location::Memory) => {
unreachable!("raw sto to mem assign")
}
(Location::Value, Location::Memory) => {
data_operations::mstore(typ, target, value)
}
(Location::Value, Location::Storage { .. }) => {
data_operations::sstore(typ, target, value)
}
(Location::Value, Location::Value) => {
let target = expr_as_ident(target)?;
statement! { [target] := [value] }
}
},
);
.expect("invalid attributes");

return match (
value_attributes.final_location(),
target_attributes.final_location(),
) {
(Location::Memory, Location::Storage { .. }) => {
data_operations::mcopys(typ, target, value)
}
(Location::Memory, Location::Value) => {
let target = expr_as_ident(target);
let value = data_operations::mload(typ, value);
statement! { [target] := [value] }
}
(Location::Memory, Location::Memory) => {
let target = expr_as_ident(target);
statement! { [target] := [value] }
}
(Location::Storage { .. }, Location::Storage { .. }) => {
data_operations::scopys(typ, target, value)
}
(Location::Storage { .. }, Location::Value) => {
let target = expr_as_ident(target);
let value = data_operations::sload(typ, value);
statement! { [target] := [value] }
}
(Location::Storage { .. }, Location::Memory) => {
unreachable!("raw sto to mem assign")
}
(Location::Value, Location::Memory) => {
data_operations::mstore(typ, target, value)
}
(Location::Value, Location::Storage { .. }) => {
data_operations::sstore(typ, target, value)
}
(Location::Value, Location::Value) => {
let target = expr_as_ident(target);
statement! { [target] := [value] }
}
};
}
}
}

unreachable!()
}

fn expr_as_ident(expr: yul::Expression) -> Result<yul::Identifier, CompileError> {
fn expr_as_ident(expr: yul::Expression) -> yul::Identifier {
if let yul::Expression::Identifier(ident) = expr {
return Ok(ident);
ident
} else {
panic!("expression is not an identifier");
}

Err(CompileError::static_str("expression is not an identifier"))
}

#[cfg(test)]
Expand Down
15 changes: 6 additions & 9 deletions compiler/src/yul/mappers/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::errors::CompileError;
use crate::yul::constructor;
use crate::yul::mappers::functions;
use crate::yul::runtime;
Expand All @@ -13,7 +12,7 @@ pub fn contract_def(
context: &Context,
stmt: &Node<fe::ModuleStmt>,
created_contracts: Vec<yul::Object>,
) -> Result<yul::Object, CompileError> {
) -> yul::Object {
if let fe::ModuleStmt::ContractDef { name, body } = &stmt.kind {
let contract_name = name.kind;
let mut init_function = None;
Expand All @@ -25,12 +24,10 @@ pub fn contract_def(
(context.get_function(stmt), &stmt.kind)
{
if name.kind == "__init__" {
init_function = Some((
functions::func_def(context, stmt)?,
attributes.param_types(),
))
init_function =
Some((functions::func_def(context, stmt), attributes.param_types()))
} else {
user_functions.push(functions::func_def(context, stmt)?)
user_functions.push(functions::func_def(context, stmt))
}
}
}
Expand Down Expand Up @@ -99,12 +96,12 @@ pub fn contract_def(
};

// We return the contract initialization object.
return Ok(yul::Object {
return yul::Object {
name: identifier! { (contract_name) },
code: constructor_code,
objects: constructor_objects,
data,
});
};
}

unreachable!()
Expand Down
12 changes: 4 additions & 8 deletions compiler/src/yul/mappers/declarations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::errors::CompileError;
use crate::yul::mappers::expressions;
use crate::yul::names;
use fe_analyzer::namespace::types::{
Expand All @@ -11,17 +10,14 @@ use fe_parser::node::Node;
use yultsur::*;

/// Builds a Yul statement from a Fe variable declaration
pub fn var_decl(
context: &Context,
stmt: &Node<fe::FuncStmt>,
) -> Result<yul::Statement, CompileError> {
pub fn var_decl(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
let decl_type = context.get_declaration(stmt).expect("missing attributes");

if let fe::FuncStmt::VarDecl { target, value, .. } = &stmt.kind {
let target = names::var_name(expressions::expr_name_str(&target));

return Ok(if let Some(value) = value {
let value = expressions::expr(context, &value)?;
return if let Some(value) = value {
let value = expressions::expr(context, &value);
statement! { let [target] := [value] }
} else {
match decl_type {
Expand All @@ -31,7 +27,7 @@ pub fn var_decl(
statement! { let [target] := alloc([size]) }
}
}
});
};
}

unreachable!()
Expand Down
Loading