Index: test/ubsan/TestCases/TypeCheck/Function/function-c.c =================================================================== --- /dev/null +++ test/ubsan/TestCases/TypeCheck/Function/function-c.c @@ -0,0 +1,68 @@ +// RUN: %clangxx -x c -fsanitize=function %s -O3 -g -o %t +// RUN: %run %t 2>&1 | FileCheck %s + +struct S {}; + +void no_proto() {} + +void proto(void) {} + +typedef struct S (*vfunc0)(void); +typedef void (*vfunc1)(void); +typedef char (*vfunc2)(void); +typedef short (*vfunc3)(void); +typedef int (*vfunc4)(void); +typedef long long (*vfunc5)(void); +typedef float (*vfunc6)(void); +typedef double (*vfunc7)(void); +typedef void (*vfunc8)(int, int, int, int, int, int, int, int, int, int, int); + +void call_proto(void) { + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'struct S (*)(void)' + vfunc0 f0 = &proto; + f0(); + + // CHECK-NOT: function-c.c:[[@LINE+2]]:3: runtime error + vfunc1 f1 = &proto; + f1(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'char (*)(void)' + vfunc2 f2 = &proto; + f2(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'short (*)(void)' + vfunc3 f3 = &proto; + f3(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'int (*)(void)' + vfunc4 f4 = &proto; + f4(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'long long (*)(void)' + vfunc5 f5 = &proto; + f5(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'float (*)(void)' + vfunc6 f6 = &proto; + f6(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'double (*)(void)' + vfunc7 f7 = &proto; + f7(); + + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function proto through pointer to incorrect function type 'void (*)(int, int, int, int, int, int, int, int, int, int, int)' + vfunc8 f8 = &proto; + f8(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); +} + +void call_no_proto(void) { + // CHECK: function-c.c:[[@LINE+2]]:3: runtime error: call to function no_proto through pointer to incorrect function type 'struct S (*)(void)' + vfunc0 f0 = &no_proto; + f0(); +} + +int main() { + call_proto(); + call_no_proto(); + return 0; +}