Skip to content
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
7 changes: 7 additions & 0 deletions infer/src/base/Stats.ml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ type t =
; pulse_interrupted_loops: IntCounter.t Atomic.t
; pulse_unknown_calls: IntCounter.t Atomic.t
; pulse_unknown_calls_on_hack_resource: IntCounter.t Atomic.t
; pulse_unknown_calls_arity_mismatch: IntCounter.t Atomic.t
; pulse_swift_alloc_unknown_type: IntCounter.t Atomic.t
; pulse_summaries_contradictions: IntCounter.t Atomic.t
; pulse_summaries_unsat_for_caller: IntCounter.t Atomic.t
Expand Down Expand Up @@ -319,6 +320,7 @@ let pp fmt stats =
~pulse_captured_vars_length_contradictions:(pp_int_field fmt)
~pulse_disjuncts_dropped:(pp_int_field fmt) ~pulse_interrupted_loops:(pp_int_field fmt)
~pulse_unknown_calls:(pp_int_field fmt) ~pulse_unknown_calls_on_hack_resource:(pp_int_field fmt)
~pulse_unknown_calls_arity_mismatch:(pp_int_field fmt)
~pulse_swift_alloc_unknown_type:(pp_int_field fmt)
~pulse_summaries_contradictions:(pp_int_field fmt)
~pulse_summaries_count:(pp_pulse_summaries_count fmt)
Expand Down Expand Up @@ -347,6 +349,7 @@ let log_to_file
; pulse_interrupted_loops
; pulse_unknown_calls
; pulse_unknown_calls_on_hack_resource
; pulse_unknown_calls_arity_mismatch
; pulse_summaries_contradictions
; pulse_summaries_unsat_for_caller
; pulse_summaries_unsat_for_caller_percent
Expand All @@ -372,6 +375,8 @@ let log_to_file
F.fprintf fmt "pulse_unknown_calls: %d@\n" (Atomic.get pulse_unknown_calls) ;
F.fprintf fmt "pulse_unknown_calls_on_hack_resource: %d@\n"
(Atomic.get pulse_unknown_calls_on_hack_resource) ;
F.fprintf fmt "pulse_unknown_calls_arity_mismatch: %d@\n"
(Atomic.get pulse_unknown_calls_arity_mismatch) ;
F.fprintf fmt "pulse_summaries_contradictions: %d@\n" (Atomic.get pulse_summaries_contradictions) ;
F.fprintf fmt "pulse_summaries_unsat_for_caller: %d@\n"
(Atomic.get pulse_summaries_unsat_for_caller) ;
Expand Down Expand Up @@ -497,6 +502,8 @@ let incr_pulse_unknown_calls () = incr Fields.pulse_unknown_calls

let incr_pulse_unknown_calls_on_hack_resource () = incr Fields.pulse_unknown_calls_on_hack_resource

let incr_pulse_unknown_calls_arity_mismatch () = incr Fields.pulse_unknown_calls_arity_mismatch

let incr_pulse_swift_alloc_unknown_type () = incr Fields.pulse_swift_alloc_unknown_type

let incr_pulse_summaries_contradictions () = incr Fields.pulse_summaries_contradictions
Expand Down
2 changes: 2 additions & 0 deletions infer/src/base/Stats.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ val incr_pulse_unknown_calls : unit -> unit

val incr_pulse_unknown_calls_on_hack_resource : unit -> unit

val incr_pulse_unknown_calls_arity_mismatch : unit -> unit

val incr_pulse_swift_alloc_unknown_type : unit -> unit
(** a [swift_allocObject] call reached the Pulse model but the size argument was not a [Sizeof]
expression, so we could not recover the dynamic type of the allocated object *)
Expand Down
47 changes: 40 additions & 7 deletions infer/src/pulse/PulseCallOperations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ let unbind_variadic_actuals previous astate =
Stack.remove_vars [var] astate )


(** In order to apply summary specialisation, we call blocks or function pointers with the closure
as the first argument, but when we want to call the actual code of the block or function, we
need to remove the closure argument again. The extra actuals of variadic functions are dropped
too. *)
let actuals_without_closure call_flags ~actuals =
if call_flags.CallFlags.cf_is_objc_block then match actuals with _ :: rest -> rest | [] -> []
else actuals


(** The actuals matched against a callee's formals: closure argument removed, then the extra actuals
of a variadic function dropped. [apply_callee] cannot use this directly -- upstream #2078 binds
those extra actuals to globals, so it needs the closure-stripped list BEFORE trimming -- but
callers that only need the final list, such as [is_arity_mismatch_with_c_definition], do. *)
let actuals_for_formals call_flags callee_pname ~formals ~actuals =
trim_actuals_if_var_arg (Some callee_pname) ~formals
~actuals:(actuals_without_closure call_flags ~actuals)


(** C functions are identified by their name only, so a call can resolve to a definition whose
formals do not match the actuals, e.g. a definition from another program analyzed together with
the caller, or one called through an unprototyped declaration or a function pointer cast. None
of the pre/posts of such a definition can be applied. *)
let is_arity_mismatch_with_c_definition call_flags callee_pname ~actuals =
Procname.is_c callee_pname
&& Option.exists (IRAttributes.load callee_pname) ~f:(fun {ProcAttributes.formals} ->
let actuals = actuals_for_formals call_flags callee_pname ~formals ~actuals in
not (Int.equal (List.length formals) (List.length actuals)) )


let is_const_version pname_method (other_method : Struct.tenv_method) =
String.equal pname_method (Procname.get_method other_method.name)
&& Option.exists (IRAttributes.load other_method.name) ~f:(fun attr ->
Expand Down Expand Up @@ -363,13 +392,7 @@ let apply_callee ({InterproceduralAnalysis.tenv; proc_desc} as analysis_data)
AbductiveDomain.Summary.remove_all_must_not_be_tainted ~kinds callee_summary
else callee_summary
in
(* In order to apply summary specialisation, we call blocks or function pointers with the closure as the first argument,
but when we want to call the actual code of the block or function, we need to remove the closure argument again. *)
let actuals =
if call_flags.CallFlags.cf_is_objc_block then
match actuals with _ :: rest -> rest | [] -> []
else actuals
in
let actuals = actuals_without_closure call_flags ~actuals in
let astate, variadic_globals =
bind_variadic_actuals path call_loc (Some callee_proc_name) ~formals ~actuals astate
in
Expand Down Expand Up @@ -1109,6 +1132,16 @@ let call ?disjunct_limit ({InterproceduralAnalysis.analyze_dependency} as analys
in
let non_disj_caller = record_direct_call non_disj_caller in
match analyze_dependency callee_pname with
| Ok _ when is_arity_mismatch_with_c_definition call_flags callee_pname ~actuals ->
L.d_printfln_escaped ~color:Orange
"The formals of %a cannot be matched with the %d actuals, treating the call as unknown"
Procname.pp callee_pname (List.length actuals) ;
Stats.incr_pulse_unknown_calls_arity_mismatch () ;
let res, (non_disj, contradiction) =
call_aux_unknown disjunct_limit analysis_data path call_loc callee_pname ~ret ~actuals
~formals_opt:None call_kind call_flags astate non_disj_caller
in
(res, non_disj, contradiction, `UnknownCall)
| Ok summary ->
let is_pulse_specialization_limit_reached =
Specialization.Pulse.is_pulse_specialization_limit_reached summary.PulseSummary.specialized
Expand Down
129 changes: 129 additions & 0 deletions infer/tests/codetoanalyze/c/pulse/arity_mismatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <stdlib.h>

// These functions are defined in arity_mismatch2.c with different parameters,
// as if by another program analyzed together with this file. C functions are
// identified by their name only, so the calls below resolve to those
// definitions.
int defined_elsewhere_without_params(int fd, int how);
int* defined_elsewhere_without_params_returns_null(int* p);
void defined_elsewhere_with_two_params(int* p);

int more_actuals_than_formals_then_npe_bad(int fd) {
int* p = NULL;
defined_elsewhere_without_params(fd, 2);
return *p;
}

int fewer_actuals_than_formals_then_npe_bad() {
int x = 0;
int* p = NULL;
defined_elsewhere_with_two_params(&x);
return *p;
}

void arity_mismatch_then_leak_bad() {
int* p = (int*)malloc(sizeof(int));
defined_elsewhere_without_params(0, 2);
}

void arity_mismatch_havocs_args_ok() {
int* p = (int*)malloc(sizeof(int));
defined_elsewhere_without_params_returns_null(p);
}

void arity_mismatch_ignores_definition_ok() {
int x = 0;
int* p = defined_elsewhere_without_params_returns_null(&x);
*p = 42;
}

int* returns_null_elsewhere(void);

void arity_match_uses_definition_bad() {
int* p = returns_null_elsewhere();
*p = 42;
}

void deref_param(int* x) { *x = 42; }

void cast_function_pointer_arity_mismatch_then_npe_bad() {
int* p = NULL;
((void (*)(int*, int))deref_param)(p, 0);
*p = 42;
}

void function_pointer_variable_arity_mismatch_then_npe_bad() {
void (*f)(int*, int) = (void (*)(int*, int))deref_param;
int* p = NULL;
f(p, 0);
*p = 42;
}

int* global_set_by_callee;

void set_global(int* v) { global_set_by_callee = v; }

// the definition is not applied and unknown calls do not havoc globals
int FP_cast_function_pointer_arity_mismatch_sets_global_ok(int* v) {
global_set_by_callee = NULL;
((void (*)(int*, int))set_global)(v, 1);
return *global_set_by_callee;
}

int* kr_returns_null(p, q)
int* p;
int* q;
{
return NULL;
}

int kr_definition_fewer_actuals_then_npe_bad() {
int x = 0;
int* p = NULL;
kr_returns_null(&x);
return *p;
}

int kr_definition_matching_arity_uses_definition_bad() {
int x = 0;
int* p = kr_returns_null(&x, &x);
return *p;
}

int* variadic_returns_null(int n, ...) { return NULL; }

int variadic_extra_actuals_use_definition_bad() {
int* p = variadic_returns_null(1, 2, 3);
return *p;
}

__attribute__((noreturn)) void exit_with_failure(void) { exit(1); }

// the noreturn attribute of the definition still applies
void cast_noreturn_function_arity_mismatch_ok() {
int* p = NULL;
((void (*)(int))exit_with_failure)(1);
*p = 42;
}

// like err() from <err.h>, but arity_mismatch2.c defines a function with this
// name that returns
__attribute__((noreturn)) void fails_here_returns_elsewhere(int status,
const char* msg);

// only the attributes of the definition are known, so the call returns
void FP_noreturn_declaration_arity_mismatch_ok() {
int* p = (int*)malloc(sizeof(int));
if (p == NULL) {
fails_here_returns_elsewhere(1, "out of memory");
}
*p = 42;
free(p);
}
18 changes: 18 additions & 0 deletions infer/tests/codetoanalyze/c/pulse/arity_mismatch2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <stddef.h>

void defined_elsewhere_without_params(void) {}

int* defined_elsewhere_without_params_returns_null(void) { return NULL; }

void defined_elsewhere_with_two_params(int* p, int* q) { *q = *p; }

int* returns_null_elsewhere(void) { return NULL; }

void fails_here_returns_elsewhere(const char* msg) {}
11 changes: 11 additions & 0 deletions infer/tests/codetoanalyze/c/pulse/issues.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ codetoanalyze/c/pulse/angelism.c, struct_value_skip_null_deref_bad, 4, NULLPTR_D
codetoanalyze/c/pulse/angelism.c, struct_value_from_pointer_skip_bad, 3, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arithmetic.c, if_negative_then_crash_latent, 3, NULLPTR_DEREFERENCE_LATENT, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arithmetic.c, call_if_negative_then_crash_with_local_bad, 2, NULLPTR_DEREFERENCE, no_bucket, ERROR, [when calling `if_negative_then_crash_latent` here,is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, more_actuals_than_formals_then_npe_bad, 3, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, fewer_actuals_than_formals_then_npe_bad, 4, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, arity_mismatch_then_leak_bad, 1, MEMORY_LEAK_C, no_bucket, ERROR, [allocation part of the trace starts here,allocated by `malloc` here,memory becomes unreachable here]
codetoanalyze/c/pulse/arity_mismatch.c, arity_match_uses_definition_bad, 2, NULLPTR_DEREFERENCE, no_bucket, ERROR, [in call to `returns_null_elsewhere`,is assigned to the null pointer,returned,return from call to `returns_null_elsewhere`,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, cast_function_pointer_arity_mismatch_then_npe_bad, 3, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, function_pointer_variable_arity_mismatch_then_npe_bad, 4, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, FP_cast_function_pointer_arity_mismatch_sets_global_ok, 3, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, kr_definition_fewer_actuals_then_npe_bad, 4, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, kr_definition_matching_arity_uses_definition_bad, 3, NULLPTR_DEREFERENCE, no_bucket, ERROR, [in call to `kr_returns_null`,is assigned to the null pointer,returned,return from call to `kr_returns_null`,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, variadic_extra_actuals_use_definition_bad, 2, NULLPTR_DEREFERENCE, no_bucket, ERROR, [in call to `variadic_returns_null`,is assigned to the null pointer,returned,return from call to `variadic_returns_null`,assigned,invalid access occurs here]
codetoanalyze/c/pulse/arity_mismatch.c, FP_noreturn_declaration_arity_mismatch_ok, 5, NULLPTR_DEREFERENCE, no_bucket, ERROR, [in call to `malloc (null case)` (modelled),is assigned to the null pointer,assigned,was compared to null on line 124, column 7,invalid access occurs here]
codetoanalyze/c/pulse/assert.c, report_on_line_offset_6_bad, 6, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,invalid access occurs here]
codetoanalyze/c/pulse/cleanup_attribute.c, cleanup_double_free_bad, 3, USE_AFTER_FREE, no_bucket, ERROR, [invalidation part of the trace starts here,allocated by call to `malloc` (modelled),assigned,was invalidated by call to `free()`,use-after-lifetime part of the trace starts here,allocated by call to `malloc` (modelled),assigned,when calling `cleanup_int` here,parameter `x` of cleanup_int,invalid access occurs here]
codetoanalyze/c/pulse/cleanup_attribute.c, cleanup_null_deref_bad, 2, NULLPTR_DEREFERENCE, no_bucket, ERROR, [is assigned to the null pointer,assigned,when calling `deref_cleanup` here,parameter `x` of deref_cleanup,invalid access occurs here]
Expand Down
Loading