Skip to content

Commit 846ce83

Browse files
committed
Remove excessive stack usage in caml_get_exception_raw_backtrace
1 parent 907fd2b commit 846ce83

1 file changed

Lines changed: 6 additions & 19 deletions

File tree

runtime/backtrace.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,34 +136,21 @@ CAMLprim value caml_get_exception_raw_backtrace(value unit)
136136
CAMLparam0();
137137
CAMLlocal1(res);
138138

139-
/* Beware: the allocations below may cause finalizers to be run, and another
140-
backtrace---possibly of a different length---to be stashed (for example
141-
if the finalizer raises then catches an exception). We choose to ignore
142-
any such finalizer backtraces and return the original one. */
143-
144139
if (!caml_backtrace_active ||
145140
caml_backtrace_buffer == NULL ||
146141
caml_backtrace_pos == 0) {
147142
res = caml_alloc(0, 0);
148143
}
149144
else {
150-
backtrace_slot saved_caml_backtrace_buffer[BACKTRACE_BUFFER_SIZE];
151-
int saved_caml_backtrace_pos;
152145
intnat i;
153146

154-
saved_caml_backtrace_pos = caml_backtrace_pos;
155-
156-
if (saved_caml_backtrace_pos > BACKTRACE_BUFFER_SIZE) {
157-
saved_caml_backtrace_pos = BACKTRACE_BUFFER_SIZE;
158-
}
159-
160-
memcpy(saved_caml_backtrace_buffer, caml_backtrace_buffer,
161-
saved_caml_backtrace_pos * sizeof(backtrace_slot));
162-
163-
res = caml_alloc(saved_caml_backtrace_pos, 0);
164-
for (i = 0; i < saved_caml_backtrace_pos; i++) {
165-
Field(res, i) = Val_backtrace_slot(saved_caml_backtrace_buffer[i]);
147+
/* We use caml_alloc_shr instead of caml_alloc, since caml_alloc might
148+
trigger finalisers, which in turn might overwrite the backtrace. */
149+
res = caml_alloc_shr(caml_backtrace_pos, 0);
150+
for (i = 0; i < caml_backtrace_pos; i++) {
151+
caml_initialize(&Field(res, i), Val_backtrace_slot(caml_backtrace_buffer[i]));
166152
}
153+
res = caml_check_urgent_gc(res);
167154
}
168155

169156
CAMLreturn(res);

0 commit comments

Comments
 (0)