Skip to content
New issue

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

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

Already on GitHub? # to your account

Disallow certain opcodes in an external context #366

Merged
merged 3 commits into from
Nov 4, 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
76 changes: 76 additions & 0 deletions core_lang/src/asm_generation/checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Various checks and heuristics that are naively run on sequences of opcodes.
//!
//! This is _not_ the place for optimization passes.
use crate::asm_generation::FinalizedAsm;
use crate::asm_lang::allocated_ops::{AllocatedOp, AllocatedOpcode};
use crate::asm_lang::*;
use crate::error::*;

/// Checks for disallowed opcodes in non-contract code.
/// i.e., if this is a script or predicate, we can't use certain contract opcodes.
/// See https://github.com/FuelLabs/sway/issues/350 for details.
pub fn check_invalid_opcodes<'sc>(asm: &FinalizedAsm<'sc>) -> CompileResult<'sc, ()> {
match asm {
FinalizedAsm::ContractAbi { .. } | FinalizedAsm::Library => ok((), vec![], vec![]),
FinalizedAsm::ScriptMain {
program_section, ..
} => check_for_contract_opcodes(&program_section.ops[..]),
FinalizedAsm::PredicateMain {
program_section, ..
} => check_for_contract_opcodes(&program_section.ops[..]),
}
}

/// Checks if an opcode is one that can only be executed from within a contract. If so, throw an
/// error.
fn check_for_contract_opcodes<'sc>(ops: &[AllocatedOp<'sc>]) -> CompileResult<'sc, ()> {
use AllocatedOpcode::*;
let default_span = crate::Span {
span: pest::Span::new("no span found for opcode", 0, 1).unwrap(),
path: None,
};
let mut errors = vec![];
for op in ops {
match op.opcode {
GM(_, VirtualImmediate18 { value: 1 }) | GM(_, VirtualImmediate18 { value: 2 }) => {
errors.push(CompileError::GMFromExternalContract {
span: op
.owning_span
.clone()
.unwrap_or_else(|| default_span.clone()),
});
}
MINT(..) => {
errors.push(CompileError::MintFromExternalContext {
span: op
.owning_span
.clone()
.unwrap_or_else(|| default_span.clone()),
});
}
BURN(..) => {
errors.push(CompileError::BurnFromExternalContext {
span: op
.owning_span
.clone()
.unwrap_or_else(|| default_span.clone()),
});
}
SWW(..) | SRW(..) | SRWQ(..) | SWWQ(..) => {
errors.push(CompileError::ContractStorageFromExternalContext {
span: op
.owning_span
.clone()
.unwrap_or_else(|| default_span.clone()),
});
}
_ => (),
}
}

if errors.is_empty() {
ok((), vec![], errors)
} else {
err(vec![], errors)
}
}
8 changes: 8 additions & 0 deletions core_lang/src/asm_generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
};
use either::Either;

pub(crate) mod checks;
pub(crate) mod compiler_constants;
mod declaration;
mod expression;
Expand Down Expand Up @@ -765,6 +766,13 @@ pub(crate) fn compile_ast_to_asm<'sc>(
println!("{}", finalized_asm);
}

check!(
super::check_invalid_opcodes(&finalized_asm),
return err(warnings, errors),
warnings,
errors
);

ok(finalized_asm, warnings, errors)
}

Expand Down
12 changes: 12 additions & 0 deletions core_lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ pub enum CompileError<'sc> {
},
#[error("File {file_path} generates an infinite dependency cycle.")]
InfiniteDependencies { file_path: String, span: Span<'sc> },
#[error("The GM (get-metadata) opcode, when called from an external context, will cause the VM to panic.")]
GMFromExternalContract { span: Span<'sc> },
#[error("The MINT opcode cannot be used in an external context.")]
MintFromExternalContext { span: Span<'sc> },
#[error("The BURN opcode cannot be used in an external context.")]
BurnFromExternalContext { span: Span<'sc> },
#[error("Contract storage cannot be used in an external context.")]
ContractStorageFromExternalContext { span: Span<'sc> },
}

impl<'sc> std::convert::From<TypeError<'sc>> for CompileError<'sc> {
Expand Down Expand Up @@ -917,6 +925,10 @@ impl<'sc> CompileError<'sc> {
RecursiveCall { span, .. } => span,
RecursiveCallChain { span, .. } => span,
InfiniteDependencies { span, .. } => span,
GMFromExternalContract { span, .. } => span,
MintFromExternalContext { span, .. } => span,
BurnFromExternalContext { span, .. } => span,
ContractStorageFromExternalContext { span, .. } => span,
}
}

Expand Down
1 change: 1 addition & 0 deletions core_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod parser;
pub mod semantic_analysis;
mod span;

use crate::asm_generation::checks::check_invalid_opcodes;
pub use crate::parse_tree::*;
pub use crate::parser::{HllParser, Rule};
use crate::{asm_generation::compile_ast_to_asm, error::*};
Expand Down
1 change: 1 addition & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn run(filter_regex: Option<regex::Regex>) {
"dependencies_parsing_error",
"mut_error_message",
"reassignment_to_non_variable_message",
"disallowed_gm",
];
project_names.into_iter().for_each(|name| {
if filter(name) {
Expand Down
5 changes: 5 additions & 0 deletions test/src/e2e_vm_tests/test_programs/disallowed_gm/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
author = "Alexander Hansen"
license = "MIT"
name = "disallowed_opcodes"
entry = "main.sw"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
script;

fn main() {
// GM should be disallowed
asm(r1) {
gm r1 i1;
};
}