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

polkavm-linker: Swap origin/target in ADD32/SUB32 #260

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions crates/polkavm-linker/src/program_from_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7960,6 +7960,17 @@ where
);
continue;
}
[(_, Kind::Mut(MutOp::Add, RelocationSize::U32, target_1)), (_, Kind::Mut(MutOp::Sub, RelocationSize::U32, target_2))] => {
relocations.insert(
current_location,
RelocationKind::Offset {
origin: *target_1,
target: *target_2,
size: SizeRelocationSize::Generic(RelocationSize::U32),
},
);
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Logically this isn't correct, so it's most likely not a proper fix (as evidenced by the failing tests).

We have RelocationKind::Offset { origin, target, ... }, so in origin we should have the base from which the offset is calculated, and in target we should have the destination, so logically this should match a relocation that looks like: offset = target - origin. However what you're doing here is the other way around: offset = origin - target.

Copy link
Contributor Author

@jarkkojs jarkkojs Jan 27, 2025

Choose a reason for hiding this comment

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

Logically this isn't correct, so it's most likely not a proper fix (as evidenced by the failing tests).

@koute, a snippet from your comment in the original issue:

                    0: R_RISCV_ADD32        .Lanon.cb76ee1d57f0804fce1a80e99f7c73f1.1
                    0: R_RISCV_SUB32        .Lswitch.table._ZN57_$LT$program_for_bug..Foo$u20$as$u20$core..fmt..Debug$GT$3fmt17h48080cab75666063E.1.rel

This is what the spec says:

Screenshot From 2025-01-27 19-05-46

It can be seen that the correct computation is (+*target_1) + (-*target_2) = *target_1 - *target_2.

Please review and point out if there is a step that went wrong.

The psABI-specification that I used my main reference is available here:
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/releases/download/draft-20240829-13bfa9f54634cb60d86b9b333e109f077805b4b3/riscv-abi.pdf

EDIT: I made this as clean and transparent as possible for more convenient review experience.

Copy link
Contributor Author

@jarkkojs jarkkojs Jan 27, 2025

Choose a reason for hiding this comment

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

I can comment my logic but I cannot comment on evidence as you did not explicitly point out the evidence. That said, I assume that we are talking about the doom test.

I'll lookup the doom example next for comparison.

Copy link
Contributor Author

@jarkkojs jarkkojs Jan 28, 2025

Choose a reason for hiding this comment

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

"Okay, so the offset is correct-ish, but it has the wrong sign."
A comment by @koute in: #247 (comment)

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'd also need a comment on whether there is "a test" or "tests" (plural) failing. I see in the CI run "a test" saying but your comment contradicts that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@koute thanks for the feedback :-) I'll have now new ideas how to approach this! All good.

[(_, Kind::Mut(MutOp::Add, size_1, target_1)), (_, Kind::Mut(MutOp::Sub, size_2, target_2))] if size_1 == size_2 => {
relocations.insert(
current_location,
Expand Down
87 changes: 64 additions & 23 deletions crates/polkavm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,11 +2042,12 @@ struct TestInstance {

const TEST_BLOB_32_ELF_ZST: &[u8] = include_bytes!("../../../test-data/test-blob_32.elf.zst");
const TEST_BLOB_64_ELF_ZST: &[u8] = include_bytes!("../../../test-data/test-blob_64.elf.zst");
const PROGRAM_FOR_BUG_32_ELF_ZST: &[u8] = include_bytes!("../../../test-data/program-for-bug_32.elf.zst");
const PROGRAM_FOR_BUG_64_ELF_ZST: &[u8] = include_bytes!("../../../test-data/program-for-bug_64.elf.zst");

impl TestInstance {
fn new(config: &Config, optimize: bool, is_64_bit: bool) -> Self {
fn new(blob: &'static [u8], config: &Config, optimize: bool, is_64_bit: bool) -> Self {
let _ = env_logger::try_init();
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let blob = get_blob_impl(optimize, is_64_bit, blob);

let engine = Engine::new(config).unwrap();
Expand Down Expand Up @@ -2110,6 +2111,14 @@ impl TestInstance {
})
.unwrap();

linker
.define_typed("debug_message", |caller: Caller<()>, buffer: u32, length: u32| {
let buffer = caller.instance.read_memory(buffer, length).unwrap();
let s = String::from_utf8(buffer).unwrap();
assert_eq!(&s, "Success");
})
.unwrap();

let instance_pre = linker.instantiate_pre(&module).unwrap();
let instance = instance_pre.instantiate().unwrap();

Expand All @@ -2126,14 +2135,16 @@ impl TestInstance {
}

fn test_blob_basic_test(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(), u32>("push_one_to_global_vec", ()).unwrap(), 1);
assert_eq!(i.call::<(), u32>("push_one_to_global_vec", ()).unwrap(), 2);
assert_eq!(i.call::<(), u32>("push_one_to_global_vec", ()).unwrap(), 3);
}

fn test_blob_atomic_fetch_add(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(u32,), u32>("atomic_fetch_add", (1,)).unwrap(), 0);
assert_eq!(i.call::<(u32,), u32>("atomic_fetch_add", (1,)).unwrap(), 1);
assert_eq!(i.call::<(u32,), u32>("atomic_fetch_add", (1,)).unwrap(), 2);
Expand All @@ -2144,7 +2155,8 @@ fn test_blob_atomic_fetch_add(config: Config, optimize: bool, is_64_bit: bool) {
}

fn test_blob_atomic_fetch_swap(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(u32,), u32>("atomic_fetch_swap", (10,)).unwrap(), 0);
assert_eq!(i.call::<(u32,), u32>("atomic_fetch_swap", (100,)).unwrap(), 10);
assert_eq!(i.call::<(u32,), u32>("atomic_fetch_swap", (1000,)).unwrap(), 100);
Expand All @@ -2169,7 +2181,8 @@ fn test_blob_atomic_fetch_minmax(config: Config, optimize: bool, is_64_bit: bool
("atomic_fetch_min_unsigned", minu),
];

let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
for (name, cb) in list {
for a in [-10, 0, 10] {
for b in [-10, 0, 10] {
Expand All @@ -2183,17 +2196,20 @@ fn test_blob_atomic_fetch_minmax(config: Config, optimize: bool, is_64_bit: bool
}

fn test_blob_hostcall(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(u32,), u32>("test_multiply_by_6", (10,)).unwrap(), 60);
}

fn test_blob_define_abi(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert!(i.call::<(), ()>("test_define_abi", ()).is_ok());
}

fn test_blob_input_registers(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert!(i.call::<(), ()>("test_input_registers", ()).is_ok());
}

Expand All @@ -2214,7 +2230,8 @@ fn test_blob_call_sbrk_from_host_function(config: Config, optimize: bool, is_64_
}

fn test_blob_program_memory_can_be_reused_and_cleared(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
let address = i.call::<(), u32>("get_global_address", ()).unwrap();

assert_eq!(i.instance.read_memory(address, 4).unwrap(), [0x00, 0x00, 0x00, 0x00]);
Expand All @@ -2233,7 +2250,8 @@ fn test_blob_program_memory_can_be_reused_and_cleared(config: Config, optimize:
}

fn test_blob_out_of_bounds_memory_access_generates_a_trap(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
let address = i.call::<(), u32>("get_global_address", ()).unwrap();
assert_eq!(i.call::<(u32,), u32>("read_u32", (address,)).unwrap(), 0);
i.call::<(), ()>("increment_global", ()).unwrap();
Expand All @@ -2246,7 +2264,8 @@ fn test_blob_out_of_bounds_memory_access_generates_a_trap(config: Config, optimi
}

fn test_blob_call_sbrk_impl(config: Config, optimize: bool, is_64_bit: bool, mut call_sbrk: impl FnMut(&mut TestInstance, u32) -> u32) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
let memory_map = i.module.memory_map().clone();
let heap_base = memory_map.heap_base();
let page_size = memory_map.page_size();
Expand Down Expand Up @@ -2301,7 +2320,8 @@ fn test_blob_call_sbrk_impl(config: Config, optimize: bool, is_64_bit: bool, mut
}

fn test_blob_add_u32(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(u32, u32), u32>("add_u32", (1, 2,)).unwrap(), 3);
assert_eq!(i.instance.reg(Reg::A0), 3);

Expand All @@ -2318,7 +2338,8 @@ fn test_blob_add_u32(config: Config, optimize: bool, is_64_bit: bool) {
}

fn test_blob_add_u64(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(u64, u64), u64>("add_u64", (1, 2,)).unwrap(), 3);
assert_eq!(i.instance.reg(Reg::A0), 3);
assert_eq!(
Expand All @@ -2328,19 +2349,22 @@ fn test_blob_add_u64(config: Config, optimize: bool, is_64_bit: bool) {
}

fn test_blob_xor_imm_u32(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
for value in [0, 0xaaaaaaaa, 0x55555555, 0x12345678, 0xffffffff] {
assert_eq!(i.call::<(u32,), u32>("xor_imm_u32", (value,)).unwrap(), value ^ 0xfb8f5c1e);
}
}

fn test_blob_branch_less_than_zero(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
i.call::<(), ()>("test_branch_less_than_zero", ()).unwrap();
}

fn test_blob_fetch_add_atomic_u64(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.call::<(u64,), u64>("fetch_add_atomic_u64", (1,)).unwrap(), 0);
assert_eq!(i.call::<(u64,), u64>("fetch_add_atomic_u64", (0,)).unwrap(), 1);
assert_eq!(i.call::<(u64,), u64>("fetch_add_atomic_u64", (0,)).unwrap(), 1);
Expand All @@ -2349,22 +2373,26 @@ fn test_blob_fetch_add_atomic_u64(config: Config, optimize: bool, is_64_bit: boo
}

fn test_blob_cmov_if_zero_with_zero_reg(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
i.call::<(), ()>("cmov_if_zero_with_zero_reg", ()).unwrap();
}

fn test_blob_cmov_if_not_zero_with_zero_reg(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
i.call::<(), ()>("cmov_if_not_zero_with_zero_reg", ()).unwrap();
}

fn test_blob_min_stack_size(config: Config, optimize: bool, is_64_bit: bool) {
let i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let i = TestInstance::new(blob, &config, optimize, is_64_bit);
assert_eq!(i.instance.module().memory_map().stack_size(), 65536);
}

fn test_blob_negate_and_add(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
if !is_64_bit {
assert_eq!(i.call::<(u32, u32), u32>("negate_and_add", (123, 1,)).unwrap(), 15);
} else {
Expand All @@ -2373,12 +2401,14 @@ fn test_blob_negate_and_add(config: Config, optimize: bool, is_64_bit: bool) {
}

fn test_blob_return_tuple_from_import(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
i.call::<(), ()>("test_return_tuple", ()).unwrap();
}

fn test_blob_return_tuple_from_export(config: Config, optimize: bool, is_64_bit: bool) {
let mut i = TestInstance::new(&config, optimize, is_64_bit);
let blob = if is_64_bit { TEST_BLOB_64_ELF_ZST } else { TEST_BLOB_32_ELF_ZST };
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
if is_64_bit {
let a0 = 0x123456789abcdefe_u64;
let a1 = 0x1122334455667788_u64;
Expand Down Expand Up @@ -2406,6 +2436,16 @@ fn test_blob_return_tuple_from_export(config: Config, optimize: bool, is_64_bit:
}
}

fn test_program_for_bug(config: Config, optimize: bool, is_64_bit: bool) {
let blob = if is_64_bit {
PROGRAM_FOR_BUG_64_ELF_ZST
} else {
PROGRAM_FOR_BUG_32_ELF_ZST
};
let mut i = TestInstance::new(blob, &config, optimize, is_64_bit);
i.call::<(), ()>("deploy", ()).unwrap();
}

fn basic_gas_metering(config: Config, gas_metering_kind: GasMeteringKind) {
let _ = env_logger::try_init();

Expand Down Expand Up @@ -3196,6 +3236,7 @@ run_test_blob_tests! {
test_blob_negate_and_add
test_blob_return_tuple_from_import
test_blob_return_tuple_from_export
test_program_for_bug
}

macro_rules! assert_impl {
Expand Down
54 changes: 47 additions & 7 deletions guest-programs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions guest-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = [
"bench-prime-sieve",

# Tests
"program-for-bug",
"test-blob",
]

Expand Down
1 change: 1 addition & 0 deletions guest-programs/build-test-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ function build_test_data() {

build_test_data "bench-pinky" "release"
build_test_data "test-blob" "no-lto"
build_test_data "program-for-bug" "no-lto"
Loading
Loading