summaryrefslogtreecommitdiff
path: root/gc.c
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2023-11-30 14:47:09 -0500
committerPeter Zhu <peter@peterzhu.ca>2023-12-01 15:04:31 -0500
commit80ea7fbad871b2222b16af1bd9f39e1f1828bbd4 (patch)
treea2cb0155f9ad11b4a8f852efffffa20390e6060c /gc.c
parent0ed55bf09713299201caec3ba9d9ef7ba6e8526f (diff)
Pin embedded shared strings
Embedded shared strings cannot be moved because strings point into the slot of the shared string. There may be code using the RSTRING_PTR on the stack, which would pin the string but not pin the shared string, causing it to move.
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/gc.c b/gc.c
index dc9974be22..9282f8a136 100644
--- a/gc.c
+++ b/gc.c
@@ -7346,7 +7346,16 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
case T_STRING:
if (STR_SHARED_P(obj)) {
- gc_mark(objspace, any->as.string.as.heap.aux.shared);
+ if (STR_EMBED_P(any->as.string.as.heap.aux.shared)) {
+ /* Embedded shared strings cannot be moved because this string
+ * points into the slot of the shared string. There may be code
+ * using the RSTRING_PTR on the stack, which would pin this
+ * string but not pin the shared string, causing it to move. */
+ gc_mark_and_pin(objspace, any->as.string.as.heap.aux.shared);
+ }
+ else {
+ gc_mark(objspace, any->as.string.as.heap.aux.shared);
+ }
}
break;
@@ -10701,7 +10710,6 @@ gc_update_object_references(rb_objspace_t *objspace, VALUE obj)
VALUE old_root = any->as.string.as.heap.aux.shared;
UPDATE_IF_MOVED(objspace, any->as.string.as.heap.aux.shared);
VALUE new_root = any->as.string.as.heap.aux.shared;
- rb_str_update_shared_ary(obj, old_root, new_root);
}
/* If, after move the string is not embedded, and can fit in the