Coverage Report

Created: 2026-01-09 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Ocache.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:   H5Ocache.c
16
 *
17
 * Purpose:   Object header metadata cache virtual functions
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5Omodule.h" /* This source code file is part of the H5O module */
27
28
/***********/
29
/* Headers */
30
/***********/
31
#include "H5private.h"   /* Generic Functions           */
32
#include "H5Eprivate.h"  /* Error handling              */
33
#include "H5FLprivate.h" /* Free lists                  */
34
#include "H5MMprivate.h" /* Memory management           */
35
#include "H5Opkg.h"      /* Object headers              */
36
37
/****************/
38
/* Local Macros */
39
/****************/
40
41
/******************/
42
/* Local Typedefs */
43
/******************/
44
45
/********************/
46
/* Package Typedefs */
47
/********************/
48
49
/********************/
50
/* Local Prototypes */
51
/********************/
52
53
/* Metadata cache callbacks */
54
static herr_t H5O__cache_get_initial_load_size(void *udata, size_t *image_len);
55
static herr_t H5O__cache_get_final_load_size(const void *image_ptr, size_t image_len, void *udata,
56
                                             size_t *actual_len);
57
static htri_t H5O__cache_verify_chksum(const void *image_ptr, size_t len, void *udata_ptr);
58
static void  *H5O__cache_deserialize(const void *image, size_t len, void *udata, bool *dirty);
59
static herr_t H5O__cache_image_len(const void *thing, size_t *image_len);
60
static herr_t H5O__cache_serialize(const H5F_t *f, void *image, size_t len, void *thing);
61
static herr_t H5O__cache_notify(H5AC_notify_action_t action, void *_thing);
62
static herr_t H5O__cache_free_icr(void *thing);
63
64
static herr_t H5O__cache_chk_get_initial_load_size(void *udata, size_t *image_len);
65
static htri_t H5O__cache_chk_verify_chksum(const void *image_ptr, size_t len, void *udata_ptr);
66
static void  *H5O__cache_chk_deserialize(const void *image, size_t len, void *udata, bool *dirty);
67
static herr_t H5O__cache_chk_image_len(const void *thing, size_t *image_len);
68
static herr_t H5O__cache_chk_serialize(const H5F_t *f, void *image, size_t len, void *thing);
69
static herr_t H5O__cache_chk_notify(H5AC_notify_action_t action, void *_thing);
70
static herr_t H5O__cache_chk_free_icr(void *thing);
71
72
/* Prefix routines */
73
static herr_t H5O__prefix_deserialize(const uint8_t *image, size_t len, H5O_cache_ud_t *udata);
74
75
/* Chunk routines */
76
static herr_t H5O__chunk_deserialize(H5O_t *oh, haddr_t addr, size_t chunk_size, const uint8_t *image,
77
                                     size_t len, H5O_common_cache_ud_t *udata, bool *dirty);
78
static herr_t H5O__chunk_serialize(const H5F_t *f, H5O_t *oh, unsigned chunkno);
79
80
/* Misc. routines */
81
static herr_t H5O__add_cont_msg(H5O_cont_msgs_t *cont_msg_info, const H5O_cont_t *cont);
82
83
/*********************/
84
/* Package Variables */
85
/*********************/
86
87
/* H5O object header prefix inherits cache-like properties from H5AC */
88
const H5AC_class_t H5AC_OHDR[1] = {{
89
    H5AC_OHDR_ID,                      /* Metadata client ID */
90
    "object header",                   /* Metadata client name (for debugging) */
91
    H5FD_MEM_OHDR,                     /* File space memory type for client */
92
    H5AC__CLASS_SPECULATIVE_LOAD_FLAG, /* Client class behavior flags */
93
    H5O__cache_get_initial_load_size,  /* 'get_initial_load_size' callback */
94
    H5O__cache_get_final_load_size,    /* 'get_final_load_size' callback */
95
    H5O__cache_verify_chksum,          /* 'verify_chksum' callback */
96
    H5O__cache_deserialize,            /* 'deserialize' callback */
97
    H5O__cache_image_len,              /* 'image_len' callback */
98
    NULL,                              /* 'pre_serialize' callback */
99
    H5O__cache_serialize,              /* 'serialize' callback */
100
    H5O__cache_notify,                 /* 'notify' callback */
101
    H5O__cache_free_icr,               /* 'free_icr' callback */
102
    NULL,                              /* 'fsf_size' callback */
103
}};
104
105
/* H5O object header chunk inherits cache-like properties from H5AC */
106
const H5AC_class_t H5AC_OHDR_CHK[1] = {{
107
    H5AC_OHDR_CHK_ID,                     /* Metadata client ID */
108
    "object header continuation chunk",   /* Metadata client name (for debugging) */
109
    H5FD_MEM_OHDR,                        /* File space memory type for client */
110
    H5AC__CLASS_NO_FLAGS_SET,             /* Client class behavior flags */
111
    H5O__cache_chk_get_initial_load_size, /* 'get_initial_load_size' callback */
112
    NULL,                                 /* 'get_final_load_size' callback */
113
    H5O__cache_chk_verify_chksum,         /* 'verify_chksum' callback */
114
    H5O__cache_chk_deserialize,           /* 'deserialize' callback */
115
    H5O__cache_chk_image_len,             /* 'image_len' callback */
116
    NULL,                                 /* 'pre_serialize' callback */
117
    H5O__cache_chk_serialize,             /* 'serialize' callback */
118
    H5O__cache_chk_notify,                /* 'notify' callback */
119
    H5O__cache_chk_free_icr,              /* 'free_icr' callback */
120
    NULL,                                 /* 'fsf_size' callback */
121
}};
122
123
/* Declare external the free list for H5O_unknown_t's */
124
H5FL_EXTERN(H5O_unknown_t);
125
126
/* Declare extern the free list for H5O_chunk_proxy_t's */
127
H5FL_EXTERN(H5O_chunk_proxy_t);
128
129
/* Declare the free list for H5O_cont_t sequences */
130
H5FL_SEQ_DEFINE(H5O_cont_t);
131
132
/*****************************/
133
/* Library Private Variables */
134
/*****************************/
135
136
/*******************/
137
/* Local Variables */
138
/*******************/
139
140
/*-------------------------------------------------------------------------
141
 * Function:    H5O__cache_get_initial_load_size()
142
 *
143
 * Purpose:     Tell the metadata cache how much data to read from file in
144
 *              the first speculative read for the object header.
145
 *
146
 * Return:      SUCCEED/FAIL
147
 *-------------------------------------------------------------------------
148
 */
149
static herr_t
150
H5O__cache_get_initial_load_size(void H5_ATTR_UNUSED *_udata, size_t *image_len)
151
72
{
152
72
    FUNC_ENTER_PACKAGE_NOERR
153
154
72
    assert(image_len);
155
156
    /* Set the image length size */
157
72
    *image_len = H5O_SPEC_READ_SIZE;
158
159
72
    FUNC_LEAVE_NOAPI(SUCCEED)
160
72
} /* end H5O__cache_get_initial_load_size() */
161
162
/*-------------------------------------------------------------------------
163
 * Function:    H5O__cache_get_final_load_size()
164
 *
165
 * Purpose:     Tell the metadata cache the final size of an object header.
166
 *
167
 * Return:      SUCCEED/FAIL
168
 *-------------------------------------------------------------------------
169
 */
170
static herr_t
171
H5O__cache_get_final_load_size(const void *image, size_t image_len, void *_udata, size_t *actual_len)
172
71
{
173
71
    H5O_cache_ud_t *udata     = (H5O_cache_ud_t *)_udata; /* User data for callback */
174
71
    herr_t          ret_value = SUCCEED;
175
176
71
    FUNC_ENTER_PACKAGE
177
178
71
    assert(image);
179
71
    assert(udata);
180
71
    assert(actual_len);
181
71
    assert(*actual_len == image_len);
182
183
    /* Deserialize the object header prefix */
184
71
    if (H5O__prefix_deserialize((const uint8_t *)image, image_len, udata) < 0)
185
7
        HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, FAIL, "can't deserialize object header prefix");
186
187
    /* Sanity check */
188
71
    assert(udata->oh);
189
190
    /* Set the final size for the cache image */
191
64
    *actual_len = udata->chunk0_size + (size_t)H5O_SIZEOF_HDR(udata->oh);
192
193
    /* Save the oh version to be used later in verify_chksum callback
194
       because oh will be freed before leaving this routine */
195
64
    udata->oh_version = udata->oh->version;
196
197
    /* Free allocated memory: fix github issue #3970 */
198
64
    if (H5O__free(udata->oh, false) < 0)
199
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "can't destroy object header");
200
64
    udata->oh = NULL;
201
202
71
done:
203
71
    FUNC_LEAVE_NOAPI(ret_value)
204
71
} /* end H5O__cache_get_final_load_size() */
205
206
/*-------------------------------------------------------------------------
207
 * Function:    H5O__cache_verify_chksum
208
 *
209
 * Purpose:     Verify the computed checksum of the data structure is the
210
 *              same as the stored chksum.
211
 *
212
 * Return:      Success:        true/false
213
 *              Failure:        Negative
214
 *-------------------------------------------------------------------------
215
 */
216
static htri_t
217
H5O__cache_verify_chksum(const void *_image, size_t len, void *_udata)
218
64
{
219
64
    const uint8_t  *image     = (const uint8_t *)_image;  /* Pointer into raw data buffer */
220
64
    H5O_cache_ud_t *udata     = (H5O_cache_ud_t *)_udata; /* User data for callback */
221
64
    htri_t          ret_value = true;
222
223
64
    FUNC_ENTER_PACKAGE
224
225
64
    assert(image);
226
64
    assert(udata);
227
228
    /* There is no checksum for version 1 */
229
64
    if (udata->oh_version != H5O_VERSION_1) {
230
5
        uint32_t stored_chksum;   /* Stored metadata checksum value */
231
5
        uint32_t computed_chksum; /* Computed metadata checksum value */
232
233
        /* Get stored and computed checksums */
234
5
        if (H5F_get_checksums(image, len, &stored_chksum, &computed_chksum) < 0)
235
1
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't get checksums");
236
237
4
        if (stored_chksum != computed_chksum)
238
4
            ret_value = false;
239
4
    }
240
59
    else
241
64
        assert(!(udata->common.file_intent & H5F_ACC_SWMR_WRITE));
242
243
64
done:
244
64
    FUNC_LEAVE_NOAPI(ret_value)
245
64
} /* end H5O__cache_verify_chksum() */
246
247
/*-------------------------------------------------------------------------
248
 * Function:    H5O__cache_deserialize
249
 *
250
 * Purpose:     Attempt to deserialize the object header contained in the
251
 *              supplied buffer, load the data into an instance of H5O_t, and
252
 *              return a pointer to the new instance.
253
 *
254
 *              Note that the object header is read with with a speculative
255
 *              read. If the initial read is too small, make note of this fact
256
 *              and return without error.  H5C__load_entry() will note the
257
 *              size discrepancy and retry the deserialize operation with
258
 *              the correct size read.
259
 *
260
 * Return:      Success:        Pointer to in core representation
261
 *              Failure:        NULL
262
 *-------------------------------------------------------------------------
263
 */
264
static void *
265
H5O__cache_deserialize(const void *image, size_t len, void *_udata, bool *dirty)
266
59
{
267
59
    H5O_t          *oh        = NULL;                     /* Object header read in */
268
59
    H5O_cache_ud_t *udata     = (H5O_cache_ud_t *)_udata; /* User data for callback */
269
59
    void           *ret_value = NULL;
270
271
59
    FUNC_ENTER_PACKAGE
272
273
59
    assert(image);
274
59
    assert(len > 0);
275
59
    assert(udata);
276
59
    assert(udata->common.f);
277
59
    assert(udata->common.cont_msg_info);
278
59
    assert(dirty);
279
59
    assert(udata->oh == NULL);
280
281
59
    if (H5O__prefix_deserialize((const uint8_t *)image, len, udata) < 0)
282
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, NULL, "can't deserialize object header prefix");
283
59
    assert(udata->oh);
284
285
59
    oh = udata->oh;
286
287
    /* Set SWMR flag, if appropriate */
288
59
    oh->swmr_write = !!(H5F_INTENT(udata->common.f) & H5F_ACC_SWMR_WRITE);
289
290
    /* Create object header proxy if doing SWMR writes */
291
59
    if (oh->swmr_write) {
292
        /* Create virtual entry, for use as proxy */
293
0
        if (NULL == (oh->proxy = H5AC_proxy_entry_create()))
294
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTCREATE, NULL, "can't create object header proxy");
295
0
    }
296
59
    else
297
59
        oh->proxy = NULL;
298
299
    /* Parse the first chunk */
300
59
    if (H5O__chunk_deserialize(oh, udata->common.addr, udata->chunk0_size, (const uint8_t *)image, len,
301
59
                               &(udata->common), dirty) < 0)
302
1
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, NULL, "can't deserialize first object header chunk");
303
304
    /* Check for corruption in object header # of messages */
305
58
    if (oh->version == H5O_VERSION_1 && udata->v1_pfx_nmesgs < oh->nmesgs)
306
0
        HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, NULL, "bad object header message count");
307
308
    /* Note that we've loaded the object header from the file */
309
58
    udata->made_attempt = true;
310
311
    /* Set return value */
312
58
    ret_value = oh;
313
314
59
done:
315
    /* Release the [possibly partially initialized] object header on errors */
316
59
    if (!ret_value && oh)
317
1
        if (H5O__free(oh, false) < 0)
318
0
            HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, NULL, "unable to destroy object header data");
319
320
59
    FUNC_LEAVE_NOAPI(ret_value)
321
59
} /* end H5O__cache_deserialize() */
322
323
/*-------------------------------------------------------------------------
324
 * Function:    H5O__cache_image_len
325
 *
326
 * Purpose:     Compute the size in bytes of the specified instance of
327
 *              H5O_t on disk, and return it in *image_len.  On failure,
328
 *              the value of *image_len is undefined.
329
 *
330
 * Return:      SUCCEED/FAIL
331
 *-------------------------------------------------------------------------
332
 */
333
static herr_t
334
H5O__cache_image_len(const void *_thing, size_t *image_len)
335
0
{
336
0
    const H5O_t *oh = (const H5O_t *)_thing; /* Object header to query */
337
338
0
    FUNC_ENTER_PACKAGE_NOERR
339
340
0
    assert(oh);
341
0
    assert(oh->cache_info.type == H5AC_OHDR);
342
0
    assert(image_len);
343
344
    /* Report the object header's prefix+first chunk length */
345
0
    *image_len = oh->chunk[0].size;
346
347
0
    FUNC_LEAVE_NOAPI(SUCCEED)
348
0
} /* end H5O__cache_image_len() */
349
350
/*-------------------------------------------------------------------------
351
 * Function:    H5O__cache_serialize
352
 *
353
 * Purpose:     Serialize the contents of the supplied object header, and
354
 *              load this data into the supplied buffer.
355
 *
356
 * Return:      SUCCEED/FAIL
357
 *-------------------------------------------------------------------------
358
 */
359
static herr_t
360
H5O__cache_serialize(const H5F_t *f, void *image, size_t len, void *_thing)
361
28
{
362
28
    H5O_t   *oh = (H5O_t *)_thing; /* Object header to encode */
363
28
    uint8_t *chunk_image;          /* Pointer to object header prefix buffer */
364
28
    herr_t   ret_value = SUCCEED;
365
366
28
    FUNC_ENTER_PACKAGE
367
368
28
    assert(f);
369
28
    assert(image);
370
28
    assert(oh);
371
28
    assert(oh->cache_info.type == H5AC_OHDR);
372
28
    assert(oh->chunk[0].size == len);
373
#ifdef H5O_DEBUG
374
    H5O__assert(oh);
375
#endif /* H5O_DEBUG */
376
377
    /* Point to raw data 'image' for first chunk, which
378
     * has room for the prefix
379
     */
380
28
    chunk_image = oh->chunk[0].image;
381
382
    /* Later versions of object header prefix have different format and
383
     * also require that chunk 0 always be updated, since the checksum
384
     * on the entire block of memory needs to be updated if anything is
385
     * modified
386
     */
387
28
    if (oh->version > H5O_VERSION_1) {
388
0
        uint64_t chunk0_size; /* Size of chunk 0's data */
389
390
0
        assert(oh->chunk[0].size >= (size_t)H5O_SIZEOF_HDR(oh));
391
0
        chunk0_size = oh->chunk[0].size - (size_t)H5O_SIZEOF_HDR(oh);
392
393
        /* Verify magic number */
394
0
        assert(!memcmp(chunk_image, H5O_HDR_MAGIC, H5_SIZEOF_MAGIC));
395
0
        chunk_image += H5_SIZEOF_MAGIC;
396
397
        /* Version */
398
0
        *chunk_image++ = oh->version;
399
400
        /* Flags */
401
0
        *chunk_image++ = oh->flags;
402
403
        /* Time fields */
404
0
        if (oh->flags & H5O_HDR_STORE_TIMES) {
405
0
            UINT32ENCODE(chunk_image, oh->atime);
406
0
            UINT32ENCODE(chunk_image, oh->mtime);
407
0
            UINT32ENCODE(chunk_image, oh->ctime);
408
0
            UINT32ENCODE(chunk_image, oh->btime);
409
0
        }
410
411
        /* Attribute fields */
412
0
        if (oh->flags & H5O_HDR_ATTR_STORE_PHASE_CHANGE) {
413
0
            UINT16ENCODE(chunk_image, oh->max_compact);
414
0
            UINT16ENCODE(chunk_image, oh->min_dense);
415
0
        }
416
417
        /* First chunk size */
418
0
        switch (oh->flags & H5O_HDR_CHUNK0_SIZE) {
419
0
            case 0: /* 1 byte size */
420
0
                assert(chunk0_size < 256);
421
0
                *chunk_image++ = (uint8_t)chunk0_size;
422
0
                break;
423
424
0
            case 1: /* 2 byte size */
425
0
                assert(chunk0_size < 65536);
426
0
                UINT16ENCODE(chunk_image, chunk0_size);
427
0
                break;
428
429
0
            case 2: /* 4 byte size */
430
                /* use <= 2**32 -1 to stay within 4 bytes integer range */
431
0
                assert(chunk0_size <= 4294967295UL);
432
0
                UINT32ENCODE(chunk_image, chunk0_size);
433
0
                break;
434
435
0
            case 3: /* 8 byte size */
436
0
                UINT64ENCODE(chunk_image, chunk0_size);
437
0
                break;
438
439
0
            default:
440
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad size for chunk 0");
441
0
        }
442
0
    }
443
28
    else {
444
        /* Version */
445
28
        *chunk_image++ = oh->version;
446
447
        /* Reserved */
448
28
        *chunk_image++ = 0;
449
450
        /* Number of messages */
451
#ifdef H5O_ENABLE_BAD_MESG_COUNT
452
        if (oh->store_bad_mesg_count)
453
            UINT16ENCODE(chunk_image, (oh->nmesgs - 1));
454
        else
455
#endif /* H5O_ENABLE_BAD_MESG_COUNT */
456
28
            UINT16ENCODE(chunk_image, oh->nmesgs);
457
458
        /* Link count */
459
28
        UINT32ENCODE(chunk_image, oh->nlink);
460
461
        /* First chunk size */
462
28
        UINT32ENCODE(chunk_image, (oh->chunk[0].size - (size_t)H5O_SIZEOF_HDR(oh)));
463
464
        /* Zero to alignment */
465
28
        memset(chunk_image, 0, (size_t)(H5O_SIZEOF_HDR(oh) - 12));
466
28
        chunk_image += (size_t)(H5O_SIZEOF_HDR(oh) - 12);
467
28
    }
468
469
28
    assert((size_t)(chunk_image - oh->chunk[0].image) ==
470
28
           (size_t)(H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh)));
471
472
    /* Serialize messages for this chunk */
473
28
    if (H5O__chunk_serialize(f, oh, (unsigned)0) < 0)
474
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTSERIALIZE, FAIL, "unable to serialize first object header chunk");
475
476
    /* copy the chunk into the image -- this is potentially expensive.
477
     * Can we rework things so that the object header and the cache
478
     * share a buffer?
479
     */
480
28
    H5MM_memcpy(image, oh->chunk[0].image, len);
481
482
28
done:
483
28
    FUNC_LEAVE_NOAPI(ret_value)
484
28
} /* end H5O__cache_serialize() */
485
486
/*-------------------------------------------------------------------------
487
 * Function:    H5O__cache_notify
488
 *
489
 * Purpose:     Handle cache action notifications
490
 *
491
 * Return:      SUCCEED/FAIL
492
 *-------------------------------------------------------------------------
493
 */
494
static herr_t
495
H5O__cache_notify(H5AC_notify_action_t action, void *_thing)
496
181
{
497
181
    H5O_t *oh        = (H5O_t *)_thing;
498
181
    herr_t ret_value = SUCCEED;
499
500
181
    FUNC_ENTER_PACKAGE
501
502
181
    assert(oh);
503
504
181
    switch (action) {
505
0
        case H5AC_NOTIFY_ACTION_AFTER_INSERT:
506
58
        case H5AC_NOTIFY_ACTION_AFTER_LOAD:
507
58
            if (oh->swmr_write) {
508
                /* Sanity check */
509
0
                assert(oh->proxy);
510
511
                /* Register the object header as a parent of the virtual entry */
512
0
                if (H5AC_proxy_entry_add_parent(oh->proxy, oh) < 0)
513
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL, "can't add object header as parent of proxy");
514
0
            }
515
58
            break;
516
517
58
        case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
518
36
        case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
519
            /* Do nothing */
520
36
            break;
521
522
29
        case H5AC_NOTIFY_ACTION_ENTRY_CLEANED: {
523
29
            unsigned u; /* Local index variable */
524
525
            /* Mark messages stored with the object header (i.e. messages in chunk 0) as clean */
526
813
            for (u = 0; u < oh->nmesgs; u++)
527
784
                if (oh->mesg[u].chunkno == 0)
528
475
                    oh->mesg[u].dirty = false;
529
#ifndef NDEBUG
530
            /* Reset the number of messages dirtied by decoding */
531
            oh->ndecode_dirtied = 0;
532
#endif
533
29
        } break;
534
535
0
        case H5AC_NOTIFY_ACTION_CHILD_DIRTIED:
536
0
        case H5AC_NOTIFY_ACTION_CHILD_CLEANED:
537
0
        case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED:
538
0
        case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED:
539
            /* Do nothing */
540
0
            break;
541
542
58
        case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
543
58
            if (oh->swmr_write) {
544
                /* Unregister the object header as a parent of the virtual entry */
545
0
                if (H5AC_proxy_entry_remove_parent(oh->proxy, oh) < 0)
546
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL, "can't remove object header as parent of proxy");
547
0
            }
548
58
            break;
549
550
58
        default:
551
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "unknown action from metadata cache");
552
181
    }
553
554
181
done:
555
181
    FUNC_LEAVE_NOAPI(ret_value)
556
181
} /* end H5O__cache_notify() */
557
558
/*-------------------------------------------------------------------------
559
 * Function:    H5O__cache_free_icr
560
 *
561
 * Purpose:     Free the in core representation of the supplied object header.
562
 *
563
 * Return:      SUCCEED/FAIL
564
 *-------------------------------------------------------------------------
565
 */
566
static herr_t
567
H5O__cache_free_icr(void *_thing)
568
58
{
569
58
    H5O_t *oh        = (H5O_t *)_thing; /* Object header to destroy */
570
58
    herr_t ret_value = SUCCEED;
571
572
58
    FUNC_ENTER_PACKAGE
573
574
58
    assert(oh);
575
58
    assert(oh->cache_info.type == H5AC_OHDR);
576
577
    /* Destroy object header */
578
58
    if (H5O__free(oh, false) < 0)
579
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "can't destroy object header");
580
581
58
done:
582
58
    FUNC_LEAVE_NOAPI(ret_value)
583
58
} /* end H5O__cache_free_icr() */
584
585
/*-------------------------------------------------------------------------
586
 * Function:    H5O__cache_chk_get_initial_load_size()
587
 *
588
 * Purpose:     Tell the metadata cache how large the on disk image of the
589
 *              chunk proxy is, so it can load the image into a buffer for the
590
 *              deserialize call.
591
 *
592
 * Return:      SUCCEED/FAIL
593
 *-------------------------------------------------------------------------
594
 */
595
static herr_t
596
H5O__cache_chk_get_initial_load_size(void *_udata, size_t *image_len)
597
42
{
598
42
    const H5O_chk_cache_ud_t *udata = (const H5O_chk_cache_ud_t *)_udata; /* User data for callback */
599
600
42
    FUNC_ENTER_PACKAGE_NOERR
601
602
42
    assert(udata);
603
42
    assert(udata->oh);
604
42
    assert(image_len);
605
42
    assert(udata->size);
606
607
    /* Set the image length size */
608
42
    *image_len = udata->size;
609
610
42
    FUNC_LEAVE_NOAPI(SUCCEED)
611
42
} /* end H5O__cache_chk_get_initial_load_size() */
612
613
/*-------------------------------------------------------------------------
614
 * Function:    H5B2__cache_chk_verify_chksum
615
 *
616
 * Purpose:     Verify the computed checksum of the data structure is the
617
 *              same as the stored chksum.
618
 *
619
 * Return:      Success:        true/false
620
 *              Failure:        Negative
621
 *-------------------------------------------------------------------------
622
 */
623
static htri_t
624
H5O__cache_chk_verify_chksum(const void *_image, size_t len, void *_udata)
625
42
{
626
42
    const uint8_t      *image     = (const uint8_t *)_image;      /* Pointer into raw data buffer */
627
42
    H5O_chk_cache_ud_t *udata     = (H5O_chk_cache_ud_t *)_udata; /* User data for callback */
628
42
    htri_t              ret_value = true;
629
630
42
    FUNC_ENTER_PACKAGE
631
632
42
    assert(image);
633
634
    /* There is no checksum for version 1 */
635
42
    if (udata->oh->version != H5O_VERSION_1) {
636
0
        uint32_t stored_chksum;   /* Stored metadata checksum value */
637
0
        uint32_t computed_chksum; /* Computed metadata checksum value */
638
639
        /* Get stored and computed checksums */
640
0
        if (H5F_get_checksums(image, len, &stored_chksum, &computed_chksum) < 0)
641
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't get checksums");
642
643
0
        if (stored_chksum != computed_chksum)
644
0
            ret_value = false;
645
0
    }
646
647
42
done:
648
42
    FUNC_LEAVE_NOAPI(ret_value)
649
42
} /* end H5O__cache_chk_verify_chksum() */
650
651
/*-------------------------------------------------------------------------
652
 * Function:    H5O__cache_chk_deserialize
653
 *
654
 * Purpose:     Attempt to deserialize the object header continuation chunk
655
 *              contained in the supplied buffer, load the data into an instance
656
 *              of H5O_chunk_proxy_t, and return a pointer to the new instance.
657
 *
658
 * Return:      Success:        Pointer to in core representation
659
 *              Failure:        NULL
660
 *-------------------------------------------------------------------------
661
 */
662
static void *
663
H5O__cache_chk_deserialize(const void *image, size_t len, void *_udata, bool *dirty)
664
42
{
665
42
    H5O_chunk_proxy_t  *chk_proxy = NULL;                         /* Chunk proxy object */
666
42
    H5O_chk_cache_ud_t *udata     = (H5O_chk_cache_ud_t *)_udata; /* User data for callback */
667
42
    void               *ret_value = NULL;
668
669
42
    FUNC_ENTER_PACKAGE
670
671
42
    assert(image);
672
42
    assert(len > 0);
673
42
    assert(udata);
674
42
    assert(udata->oh);
675
42
    assert(dirty);
676
677
    /* Allocate space for the object header data structure */
678
42
    if (NULL == (chk_proxy = H5FL_CALLOC(H5O_chunk_proxy_t)))
679
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, NULL, "memory allocation failed");
680
681
    /* Check if we are still decoding the object header */
682
    /* (as opposed to bringing a piece of it back from the file) */
683
42
    if (udata->decoding) {
684
38
        assert(udata->common.f);
685
38
        assert(udata->common.cont_msg_info);
686
687
        /* Parse the chunk */
688
38
        if (H5O__chunk_deserialize(udata->oh, udata->common.addr, udata->size, (const uint8_t *)image, len,
689
38
                                   &(udata->common), dirty) < 0)
690
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, NULL, "can't deserialize object header chunk");
691
692
        /* Set the chunk number for the chunk proxy */
693
38
        H5_CHECKED_ASSIGN(chk_proxy->chunkno, unsigned, udata->oh->nchunks - 1, size_t);
694
38
    }
695
4
    else {
696
        /* Sanity check */
697
4
        assert(udata->chunkno < udata->oh->nchunks);
698
699
        /* Set the chunk number for the chunk proxy */
700
4
        chk_proxy->chunkno = udata->chunkno;
701
702
        /* Sanity check that the chunk representation we have in memory is
703
         * the same as the one being brought in from disk.
704
         */
705
4
        assert(0 == memcmp(image, udata->oh->chunk[chk_proxy->chunkno].image,
706
4
                           udata->oh->chunk[chk_proxy->chunkno].size));
707
4
    }
708
709
    /* Increment reference count of object header */
710
42
    if (H5O__inc_rc(udata->oh) < 0)
711
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINC, NULL, "can't increment reference count on object header");
712
42
    chk_proxy->oh = udata->oh;
713
714
    /* Set return value */
715
42
    ret_value = chk_proxy;
716
717
42
done:
718
42
    if (NULL == ret_value)
719
0
        if (chk_proxy && H5O__chunk_dest(chk_proxy) < 0)
720
0
            HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, NULL, "unable to destroy object header chunk");
721
722
42
    FUNC_LEAVE_NOAPI(ret_value)
723
42
} /* end H5O__cache_chk_deserialize() */
724
725
/*-------------------------------------------------------------------------
726
 * Function:    H5O__cache_chk_image_len
727
 *
728
 * Purpose:     Return the on disk image size of a object header chunk to the
729
 *              metadata cache via the image_len.
730
 *
731
 * Return:      SUCCEED/FAIL
732
 *-------------------------------------------------------------------------
733
 */
734
static herr_t
735
H5O__cache_chk_image_len(const void *_thing, size_t *image_len)
736
7
{
737
7
    const H5O_chunk_proxy_t *chk_proxy = (const H5O_chunk_proxy_t *)_thing; /* Chunk proxy to query */
738
739
7
    FUNC_ENTER_PACKAGE_NOERR
740
741
7
    assert(chk_proxy);
742
7
    assert(chk_proxy->cache_info.type == H5AC_OHDR_CHK);
743
7
    assert(chk_proxy->oh);
744
7
    assert(image_len);
745
746
7
    *image_len = chk_proxy->oh->chunk[chk_proxy->chunkno].size;
747
748
7
    FUNC_LEAVE_NOAPI(SUCCEED)
749
7
} /* end H5O__cache_chk_image_len() */
750
751
/*-------------------------------------------------------------------------
752
 * Function:    H5O__cache_chk_serialize
753
 *
754
 * Purpose:     Given a pointer to an instance of an object header chunk and an
755
 *              appropriately sized buffer, serialize the contents of the
756
 *              instance for writing to disk, and copy the serialized data
757
 *              into the buffer.
758
 *
759
 * Return:      SUCCEED/FAIL
760
 *-------------------------------------------------------------------------
761
 */
762
static herr_t
763
H5O__cache_chk_serialize(const H5F_t *f, void *image, size_t len, void *_thing)
764
26
{
765
26
    H5O_chunk_proxy_t *chk_proxy = (H5O_chunk_proxy_t *)_thing; /* Object header chunk to serialize */
766
26
    herr_t             ret_value = SUCCEED;
767
768
26
    FUNC_ENTER_PACKAGE
769
770
26
    assert(f);
771
26
    assert(image);
772
26
    assert(chk_proxy);
773
26
    assert(chk_proxy->cache_info.type == H5AC_OHDR_CHK);
774
26
    assert(chk_proxy->oh);
775
26
    assert(chk_proxy->oh->chunk[chk_proxy->chunkno].size == len);
776
777
    /* Serialize messages for this chunk */
778
26
    if (H5O__chunk_serialize(f, chk_proxy->oh, chk_proxy->chunkno) < 0)
779
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTSERIALIZE, FAIL,
780
26
                    "unable to serialize object header continuation chunk");
781
782
    /* copy the chunk into the image -- this is potentially expensive.
783
     * Can we rework things so that the chunk and the cache share a buffer?
784
     */
785
    /* Ensure len does not exceed the size of the source buffer */
786
26
    if (len > chk_proxy->oh->chunk[chk_proxy->chunkno].size)
787
0
        HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "buffer overflow detected");
788
789
26
    H5MM_memcpy(image, chk_proxy->oh->chunk[chk_proxy->chunkno].image, len);
790
791
26
done:
792
26
    FUNC_LEAVE_NOAPI(ret_value)
793
26
} /* end H5O__cache_chk_serialize() */
794
795
/*-------------------------------------------------------------------------
796
 * Function:    H5O__cache_chk_notify
797
 *
798
 * Purpose:     Handle cache action notifications
799
 *
800
 * Return:      SUCCEED/FAIL
801
 *-------------------------------------------------------------------------
802
 */
803
static herr_t
804
H5O__cache_chk_notify(H5AC_notify_action_t action, void *_thing)
805
142
{
806
142
    H5O_chunk_proxy_t *chk_proxy = (H5O_chunk_proxy_t *)_thing;
807
142
    herr_t             ret_value = SUCCEED;
808
809
142
    FUNC_ENTER_PACKAGE
810
811
142
    assert(chk_proxy);
812
142
    assert(chk_proxy->oh);
813
814
142
    switch (action) {
815
0
        case H5AC_NOTIFY_ACTION_AFTER_INSERT:
816
42
        case H5AC_NOTIFY_ACTION_AFTER_LOAD:
817
42
            if (chk_proxy->oh->swmr_write) {
818
                /* Add flush dependency on chunk with continuation, if one exists */
819
0
                if (chk_proxy->fd_parent) {
820
                    /* Sanity checks */
821
0
                    assert(((H5C_cache_entry_t *)(chk_proxy->fd_parent))->type);
822
0
                    assert((((H5C_cache_entry_t *)(chk_proxy->fd_parent))->type->id == H5AC_OHDR_ID) ||
823
0
                           (((H5C_cache_entry_t *)(chk_proxy->fd_parent))->type->id == H5AC_OHDR_CHK_ID));
824
825
                    /* Add flush dependency from chunk containing the continuation message
826
                     * that points to this chunk (either oh or another chunk proxy object)
827
                     */
828
0
                    if (H5AC_create_flush_dependency(chk_proxy->fd_parent, chk_proxy) < 0)
829
0
                        HGOTO_ERROR(H5E_OHDR, H5E_CANTDEPEND, FAIL, "unable to create flush dependency");
830
0
                }
831
832
                /* Add flush dependency on object header */
833
0
                {
834
0
                    if (H5AC_create_flush_dependency(chk_proxy->oh, chk_proxy) < 0)
835
0
                        HGOTO_ERROR(H5E_OHDR, H5E_CANTDEPEND, FAIL, "unable to create flush dependency");
836
0
                }
837
838
                /* Add flush dependency on object header proxy, if proxy exists */
839
0
                {
840
                    /* Sanity check */
841
0
                    assert(chk_proxy->oh->proxy);
842
843
                    /* Register the object header chunk as a parent of the virtual entry */
844
0
                    if (H5AC_proxy_entry_add_parent(chk_proxy->oh->proxy, chk_proxy) < 0)
845
0
                        HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL,
846
0
                                    "can't add object header chunk as parent of proxy");
847
0
                }
848
0
            }
849
42
            break;
850
851
42
        case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
852
30
        case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
853
            /* Do nothing */
854
30
            break;
855
856
28
        case H5AC_NOTIFY_ACTION_ENTRY_CLEANED: {
857
28
            unsigned u; /* Local index variable */
858
859
            /* Mark messages in chunk as clean */
860
1.32k
            for (u = 0; u < chk_proxy->oh->nmesgs; u++)
861
1.30k
                if (chk_proxy->oh->mesg[u].chunkno == chk_proxy->chunkno)
862
363
                    chk_proxy->oh->mesg[u].dirty = false;
863
28
        } break;
864
865
0
        case H5AC_NOTIFY_ACTION_CHILD_DIRTIED:
866
0
        case H5AC_NOTIFY_ACTION_CHILD_CLEANED:
867
0
        case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED:
868
0
        case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED:
869
            /* Do nothing */
870
0
            break;
871
872
42
        case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
873
42
            if (chk_proxy->oh->swmr_write) {
874
                /* Remove flush dependency on parent object header chunk, if one is set */
875
0
                if (chk_proxy->fd_parent) {
876
                    /* Sanity checks */
877
0
                    assert(((H5C_cache_entry_t *)(chk_proxy->fd_parent))->type);
878
0
                    assert((((H5C_cache_entry_t *)(chk_proxy->fd_parent))->type->id == H5AC_OHDR_ID) ||
879
0
                           (((H5C_cache_entry_t *)(chk_proxy->fd_parent))->type->id == H5AC_OHDR_CHK_ID));
880
881
0
                    if (H5AC_destroy_flush_dependency(chk_proxy->fd_parent, chk_proxy) < 0)
882
0
                        HGOTO_ERROR(H5E_OHDR, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency");
883
0
                    chk_proxy->fd_parent = NULL;
884
0
                }
885
886
                /* Unregister the object header as a parent of the virtual entry */
887
0
                if (H5AC_destroy_flush_dependency(chk_proxy->oh, chk_proxy) < 0)
888
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency");
889
890
                /* Unregister the object header chunk as a parent of the virtual entry */
891
0
                if (H5AC_proxy_entry_remove_parent(chk_proxy->oh->proxy, chk_proxy) < 0)
892
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL,
893
0
                                "can't remove object header chunk as parent of proxy");
894
0
            }
895
42
            break;
896
897
42
        default:
898
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "unknown action from metadata cache");
899
142
    }
900
901
142
done:
902
142
    FUNC_LEAVE_NOAPI(ret_value)
903
142
} /* end H5O__cache_chk_notify() */
904
905
/*-------------------------------------------------------------------------
906
 * Function:    H5O__cache_chk_free_icr
907
 *
908
 * Purpose:     Free the in core memory associated with the supplied object
909
 *              header continuation chunk.
910
 *
911
 * Return:      SUCCEED/FAIL
912
 *-------------------------------------------------------------------------
913
 */
914
static herr_t
915
H5O__cache_chk_free_icr(void *_thing)
916
42
{
917
42
    H5O_chunk_proxy_t *chk_proxy = (H5O_chunk_proxy_t *)_thing; /* Object header chunk proxy to release */
918
42
    herr_t             ret_value = SUCCEED;
919
920
42
    FUNC_ENTER_PACKAGE
921
922
42
    assert(chk_proxy);
923
42
    assert(chk_proxy->cache_info.type == H5AC_OHDR_CHK);
924
925
    /* Destroy object header chunk proxy */
926
42
    if (H5O__chunk_dest(chk_proxy) < 0)
927
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "unable to destroy object header chunk proxy");
928
929
42
done:
930
42
    FUNC_LEAVE_NOAPI(ret_value)
931
42
} /* end H5O__cache_chk_free_icr() */
932
933
/*-------------------------------------------------------------------------
934
 * Function:  H5O__add_cont_msg
935
 *
936
 * Purpose:     Add information from a continuation message to the list of
937
 *              continuation messages in the object header
938
 *
939
 * Return:      SUCCEED/FAIL
940
 *-------------------------------------------------------------------------
941
 */
942
static herr_t
943
H5O__add_cont_msg(H5O_cont_msgs_t *cont_msg_info, const H5O_cont_t *cont)
944
77
{
945
77
    size_t contno; /* Continuation message index */
946
77
    herr_t ret_value = SUCCEED;
947
948
77
    FUNC_ENTER_PACKAGE
949
950
77
    assert(cont_msg_info);
951
77
    assert(cont);
952
953
    /* Increase chunk array size, if necessary */
954
77
    if (cont_msg_info->nmsgs >= cont_msg_info->alloc_nmsgs) {
955
26
        size_t na = MAX(H5O_NCHUNKS, cont_msg_info->alloc_nmsgs * 2); /* Double # of messages allocated */
956
26
        H5O_cont_t *x;
957
958
26
        if (NULL == (x = H5FL_SEQ_REALLOC(H5O_cont_t, cont_msg_info->msgs, na)))
959
0
            HGOTO_ERROR(H5E_OHDR, H5E_NOSPACE, FAIL, "memory allocation failed");
960
26
        cont_msg_info->alloc_nmsgs = na;
961
26
        cont_msg_info->msgs        = x;
962
26
    }
963
964
    /* Init the continuation message info */
965
77
    contno                              = cont_msg_info->nmsgs++;
966
77
    cont_msg_info->msgs[contno].addr    = cont->addr;
967
77
    cont_msg_info->msgs[contno].size    = cont->size;
968
77
    cont_msg_info->msgs[contno].chunkno = cont->chunkno;
969
970
77
done:
971
77
    FUNC_LEAVE_NOAPI(ret_value)
972
77
} /* H5O__add_cont_msg() */
973
974
/*-------------------------------------------------------------------------
975
 * Function:    H5O__prefix_deserialize()
976
 *
977
 * Purpose:     Deserialize an object header prefix
978
 *
979
 * Return:      SUCCEED/FAIL
980
 *-------------------------------------------------------------------------
981
 */
982
static herr_t
983
H5O__prefix_deserialize(const uint8_t *_image, size_t len, H5O_cache_ud_t *udata)
984
130
{
985
130
    const uint8_t *image     = (const uint8_t *)_image; /* Pointer into raw data buffer */
986
130
    const uint8_t *p_end     = image + len - 1;         /* End of image buffer */
987
130
    H5O_t         *oh        = NULL;                    /* Object header read in */
988
130
    herr_t         ret_value = SUCCEED;
989
990
130
    FUNC_ENTER_PACKAGE
991
992
130
    assert(image);
993
130
    assert(udata);
994
995
    /* Allocate space for the new object header data structure */
996
130
    if (NULL == (oh = H5FL_CALLOC(H5O_t)))
997
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "memory allocation failed");
998
999
    /* File-specific, non-stored information */
1000
130
    oh->sizeof_size = H5F_SIZEOF_SIZE(udata->common.f);
1001
130
    oh->sizeof_addr = H5F_SIZEOF_ADDR(udata->common.f);
1002
1003
    /* Check for presence of magic number */
1004
    /* (indicates version 2 or later) */
1005
130
    if (H5_IS_BUFFER_OVERFLOW(image, H5_SIZEOF_MAGIC, p_end))
1006
0
        HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1007
130
    if (!memcmp(image, H5O_HDR_MAGIC, (size_t)H5_SIZEOF_MAGIC)) {
1008
1009
        /* Magic number (bounds checked above) */
1010
5
        image += H5_SIZEOF_MAGIC;
1011
1012
        /* Version */
1013
5
        if (H5_IS_BUFFER_OVERFLOW(image, 1, p_end))
1014
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1015
5
        oh->version = *image++;
1016
5
        if (H5O_VERSION_2 != oh->version)
1017
0
            HGOTO_ERROR(H5E_OHDR, H5E_VERSION, FAIL, "bad object header version number");
1018
1019
        /* Flags */
1020
5
        if (H5_IS_BUFFER_OVERFLOW(image, 1, p_end))
1021
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1022
5
        oh->flags = *image++;
1023
5
        if (oh->flags & ~H5O_HDR_ALL_FLAGS)
1024
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "unknown object header status flag(s)");
1025
1026
        /* Number of links to object (unless overridden by refcount message) */
1027
5
        oh->nlink = 1;
1028
1029
        /* Time fields */
1030
5
        if (oh->flags & H5O_HDR_STORE_TIMES) {
1031
1
            uint32_t tmp;
1032
1033
1
            if (H5_IS_BUFFER_OVERFLOW(image, 4 + 4 + 4 + 4, p_end))
1034
0
                HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1035
1036
1
            UINT32DECODE(image, tmp);
1037
1
            oh->atime = (time_t)tmp;
1038
1
            UINT32DECODE(image, tmp);
1039
1
            oh->mtime = (time_t)tmp;
1040
1
            UINT32DECODE(image, tmp);
1041
1
            oh->ctime = (time_t)tmp;
1042
1
            UINT32DECODE(image, tmp);
1043
1
            oh->btime = (time_t)tmp;
1044
1
        }
1045
4
        else
1046
4
            oh->atime = oh->mtime = oh->ctime = oh->btime = 0;
1047
1048
        /* Attribute fields */
1049
5
        if (oh->flags & H5O_HDR_ATTR_STORE_PHASE_CHANGE) {
1050
5
            if (H5_IS_BUFFER_OVERFLOW(image, 2 + 2, p_end))
1051
0
                HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1052
1053
5
            UINT16DECODE(image, oh->max_compact);
1054
5
            UINT16DECODE(image, oh->min_dense);
1055
5
            if (oh->max_compact < oh->min_dense)
1056
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad object header attribute phase change values");
1057
5
        }
1058
0
        else {
1059
0
            oh->max_compact = H5O_CRT_ATTR_MAX_COMPACT_DEF;
1060
0
            oh->min_dense   = H5O_CRT_ATTR_MIN_DENSE_DEF;
1061
0
        }
1062
1063
        /* First chunk size */
1064
5
        switch (oh->flags & H5O_HDR_CHUNK0_SIZE) {
1065
0
            case 0: /* 1 byte size */
1066
0
                if (H5_IS_BUFFER_OVERFLOW(image, 1, p_end))
1067
0
                    HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1068
0
                udata->chunk0_size = *image++;
1069
0
                break;
1070
1071
0
            case 1: /* 2 byte size */
1072
0
                if (H5_IS_BUFFER_OVERFLOW(image, 2, p_end))
1073
0
                    HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1074
0
                UINT16DECODE(image, udata->chunk0_size);
1075
0
                break;
1076
1077
4
            case 2: /* 4 byte size */
1078
4
                if (H5_IS_BUFFER_OVERFLOW(image, 4, p_end))
1079
0
                    HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1080
4
                UINT32DECODE(image, udata->chunk0_size);
1081
4
                break;
1082
1083
1
            case 3: /* 8 byte size */
1084
1
                if (H5_IS_BUFFER_OVERFLOW(image, 8, p_end))
1085
0
                    HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1086
1
                UINT64DECODE(image, udata->chunk0_size);
1087
1
                break;
1088
1089
0
            default:
1090
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad size for chunk 0");
1091
5
        }
1092
5
        if (udata->chunk0_size > 0 && udata->chunk0_size < H5O_SIZEOF_MSGHDR_OH(oh))
1093
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad object header chunk size");
1094
5
    }
1095
125
    else {
1096
        /* Version */
1097
125
        if (H5_IS_BUFFER_OVERFLOW(image, 1, p_end))
1098
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1099
125
        oh->version = *image++;
1100
125
        if (H5O_VERSION_1 != oh->version)
1101
6
            HGOTO_ERROR(H5E_OHDR, H5E_VERSION, FAIL, "bad object header version number");
1102
1103
        /* Flags */
1104
119
        oh->flags = H5O_CRT_OHDR_FLAGS_DEF;
1105
1106
        /* Reserved */
1107
119
        if (H5_IS_BUFFER_OVERFLOW(image, 1, p_end))
1108
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1109
119
        image++;
1110
1111
        /* Number of messages */
1112
119
        if (H5_IS_BUFFER_OVERFLOW(image, 2, p_end))
1113
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1114
119
        UINT16DECODE(image, udata->v1_pfx_nmesgs);
1115
1116
        /* Link count */
1117
119
        if (H5_IS_BUFFER_OVERFLOW(image, 4, p_end))
1118
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1119
119
        UINT32DECODE(image, oh->nlink);
1120
1121
        /* Reset unused time fields */
1122
119
        oh->atime = oh->mtime = oh->ctime = oh->btime = 0;
1123
1124
        /* Reset unused attribute fields */
1125
119
        oh->max_compact = 0;
1126
119
        oh->min_dense   = 0;
1127
1128
        /* First chunk size */
1129
119
        if (H5_IS_BUFFER_OVERFLOW(image, 4, p_end))
1130
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1131
119
        UINT32DECODE(image, udata->chunk0_size);
1132
119
        if ((udata->v1_pfx_nmesgs > 0 && udata->chunk0_size < H5O_SIZEOF_MSGHDR_OH(oh)) ||
1133
119
            (udata->v1_pfx_nmesgs == 0 && udata->chunk0_size > 0))
1134
1
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad object header chunk size");
1135
1136
        /* Reserved, in version 1 (for 8-byte alignment padding) */
1137
118
        if (H5_IS_BUFFER_OVERFLOW(image, 4, p_end))
1138
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1139
118
        image += 4;
1140
118
    }
1141
1142
    /* Verify object header prefix length */
1143
123
    if ((size_t)(image - _image) != (size_t)(H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh)))
1144
0
        HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad object header prefix length");
1145
1146
    /* Save the object header for later use in 'deserialize' callback */
1147
123
    udata->oh = oh;
1148
123
    oh        = NULL;
1149
1150
130
done:
1151
    /* Release the [possibly partially initialized] object header on errors */
1152
130
    if (ret_value < 0 && oh)
1153
7
        if (H5O__free(oh, false) < 0)
1154
0
            HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "unable to destroy object header data");
1155
1156
130
    FUNC_LEAVE_NOAPI(ret_value)
1157
130
} /* end H5O__prefix_deserialize() */
1158
1159
/*-------------------------------------------------------------------------
1160
 * Function:  H5O__chunk_deserialize
1161
 *
1162
 * Purpose:     Deserialize a chunk for an object header
1163
 *
1164
 * Return:      SUCCEED/FAIL
1165
 *-------------------------------------------------------------------------
1166
 */
1167
static herr_t
1168
H5O__chunk_deserialize(H5O_t *oh, haddr_t addr, size_t chunk_size, const uint8_t *image, size_t len,
1169
                       H5O_common_cache_ud_t *udata, bool *dirty)
1170
97
{
1171
97
    const uint8_t *chunk_image = NULL;   /* Pointer into buffer to decode */
1172
97
    const uint8_t *p_end       = NULL;   /* End of image buffer */
1173
97
    uint8_t       *eom_ptr;              /* Pointer to end of messages for a chunk */
1174
97
    unsigned       merged_null_msgs = 0; /* Number of null messages merged together */
1175
97
    unsigned       chunkno;              /* Current chunk's index */
1176
97
    unsigned       nullcnt;              /* Count of null messages (for sanity checking gaps in chunks) */
1177
97
    bool           mesgs_modified =
1178
97
        false; /* Whether any messages were modified when the object header was deserialized */
1179
97
    herr_t ret_value = SUCCEED;
1180
1181
97
    FUNC_ENTER_PACKAGE
1182
1183
97
    assert(oh);
1184
97
    assert(H5_addr_defined(addr));
1185
97
    assert(image);
1186
97
    assert(len);
1187
97
    assert(udata->f);
1188
97
    assert(udata->cont_msg_info);
1189
1190
    /* Increase chunk array size, if necessary */
1191
97
    if (oh->nchunks >= oh->alloc_nchunks) {
1192
77
        size_t       na = MAX(H5O_NCHUNKS, oh->alloc_nchunks * 2); /* Double # of chunks allocated */
1193
77
        H5O_chunk_t *x;
1194
1195
77
        if (NULL == (x = H5FL_SEQ_REALLOC(H5O_chunk_t, oh->chunk, na)))
1196
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "memory allocation failed");
1197
77
        oh->alloc_nchunks = na;
1198
77
        oh->chunk         = x;
1199
77
    }
1200
1201
    /* Init the chunk data info */
1202
97
    chunkno                 = (unsigned)oh->nchunks++;
1203
97
    oh->chunk[chunkno].gap  = 0;
1204
97
    oh->chunk[chunkno].addr = addr;
1205
97
    if (chunkno == 0)
1206
        /* First chunk's 'image' includes room for the object header prefix */
1207
59
        oh->chunk[0].size = chunk_size + (size_t)H5O_SIZEOF_HDR(oh);
1208
38
    else
1209
38
        oh->chunk[chunkno].size = chunk_size;
1210
97
    if (NULL == (oh->chunk[chunkno].image = H5FL_BLK_MALLOC(chunk_image, oh->chunk[chunkno].size)))
1211
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "memory allocation failed");
1212
97
    oh->chunk[chunkno].chunk_proxy = NULL;
1213
1214
    /* Copy disk image into chunk's image */
1215
97
    if (len < oh->chunk[chunkno].size)
1216
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTCOPY, FAIL, "attempted to copy too many disk image bytes into buffer");
1217
97
    H5MM_memcpy(oh->chunk[chunkno].image, image, oh->chunk[chunkno].size);
1218
1219
    /* Point into chunk image to decode */
1220
97
    chunk_image = oh->chunk[chunkno].image;
1221
97
    p_end       = chunk_image + oh->chunk[chunkno].size - 1;
1222
1223
    /* Skip over [already decoded] prefix in special case of chunk 0 */
1224
97
    if (chunkno == 0) {
1225
59
        size_t skip = (size_t)(H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh));
1226
1227
59
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, skip, p_end))
1228
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1229
59
        chunk_image += skip;
1230
59
    }
1231
1232
    /* Check for magic # on chunks > 0 in later versions of the format */
1233
38
    else if (chunkno > 0 && oh->version > H5O_VERSION_1) {
1234
        /* Magic number */
1235
0
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, H5_SIZEOF_MAGIC, p_end))
1236
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1237
0
        if (memcmp(chunk_image, H5O_CHK_MAGIC, H5_SIZEOF_MAGIC) != 0)
1238
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "wrong object header chunk signature");
1239
0
        chunk_image += H5_SIZEOF_MAGIC;
1240
0
    }
1241
1242
    /* Decode messages from this chunk */
1243
97
    eom_ptr = oh->chunk[chunkno].image + (oh->chunk[chunkno].size - H5O_SIZEOF_CHKSUM_OH(oh));
1244
97
    nullcnt = 0;
1245
1246
14.8k
    while (chunk_image < eom_ptr) {
1247
14.7k
        size_t            mesg_size;   /* Size of message read in */
1248
14.7k
        unsigned          id;          /* ID (type) of current message */
1249
14.7k
        uint8_t           flags;       /* Flags for current message */
1250
14.7k
        H5O_msg_crt_idx_t crt_idx = 0; /* Creation index for current message */
1251
1252
        /* Decode message prefix info */
1253
1254
        /* Version # */
1255
14.7k
        if (oh->version == H5O_VERSION_1) {
1256
14.7k
            if (H5_IS_BUFFER_OVERFLOW(chunk_image, 2, p_end))
1257
0
                HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1258
14.7k
            UINT16DECODE(chunk_image, id);
1259
14.7k
        }
1260
0
        else {
1261
0
            if (H5_IS_BUFFER_OVERFLOW(chunk_image, 1, p_end))
1262
0
                HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1263
0
            id = *chunk_image++;
1264
0
        }
1265
1266
        /* Message size */
1267
14.7k
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, 2, p_end))
1268
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1269
14.7k
        UINT16DECODE(chunk_image, mesg_size);
1270
14.7k
        if (mesg_size != H5O_ALIGN_OH(oh, mesg_size))
1271
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "message not aligned");
1272
1273
14.7k
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, mesg_size, p_end))
1274
1
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "message size exceeds buffer end");
1275
1276
        /* Message flags */
1277
14.7k
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, 1, p_end))
1278
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1279
14.7k
        flags = *chunk_image++;
1280
14.7k
        if (flags & ~H5O_MSG_FLAG_BITS)
1281
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "unknown flag for message");
1282
14.7k
        if ((flags & H5O_MSG_FLAG_SHARED) && (flags & H5O_MSG_FLAG_DONTSHARE))
1283
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "bad flag combination for message");
1284
14.7k
        if ((flags & H5O_MSG_FLAG_WAS_UNKNOWN) && (flags & H5O_MSG_FLAG_FAIL_IF_UNKNOWN_AND_OPEN_FOR_WRITE))
1285
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "bad flag combination for message");
1286
14.7k
        if ((flags & H5O_MSG_FLAG_WAS_UNKNOWN) && !(flags & H5O_MSG_FLAG_MARK_IF_UNKNOWN))
1287
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "bad flag combination for message");
1288
        /* Delay checking the "shareable" flag until we've made sure id
1289
         * references a valid message class that this version of the library
1290
         * knows about */
1291
1292
        /* Reserved bytes/creation index */
1293
14.7k
        if (oh->version == H5O_VERSION_1) {
1294
            /* Reserved bytes */
1295
14.7k
            if (H5_IS_BUFFER_OVERFLOW(chunk_image, 3, p_end))
1296
0
                HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1297
14.7k
            chunk_image += 3;
1298
14.7k
        }
1299
0
        else {
1300
            /* Only decode creation index if they are being tracked */
1301
0
            if (oh->flags & H5O_HDR_ATTR_CRT_ORDER_TRACKED) {
1302
0
                if (H5_IS_BUFFER_OVERFLOW(chunk_image, 2, p_end))
1303
0
                    HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1304
0
                UINT16DECODE(chunk_image, crt_idx);
1305
0
            }
1306
0
        }
1307
1308
        /* Increment count of null messages */
1309
14.7k
        if (H5O_NULL_ID == id)
1310
13.9k
            nullcnt++;
1311
1312
        /* Check for combining two adjacent 'null' messages */
1313
14.7k
        if ((udata->file_intent & H5F_ACC_RDWR) && H5O_NULL_ID == id && oh->nmesgs > 0 &&
1314
13.9k
            H5O_NULL_ID == oh->mesg[oh->nmesgs - 1].type->id && oh->mesg[oh->nmesgs - 1].chunkno == chunkno) {
1315
1316
13.6k
            size_t mesgno; /* Current message to operate on */
1317
1318
            /* Combine adjacent null messages */
1319
13.6k
            mesgno = oh->nmesgs - 1;
1320
13.6k
            oh->mesg[mesgno].raw_size += (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + mesg_size;
1321
13.6k
            oh->mesg[mesgno].dirty = true;
1322
13.6k
            merged_null_msgs++;
1323
13.6k
        }
1324
1.11k
        else {
1325
1.11k
            H5O_mesg_t *mesg;        /* Pointer to new message */
1326
1.11k
            unsigned    ioflags = 0; /* Flags for decode routine */
1327
1328
            /* Check if we need to extend message table to hold the new message */
1329
1.11k
            if (oh->nmesgs >= oh->alloc_nmesgs)
1330
203
                if (H5O__alloc_msgs(oh, (size_t)1) < 0)
1331
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "can't allocate more space for messages");
1332
1333
            /* Get pointer to message to set up */
1334
1.11k
            mesg = &oh->mesg[oh->nmesgs];
1335
1336
            /* Increment # of messages */
1337
1.11k
            oh->nmesgs++;
1338
1339
            /* Initialize information about message */
1340
1.11k
            mesg->dirty   = false;
1341
1.11k
            mesg->flags   = flags;
1342
1.11k
            mesg->crt_idx = crt_idx;
1343
1.11k
            mesg->native  = NULL;
1344
1.11k
            H5_WARN_CAST_AWAY_CONST_OFF
1345
1.11k
            mesg->raw = (uint8_t *)chunk_image;
1346
1.11k
            H5_WARN_CAST_AWAY_CONST_ON
1347
1.11k
            mesg->raw_size = mesg_size;
1348
1.11k
            mesg->chunkno  = chunkno;
1349
1350
            /* Point unknown messages at 'unknown' message class */
1351
            /* (Usually from future versions of the library) */
1352
1.11k
            if (id >= H5O_UNKNOWN_ID ||
1353
#ifdef H5O_ENABLE_BOGUS
1354
                id == H5O_BOGUS_VALID_ID ||
1355
#endif
1356
1.11k
                NULL == H5O_msg_class_g[id]) {
1357
1358
611
                H5O_unknown_t *unknown; /* Pointer to "unknown" message info */
1359
1360
                /* Allocate "unknown" message info */
1361
611
                if (NULL == (unknown = H5FL_MALLOC(H5O_unknown_t)))
1362
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "memory allocation failed");
1363
1364
                /* Save the original message type ID */
1365
611
                *unknown = id;
1366
1367
                /* Save 'native' form of unknown message */
1368
611
                mesg->native = unknown;
1369
1370
                /* Set message to "unknown" class */
1371
611
                mesg->type = H5O_msg_class_g[H5O_UNKNOWN_ID];
1372
1373
                /* Check for "fail if unknown" message flags */
1374
611
                if (((udata->file_intent & H5F_ACC_RDWR) &&
1375
611
                     (flags & H5O_MSG_FLAG_FAIL_IF_UNKNOWN_AND_OPEN_FOR_WRITE)) ||
1376
611
                    (flags & H5O_MSG_FLAG_FAIL_IF_UNKNOWN_ALWAYS))
1377
0
                    HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL,
1378
611
                                "unknown message with 'fail if unknown' flag found");
1379
                /* Check for "mark if unknown" message flag, etc. */
1380
611
                else if ((flags & H5O_MSG_FLAG_MARK_IF_UNKNOWN) && !(flags & H5O_MSG_FLAG_WAS_UNKNOWN) &&
1381
11
                         (udata->file_intent & H5F_ACC_RDWR)) {
1382
1383
                    /* Mark the message as "unknown" */
1384
                    /* This is a bit aggressive, since the application may
1385
                     * never change anything about the object (metadata or
1386
                     * raw data), but we can sort out the finer details
1387
                     * when/if we start using the flag.
1388
                     */
1389
                    /* Also, it's possible that this functionality may not
1390
                     * get invoked if the object header is brought into
1391
                     * the metadata cache in some other "weird" way, like
1392
                     * using H5Ocopy().
1393
                     */
1394
11
                    mesg->flags |= H5O_MSG_FLAG_WAS_UNKNOWN;
1395
1396
                    /* Mark the message and chunk as dirty */
1397
11
                    mesg->dirty    = true;
1398
11
                    mesgs_modified = true;
1399
11
                }
1400
611
            }
1401
500
            else {
1402
                /* Check for message of unshareable class marked as "shareable"
1403
                 */
1404
500
                if (((flags & H5O_MSG_FLAG_SHARED) || (flags & H5O_MSG_FLAG_SHAREABLE)) &&
1405
0
                    H5O_msg_class_g[id] && !(H5O_msg_class_g[id]->share_flags & H5O_SHARE_IS_SHARABLE))
1406
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL,
1407
500
                                "message of unshareable class flagged as shareable");
1408
1409
                /* Set message class for "known" messages */
1410
500
                mesg->type = H5O_msg_class_g[id];
1411
500
            }
1412
1413
            /* Do some inspection/interpretation of new messages from this chunk */
1414
            /* (detect continuation messages, ref. count messages, etc.) */
1415
1416
            /* Check if message is a continuation message */
1417
1.11k
            if (H5O_CONT_ID == id) {
1418
77
                H5O_cont_t *cont;
1419
1420
                /* Decode continuation message */
1421
77
                if (NULL == (cont = (H5O_cont_t *)(H5O_MSG_CONT->decode)(udata->f, NULL, 0, &ioflags,
1422
77
                                                                         mesg->raw_size, mesg->raw)))
1423
0
                    HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "bad continuation message found");
1424
77
                H5_CHECKED_ASSIGN(cont->chunkno, unsigned, udata->cont_msg_info->nmsgs + 1,
1425
77
                                  size_t); /* the next continuation message/chunk */
1426
1427
                /* Save 'native' form of continuation message */
1428
77
                mesg->native = cont;
1429
1430
                /* Add to continuation messages left to interpret */
1431
77
                if (H5O__add_cont_msg(udata->cont_msg_info, cont) < 0)
1432
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL, "can't add continuation message");
1433
77
            }
1434
            /* Check if message is a ref. count message */
1435
1.03k
            else if (H5O_REFCOUNT_ID == id) {
1436
0
                H5O_refcount_t *refcount;
1437
1438
                /* Decode ref. count message */
1439
0
                if (oh->version <= H5O_VERSION_1)
1440
0
                    HGOTO_ERROR(H5E_OHDR, H5E_VERSION, FAIL,
1441
0
                                "object header version does not support reference count message");
1442
0
                refcount = (H5O_refcount_t *)(H5O_MSG_REFCOUNT->decode)(udata->f, NULL, 0, &ioflags,
1443
0
                                                                        mesg->raw_size, mesg->raw);
1444
1445
                /* Save 'native' form of ref. count message */
1446
0
                mesg->native = refcount;
1447
1448
                /* Set object header values */
1449
0
                oh->has_refcount_msg = true;
1450
0
                if (!refcount)
1451
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL, "can't decode refcount");
1452
0
                oh->nlink = *refcount;
1453
0
            }
1454
            /* Check if message is an old mtime message */
1455
1.03k
            else if (H5O_MTIME_ID == id) {
1456
0
                time_t *mtime = NULL;
1457
1458
                /* Decode mtime message */
1459
0
                mtime =
1460
0
                    (time_t *)(H5O_MSG_MTIME->decode)(udata->f, NULL, 0, &ioflags, mesg->raw_size, mesg->raw);
1461
1462
                /* Save the decoded old format mtime */
1463
0
                if (!mtime)
1464
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, FAIL, "can't decode old format mtime");
1465
1466
                /* Save 'native' form of mtime message and its value */
1467
0
                mesg->native = mtime;
1468
0
                oh->ctime    = *mtime;
1469
0
            }
1470
            /* Check if message is an new mtime message */
1471
1.03k
            else if (H5O_MTIME_NEW_ID == id) {
1472
0
                time_t *mtime = NULL;
1473
1474
                /* Decode mtime message */
1475
0
                mtime = (time_t *)(H5O_MSG_MTIME_NEW->decode)(udata->f, NULL, 0, &ioflags, mesg->raw_size,
1476
0
                                                              mesg->raw);
1477
1478
                /* Save the decoded new format mtime */
1479
0
                if (!mtime)
1480
0
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, FAIL, "can't decode new format mtime");
1481
1482
                /* Save 'native' form of mtime message and its value */
1483
0
                mesg->native = mtime;
1484
0
                oh->ctime    = *mtime;
1485
0
            }
1486
            /* Check if message is a link message */
1487
1.03k
            else if (H5O_LINK_ID == id) {
1488
                /* Increment the count of link messages */
1489
36
                oh->link_msgs_seen++;
1490
36
            }
1491
            /* Check if message is an attribute message */
1492
998
            else if (H5O_ATTR_ID == id) {
1493
                /* Increment the count of attribute messages */
1494
0
                oh->attr_msgs_seen++;
1495
0
            }
1496
1497
            /* Mark the message & chunk as dirty if the message was changed by decoding */
1498
1.11k
            if ((ioflags & H5O_DECODEIO_DIRTY) && (udata->file_intent & H5F_ACC_RDWR)) {
1499
0
                mesg->dirty    = true;
1500
0
                mesgs_modified = true;
1501
0
            }
1502
1.11k
        }
1503
1504
        /* Advance decode pointer past message */
1505
14.7k
        chunk_image += mesg_size;
1506
1507
        /* Check for 'gap' at end of chunk */
1508
14.7k
        if ((eom_ptr - chunk_image) > 0 && (eom_ptr - chunk_image) < H5O_SIZEOF_MSGHDR_OH(oh)) {
1509
            /* Gaps can only occur in later versions of the format */
1510
0
            if (oh->version == H5O_VERSION_1)
1511
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "gap found in early version of file format");
1512
1513
            /* Gaps should only occur in chunks with no null messages */
1514
0
            if (nullcnt != 0)
1515
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "gap in chunk with no null messages");
1516
1517
            /* Set gap information for chunk */
1518
0
            oh->chunk[chunkno].gap = (size_t)(eom_ptr - chunk_image);
1519
1520
            /* Increment location in chunk */
1521
0
            chunk_image += oh->chunk[chunkno].gap;
1522
0
        }
1523
14.7k
    }
1524
1525
    /* Check for correct checksum on chunks, in later versions of the format */
1526
96
    if (oh->version > H5O_VERSION_1) {
1527
0
        uint32_t stored_chksum; /* Checksum from file */
1528
1529
        /* checksum verification already done in verify_chksum cb */
1530
1531
        /* Metadata checksum */
1532
0
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, 4, p_end))
1533
0
            HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
1534
0
        UINT32DECODE(chunk_image, stored_chksum);
1535
0
    }
1536
1537
    /* Size check */
1538
96
    if (chunk_image != oh->chunk[chunkno].image + oh->chunk[chunkno].size)
1539
0
        HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "object header image size mismatch");
1540
1541
    /* Mark the chunk dirty if we've modified messages */
1542
96
    if (mesgs_modified)
1543
11
        *dirty = true;
1544
1545
    /* Mark the chunk dirty if we've merged null messages */
1546
96
    if (merged_null_msgs > 0) {
1547
42
        udata->merged_null_msgs += merged_null_msgs;
1548
42
        *dirty = true;
1549
42
    }
1550
1551
97
done:
1552
97
    if (ret_value < 0 && udata->cont_msg_info->msgs) {
1553
0
        udata->cont_msg_info->msgs        = H5FL_SEQ_FREE(H5O_cont_t, udata->cont_msg_info->msgs);
1554
0
        udata->cont_msg_info->alloc_nmsgs = 0;
1555
0
    }
1556
97
    FUNC_LEAVE_NOAPI(ret_value)
1557
97
} /* H5O__chunk_deserialize() */
1558
1559
/*-------------------------------------------------------------------------
1560
 * Function:    H5O__chunk_serialize
1561
 *
1562
 * Purpose:     Serialize a chunk for an object header
1563
 *
1564
 * Return:      SUCCEED/FAIL
1565
 *-------------------------------------------------------------------------
1566
 */
1567
static herr_t
1568
H5O__chunk_serialize(const H5F_t *f, H5O_t *oh, unsigned chunkno)
1569
54
{
1570
54
    H5O_mesg_t *curr_msg; /* Pointer to current message being operated on */
1571
54
    unsigned    u;        /* Local index variable */
1572
54
    herr_t      ret_value = SUCCEED;
1573
1574
54
    FUNC_ENTER_PACKAGE
1575
1576
54
    assert(f);
1577
54
    assert(oh);
1578
1579
    /* Encode any dirty messages in this chunk */
1580
1.71k
    for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++)
1581
1.65k
        if (curr_msg->dirty && curr_msg->chunkno == chunkno) {
1582
155
            H5_WARN_CAST_AWAY_CONST_OFF
1583
155
            if (H5O_msg_flush((H5F_t *)f, oh, curr_msg) < 0)
1584
0
                HGOTO_ERROR(H5E_OHDR, H5E_CANTENCODE, FAIL, "unable to encode object header message");
1585
155
            H5_WARN_CAST_AWAY_CONST_ON
1586
155
        }
1587
1588
    /* Sanity checks */
1589
54
    if (oh->version > H5O_VERSION_1)
1590
        /* Make certain the magic # is present */
1591
54
        assert(!memcmp(oh->chunk[chunkno].image, (chunkno == 0 ? H5O_HDR_MAGIC : H5O_CHK_MAGIC),
1592
54
                       H5_SIZEOF_MAGIC));
1593
54
    else
1594
        /* Gaps should never occur in version 1 of the format */
1595
54
        assert(oh->chunk[chunkno].gap == 0);
1596
1597
    /* Extra work, for later versions of the format */
1598
54
    if (oh->version > H5O_VERSION_1) {
1599
0
        uint32_t metadata_chksum; /* Computed metadata checksum value */
1600
0
        uint8_t *chunk_image;     /* Pointer into object header chunk */
1601
1602
        /* Check for gap in chunk & zero it out */
1603
0
        if (oh->chunk[chunkno].gap)
1604
0
            memset((oh->chunk[chunkno].image + oh->chunk[chunkno].size) -
1605
0
                       (H5O_SIZEOF_CHKSUM + oh->chunk[chunkno].gap),
1606
0
                   0, oh->chunk[chunkno].gap);
1607
1608
        /* Compute metadata checksum */
1609
0
        metadata_chksum =
1610
0
            H5_checksum_metadata(oh->chunk[chunkno].image, (oh->chunk[chunkno].size - H5O_SIZEOF_CHKSUM), 0);
1611
1612
        /* Metadata checksum */
1613
0
        chunk_image = oh->chunk[chunkno].image + (oh->chunk[chunkno].size - H5O_SIZEOF_CHKSUM);
1614
0
        UINT32ENCODE(chunk_image, metadata_chksum);
1615
0
    }
1616
1617
54
done:
1618
54
    FUNC_LEAVE_NOAPI(ret_value)
1619
54
} /* H5O__chunk_serialize() */