Skip to content

Commit 5a7e94a

Browse files
committed
plugins ext BUGFIX printed context align all sizes and mem
Refs #2455
1 parent be942c7 commit 5a7e94a

File tree

5 files changed

+100
-101
lines changed

5 files changed

+100
-101
lines changed

src/plugins_exts.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,13 +953,26 @@ LIBYANG_API_DECL void lyplg_ext_cfree_instance_substatements(const struct ly_ctx
953953
* compiled size
954954
*/
955955

956+
/**
957+
* @brief Alignment of all the printed data, ensures all memory access is aligned to this number (B)
958+
*/
959+
#define LY_CTXP_MEM_ALIGN 8
960+
961+
/**
962+
* @brief Adjust data size to an aligned size to make sure the following data is aligned.
963+
*
964+
* @param[in,out] SIZE Data size that is adjusted.
965+
*/
966+
#define LY_CTXP_MEM_SIZE(SIZE) ((SIZE) + ((~(SIZE) + 1) & (LY_CTXP_MEM_ALIGN - 1)))
967+
956968
/**
957969
* @brief Callback to return the size of the custom compiled structure and substmts array. If there are none, do not
958970
* define this callback.
959971
*
960972
* @param[in] ext Compiled extension structure.
961973
* @param[in,out] addr_ht Hash table with addresses of shared structures that were already accounted for, can be added to.
962-
* @return Total size of the custom compiled structures and the substmts array;
974+
* @return Total size of the custom compiled structures and the substmts array. Always use ::LY_CTXP_MEM_SIZE when
975+
* adding to the size for correct alignment.
963976
* @return -1 on error.
964977
*/
965978
typedef int (*lyplg_ext_compiled_size_clb)(const struct lysc_ext_instance *ext, struct ly_ht *addr_ht);
@@ -984,7 +997,7 @@ LIBYANG_API_DECL int lyplg_ext_compiled_stmts_storage_size(const struct lysc_ext
984997
* @param[in] orig_ext Compiled extension structure to print.
985998
* @param[in,out] ext Printed extension structure to modify.
986999
* @param[in,out] mem Memory chunk of the size returned by ::lyplg_ext_compiled_size_clb() to print into, is moved
987-
* after all the printed data.
1000+
* after all the printed data. Always use ::LY_CTXP_MEM_SIZE when moving @p mem for correct alignment.
9881001
* @param[in,out] ptr_set Set with pointers to set to printed addresses.
9891002
* @param[in,out] addr_ht Hash table with pairs of addresses of shared structures to be printed and their printed
9901003
* addresses, can be added to.

src/plugins_exts/metadata.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ annotation_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *addr
220220
{
221221
int size = 0;
222222

223-
size += sizeof(struct lysc_ext_metadata);
223+
size += LY_CTXP_MEM_SIZE(sizeof(struct lysc_ext_metadata));
224224
size += lyplg_ext_compiled_stmts_storage_size(ext->substmts, addr_ht);
225225

226226
return size;
@@ -233,7 +233,7 @@ annotation_compiled_print(const struct lysc_ext_instance *orig_ext, struct lysc_
233233
struct lysc_ext_metadata *ann_cdata;
234234

235235
ann_cdata = ext->compiled = *mem;
236-
*mem = (char *)*mem + sizeof *ann_cdata;
236+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *ann_cdata);
237237
memset(ann_cdata, 0, sizeof *ann_cdata);
238238

239239
ext->substmts[1].storage_p = (void **)&ann_cdata->units;

src/plugins_exts/schema_mount.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ struct sprinter_tree_priv {
7979
lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__); \
8080
return LY_EINT
8181

82-
#define CTXP_MEM_SIZE(SIZE) ((SIZE) + ((~(SIZE) + 1) & (8 - 1)))
83-
8482
/**
8583
* @brief Check if given mount point is unique among its siblings
8684
*
@@ -1468,20 +1466,20 @@ schema_mount_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *ad
14681466
return 0;
14691467
}
14701468

1471-
size += CTXP_MEM_SIZE(sizeof *sm_data);
1469+
size += LY_CTXP_MEM_SIZE(sizeof *sm_data);
14721470

14731471
/* ht addr check, make sure the shared context is stored only once */
14741472
hash = lyht_hash((const char *)&sm_data->shared, sizeof sm_data->shared);
14751473
if (lyht_insert(addr_ht, &sm_data->shared, hash, NULL) == LY_EEXIST) {
14761474
return size;
14771475
}
14781476

1479-
size += CTXP_MEM_SIZE(sizeof *sm_data->shared);
1480-
size += CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
1477+
size += LY_CTXP_MEM_SIZE(sizeof *sm_data->shared);
1478+
size += LY_CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
14811479
for (i = 0; i < sm_data->shared->schema_count; ++i) {
1482-
size += CTXP_MEM_SIZE(ly_ctx_compiled_size(sm_data->shared->schemas[i].ctx));
1483-
size += CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
1484-
size += CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
1480+
size += LY_CTXP_MEM_SIZE(ly_ctx_compiled_size(sm_data->shared->schemas[i].ctx));
1481+
size += LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
1482+
size += LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
14851483
}
14861484

14871485
/* inlined contexts cannot be reused and will not be printed */
@@ -1501,7 +1499,7 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
15011499

15021500
/* sm_data */
15031501
ext->compiled = sm_data = *mem;
1504-
*mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *sm_data);
1502+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *sm_data);
15051503

15061504
pthread_mutexattr_init(&attr);
15071505
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
@@ -1517,15 +1515,15 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
15171515

15181516
/* sm_data->shared */
15191517
sm_data->shared = *mem;
1520-
*mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *sm_data->shared);
1518+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *sm_data->shared);
15211519

15221520
sm_data->shared->schema_count = orig_sm_data->shared->schema_count;
15231521
sm_data->shared->ref_count = orig_sm_data->shared->ref_count;
15241522

15251523
/* sm_data->shared->schemas */
15261524
if (orig_sm_data->shared->schemas) {
15271525
sm_data->shared->schemas = *mem;
1528-
*mem = (char *)*mem + CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
1526+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(sm_data->shared->schema_count * sizeof *sm_data->shared->schemas);
15291527

15301528
for (i = 0; i < sm_data->shared->schema_count; ++i) {
15311529
/* ctx */
@@ -1538,12 +1536,12 @@ schema_mount_compiled_print(const struct lysc_ext_instance *orig_ext, struct lys
15381536
/* mount_point */
15391537
strcpy(*mem, orig_sm_data->shared->schemas[i].mount_point);
15401538
sm_data->shared->schemas[i].mount_point = *mem;
1541-
*mem = (char *)*mem + CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
1539+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].mount_point) + 1);
15421540

15431541
/* content_id */
15441542
strcpy(*mem, orig_sm_data->shared->schemas[i].content_id);
15451543
sm_data->shared->schemas[i].content_id = *mem;
1546-
*mem = (char *)*mem + CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
1544+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(strlen(sm_data->shared->schemas[i].content_id) + 1);
15471545
}
15481546
}
15491547

src/plugins_exts/structure.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ structure_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *addr_
329329

330330
struct_cdata = ext->compiled;
331331

332-
size += sizeof *struct_cdata;
332+
size += LY_CTXP_MEM_SIZE(sizeof *struct_cdata);
333333
size += lyplg_ext_compiled_stmts_storage_size(ext->substmts, addr_ht);
334-
size += sizeof *struct_cdata->top_cont;
334+
size += LY_CTXP_MEM_SIZE(sizeof *struct_cdata->top_cont);
335335

336336
return size;
337337
}
@@ -347,12 +347,12 @@ structure_compiled_print(const struct lysc_ext_instance *orig_ext, struct lysc_e
347347

348348
/* ext structure */
349349
struct_cdata = ext->compiled = *mem;
350-
*mem = (char *)*mem + sizeof *struct_cdata;
350+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *struct_cdata);
351351
memset(struct_cdata, 0, sizeof *struct_cdata);
352352

353353
/* top_cont */
354354
struct_cdata->top_cont = *mem;
355-
*mem = (char *)*mem + sizeof *struct_cdata->top_cont;
355+
*mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *struct_cdata->top_cont);
356356
lyplg_ext_compiled_print_add_addr(addr_ht, orig_cdata->top_cont, struct_cdata->top_cont);
357357
memset(struct_cdata->top_cont, 0, sizeof *struct_cdata->top_cont);
358358

0 commit comments

Comments
 (0)