]> git.kaiwu.me - njs.git/commitdiff
Buffer: fix use-after-free on detach during argument coercion
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Wed, 12 Aug 2026 15:07:30 +0000 (20:37 +0530)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 14 Nov 2026 00:04:03 +0000 (16:04 -0800)
Several Buffer.prototype methods fetched the data pointer of the
underlying ArrayBuffer before converting their offset, length or value
arguments.  The conversion may call user code (valueOf) which can
detach the buffer, for example with ArrayBuffer.prototype.transfer().
The bounds check and the memory access then used the stale pointer.

In QuickJS this affected readInt/readFloat, writeInt/writeFloat,
write(), toString(), indexOf(), compare() and copy().  In njs it
affected the range helper behind compare() and copy().

Now all arguments are converted first, and the buffer is fetched and
checked right before it is used, so a detach during the conversion is
reported as an ordinary "detached buffer" error.  The order in which
the arguments are converted is preserved.

In njs, copy() now makes the target writable before computing the
target range.  A target sharing external data is copied on write, so
pointers computed earlier pointed to the old data and the copy was
lost.

Similarly, toJSON() in QuickJS stored the bytes with
JS_SetPropertyUint32(), which may call a setter inherited from
Array.prototype and detach the buffer during the loop.  The elements
are now defined directly on the new array.

src/njs_buffer.c
src/qjs_buffer.c
src/test/njs_unit_test.c
test/buffer.t.js

index 5bdab1a7c49d318e8bb3f19db695c6788c5ec4cd..39448741130c76d39c88c5a317c27a6c584a0539 100644 (file)
@@ -674,41 +674,36 @@ njs_value_buffer_get(njs_vm_t *vm, njs_value_t *value, njs_str_t *dst)
 }
 
 
+static njs_int_t
+njs_buffer_range_arg(njs_vm_t *vm, const njs_value_t *value, uint64_t *num)
+{
+    if (!njs_is_defined(value)) {
+        return NJS_OK;
+    }
+
+    return njs_value_to_index(vm, njs_value_arg(value), num);
+}
+
+
 static njs_int_t
 njs_buffer_array_range(njs_vm_t *vm, njs_typed_array_t *array,
-    const njs_value_t *start, const njs_value_t *end, const char *name,
+    uint64_t num_start, uint64_t num_end, const char *name,
     uint8_t **out_start, uint8_t **out_end)
 {
-    uint64_t            num_start, num_end;
-    njs_int_t           ret;
     njs_array_buffer_t  *buffer;
 
-    num_start = 0;
-
     if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
         njs_type_error(vm, "detached buffer");
         return NJS_ERROR;
     }
 
-    if (njs_is_defined(start)) {
-        ret = njs_value_to_index(vm, njs_value_arg(start), &num_start);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
     if (num_start > array->byte_length) {
         njs_range_error(vm, "\"%sStart\" is out of range: %L", name, num_start);
         return NJS_ERROR;
     }
 
-    num_end = array->byte_length;
-
-    if (njs_is_defined(end)) {
-        ret = njs_value_to_index(vm, njs_value_arg(end), &num_end);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
+    if (num_end == UINT64_MAX) {
+        num_end = array->byte_length;
     }
 
     if (num_end > array->byte_length) {
@@ -736,6 +731,8 @@ njs_buffer_compare_array(njs_vm_t *vm, njs_value_t *val1, njs_value_t *val2,
 {
     size_t             size, src_size, trg_size;
     uint8_t            *src, *src_end, *trg, *trg_end;
+    uint64_t           num_target_start, num_target_end, num_source_start,
+                       num_source_end;
     njs_int_t          ret;
     njs_typed_array_t  *source, *target;
 
@@ -749,14 +746,39 @@ njs_buffer_compare_array(njs_vm_t *vm, njs_value_t *val1, njs_value_t *val2,
         return NJS_ERROR;
     }
 
-    ret = njs_buffer_array_range(vm, target, target_start, target_end, "target",
-                                 &trg, &trg_end);
+    num_target_start = 0;
+    num_target_end = UINT64_MAX;
+    num_source_start = 0;
+    num_source_end = UINT64_MAX;
+
+    ret = njs_buffer_range_arg(vm, target_start, &num_target_start);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    ret = njs_buffer_range_arg(vm, target_end, &num_target_end);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
 
-    ret = njs_buffer_array_range(vm, source, source_start, source_end, "source",
-                                 &src, &src_end);
+    ret = njs_buffer_range_arg(vm, source_start, &num_source_start);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    ret = njs_buffer_range_arg(vm, source_end, &num_source_end);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    ret = njs_buffer_array_range(vm, target, num_target_start, num_target_end,
+                                 "target", &trg, &trg_end);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    ret = njs_buffer_array_range(vm, source, num_source_start, num_source_end,
+                                 "source", &src, &src_end);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
@@ -2022,6 +2044,7 @@ njs_buffer_prototype_copy(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 {
     size_t              size;
     uint8_t             *src, *src_end, *trg, *trg_end;
+    uint64_t            num_target_start, num_source_start, num_source_end;
     njs_int_t           ret;
     njs_value_t         *val1, *val2;
     njs_typed_array_t   *source, *target;
@@ -2040,16 +2063,21 @@ njs_buffer_prototype_copy(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         return NJS_ERROR;
     }
 
-    ret = njs_buffer_array_range(vm, target, njs_arg(args, nargs, 2),
-                                 &njs_value_undefined, "target", &trg,
-                                 &trg_end);
+    num_target_start = 0;
+    num_source_start = 0;
+    num_source_end = UINT64_MAX;
+
+    ret = njs_buffer_range_arg(vm, njs_arg(args, nargs, 2), &num_target_start);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    ret = njs_buffer_range_arg(vm, njs_arg(args, nargs, 3), &num_source_start);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
 
-    ret = njs_buffer_array_range(vm, source, njs_arg(args, nargs, 3),
-                                 njs_arg(args, nargs, 4), "source", &src,
-                                 &src_end);
+    ret = njs_buffer_range_arg(vm, njs_arg(args, nargs, 4), &num_source_end);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
@@ -2059,6 +2087,18 @@ njs_buffer_prototype_copy(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         return NJS_ERROR;
     }
 
+    ret = njs_buffer_array_range(vm, target, num_target_start, UINT64_MAX,
+                                 "target", &trg, &trg_end);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
+    ret = njs_buffer_array_range(vm, source, num_source_start, num_source_end,
+                                 "source", &src, &src_end);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
     size = njs_min(trg_end - trg, src_end - src);
 
     if (!njs_memory_overlaps(trg, size, src, size)) {
index c21e835bf6b62e6e19596d7ba7771b459a397080..79175e4019298c195217de90338187245bda327e 100644 (file)
@@ -731,31 +731,28 @@ qjs_buffer_is_encoding(JSContext *ctx, JSValueConst this_val,
 }
 
 
-static JSValue
-qjs_buffer_array_range(JSContext *ctx, njs_str_t *array, JSValueConst start,
-    JSValueConst end, const char *name)
+static int
+qjs_buffer_range_arg(JSContext *ctx, JSValueConst value, int64_t *num)
 {
-    int64_t  num_start, num_end;
+    if (JS_IsUndefined(value)) {
+        return 0;
+    }
 
-    num_start = 0;
+    return JS_ToInt64(ctx, num, value);
+}
 
-    if (!JS_IsUndefined(start)) {
-        if (JS_ToInt64(ctx, &num_start, start)) {
-            return JS_EXCEPTION;
-        }
-    }
 
+static JSValue
+qjs_buffer_array_range(JSContext *ctx, njs_str_t *array, int64_t num_start,
+    int64_t num_end, const char *name)
+{
     if (num_start < 0 || (size_t) num_start > array->length) {
         return JS_ThrowRangeError(ctx, "\"%sStart\" is out of range: %" PRId64,
                                   name, num_start);
     }
 
-    num_end = array->length;
-
-    if (!JS_IsUndefined(end)) {
-        if (JS_ToInt64(ctx, &num_end, end)) {
-            return JS_EXCEPTION;
-        }
+    if (num_end == INT64_MAX) {
+        num_end = array->length;
     }
 
     if (num_end < 0 || (size_t) num_end > array->length) {
@@ -782,8 +779,31 @@ qjs_buffer_compare_array(JSContext *ctx, JSValue val1, JSValue val2,
     int        rc;
     size_t     size;
     JSValue    ret;
+    int64_t    num_source_start, num_source_end, num_target_start,
+               num_target_end;
     njs_str_t  src, target;
 
+    num_source_start = 0;
+    num_source_end = INT64_MAX;
+    num_target_start = 0;
+    num_target_end = INT64_MAX;
+
+    if (qjs_buffer_range_arg(ctx, source_start, &num_source_start)) {
+        return JS_EXCEPTION;
+    }
+
+    if (qjs_buffer_range_arg(ctx, source_end, &num_source_end)) {
+        return JS_EXCEPTION;
+    }
+
+    if (qjs_buffer_range_arg(ctx, target_start, &num_target_start)) {
+        return JS_EXCEPTION;
+    }
+
+    if (qjs_buffer_range_arg(ctx, target_end, &num_target_end)) {
+        return JS_EXCEPTION;
+    }
+
     ret = qjs_typed_array_data(ctx, val1, &src);
     if (JS_IsException(ret)) {
         return ret;
@@ -794,13 +814,14 @@ qjs_buffer_compare_array(JSContext *ctx, JSValue val1, JSValue val2,
         return ret;
     }
 
-    ret = qjs_buffer_array_range(ctx, &src, source_start, source_end, "source");
+    ret = qjs_buffer_array_range(ctx, &src, num_source_start, num_source_end,
+                                 "source");
     if (JS_IsException(ret)) {
         return ret;
     }
 
-    ret = qjs_buffer_array_range(ctx, &target, target_start, target_end,
-                                 "target");
+    ret = qjs_buffer_array_range(ctx, &target, num_target_start,
+                                 num_target_end, "target");
     if (JS_IsException(ret)) {
         return ret;
     }
@@ -839,8 +860,25 @@ qjs_buffer_prototype_copy(JSContext *ctx, JSValueConst this_val, int argc,
 {
     size_t     size;
     JSValue    ret;
+    int64_t    num_target_start, num_source_start, num_source_end;
     njs_str_t  src, target;
 
+    num_target_start = 0;
+    num_source_start = 0;
+    num_source_end = INT64_MAX;
+
+    if (qjs_buffer_range_arg(ctx, argv[1], &num_target_start)) {
+        return JS_EXCEPTION;
+    }
+
+    if (qjs_buffer_range_arg(ctx, argv[2], &num_source_start)) {
+        return JS_EXCEPTION;
+    }
+
+    if (qjs_buffer_range_arg(ctx, argv[3], &num_source_end)) {
+        return JS_EXCEPTION;
+    }
+
     ret = qjs_typed_array_data(ctx, this_val, &src);
     if (JS_IsException(ret)) {
         return ret;
@@ -851,12 +889,14 @@ qjs_buffer_prototype_copy(JSContext *ctx, JSValueConst this_val, int argc,
         return ret;
     }
 
-    ret = qjs_buffer_array_range(ctx, &target, argv[1], JS_UNDEFINED, "target");
+    ret = qjs_buffer_array_range(ctx, &target, num_target_start, INT64_MAX,
+                                 "target");
     if (JS_IsException(ret)) {
         return ret;
     }
 
-    ret = qjs_buffer_array_range(ctx, &src, argv[2], argv[3], "source");
+    ret = qjs_buffer_array_range(ctx, &src, num_source_start, num_source_end,
+                                 "source");
     if (JS_IsException(ret)) {
         return ret;
     }
@@ -965,9 +1005,27 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,
     JSValue                      ret, buffer, encode, value;
     int64_t                      from, to, increment, length, i;
     uint32_t                     byte;
+    njs_bool_t                   has_from;
     njs_str_t                    self, str;
     const qjs_buffer_encoding_t  *encoding;
 
+    from = 0;
+    has_from = 0;
+    encode = argv[2];
+
+    if (!JS_IsUndefined(argv[1])) {
+        if (JS_IsString(argv[0]) && JS_IsString(argv[1])) {
+            encode = argv[1];
+
+        } else {
+            if (JS_ToInt64(ctx, &from, argv[1])) {
+                return JS_EXCEPTION;
+            }
+
+            has_from = 1;
+        }
+    }
+
     ret = qjs_typed_array_data(ctx, this_val, &self);
     if (JS_IsException(ret)) {
         return ret;
@@ -976,34 +1034,22 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,
     length = self.length;
 
     if (last) {
-        from = length - 1;
         to = -1;
         increment = -1;
 
     } else {
-        from = 0;
         to = length;
         increment = 1;
     }
 
-    encode = argv[2];
-
-    if (!JS_IsUndefined(argv[1])) {
-        if (JS_IsString(argv[0]) && JS_IsString(argv[1])) {
-            encode = argv[1];
-            goto encoding;
-        }
+    if (!has_from) {
+        from = last ? length - 1 : 0;
 
-        if (JS_ToInt64(ctx, &from, argv[1])) {
-            return JS_EXCEPTION;
-        }
-
-        if (from >= 0) {
-            from = njs_min(from, length);
+    } else if (from >= 0) {
+        from = njs_min(from, length);
 
-        } else {
-            from = njs_max(0, length + from);
-        }
+    } else {
+        from = njs_max(0, length + from);
     }
 
     if (JS_IsNumber(argv[0])) {
@@ -1024,8 +1070,6 @@ qjs_buffer_prototype_index_of(JSContext *ctx, JSValueConst this_val, int argc,
         return JS_NewInt32(ctx, -1);
     }
 
-encoding:
-
     buffer = JS_UNDEFINED;
     value = argv[0];
 
@@ -1097,15 +1141,15 @@ qjs_buffer_prototype_read_float(JSContext *ctx, JSValueConst this_val,
     njs_conv_f32_t  conv_f32;
     njs_conv_f64_t  conv_f64;
 
+    if (JS_ToIndex(ctx, &index, argv[0])) {
+        return JS_EXCEPTION;
+    }
+
     ret = qjs_typed_array_data(ctx, this_val, &self);
     if (JS_IsException(ret)) {
         return ret;
     }
 
-    if (JS_ToIndex(ctx, &index, argv[0])) {
-        return JS_EXCEPTION;
-    }
-
     size = magic >> 2;
 
     if (size + index > self.length) {
@@ -1159,11 +1203,6 @@ qjs_buffer_prototype_read_int(JSContext *ctx, JSValueConst this_val,
     njs_str_t   self;
     njs_bool_t  little, swap, sign;
 
-    ret = qjs_typed_array_data(ctx, this_val, &self);
-    if (JS_IsException(ret)) {
-        return ret;
-    }
-
     if (JS_ToIndex(ctx, &index, argv[0])) {
         return JS_EXCEPTION;
     }
@@ -1185,6 +1224,11 @@ qjs_buffer_prototype_read_int(JSContext *ctx, JSValueConst this_val,
         }
     }
 
+    ret = qjs_typed_array_data(ctx, this_val, &self);
+    if (JS_IsException(ret)) {
+        return ret;
+    }
+
     if (size + index > self.length) {
         return JS_ThrowRangeError(ctx, "index %" PRIu64 " is outside the bound"
                                   " of the buffer", index);
@@ -1357,7 +1401,9 @@ qjs_buffer_prototype_to_json(JSContext *ctx, JSValueConst this_val, int argc,
     }
 
     for (i = 0; i < src.length; i++) {
-        rc = JS_SetPropertyUint32(ctx, data, i, JS_NewInt32(ctx, src.start[i]));
+        rc = JS_DefinePropertyValueUint32(ctx, data, i,
+                                          JS_NewInt32(ctx, src.start[i]),
+                                          JS_PROP_C_W_E);
         if (rc == -1) {
             JS_FreeValue(ctx, obj);
             return JS_EXCEPTION;
@@ -1426,31 +1472,30 @@ qjs_buffer_prototype_to_string(JSContext *ctx, JSValueConst this_val,
     njs_str_t                    src, data;
     const qjs_buffer_encoding_t  *encoding;
 
-    ret = qjs_typed_array_data(ctx, this_val, &src);
-    if (JS_IsException(ret)) {
-        return JS_ThrowTypeError(ctx, "method toString() called on incompatible"
-                                 " object");
-    }
-
     start = 0;
-    end = src.length;
+    end = UINT64_MAX;
 
     if (!JS_IsUndefined(argv[1])) {
         if (JS_ToIndex(ctx, &start, argv[1])) {
             return JS_EXCEPTION;
         }
-
-        start = njs_min(start, src.length);
     }
 
     if (!JS_IsUndefined(argv[2])) {
         if (JS_ToIndex(ctx, &end, argv[2])) {
             return JS_EXCEPTION;
         }
+    }
 
-        end = njs_min(end, src.length);
+    ret = qjs_typed_array_data(ctx, this_val, &src);
+    if (JS_IsException(ret)) {
+        return JS_ThrowTypeError(ctx, "method toString() called on incompatible"
+                                 " object");
     }
 
+    start = njs_min(start, src.length);
+    end = njs_min(end, src.length);
+
     if (start >= end) {
         src.length = 0;
 
@@ -1503,13 +1548,8 @@ qjs_buffer_prototype_write(JSContext *ctx, JSValueConst this_val,
     const uint8_t                *p, *end, *prev;
     const qjs_buffer_encoding_t  *encoding;
 
-    ret = qjs_typed_array_data(ctx, this_val, &self);
-    if (JS_IsException(ret)) {
-        return ret;
-    }
-
     offset = 0;
-    max_length = self.length;
+    max_length = UINT64_MAX;
     encode = argv[3];
 
     if (!JS_IsUndefined(argv[1])) {
@@ -1521,8 +1561,6 @@ qjs_buffer_prototype_write(JSContext *ctx, JSValueConst this_val,
         if (JS_ToIndex(ctx, &offset, argv[1])) {
             return JS_EXCEPTION;
         }
-
-        max_length = self.length - offset;
     }
 
     if (!JS_IsUndefined(argv[2])) {
@@ -1538,6 +1576,11 @@ qjs_buffer_prototype_write(JSContext *ctx, JSValueConst this_val,
 
 write:
 
+    ret = qjs_typed_array_data(ctx, this_val, &self);
+    if (JS_IsException(ret)) {
+        return ret;
+    }
+
     encoding = qjs_buffer_encoding(ctx, encode, 1);
     if (encoding == NULL) {
         return JS_EXCEPTION;
@@ -1555,6 +1598,10 @@ write:
         return JS_ThrowRangeError(ctx, "\"offset\" is out of range");
     }
 
+    if (max_length == UINT64_MAX) {
+        max_length = self.length - offset;
+    }
+
     if (src.length == 0) {
         JS_FreeValue(ctx, buffer);
         return JS_NewInt32(ctx, 0);
@@ -1601,11 +1648,6 @@ qjs_buffer_prototype_write_int(JSContext *ctx, JSValueConst this_val,
     njs_str_t   self;
     njs_bool_t  little, swap, sign;
 
-    ret = qjs_typed_array_data(ctx, this_val, &self);
-    if (JS_IsException(ret)) {
-        return ret;
-    }
-
     if (JS_ToIndex(ctx, &index, argv[1])) {
         return JS_EXCEPTION;
     }
@@ -1623,6 +1665,15 @@ qjs_buffer_prototype_write_int(JSContext *ctx, JSValueConst this_val,
         }
     }
 
+    if (JS_ToInt64(ctx, &i64, argv[0])) {
+        return JS_EXCEPTION;
+    }
+
+    ret = qjs_typed_array_data(ctx, this_val, &self);
+    if (JS_IsException(ret)) {
+        return ret;
+    }
+
     if (size + index > self.length) {
         return JS_ThrowRangeError(ctx, "index %" PRIu64 " is outside the bound"
                                   " of the buffer", index);
@@ -1636,10 +1687,6 @@ qjs_buffer_prototype_write_int(JSContext *ctx, JSValueConst this_val,
     swap = !swap;
 #endif
 
-    if (JS_ToInt64(ctx, &i64, argv[0])) {
-        return JS_EXCEPTION;
-    }
-
     switch (size) {
     case 1:
         if (sign) {
@@ -1814,11 +1861,6 @@ qjs_buffer_prototype_write_float(JSContext *ctx, JSValueConst this_val,
     njs_conv_f32_t  conv_f32;
     njs_conv_f64_t  conv_f64;
 
-    ret = qjs_typed_array_data(ctx, this_val, &self);
-    if (JS_IsException(ret)) {
-        return ret;
-    }
-
     if (JS_ToFloat64(ctx, &v, argv[0])) {
         return JS_EXCEPTION;
     }
@@ -1827,6 +1869,11 @@ qjs_buffer_prototype_write_float(JSContext *ctx, JSValueConst this_val,
         return JS_EXCEPTION;
     }
 
+    ret = qjs_typed_array_data(ctx, this_val, &self);
+    if (JS_IsException(ret)) {
+        return ret;
+    }
+
     size = magic >> 2;
 
     if (size + index > self.length) {
index e70beff91e63eb1bf58391f467e01454b079c477..88c71ceb230e7b3bbd18f4187602d921cfd79070 100644 (file)
@@ -22130,6 +22130,10 @@ static njs_unit_test_t  njs_externals_test[] =
     { njs_str("new Uint8Array($r.buffer.sort().slice(0,3))"),
       njs_str("129,144,145") },
 
+    { njs_str("var b = $r.buffer; "
+              "[Buffer.from('@#').copy(b, 4), $r.buffer[4], b[4]]"),
+      njs_str("2,208,64") },
+
     { njs_str("var b = $r.buffer; "
               "var v = new DataView(b.buffer, b.byteOffset);"
               "v.setUint8(0, '@'.codePointAt(0));"
index 21fdedfd0cd4c67e87f0812aab628cacb6ce7bdc..6e957d45d1877b23403b4df805807242481f6964 100644 (file)
@@ -211,6 +211,44 @@ let concatRevalidate_tsuite = {
 };
 
 
+let detachCoercion_tsuite = {
+    name: "Buffer detach during argument coercion tests",
+    skip: () => (!has_buffer() || !is_detach_available()),
+    T: async (params) => {
+        let b = Buffer.alloc(16);
+        let o = Buffer.alloc(16);
+        let d = (buf, v) => ({ valueOf() { detach(buf.buffer); return v; } });
+
+        try {
+            params.run(b, o, d);
+
+        } catch (e) {
+            if (e instanceof TypeError) {
+                return 'SUCCESS';
+            }
+
+            throw e;
+        }
+
+        throw Error('detached buffer was not detected');
+    },
+
+    tests: [
+        { run: (b, o, d) => b.readUInt32LE(d(b, 0)) },
+        { run: (b, o, d) => b.readFloatLE(d(b, 0)) },
+        { run: (b, o, d) => b.writeUInt32LE(d(b, 1), 0) },
+        { run: (b, o, d) => b.writeFloatLE(1, d(b, 0)) },
+        { run: (b, o, d) => b.write('a', d(b, 0)) },
+        { run: (b, o, d) => b.toString('utf8', d(b, 0)) },
+        { run: (b, o, d) => b.indexOf(1, d(b, 0)) },
+        { run: (b, o, d) => b.compare(o, d(b, 0)) },
+        { run: (b, o, d) => b.compare(o, 0, 16, d(o, 0)) },
+        { run: (b, o, d) => b.copy(o, d(b, 0)) },
+        { run: (b, o, d) => b.copy(o, 0, d(o, 0)) },
+    ],
+};
+
+
 let compare_tsuite = {
     name: "Buffer.compare() tests",
     skip: () => (!has_buffer()),
@@ -990,7 +1028,21 @@ let toJSON_tsuite = {
     name: "Buffer.toJSON() tests",
     skip: () => (!has_buffer()),
     T: async (params) => {
-        let r = Buffer.from(params.value).toJSON();
+        let b = Buffer.from(params.value);
+
+        if (params.detach) {
+            Object.defineProperty(Array.prototype, '0', { configurable: true,
+                                  set() { detach(b.buffer); } });
+        }
+
+        let r;
+
+        try {
+            r = b.toJSON();
+
+        } finally {
+            delete Array.prototype[0];
+        }
 
         if (!compare_object(r, params.expected)) {
             throw Error(`unexpected output "${JSON.stringify(r)}" != "${JSON.stringify(params.expected)}"`);
@@ -1003,6 +1055,8 @@ let toJSON_tsuite = {
         { value: '', expected: { type: 'Buffer', data: [] } },
         { value: 'αβγ', expected: { type: 'Buffer', data: [0xCE, 0xB1, 0xCE, 0xB2, 0xCE, 0xB3] } },
         { value: new Uint8Array([0xff, 0xde, 0xba]), expected: { type: 'Buffer', data: [0xFF, 0xDE, 0xBA] } },
+        { value: 'ab', detach: true, skip: () => !is_detach_available(),
+          expected: { type: 'Buffer', data: [0x61, 0x62] } },
     ],
 };
 
@@ -1188,6 +1242,7 @@ run([
     byteLength_tsuite,
     concat_tsuite,
     concatRevalidate_tsuite,
+    detachCoercion_tsuite,
     compare_tsuite,
     comparePrototype_tsuite,
     copy_tsuite,