Skip to content

Commit 0c3accf

Browse files
committed
add attributes for struct api
1 parent c550c6e commit 0c3accf

File tree

400 files changed

+623
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

400 files changed

+623
-460
lines changed

aptos-move/aptos-release-builder/src/components/feature_flags.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub enum FeatureFlag {
156156
VMBinaryFormatV9,
157157
EnableFrameworkForOption,
158158
SessionContinuation,
159+
VMBinaryFormatV10,
159160
}
160161

161162
fn generate_features_blob(writer: &CodeWriter, data: &[u64]) {
@@ -411,6 +412,7 @@ impl From<FeatureFlag> for AptosFeatureFlag {
411412
FeatureFlag::VMBinaryFormatV9 => AptosFeatureFlag::VM_BINARY_FORMAT_V9,
412413
FeatureFlag::EnableFrameworkForOption => AptosFeatureFlag::ENABLE_FRAMEWORK_FOR_OPTION,
413414
FeatureFlag::SessionContinuation => AptosFeatureFlag::SESSION_CONTINUATION,
415+
FeatureFlag::VMBinaryFormatV10 => AptosFeatureFlag::VM_BINARY_FORMAT_V10,
414416
}
415417
}
416418
}
@@ -593,6 +595,7 @@ impl From<AptosFeatureFlag> for FeatureFlag {
593595
AptosFeatureFlag::VM_BINARY_FORMAT_V9 => FeatureFlag::VMBinaryFormatV9,
594596
AptosFeatureFlag::ENABLE_FRAMEWORK_FOR_OPTION => FeatureFlag::EnableFrameworkForOption,
595597
AptosFeatureFlag::SESSION_CONTINUATION => FeatureFlag::SessionContinuation,
598+
AptosFeatureFlag::VM_BINARY_FORMAT_V10 => FeatureFlag::VMBinaryFormatV10,
596599
}
597600
}
598601
}

aptos-move/aptos-transactional-test-harness/tests/aptos_test_harness/diamond_clicker.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
processed 4 tasks
22
task 1 lines 4-35: print-bytecode --input=module [module Alice::game {]
3-
// Bytecode version v9
3+
// Bytecode version v10
44
module 0xf75daa73fc071f93593335eb9033da804777eb94491650dd3f095ce6f778acb6::game
55
use 0x1::signer
66
use 0x1::debug

third_party/move/move-binary-format/src/deserializer.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ fn load_function_handle(
938938
};
939939

940940
let attributes = if version >= VERSION_8 {
941-
load_function_attributes(cursor)?
941+
load_function_attributes(cursor, version)?
942942
} else {
943943
vec![]
944944
};
@@ -1061,23 +1061,46 @@ fn load_signature_tokens(cursor: &mut VersionedCursor) -> BinaryLoaderResult<Vec
10611061

10621062
fn load_function_attributes(
10631063
cursor: &mut VersionedCursor,
1064+
bytecode_version: u32,
10641065
) -> BinaryLoaderResult<Vec<FunctionAttribute>> {
10651066
let count = read_uleb_internal(cursor, ATTRIBUTE_COUNT_MAX)?;
10661067
let mut attributes = Vec::with_capacity(count);
10671068
for _ in 0..count {
1068-
attributes.push(load_attribute(cursor)?);
1069+
attributes.push(load_attribute(cursor, bytecode_version)?);
10691070
}
10701071
Ok(attributes)
10711072
}
10721073

1073-
fn load_attribute(cursor: &mut VersionedCursor) -> BinaryLoaderResult<FunctionAttribute> {
1074+
fn load_attribute(
1075+
cursor: &mut VersionedCursor,
1076+
bytecode_version: u32,
1077+
) -> BinaryLoaderResult<FunctionAttribute> {
10741078
use SerializedFunctionAttribute::*;
1075-
Ok(
1076-
match SerializedFunctionAttribute::from_u8(load_u8(cursor)?)? {
1077-
PERSISTENT => FunctionAttribute::Persistent,
1078-
MODULE_LOCK => FunctionAttribute::ModuleLock,
1079+
match SerializedFunctionAttribute::from_u8(load_u8(cursor)?)? {
1080+
PERSISTENT => Ok(FunctionAttribute::Persistent),
1081+
MODULE_LOCK => Ok(FunctionAttribute::ModuleLock),
1082+
PACK => {
1083+
if bytecode_version < VERSION_10 {
1084+
Err(PartialVMError::new(StatusCode::MALFORMED)
1085+
.with_message("Unexpected EOF".to_string()))?
1086+
} else {
1087+
Ok(FunctionAttribute::Pack(load_struct_def_index(cursor)?))
1088+
}
10791089
},
1080-
)
1090+
PACK_VARIANT => {
1091+
if bytecode_version < VERSION_10 {
1092+
Err(PartialVMError::new(StatusCode::MALFORMED)
1093+
.with_message("Unexpected EOF".to_string()))?
1094+
} else {
1095+
let definition_index = load_struct_def_index(cursor)?;
1096+
let variant_index = load_bytecode_index(cursor)?;
1097+
Ok(FunctionAttribute::PackVariant(
1098+
definition_index,
1099+
variant_index,
1100+
))
1101+
}
1102+
},
1103+
}
10811104
}
10821105

10831106
fn load_access_specifiers(
@@ -2249,8 +2272,10 @@ impl SerializedFunctionAttribute {
22492272
match value {
22502273
0x1 => Ok(PERSISTENT),
22512274
0x2 => Ok(MODULE_LOCK),
2275+
0x3 => Ok(PACK),
2276+
0x4 => Ok(PACK_VARIANT),
22522277
_ => Err(PartialVMError::new(StatusCode::MALFORMED)
2253-
.with_message("malformed attribute".to_owned())),
2278+
.with_message(format!("malformed attribute: {}", value))),
22542279
}
22552280
}
22562281
}

third_party/move/move-binary-format/src/file_format.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ pub enum FunctionAttribute {
368368
Persistent,
369369
/// During execution of the function, a module reentrancy lock is established.
370370
ModuleLock,
371+
Pack(StructDefinitionIndex),
372+
PackVariant(StructDefinitionIndex, VariantIndex),
371373
}
372374

373375
impl FunctionAttribute {
@@ -389,6 +391,16 @@ impl fmt::Display for FunctionAttribute {
389391
match self {
390392
FunctionAttribute::Persistent => write!(f, "persistent"),
391393
FunctionAttribute::ModuleLock => write!(f, "module_lock"),
394+
FunctionAttribute::Pack(struct_definition_index) => {
395+
write!(f, "pack({})", struct_definition_index)
396+
},
397+
FunctionAttribute::PackVariant(struct_definition_index, variant_index) => {
398+
write!(
399+
f,
400+
"pack_variant({},{})",
401+
struct_definition_index, variant_index
402+
)
403+
},
392404
}
393405
}
394406
}

third_party/move/move-binary-format/src/file_format_common.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ pub enum SerializedNativeStructFlag {
222222
pub enum SerializedFunctionAttribute {
223223
PERSISTENT = 0x1,
224224
MODULE_LOCK = 0x2,
225+
PACK = 0x3,
226+
PACK_VARIANT = 0x4,
225227
}
226228

227229
/// List of opcodes constants.
@@ -552,8 +554,12 @@ pub const VERSION_8: u32 = 8;
552554
/// + allow `$` in identifiers
553555
pub const VERSION_9: u32 = 9;
554556

557+
/// Version 10: changes compared to version 9
558+
/// + new attributes for structs api
559+
pub const VERSION_10: u32 = 10;
560+
555561
/// Mark which version is the latest version.
556-
pub const VERSION_MAX: u32 = VERSION_9;
562+
pub const VERSION_MAX: u32 = VERSION_10;
557563

558564
/// Mark which version is the default version. This is the version used by default by tools like
559565
/// the compiler. Notice that this version might be different from the one supported on nodes.
@@ -566,6 +572,9 @@ pub const VERSION_DEFAULT_LANG_V2: u32 = VERSION_8;
566572
/// Mark which version is the default version if compiling with language version 2.3
567573
pub const VERSION_DEFAULT_LANG_V2_3: u32 = VERSION_9;
568574

575+
/// Mark which version is the default version if compiling with language version 2.4
576+
pub const VERSION_DEFAULT_LANG_V2_4: u32 = VERSION_10;
577+
569578
// Mark which oldest version is supported.
570579
pub const VERSION_MIN: u32 = VERSION_5;
571580

third_party/move/move-binary-format/src/serializer.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ fn serialize_function_handle(
549549
));
550550
}
551551
if major_version >= VERSION_8 {
552-
serialize_function_attributes(binary, &function_handle.attributes)
552+
serialize_function_attributes(binary, &function_handle.attributes, major_version)
553553
} else if !function_handle.attributes.is_empty() {
554554
Err(anyhow!(
555555
"Function attributes not supported in bytecode version {}",
@@ -867,22 +867,45 @@ fn serialize_ability_sets(binary: &mut BinaryData, sets: &[AbilitySet]) -> Resul
867867
fn serialize_function_attributes(
868868
binary: &mut BinaryData,
869869
attributes: &[FunctionAttribute],
870+
version: u32,
870871
) -> Result<()> {
871872
write_as_uleb128(binary, attributes.len() as u64, ATTRIBUTE_COUNT_MAX)?;
872873
for attr in attributes {
873-
serialize_function_attribute(binary, attr)?;
874+
serialize_function_attribute(binary, attr, version)?;
874875
}
875876
Ok(())
876877
}
877878

878879
fn serialize_function_attribute(
879880
binary: &mut BinaryData,
880881
attribute: &FunctionAttribute,
882+
version: u32,
881883
) -> Result<()> {
882884
use FunctionAttribute::*;
883885
match attribute {
884886
Persistent => binary.push(SerializedFunctionAttribute::PERSISTENT as u8),
885887
ModuleLock => binary.push(SerializedFunctionAttribute::MODULE_LOCK as u8),
888+
Pack(struct_definition_index) => {
889+
if version < VERSION_10 {
890+
return Err(anyhow!(
891+
"Pack attribute not supported in bytecode version {}",
892+
version
893+
));
894+
}
895+
binary.push(SerializedFunctionAttribute::PACK as u8)?;
896+
serialize_struct_def_index(binary, struct_definition_index)
897+
},
898+
PackVariant(struct_definition_index, variant_index) => {
899+
if version < VERSION_10 {
900+
return Err(anyhow!(
901+
"PackVariant attribute not supported in bytecode version {}",
902+
version
903+
));
904+
}
905+
binary.push(SerializedFunctionAttribute::PACK_VARIANT as u8)?;
906+
serialize_struct_def_index(binary, struct_definition_index)?;
907+
write_as_uleb128(binary, *variant_index, BYTECODE_INDEX_MAX)
908+
},
886909
}
887910
}
888911

third_party/move/move-compiler-v2/src/file_format_generator/module_generator.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct ModuleGenerator {
6363
/// A mapping from structs to indices of pack apis.
6464
struct_to_api_pack_idx:
6565
BTreeMap<QualifiedId<StructId>, BTreeMap<Option<Symbol>, FF::FunctionHandleIndex>>,
66+
struct_to_definition_idx: BTreeMap<QualifiedId<StructId>, FF::StructDefinitionIndex>,
6667
/// A mapping from structs to indices of unpack apis.
6768
struct_to_api_unpack_idx:
6869
BTreeMap<QualifiedId<StructId>, BTreeMap<Option<Symbol>, FF::FunctionHandleIndex>>,
@@ -191,6 +192,7 @@ impl ModuleGenerator {
191192
struct_to_api_imm_borrow_field_idx: Default::default(),
192193
struct_to_api_mut_borrow_field_idx: Default::default(),
193194
struct_to_idx: Default::default(),
195+
struct_to_definition_idx: Default::default(),
194196
struct_def_inst_to_idx: Default::default(),
195197
field_to_idx: Default::default(),
196198
field_inst_to_idx: Default::default(),
@@ -274,6 +276,8 @@ impl ModuleGenerator {
274276
MAX_STRUCT_DEF_COUNT,
275277
"struct",
276278
));
279+
self.struct_to_definition_idx
280+
.insert(struct_env.get_qualified_id(), def_idx);
277281
self.source_map
278282
.add_top_level_struct_mapping(def_idx, ctx.env.to_ir_loc(loc))
279283
.expect(SOURCE_MAP_OK);
@@ -681,6 +685,31 @@ impl ModuleGenerator {
681685

682686
let name_sym = struct_env.env().symbol_pool().make(&name);
683687
let name_idx = self.name_index(ctx, loc, name_sym);
688+
let qid = struct_env.get_qualified_id();
689+
let attributes = if op_prefix == well_known::PACK {
690+
if let Some(variant) = variant_opt {
691+
if let Some(def_idx) = self.struct_to_definition_idx.get(&qid) {
692+
let variant_index = struct_env
693+
.get_variants()
694+
.position(|v| v == variant)
695+
.unwrap();
696+
vec![FF::FunctionAttribute::PackVariant(
697+
*def_idx,
698+
variant_index as FF::VariantIndex,
699+
)]
700+
} else {
701+
vec![]
702+
}
703+
} else if let Some(def_idx) = self.struct_to_definition_idx.get(&qid) {
704+
vec![FF::FunctionAttribute::Pack(*def_idx)]
705+
} else {
706+
vec![]
707+
}
708+
} else {
709+
vec![]
710+
};
711+
// TODO: add function attribute
712+
// let function_attribute = FunctionAttribute::Pack(struct_env.get_definition_index());
684713

685714
let handle = FF::FunctionHandle {
686715
module,
@@ -689,7 +718,7 @@ impl ModuleGenerator {
689718
parameters: params_sig,
690719
return_: return_sig,
691720
access_specifiers: None,
692-
attributes: vec![],
721+
attributes,
693722
};
694723

695724
self.module.function_handles.push(handle);

third_party/move/move-compiler-v2/tests/ability-transform/borrowed_from_one_path.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ fun m::f($t0: u8, $t1: &vector<u64>): u64 {
10861086

10871087

10881088
============ disassembled file-format ==================
1089-
// Bytecode version v9
1089+
// Bytecode version v10
10901090
module 0x42::m
10911091
struct R has key
10921092
data: vector<u64>

third_party/move/move-compiler-v2/tests/ability-transform/by_reference.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3782,7 +3782,7 @@ fun <SELF>_0::check() {
37823782

37833783

37843784
============ disassembled file-format ==================
3785-
// Bytecode version v9
3785+
// Bytecode version v10
37863786
script
37873787
// Function definition at index 0
37883788
entry public fun main()

third_party/move/move-compiler-v2/tests/ability-transform/copy_ability_tuple.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public fun M::g($t0: &signer) {
705705

706706

707707
============ disassembled file-format ==================
708-
// Bytecode version v9
708+
// Bytecode version v10
709709
module 0x42::M
710710
struct R has key
711711
f: u64

0 commit comments

Comments
 (0)