Linux SPARSE checker discussions
 help / color / mirror / Atom feed
* sparse-llvm functions must be cast to correct type before calling
@ 2017-03-16 20:58 Dibyendu Majumdar
  2017-03-19  0:20 ` Luc Van Oostenryck
  0 siblings, 1 reply; 2+ messages in thread
From: Dibyendu Majumdar @ 2017-03-16 20:58 UTC (permalink / raw)
  To: Linux-Sparse

Hi,

The following test (adapted from snippet posted by Linus) fails in sparse-llvm:

typedef unsigned int (*binop_t)(int, int);
typedef unsigned int (*unop_t)(int);
extern int printf(const char *, ...);
#define BINOP 0
#define UNOP 1
static unsigned int execute(int type, void *fn, int arg1, int arg2)
{
 if (type == BINOP)
  return ((binop_t)fn)(arg1,arg2);
 return ((unop_t)fn)(arg1);
}
static unsigned int unary(int arg1)
{
 return arg1+3;
}
int main(void)
{
 return execute(UNOP, unary, 3, 10) == 6 ? 0 : 1;
}

To resolve this before calling a function we need to cast it to the
expected type. This can be done like this in output_op_call():

 struct symbol *ftype = get_function_basetype(insn->fntype);

...

 LLVMTypeRef function_type = symbol_type(C, fn->module, ftype);
 LLVMTypeRef fptr_type = LLVMPointerType(function_type, 0);
 LLVMTypeRef bytep = LLVMPointerType(LLVMInt8Type(), 0);
 target = LLVMBuildBitCast(fn->builder, func, bytep, name);
 target = LLVMBuildBitCast(fn->builder, target, fptr_type, name);
 target = LLVMBuildCall(fn->builder, target, args, n_arg, name);
 insn->target->priv = target;


Thanks and Regards
Dibyendu

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-03-19  0:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-16 20:58 sparse-llvm functions must be cast to correct type before calling Dibyendu Majumdar
2017-03-19  0:20 ` Luc Van Oostenryck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox