From 5b66c999ec97c297089a24c7a82d99802f05bc3c Mon Sep 17 00:00:00 2001 From: shuangxiang kan <18550887212@163.com> Date: Mon, 18 Dec 2023 13:32:32 +1100 Subject: [PATCH 1/2] Fix matching parameters and return values for "OVERWRITE" functions and add some new functions sepcfications in extapi.c --- svf-llvm/lib/LLVMModule.cpp | 36 +++++++++++++++ svf-llvm/lib/extapi.c | 88 ++++++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 5 deletions(-) diff --git a/svf-llvm/lib/LLVMModule.cpp b/svf-llvm/lib/LLVMModule.cpp index e803b9aed..70980ffbf 100644 --- a/svf-llvm/lib/LLVMModule.cpp +++ b/svf-llvm/lib/LLVMModule.cpp @@ -979,6 +979,42 @@ void LLVMModuleSet::buildFunToFunMap() { if (appfunc->getName().str().compare(owfunc->getName().str()) == 0) { + Type* returnType1 = appfunc->getReturnType(); + Type* returnType2 = owfunc->getReturnType(); + + // Check if the return types are compatible: (1) The types are exactly the same, (2) Both are pointer types, and at least one of them is a void*. + if (!(returnType1 == returnType2 || + (returnType1->isPointerTy() && returnType2->isPointerTy() && + (returnType1->getPointerElementType()->isIntegerTy(8) || returnType2->getPointerElementType()->isIntegerTy(8))))) + { + continue; + } + + if (appfunc->arg_size() != owfunc->arg_size()) + continue; + + bool argMismatch = false; + Function::const_arg_iterator argIter1 = appfunc->arg_begin(); + Function::const_arg_iterator argIter2 = owfunc->arg_begin(); + while (argIter1 != appfunc->arg_end() && argIter2 != owfunc->arg_end()) + { + Type* argType1 = argIter1->getType(); + Type* argType2 = argIter2->getType(); + + // Check if the parameters types are compatible: (1) The types are exactly the same, (2) Both are pointer types, and at least one of them is a void*. + if (!(argType1 == argType2 || + (argType1->isPointerTy() && argType2->isPointerTy() && + (argType1->getPointerElementType()->isIntegerTy(8) || argType2->getPointerElementType()->isIntegerTy(8))))) + { + argMismatch = true; + break; + } + argIter1++; + argIter2++; + } + if (argMismatch) + continue; + Function* fun = const_cast(appfunc); Module* mod = fun->getParent(); FunctionType* funType = fun->getFunctionType(); diff --git a/svf-llvm/lib/extapi.c b/svf-llvm/lib/extapi.c index d0c640ad6..4ffa70032 100644 --- a/svf-llvm/lib/extapi.c +++ b/svf-llvm/lib/extapi.c @@ -459,16 +459,20 @@ void* safexrealloc() return NULL; } -__attribute__((annotate("REALLOC_RET"), annotate("AllocSize:UNKNOWN"))) + char *strtok(char *str, const char *delim) { - return NULL; + return str; } -__attribute__((annotate("REALLOC_RET"), annotate("AllocSize:UNKNOWN"))) char *strtok_r(char *str, const char *delim, char **saveptr) { - return NULL; + return str; +} + +char* strsep(char** stringp, const char* delim) +{ + return *stringp; } __attribute__((annotate("REALLOC_RET"), annotate("AllocSize:Arg1"))) @@ -727,11 +731,26 @@ char *fgets(char *str, int n, void *stream) return str; } +char *fgets_unlocked(char *str, int n, void *stream) +{ + return str; +} + +char* gets(char *str) +{ + return str; +} + void *memchr(const void *str, int c, unsigned long n) { return (void *)str; } +void *memrchr(const void *str, int c, unsigned long n) +{ + return (void *)str; +} + void * mremap(void * old_address, unsigned long old_size, unsigned long new_size, int flags) { return old_address; @@ -742,6 +761,26 @@ char *strchr(const char *str, int c) return (char *)str; } +char *__strchrnull(const char *s, int c) +{ + return (char *)s; +} + +char *strcasestr(const char *haystack, const char *needle) +{ + return (char *)haystack; +} + +char* index(const char *s, int c) +{ + return (char *)s; +} + +char* rindex(const char *s, int c) +{ + return (char *)s; +} + char *strerror_r(int errnum, char *buf, unsigned long buflen) { return buf; @@ -888,18 +927,36 @@ double strtod(const char *str, char **endptr) return 0.0; } +double strtod_l(const char *str, char **endptr, void *loc) +{ + *endptr = (char *)str; + return 0.0; +} + float strtof(const char *nptr, char **endptr) { *endptr = (char *)nptr; return 0.0; } +float strtof_l(const char *nptr, char **endptr, void *loc) +{ + *endptr = (char *)nptr; + return 0.0; +} + long int strtol(const char *str, char **endptr, int base) { *endptr = (char *)str; return 0; } +long long strtoll(const char *str, char **endptr, int base) +{ + *endptr = (char *)str; + return 0; +} + long double strtold(const char* str, char** endptr) { *endptr = (char *)str; @@ -912,6 +969,27 @@ unsigned long int strtoul(const char *str, char **endptr, int base) return 0; } +unsigned long long strtoull(const char *str, char **endptr, int base) +{ + *endptr = (char *)str; + return 0; +} + +char *gcvt(double x, int ndigit, char *buf) +{ + return buf; +} + +void *memmem(const void *haystack, unsigned long haystacklen, const void *needle, unsigned long needlelen) +{ + return (void *)haystack; +} + +char* ctime_r(const char *timer, char *buf) +{ + return buf; +} + int readdir_r(void *__restrict__dir, void *__restrict__entry, void **__restrict__result) { __restrict__entry = *__restrict__result; @@ -997,7 +1075,7 @@ char * bind_textdomain_codeset(const char * domainname, const char * codeset) char *ctermid(char *s) { - return STATIC_OBJECT; + return s; } char * dcgettext(const char * domainname, const char * msgid, int category) From 8f21529d8b8cad2f703c569fc180413d571897dd Mon Sep 17 00:00:00 2001 From: shuangxiang kan <18550887212@163.com> Date: Mon, 18 Dec 2023 18:42:32 +1100 Subject: [PATCH 2/2] Remove getPointerElementType() The getPointerElementType will be deprecated in future versions of LLVM. Considering compatibility, avoid using getPointerElementType()->isIntegerTy(8) to determine if the arguments and return values are of the void * type. --- svf-llvm/lib/LLVMModule.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/svf-llvm/lib/LLVMModule.cpp b/svf-llvm/lib/LLVMModule.cpp index 70980ffbf..03e60077c 100644 --- a/svf-llvm/lib/LLVMModule.cpp +++ b/svf-llvm/lib/LLVMModule.cpp @@ -982,10 +982,12 @@ void LLVMModuleSet::buildFunToFunMap() Type* returnType1 = appfunc->getReturnType(); Type* returnType2 = owfunc->getReturnType(); - // Check if the return types are compatible: (1) The types are exactly the same, (2) Both are pointer types, and at least one of them is a void*. - if (!(returnType1 == returnType2 || - (returnType1->isPointerTy() && returnType2->isPointerTy() && - (returnType1->getPointerElementType()->isIntegerTy(8) || returnType2->getPointerElementType()->isIntegerTy(8))))) + // Check if the return types are compatible: + // (1) The types are exactly the same, + // (2) Both are pointer types, and at least one of them is a void*. + // Note that getPointerElementType() will be deprecated in the future versions of LLVM. + // Considering compatibility, avoid using getPointerElementType()->isIntegerTy(8) to determine if it is a void * type. + if (!(returnType1 == returnType2 || (returnType1->isPointerTy() && returnType2->isPointerTy()))) { continue; } @@ -1002,9 +1004,7 @@ void LLVMModuleSet::buildFunToFunMap() Type* argType2 = argIter2->getType(); // Check if the parameters types are compatible: (1) The types are exactly the same, (2) Both are pointer types, and at least one of them is a void*. - if (!(argType1 == argType2 || - (argType1->isPointerTy() && argType2->isPointerTy() && - (argType1->getPointerElementType()->isIntegerTy(8) || argType2->getPointerElementType()->isIntegerTy(8))))) + if (!(argType1 == argType2 || (argType1->isPointerTy() && argType2->isPointerTy()))) { argMismatch = true; break;