From 3e801e61930de2116d02cb8d91f1a98a4728d560 Mon Sep 17 00:00:00 2001 From: Kevin Valerio Date: Wed, 17 Jun 2026 15:25:30 +0200 Subject: [PATCH] fix(bpf): avoid extending narrow extern C returns --- compiler/rustc_target/src/callconv/bpf.rs | 3 +- .../codegen-llvm/bpf-abi-extern-narrow-ret.rs | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/codegen-llvm/bpf-abi-extern-narrow-ret.rs diff --git a/compiler/rustc_target/src/callconv/bpf.rs b/compiler/rustc_target/src/callconv/bpf.rs index 3624f406704e9..7123ba2946ffb 100644 --- a/compiler/rustc_target/src/callconv/bpf.rs +++ b/compiler/rustc_target/src/callconv/bpf.rs @@ -6,8 +6,6 @@ use crate::callconv::{ArgAbi, FnAbi}; fn classify_ret(ret: &mut ArgAbi<'_, Ty>) { if ret.layout.is_aggregate() || ret.layout.size.bits() > 64 { ret.make_indirect(); - } else { - ret.extend_integer_width_to(32); } } @@ -45,5 +43,6 @@ where pub(crate) fn compute_rust_abi_info(fn_abi: &mut FnAbi<'_, Ty>) { if !fn_abi.ret.is_ignore() { classify_ret(&mut fn_abi.ret); + fn_abi.ret.extend_integer_width_to(32); } } diff --git a/tests/codegen-llvm/bpf-abi-extern-narrow-ret.rs b/tests/codegen-llvm/bpf-abi-extern-narrow-ret.rs new file mode 100644 index 0000000000000..fc1323e5621e3 --- /dev/null +++ b/tests/codegen-llvm/bpf-abi-extern-narrow-ret.rs @@ -0,0 +1,50 @@ +//@ add-minicore +//@ needs-llvm-components: bpf +//@ compile-flags: --target bpfel-unknown-none + +#![feature(no_core)] +#![crate_type = "lib"] +#![no_core] + +extern crate minicore; +use minicore::*; + +unsafe extern "C" { + fn c_ret_u8() -> u8; + fn c_ret_u16() -> u16; + fn c_ret_i8() -> i8; + fn c_ret_i16() -> i16; +} + +// CHECK-LABEL: define {{.*}} @observe_u8( +// CHECK: call noundef i8 @c_ret_u8() +#[unsafe(no_mangle)] +pub unsafe extern "C" fn observe_u8() -> u64 { + c_ret_u8() as u64 +} + +// CHECK-LABEL: define {{.*}} @observe_u16( +// CHECK: call noundef i16 @c_ret_u16() +#[unsafe(no_mangle)] +pub unsafe extern "C" fn observe_u16() -> u64 { + c_ret_u16() as u64 +} + +// CHECK-LABEL: define {{.*}} @observe_i8( +// CHECK: call noundef i8 @c_ret_i8() +#[unsafe(no_mangle)] +pub unsafe extern "C" fn observe_i8() -> i64 { + c_ret_i8() as i64 +} + +// CHECK-LABEL: define {{.*}} @observe_i16( +// CHECK: call noundef i16 @c_ret_i16() +#[unsafe(no_mangle)] +pub unsafe extern "C" fn observe_i16() -> i64 { + c_ret_i16() as i64 +} + +// CHECK: declare noundef i8 @c_ret_u8() +// CHECK: declare noundef i16 @c_ret_u16() +// CHECK: declare noundef i8 @c_ret_i8() +// CHECK: declare noundef i16 @c_ret_i16()