Skip to content

Commit 8547380

Browse files
LaszloLangorobertsipka
authored andcommitted
Fix constantness of 'jerry_merge_snapshots' function. (#2504)
'jerry_merge_snapshots' should not modify the input snapshots. Updated the related unit test. Also fixed some minor style issues. JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 2ad883e commit 8547380

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

jerry-core/api/jerry-snapshot.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ jerry_snapshot_result (const uint32_t *snapshot_p, /**< snapshot */
965965
}
966966
else
967967
{
968-
const uint8_t *literal_base_p = (uint8_t *) (snapshot_data_p + header_p->lit_table_offset);
968+
const uint8_t *literal_base_p = snapshot_data_p + header_p->lit_table_offset;
969969

970970
bytecode_p = snapshot_load_compiled_code ((const uint8_t *) bytecode_p,
971971
literal_base_p,
@@ -1055,13 +1055,13 @@ scan_snapshot_functions (const uint8_t *buffer_p, /**< snapshot buffer start */
10551055

10561056
do
10571057
{
1058-
ecma_compiled_code_t *bytecode_p = (ecma_compiled_code_t *) buffer_p;
1058+
const ecma_compiled_code_t *bytecode_p = (ecma_compiled_code_t *) buffer_p;
10591059
uint32_t code_size = ((uint32_t) bytecode_p->size) << JMEM_ALIGNMENT_LOG;
10601060

10611061
if ((bytecode_p->status_flags & CBC_CODE_FLAGS_FUNCTION)
10621062
&& !(bytecode_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
10631063
{
1064-
ecma_value_t *literal_start_p;
1064+
const ecma_value_t *literal_start_p;
10651065
uint32_t argument_end;
10661066
uint32_t const_literal_end;
10671067

@@ -1087,7 +1087,6 @@ scan_snapshot_functions (const uint8_t *buffer_p, /**< snapshot buffer start */
10871087
if ((literal_start_p[i] & ECMA_VALUE_TYPE_MASK) == ECMA_TYPE_SNAPSHOT_OFFSET)
10881088
{
10891089
ecma_value_t lit_value = ecma_snapshot_get_literal (literal_base_p, literal_start_p[i]);
1090-
literal_start_p[i] = lit_value;
10911090
ecma_save_literals_append_value (lit_value, lit_pool_p);
10921091
}
10931092
}
@@ -1103,7 +1102,6 @@ scan_snapshot_functions (const uint8_t *buffer_p, /**< snapshot buffer start */
11031102
if ((literal_start_p[i] & ECMA_VALUE_TYPE_MASK) == ECMA_TYPE_SNAPSHOT_OFFSET)
11041103
{
11051104
ecma_value_t lit_value = ecma_snapshot_get_literal (literal_base_p, literal_start_p[i]);
1106-
literal_start_p[i] = lit_value;
11071105
ecma_save_literals_append_value (lit_value, lit_pool_p);
11081106
}
11091107
}
@@ -1119,15 +1117,16 @@ scan_snapshot_functions (const uint8_t *buffer_p, /**< snapshot buffer start */
11191117
* Update all literal offsets in a snapshot data.
11201118
*/
11211119
static void
1122-
update_literal_offsets (uint8_t *buffer_p, /**< snapshot buffer start */
1120+
update_literal_offsets (uint8_t *buffer_p, /**< [in,out] snapshot buffer start */
11231121
const uint8_t *buffer_end_p, /**< snapshot buffer end */
1124-
lit_mem_to_snapshot_id_map_entry_t *lit_map_p) /**< literal map */
1122+
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< literal map */
1123+
const uint8_t *literal_base_p) /**< start of literal data */
11251124
{
11261125
JERRY_ASSERT (buffer_end_p > buffer_p);
11271126

11281127
do
11291128
{
1130-
ecma_compiled_code_t *bytecode_p = (ecma_compiled_code_t *) buffer_p;
1129+
const ecma_compiled_code_t *bytecode_p = (ecma_compiled_code_t *) buffer_p;
11311130
uint32_t code_size = ((uint32_t) bytecode_p->size) << JMEM_ALIGNMENT_LOG;
11321131

11331132
if ((bytecode_p->status_flags & CBC_CODE_FLAGS_FUNCTION)
@@ -1156,12 +1155,12 @@ update_literal_offsets (uint8_t *buffer_p, /**< snapshot buffer start */
11561155

11571156
for (uint32_t i = 0; i < const_literal_end; i++)
11581157
{
1159-
if (ecma_is_value_string (literal_start_p[i])
1160-
|| ecma_is_value_float_number (literal_start_p[i]))
1158+
if ((literal_start_p[i] & ECMA_VALUE_TYPE_MASK) == ECMA_TYPE_SNAPSHOT_OFFSET)
11611159
{
1162-
lit_mem_to_snapshot_id_map_entry_t *current_p = lit_map_p;
1160+
ecma_value_t lit_value = ecma_snapshot_get_literal (literal_base_p, literal_start_p[i]);
1161+
const lit_mem_to_snapshot_id_map_entry_t *current_p = lit_map_p;
11631162

1164-
while (current_p->literal_id != literal_start_p[i])
1163+
while (current_p->literal_id != lit_value)
11651164
{
11661165
current_p++;
11671166
}
@@ -1178,11 +1177,12 @@ update_literal_offsets (uint8_t *buffer_p, /**< snapshot buffer start */
11781177

11791178
for (uint32_t i = 0; i < argument_end; i++)
11801179
{
1181-
if (literal_start_p[i] != ECMA_VALUE_EMPTY)
1180+
if ((literal_start_p[i] & ECMA_VALUE_TYPE_MASK) == ECMA_TYPE_SNAPSHOT_OFFSET)
11821181
{
1183-
lit_mem_to_snapshot_id_map_entry_t *current_p = lit_map_p;
1182+
ecma_value_t lit_value = ecma_snapshot_get_literal (literal_base_p, literal_start_p[i]);
1183+
const lit_mem_to_snapshot_id_map_entry_t *current_p = lit_map_p;
11841184

1185-
while (current_p->literal_id != literal_start_p[i])
1185+
while (current_p->literal_id != lit_value)
11861186
{
11871187
current_p++;
11881188
}
@@ -1251,7 +1251,7 @@ jerry_merge_snapshots (const uint32_t **inp_buffers_p, /**< array of (pointers t
12511251

12521252
uint32_t start_offset = header_p->func_offsets[0];
12531253
const uint8_t *data_p = (const uint8_t *) inp_buffers_p[i];
1254-
const uint8_t *literal_base_p = (uint8_t *) (data_p + header_p->lit_table_offset);
1254+
const uint8_t *literal_base_p = data_p + header_p->lit_table_offset;
12551255

12561256
JERRY_ASSERT (header_p->number_of_funcs > 0);
12571257

@@ -1311,9 +1311,11 @@ jerry_merge_snapshots (const uint32_t **inp_buffers_p, /**< array of (pointers t
13111311
((const uint8_t *) inp_buffers_p[i]) + start_offset,
13121312
current_header_p->lit_table_offset - start_offset);
13131313

1314+
const uint8_t *literal_base_p = ((const uint8_t *) inp_buffers_p[i]) + current_header_p->lit_table_offset;
13141315
update_literal_offsets (dst_p,
13151316
dst_p + current_header_p->lit_table_offset - start_offset,
1316-
lit_map_p);
1317+
lit_map_p,
1318+
literal_base_p);
13171319

13181320
uint32_t current_offset = (uint32_t) (dst_p - (uint8_t *) out_buffer_p) - start_offset;
13191321

jerry-core/ecma/base/ecma-literal-storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ ecma_save_literals_add_compiled_code (const ecma_compiled_code_t *compiled_code_
346346
* Save literals to specified snapshot buffer.
347347
*
348348
* Note:
349-
* Frees lit_pool_p regardless of success.
349+
* Frees 'lit_pool_p' regardless of success.
350350
*
351351
* @return true - if save was performed successfully (i.e. buffer size is sufficient),
352352
* false - otherwise

tests/unit-core/test-snapshot.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ main (void)
286286
size_t snapshot_sizes[2];
287287
static uint32_t merged_snapshot_buffer[SNAPSHOT_BUFFER_SIZE];
288288

289-
const char *code_to_snapshot_p = "123";
289+
const char *code_to_snapshot_p = "var a = 'hello'; 123";
290290

291291
jerry_init (JERRY_INIT_EMPTY);
292292
jerry_value_t generate_result;
@@ -305,7 +305,7 @@ main (void)
305305

306306
jerry_cleanup ();
307307

308-
code_to_snapshot_p = "456";
308+
code_to_snapshot_p = "var b = 'hello'; 456";
309309

310310
jerry_init (JERRY_INIT_EMPTY);
311311
generate_result = jerry_generate_snapshot (NULL,
@@ -331,6 +331,12 @@ main (void)
331331
snapshot_buffers[0] = snapshot_buffer_0;
332332
snapshot_buffers[1] = snapshot_buffer_1;
333333

334+
static uint32_t snapshot_buffer_0_bck[SNAPSHOT_BUFFER_SIZE];
335+
static uint32_t snapshot_buffer_1_bck[SNAPSHOT_BUFFER_SIZE];
336+
337+
memcpy (snapshot_buffer_0_bck, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE);
338+
memcpy (snapshot_buffer_1_bck, snapshot_buffer_1, SNAPSHOT_BUFFER_SIZE);
339+
334340
size_t merged_size = jerry_merge_snapshots (snapshot_buffers,
335341
snapshot_sizes,
336342
2,
@@ -340,6 +346,8 @@ main (void)
340346

341347
jerry_cleanup ();
342348

349+
TEST_ASSERT (0 == memcmp (snapshot_buffer_0_bck, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE));
350+
TEST_ASSERT (0 == memcmp (snapshot_buffer_1_bck, snapshot_buffer_1, SNAPSHOT_BUFFER_SIZE));
343351

344352
jerry_init (JERRY_INIT_EMPTY);
345353

0 commit comments

Comments
 (0)