Prevent use of linux-x64 binaries with v1 microarchitecture

This commit is contained in:
Lovell Fuller 2025-03-19 17:24:46 +00:00
parent d419aba76d
commit 3fd818c4b5
5 changed files with 31 additions and 2 deletions

View File

@ -24,6 +24,8 @@ Requires libvips v8.16.1
* Expose WebP `smartDeblock` output option. * Expose WebP `smartDeblock` output option.
* Prevent use of linux-x64 binaries with v1 microarchitecture.
* Add `autoOrient` operation and constructor option. * Add `autoOrient` operation and constructor option.
[#4151](https://github.com/lovell/sharp/pull/4151) [#4151](https://github.com/lovell/sharp/pull/4151)
[@happycollision](https://github.com/happycollision) [@happycollision](https://github.com/happycollision)

View File

@ -17,9 +17,9 @@ const paths = [
'@img/sharp-wasm32/sharp.node' '@img/sharp-wasm32/sharp.node'
]; ];
let sharp; let path, sharp;
const errors = []; const errors = [];
for (const path of paths) { for (path of paths) {
try { try {
sharp = require(path); sharp = require(path);
break; break;
@ -29,6 +29,14 @@ for (const path of paths) {
} }
} }
/* istanbul ignore next */
if (sharp && path.startsWith('@img/sharp-linux-x64') && !sharp._isUsingX64V2()) {
const err = new Error('Prebuilt binaries for linux-x64 require v2 microarchitecture');
err.code = 'Unsupported CPU';
errors.push(err);
sharp = null;
}
/* istanbul ignore next */ /* istanbul ignore next */
if (sharp) { if (sharp) {
module.exports = sharp; module.exports = sharp;

View File

@ -33,6 +33,7 @@ Napi::Object init(Napi::Env env, Napi::Object exports) {
exports.Set("block", Napi::Function::New(env, block)); exports.Set("block", Napi::Function::New(env, block));
exports.Set("_maxColourDistance", Napi::Function::New(env, _maxColourDistance)); exports.Set("_maxColourDistance", Napi::Function::New(env, _maxColourDistance));
exports.Set("_isUsingJemalloc", Napi::Function::New(env, _isUsingJemalloc)); exports.Set("_isUsingJemalloc", Napi::Function::New(env, _isUsingJemalloc));
exports.Set("_isUsingX64V2", Napi::Function::New(env, _isUsingX64V2));
exports.Set("stats", Napi::Function::New(env, stats)); exports.Set("stats", Napi::Function::New(env, stats));
return exports; return exports;
} }

View File

@ -267,3 +267,20 @@ Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(env, false); return Napi::Boolean::New(env, false);
} }
#endif #endif
#if defined(__GNUC__) && defined(__x86_64__)
// Are SSE 4.2 intrinsics available at runtime?
Napi::Value _isUsingX64V2(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
unsigned int eax, ebx, ecx, edx;
__asm__ __volatile__("cpuid"
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
: "a"(1));
return Napi::Boolean::New(env, (ecx & 1U << 20) != 0);
}
#else
Napi::Value _isUsingX64V2(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::Boolean::New(env, false);
}
#endif

View File

@ -15,5 +15,6 @@ Napi::Value format(const Napi::CallbackInfo& info);
void block(const Napi::CallbackInfo& info); void block(const Napi::CallbackInfo& info);
Napi::Value _maxColourDistance(const Napi::CallbackInfo& info); Napi::Value _maxColourDistance(const Napi::CallbackInfo& info);
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info); Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info);
Napi::Value _isUsingX64V2(const Napi::CallbackInfo& info);
#endif // SRC_UTILITIES_H_ #endif // SRC_UTILITIES_H_