Coverage Report

Created: 2025-11-11 06:33

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
10
{
152
10
    FUNC_ENTER_PACKAGE_NOERR
153
154
10
    assert(image_len);
155
156
    /* Set the image length size */
157
10
    *image_len = H5O_SPEC_READ_SIZE;
158
159
10
    FUNC_LEAVE_NOAPI(SUCCEED)
160
10
} /* 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
10
{
173
10
    H5O_cache_ud_t *udata     = (H5O_cache_ud_t *)_udata; /* User data for callback */
174
10
    herr_t          ret_value = SUCCEED;
175
176
10
    FUNC_ENTER_PACKAGE
177
178
10
    assert(image);
179
10
    assert(udata);
180
10
    assert(actual_len);
181
10
    assert(*actual_len == image_len);
182
183
    /* Deserialize the object header prefix */
184
10
    if (H5O__prefix_deserialize((const uint8_t *)image, image_len, udata) < 0)
185
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, FAIL, "can't deserialize object header prefix");
186
187
    /* Sanity check */
188
10
    assert(udata->oh);
189
190
    /* Set the final size for the cache image */
191
10
    *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
10
    udata->oh_version = udata->oh->version;
196
197
    /* Free allocated memory: fix github issue #3970 */
198
10
    if (H5O__free(udata->oh, false) < 0)
199
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "can't destroy object header");
200
10
    udata->oh = NULL;
201
202
10
done:
203
10
    FUNC_LEAVE_NOAPI(ret_value)
204
10
} /* 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
10
{
219
10
    const uint8_t  *image     = (const uint8_t *)_image;  /* Pointer into raw data buffer */
220
10
    H5O_cache_ud_t *udata     = (H5O_cache_ud_t *)_udata; /* User data for callback */
221
10
    htri_t          ret_value = true;
222
223
10
    FUNC_ENTER_PACKAGE
224
225
10
    assert(image);
226
10
    assert(udata);
227
228
    /* There is no checksum for version 1 */
229
10
    if (udata->oh_version != H5O_VERSION_1) {
230
0
        uint32_t stored_chksum;   /* Stored metadata checksum value */
231
0
        uint32_t computed_chksum; /* Computed metadata checksum value */
232
233
        /* Get stored and computed checksums */
234
0
        if (H5F_get_checksums(image, len, &stored_chksum, &computed_chksum) < 0)
235
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't get checksums");
236
237
0
        if (stored_chksum != computed_chksum)
238
0
            ret_value = false;
239
0
    }
240
10
    else
241
10
        assert(!(udata->common.file_intent & H5F_ACC_SWMR_WRITE));
242
243
10
done:
244
10
    FUNC_LEAVE_NOAPI(ret_value)
245
10
} /* 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
10
{
267
10
    H5O_t          *oh        = NULL;                     /* Object header read in */
268
10
    H5O_cache_ud_t *udata     = (H5O_cache_ud_t *)_udata; /* User data for callback */
269
10
    void           *ret_value = NULL;
270
271
10
    FUNC_ENTER_PACKAGE
272
273
10
    assert(image);
274
10
    assert(len > 0);
275
10
    assert(udata);
276
10
    assert(udata->common.f);
277
10
    assert(udata->common.cont_msg_info);
278
10
    assert(dirty);
279
10
    assert(udata->oh == NULL);
280
281
10
    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
10
    assert(udata->oh);
284
285
10
    oh = udata->oh;
286
287
    /* Set SWMR flag, if appropriate */
288
10
    oh->swmr_write = !!(H5F_INTENT(udata->common.f) & H5F_ACC_SWMR_WRITE);
289
290
    /* Create object header proxy if doing SWMR writes */
291
10
    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
10
    else
297
10
        oh->proxy = NULL;
298
299
    /* Parse the first chunk */
300
10
    if (H5O__chunk_deserialize(oh, udata->common.addr, udata->chunk0_size, (const uint8_t *)image, len,
301
10
                               &(udata->common), dirty) < 0)
302
0
        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
10
    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
10
    udata->made_attempt = true;
310
311
    /* Set return value */
312
10
    ret_value = oh;
313
314
10
done:
315
    /* Release the [possibly partially initialized] object header on errors */
316
10
    if (!ret_value && oh)
317
0
        if (H5O__free(oh, false) < 0)
318
0
            HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, NULL, "unable to destroy object header data");
319
320
10
    FUNC_LEAVE_NOAPI(ret_value)
321
10
} /* 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
2
{
362
2
    H5O_t   *oh = (H5O_t *)_thing; /* Object header to encode */
363
2
    uint8_t *chunk_image;          /* Pointer to object header prefix buffer */
364
2
    herr_t   ret_value = SUCCEED;
365
366
2
    FUNC_ENTER_PACKAGE
367
368
2
    assert(f);
369
2
    assert(image);
370
2
    assert(oh);
371
2
    assert(oh->cache_info.type == H5AC_OHDR);
372
2
    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
2
    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
2
    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
2
    else {
444
        /* Version */
445
2
        *chunk_image++ = oh->version;
446
447
        /* Reserved */
448
2
        *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
2
            UINT16ENCODE(chunk_image, oh->nmesgs);
457
458
        /* Link count */
459
2
        UINT32ENCODE(chunk_image, oh->nlink);
460
461
        /* First chunk size */
462
2
        UINT32ENCODE(chunk_image, (oh->chunk[0].size - (size_t)H5O_SIZEOF_HDR(oh)));
463
464
        /* Zero to alignment */
465
2
        memset(chunk_image, 0, (size_t)(H5O_SIZEOF_HDR(oh) - 12));
466
2
        chunk_image += (size_t)(H5O_SIZEOF_HDR(oh) - 12);
467
2
    }
468
469
2
    assert((size_t)(chunk_image - oh->chunk[0].image) ==
470
2
           (size_t)(H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh)));
471
472
    /* Serialize messages for this chunk */
473
2
    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
2
    H5MM_memcpy(image, oh->chunk[0].image, len);
481
482
2
done:
483
2
    FUNC_LEAVE_NOAPI(ret_value)
484
2
} /* 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
26
{
497
26
    H5O_t *oh        = (H5O_t *)_thing;
498
26
    herr_t ret_value = SUCCEED;
499
500
26
    FUNC_ENTER_PACKAGE
501
502
26
    assert(oh);
503
504
26
    switch (action) {
505
0
        case H5AC_NOTIFY_ACTION_AFTER_INSERT:
506
10
        case H5AC_NOTIFY_ACTION_AFTER_LOAD:
507
10
            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
10
            break;
516
517
10
        case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
518
4
        case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
519
            /* Do nothing */
520
4
            break;
521
522
2
        case H5AC_NOTIFY_ACTION_ENTRY_CLEANED: {
523
2
            unsigned u; /* Local index variable */
524
525
            /* Mark messages stored with the object header (i.e. messages in chunk 0) as clean */
526
14
            for (u = 0; u < oh->nmesgs; u++)
527
12
                if (oh->mesg[u].chunkno == 0)
528
12
                    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
2
        } 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
10
        case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
543
10
            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
10
            break;
549
550
10
        default:
551
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "unknown action from metadata cache");
552
26
    }
553
554
26
done:
555
26
    FUNC_LEAVE_NOAPI(ret_value)
556
26
} /* 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
10
{
569
10
    H5O_t *oh        = (H5O_t *)_thing; /* Object header to destroy */
570
10
    herr_t ret_value = SUCCEED;
571
572
10
    FUNC_ENTER_PACKAGE
573
574
10
    assert(oh);
575
10
    assert(oh->cache_info.type == H5AC_OHDR);
576
577
    /* Destroy object header */
578
10
    if (H5O__free(oh, false) < 0)
579
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "can't destroy object header");
580
581
10
done:
582
10
    FUNC_LEAVE_NOAPI(ret_value)
583
10
} /* 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
15
{
598
15
    const H5O_chk_cache_ud_t *udata = (const H5O_chk_cache_ud_t *)_udata; /* User data for callback */
599
600
15
    FUNC_ENTER_PACKAGE_NOERR
601
602
15
    assert(udata);
603
15
    assert(udata->oh);
604
15
    assert(image_len);
605
15
    assert(udata->size);
606
607
    /* Set the image length size */
608
15
    *image_len = udata->size;
609
610
15
    FUNC_LEAVE_NOAPI(SUCCEED)
611
15
} /* 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
15
{
626
15
    const uint8_t      *image     = (const uint8_t *)_image;      /* Pointer into raw data buffer */
627
15
    H5O_chk_cache_ud_t *udata     = (H5O_chk_cache_ud_t *)_udata; /* User data for callback */
628
15
    htri_t              ret_value = true;
629
630
15
    FUNC_ENTER_PACKAGE
631
632
15
    assert(image);
633
634
    /* There is no checksum for version 1 */
635
15
    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
15
done:
648
15
    FUNC_LEAVE_NOAPI(ret_value)
649
15
} /* 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
15
{
665
15
    H5O_chunk_proxy_t  *chk_proxy = NULL;                         /* Chunk proxy object */
666
15
    H5O_chk_cache_ud_t *udata     = (H5O_chk_cache_ud_t *)_udata; /* User data for callback */
667
15
    void               *ret_value = NULL;
668
669
15
    FUNC_ENTER_PACKAGE
670
671
15
    assert(image);
672
15
    assert(len > 0);
673
15
    assert(udata);
674
15
    assert(udata->oh);
675
15
    assert(dirty);
676
677
    /* Allocate space for the object header data structure */
678
15
    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
15
    if (udata->decoding) {
684
15
        assert(udata->common.f);
685
15
        assert(udata->common.cont_msg_info);
686
687
        /* Parse the chunk */
688
15
        if (H5O__chunk_deserialize(udata->oh, udata->common.addr, udata->size, (const uint8_t *)image, len,
689
15
                                   &(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
15
        H5_CHECKED_ASSIGN(chk_proxy->chunkno, unsigned, udata->oh->nchunks - 1, size_t);
694
15
    }
695
0
    else {
696
        /* Sanity check */
697
0
        assert(udata->chunkno < udata->oh->nchunks);
698
699
        /* Set the chunk number for the chunk proxy */
700
0
        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
0
        assert(0 == memcmp(image, udata->oh->chunk[chk_proxy->chunkno].image,
706
0
                           udata->oh->chunk[chk_proxy->chunkno].size));
707
0
    }
708
709
    /* Increment reference count of object header */
710
15
    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
15
    chk_proxy->oh = udata->oh;
713
714
    /* Set return value */
715
15
    ret_value = chk_proxy;
716
717
15
done:
718
15
    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
15
    FUNC_LEAVE_NOAPI(ret_value)
723
15
} /* 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
0
{
737
0
    const H5O_chunk_proxy_t *chk_proxy = (const H5O_chunk_proxy_t *)_thing; /* Chunk proxy to query */
738
739
0
    FUNC_ENTER_PACKAGE_NOERR
740
741
0
    assert(chk_proxy);
742
0
    assert(chk_proxy->cache_info.type == H5AC_OHDR_CHK);
743
0
    assert(chk_proxy->oh);
744
0
    assert(image_len);
745
746
0
    *image_len = chk_proxy->oh->chunk[chk_proxy->chunkno].size;
747
748
0
    FUNC_LEAVE_NOAPI(SUCCEED)
749
0
} /* 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
0
{
765
0
    H5O_chunk_proxy_t *chk_proxy = (H5O_chunk_proxy_t *)_thing; /* Object header chunk to serialize */
766
0
    herr_t             ret_value = SUCCEED;
767
768
0
    FUNC_ENTER_PACKAGE
769
770
0
    assert(f);
771
0
    assert(image);
772
0
    assert(chk_proxy);
773
0
    assert(chk_proxy->cache_info.type == H5AC_OHDR_CHK);
774
0
    assert(chk_proxy->oh);
775
0
    assert(chk_proxy->oh->chunk[chk_proxy->chunkno].size == len);
776
777
    /* Serialize messages for this chunk */
778
0
    if (H5O__chunk_serialize(f, chk_proxy->oh, chk_proxy->chunkno) < 0)
779
0
        HGOTO_ERROR(H5E_OHDR, H5E_CANTSERIALIZE, FAIL,
780
0
                    "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
0
    if (len > chk_proxy->oh->chunk[chk_proxy->chunkno].size)
787
0
        HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "buffer overflow detected");
788
789
0
    H5MM_memcpy(image, chk_proxy->oh->chunk[chk_proxy->chunkno].image, len);
790
791
0
done:
792
0
    FUNC_LEAVE_NOAPI(ret_value)
793
0
} /* 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
30
{
806
30
    H5O_chunk_proxy_t *chk_proxy = (H5O_chunk_proxy_t *)_thing;
807
30
    herr_t             ret_value = SUCCEED;
808
809
30
    FUNC_ENTER_PACKAGE
810
811
30
    assert(chk_proxy);
812
30
    assert(chk_proxy->oh);
813
814
30
    switch (action) {
815
0
        case H5AC_NOTIFY_ACTION_AFTER_INSERT:
816
15
        case H5AC_NOTIFY_ACTION_AFTER_LOAD:
817
15
            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
15
            break;
850
851
15
        case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
852
0
        case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
853
            /* Do nothing */
854
0
            break;
855
856
0
        case H5AC_NOTIFY_ACTION_ENTRY_CLEANED: {
857
0
            unsigned u; /* Local index variable */
858
859
            /* Mark messages in chunk as clean */
860
0
            for (u = 0; u < chk_proxy->oh->nmesgs; u++)
861
0
                if (chk_proxy->oh->mesg[u].chunkno == chk_proxy->chunkno)
862
0
                    chk_proxy->oh->mesg[u].dirty = false;
863
0
        } 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
15
        case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
873
15
            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
15
            break;
896
897
15
        default:
898
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "unknown action from metadata cache");
899
30
    }
900
901
30
done:
902
30
    FUNC_LEAVE_NOAPI(ret_value)
903
30
} /* 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
15
{
917
15
    H5O_chunk_proxy_t *chk_proxy = (H5O_chunk_proxy_t *)_thing; /* Object header chunk proxy to release */
918
15
    herr_t             ret_value = SUCCEED;
919
920
15
    FUNC_ENTER_PACKAGE
921
922
15
    assert(chk_proxy);
923
15
    assert(chk_proxy->cache_info.type == H5AC_OHDR_CHK);
924
925
    /* Destroy object header chunk proxy */
926
15
    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
15
done:
930
15
    FUNC_LEAVE_NOAPI(ret_value)
931
15
} /* 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
105
{
945
105
    size_t contno; /* Continuation message index */
946
105
    herr_t ret_value = SUCCEED;
947
948
105
    FUNC_ENTER_PACKAGE
949
950
105
    assert(cont_msg_info);
951
105
    assert(cont);
952
953
    /* Increase chunk array size, if necessary */
954
105
    if (cont_msg_info->nmsgs >= cont_msg_info->alloc_nmsgs) {
955
18
        size_t na = MAX(H5O_NCHUNKS, cont_msg_info->alloc_nmsgs * 2); /* Double # of messages allocated */
956
18
        H5O_cont_t *x;
957
958
18
        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
18
        cont_msg_info->alloc_nmsgs = na;
961
18
        cont_msg_info->msgs        = x;
962
18
    }
963
964
    /* Init the continuation message info */
965
105
    contno                              = cont_msg_info->nmsgs++;
966
105
    cont_msg_info->msgs[contno].addr    = cont->addr;
967
105
    cont_msg_info->msgs[contno].size    = cont->size;
968
105
    cont_msg_info->msgs[contno].chunkno = cont->chunkno;
969
970
105
done:
971
105
    FUNC_LEAVE_NOAPI(ret_value)
972
105
} /* 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
20
{
985
20
    const uint8_t *image     = (const uint8_t *)_image; /* Pointer into raw data buffer */
986
20
    const uint8_t *p_end     = image + len - 1;         /* End of image buffer */
987
20
    H5O_t         *oh        = NULL;                    /* Object header read in */
988
20
    herr_t         ret_value = SUCCEED;
989
990
20
    FUNC_ENTER_PACKAGE
991
992
20
    assert(image);
993
20
    assert(udata);
994
995
    /* Allocate space for the new object header data structure */
996
20
    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
20
    oh->sizeof_size = H5F_SIZEOF_SIZE(udata->common.f);
1001
20
    oh->sizeof_addr = H5F_SIZEOF_ADDR(udata->common.f);
1002
1003
    /* Check for presence of magic number */
1004
    /* (indicates version 2 or later) */
1005
20
    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
20
    if (!memcmp(image, H5O_HDR_MAGIC, (size_t)H5_SIZEOF_MAGIC)) {
1008
1009
        /* Magic number (bounds checked above) */
1010
0
        image += H5_SIZEOF_MAGIC;
1011
1012
        /* Version */
1013
0
        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
0
        oh->version = *image++;
1016
0
        if (H5O_VERSION_2 != oh->version)
1017
0
            HGOTO_ERROR(H5E_OHDR, H5E_VERSION, FAIL, "bad object header version number");
1018
1019
        /* Flags */
1020
0
        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
0
        oh->flags = *image++;
1023
0
        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
0
        oh->nlink = 1;
1028
1029
        /* Time fields */
1030
0
        if (oh->flags & H5O_HDR_STORE_TIMES) {
1031
0
            uint32_t tmp;
1032
1033
0
            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
0
            UINT32DECODE(image, tmp);
1037
0
            oh->atime = (time_t)tmp;
1038
0
            UINT32DECODE(image, tmp);
1039
0
            oh->mtime = (time_t)tmp;
1040
0
            UINT32DECODE(image, tmp);
1041
0
            oh->ctime = (time_t)tmp;
1042
0
            UINT32DECODE(image, tmp);
1043
0
            oh->btime = (time_t)tmp;
1044
0
        }
1045
0
        else
1046
0
            oh->atime = oh->mtime = oh->ctime = oh->btime = 0;
1047
1048
        /* Attribute fields */
1049
0
        if (oh->flags & H5O_HDR_ATTR_STORE_PHASE_CHANGE) {
1050
0
            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
0
            UINT16DECODE(image, oh->max_compact);
1054
0
            UINT16DECODE(image, oh->min_dense);
1055
0
            if (oh->max_compact < oh->min_dense)
1056
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad object header attribute phase change values");
1057
0
        }
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
0
        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
0
            case 2: /* 4 byte size */
1078
0
                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
0
                UINT32DECODE(image, udata->chunk0_size);
1081
0
                break;
1082
1083
0
            case 3: /* 8 byte size */
1084
0
                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
0
                UINT64DECODE(image, udata->chunk0_size);
1087
0
                break;
1088
1089
0
            default:
1090
0
                HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad size for chunk 0");
1091
0
        }
1092
0
        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
0
    }
1095
20
    else {
1096
        /* Version */
1097
20
        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
20
        oh->version = *image++;
1100
20
        if (H5O_VERSION_1 != oh->version)
1101
0
            HGOTO_ERROR(H5E_OHDR, H5E_VERSION, FAIL, "bad object header version number");
1102
1103
        /* Flags */
1104
20
        oh->flags = H5O_CRT_OHDR_FLAGS_DEF;
1105
1106
        /* Reserved */
1107
20
        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
20
        image++;
1110
1111
        /* Number of messages */
1112
20
        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
20
        UINT16DECODE(image, udata->v1_pfx_nmesgs);
1115
1116
        /* Link count */
1117
20
        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
20
        UINT32DECODE(image, oh->nlink);
1120
1121
        /* Reset unused time fields */
1122
20
        oh->atime = oh->mtime = oh->ctime = oh->btime = 0;
1123
1124
        /* Reset unused attribute fields */
1125
20
        oh->max_compact = 0;
1126
20
        oh->min_dense   = 0;
1127
1128
        /* First chunk size */
1129
20
        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
20
        UINT32DECODE(image, udata->chunk0_size);
1132
20
        if ((udata->v1_pfx_nmesgs > 0 && udata->chunk0_size < H5O_SIZEOF_MSGHDR_OH(oh)) ||
1133
20
            (udata->v1_pfx_nmesgs == 0 && udata->chunk0_size > 0))
1134
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad object header chunk size");
1135
1136
        /* Reserved, in version 1 (for 8-byte alignment padding) */
1137
20
        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
20
        image += 4;
1140
20
    }
1141
1142
    /* Verify object header prefix length */
1143
20
    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
20
    udata->oh = oh;
1148
20
    oh        = NULL;
1149
1150
20
done:
1151
    /* Release the [possibly partially initialized] object header on errors */
1152
20
    if (ret_value < 0 && oh)
1153
0
        if (H5O__free(oh, false) < 0)
1154
0
            HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "unable to destroy object header data");
1155
1156
20
    FUNC_LEAVE_NOAPI(ret_value)
1157
20
} /* 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
25
{
1171
25
    const uint8_t *chunk_image = NULL;   /* Pointer into buffer to decode */
1172
25
    const uint8_t *p_end       = NULL;   /* End of image buffer */
1173
25
    uint8_t       *eom_ptr;              /* Pointer to end of messages for a chunk */
1174
25
    unsigned       merged_null_msgs = 0; /* Number of null messages merged together */
1175
25
    unsigned       chunkno;              /* Current chunk's index */
1176
25
    unsigned       nullcnt;              /* Count of null messages (for sanity checking gaps in chunks) */
1177
25
    bool           mesgs_modified =
1178
25
        false; /* Whether any messages were modified when the object header was deserialized */
1179
25
    herr_t ret_value = SUCCEED;
1180
1181
25
    FUNC_ENTER_PACKAGE
1182
1183
25
    assert(oh);
1184
25
    assert(H5_addr_defined(addr));
1185
25
    assert(image);
1186
25
    assert(len);
1187
25
    assert(udata->f);
1188
25
    assert(udata->cont_msg_info);
1189
1190
    /* Increase chunk array size, if necessary */
1191
25
    if (oh->nchunks >= oh->alloc_nchunks) {
1192
16
        size_t       na = MAX(H5O_NCHUNKS, oh->alloc_nchunks * 2); /* Double # of chunks allocated */
1193
16
        H5O_chunk_t *x;
1194
1195
16
        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
16
        oh->alloc_nchunks = na;
1198
16
        oh->chunk         = x;
1199
16
    }
1200
1201
    /* Init the chunk data info */
1202
25
    chunkno                 = (unsigned)oh->nchunks++;
1203
25
    oh->chunk[chunkno].gap  = 0;
1204
25
    oh->chunk[chunkno].addr = addr;
1205
25
    if (chunkno == 0)
1206
        /* First chunk's 'image' includes room for the object header prefix */
1207
10
        oh->chunk[0].size = chunk_size + (size_t)H5O_SIZEOF_HDR(oh);
1208
15
    else
1209
15
        oh->chunk[chunkno].size = chunk_size;
1210
25
    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
25
    oh->chunk[chunkno].chunk_proxy = NULL;
1213
1214
    /* Copy disk image into chunk's image */
1215
25
    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
25
    H5MM_memcpy(oh->chunk[chunkno].image, image, oh->chunk[chunkno].size);
1218
1219
    /* Point into chunk image to decode */
1220
25
    chunk_image = oh->chunk[chunkno].image;
1221
25
    p_end       = chunk_image + oh->chunk[chunkno].size - 1;
1222
1223
    /* Skip over [already decoded] prefix in special case of chunk 0 */
1224
25
    if (chunkno == 0) {
1225
10
        size_t skip = (size_t)(H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh));
1226
1227
10
        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
10
        chunk_image += skip;
1230
10
    }
1231
1232
    /* Check for magic # on chunks > 0 in later versions of the format */
1233
15
    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
25
    eom_ptr = oh->chunk[chunkno].image + (oh->chunk[chunkno].size - H5O_SIZEOF_CHKSUM_OH(oh));
1244
25
    nullcnt = 0;
1245
1246
403
    while (chunk_image < eom_ptr) {
1247
378
        size_t            mesg_size;   /* Size of message read in */
1248
378
        unsigned          id;          /* ID (type) of current message */
1249
378
        uint8_t           flags;       /* Flags for current message */
1250
378
        H5O_msg_crt_idx_t crt_idx = 0; /* Creation index for current message */
1251
1252
        /* Decode message prefix info */
1253
1254
        /* Version # */
1255
378
        if (oh->version == H5O_VERSION_1) {
1256
378
            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
378
            UINT16DECODE(chunk_image, id);
1259
378
        }
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
378
        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
378
        UINT16DECODE(chunk_image, mesg_size);
1270
378
        if (mesg_size != H5O_ALIGN_OH(oh, mesg_size))
1271
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "message not aligned");
1272
1273
378
        if (H5_IS_BUFFER_OVERFLOW(chunk_image, mesg_size, p_end))
1274
0
            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "message size exceeds buffer end");
1275
1276
        /* Message flags */
1277
378
        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
378
        flags = *chunk_image++;
1280
378
        if (flags & ~H5O_MSG_FLAG_BITS)
1281
0
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "unknown flag for message");
1282
378
        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
378
        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
378
        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
378
        if (oh->version == H5O_VERSION_1) {
1294
            /* Reserved bytes */
1295
378
            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
378
            chunk_image += 3;
1298
378
        }
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
378
        if (H5O_NULL_ID == id)
1310
36
            nullcnt++;
1311
1312
        /* Check for combining two adjacent 'null' messages */
1313
378
        if ((udata->file_intent & H5F_ACC_RDWR) && H5O_NULL_ID == id && oh->nmesgs > 0 &&
1314
36
            H5O_NULL_ID == oh->mesg[oh->nmesgs - 1].type->id && oh->mesg[oh->nmesgs - 1].chunkno == chunkno) {
1315
1316
0
            size_t mesgno; /* Current message to operate on */
1317
1318
            /* Combine adjacent null messages */
1319
0
            mesgno = oh->nmesgs - 1;
1320
0
            oh->mesg[mesgno].raw_size += (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + mesg_size;
1321
0
            oh->mesg[mesgno].dirty = true;
1322
0
            merged_null_msgs++;
1323
0
        }
1324
378
        else {
1325
378
            H5O_mesg_t *mesg;        /* Pointer to new message */
1326
378
            unsigned    ioflags = 0; /* Flags for decode routine */
1327
1328
            /* Check if we need to extend message table to hold the new message */
1329
378
            if (oh->nmesgs >= oh->alloc_nmesgs)
1330
32
                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
378
            mesg = &oh->mesg[oh->nmesgs];
1335
1336
            /* Increment # of messages */
1337
378
            oh->nmesgs++;
1338
1339
            /* Initialize information about message */
1340
378
            mesg->dirty   = false;
1341
378
            mesg->flags   = flags;
1342
378
            mesg->crt_idx = crt_idx;
1343
378
            mesg->native  = NULL;
1344
378
            H5_WARN_CAST_AWAY_CONST_OFF
1345
378
            mesg->raw = (uint8_t *)chunk_image;
1346
378
            H5_WARN_CAST_AWAY_CONST_ON
1347
378
            mesg->raw_size = mesg_size;
1348
378
            mesg->chunkno  = chunkno;
1349
1350
            /* Point unknown messages at 'unknown' message class */
1351
            /* (Usually from future versions of the library) */
1352
378
            if (id >= H5O_UNKNOWN_ID ||
1353
#ifdef H5O_ENABLE_BOGUS
1354
                id == H5O_BOGUS_VALID_ID ||
1355
#endif
1356
378
                NULL == H5O_msg_class_g[id]) {
1357
1358
188
                H5O_unknown_t *unknown; /* Pointer to "unknown" message info */
1359
1360
                /* Allocate "unknown" message info */
1361
188
                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
188
                *unknown = id;
1366
1367
                /* Save 'native' form of unknown message */
1368
188
                mesg->native = unknown;
1369
1370
                /* Set message to "unknown" class */
1371
188
                mesg->type = H5O_msg_class_g[H5O_UNKNOWN_ID];
1372
1373
                /* Check for "fail if unknown" message flags */
1374
188
                if (((udata->file_intent & H5F_ACC_RDWR) &&
1375
188
                     (flags & H5O_MSG_FLAG_FAIL_IF_UNKNOWN_AND_OPEN_FOR_WRITE)) ||
1376
188
                    (flags & H5O_MSG_FLAG_FAIL_IF_UNKNOWN_ALWAYS))
1377
0
                    HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL,
1378
188
                                "unknown message with 'fail if unknown' flag found");
1379
                /* Check for "mark if unknown" message flag, etc. */
1380
188
                else if ((flags & H5O_MSG_FLAG_MARK_IF_UNKNOWN) && !(flags & H5O_MSG_FLAG_WAS_UNKNOWN) &&
1381
0
                         (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
0
                    mesg->flags |= H5O_MSG_FLAG_WAS_UNKNOWN;
1395
1396
                    /* Mark the message and chunk as dirty */
1397
0
                    mesg->dirty    = true;
1398
0
                    mesgs_modified = true;
1399
0
                }
1400
188
            }
1401
190
            else {
1402
                /* Check for message of unshareable class marked as "shareable"
1403
                 */
1404
190
                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
190
                                "message of unshareable class flagged as shareable");
1408
1409
                /* Set message class for "known" messages */
1410
190
                mesg->type = H5O_msg_class_g[id];
1411
190
            }
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
378
            if (H5O_CONT_ID == id) {
1418
105
                H5O_cont_t *cont;
1419
1420
                /* Decode continuation message */
1421
105
                if (NULL == (cont = (H5O_cont_t *)(H5O_MSG_CONT->decode)(udata->f, NULL, 0, &ioflags,
1422
105
                                                                         mesg->raw_size, mesg->raw)))
1423
0
                    HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, FAIL, "bad continuation message found");
1424
105
                H5_CHECKED_ASSIGN(cont->chunkno, unsigned, udata->cont_msg_info->nmsgs + 1,
1425
105
                                  size_t); /* the next continuation message/chunk */
1426
1427
                /* Save 'native' form of continuation message */
1428
105
                mesg->native = cont;
1429
1430
                /* Add to continuation messages left to interpret */
1431
105
                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
105
            }
1434
            /* Check if message is a ref. count message */
1435
273
            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
273
            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
273
            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
273
            else if (H5O_LINK_ID == id) {
1488
                /* Increment the count of link messages */
1489
0
                oh->link_msgs_seen++;
1490
0
            }
1491
            /* Check if message is an attribute message */
1492
273
            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
378
            if ((ioflags & H5O_DECODEIO_DIRTY) && (udata->file_intent & H5F_ACC_RDWR)) {
1499
0
                mesg->dirty    = true;
1500
0
                mesgs_modified = true;
1501
0
            }
1502
378
        }
1503
1504
        /* Advance decode pointer past message */
1505
378
        chunk_image += mesg_size;
1506
1507
        /* Check for 'gap' at end of chunk */
1508
378
        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
378
    }
1524
1525
    /* Check for correct checksum on chunks, in later versions of the format */
1526
25
    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
25
    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
25
    if (mesgs_modified)
1543
0
        *dirty = true;
1544
1545
    /* Mark the chunk dirty if we've merged null messages */
1546
25
    if (merged_null_msgs > 0) {
1547
0
        udata->merged_null_msgs += merged_null_msgs;
1548
0
        *dirty = true;
1549
0
    }
1550
1551
25
done:
1552
25
    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
25
    FUNC_LEAVE_NOAPI(ret_value)
1557
25
} /* 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
2
{
1570
2
    H5O_mesg_t *curr_msg; /* Pointer to current message being operated on */
1571
2
    unsigned    u;        /* Local index variable */
1572
2
    herr_t      ret_value = SUCCEED;
1573
1574
2
    FUNC_ENTER_PACKAGE
1575
1576
2
    assert(f);
1577
2
    assert(oh);
1578
1579
    /* Encode any dirty messages in this chunk */
1580
14
    for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++)
1581
12
        if (curr_msg->dirty && curr_msg->chunkno == chunkno) {
1582
2
            H5_WARN_CAST_AWAY_CONST_OFF
1583
2
            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
2
            H5_WARN_CAST_AWAY_CONST_ON
1586
2
        }
1587
1588
    /* Sanity checks */
1589
2
    if (oh->version > H5O_VERSION_1)
1590
        /* Make certain the magic # is present */
1591
2
        assert(!memcmp(oh->chunk[chunkno].image, (chunkno == 0 ? H5O_HDR_MAGIC : H5O_CHK_MAGIC),
1592
2
                       H5_SIZEOF_MAGIC));
1593
2
    else
1594
        /* Gaps should never occur in version 1 of the format */
1595
2
        assert(oh->chunk[chunkno].gap == 0);
1596
1597
    /* Extra work, for later versions of the format */
1598
2
    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
2
done:
1618
2
    FUNC_LEAVE_NOAPI(ret_value)
1619
2
} /* H5O__chunk_serialize() */