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

refac: Use offset from SDK for executable loaders #6693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Abeeujah
Copy link

@Abeeujah Abeeujah commented Nov 4, 2024

Description

Uses the offset exposed by the SDK instead of recalculating it and having to manually update the code each time the implementation changes.

closes #6683

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@Abeeujah Abeeujah requested a review from a team as a code owner November 4, 2024 14:24
@CLAassistant
Copy link

CLAassistant commented Nov 4, 2024

CLA assistant check
All committers have signed the CLA.

@Abeeujah
Copy link
Author

Abeeujah commented Nov 4, 2024

Hi @kayagokalp @Br1ght0ne @Salka1988 @hal3e please can you review?

@Abeeujah Abeeujah reopened this Nov 5, 2024
Copy link
Member

@kayagokalp kayagokalp left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks for the contribution. Can we add a test maybe? I left the details in the comment 🚀

Comment on lines +385 to +403
let loader_data_section_offset = loader.data_offset_in_code();
if let ProgramABI::Fuel(mut fuel_abi) = pkg.program_abi.clone() {
println_action_green("Generating", "loader abi for the uploaded executable.");
let json_abi_path = out_dir.join(format!("{pkg_name}-loader-abi.json"));
let original_data_section = extract_data_offset(&pkg.bytecode.bytes).unwrap();
let offset_shift = original_data_section - loader_data_section_offset;
// if there are configurables in the abi we need to shift them by `offset_shift`.
let configurables = fuel_abi.configurables.clone().map(|configs| {
configs
.into_iter()
.map(|config| Configurable {
offset: config.offset - offset_shift as u64,
..config.clone()
})
.collect()
});
fuel_abi.configurables = configurables;
let json_string = serde_json::to_string_pretty(&fuel_abi)?;
std::fs::write(json_abi_path, json_string)?;
Copy link
Member

Choose a reason for hiding this comment

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

Looks good to me, but I believe we need a test for this. Especially because we are not sure what is going to happen if the binary does not have a data section. In earlier implementation it was obvious that the function to calculate offset returns None in that case but now I am not sure what is going to happen.

So if you are up for it, let's add a test which get's its oracle values (like the calculated shifted values with the previous implementation) and in a next commit let's apply this change and see if the calculated values are same.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @kayagokalp I did look at the SDK before I implemented this change, the function implementation is guaranteed to always return an offset, and cases where it wouldn't the program crashes here's a snippet from Executable<Loader> impl block

pub fn data_offset_in_code(&self) -> usize {
        self.code_with_offset().1
    }

    fn code_with_offset(&self) -> (Vec<u8>, usize) {
        let mut code = self.state.code.clone();

        self.state.configurables.update_constants_in(&mut code);

        let blob_id = self.blob().id();

        transform_into_configurable_loader(code, &blob_id)
            .expect("checked before turning into a Executable<Loader>")
    }

But if you'd want a test after looking at this, what should I be testing for? that the value isn't zero? within a specific range? I'd like to hear your POV

Copy link
Member

Choose a reason for hiding this comment

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

Hey again, it is possible that a binary does not contain any data section, that happens if the sway code does not have any constants and no configurable section. So if SDK is expecting this to be checked before calling the function, we might need to check this explicitly.

For testing we can use a example test script or predicate's binary from /test/data. And (possibly) extract the shift calculation to a separate function so that we can write tests against it.

Basically call this new calculate shift function and check if the calculated shift is being calculated correctly for two types of sway scripts:

  1. A script that has a single main function and nothing else.
  2. The script like https://github.com/FuelLabs/sway/blob/master/forc-plugins/forc-client/test/data/deployed_script/src/main.sw which needs this shift.

I can help with testing if you need it feel free to ping me

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I'd need some help with testing it, How do I ping you? I tried on twitter, 2 days ago and you haven't responded

Copy link
Member

@kayagokalp kayagokalp Nov 8, 2024

Choose a reason for hiding this comment

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

I will be adding the tests to this PR directly to better streamline the process, possibly tomorrow. Other than the tests this looks good to me, after the test I'll approve and hunt for your second approval :) Thanks again for the contribution.

@kayagokalp kayagokalp added code quality forc-deploy Everything to do with forc-deploy labels Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality forc-deploy Everything to do with forc-deploy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use offset from SDK for executable loaders instead of re-calculating it
3 participants