Skip to content

Commit

Permalink
Fix c2rust-analyze panic for variadic functions
Browse files Browse the repository at this point in the history
Add support for variadic functions in the "Infer pointee types"
phase of c2rust-analyze.
  • Loading branch information
ahomescu committed Jul 12, 2024
1 parent 9ad9d35 commit b6a871a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions c2rust-analyze/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ 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 +608,9 @@ 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 +668,12 @@ 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

0 comments on commit b6a871a

Please sign in to comment.