microsoft / SymCrypt

Cryptographic library
MIT License
660 stars 68 forks source link

LLVM Clang error: definition of builtin function '__cpuid' #27

Closed ajkhoury closed 11 months ago

ajkhoury commented 1 year ago

Building on Ubuntu 22.10 with LLVM Clang 15 the following error occurs when attempting to compile linux/intrinsics.c:

SymCrypt/lib/linux/intrinsics.c:8:6: error: definition of builtin function '__cpuid'

Please consider adding a simple __has_builtin guard to linux/intrinsics.c in order to avoid redefining the cpuid intrinsics for compilers that support the cpuid intrinsics as builtins. Below is a patch:

diff --git lib/linux/intrinsics.c lib/linux/intrinsics.c
index 8670254..0935176 100644
--- lib/linux/intrinsics.c
+++ lib/linux/intrinsics.c
@@ -5,6 +5,12 @@
 // Copyright (c) Microsoft Corporation. Licensed under the MIT license.
 //

+#if !defined(__has_builtin)     // Optional of course.
+#define __has_builtin(x) 0      // Compatibility with non-clang compilers.
+#endif
+
+#if !__has_builtin(__cpuid)
+
 void __cpuid(int CPUInfo[4], int InfoType)
 {
     asm volatile ("cpuid"
@@ -12,9 +18,15 @@ void __cpuid(int CPUInfo[4], int InfoType)
         : "a" (InfoType));
 }

+#endif // !__has_builtin(__cpuid)
+
+#if !__has_builtin(__cpuidex)
+
 void __cpuidex(int CPUInfo[4], int InfoType, int ECXValue)
 {
     asm volatile ("cpuid"
         : "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
         : "a" (InfoType), "c" (ECXValue));
 }
+
+#endif // !__has_builtin(__cpuidex)
\ No newline at end of file
mlindgren commented 1 year ago

Thanks for your report. This has been fixed internally and will be published with our next release.

mlindgren commented 11 months ago

We've now published the latest version which contains this fix. Sorry for the delay.