Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix c2rust-analyze panic for variadic functions #1105

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions c2rust-analyze/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,12 @@ pub(super) fn gather_foreign_sigs<'tcx>(gacx: &mut GlobalAnalysisCtxt<'tcx>, tcx

let inputs = gacx.lcx.mk_slice(&inputs);
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
let lsig = LFnSig { inputs, output };
let c_variadic = sig.c_variadic;
let lsig = LFnSig {
inputs,
output,
c_variadic,
};
gacx.fn_sigs.insert(did, lsig);
}
}
Expand Down Expand Up @@ -607,8 +612,13 @@ fn run(tcx: TyCtxt) {
.collect::<Vec<_>>();
let inputs = gacx.lcx.mk_slice(&inputs);
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
let c_variadic = sig.c_variadic;

let lsig = LFnSig { inputs, output };
let lsig = LFnSig {
inputs,
output,
c_variadic,
};
gacx.fn_sigs.insert(ldid.to_def_id(), lsig);
}

Expand Down Expand Up @@ -666,6 +676,14 @@ fn run(tcx: TyCtxt) {
// TODO: set PointerInfo::ANNOTATED for the parts of the type with user annotations
let lty = match mir.local_kind(local) {
LocalKind::Var | LocalKind::Temp => acx.assign_pointer_ids(decl.ty),
LocalKind::Arg
if lsig.c_variadic && local.as_usize() - 1 == lsig.inputs.len() =>
{
// This is the hidden VaList<'a> argument at the end
// of the argument list of a variadic function. It does not
// appear in lsig.inputs, so we handle it separately here.
acx.assign_pointer_ids(decl.ty)
}
LocalKind::Arg => {
debug_assert!(local.as_usize() >= 1 && local.as_usize() <= mir.arg_count);
lsig.inputs[local.as_usize() - 1]
Expand Down
1 change: 1 addition & 0 deletions c2rust-analyze/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ pub type LTyCtxt<'tcx> = LabeledTyCtxt<'tcx, PointerId>;
pub struct LFnSig<'tcx> {
pub inputs: &'tcx [LTy<'tcx>],
pub output: LTy<'tcx>,
pub c_variadic: bool,
}

impl<'tcx> LFnSig<'tcx> {
Expand Down
Loading