Repository navigation
Raise out_of_memory when list_to_binary cannot allocate its result - #2495
gabrielmancini wants to merge 1 commit into
Conversation
|
AMP code: (human me doesn't entirely understand all of this) PR #2495 reviewRecommendation: keep the OOM fix, but limit it to Reviewed the last commit, P2 · Unchecked callers can publish an invalid term
The failed allocation itself is pre-existing, and these callers already hit an invalid copy on the parent commit. What changes is the failure mode: they can now return malformed results or enter exception handling without an exception set by the failing NIF. This is not a regression on successful allocations. Suggested fix: keep the checked allocation in the shared NIFThe two hunks below apply to the reviewed commit. Use the already-fallible allocator in diff --git a/src/libAtomVM/nifs.c b/src/libAtomVM/nifs.c
--- a/src/libAtomVM/nifs.c
+++ b/src/libAtomVM/nifs.c
@@ -3071,11 +3071,12 @@ static term nif_erlang_list_to_binary_1(Context *ctx, int argc, term argv[])
free(alloc_ptr);
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
- term bin_res = term_from_literal_binary(bin_buf, bin_size, &ctx->heap, ctx->global);
-
- free(alloc_ptr);
+ term bin_res = term_create_uninitialized_binary(bin_size, &ctx->heap, ctx->global);
if (UNLIKELY(term_is_invalid_term(bin_res))) {
+ free(alloc_ptr);
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
+ memcpy((void *) term_binary_data(bin_res), bin_buf, bin_size);
+ free(alloc_ptr);
return bin_res;
}
diff --git a/src/libAtomVM/term.h b/src/libAtomVM/term.h
--- a/src/libAtomVM/term.h
+++ b/src/libAtomVM/term.h
@@ -2124,13 +2124,11 @@ static inline term term_create_uninitialized_binary(size_t size, Heap *heap, Glo
* @param size size of binary data buffer.
* @param heap the heap to allocate the binary in
* @param glb the global context as refc binaries are global
- * @return a term pointing to the boxed binary pointer, or an invalid term if the binary could not be allocated.
+ * @return a term pointing to the boxed binary pointer.
*/
static inline term term_from_literal_binary(const void *data, size_t size, Heap *heap, GlobalContext *glb)
{
term binary = term_create_uninitialized_binary(size, heap, glb);
- if (LIKELY(!term_is_invalid_term(binary))) {
- memcpy((void *) term_binary_data(binary), data, size);
- }
+ memcpy((void *) term_binary_data(binary), data, size);
return binary;
}Regression coverageThe conversion tests cover small and empty results and |
term_alloc_refc_binary returns an invalid term when the refc buffer cannot be malloc'd, but list_to_binary/1 copied the data into it through term_from_literal_binary anyway, writing through a NULL pointer. On ESP32 with a fragmented heap, erlang:iolist_to_binary/1 of a ~14 KB iolist (gen_tcp_socket flattening an HTTP reply) panicked with StoreProhibited right after 'Unable to allocate 14442 bytes for refc_binary'. Allocate the result with term_create_uninitialized_binary, check it, and copy only on success, so list_to_binary/1 and iolist_to_binary/1 raise out_of_memory and only the calling process crashes. The shared term_from_literal_binary is left as it was, since its other callers do not check for an invalid term. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
810ede4 to
8da2743
Compare
|
Thanks for the review. Applied your suggestion in 8da2743: There is still no regression test, since there is no targeted allocation-failure hook. The proof is the ESP32 field case: |
As atomvm#2495 after review: list_to_binary/1 allocates with term_create_uninitialized_binary, checks it and copies only on success, and the shared term_from_literal_binary is left as upstream has it, since its other callers do not check for an invalid term. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
term_from_literal_binarycopies the data into the binary thatterm_create_uninitialized_binaryreturns, but for a refc binary that is an invalid term when the buffer cannot be malloc'd, so the copy writes through a NULL pointer (term_binary_dataof the invalid term).list_to_binary/1(andiolist_to_binary/1, which calls it) hits this whenever the heap has no block for the result: it already holds a temporary buffer of the same size, so on a fragmented ESP32 heap a result of half the largest free block can be enough. We saw it on an ESP32 without PSRAM whengen_tcp_socketflattened a ~14 KB HTTP reply:This skips the copy for an invalid term, and makes
list_to_binary/1raiseout_of_memoryin that case, so the calling process crashes instead of the VM.Field check, same board (ESP32, no PSRAM), asking a spawned process for
iolist_to_binary/1of 64 one-KB chunks:out_of_memory; VM keeps runningBefore the fix, the first one is the panic above.
No test: the failure needs a malloc that fails for the second buffer only, which neither generic_unix nor the QEMU test app gives us deterministically. Happy to add one if there is an allocation-failure hook I missed. CHANGELOG entry under Fixed.