Add option for using Address and Thread sanitizers
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#define FORCE_INLINE inline
|
||||
#define FORCE_NOINLINE __attribute__((noinline))
|
||||
#define NO_RETURN __attribute__((noreturn))
|
||||
#define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
|
||||
#define PACK_BEGIN()
|
||||
#define PACK_END() __attribute__((__packed__))
|
||||
#define ALIGN_BEGIN(_align)
|
||||
@@ -44,6 +45,7 @@
|
||||
#define FORCE_INLINE inline
|
||||
#define FORCE_NOINLINE __attribute__((noinline))
|
||||
#define NO_RETURN __attribute__((noreturn))
|
||||
#define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
|
||||
#define PACK_BEGIN()
|
||||
#define PACK_END() __attribute__((__packed__))
|
||||
#define ALIGN_BEGIN(_align)
|
||||
@@ -69,6 +71,7 @@
|
||||
#define FORCE_INLINE __forceinline
|
||||
#define FORCE_NOINLINE __declspec(noinline)
|
||||
#define NO_RETURN __declspec(noreturn)
|
||||
#define NO_SANITIZE_ADDRESS
|
||||
#define PACK_BEGIN() __pragma(pack(push, 1))
|
||||
#define PACK_END() ; __pragma(pack(pop))
|
||||
#define ALIGN_BEGIN(_align) __declspec(align(_align))
|
||||
|
||||
@@ -26,6 +26,28 @@ namespace Flax.Build.NativeCpp
|
||||
SmallCode
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The code sanitizers for core errors detection by compiler-supported checks.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum Sanitizer
|
||||
{
|
||||
/// <summary>
|
||||
/// No sanitizers in use.
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Memory errors detector,
|
||||
/// </summary>
|
||||
Address = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Data races and deadlocks detector.
|
||||
/// </summary>
|
||||
Thread = 2,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The compilation optimization hint.
|
||||
/// </summary>
|
||||
@@ -67,6 +89,11 @@ namespace Flax.Build.NativeCpp
|
||||
/// </summary>
|
||||
public FavorSizeOrSpeed FavorSizeOrSpeed = FavorSizeOrSpeed.Neither;
|
||||
|
||||
/// <summary>
|
||||
/// Selects a sanitizers to use (as flags).
|
||||
/// </summary>
|
||||
public Sanitizer Sanitizers = Sanitizer.None;
|
||||
|
||||
/// <summary>
|
||||
/// Enables exceptions support.
|
||||
/// </summary>
|
||||
@@ -184,6 +211,7 @@ namespace Flax.Build.NativeCpp
|
||||
{
|
||||
CppVersion = CppVersion,
|
||||
FavorSizeOrSpeed = FavorSizeOrSpeed,
|
||||
Sanitizers = Sanitizers,
|
||||
EnableExceptions = EnableExceptions,
|
||||
RuntimeTypeInfo = RuntimeTypeInfo,
|
||||
Inlining = Inlining,
|
||||
|
||||
@@ -249,6 +249,7 @@ namespace Flax.Build
|
||||
}
|
||||
|
||||
options.CompileEnv.EnableExceptions = true; // TODO: try to disable this!
|
||||
options.CompileEnv.Sanitizers = Configuration.Sanitizers;
|
||||
switch (options.Configuration)
|
||||
{
|
||||
case TargetConfiguration.Debug:
|
||||
|
||||
@@ -182,5 +182,18 @@ namespace Flax.Build
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Utility that outputs the arguments for Clang with specific sanitizer.
|
||||
/// </summary>
|
||||
/// <param name="options">The sanitizers as flags.</param>
|
||||
/// <param name="options">The arguments list.</param>
|
||||
protected void AddClangSanitizerArgs(Sanitizer sanitizers, List<string> args)
|
||||
{
|
||||
if (sanitizers.HasFlag(Sanitizer.Address))
|
||||
args.Add("-fsanitize=address");
|
||||
if (sanitizers.HasFlag(Sanitizer.Thread))
|
||||
args.Add("-fsanitize=thread");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,6 +225,12 @@ namespace Flax.Build
|
||||
[CommandLine("compiler", "<name>", "Overrides the compiler to use for building. Eg. v140 overrides the toolset when building for Windows.")]
|
||||
public static string Compiler = null;
|
||||
|
||||
/// <summary>
|
||||
/// Selects a sanitizers to use (as flags). Options: Address, Thread.
|
||||
/// </summary>
|
||||
[CommandLine("sanitizers", "<name>", "Selects a sanitizers to use (as flags). Options: Address, Thread.")]
|
||||
public static Flax.Build.NativeCpp.Sanitizer Sanitizers = Flax.Build.NativeCpp.Sanitizer.None;
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the dotnet SDK version to use for the build. Eg. set to '7' to use .NET 7 even if .NET 8 is installed.
|
||||
/// </summary>
|
||||
@@ -242,6 +248,8 @@ namespace Flax.Build
|
||||
cmdLine += " -compiler=" + Compiler;
|
||||
if (!string.IsNullOrEmpty(Dotnet))
|
||||
cmdLine += " -dotnet=" + Dotnet;
|
||||
if (Sanitizers != Flax.Build.NativeCpp.Sanitizer.None)
|
||||
cmdLine += " -sanitizers=" + Sanitizers.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -105,6 +105,7 @@ namespace Flax.Build.Platforms
|
||||
commonArgs.Add("objective-c++");
|
||||
commonArgs.Add("-stdlib=libc++");
|
||||
AddArgsCommon(options, commonArgs);
|
||||
AddClangSanitizerArgs(compileEnvironment.Sanitizers, commonArgs);
|
||||
|
||||
switch (compileEnvironment.CppVersion)
|
||||
{
|
||||
@@ -155,14 +156,24 @@ namespace Flax.Build.Platforms
|
||||
|
||||
commonArgs.Add("-pthread");
|
||||
|
||||
if (compileEnvironment.FavorSizeOrSpeed == FavorSizeOrSpeed.FastCode)
|
||||
commonArgs.Add("-Ofast");
|
||||
else if (compileEnvironment.FavorSizeOrSpeed == FavorSizeOrSpeed.SmallCode)
|
||||
commonArgs.Add("-Os");
|
||||
if (compileEnvironment.Optimization)
|
||||
commonArgs.Add("-O3");
|
||||
if (compileEnvironment.Sanitizers.HasFlag(Sanitizer.Address))
|
||||
{
|
||||
commonArgs.Add("-fno-optimize-sibling-calls");
|
||||
commonArgs.Add("-fno-omit-frame-pointer");
|
||||
if (compileEnvironment.Optimization)
|
||||
commonArgs.Add("-O1");
|
||||
}
|
||||
else
|
||||
commonArgs.Add("-O0");
|
||||
{
|
||||
if (compileEnvironment.FavorSizeOrSpeed == FavorSizeOrSpeed.FastCode)
|
||||
commonArgs.Add("-Ofast");
|
||||
else if (compileEnvironment.FavorSizeOrSpeed == FavorSizeOrSpeed.SmallCode)
|
||||
commonArgs.Add("-Os");
|
||||
if (compileEnvironment.Optimization)
|
||||
commonArgs.Add("-O3");
|
||||
else
|
||||
commonArgs.Add("-O0");
|
||||
}
|
||||
|
||||
if (compileEnvironment.BufferSecurityCheck)
|
||||
commonArgs.Add("-fstack-protector");
|
||||
@@ -240,6 +251,7 @@ namespace Flax.Build.Platforms
|
||||
{
|
||||
args.Add(string.Format("-o \"{0}\"", outputFilePath));
|
||||
AddArgsCommon(options, args);
|
||||
AddClangSanitizerArgs(options.CompileEnv.Sanitizers, args);
|
||||
|
||||
if (isArchive)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user