Illegal Instruction crash in sodium_malloc on Intel CPUs in v1.0.19 (from prebuilt binaries) #1358
Replies: 12 comments 5 replies
-
Oh, I'm guessing that this line from the release notes should probably have been a tip-off:
|
Beta Was this translation helpful? Give feedback.
-
OK, so these two CPUs don't support AVX, hence the error. If I take baseline, what do I lose? Is it only performance? |
Beta Was this translation helpful? Give feedback.
-
Hi! The AVX instruction set has been around for a very long time (it was introduced circa 2011), so I blindly assumed that the .NET framework didn't even support CPUs that didn't have it. The legacy build system enables support for AVX, AVX2, etc. only for specific files where these instructions are required. But in Zig builds, everything is currently using the same compilation flags. Sandy bridge looked like a reasonable baseline target for .NET. But apparently not. The best way to solve this would be to port the old logic to the zig build system. If you take |
Beta Was this translation helpful? Give feedback.
-
Thanks; is AES-GCM depending on AVX and AVX2 a part of the 19 version as part of the re-write? Or has AES-GCM required AVX for a while? I may have to bundle both native binaries, and load the one I want at runtime based on detecting AVX availability. |
Beta Was this translation helpful? Give feedback.
-
Oh, also worth noting that these processors are quite new (2021-ish), but they are processors for embedded hardware and micro PCs that may just have less features. |
Beta Was this translation helpful? Give feedback.
-
Hi again @jedisct1; I've got past the illegal instruction error by creating a build for the In 1.0.18 These CPUs are typically lower-powered IoT devices, so hardware-accelerating AES is pretty important for us; what's the impact of removing the check for avx on AESGCM on our build? Am I correct in that the rewrite of AES-GCM adds the hard dependency on AVX? |
Beta Was this translation helpful? Give feedback.
-
OK, that works. I'm now bundling two builds of libsodium with my .NET app, For future arrivals at this discussion, here's the .NET code that lets me switch out the libsodium binary, called from protected static void InitialiseLibsodiumSelection()
{
static bool PlatformRequiresNoAvxFallback()
{
// On x64 linux, on processors that do not support the AVX2 instruction set,
// we need to use our alternate version of libsodium.
return OperatingSystem.IsLinux() &&
RuntimeInformation.ProcessArchitecture == Architecture.X64 &&
!Avx2.IsSupported;
}
static IntPtr ResolveFallbackLibsodium(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName == "libsodium")
{
return NativeLibrary.Load("libsodium-noavx.so", assembly, searchPath);
}
// fall back to the default resolver.
return IntPtr.Zero;
}
if (PlatformRequiresNoAvxFallback())
{
Console.WriteLine("No AVX2 support detected, falling back to libsodium version with no AVX requirement.");
NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), ResolveFallbackLibsodium);
}
} |
Beta Was this translation helpful? Give feedback.
-
Hit this Problem as well, twice in fact. Missing AVX can easily happen on VMs where the selected guest CPU feature-set is conservative for live migration (often the default). Or on long term support platforms such as the mentioned Goldmont, Tremont Generation (Jasper Lake CPUs), these are Products launched in 2021 and everywhere in embedded PCs, NAS systems, cheap computers etc. This makes shipping software with the .net library very difficult. Maybe there could be a -compat version of the libsodium nuget package or just include a fallback .so and switch in the .net code as mentioned above. The stable lightweight implementation of libsodium makes it attractive for embedded use, requiring AVX turns out to make this quite a hassle. |
Beta Was this translation helpful? Give feedback.
-
I'm working on this. Essentially enabling CPU-specific flags only on required files, as in the old build system. |
Beta Was this translation helpful? Give feedback.
-
9b369db should address this. |
Beta Was this translation helpful? Give feedback.
-
Ok, it took much more work than anticipated, but that should finally be fixed, including in |
Beta Was this translation helpful? Give feedback.
-
Version |
Beta Was this translation helpful? Give feedback.
-
Hi,
Since upgrading our dependency on libsodium to v1.0.19 we've seen illegal instruction errors reported on multiple Intel CPUs when running linux, one on the Jasper Lake (Tremont) micro-architecture, and one on the Apollo Lake (Goldmont) micro-architecture (so far).
The failing code path appears to be
sodium_malloc
(please forgive the image, had to capture an image on a customer call):Here's a procinfo output for two of the failing devices, one CPU each:
Intel N5095:
Intel J3455
Now, I can see that in b5bd5b8 @jedisct1 configured the zig builds to target sandybridge instead of baseline. I'm going to assume that this may be the cause of the incompatibility with these CPUs.
Two questions then:
Thanks
Beta Was this translation helpful? Give feedback.
All reactions