Skip to content

System.alloc returns unaligned pointer if align > size #45955

Closed
@RalfJung

Description

@RalfJung

The following code:

#![feature(allocator_api, alloc_system)]

extern crate alloc_system;

use std::heap::{Alloc, Layout};
use alloc_system::System;

pub fn main() {
    unsafe {
        let x = System.alloc(Layout::from_size_align(8, 16).unwrap()).unwrap();
        let y = System.alloc(Layout::from_size_align(8, 16).unwrap()).unwrap();

        println!("{:?} {:?}", (x as usize) % 16, (y as usize) % 16);
    }
}

prints 8 0 on my machine and on the playground. That's a bug; since I requested alignment 16 for both pointers, it should only ever print 0 0.

This is probably caused by the MIN_ALIGN optimization in alloc_system. This optimization is currently mostly useless, but it also seems to be wrong. I do not know what the actual alignment guarantees are; maybe the values are wrong or maybe the guarantee only holds for size >= align.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions