Skip to content

Deprecate enif_make_resource* in favor of term_from_resource*#2108

Merged
bettio merged 1 commit intoatomvm:mainfrom
pguyot:w08/enif_make_resource_oom
Mar 8, 2026
Merged

Deprecate enif_make_resource* in favor of term_from_resource*#2108
bettio merged 1 commit intoatomvm:mainfrom
pguyot:w08/enif_make_resource_oom

Conversation

@pguyot
Copy link
Collaborator

@pguyot pguyot commented Feb 17, 2026

Also fix allocation size for enif_make_resource_binary

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@bettio
Copy link
Collaborator

bettio commented Feb 23, 2026

I made this comment on the enif_make_resource* topic: #2101 (comment)

Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that our memory managment model and the fact that we care about malloc failures, cannot work well with the BEAM original API.

So as I previously mentioned in #2101, IMHO the way to go is using term_from_resource() + explicit memory allocation.

So right now, we have a number of components around (both ours and 3rd party) using enif_make_resource.

If we do this change, if any of those components miss adding the term_is_invalid_term() check, we end having in the best case a crash or even undefined behaviors around. This kind of change can be hard to spot since we are not changing function signature.

Also the purpose of enif_make_resource (and the other similar functions) was to match the BEAM API, so I don't think we should require additional actions / checks after them, otherwise we mostly defeat their purpose.

Anyway, I dont like calling AVM_ABORT() but I think it is the less risky approach right now.

Also, there was the problem I mentioned here. So I think that right now the only solution is unconditionally allocate a heap fragment just for the resource. It is not efficient, but it is not risky as we do now, and it is acceptable since we are saying that term_from_resource() should be used instead.

@pguyot pguyot force-pushed the w08/enif_make_resource_oom branch from 7246c60 to c9de9be Compare March 7, 2026 22:20
@pguyot pguyot changed the title Make enif_make_resource* return invalid term in case of OOM Deprecate enif_make_resource* in favor of term_from_resource* Mar 7, 2026
@pguyot pguyot force-pushed the w08/enif_make_resource_oom branch from c9de9be to 58547a5 Compare March 8, 2026 08:10
Update all call sites to use `term_from_resource` and
`term_from_resource_binary`.

Also fix allocation size for `enif_make_resource_binary`

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
@pguyot pguyot force-pushed the w08/enif_make_resource_oom branch from 58547a5 to 3f631d9 Compare March 8, 2026 09:10
@bettio bettio merged commit 1597aa7 into atomvm:main Mar 8, 2026
213 of 215 checks passed
@pguyot pguyot deleted the w08/enif_make_resource_oom branch March 8, 2026 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants