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

f64 return value mismatch #183

Closed
CryZe opened this issue Feb 15, 2019 · 11 comments
Closed

f64 return value mismatch #183

CryZe opened this issue Feb 15, 2019 · 11 comments
Assignees

Comments

@CryZe
Copy link
Contributor

CryZe commented Feb 15, 2019

Okay, so I've been able to test out wasmer on Windows now and for the most part it seems to work fine, except the occasional segfaults (I have yet to figure out when they happen) and the issue this thread is about: I have an exported function that returns 250.0 f64 value, but the host sees a garbage value instead:

fn main() {
    let binary = [
        0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x23, 0x07, 0x60, 0x03, 0x7F, 0x7F,
        0x7F, 0x01, 0x7F, 0x60, 0x00, 0x00, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F,
        0x7F, 0x00, 0x60, 0x01, 0x7F, 0x00, 0x60, 0x01, 0x7F, 0x01, 0x7E, 0x60, 0x00, 0x01, 0x7C,
        0x03, 0x0B, 0x0A, 0x01, 0x02, 0x02, 0x03, 0x04, 0x04, 0x04, 0x04, 0x05, 0x06, 0x04, 0x05,
        0x01, 0x70, 0x01, 0x05, 0x05, 0x05, 0x03, 0x01, 0x00, 0x11, 0x06, 0x19, 0x03, 0x7F, 0x01,
        0x41, 0x80, 0x80, 0xC0, 0x00, 0x0B, 0x7F, 0x00, 0x41, 0xB0, 0x82, 0xC0, 0x00, 0x0B, 0x7F,
        0x00, 0x41, 0xB0, 0x82, 0xC0, 0x00, 0x0B, 0x07, 0x4D, 0x05, 0x06, 0x6D, 0x65, 0x6D, 0x6F,
        0x72, 0x79, 0x02, 0x00, 0x19, 0x5F, 0x5F, 0x69, 0x6E, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74,
        0x5F, 0x66, 0x75, 0x6E, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x5F, 0x74, 0x61, 0x62, 0x6C, 0x65,
        0x01, 0x00, 0x0B, 0x5F, 0x5F, 0x68, 0x65, 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, 0x65, 0x03,
        0x01, 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, 0x02, 0x09,
        0x67, 0x61, 0x6D, 0x65, 0x5F, 0x74, 0x69, 0x6D, 0x65, 0x00, 0x09, 0x09, 0x0A, 0x01, 0x00,
        0x41, 0x01, 0x0B, 0x04, 0x01, 0x02, 0x07, 0x08, 0x0A, 0xCC, 0x0E, 0x0A, 0x89, 0x01, 0x01,
        0x01, 0x7F, 0x23, 0x00, 0x41, 0x40, 0x6A, 0x22, 0x00, 0x24, 0x00, 0x20, 0x00, 0x41, 0x10,
        0x36, 0x02, 0x0C, 0x20, 0x00, 0x41, 0xAE, 0x81, 0xC0, 0x00, 0x36, 0x02, 0x08, 0x20, 0x00,
        0x41, 0x34, 0x6A, 0x41, 0x01, 0x36, 0x02, 0x00, 0x20, 0x00, 0x41, 0x24, 0x6A, 0x41, 0x02,
        0x36, 0x02, 0x00, 0x20, 0x00, 0x41, 0x1C, 0x6A, 0x41, 0x02, 0x36, 0x02, 0x00, 0x20, 0x00,
        0x41, 0x02, 0x36, 0x02, 0x2C, 0x20, 0x00, 0x41, 0x98, 0x80, 0xC0, 0x00, 0x36, 0x02, 0x18,
        0x20, 0x00, 0x41, 0x02, 0x36, 0x02, 0x14, 0x20, 0x00, 0x41, 0xC0, 0x81, 0xC0, 0x00, 0x36,
        0x02, 0x10, 0x20, 0x00, 0x20, 0x00, 0x41, 0x38, 0x6A, 0x36, 0x02, 0x30, 0x20, 0x00, 0x20,
        0x00, 0x41, 0x08, 0x6A, 0x36, 0x02, 0x28, 0x20, 0x00, 0x20, 0x00, 0x41, 0x28, 0x6A, 0x36,
        0x02, 0x20, 0x20, 0x00, 0x41, 0x10, 0x6A, 0x41, 0xD0, 0x81, 0xC0, 0x00, 0x10, 0x03, 0x00,
        0x0B, 0x1C, 0x00, 0x20, 0x01, 0x28, 0x02, 0x18, 0x41, 0xA0, 0x81, 0xC0, 0x00, 0x41, 0x0E,
        0x20, 0x01, 0x41, 0x1C, 0x6A, 0x28, 0x02, 0x00, 0x28, 0x02, 0x0C, 0x11, 0x00, 0x00, 0x0B,
        0xB7, 0x09, 0x01, 0x0D, 0x7F, 0x02, 0x40, 0x02, 0x40, 0x23, 0x00, 0x41, 0x10, 0x6B, 0x22,
        0x04, 0x24, 0x00, 0x20, 0x01, 0x28, 0x02, 0x10, 0x21, 0x03, 0x20, 0x00, 0x28, 0x02, 0x04,
        0x21, 0x05, 0x20, 0x00, 0x28, 0x02, 0x00, 0x21, 0x07, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40,
        0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x01, 0x28, 0x02, 0x08, 0x22, 0x0D,
        0x41, 0x01, 0x46, 0x04, 0x40, 0x20, 0x03, 0x0D, 0x01, 0x0C, 0x02, 0x0B, 0x20, 0x03, 0x45,
        0x0D, 0x06, 0x0B, 0x20, 0x05, 0x04, 0x40, 0x20, 0x07, 0x20, 0x05, 0x6A, 0x21, 0x0A, 0x20,
        0x01, 0x41, 0x14, 0x6A, 0x28, 0x02, 0x00, 0x41, 0x7F, 0x73, 0x21, 0x06, 0x20, 0x07, 0x22,
        0x00, 0x21, 0x0E, 0x02, 0x40, 0x02, 0x40, 0x03, 0x40, 0x20, 0x00, 0x41, 0x01, 0x6A, 0x21,
        0x03, 0x02, 0x40, 0x20, 0x00, 0x2C, 0x00, 0x00, 0x22, 0x02, 0x41, 0x00, 0x4E, 0x04, 0x40,
        0x20, 0x02, 0x41, 0xFF, 0x01, 0x71, 0x21, 0x02, 0x20, 0x03, 0x21, 0x00, 0x20, 0x06, 0x41,
        0x01, 0x6A, 0x22, 0x06, 0x0D, 0x01, 0x0C, 0x03, 0x0B, 0x02, 0x40, 0x02, 0x40, 0x20, 0x03,
        0x20, 0x0A, 0x47, 0x04, 0x40, 0x20, 0x03, 0x2D, 0x00, 0x00, 0x41, 0x3F, 0x71, 0x21, 0x08,
        0x20, 0x00, 0x41, 0x02, 0x6A, 0x22, 0x00, 0x21, 0x03, 0x20, 0x02, 0x41, 0x1F, 0x71, 0x21,
        0x0B, 0x20, 0x02, 0x41, 0xFF, 0x01, 0x71, 0x22, 0x02, 0x41, 0xE0, 0x01, 0x49, 0x0D, 0x01,
        0x0C, 0x02, 0x0B, 0x41, 0x00, 0x21, 0x08, 0x20, 0x0A, 0x21, 0x00, 0x20, 0x02, 0x41, 0x1F,
        0x71, 0x21, 0x0B, 0x20, 0x02, 0x41, 0xFF, 0x01, 0x71, 0x22, 0x02, 0x41, 0xE0, 0x01, 0x4F,
        0x0D, 0x01, 0x0B, 0x20, 0x08, 0x20, 0x0B, 0x41, 0x06, 0x74, 0x72, 0x21, 0x02, 0x20, 0x03,
        0x21, 0x00, 0x20, 0x06, 0x41, 0x01, 0x6A, 0x22, 0x06, 0x0D, 0x01, 0x0C, 0x03, 0x0B, 0x02,
        0x40, 0x02, 0x40, 0x20, 0x00, 0x20, 0x0A, 0x47, 0x04, 0x40, 0x20, 0x00, 0x41, 0x01, 0x6A,
        0x22, 0x03, 0x21, 0x0C, 0x20, 0x00, 0x2D, 0x00, 0x00, 0x41, 0x3F, 0x71, 0x20, 0x08, 0x41,
        0x06, 0x74, 0x72, 0x21, 0x08, 0x20, 0x02, 0x41, 0xF0, 0x01, 0x49, 0x0D, 0x01, 0x0C, 0x02,
        0x0B, 0x20, 0x0A, 0x21, 0x0C, 0x20, 0x08, 0x41, 0x06, 0x74, 0x21, 0x08, 0x20, 0x02, 0x41,
        0xF0, 0x01, 0x4F, 0x0D, 0x01, 0x0B, 0x20, 0x08, 0x20, 0x0B, 0x41, 0x0C, 0x74, 0x72, 0x21,
        0x02, 0x20, 0x03, 0x21, 0x00, 0x20, 0x06, 0x41, 0x01, 0x6A, 0x22, 0x06, 0x0D, 0x01, 0x0C,
        0x03, 0x0B, 0x02, 0x7F, 0x20, 0x0C, 0x20, 0x0A, 0x47, 0x04, 0x40, 0x20, 0x0C, 0x2D, 0x00,
        0x00, 0x41, 0x3F, 0x71, 0x21, 0x02, 0x20, 0x0C, 0x41, 0x01, 0x6A, 0x0C, 0x01, 0x0B, 0x41,
        0x00, 0x21, 0x02, 0x20, 0x03, 0x0B, 0x21, 0x00, 0x20, 0x08, 0x41, 0x06, 0x74, 0x20, 0x0B,
        0x41, 0x12, 0x74, 0x41, 0x80, 0x80, 0xF0, 0x00, 0x71, 0x72, 0x20, 0x02, 0x72, 0x22, 0x02,
        0x41, 0x80, 0x80, 0xC4, 0x00, 0x46, 0x0D, 0x03, 0x20, 0x06, 0x41, 0x01, 0x6A, 0x22, 0x06,
        0x45, 0x0D, 0x02, 0x0B, 0x20, 0x09, 0x20, 0x0E, 0x6B, 0x20, 0x00, 0x6A, 0x21, 0x09, 0x20,
        0x00, 0x21, 0x0E, 0x20, 0x0A, 0x20, 0x00, 0x47, 0x0D, 0x00, 0x0B, 0x0C, 0x01, 0x0B, 0x20,
        0x02, 0x41, 0x80, 0x80, 0xC4, 0x00, 0x46, 0x0D, 0x00, 0x02, 0x40, 0x02, 0x40, 0x20, 0x09,
        0x45, 0x0D, 0x00, 0x20, 0x09, 0x20, 0x05, 0x46, 0x0D, 0x00, 0x41, 0x00, 0x21, 0x00, 0x20,
        0x09, 0x20, 0x05, 0x4F, 0x0D, 0x01, 0x20, 0x07, 0x20, 0x09, 0x6A, 0x2C, 0x00, 0x00, 0x41,
        0x40, 0x48, 0x0D, 0x01, 0x0B, 0x20, 0x07, 0x21, 0x00, 0x0B, 0x20, 0x09, 0x20, 0x05, 0x20,
        0x00, 0x1B, 0x21, 0x05, 0x20, 0x00, 0x20, 0x07, 0x20, 0x00, 0x1B, 0x21, 0x07, 0x0B, 0x20,
        0x0D, 0x45, 0x0D, 0x08, 0x0C, 0x01, 0x0B, 0x41, 0x00, 0x21, 0x05, 0x20, 0x0D, 0x45, 0x0D,
        0x07, 0x0B, 0x41, 0x00, 0x21, 0x03, 0x20, 0x05, 0x04, 0x40, 0x20, 0x05, 0x21, 0x02, 0x20,
        0x07, 0x21, 0x00, 0x03, 0x40, 0x20, 0x03, 0x20, 0x00, 0x2D, 0x00, 0x00, 0x41, 0xC0, 0x01,
        0x71, 0x41, 0x80, 0x01, 0x46, 0x6A, 0x21, 0x03, 0x20, 0x00, 0x41, 0x01, 0x6A, 0x21, 0x00,
        0x20, 0x02, 0x41, 0x7F, 0x6A, 0x22, 0x02, 0x0D, 0x00, 0x0B, 0x0B, 0x20, 0x05, 0x20, 0x03,
        0x6B, 0x20, 0x01, 0x41, 0x0C, 0x6A, 0x28, 0x02, 0x00, 0x22, 0x06, 0x4F, 0x0D, 0x06, 0x41,
        0x00, 0x21, 0x03, 0x20, 0x05, 0x04, 0x40, 0x20, 0x05, 0x21, 0x02, 0x20, 0x07, 0x21, 0x00,
        0x03, 0x40, 0x20, 0x03, 0x20, 0x00, 0x2D, 0x00, 0x00, 0x41, 0xC0, 0x01, 0x71, 0x41, 0x80,
        0x01, 0x46, 0x6A, 0x21, 0x03, 0x20, 0x00, 0x41, 0x01, 0x6A, 0x21, 0x00, 0x20, 0x02, 0x41,
        0x7F, 0x6A, 0x22, 0x02, 0x0D, 0x00, 0x0B, 0x0B, 0x20, 0x03, 0x20, 0x05, 0x6B, 0x20, 0x06,
        0x6A, 0x21, 0x02, 0x41, 0x00, 0x20, 0x01, 0x2D, 0x00, 0x30, 0x22, 0x00, 0x20, 0x00, 0x41,
        0x03, 0x46, 0x1B, 0x41, 0x03, 0x71, 0x22, 0x00, 0x45, 0x0D, 0x01, 0x20, 0x00, 0x41, 0x02,
        0x46, 0x0D, 0x02, 0x41, 0x00, 0x21, 0x06, 0x0C, 0x03, 0x00, 0x0B, 0x00, 0x0B, 0x20, 0x02,
        0x21, 0x06, 0x41, 0x00, 0x21, 0x02, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x41, 0x01, 0x6A, 0x41,
        0x01, 0x76, 0x21, 0x06, 0x20, 0x02, 0x41, 0x01, 0x76, 0x21, 0x02, 0x0B, 0x20, 0x04, 0x41,
        0x00, 0x36, 0x02, 0x0C, 0x02, 0x7F, 0x20, 0x01, 0x28, 0x02, 0x04, 0x22, 0x00, 0x41, 0xFF,
        0x00, 0x4D, 0x04, 0x40, 0x20, 0x04, 0x20, 0x00, 0x3A, 0x00, 0x0C, 0x41, 0x01, 0x0C, 0x01,
        0x0B, 0x20, 0x00, 0x41, 0xFF, 0x0F, 0x4D, 0x04, 0x40, 0x20, 0x04, 0x20, 0x00, 0x41, 0x3F,
        0x71, 0x41, 0x80, 0x01, 0x72, 0x3A, 0x00, 0x0D, 0x20, 0x04, 0x20, 0x00, 0x41, 0x06, 0x76,
        0x41, 0x1F, 0x71, 0x41, 0xC0, 0x01, 0x72, 0x3A, 0x00, 0x0C, 0x41, 0x02, 0x0C, 0x01, 0x0B,
        0x20, 0x00, 0x41, 0xFF, 0xFF, 0x03, 0x4D, 0x04, 0x40, 0x20, 0x04, 0x20, 0x00, 0x41, 0x3F,
        0x71, 0x41, 0x80, 0x01, 0x72, 0x3A, 0x00, 0x0E, 0x20, 0x04, 0x20, 0x00, 0x41, 0x06, 0x76,
        0x41, 0x3F, 0x71, 0x41, 0x80, 0x01, 0x72, 0x3A, 0x00, 0x0D, 0x20, 0x04, 0x20, 0x00, 0x41,
        0x0C, 0x76, 0x41, 0x0F, 0x71, 0x41, 0xE0, 0x01, 0x72, 0x3A, 0x00, 0x0C, 0x41, 0x03, 0x0C,
        0x01, 0x0B, 0x20, 0x04, 0x20, 0x00, 0x41, 0x3F, 0x71, 0x41, 0x80, 0x01, 0x72, 0x3A, 0x00,
        0x0F, 0x20, 0x04, 0x20, 0x00, 0x41, 0x12, 0x76, 0x41, 0xF0, 0x01, 0x72, 0x3A, 0x00, 0x0C,
        0x20, 0x04, 0x20, 0x00, 0x41, 0x06, 0x76, 0x41, 0x3F, 0x71, 0x41, 0x80, 0x01, 0x72, 0x3A,
        0x00, 0x0E, 0x20, 0x04, 0x20, 0x00, 0x41, 0x0C, 0x76, 0x41, 0x3F, 0x71, 0x41, 0x80, 0x01,
        0x72, 0x3A, 0x00, 0x0D, 0x41, 0x04, 0x0B, 0x21, 0x03, 0x41, 0x7F, 0x21, 0x00, 0x02, 0x40,
        0x02, 0x40, 0x02, 0x40, 0x03, 0x40, 0x20, 0x00, 0x41, 0x01, 0x6A, 0x22, 0x00, 0x20, 0x02,
        0x4F, 0x0D, 0x01, 0x20, 0x01, 0x41, 0x18, 0x6A, 0x28, 0x02, 0x00, 0x20, 0x04, 0x41, 0x0C,
        0x6A, 0x20, 0x03, 0x20, 0x01, 0x41, 0x1C, 0x6A, 0x28, 0x02, 0x00, 0x28, 0x02, 0x0C, 0x11,
        0x00, 0x00, 0x45, 0x0D, 0x00, 0x0B, 0x0C, 0x04, 0x0B, 0x20, 0x01, 0x41, 0x18, 0x6A, 0x22,
        0x02, 0x28, 0x02, 0x00, 0x20, 0x07, 0x20, 0x05, 0x20, 0x01, 0x41, 0x1C, 0x6A, 0x22, 0x01,
        0x28, 0x02, 0x00, 0x28, 0x02, 0x0C, 0x11, 0x00, 0x00, 0x45, 0x0D, 0x01, 0x0B, 0x0C, 0x02,
        0x0B, 0x41, 0x7F, 0x21, 0x00, 0x02, 0x40, 0x03, 0x40, 0x20, 0x00, 0x41, 0x01, 0x6A, 0x22,
        0x00, 0x20, 0x06, 0x4F, 0x0D, 0x01, 0x20, 0x02, 0x28, 0x02, 0x00, 0x20, 0x04, 0x41, 0x0C,
        0x6A, 0x20, 0x03, 0x20, 0x01, 0x28, 0x02, 0x00, 0x28, 0x02, 0x0C, 0x11, 0x00, 0x00, 0x45,
        0x0D, 0x00, 0x0B, 0x0C, 0x02, 0x0B, 0x20, 0x04, 0x41, 0x10, 0x6A, 0x24, 0x00, 0x41, 0x00,
        0x0F, 0x0B, 0x20, 0x01, 0x28, 0x02, 0x18, 0x20, 0x07, 0x20, 0x05, 0x20, 0x01, 0x41, 0x1C,
        0x6A, 0x28, 0x02, 0x00, 0x28, 0x02, 0x0C, 0x11, 0x00, 0x00, 0x21, 0x00, 0x20, 0x04, 0x41,
        0x10, 0x6A, 0x24, 0x00, 0x20, 0x00, 0x0F, 0x0B, 0x20, 0x04, 0x41, 0x10, 0x6A, 0x24, 0x00,
        0x41, 0x01, 0x0F, 0x0B, 0x20, 0x01, 0x28, 0x02, 0x18, 0x20, 0x07, 0x20, 0x05, 0x20, 0x01,
        0x41, 0x1C, 0x6A, 0x28, 0x02, 0x00, 0x28, 0x02, 0x0C, 0x11, 0x00, 0x00, 0x21, 0x00, 0x20,
        0x04, 0x41, 0x10, 0x6A, 0x24, 0x00, 0x20, 0x00, 0x0B, 0x4A, 0x02, 0x01, 0x7F, 0x01, 0x7E,
        0x23, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, 0x24, 0x00, 0x20, 0x01, 0x29, 0x02, 0x00, 0x21,
        0x03, 0x20, 0x02, 0x41, 0x14, 0x6A, 0x20, 0x01, 0x29, 0x02, 0x08, 0x37, 0x02, 0x00, 0x20,
        0x02, 0x20, 0x03, 0x37, 0x02, 0x0C, 0x20, 0x02, 0x20, 0x00, 0x36, 0x02, 0x08, 0x20, 0x02,
        0x41, 0xE0, 0x81, 0xC0, 0x00, 0x36, 0x02, 0x04, 0x20, 0x02, 0x41, 0xE0, 0x80, 0xC0, 0x00,
        0x36, 0x02, 0x00, 0x20, 0x02, 0x10, 0x05, 0x00, 0x0B, 0x6D, 0x01, 0x01, 0x7F, 0x41, 0x01,
        0x21, 0x00, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x41, 0x88, 0x82, 0xC0, 0x00, 0x28, 0x02,
        0x00, 0x41, 0x01, 0x46, 0x04, 0x40, 0x41, 0x8C, 0x82, 0xC0, 0x00, 0x41, 0x8C, 0x82, 0xC0,
        0x00, 0x28, 0x02, 0x00, 0x41, 0x01, 0x6A, 0x22, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00, 0x41,
        0x03, 0x49, 0x0D, 0x01, 0x0C, 0x02, 0x0B, 0x41, 0x88, 0x82, 0xC0, 0x00, 0x42, 0x81, 0x80,
        0x80, 0x80, 0x10, 0x37, 0x03, 0x00, 0x0B, 0x41, 0x90, 0x82, 0xC0, 0x00, 0x28, 0x02, 0x00,
        0x22, 0x01, 0x41, 0x7F, 0x4C, 0x0D, 0x00, 0x41, 0x90, 0x82, 0xC0, 0x00, 0x20, 0x01, 0x36,
        0x02, 0x00, 0x20, 0x00, 0x41, 0x02, 0x49, 0x0D, 0x01, 0x0B, 0x00, 0x0B, 0x00, 0x0B, 0x41,
        0x01, 0x02, 0x7F, 0x23, 0x00, 0x41, 0x10, 0x6B, 0x22, 0x01, 0x24, 0x00, 0x02, 0x7F, 0x20,
        0x00, 0x28, 0x02, 0x08, 0x22, 0x02, 0x20, 0x02, 0x0D, 0x00, 0x1A, 0x41, 0xF0, 0x81, 0xC0,
        0x00, 0x10, 0x06, 0x00, 0x0B, 0x1A, 0x20, 0x01, 0x20, 0x00, 0x41, 0x14, 0x6A, 0x29, 0x02,
        0x00, 0x37, 0x03, 0x08, 0x20, 0x01, 0x20, 0x00, 0x29, 0x02, 0x0C, 0x37, 0x03, 0x00, 0x20,
        0x01, 0x10, 0x04, 0x00, 0x0B, 0x68, 0x02, 0x01, 0x7F, 0x03, 0x7E, 0x23, 0x00, 0x41, 0x30,
        0x6B, 0x22, 0x01, 0x24, 0x00, 0x20, 0x00, 0x29, 0x02, 0x08, 0x21, 0x02, 0x20, 0x00, 0x29,
        0x02, 0x10, 0x21, 0x03, 0x20, 0x00, 0x29, 0x02, 0x00, 0x21, 0x04, 0x20, 0x01, 0x41, 0x14,
        0x6A, 0x41, 0x00, 0x36, 0x02, 0x00, 0x20, 0x01, 0x20, 0x04, 0x37, 0x03, 0x18, 0x20, 0x01,
        0x41, 0xE0, 0x80, 0xC0, 0x00, 0x36, 0x02, 0x10, 0x20, 0x01, 0x42, 0x01, 0x37, 0x02, 0x04,
        0x20, 0x01, 0x20, 0x01, 0x41, 0x18, 0x6A, 0x36, 0x02, 0x00, 0x20, 0x01, 0x20, 0x03, 0x37,
        0x03, 0x28, 0x20, 0x01, 0x20, 0x02, 0x37, 0x03, 0x20, 0x20, 0x01, 0x20, 0x01, 0x41, 0x20,
        0x6A, 0x10, 0x03, 0x00, 0x0B, 0x03, 0x00, 0x01, 0x0B, 0x0C, 0x00, 0x42, 0x8E, 0xF4, 0xBC,
        0xB0, 0xF7, 0x9B, 0xAF, 0xC8, 0x2D, 0x0B, 0x74, 0x01, 0x01, 0x7C, 0x02, 0x40, 0x41, 0x98,
        0x82, 0xC0, 0x00, 0x29, 0x03, 0x00, 0x42, 0x01, 0x51, 0x04, 0x40, 0x41, 0xA0, 0x82, 0xC0,
        0x00, 0x28, 0x02, 0x00, 0x0D, 0x01, 0x41, 0xA8, 0x82, 0xC0, 0x00, 0x2B, 0x03, 0x00, 0x21,
        0x00, 0x41, 0xA0, 0x82, 0xC0, 0x00, 0x41, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00, 0x0F, 0x0B,
        0x41, 0xA8, 0x82, 0xC0, 0x00, 0x42, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xD0, 0xB7, 0xC0,
        0x00, 0x37, 0x03, 0x00, 0x41, 0x98, 0x82, 0xC0, 0x00, 0x42, 0x01, 0x37, 0x03, 0x00, 0x41,
        0xA0, 0x82, 0xC0, 0x00, 0x41, 0x00, 0x36, 0x02, 0x00, 0x41, 0xA0, 0x82, 0xC0, 0x00, 0x41,
        0x00, 0x36, 0x02, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x6F, 0x40, 0x0F, 0x0B,
        0x10, 0x00, 0x00, 0x0B, 0x0B, 0x95, 0x02, 0x02, 0x00, 0x41, 0x80, 0x80, 0xC0, 0x00, 0x0B,
        0xBE, 0x01, 0x3A, 0x20, 0x73, 0x72, 0x63, 0x2F, 0x6C, 0x69, 0x62, 0x63, 0x6F, 0x72, 0x65,
        0x2F, 0x72, 0x65, 0x73, 0x75, 0x6C, 0x74, 0x2E, 0x72, 0x73, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00,
        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x63, 0x61, 0x6C, 0x6C, 0x65, 0x64, 0x20,
        0x60, 0x4F, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x3A, 0x3A, 0x75, 0x6E, 0x77, 0x72, 0x61, 0x70,
        0x28, 0x29, 0x60, 0x20, 0x6F, 0x6E, 0x20, 0x61, 0x20, 0x60, 0x4E, 0x6F, 0x6E, 0x65, 0x60,
        0x20, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x73, 0x72, 0x63, 0x2F, 0x6C, 0x69, 0x62, 0x63, 0x6F,
        0x72, 0x65, 0x2F, 0x6F, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x2E, 0x72, 0x73, 0x42, 0x6F, 0x72,
        0x72, 0x6F, 0x77, 0x4D, 0x75, 0x74, 0x45, 0x72, 0x72, 0x6F, 0x72, 0x61, 0x6C, 0x72, 0x65,
        0x61, 0x64, 0x79, 0x20, 0x62, 0x6F, 0x72, 0x72, 0x6F, 0x77, 0x65, 0x64, 0x00, 0x41, 0xC0,
        0x81, 0xC0, 0x00, 0x0B, 0x45, 0x60, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00, 0x15, 0x00, 0x00, 0x00, 0xF1,
        0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x60, 0x00, 0x10, 0x00, 0x2B, 0x00, 0x00,
        0x00, 0x8B, 0x00, 0x10, 0x00, 0x15, 0x00, 0x00, 0x00, 0x63, 0x01, 0x00, 0x00, 0x15, 0x00,
        0x51, 0x09, 0x70, 0x72, 0x6F, 0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x02, 0x08, 0x6C, 0x61,
        0x6E, 0x67, 0x75, 0x61, 0x67, 0x65, 0x01, 0x04, 0x52, 0x75, 0x73, 0x74, 0x04, 0x32, 0x30,
        0x31, 0x38, 0x0C, 0x70, 0x72, 0x6F, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2D, 0x62, 0x79,
        0x01, 0x05, 0x72, 0x75, 0x73, 0x74, 0x63, 0x1D, 0x31, 0x2E, 0x33, 0x32, 0x2E, 0x30, 0x20,
        0x28, 0x39, 0x66, 0x64, 0x61, 0x37, 0x63, 0x32, 0x32, 0x33, 0x20, 0x32, 0x30, 0x31, 0x39,
        0x2D, 0x30, 0x31, 0x2D, 0x31, 0x36, 0x29,
    ];
    let import_object = wasmer_runtime::imports! {};
    let instance = wasmer_runtime::instantiate(&binary, &import_object).unwrap();
    if let Ok(func) = instance.func::<(), f64>("game_time") {
        let ret_val = func.call().unwrap();
        println!("{}", ret_val);
    }
}

The game_time function as WAST looks like this:

  (func $game_time (type $t6) (result f64)
    (local $l0 f64)
    block $B0
      i32.const 1048856
      i64.load
      i64.const 1
      i64.eq
      if $I1
        i32.const 1048864
        i32.load
        br_if $B0
        i32.const 1048872
        f64.load
        set_local $l0
        i32.const 1048864
        i32.const 0
        i32.store
        get_local $l0
        return
      end
      i32.const 1048872
      i64.const 4643000109586448384
      i64.store
      i32.const 1048856
      i64.const 1
      i64.store
      i32.const 1048864
      i32.const 0
      i32.store
      i32.const 1048864
      i32.const 0
      i32.store
      f64.const 0x1.f4p+7 (;=250;)
      return
    end
    call $f0
    unreachable)

This is the original source of the wasm file:

use std::cell::RefCell;

struct Context {
    val: f64,
}

fn build_context() -> RefCell<Context> {
    RefCell::new(Context {
        val: 250.0,
    })
}

thread_local! {
    static CONTEXT: RefCell<Context> = build_context();
}

#[no_mangle]
pub extern "C" fn game_time() -> f64 {
    CONTEXT.with(|ctx| {
        ctx.borrow_mut().val
    })
}

It does not break if I return 250.0 as a constant instead. So the thread_local RefCell is important (maybe just a static or thread_local would suffice, not sure yet).

This may or may not be broken on Linux and macOS too.

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

The RefCell is not necessary, this results in a much smaller file:

fn main() {
    let binary = [
        0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x60, 0x00, 0x00, 0x60,
        0x00, 0x01, 0x7C, 0x03, 0x03, 0x02, 0x00, 0x01, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, 0x01,
        0x05, 0x03, 0x01, 0x00, 0x11, 0x06, 0x19, 0x03, 0x7F, 0x01, 0x41, 0x80, 0x80, 0xC0, 0x00,
        0x0B, 0x7F, 0x00, 0x41, 0x90, 0x80, 0xC0, 0x00, 0x0B, 0x7F, 0x00, 0x41, 0x90, 0x80, 0xC0,
        0x00, 0x0B, 0x07, 0x4D, 0x05, 0x06, 0x6D, 0x65, 0x6D, 0x6F, 0x72, 0x79, 0x02, 0x00, 0x19,
        0x5F, 0x5F, 0x69, 0x6E, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x5F, 0x66, 0x75, 0x6E, 0x63,
        0x74, 0x69, 0x6F, 0x6E, 0x5F, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x01, 0x00, 0x0B, 0x5F, 0x5F,
        0x68, 0x65, 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, 0x65, 0x03, 0x01, 0x0A, 0x5F, 0x5F, 0x64,
        0x61, 0x74, 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, 0x02, 0x09, 0x67, 0x61, 0x6D, 0x65, 0x5F,
        0x74, 0x69, 0x6D, 0x65, 0x00, 0x01, 0x0A, 0x42, 0x02, 0x02, 0x00, 0x0B, 0x3D, 0x00, 0x02,
        0x40, 0x41, 0x00, 0x29, 0x03, 0x80, 0x80, 0x40, 0x42, 0x01, 0x52, 0x0D, 0x00, 0x41, 0x00,
        0x2B, 0x03, 0x88, 0x80, 0x40, 0x0F, 0x0B, 0x41, 0x00, 0x42, 0x80, 0x80, 0x80, 0x80, 0x80,
        0x80, 0xD0, 0xB7, 0xC0, 0x00, 0x37, 0x03, 0x88, 0x80, 0x40, 0x41, 0x00, 0x42, 0x01, 0x37,
        0x03, 0x80, 0x80, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x6F, 0x40, 0x0B, 0x0B,
        0x19, 0x01, 0x00, 0x41, 0x80, 0x80, 0xC0, 0x00, 0x0B, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2D, 0x04, 0x6E,
        0x61, 0x6D, 0x65, 0x01, 0x1F, 0x02, 0x00, 0x11, 0x5F, 0x5F, 0x77, 0x61, 0x73, 0x6D, 0x5F,
        0x63, 0x61, 0x6C, 0x6C, 0x5F, 0x63, 0x74, 0x6F, 0x72, 0x73, 0x01, 0x09, 0x67, 0x61, 0x6D,
        0x65, 0x5F, 0x74, 0x69, 0x6D, 0x65, 0x02, 0x05, 0x02, 0x00, 0x00, 0x01, 0x00,
    ];
    let import_object = wasmer_runtime::imports! {};
    let instance = wasmer_runtime::instantiate(&binary, &import_object).unwrap();
    if let Ok(func) = instance.func::<(), f64>("game_time") {
        let ret_val = func.call().unwrap();
        println!("{} {:#016x}", ret_val, ret_val.to_bits());
    }
}

WAST:

(module
  (type $t0 (func))
  (type $t1 (func (result f64)))
  (func $__wasm_call_ctors (type $t0))
  (func $game_time (type $t1) (result f64)
    block $B0
      i32.const 0
      i64.load offset=1048576
      i64.const 1
      i64.ne
      br_if $B0
      i32.const 0
      f64.load offset=1048584
      return
    end
    i32.const 0
    i64.const 4643000109586448384
    i64.store offset=1048584
    i32.const 0
    i64.const 1
    i64.store offset=1048576
    f64.const 0x1.f4p+7 (;=250;))
  (table $__indirect_function_table 1 1 anyfunc)
  (memory $memory 17)
  (global $g0 (mut i32) (i32.const 1048576))
  (global $__heap_base i32 (i32.const 1048592))
  (global $__data_end i32 (i32.const 1048592))
  (export "memory" (memory 0))
  (export "__indirect_function_table" (table 0))
  (export "__heap_base" (global 1))
  (export "__data_end" (global 2))
  (export "game_time" (func $game_time))
  (data (i32.const 1048576) "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"))

Source:

use std::cell::RefCell;

struct Context {
    val: f64,
}

fn build_context() -> Context {
    Context {
        val: 250.0,
    }
}

thread_local! {
    static CONTEXT: Context = build_context();
}

#[no_mangle]
pub extern "C" fn game_time() -> f64 {
    CONTEXT.with(|ctx| {
        ctx.val
    })
}

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

It seems like global variables don't work at all actually (if they are sufficiently uninlinable) as shown here:

(module
  (type $t0 (func))
  (type $t1 (func (result f64)))
  (func $__wasm_call_ctors (type $t0))
  (func $mutate (type $t0)
    i32.const 0
    i64.const 4636737291354636288
    i64.store offset=1048576)
  (func $game_time (type $t1) (result f64)
    i32.const 0
    f64.load offset=1048576)
  (table $__indirect_function_table 1 1 anyfunc)
  (memory $memory 17)
  (global $g0 (mut i32) (i32.const 1048576))
  (global $__heap_base i32 (i32.const 1048584))
  (global $__data_end i32 (i32.const 1048584))
  (export "memory" (memory 0))
  (export "__indirect_function_table" (table 0))
  (export "__heap_base" (global 1))
  (export "__data_end" (global 2))
  (export "mutate" (func $mutate))
  (export "game_time" (func $game_time))
  (data (i32.const 1048576) "\00\00\00\00\00@o@"))

Neither calling mutate nor skipping it produce the correct value, suggesting that it is indeed an ABI problem, because even if it would store / load from an incorrect address, at least mutate would write to the same location as the game_time function is loading from, and yet it still doesn't return the correct value.

@xmclark xmclark self-assigned this Feb 15, 2019
@syrusakbary
Copy link
Member

Thanks for the super detailed examples, we'll take a look on it soon!

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

Ah, I figured out the problem. It's the assumption that c structs with a single element have the same ABI as the single element itself, but that isn't always the case. So the S1<f64> WasmExternType impl destructures the S1 by derefencing from a pointer, storing the value in XMM0. However the JIT'ed code already has the f64 residing in the XMM0 register, which now gets overwritten by garbage:

https://i.imgur.com/ATms1zT.png

@xmclark
Copy link
Contributor

xmclark commented Feb 15, 2019

@CryZe Looks like you're using visual studio to debug Rust! I personally have not got this to work correctly. This is unrelated to the issue, but could you point me to resources on how you got visual studio configured for rust? How you're attaching the debugger?

In addition to learning how to do that, I want to reproduce and visual the problem the same way you have.

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

I did not really set up much. It works straight out of the box. You just need to make sure you use the msvc target and then you open the actual .exe file as a project (not as a file) in Visual Studio. After the "project" is opened you want to open the main.rs and put a breakpoint in there. Then you can start the project and it should hit the breakpoint. From there I added the disassembly and register view (which you can find under debugging -> views (or something, mine is german)). And then I right clicked the register view to show the SSE registers.

@xmclark
Copy link
Contributor

xmclark commented Feb 15, 2019

Wow that is totally unintuitive, but that works. Do you manually open the source files with the "Open" button, or have you found a way to load the source into the solution explorer? As it is now, I only have the wasmer.exe shown in solution explorer.

image

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

I actually usually use the Visual Studio Debugger in VS Code where I also have direct access to all my source files and stuff. However for this, VS Code's debugger is missing the disassembly and register view, which was necessary here. So I switched to Visual Studio instead. So since this is such a temporary occurance I just manually stepped through the calls which auto opened the files, and didn't bother with properly setting up any solution or anything. But I guess you could theoretically add the folders to the solution and then save it as a proper solution? I really don't know though.

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

I also was able to replicate this on Godbolt right now, where #[repr(C)] struct F(f64) and f64 behave differently when targeting windows (not linux though): https://rust.godbolt.org/z/pNibCT

@CryZe
Copy link
Contributor Author

CryZe commented Feb 15, 2019

So I tweaked the macro so I can specify the repr per S* type and gave the S1 a repr(transparent) and now everything works. Would that be a sufficient solution that I could PR (or maybe you guys do the change real quick) or is there a better solution?

CryZe added a commit to CryZe/wasmer that referenced this issue Feb 15, 2019
The ABI of aggregates such as single element structs is not required to
be the same as the single elements themselves. This is especially true
for f64 vs. #[repr(c)] struct F(f64); on Windows. Therefore the macro
has been tweaked so S1 uses repr(transparent) which is made for exactly
for this use case.

Closes wasmerio#183
@lachlansneff
Copy link
Contributor

That seems like a good solution to me! Make a pr!

xmclark pushed a commit that referenced this issue Feb 15, 2019
The ABI of aggregates such as single element structs is not required to
be the same as the single elements themselves. This is especially true
for f64 vs. #[repr(c)] struct F(f64); on Windows. Therefore the macro
has been tweaked so S1 uses repr(transparent) which is made for exactly
for this use case.

Closes #183
nlewycky pushed a commit that referenced this issue Aug 13, 2020
feat(cache) `blake3` is no longer optional
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants