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

Mis-aligned allocation in ink #1535

Closed
kvinwang opened this issue Dec 13, 2022 · 4 comments · Fixed by #2100
Closed

Mis-aligned allocation in ink #1535

kvinwang opened this issue Dec 13, 2022 · 4 comments · Fixed by #2100
Assignees
Labels
C-bug Something isn't working

Comments

@kvinwang
Copy link
Contributor

kvinwang commented Dec 13, 2022

I am trying to port the quickjs runtime into ink. It always crashes because it rely on the heap allocation alignment.

Looks like the ink's bump allocator doesn't obey Rust's alignment requirements.
Given the follow code:

let buf1 = Vec::<u8>::with_capacity(3);
let buf2 = Vec::<u64>::with_capacity(1);
info!("ptr to buf1 :{:?}", buf1.as_ptr());
info!("ptr to buf2 :{:?}", buf2.as_ptr());
info!("align of u64:{}", align_of::<u64>());

Runing in ink would get somthing like:

 ptr to buf1 :0x30022
 ptr to buf2 :0x30025
 align of u64:8

Where the ptr of buf2 is mis-aligned.

Expected behavior
It should be aligned as Rust's default allocator do. See here which meets the Allocator requirements.

@HCastano
Copy link
Contributor

@kvinwang can you share the code that you're trying to run?

@kvinwang
Copy link
Contributor Author

@kvinwang can you share the code that you're trying to run?

Here is the project I am working on.
Steps to reproduce:

  1. Clone the source
git clone https://github.com/kvinwang/qjs.git --recursive
cd qjs
  1. Remove the feature wee-alloc here: https://github.com/kvinwang/qjs/blob/bd36fa41d9b7687bb96053e4c8bec8381b8d893f/Cargo.toml#L13
  2. Use another way to print the log here: https://github.com/kvinwang/qjs/blob/bd36fa41d9b7687bb96053e4c8bec8381b8d893f/lib.rs#L10
  3. Build
cargo +nightly contract build --skip-linting --release

Now we got a compiled contract with a method eval which accepts a piece of JavaScript code as input.
For example: JsTest::eval("console.log('Hello, World!')");
However, the contract is indeterministic, so we need another contract to call it via delegate_call.

@SkymanOne
Copy link
Contributor

@kvinwang Sorry for picking this up late. The links seem to be broken. Can you please either update them or confirm the issue?

@SkymanOne
Copy link
Contributor

Just for records. I managed to partially reproduce the issue by inspecting logs from

#[ink(message)]
pub fn buffer(&self) {
    let buf1 = Vec::<u8>::with_capacity(3);
    let buf2 = Vec::<u64>::with_capacity(1);
    ink::env::debug_println!("ptr to buf1 :{:?}", buf1.as_ptr());
    ink::env::debug_println!("ptr to buf2 :{:?}", buf2.as_ptr());
    ink::env::debug_println!("align of u64:{}", align_of::<u64>());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants