rusticl/mesa: create proper build-id hash for the disk cache

Without generating a proper timestamp for the disk cache, we pull old
binaries out of the disk cache, potentially being buggy or simply
outdated.

Once meson 1.2 lands we can easily pull in LLVM functions.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Nora Allen <blackcatgames@protonmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21612>
This commit is contained in:
Karol Herbst 2023-03-01 00:39:25 +01:00 committed by Marge Bot
parent 29b932512a
commit fbe9a7ca3e
2 changed files with 27 additions and 5 deletions

View file

@ -40,9 +40,13 @@ static mut DISK_CACHE: Option<DiskCache> = None;
static DISK_CACHE_ONCE: Once = Once::new();
fn get_disk_cache() -> &'static Option<DiskCache> {
let func_ptrs = [
// ourselves
get_disk_cache as _,
];
unsafe {
DISK_CACHE_ONCE.call_once(|| {
DISK_CACHE = DiskCache::new("rusticl", "rusticl", 0);
DISK_CACHE = DiskCache::new("rusticl", &func_ptrs, 0);
});
&DISK_CACHE
}

View file

@ -1,6 +1,6 @@
use mesa_rust_gen::*;
use std::ffi::CString;
use std::ffi::{c_void, CString};
use std::ops::Deref;
use std::ptr;
use std::ptr::NonNull;
@ -74,10 +74,28 @@ impl DiskCacheBorrowed {
}
impl DiskCache {
pub fn new(name: &str, driver_id: &str, flags: u64) -> Option<Self> {
pub fn new(name: &str, func_ptrs: &[*mut c_void], flags: u64) -> Option<Self> {
let c_name = CString::new(name).unwrap();
let c_id = CString::new(driver_id).unwrap();
let cache = unsafe { disk_cache_create(c_name.as_ptr(), c_id.as_ptr(), flags) };
let mut sha_ctx = SHA1_CTX::default();
let mut sha = [0; SHA1_DIGEST_LENGTH as usize];
let mut cache_id = [0; SHA1_DIGEST_STRING_LENGTH as usize];
let cache = unsafe {
SHA1Init(&mut sha_ctx);
for &func_ptr in func_ptrs {
if !disk_cache_get_function_identifier(func_ptr, &mut sha_ctx) {
return None;
}
}
SHA1Final(&mut sha, &mut sha_ctx);
mesa_bytes_to_hex(
cache_id.as_mut_ptr(),
sha.as_ptr(),
(cache_id.len() - 1) as u32,
);
disk_cache_create(c_name.as_ptr(), cache_id.as_ptr(), flags)
};
DiskCacheBorrowed::from_ptr(cache).map(|c| Self { inner: c })
}