Skip to content

Conversation

@linh2931
Copy link
Contributor

@linh2931 linh2931 commented Mar 31, 2025

The following host functions are not allowed to be called from sync calls:

  1. require_auth(),
  2. require_auth2(),
  3. has_auth(),
  4. require_recipient(),
  5. read_action_data(),
  6. action_data_size(),
  7. set_action_return_value(),
  8. get_context_free_data(),
  9. send_inline(),
  10. send_context_free_inline(),
  11. send_deferred(),
  12. cancel_deferred()

The following host functions are not allowed to be called from actions:

  1. get_call_data(),
  2. set_call_return_value()

This PR enforces those preconditions via host function definition wrappers, which is cleaner than the checks are scattered in the function implementation. Thanks @spoonincode for the suggestion!

For each of the host functions, a test is added to verify the precondition is checked.

Resolves #1218

@linh2931 linh2931 requested review from heifner and spoonincode March 31, 2025 19:10
* @throws missing_auth_exception If no sufficient permission was found
*/
virtual void require_authorization(const account_name& account) = 0;
virtual void require_authorization(const account_name& account) { EOS_ASSERT(false, unaccessible_api, "default implemention should never be used"); } // This function should never be called in sync calls due to require_auth host wrapper preconditions. This EOS_ASSERT double makes sure that.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's impossible to get here would assert() be a more appropriate choice for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was debating over assert or EOS_ASSERT. Now I think about it more, assert is more appropriate as it is a coding error to get to this point. Will change.

virtual int get_action( uint32_t type, uint32_t index, char* buffer, size_t buffer_size )const = 0;
virtual int get_context_free_data( uint32_t index, char* buffer, size_t buffer_size )const = 0;
virtual int get_action( uint32_t type, uint32_t index, char* buffer, size_t buffer_size ) const { EOS_ASSERT(false, unaccessible_api, "default implemention should never be used"); }
virtual int get_context_free_data( uint32_t index, char* buffer, size_t buffer_size )const { EOS_ASSERT(false, unaccessible_api, "default implemention should never be used"); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, I guess for these couldn't just do an assert(), but would also need a __builtin_unreachable() to silence the warning of not having a return value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string _pending_console_output;

const bool _is_action = true; // used to avoid using dynamic_cast to identify type of host_context
const bool _is_sync_call = false; // used to avoid using dynamic_cast to identify type of host_context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to these would be for is_action() and is_sync_call() to be virtual and return false on host_context, and then apply_context and sync_call_context can override the appropriate one to return true. I'm indifferent on which approach to take, just mentioning it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your idea. It removes 2 member fields and makes the constructor a little simpler.

0b3559c

@linh2931 linh2931 merged commit 311f00e into enforce_max_call_depth Apr 3, 2025
36 checks passed
@linh2931 linh2931 deleted the enforce_host_func_preconditions branch April 3, 2025 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants