Skip to content

Commit

Permalink
Assertions on if the arraybuffer has been detached
Browse files Browse the repository at this point in the history
JerryScript-DCO-1.0-Signed-off-by: legendecas [email protected]
  • Loading branch information
legendecas committed Oct 12, 2019
1 parent 923bc54 commit 65d48be
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ ecma_builtin_arraybuffer_prototype_bytelength_getter (ecma_value_t this_arg) /**

if (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
{
if (ecma_arraybuffer_is_detached (object_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}
ecma_length_t len = ecma_arraybuffer_get_length (object_p);

return ecma_make_uint32_value (len);
Expand Down Expand Up @@ -96,6 +100,11 @@ ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< thi
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an ArrayBuffer object."));
}

if (ecma_arraybuffer_is_detached (object_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_length_t len = ecma_arraybuffer_get_length (object_p);

ecma_length_t start = 0, end = len;
Expand Down
11 changes: 11 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-dataview-prototype.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

#include "ecma-arraybuffer-object.h"
#include "ecma-exceptions.h"
#include "ecma-dataview-object.h"
#include "ecma-gc.h"

Expand Down Expand Up @@ -110,11 +112,20 @@ ecma_builtin_dataview_prototype_object_getters (ecma_value_t this_arg, /**< this
}
case ECMA_DATAVIEW_PROTOTYPE_BYTE_LENGTH_GETTER:
{
if (ecma_arraybuffer_is_detached (obj_p->buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}
return ecma_make_uint32_value (obj_p->header.u.class_prop.u.length);
}
default:
{
JERRY_ASSERT (builtin_routine_id == ECMA_DATAVIEW_PROTOTYPE_BYTE_OFFSET_GETTER);

if (ecma_arraybuffer_is_detached (obj_p->buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}
return ecma_make_uint32_value (obj_p->byte_offset);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ ecma_builtin_typedarray_prototype_exec_routine (ecma_value_t this_arg, /**< this

ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_typedarray_info_t info = ecma_typedarray_get_info (obj_p);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (obj_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_getter_fn_t typedarray_getter_cb = ecma_get_typedarray_getter_fn (info.id);

Expand Down Expand Up @@ -435,6 +440,12 @@ ecma_builtin_typedarray_prototype_map (ecma_value_t this_arg, /**< this argument
}

ecma_object_t *src_obj_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (src_obj_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t src_info = ecma_typedarray_get_info (src_obj_p);

ecma_object_t *func_object_p = ecma_get_object_from_value (cb_func_val);
Expand Down Expand Up @@ -517,6 +528,12 @@ ecma_builtin_typedarray_prototype_reduce_with_direction (ecma_value_t this_arg,
}

ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (obj_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t info = ecma_typedarray_get_info (obj_p);

ecma_typedarray_getter_fn_t getter_cb = ecma_get_typedarray_getter_fn (info.id);
Expand Down Expand Up @@ -770,6 +787,12 @@ ecma_builtin_typedarray_prototype_reverse (ecma_value_t this_arg) /**< this argu
}

ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (obj_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t info = ecma_typedarray_get_info (obj_p);

uint32_t middle = (info.length / 2) << info.shift;
Expand Down Expand Up @@ -822,9 +845,21 @@ ecma_op_typedarray_set_with_typedarray (ecma_value_t this_arg, /**< this argumen
}

ecma_object_t *target_typedarray_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (target_typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t target_info = ecma_typedarray_get_info (target_typedarray_p);

ecma_object_t *src_typedarray_p = ecma_get_object_from_value (arr_val);
ecma_object_t *src_arraybuffer_p = ecma_typedarray_get_arraybuffer (src_typedarray_p);
if (ecma_arraybuffer_is_detached (src_arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t src_info = ecma_typedarray_get_info (src_typedarray_p);

uint32_t target_offset_uint32 = ecma_number_to_uint32 (target_offset_num);
Expand Down Expand Up @@ -911,6 +946,12 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument

/* 11. ~ 15. */
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t target_info = ecma_typedarray_get_info (typedarray_p);

/* 16.~ 17. */
Expand Down Expand Up @@ -1476,6 +1517,11 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
}

ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}
ecma_typedarray_info_t info = ecma_typedarray_get_info (typedarray_p);

if (!info.length)
Expand Down Expand Up @@ -1577,6 +1623,11 @@ ecma_builtin_typedarray_prototype_find_helper (ecma_value_t this_arg, /**< this

ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
ecma_typedarray_info_t info = ecma_typedarray_get_info (typedarray_p);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

uint32_t buffer_index = 0;
uint32_t limit = info.length * info.element_size;
Expand Down Expand Up @@ -1678,6 +1729,11 @@ ecma_builtin_typedarray_prototype_index_helper (ecma_value_t this_arg, /**< this

ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
ecma_typedarray_info_t info = ecma_typedarray_get_info (typedarray_p);
if (ecma_arraybuffer_is_detached (info.array_buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

uint32_t limit = info.length * info.element_size;
uint32_t from_index;

Expand Down Expand Up @@ -1901,6 +1957,12 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
}

ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

ecma_typedarray_info_t info = ecma_typedarray_get_info (typedarray_p);
uint32_t start = 0;
uint32_t end = info.length;
Expand Down Expand Up @@ -1944,6 +2006,7 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume
if (count > 0)
{
ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);

lit_utf8_byte_t *new_typedarray_buffer_p = ecma_typedarray_get_buffer (new_typedarray_p);
uint32_t src_byte_index = (start * info.element_size);

Expand Down
26 changes: 26 additions & 0 deletions jerry-core/ecma/operations/ecma-arraybuffer-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,32 @@ ecma_arraybuffer_get_buffer (ecma_object_t *object_p) /**< pointer to the ArrayB
}
} /* ecma_arraybuffer_get_buffer */

/**
* Helper function: check if the target ArrayBuffer is detached
*
*
* @return true - if value is an detached ArrayBuffer object
* false - otherwise
*/
inline bool JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
ecma_arraybuffer_is_detached (ecma_object_t *object_p) /**< pointer to the ArrayBuffer object */
{
JERRY_ASSERT (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));

ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;

if (ECMA_ARRAYBUFFER_HAS_EXTERNAL_MEMORY (ext_object_p))
{
ecma_arraybuffer_external_info *array_p = (ecma_arraybuffer_external_info *) ext_object_p;
/* in case the arraybuffer has been detached */
return array_p->buffer_p == NULL;
}
else
{
return false;
}
} /* ecma_arraybuffer_is_detached */

/**
* Helper function: check if the target ArrayBuffer is detachable
*
Expand Down
2 changes: 2 additions & 0 deletions jerry-core/ecma/operations/ecma-arraybuffer-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ ecma_arraybuffer_get_buffer (ecma_object_t *obj_p);
ecma_length_t JERRY_ATTR_PURE
ecma_arraybuffer_get_length (ecma_object_t *obj_p);
bool JERRY_ATTR_PURE
ecma_arraybuffer_is_detached (ecma_object_t *obj_p);
bool JERRY_ATTR_PURE
ecma_arraybuffer_is_detachable (ecma_object_t *obj_p);
bool
ecma_arraybuffer_detach (ecma_object_t *obj_p);
Expand Down
10 changes: 9 additions & 1 deletion jerry-core/ecma/operations/ecma-dataview-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ ecma_op_dataview_create (const ecma_value_t *arguments_list_p, /**< arguments li
}
}

/* 8. TODO: Throw TypeError, when Detached ArrayBuffer will be supported. */
/* 8. */
if (ecma_arraybuffer_is_detached (buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

/* 9. */
ecma_length_t buffer_byte_length = ecma_arraybuffer_get_length (buffer_p);
Expand Down Expand Up @@ -284,6 +288,10 @@ ecma_op_dataview_get_set_view_value (ecma_value_t view, /**< the operation's 'vi
/* 9. */
ecma_object_t *buffer_p = view_p->buffer_p;
JERRY_ASSERT (ecma_object_class_is (buffer_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));
if (ecma_arraybuffer_is_detached (buffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}

/* 10. */
uint32_t view_offset = view_p->byte_offset;
Expand Down
26 changes: 25 additions & 1 deletion jerry-core/ecma/operations/ecma-typedarray-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ ecma_typedarray_create_object_with_buffer (ecma_object_t *arraybuffer_p, /**< th
uint8_t element_size_shift, /**< the size shift of the element length */
ecma_typedarray_type_t typedarray_id) /**< id of the typedarray */
{
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached."));
}
ecma_length_t expected_length = (ecma_arraybuffer_get_length (arraybuffer_p) >> element_size_shift);

bool needs_ext_typedarray_obj = (byte_offset != 0 || array_length != expected_length);
Expand Down Expand Up @@ -586,6 +590,11 @@ ecma_typedarray_create_object_with_typedarray (ecma_object_t *typedarray_p, /**<
ecma_typedarray_type_t typedarray_id) /**< id of the typedarray */
{
ecma_length_t array_length = ecma_typedarray_get_length (typedarray_p);
ecma_object_t *src_arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (src_arraybuffer_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Invalid detached ArrayBuffer."));
}

ecma_value_t new_typedarray = ecma_typedarray_create_object_with_length (array_length,
proto_p,
Expand All @@ -599,7 +608,6 @@ ecma_typedarray_create_object_with_typedarray (ecma_object_t *typedarray_p, /**<

ecma_object_t *new_typedarray_p = ecma_get_object_from_value (new_typedarray);

ecma_object_t *src_arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
lit_utf8_byte_t *src_buf_p = ecma_arraybuffer_get_buffer (src_arraybuffer_p);

ecma_object_t *dst_arraybuffer_p = ecma_typedarray_get_arraybuffer (new_typedarray_p);
Expand Down Expand Up @@ -825,6 +833,12 @@ ecma_typedarray_get_length (ecma_object_t *typedarray_p) /**< the pointer to the
return buffer_length >> shift;
}

ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return 0;
}

ecma_extended_typedarray_object_t *info_p = (ecma_extended_typedarray_object_t *) ext_object_p;

return info_p->array_length;
Expand All @@ -847,6 +861,12 @@ ecma_typedarray_get_offset (ecma_object_t *typedarray_p) /**< the pointer to the
return 0;
}

ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
return 0;
}

ecma_extended_typedarray_object_t *info_p = (ecma_extended_typedarray_object_t *) ext_object_p;

return info_p->byte_offset;
Expand Down Expand Up @@ -947,6 +967,10 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
{
ret = ecma_raise_range_error (ECMA_ERR_MSG ("Invalid offset."));
}
else if (ecma_arraybuffer_is_detached (arraybuffer_p))
{
ret = ecma_raise_range_error (ECMA_ERR_MSG ("Invalid detached ArrayBuffer."));
}
else
{
ecma_length_t buf_byte_length = ecma_arraybuffer_get_length (arraybuffer_p);
Expand Down

0 comments on commit 65d48be

Please sign in to comment.