Coverage Report

Created: 2026-11-23 00:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5HGcache.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*-------------------------------------------------------------------------
14
 *
15
 * Created:   H5HGcache.c
16
 *
17
 * Purpose:   Implement global heap metadata cache methods
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5HGmodule.h" /* This source code file is part of the H5HG module */
27
28
/***********/
29
/* Headers */
30
/***********/
31
#include "H5private.h"   /* Generic Functions           */
32
#include "H5Eprivate.h"  /* Error handling              */
33
#include "H5Fprivate.h"  /* File access                 */
34
#include "H5FLprivate.h" /* Free Lists                               */
35
#include "H5HGpkg.h"     /* Global heaps                */
36
#include "H5MMprivate.h" /* Memory management           */
37
38
/****************/
39
/* Local Macros */
40
/****************/
41
42
/******************/
43
/* Local Typedefs */
44
/******************/
45
46
/********************/
47
/* Package Typedefs */
48
/********************/
49
50
/********************/
51
/* Local Prototypes */
52
/********************/
53
54
/* Metadata cache callbacks */
55
static herr_t H5HG__cache_heap_get_initial_load_size(void *udata, size_t *image_len);
56
static herr_t H5HG__cache_heap_get_final_load_size(const void *_image, size_t image_len, void *udata,
57
                                                   size_t *actual_len);
58
static void  *H5HG__cache_heap_deserialize(const void *image, size_t len, void *udata, bool *dirty);
59
static herr_t H5HG__cache_heap_image_len(const void *thing, size_t *image_len);
60
static herr_t H5HG__cache_heap_serialize(const H5F_t *f, void *image, size_t len, void *thing);
61
static herr_t H5HG__cache_heap_free_icr(void *thing);
62
63
/* Prefix deserialization */
64
static herr_t H5HG__hdr_deserialize(H5HG_heap_t *heap, const uint8_t *image, size_t len, const H5F_t *f);
65
66
/*********************/
67
/* Package Variables */
68
/*********************/
69
70
/* H5HG inherits cache-like properties from H5AC */
71
const H5AC_class_t H5AC_GHEAP[1] = {{
72
    H5AC_GHEAP_ID,                          /* Metadata client ID */
73
    "global heap",                          /* Metadata client name (for debugging) */
74
    H5FD_MEM_GHEAP,                         /* File space memory type for client */
75
    H5AC__CLASS_SPECULATIVE_LOAD_FLAG,      /* Client class behavior flags */
76
    H5HG__cache_heap_get_initial_load_size, /* 'get_initial_load_size' callback */
77
    H5HG__cache_heap_get_final_load_size,   /* 'get_final_load_size' callback */
78
    NULL,                                   /* 'verify_chksum' callback */
79
    H5HG__cache_heap_deserialize,           /* 'deserialize' callback */
80
    H5HG__cache_heap_image_len,             /* 'image_len' callback */
81
    NULL,                                   /* 'pre_serialize' callback */
82
    H5HG__cache_heap_serialize,             /* 'serialize' callback */
83
    NULL,                                   /* 'notify' callback */
84
    H5HG__cache_heap_free_icr,              /* 'free_icr' callback */
85
    NULL,                                   /* 'fsf_size' callback */
86
}};
87
88
/*****************************/
89
/* Library Private Variables */
90
/*****************************/
91
92
/*******************/
93
/* Local Variables */
94
/*******************/
95
96
/*-------------------------------------------------------------------------
97
 * Function:    H5HG__hdr_deserialize
98
 *
99
 * Purpose:     Decode a global heap's header
100
 *
101
 * Return:      SUCCEED/FAIL
102
 *-------------------------------------------------------------------------
103
 */
104
static herr_t
105
H5HG__hdr_deserialize(H5HG_heap_t *heap, const uint8_t *image, size_t len, const H5F_t *f)
106
4.21k
{
107
4.21k
    const uint8_t *p_end     = image + len - 1; /* End of image buffer */
108
4.21k
    herr_t         ret_value = SUCCEED;
109
110
4.21k
    FUNC_ENTER_PACKAGE
111
112
4.21k
    assert(heap);
113
4.21k
    assert(image);
114
4.21k
    assert(f);
115
116
    /* Magic number */
117
4.21k
    if (H5_IS_BUFFER_OVERFLOW(image, H5_SIZEOF_MAGIC, p_end))
118
0
        HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
119
4.21k
    if (memcmp(image, H5HG_MAGIC, (size_t)H5_SIZEOF_MAGIC) != 0)
120
135
        HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, FAIL, "bad global heap collection signature");
121
4.07k
    image += H5_SIZEOF_MAGIC;
122
123
    /* Version */
124
4.07k
    if (H5_IS_BUFFER_OVERFLOW(image, 1, p_end))
125
0
        HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
126
4.07k
    if (H5HG_VERSION != *image++)
127
7
        HGOTO_ERROR(H5E_HEAP, H5E_VERSION, FAIL, "wrong version number in global heap");
128
129
    /* Reserved */
130
4.07k
    if (H5_IS_BUFFER_OVERFLOW(image, 3, p_end))
131
0
        HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
132
4.07k
    image += 3;
133
134
    /* Size */
135
4.07k
    if (H5_IS_BUFFER_OVERFLOW(image, H5F_sizeof_size(f), p_end))
136
0
        HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
137
4.07k
    H5F_DECODE_LENGTH(f, image, heap->size);
138
4.07k
    if (heap->size < H5HG_MINSIZE)
139
22
        HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, FAIL, "global heap size is too small");
140
141
4.21k
done:
142
4.21k
    FUNC_LEAVE_NOAPI(ret_value)
143
4.21k
} /* end H5HG__hdr_deserialize() */
144
145
/*-------------------------------------------------------------------------
146
 * Function:    H5HG__cache_heap_get_initial_load_size
147
 *
148
 * Purpose:     Return the initial speculative read size to the metadata
149
 *              cache.  This size will be used in the initial attempt to read
150
 *              the global heap.  If this read is too small, the cache will
151
 *              try again with the correct value obtained from
152
 *              H5HG__cache_get_final_load_size().
153
 *
154
 * Return:      SUCCEED/FAIL
155
 *-------------------------------------------------------------------------
156
 */
157
static herr_t
158
H5HG__cache_heap_get_initial_load_size(void H5_ATTR_UNUSED *_udata, size_t *image_len)
159
2.38k
{
160
2.38k
    FUNC_ENTER_PACKAGE_NOERR
161
162
2.38k
    assert(image_len);
163
164
2.38k
    *image_len = H5HG_MINSIZE;
165
166
2.38k
    FUNC_LEAVE_NOAPI(SUCCEED)
167
2.38k
} /* end H5HG__cache_heap_get_initial_load_size() */
168
169
/*-------------------------------------------------------------------------
170
 * Function:    H5HG__cache_heap_get_final_load_size
171
 *
172
 * Purpose:     Return the final read size for a speculatively ready heap to
173
 *              the metadata cache.
174
 *
175
 * Return:      SUCCEED/FAIL
176
 *-------------------------------------------------------------------------
177
 */
178
static herr_t
179
H5HG__cache_heap_get_final_load_size(const void *image, size_t image_len, void *udata, size_t *actual_len)
180
2.21k
{
181
2.21k
    H5HG_heap_t heap;
182
2.21k
    herr_t      ret_value = SUCCEED;
183
184
2.21k
    FUNC_ENTER_PACKAGE
185
186
2.21k
    assert(image);
187
2.21k
    assert(udata);
188
2.21k
    assert(actual_len);
189
2.21k
    assert(*actual_len == image_len);
190
2.21k
    assert(image_len == H5HG_MINSIZE);
191
192
    /* Deserialize the heap's header */
193
2.21k
    heap.size = 0;
194
2.21k
    if (H5HG__hdr_deserialize(&heap, (const uint8_t *)image, image_len, (const H5F_t *)udata) < 0)
195
164
        HGOTO_ERROR(H5E_HEAP, H5E_CANTDECODE, FAIL, "can't decode global heap prefix");
196
197
    /* Set the actual global heap size */
198
2.05k
    *actual_len = heap.size;
199
200
2.21k
done:
201
2.21k
    FUNC_LEAVE_NOAPI(ret_value)
202
2.21k
} /* end H5HG__cache_heap_get_final_load_size() */
203
204
/*-------------------------------------------------------------------------
205
 * Function:    H5HG__cache_heap_deserialize
206
 *
207
 * Purpose:     Given a buffer containing the on disk image of the global
208
 *              heap, deserialize it, load its contents into a newly allocated
209
 *              instance of H5HG_heap_t, and return a pointer to the new
210
 *              instance.
211
 *
212
 * Return:      Success:    Pointer to a new global heap
213
 *              Failure:    NULL
214
 *-------------------------------------------------------------------------
215
 */
216
static void *
217
H5HG__cache_heap_deserialize(const void *_image, size_t len, void *_udata, bool H5_ATTR_UNUSED *dirty)
218
1.99k
{
219
1.99k
    H5F_t         *f         = (H5F_t *)_udata; /* File pointer */
220
1.99k
    H5HG_heap_t   *heap      = NULL;            /* New global heap */
221
1.99k
    uint8_t       *p         = NULL;            /* Pointer to objects in (copied) image buffer */
222
1.99k
    const uint8_t *p_end     = NULL;            /* End of (copied) image buffer */
223
1.99k
    size_t         max_idx   = 0;               /* Maximum heap object index seen */
224
1.99k
    size_t         nalloc    = 0;               /* Number of objects allocated */
225
1.99k
    void          *ret_value = NULL;
226
227
1.99k
    FUNC_ENTER_PACKAGE
228
229
1.99k
    assert(_image);
230
1.99k
    assert(len >= (size_t)H5HG_MINSIZE);
231
1.99k
    assert(f);
232
1.99k
    assert(dirty);
233
234
    /* Allocate a new global heap */
235
1.99k
    if (NULL == (heap = H5FL_CALLOC(H5HG_heap_t)))
236
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
237
1.99k
    heap->shared = H5F_SHARED(f);
238
1.99k
    if (NULL == (heap->chunk = H5FL_BLK_MALLOC(gheap_chunk, len)))
239
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
240
241
    /* Copy the image buffer into the newly allocated chunk */
242
1.99k
    H5MM_memcpy(heap->chunk, _image, len);
243
244
    /* Set p_end
245
     *
246
     * Note that parsing moves along p / heap->chunk, so p_end
247
     * has to refer to the end of that buffer and NOT _image
248
     */
249
1.99k
    p_end = heap->chunk + len - 1;
250
251
    /* Deserialize the heap's header */
252
1.99k
    if (H5_IS_BUFFER_OVERFLOW(heap->chunk, H5HG_SIZEOF_HDR(f), p_end))
253
0
        HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
254
1.99k
    if (H5HG__hdr_deserialize(heap, (const uint8_t *)heap->chunk, len, f) < 0)
255
0
        HGOTO_ERROR(H5E_HEAP, H5E_CANTDECODE, NULL, "can't decode global heap header");
256
257
    /* Decode each object */
258
259
    /* Set the p pointer to the objects in heap->chunk */
260
1.99k
    p = heap->chunk + H5HG_SIZEOF_HDR(f);
261
262
    /* Set the number of allocated objects */
263
1.99k
    nalloc = H5HG_NOBJS(f, heap->size);
264
265
    /* Calloc the obj array because the file format spec makes no guarantee
266
     * about the order of the objects, and unused slots must be set to zero.
267
     */
268
1.99k
    if (NULL == (heap->obj = H5FL_SEQ_CALLOC(H5HG_obj_t, nalloc)))
269
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
270
1.99k
    heap->nalloc = nalloc;
271
272
87.1k
    while (p < (heap->chunk + heap->size)) {
273
274
86.0k
        if ((p + H5HG_SIZEOF_OBJHDR(f)) > (heap->chunk + heap->size)) {
275
276
            /* The last bit of space is too tiny for an object header, so
277
             * we assume that it's free space.
278
             */
279
43
            if (NULL != heap->obj[0].begin)
280
16
                HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "object 0 should not be set");
281
27
            heap->obj[0].size  = (size_t)(((const uint8_t *)heap->chunk + heap->size) - p);
282
27
            heap->obj[0].begin = p;
283
284
            /* No buffer overflow check here since this just moves the pointer
285
             * to the end of the buffer, which was calculated above
286
             */
287
27
            p += heap->obj[0].size;
288
27
        }
289
85.9k
        else {
290
85.9k
            size_t   need = 0;  /* # bytes needed to store the object */
291
85.9k
            unsigned idx;       /* Heap object index */
292
85.9k
            uint8_t *begin = p; /* Pointer to start of object */
293
294
            /* Parse a normal heap entry */
295
296
85.9k
            if (H5_IS_BUFFER_OVERFLOW(p, 2, p_end))
297
0
                HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
298
85.9k
            UINT16DECODE(p, idx);
299
300
            /* Check if we need more room to store heap objects */
301
85.9k
            if (idx >= heap->nalloc) {
302
612
                size_t      new_alloc; /* New allocation number */
303
612
                H5HG_obj_t *new_obj;   /* New array of object descriptions */
304
305
                /* Determine the new number of objects to index */
306
612
                new_alloc = MAX(heap->nalloc * 2, (idx + 1));
307
612
                if (idx >= new_alloc)
308
0
                    HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "inappropriate heap index");
309
310
                /* Reallocate array of objects */
311
612
                if (NULL == (new_obj = H5FL_SEQ_REALLOC(H5HG_obj_t, heap->obj, new_alloc)))
312
0
                    HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
313
314
                /* Clear newly allocated space */
315
612
                memset(&new_obj[heap->nalloc], 0, (new_alloc - heap->nalloc) * sizeof(heap->obj[0]));
316
317
                /* Update heap information */
318
612
                heap->nalloc = new_alloc;
319
612
                heap->obj    = new_obj;
320
612
                if (heap->nalloc <= heap->nused)
321
0
                    HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "inappropriate # allocated slots");
322
612
            }
323
324
            /* Number of references */
325
85.9k
            if (H5_IS_BUFFER_OVERFLOW(p, 2, p_end))
326
0
                HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
327
85.9k
            UINT16DECODE(p, heap->obj[idx].nrefs);
328
329
            /* Reserved bytes */
330
85.9k
            if (H5_IS_BUFFER_OVERFLOW(p, 4, p_end))
331
0
                HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
332
85.9k
            p += 4;
333
334
            /* Object length */
335
85.9k
            if (H5_IS_BUFFER_OVERFLOW(p, H5F_sizeof_size(f), p_end))
336
0
                HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
337
85.9k
            H5F_DECODE_LENGTH(f, p, heap->obj[idx].size);
338
339
            /* Object
340
             *
341
             * Points to beginning of object, INCLUDING the header.
342
             */
343
85.9k
            heap->obj[idx].begin = begin;
344
345
            /* The total storage size includes the size of the object
346
             * header and is zero padded so the next object header is
347
             * properly aligned. The entire obj array was calloc'ed,
348
             * so no need to zero the space here. The last bit of space
349
             * is the free space object whose size is never padded and
350
             * already includes the object header.
351
             */
352
85.9k
            if (idx > 0) {
353
84.7k
                need = H5HG_SIZEOF_OBJHDR(f) + H5HG_ALIGN(heap->obj[idx].size);
354
84.7k
                if (idx > max_idx)
355
74.8k
                    max_idx = idx;
356
84.7k
            }
357
1.25k
            else
358
1.25k
                need = heap->obj[idx].size;
359
360
            /* Make sure the extra padding doesn't cause us to overrun
361
             * the buffer
362
             */
363
85.9k
            if (H5_IS_BUFFER_OVERFLOW(begin, need, p_end))
364
877
                HGOTO_ERROR(H5E_HEAP, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
365
85.1k
            p = begin + need;
366
85.1k
        }
367
86.0k
    }
368
369
    /* Post-parse checks */
370
1.10k
    if (p != heap->chunk + heap->size)
371
0
        HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "partially decoded global heap");
372
1.10k
    if (false == H5HG_ISALIGNED(heap->obj[0].size))
373
14
        HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "decoded global heap is not aligned");
374
375
    /* Set the next index value to use when creating a new object */
376
1.08k
    if (max_idx > 0)
377
1.08k
        heap->nused = max_idx + 1;
378
0
    else
379
0
        heap->nused = 1;
380
381
1.08k
    if (max_idx >= heap->nused)
382
0
        HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "bad `next unused` heap index value");
383
384
    /* Add the new heap to the CWFS list for the file */
385
1.08k
    if (H5F_cwfs_add(f, heap) < 0)
386
0
        HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, NULL, "unable to add global heap collection to file's CWFS");
387
388
1.08k
    ret_value = heap;
389
390
1.99k
done:
391
1.99k
    if (!ret_value && heap)
392
907
        if (H5HG__free(heap) < 0)
393
0
            HDONE_ERROR(H5E_HEAP, H5E_CANTFREE, NULL, "unable to destroy global heap collection");
394
395
1.99k
    FUNC_LEAVE_NOAPI(ret_value)
396
1.99k
} /* end H5HG__cache_heap_deserialize() */
397
398
/*-------------------------------------------------------------------------
399
 * Function:    H5HG__cache_heap_image_len
400
 *
401
 * Purpose:     Return the on disk image size of the global heap to the
402
 *              metadata cache via the image_len.
403
 *
404
 * Return:      SUCCEED/FAIL
405
 *-------------------------------------------------------------------------
406
 */
407
static herr_t
408
H5HG__cache_heap_image_len(const void *_thing, size_t *image_len)
409
0
{
410
0
    const H5HG_heap_t *heap = (const H5HG_heap_t *)_thing;
411
412
0
    FUNC_ENTER_PACKAGE_NOERR
413
414
0
    assert(heap);
415
0
    assert(heap->cache_info.type == H5AC_GHEAP);
416
0
    assert(heap->size >= H5HG_MINSIZE);
417
0
    assert(image_len);
418
419
0
    *image_len = heap->size;
420
421
0
    FUNC_LEAVE_NOAPI(SUCCEED)
422
0
} /* end H5HG__cache_heap_image_len() */
423
424
/*-------------------------------------------------------------------------
425
 * Function:    H5HG__cache_heap_serialize
426
 *
427
 * Purpose:     Given an appropriately sized buffer and an instance of
428
 *              H5HG_heap_t, serialize the global heap for writing to file,
429
 *              and copy the serialized version into the buffer.
430
 *
431
 *
432
 * Return:      SUCCEED/FAIL
433
 *-------------------------------------------------------------------------
434
 */
435
static herr_t
436
H5HG__cache_heap_serialize(const H5F_t H5_ATTR_NDEBUG_UNUSED *f, void *image, size_t len, void *_thing)
437
0
{
438
0
    H5HG_heap_t *heap = (H5HG_heap_t *)_thing;
439
440
0
    FUNC_ENTER_PACKAGE_NOERR
441
442
0
    assert(f);
443
0
    assert(image);
444
0
    assert(heap);
445
0
    assert(heap->cache_info.type == H5AC_GHEAP);
446
0
    assert(heap->size == len);
447
0
    assert(heap->chunk);
448
449
    /* Copy the image into the buffer */
450
0
    H5MM_memcpy(image, heap->chunk, len);
451
452
0
    FUNC_LEAVE_NOAPI(SUCCEED)
453
0
} /* end H5HG__cache_heap_serialize() */
454
455
/*-------------------------------------------------------------------------
456
 * Function:    H5HG__cache_heap_free_icr
457
 *
458
 * Purpose:     Free the in memory representation of the supplied global heap.
459
 *
460
 * Return:      SUCCEED/FAIL
461
 *-------------------------------------------------------------------------
462
 */
463
static herr_t
464
H5HG__cache_heap_free_icr(void *_thing)
465
1.08k
{
466
1.08k
    H5HG_heap_t *heap      = (H5HG_heap_t *)_thing;
467
1.08k
    herr_t       ret_value = SUCCEED;
468
469
1.08k
    FUNC_ENTER_PACKAGE
470
471
1.08k
    assert(heap);
472
1.08k
    assert(heap->cache_info.type == H5AC_GHEAP);
473
474
    /* Destroy global heap collection */
475
1.08k
    if (H5HG__free(heap) < 0)
476
0
        HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to destroy global heap collection");
477
478
1.08k
done:
479
1.08k
    FUNC_LEAVE_NOAPI(ret_value)
480
1.08k
} /* end H5HG__cache_heap_free_icr() */