Skip to content

Regression in beta: unsafe-free, pure Rust pgm. with no deps segfaults #36401

Closed
@osa1

Description

@osa1
Contributor
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Key {
    AltArrow(Arrow),
    Arrow(Arrow),
    Ctrl(char),
    Char(char),
    Esc,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Arrow { Left, Right, Up, Down }

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {
    Key(Key),
    String(String),
    Resize,
    Unknown(Vec<u8>),
}

static XTERM_SINGLE_BYTES : [(u8, Event); 5] =
    [ (1,  Event::Key(Key::Ctrl('a'))),
      (5,  Event::Key(Key::Ctrl('e'))),
      (23, Event::Key(Key::Ctrl('w'))),
      (11, Event::Key(Key::Ctrl('k'))),
      (4,  Event::Key(Key::Ctrl('d'))),
    ];

fn main() {
    println!("{:?}", XTERM_SINGLE_BYTES);
}

(can be tested here: https://is.gd/EkJ3by)

When run with beta or nightly, this program segfaults.

I tried to simplify this a little bit, but my attempts either turned segfault into a stack overflow, or made it working. Here are some changes I've tried:

  • I tried [(u8, char)] for XTERM_SINGLE_BYTES and it worked fine.
  • I tried removing Event::Keys (e.g. [(u8, Key)]) and it worked.
  • I removed some of the constructors from Event enum, most of the time the error turned into a stack overflow.

In my original program I'm iterating the array, and for the u8 part I'm getting random bytes instead of bytes I put in the array. I'm using rustc 1.13.0-nightly (70598e0 2016-09-03).

Activity

durka

durka commented on Sep 11, 2016

@durka
Contributor

Minimized a bit:

#[derive(Debug)]
pub enum Event {
    Key(u8),

    Resize,

    Unknown(Vec<u8>),
}

static XTERM_SINGLE_BYTES : [(u8, Event); 1] =
    [ (1,  Event::Resize)
    ];

fn main() {
    println!("{:?}", XTERM_SINGLE_BYTES);
}

This incorrectly prints [(1, Key(0))].

I wonder if it has something to do with the nullable pointer layout optimization, as the issue goes away if the Vec variant is removed.

TimNN

TimNN commented on Sep 11, 2016

@TimNN
Contributor

With -Z orbit, this was introduced between nightly-2016-05-08 and nightly-2016-05-09 (Changes).

(Edit: I missed a nightly in the original version of this comment.)

osa1

osa1 commented on Sep 11, 2016

@osa1
ContributorAuthor

Thanks @TimNN, as a workaround until this is fixed I switched to nightly-2016-05-08 and it works nicely.

added
A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
on Sep 11, 2016
durka

durka commented on Sep 11, 2016

@durka
Contributor

In that case I'm suspicious of #33130 cc @eddyb

TimNN

TimNN commented on Sep 11, 2016

@TimNN
Contributor

The Vec is not necessary to reproduce,

#[derive(Debug)]
pub enum Event {
    Key(u8),
    Resize,
    Unknown(i16),
}

as long as the types wrapped are of different sizes.

Also, I can reproduce this only as long as the size of the integer used in the static is smaller than than the size of the larger "wrapped" integer (in this case, the wrapped integer is i16, so only i8 and u8 will reproduce this).

arielb1

arielb1 commented on Sep 11, 2016

@arielb1
Contributor

Looks like missing alignment:

@_ZN8rust_out18XTERM_SINGLE_BYTES17heee0b00158a3d910E = internal constant { { i8, { i8, [3 x i8] } } } { { i8, { i8, [3 x i8] } } { i8 1, { i8, [3 x i8] } { i8 1, [3 x i8] undef } } }, align 2
nagisa

nagisa commented on Sep 11, 2016

@nagisa
Member

@arielb1 where did you get this? With original test case on play.rlo has align 8 for all stable, beta and nightly, which seems to be the expected value?

arielb1

arielb1 commented on Sep 11, 2016

@arielb1
Contributor

The u16 variant by @TimNN. The problem is not the align 2 but the { i8, { i8, [3 x i8] } }

added a commit that references this issue on Sep 11, 2016
f1bd907
added a commit that references this issue on Sep 12, 2016

Auto merge of #36406 - arielb1:constant-padding, r=eddyb

85592fb

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlregression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @durka@osa1@nagisa@TimNN@arielb1

        Issue actions

          Regression in beta: unsafe-free, pure Rust pgm. with no deps segfaults · Issue #36401 · rust-lang/rust