Skip to content

Commit

Permalink
[events v2] Sample of how to check for event type attributes in the e…
Browse files Browse the repository at this point in the history
…xtended checker
  • Loading branch information
wrwg authored and msmouse committed Aug 21, 2023
1 parent c93e61d commit 72cb7b4
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions aptos-move/framework/src/extended_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ use move_core_types::{
use move_model::{
ast::{Attribute, AttributeValue, Value},
model::{
FunctionEnv, GlobalEnv, Loc, ModuleEnv, NamedConstantEnv, Parameter, QualifiedId,
FunId, FunctionEnv, GlobalEnv, Loc, ModuleEnv, NamedConstantEnv, Parameter, QualifiedId,
StructEnv, StructId,
},
symbol::Symbol,
ty::{PrimitiveType, ReferenceKind, Type},
};
use move_stackless_bytecode::{
function_target::{FunctionData, FunctionTarget},
stackless_bytecode::{AttrId, Bytecode, Operation},
stackless_bytecode_generator::StacklessBytecodeGenerator,
};
use once_cell::sync::Lazy;
use std::{
collections::{BTreeMap, BTreeSet},
Expand Down Expand Up @@ -99,6 +104,7 @@ impl<'a> ExtendedChecker<'a> {
self.check_and_record_resource_group_members(module);
self.check_and_record_view_functions(module);
self.check_entry_functions(module);
self.check_events(module);
self.check_init_module(module);
self.build_error_map(module)
}
Expand Down Expand Up @@ -460,6 +466,59 @@ impl<'a> ExtendedChecker<'a> {
}
}

// ----------------------------------------------------------------------------------
// Events

impl<'a> ExtendedChecker<'a> {
fn check_events(&mut self, module: &ModuleEnv) {
for fun in module.get_functions() {
if fun.is_inline() || fun.is_native() {
continue;
}
// Holder for stackless function data
let data = self.get_stackless_data(&fun);
// Handle to work with stackless functions -- function targets.
let target = FunctionTarget::new(&fun, &data);
// Now check for event emit calls.
for bc in target.get_bytecode() {
if let Bytecode::Call(attr_id, _, Operation::Function(mid, fid, type_inst), _, _) =
bc
{
self.check_emit_event_call(&target, *attr_id, mid.qualified(*fid), type_inst);
}
}
}
}

fn check_emit_event_call(
&mut self,
target: &FunctionTarget,
attr_id: AttrId,
callee: QualifiedId<FunId>,
type_inst: &[Type],
) {
if !self.is_function(callee, "0x1::event::emit") {
return;
}
// We are looking at `0x1::event::emit<T>` and extracting the `T`
let event_type = &type_inst[0];
// Now check whether this type has the event attribute
let type_ok = match event_type {
Type::Struct(mid, sid, _) => {
let struct_ = self.env.get_struct(mid.qualified(*sid));
self.has_attribute_iter(struct_.get_attributes().iter(), "event")
},
_ => false,
};
if !type_ok {
let loc = target.get_bytecode_loc(attr_id);
self.env.error(&loc,
&format!("`0x1::event::emit` called with type `{}` which does not have the `#[event]` attribute",
event_type.display(&self.env.get_type_display_ctx())));
}
}
}

// ----------------------------------------------------------------------------------
// Error Map

Expand Down Expand Up @@ -508,7 +567,15 @@ impl<'a> ExtendedChecker<'a> {

impl<'a> ExtendedChecker<'a> {
fn has_attribute(&self, fun: &FunctionEnv, attr_name: &str) -> bool {
fun.get_attributes().iter().any(|attr| {
self.has_attribute_iter(fun.get_attributes().iter(), attr_name)
}

fn has_attribute_iter(
&self,
mut attrs: impl Iterator<Item = &'a Attribute>,
attr_name: &str,
) -> bool {
attrs.any(|attr| {
if let Attribute::Apply(_, name, _) = attr {
self.env.symbol_pool().string(*name).as_str() == attr_name
} else {
Expand All @@ -529,6 +596,15 @@ impl<'a> ExtendedChecker<'a> {
fn name_string(&self, symbol: Symbol) -> Rc<String> {
self.env.symbol_pool().string(symbol)
}

fn get_stackless_data(&self, fun: &FunctionEnv) -> FunctionData {
StacklessBytecodeGenerator::new(fun).generate_function()
}

fn is_function(&self, id: QualifiedId<FunId>, full_name_str: &str) -> bool {
let fun = &self.env.get_function(id);
fun.get_full_name_str() == full_name_str
}
}

// ----------------------------------------------------------------------------------
Expand Down

0 comments on commit 72cb7b4

Please sign in to comment.