caml_make_array assumes small arrays #6460
Original bug ID: 6460
caml_make_array asserts that the size of the array is less than Max_young_wosize so that it can use caml_alloc_small. However, the compiler does not enforce this invariant.
Comment author: nevor
This bug is interesting because any literal given (including the one in test.ml) will be valid as long as it is smaller than the size of the minor heap which is a lot bigger than the enforced invariant.
So we found a minimal test that trashes the memory because of the violated invariant and we have attached a simple patch that corrects the problem by calling caml_alloc_shr when the size goes over Max_young_wosize. The new example and the patch work both on bytecode and native.
Maybe this was not done before for a good reason, in that case we might consider something along the lines of refusing too big literals, any constant smaller than Minor_heap_min would be good enough.
Comment author: @xavierleroy
Fixed as proposed in 4.02 (commit 15010) and on trunk (commit 15011).
I heard some questions about the use of caml_check_urgent_gc after caml_alloc_shr. This is correct, this is cheap, and this can help memory performance. caml_alloc_shr never triggers a GC, because often it is being called during a minor GC. However, caml_alloc_shr can set flags saying that it is "urgent" to perform a GC in the future. caml_check_urgent_gc will check and honor this flag, without waiting for the next polling of this flag by the bytecode interpreter or generated native code.