]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Add a method for checking if ZSTD was compiled with flags that impact determinism
authorNick Terrell <terrelln@meta.com>
Thu, 6 Mar 2025 22:31:57 +0000 (17:31 -0500)
committerNick Terrell <nickrterrell@gmail.com>
Fri, 7 Mar 2025 15:31:19 +0000 (10:31 -0500)
lib/common/portability_macros.h
lib/common/zstd_common.c
lib/zstd.h
programs/zstdcli.c

index 860734141dfbd934ea104aae98b6e7675e8b8ffb..bcca634e4192c800333871de7951a9ce8dbcc275 100644 (file)
 # define ZSTD_CET_ENDBRANCH
 #endif
 
+/**
+ * ZSTD_IS_DETERMINISTIC_BUILD must be set to 0 if any compilation macro is
+ * active that impacts the compressed output.
+ *
+ * NOTE: ZSTD_MULTITHREAD is allowed to be set or unset.
+ */
+#if defined(ZSTD_CLEVEL_DEFAULT) \
+    || defined(ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR) \
+    || defined(ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR) \
+    || defined(ZSTD_EXCLUDE_LAZY_BLOCK_COMPRESSOR) \
+    || defined(ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR) \
+    || defined(ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR) \
+    || defined(ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR) \
+    || defined(ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR)
+# define ZSTD_IS_DETERMINISTIC_BUILD 0
+#else
+# define ZSTD_IS_DETERMINISTIC_BUILD 1
+#endif
+
 #endif /* ZSTD_PORTABILITY_MACROS_H */
index 3f04c22abf6cc331c63e7ea926a456b74b205e3e..98542061870cf1eea49c6f39f7c3402fa5b2ce5a 100644 (file)
@@ -46,3 +46,12 @@ ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
 /*! ZSTD_getErrorString() :
  *  provides error code string from enum */
 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
+
+int ZSTD_isDeterministicBuild(void)
+{
+#if ZSTD_IS_DETERMINISTIC_BUILD
+    return 1;
+#else
+    return 0;
+#endif
+}
index b8c0644a7ec6533916e06aa8392a2727cbe55573..4ce2f7742978188ae40e5d41c16226c8ad5ddee3 100644 (file)
@@ -3138,6 +3138,18 @@ ZSTDLIB_STATIC_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
 
 
 
+/*! ZSTD_isDeterministicBuild() :
+ * Returns 1 if the library is built using standard compilation flags,
+ * and participates in determinism guarantees with other builds of the
+ * same version.
+ * If this function returns 0, it means the library was compiled with
+ * non-standard compilation flags that change the output of the
+ * compressor.
+ * This is mainly used for Zstd's determinism test suite, which is only
+ * run when this function returns 1.
+ */
+ZSTDLIB_API int ZSTD_isDeterministicBuild(void);
+
 
 /* ========================================= */
 /**       Block level API (DEPRECATED)       */
index e2771f5340cf2f649e49d4c5fa0ee4ef53e331db..66e9d064ba91496d3d077af95c61f4e41e56b161 100644 (file)
@@ -695,7 +695,10 @@ static void printVersion(void)
         #ifdef PLATFORM_POSIX_VERSION
             DISPLAYOUT("PLATFORM_POSIX_VERSION defined: %ldL\n", (long) PLATFORM_POSIX_VERSION);
         #endif
-    }   }
+
+            if (!ZSTD_isDeterministicBuild()) {
+                DISPLAYOUT("non-deterministic build\n");
+    }   }   }
 }
 
 #define ZSTD_NB_STRATEGIES 9