rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.33k stars 12.58k forks source link

xabort incorrect argument #73814

Open valarauca opened 4 years ago

valarauca commented 4 years ago

I tried this code:

godbolt link, you need to pass platform features.

#![no_std]
#![feature(stdsimd)]
use core::arch::x86_64::{_xabort, _xtest};

pub fn abort(x: i8) {
    unsafe {
        if _xtest() {
            _xabort(x);
        }
    }
}

I expected to see this happen:

Either a clean compile, or a type error that u8 was expected instead of i8.

Instead, this happened:

The current code states that _xabort takes a u32 as an argument. This appears to be incorrect.

As the xabort instruction can only encode an 8bit argument.

The xbegin instruction will only encode the abort flag within bits 24:31 of eax. Meaning that you can't recieve an error code larger than 8bits.

Meta

rustc --version --verbose:

rustc 1.46.0-nightly (7750c3d46 2020-06-26)
binary: rustc
commit-hash: 7750c3d46bc19784adb1ee6e37a5ec7e4cd7e772
commit-date: 2020-06-26
host: x86_64-unknown-linux-gnu
release: 1.46.0nightly
LLVM version: 10.0
nbdd0121 commented 4 years ago

The code won't compile regardless the type, because xabort takes constant immediate only.

nbdd0121 commented 4 years ago

The current code is reflecting C/C++'s use of unsigned int (https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/intrinsics/intrinsics-for-intel-advanced-vector-extensions-2/intrinsics-for-intel-transactional-synchronization-extensions-intel-tsx/intrinsics-for-restricted-transactional-memory-operations/xabort.html)

valarauca commented 4 years ago

because xabort takes constant immediate only.

I am aware, the type error is returned before the constant error.

Irregardless of constant or not, it may only encode 8 bits of data.