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

Support replacing identifier, address and string constant in module bytecodes #1042

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasPar
[.request_init_functions.per_byte, "request_init_functions.per_byte", (5 + 1) * MUL],
[.check_compatibililty_inner.base, "check_compatibililty_inner.base", (5 + 1) * MUL],
[.check_compatibililty_inner.per_byte, "check_compatibililty_inner.per_byte", (5 + 1) * MUL],
[.remap_module_addresses_inner.base, "remap_module_addresses_inner.base", (5 + 1) * MUL],
[.remap_module_addresses_inner.per_byte, "remap_module_addresses_inner.per_byte", (5 + 1) * MUL],
[.replace_address_identifiers.base, "replace_address_identifiers.base", (5 + 1) * MUL],
[.replace_address_identifiers.per_byte, "replace_address_identifiers.per_byte", (5 + 1) * MUL],
[.replace_addresses_constant.base, "replace_addresses_constant.base", (5 + 1) * MUL],
[.replace_addresses_constant.per_byte, "replace_addresses_constant.per_byte", (5 + 1) * MUL],
[.replace_identifiers.base, "replace_identifiers.base", (5 + 1) * MUL],
[.replace_identifiers.per_byte, "replace_identifiers.per_byte", (5 + 1) * MUL],
[.replace_bytes_constant.base, "replace_bytes_constant.base", (5 + 1) * MUL],
[.replace_bytes_constant.per_byte, "replace_bytes_constant.per_byte", (5 + 1) * MUL],
]);
8 changes: 4 additions & 4 deletions examples/publish_modules/sources/publish.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// If the entry functions argument type is changed or the bytecode of the module is changed, and the tests are not updated, the tests will fail.
/// Please update the tests as follows:
/// 1. Compile the example/counter with `./target/debug/rooch move test -p examples/counter -d`
/// 2. Run the following command to get the bytecode of the compiled module: `xxd -c 0 -p examples/counter/build/counter/bytecode_modules/counter.mv`
/// 2. Run the following command to get the bytecode of the compiled module: `xxd -c 99999 -p examples/counter/build/counter/bytecode_modules/counter.mv`
/// 3. Copy the bytecode of the compiled module from the output of the above command, and update the `module_bytes` variable in the tests below.
module rooch_examples::publish {
use std::vector;
Expand All @@ -26,9 +26,9 @@ module rooch_examples::publish {
// with account 0x42
let module_bytes: vector<u8> = x"a11ceb0b060000000b010006020608030e26043406053a32076c7d08e9014006a902220acb02050cd002560da6030200000101010200030c00020400000005000100000600010000070201000008030400010907080108010a09010108010b0a0b0108040605060606010708010002070801060c0106080101030107080001080002070801050107090003070801060c090002060801050106090007636f756e7465720f6163636f756e745f73746f7261676507636f6e7465787407436f756e74657207436f6e7465787408696e63726561736509696e6372656173655f04696e69740576616c756511676c6f62616c5f626f72726f775f6d75740e676c6f62616c5f6d6f76655f746f0d676c6f62616c5f626f72726f77000000000000000000000000000000000000000000000000000000000000004200000000000000000000000000000000000000000000000000000000000000020520000000000000000000000000000000000000000000000000000000000000004200020108030001040001030b0011010201010000050d0b00070038000c010a01100014060100000000000000160b010f0015020200000001060b000b0106000000000000000012003801020301000001060b000700380210001402000000";
let modules = vector::singleton(move_module::new(module_bytes));
let old_addresses = vector::singleton(@0x42);
let new_addresses = vector::singleton(signer::address_of(account));
let remapped_modules = move_module::remap_module_addresses(modules, old_addresses, new_addresses);
let old_address = @0x42;
let new_address = signer::address_of(account);
let remapped_modules = move_module::binding_module_address(modules, old_address, new_address);
account_storage::publish_modules(ctx, account, remapped_modules);
}
}
233 changes: 215 additions & 18 deletions moveos/moveos-stdlib/moveos-stdlib/doc/move_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
- [Function `module_name`](#0x2_move_module_module_name)
- [Function `sort_and_verify_modules`](#0x2_move_module_sort_and_verify_modules)
- [Function `check_comatibility`](#0x2_move_module_check_comatibility)
- [Function `remap_module_addresses`](#0x2_move_module_remap_module_addresses)
- [Function `binding_module_address`](#0x2_move_module_binding_module_address)
- [Function `replace_module_identiner`](#0x2_move_module_replace_module_identiner)
- [Function `replace_struct_identifier`](#0x2_move_module_replace_struct_identifier)
- [Function `request_init_functions`](#0x2_move_module_request_init_functions)
- [Function `replace_address_identifiers`](#0x2_move_module_replace_address_identifiers)
- [Function `replace_identifiers`](#0x2_move_module_replace_identifiers)
- [Function `replace_addresses_constant`](#0x2_move_module_replace_addresses_constant)
- [Function `replace_bytes_constant`](#0x2_move_module_replace_bytes_constant)


<pre><code><b>use</b> <a href="">0x1::error</a>;
<b>use</b> <a href="">0x1::string</a>;
<b>use</b> <a href="">0x1::vector</a>;
</code></pre>


Expand Down Expand Up @@ -207,15 +214,14 @@ Abort if the new module is not compatible with the old module.

</details>

<a name="0x2_move_module_remap_module_addresses"></a>
<a name="0x2_move_module_binding_module_address"></a>

## Function `remap_module_addresses`
## Function `binding_module_address`

Remap addresses in module binary where the length of
<code>old_addresses</code> must equal to that of <code>new_addresses</code>.
Binding given module's address to the new address


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_remap_module_addresses">remap_module_addresses</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;, new_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_binding_module_address">binding_module_address</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_address: <b>address</b>, new_address: <b>address</b>): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
</code></pre>


Expand All @@ -224,13 +230,64 @@ Remap addresses in module binary where the length of
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_remap_module_addresses">remap_module_addresses</a>(
<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_binding_module_address">binding_module_address</a>(
modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;,
old_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;,
new_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;,
old_address: <b>address</b>,
new_address: <b>address</b>,
): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt; {
<b>let</b> bytes_vec = <a href="_empty">vector::empty</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;();
<b>let</b> i = 0u64;
<b>let</b> len = <a href="_length">vector::length</a>(&modules);
<b>while</b> (i &lt; len) {
<a href="_push_back">vector::push_back</a>(&<b>mut</b> bytes_vec, <a href="_pop_back">vector::pop_back</a>(&<b>mut</b> modules).byte_codes);
i = i + 1;
};
<b>let</b> old_addresses = <a href="_singleton">vector::singleton</a>(old_address);
<b>let</b> new_addresses = <a href="_singleton">vector::singleton</a>(new_address);

<b>let</b> rebinded_bytes = <a href="move_module.md#0x2_move_module_replace_address_identifiers">replace_address_identifiers</a>(bytes_vec, old_addresses, new_addresses);
<b>let</b> rebinded_bytes = <a href="move_module.md#0x2_move_module_replace_addresses_constant">replace_addresses_constant</a>(rebinded_bytes, old_addresses, new_addresses);
<b>let</b> rebinded_modules = <a href="_empty">vector::empty</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;();
i = 0u64;
<b>let</b> len = <a href="_length">vector::length</a>(&rebinded_bytes);
<b>while</b> (i &lt; len) {
<a href="_push_back">vector::push_back</a>(&<b>mut</b> rebinded_modules, <a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a> {
byte_codes: <a href="_pop_back">vector::pop_back</a>(&<b>mut</b> rebinded_bytes),
});
i = i + 1;
};
<a href="_destroy_empty">vector::destroy_empty</a>(rebinded_bytes);
rebinded_modules
}
</code></pre>



</details>

<a name="0x2_move_module_replace_module_identiner"></a>

## Function `replace_module_identiner`

Replace given module's identifier to the new ones


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_module_identiner">replace_module_identiner</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;, new_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_module_identiner">replace_module_identiner</a> (
modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;,
old_names: <a href="">vector</a>&lt;String&gt;,
new_names: <a href="">vector</a>&lt;String&gt;,
): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt; {
<b>assert</b>!(
<a href="_length">vector::length</a>(&old_addresses) == <a href="_length">vector::length</a>(&new_addresses),
<a href="_length">vector::length</a>(&old_names) == <a href="_length">vector::length</a>(&new_names),
<a href="_invalid_argument">error::invalid_argument</a>(<a href="move_module.md#0x2_move_module_ErrorLengthNotMatch">ErrorLengthNotMatch</a>)
);
<b>let</b> bytes_vec = <a href="_empty">vector::empty</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;();
Expand All @@ -240,19 +297,48 @@ Remap addresses in module binary where the length of
<a href="_push_back">vector::push_back</a>(&<b>mut</b> bytes_vec, <a href="_pop_back">vector::pop_back</a>(&<b>mut</b> modules).byte_codes);
i = i + 1;
};
<b>let</b> remapped_bytes = <a href="move_module.md#0x2_move_module_remap_module_addresses_inner">remap_module_addresses_inner</a>(bytes_vec, old_addresses, new_addresses);
// <b>let</b> remapped_bytes = <a href="move_module.md#0x2_move_module_remap_module_addresses_inner">remap_module_addresses_inner</a>(bytes_vec);
<b>let</b> remapped_modules = <a href="_empty">vector::empty</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;();

<b>let</b> rebinded_bytes = <a href="move_module.md#0x2_move_module_replace_identifiers">replace_identifiers</a>(bytes_vec, old_names, new_names);
<b>let</b> rebinded_modules = <a href="_empty">vector::empty</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;();
i = 0u64;
<b>let</b> len = <a href="_length">vector::length</a>(&remapped_bytes);
<b>let</b> len = <a href="_length">vector::length</a>(&rebinded_bytes);
<b>while</b> (i &lt; len) {
<a href="_push_back">vector::push_back</a>(&<b>mut</b> remapped_modules, <a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a> {
byte_codes: <a href="_pop_back">vector::pop_back</a>(&<b>mut</b> remapped_bytes),
<a href="_push_back">vector::push_back</a>(&<b>mut</b> rebinded_modules, <a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a> {
byte_codes: <a href="_pop_back">vector::pop_back</a>(&<b>mut</b> rebinded_bytes),
});
i = i + 1;
};
<a href="_destroy_empty">vector::destroy_empty</a>(remapped_bytes);
remapped_modules
<a href="_destroy_empty">vector::destroy_empty</a>(rebinded_bytes);
rebinded_modules
}
</code></pre>



</details>

<a name="0x2_move_module_replace_struct_identifier"></a>

## Function `replace_struct_identifier`

Replace given struct's identifier to the new ones


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_struct_identifier">replace_struct_identifier</a>(modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;, old_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;, new_names: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">move_module::MoveModule</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="move_module.md#0x2_move_module_replace_struct_identifier">replace_struct_identifier</a>(
modules: <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt;,
old_names: <a href="">vector</a>&lt;String&gt;,
new_names: <a href="">vector</a>&lt;String&gt;,
): <a href="">vector</a>&lt;<a href="move_module.md#0x2_move_module_MoveModule">MoveModule</a>&gt; {
<a href="move_module.md#0x2_move_module_replace_module_identiner">replace_module_identiner</a>(modules, old_names, new_names)
}
</code></pre>

Expand Down Expand Up @@ -283,4 +369,115 @@ account_address: address of all the modules



</details>

<a name="0x2_move_module_replace_address_identifiers"></a>

## Function `replace_address_identifiers`

Native function to replace addresses identifier in module binary where the length of
<code>old_addresses</code> must equal to that of <code>new_addresses</code>.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_address_identifiers">replace_address_identifiers</a>(bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, old_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;, new_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_address_identifiers">replace_address_identifiers</a>(
bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;,
old_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;,
new_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;,
): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;;
</code></pre>



</details>

<a name="0x2_move_module_replace_identifiers"></a>

## Function `replace_identifiers`

Native function to replace the name identifier <code>old_name</code> to <code>new_name</code> in module binary.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_identifiers">replace_identifiers</a>(bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, old_idents: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;, new_idents: <a href="">vector</a>&lt;<a href="_String">string::String</a>&gt;): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_identifiers">replace_identifiers</a>(
bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;,
old_idents: <a href="">vector</a>&lt;String&gt;,
new_idents: <a href="">vector</a>&lt;String&gt;,
): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;;
</code></pre>



</details>

<a name="0x2_move_module_replace_addresses_constant"></a>

## Function `replace_addresses_constant`

Native function to replace constant addresses in module binary where the length of
<code>old_addresses</code> must equal to that of <code>new_addresses</code>.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_addresses_constant">replace_addresses_constant</a>(bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, old_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;, new_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_addresses_constant">replace_addresses_constant</a>(
bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;,
old_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;,
new_addresses: <a href="">vector</a>&lt;<b>address</b>&gt;,
): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;;
</code></pre>



</details>

<a name="0x2_move_module_replace_bytes_constant"></a>

## Function `replace_bytes_constant`

Native function to replace constant bytes in module binary where the length of
<code>old_bytes</code> must equal to that of <code>new_bytes</code>.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_bytes_constant">replace_bytes_constant</a>(bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, old_bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, new_bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b>(<b>friend</b>) <b>fun</b> <a href="move_module.md#0x2_move_module_replace_bytes_constant">replace_bytes_constant</a>(
bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;,
old_bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;,
new_bytes: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;,
): <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;;
</code></pre>



</details>
Loading