[PATCH] D29749: [libFuzzer] Fix FuzzerExtFunctions when working with weak functions.

Marcos Pividori via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 8 22:11:08 PST 2017


mpividori created this revision.

For weak functions we also need to check if "fun__dll" is present.
So, we were not checking properly for weak functions. This did not generate any problem before because the only weak function that we include is `__sanitizer_print_memory_profile`, but could generate problem in the future.
So, in this diff I include the changes to fix that.


https://reviews.llvm.org/D29749

Files:
  lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp


Index: lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
===================================================================
--- lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
+++ lib/Fuzzer/FuzzerExtFunctionsWeakAlias.cpp
@@ -18,33 +18,39 @@
 
 extern "C" {
 // Declare these symbols as weak to allow them to be optionally defined.
+// Default implementation of weak functions are exported with the __dll suffix,
+// so we need to check if "fun" or "fun__dll" is present.
 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
   RETURN_TYPE NAME##Def FUNC_SIG {                                             \
     Printf("ERROR: Function \"%s\" not defined.\n", #NAME);                    \
     exit(1);                                                                   \
   }                                                                            \
-  RETURN_TYPE NAME FUNC_SIG __attribute__((weak, alias(#NAME "Def")));
+  RETURN_TYPE NAME FUNC_SIG __attribute__((weak, alias(#NAME "Def")));         \
+  RETURN_TYPE NAME##__dll FUNC_SIG __attribute__((weak, alias(#NAME "Def")));
 
 #include "FuzzerExtFunctions.def"
 
 #undef EXT_FUNC
 }
 
 template <typename T>
-static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
-  if (Fun == FunDef) {
-    if (WarnIfMissing)
-      Printf("WARNING: Failed to find function \"%s\".\n", FnName);
-    return nullptr;
-  }
-  return Fun;
+static T *GetFnPtr(T *Fun, T *FunDll, T *FunDef, const char *FnName,
+    bool WarnIfMissing) {
+  if (Fun != FunDef)
+    return Fun;
+  if (FunDll != FunDef)
+    return FunDll;
+  if (WarnIfMissing)
+    Printf("WARNING: Failed to find function \"%s\".\n", FnName);
+  return nullptr;
 }
 
 namespace fuzzer {
 
 ExternalFunctions::ExternalFunctions() {
 #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN)                            \
-  this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);
+  this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##__dll, ::NAME##Def,  \
+      #NAME, WARN);
 
 #include "FuzzerExtFunctions.def"
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29749.87760.patch
Type: text/x-patch
Size: 2088 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170209/ab1a5d78/attachment.bin>


More information about the llvm-commits mailing list