s390x: Fix integer overflow during negation (#5866)

Use wrapping_neg in i{64,32,16}_from_negated_value to avoid Rust
aborts due to integer overflow.  The resulting INT_MIN is already
handled correctly in subsequent operations.

Fixes https://github.com/bytecodealliance/wasmtime/issues/5863.
This commit is contained in:
Ulrich Weigand
2023-02-23 17:32:10 +01:00
committed by GitHub
parent 761e44bd36
commit 9719147f91

View File

@@ -628,21 +628,21 @@ impl generated_code::Context for IsleContext<'_, '_, MInst, S390xBackend> {
#[inline]
fn i64_from_negated_value(&mut self, val: Value) -> Option<i64> {
let constant = self.u64_from_signed_value(val)? as i64;
let imm = -constant;
let imm = constant.wrapping_neg();
Some(imm)
}
#[inline]
fn i32_from_negated_value(&mut self, val: Value) -> Option<i32> {
let constant = self.u64_from_signed_value(val)? as i64;
let imm = i32::try_from(-constant).ok()?;
let imm = i32::try_from(constant.wrapping_neg()).ok()?;
Some(imm)
}
#[inline]
fn i16_from_negated_value(&mut self, val: Value) -> Option<i16> {
let constant = self.u64_from_signed_value(val)? as i64;
let imm = i16::try_from(-constant).ok()?;
let imm = i16::try_from(constant.wrapping_neg()).ok()?;
Some(imm)
}