Skip to content

Conversation

@mat-hek
Copy link
Contributor

@mat-hek mat-hek commented Mar 27, 2025

We've been getting ASAN errors in the calls to nif_atomvm_posix_read, and it seems it's because when GC moves things around, it also moves what the fd_obj_ptr variable points to. The proposed solution is to move the GC before enif_get_resource. We haven't encountered the error after the fix, but please verify that it makes sense.

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

int count = term_to_int(count_term);

size_t size = term_binary_data_size_in_terms(count) + BINARY_HEADER_SIZE + TERM_BOXED_SUB_BINARY_SIZE + TUPLE_SIZE(2);
if (UNLIKELY(memory_ensure_free_with_roots(ctx, size, 2, argv, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We do not need to keep argv[1] in roots AFAIK

Copy link
Contributor Author

@mat-hek mat-hek Mar 27, 2025

Choose a reason for hiding this comment

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

Yup, but I thought it would be more future-proof to put all the args in the roots, and it shouldn't have noticeable performance overhead. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

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

in that specific case, if you just want to future proof it would be better to have argc, argv otherwise let's use 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, done

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.

This bug is likely also in release-0.6: let's rebase this PR and metion this on our changelog under v0.6.6 / Fixed sections.

@mat-hek mat-hek force-pushed the upstream-fix-posix-read branch 2 times, most recently from 3bb7b06 to 804de39 Compare March 27, 2025 13:14
@mat-hek mat-hek changed the base branch from main to release-0.6 March 27, 2025 13:14
@mat-hek mat-hek requested a review from bettio March 27, 2025 13:15
@mat-hek mat-hek force-pushed the upstream-fix-posix-read branch 2 times, most recently from 40c12af to 1d00d27 Compare March 28, 2025 16:23
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.

Everything good. However I didn't notice that you didn't --signoff your code (see also git commit --signoff) and CI failed.

@mat-hek mat-hek force-pushed the upstream-fix-posix-read branch from 1d00d27 to 0c8c1a5 Compare March 28, 2025 16:32
@bettio bettio merged commit b134785 into atomvm:release-0.6 Mar 28, 2025
172 of 178 checks passed
bettio added a commit that referenced this pull request Mar 29, 2025
Merge fixe for resources concurrency bug (#1603) and fixes for memory
corruptions (#1600 and #1601) from release-0.6 branch.
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.

3 participants