]> granicus.if.org Git - php/commitdiff
basic ASAN suport for clang on Windows
authorAnatol Belski <ab@php.net>
Mon, 3 Apr 2017 09:46:51 +0000 (11:46 +0200)
committerAnatol Belski <ab@php.net>
Mon, 3 Apr 2017 10:02:51 +0000 (12:02 +0200)
fix makefile generation for non clang

rename option

win32/build/config.w32
win32/build/confutils.js

index ae080a9f4b04defc16a8b6d6238e56352d96c04e..43b03626a422f56e37b959efaba04e99ccb0dc70 100644 (file)
@@ -311,6 +311,14 @@ if (CLANG_TOOLSET) {
                -Wno-logical-op-parentheses -Wno-msvc-include -Wno-invalid-source-encoding -Wno-unknown-pragmas \
                -Wno-unused-command-line-argument");
        }
+
+       ARG_ENABLE("sanitizer", "Enable address sanitizer extension", "no");
+       if (PHP_SANITIZER == "yes") {
+               if (COMPILER_NUMERIC_VERSION < 500) {
+                       ERROR("Clang at least 5.0.0 required for sanitation plugins");
+               }
+               add_asan_opts("CFLAGS", "LIBS", "LDFLAGS");
+       }
 }
 
 ARG_WITH("codegen-arch", "Architecture for code generation: ia32, sse, sse2, avx, avx2", "no");
index 4db2a7c3303d4891161342900453798e18c2605d..2c603b3541e21e713a3891a9929dcf48c6a47cde 100644 (file)
@@ -1191,10 +1191,11 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir)
                MFO.WriteLine("$(BUILD_DIR)\\" + makefiletarget + ": $(DEPS_" + SAPI + ") $(" + SAPI + "_GLOBAL_OBJS) $(BUILD_DIR)\\$(PHPLIB) $(BUILD_DIR)\\" + resname  + " $(BUILD_DIR)\\" + manifest_name);
        }
 
+       var is_lib = makefiletarget.match(new RegExp("\\.lib$"));
        if (makefiletarget.match(new RegExp("\\.dll$"))) {
                ldflags = "/dll $(LDFLAGS)";
                manifest = "-@$(_VC_MANIFEST_EMBED_DLL)";
-       } else if (makefiletarget.match(new RegExp("\\.lib$"))) {
+       } else if (is_lib) {
                ldflags = "$(ARFLAGS)";
                ld = "@$(MAKE_LIB)";
        } else {
@@ -1202,6 +1203,12 @@ function SAPI(sapiname, file_list, makefiletarget, cflags, obj_dir)
                manifest = "-@$(_VC_MANIFEST_EMBED_EXE)";
        }
        
+       if (PHP_SANITIZER == "yes") {
+               if (CLANG_TOOLSET) {
+                       add_asan_opts("CFLAGS_" + SAPI, "LIBS_" + SAPI, (is_lib ? "ARFLAGS_" : "LDFLAGS_") + SAPI);
+               }
+       }
+
        if(is_pgo_desired(sapiname) && (PHP_PGI == "yes" || PHP_PGO != "no")) {
                // Add compiler and link flags if PGO options are selected
                if (PHP_DEBUG != "yes" && PHP_PGI == "yes") {
@@ -2470,6 +2477,11 @@ function generate_makefile()
                        }
                }
        }
+       if (PHP_SANITIZER == "yes") {
+               if (CLANG_TOOLSET) {
+                       extra_path = extra_path + ";" + get_clang_lib_dir() + "\\windows";
+               }
+       }
        MF.WriteLine("set-tmp-env:");
        MF.WriteLine("  @set PATH=" + extra_path + ";$(PATH)");
 
@@ -3365,3 +3377,51 @@ function check_binary_tools_sdk()
        }
 }
 
+function get_clang_lib_dir()
+{
+       var ret = null;
+       var ver = null;
+
+       if (COMPILER_NAME.match(/clang version ([\d\.]+) \((.*)\)/)) {
+               ver = RegExp.$1;
+       } else {
+               ERROR("Faled to determine clang lib path");
+       }
+
+       /* FIXME existence check, etc. */
+       if (X64) {
+               ret = PROGRAM_FILES + "\\LLVM\\lib\\clang\\" + ver + "\\lib";
+       } else {
+               ret = PROGRAM_FILESx86 + "\\LLVM\\lib\\clang\\" + ver + "\\lib";
+       }
+
+       return ret;
+}
+
+function add_asan_opts(cflags_name, libs_name, ldflags_name)
+{
+
+       var ver = null;
+
+       if (COMPILER_NAME.match(/clang version ([\d\.]+) \((.*)\)/)) {
+               ver = RegExp.$1;
+       } else {
+               ERROR("Faled to determine clang lib path");
+       }
+
+       if (!!cflags_name) {
+               ADD_FLAG(cflags_name, "-fsanitize=address");
+       }
+       if (!!libs_name) {
+               if (X64) {
+                       ADD_FLAG(libs_name, "clang_rt.asan_dynamic-x86_64.lib clang_rt.asan_dynamic_runtime_thunk-x86_64.lib");
+               } else {
+                       ADD_FLAG(libs_name, "clang_rt.asan_dynamic-i386.lib clang_rt.asan_dynamic_runtime_thunk-i386.lib");
+               }
+       }
+
+       if (!!ldflags_name) {
+               ADD_FLAG(ldflags_name, "/libpath:\"" + get_clang_lib_dir() + "\\windows\"");
+       }
+}
+