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

[aptos-vm][move] Avoid module loads when getting the struct name #15681

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

georgemitenkov
Copy link
Contributor

@georgemitenkov georgemitenkov commented Jan 7, 2025

Description

For transaction argument validation, we check if the struct type name matches the allowed constructor. Previously, we were loading modules to check that, which is unnecessary because we can get the name from the struct re-indexing map. This PR changes the check to this.

How Has This Been Tested?

Existing tests.

Key Areas to Review

Double-check the behaviour is the same.

Type of Change

  • Refactoring

Which Components or Systems Does This Change Impact?

  • Move/Aptos Virtual Machine

Checklist

  • I have read and followed the CONTRIBUTING doc
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I identified and added all stakeholders and component owners affected by this change as reviewers
  • I tested both happy and unhappy path of the functionality
  • I have made corresponding changes to the documentation

Copy link

trunk-io bot commented Jan 7, 2025

⏱️ 51m total CI duration on this PR
Job Cumulative Duration Recent Runs
rust-targeted-unit-tests 20m 🟩
rust-move-tests 13m 🟩
rust-cargo-deny 5m 🟩🟩🟩
rust-move-tests 5m 🟥
check-dynamic-deps 4m 🟩🟩🟩
general-lints 1m 🟩🟩🟩
semgrep/ci 1m 🟩🟩🟩
file_change_determinator 37s 🟩🟩🟩
permission-check 10s 🟩🟩🟩
permission-check 9s 🟩🟩🟩
check-branch-prefix 1s 🟩

settingsfeedbackdocs ⋅ learn more about trunk.io

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@georgemitenkov georgemitenkov marked this pull request as ready for review January 7, 2025 14:55
@georgemitenkov georgemitenkov removed the request for review from davidiw January 7, 2025 14:55
.move_vm
.runtime
.loader()
.struct_name_index_map(module_storage)
Copy link
Contributor

Choose a reason for hiding this comment

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

what if the module contains the struct is not loaded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The module cannot contain non-loaded struct? Or what do you mean?

My goal here is to do the following:

  1. If we have a struct Type, it contains an index.
  2. The only way to create the index is via struct_name_index_map (I guess having a public inner field + constructor is a problem you are referring to? It might make sense to enforce that we can construct indices only via the map, or it should be test-only construction)
  3. Hence, we must get the correct indexed name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zekun000 updated the code so that struct name index has private constructor and only can be got via indexed map.

Copy link
Contributor

Choose a reason for hiding this comment

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

It also assumes that the map can never be cleared or things removed, right?

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, but that is also true for other caches? We stored indices in Type::Struct { .. }s, and these types can be in per-frame cache, etc. cached by interpreter. If struct name index map is flashed during the transaction execution, we have dangling indices.

use move_vm_types::loaded_data::runtime_types::Type::*;

match ty {
Ok(match ty {
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment of the fn says "whether argument is valid / allowed and whether it needs construction"
Is it an actual &? so true means it is valid and does not need construction? Let's clarify the comment (maybe also adding error condition)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, true means it is valid, e.g., an integer, or struct has a name for which we have a constructor. Updated the comment to make it more clear.

Struct { .. } | StructInstantiation { .. } => {
let (module_id, identifier) = session
.get_struct_name(ty, module_storage)
.map_err(|_| invalid_signature())?
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this correct - i.e. invalid_signature mapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code was using .ok_or_else(invalid_signature)?; before, so for compatibility I just re-used the mapping. The code is a mess and probably we need to rewrite it in any case at some point, without this kind of remapping, or at least with clear error messages.

.move_vm
.runtime
.loader()
.struct_name_index_map(module_storage)
Copy link
Contributor

Choose a reason for hiding this comment

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

It also assumes that the map can never be cleared or things removed, right?

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.

3 participants