Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage of live in opcodes and no longer preserve registers by default #795

Merged
merged 1 commit into from
Jan 28, 2024

Conversation

pguyot
Copy link
Collaborator

@pguyot pguyot commented Aug 27, 2023

Many opcodes have a live parameter which is the number of registers that should
be preserved by GC operations. Take this into account and conversely stop
preserving registers by default.

This affects significantly the safety of memory_ensure_free when writing nifs.
Indeed, arguments (which come from registers) are no longer preserved.
However, no platform-specific nif was found that needed to be fixed. If
required, calls to memory_ensure_free or memory_ensure_free_opt should be
replaced with calls to memory_ensure_free_with_roots.

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

@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from acb12d9 to b22465b Compare September 2, 2023 04:32
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from b22465b to 139baa0 Compare September 11, 2023 05:52
@pguyot pguyot mentioned this pull request Sep 24, 2023
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from 139baa0 to aab2e6b Compare October 16, 2023 20:20
@pguyot pguyot marked this pull request as ready for review October 16, 2023 20:21
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from aab2e6b to 73eda50 Compare October 16, 2023 22:26
@fadushin
Copy link
Collaborator

This affects significantly the safety of memory_ensure_free when writing nifs. Indeed, arguments (which come from registers) are no longer preserved. However, no platform-specific nif was found that needed to be fixed. If required, calls to memory_ensure_free or memory_ensure_free_opt should be replaced with calls to memory_ensure_free_with_roots.

We probably have a host of “downstream” nifs in the atomvm project that may need attention as the result of this change. Do-able, but it may have an impact on other projects.

Ones I can think of:

  • atomvm_adc
  • atomvm_ssd1306
  • atomvm_neopixel
  • atomvm_dht

others?

@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from 73eda50 to b623659 Compare October 26, 2023 04:57
@pguyot
Copy link
Collaborator Author

pguyot commented Oct 27, 2023

As mentioned, no nif in AtomVM needed to be updated.

I did check the nifs you mention and they do not need an update either:

  • atomvm_adc always calls memory_ensure_free to return an error tuple and these calls are not followed by accesses to non immediate terms that would be invalidated
  • atomvm_ssd1306 doesn't call memory_ensure_free at all
  • atomvm_neopixel is like atomvm_adc
  • atomvm_dht does call memory_ensure_free for something else than an error tuple, but doesn't access any non-immediate term after this allocation.

bettio added a commit that referenced this pull request Nov 7, 2023
This nif does not need to be updated after #795 is merged.

Also rewrite several lists module functions to not use this nif, either because
it was not necessary (`lists:seq/2,3`) or because a non-tail recursive loop
is more efficient memory-wise.

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
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from b623659 to 389676e Compare November 11, 2023 20:48
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch 2 times, most recently from 01909d8 to 18c498b Compare November 13, 2023 22:02
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch 2 times, most recently from 4acf545 to c8ec0e2 Compare November 22, 2023 07:17
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from c8ec0e2 to 9e06d11 Compare December 8, 2023 17:48
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from 9e06d11 to 75eb372 Compare December 26, 2023 07:24
Many opcodes have a live parameter which is the number of registers that should
be preserved by GC operations. Take this into account and conversely stop
preserving registers by default.

This affects significantly the safety of `memory_ensure_free` when writing nifs.
Indeed, arguments (which come from registers) are no longer preserved.
However, no platform-specific nif was found that needed to be fixed. If
required, calls to `memory_ensure_free` or `memory_ensure_free_opt` should be
replaced with calls to `memory_ensure_free_with_roots`.

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
@pguyot pguyot force-pushed the w34/fix-usage-of-live branch from 75eb372 to b67d320 Compare December 26, 2023 18:05
@bettio bettio merged commit 5bf6794 into atomvm:master Jan 28, 2024
85 checks passed
bettio added a commit that referenced this pull request Apr 10, 2024
…ng-realloc

Optimize garbage collection by using realloc

***
In addition to Paul's changes, this commit introduces an addition to make
it working with newer revisions, also it makes this feature optional and
available as experimental already in release-0.6.
***

This is a continuation of #795

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
@pguyot pguyot deleted the w34/fix-usage-of-live branch November 19, 2024 20:31
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