Repository navigation
Free native string returns after copying them into Vale - #645
Open
nuttyartist wants to merge 2 commits into
Open
nuttyartist wants to merge 2 commits into
nuttyartist wants to merge 2 commits into
Conversation
Fixes for ARM, VmdSiteGen, and linear types.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AI assisted (GPT-6 Astra Extra High)
Each
str + strcurrently leaks the nativeValeStrreturned by__vale_addStr. A loop of 1,000 concatenations exits successfully but leaves 1,000 unreachable allocations. Free the native string afterreceiveHostObjectIntoValehas copied it into Vale, using the existingLinear::dealiashelper.The compiler change is six lines and applies to externs returning
str, including conversions, substring, and user-defined externs. It does not change aggregate-return or exported-argument ownership.Reproduction
Base:
6fb98324c0b919a55812376f4d74cfcbfc7f2db7(master). Save this asrepro.vale:With a working compiler package from that revision, set
VALECto its absolutevalecpath:On macOS ARM64, the program returns 0 but
leaksreturns 1:Repeat with
--census true: same leak, and Vale's own census check still passes. Census counts Vale-managed allocations, not the C allocation made byValeStrNew. The requested allocation is 15 bytes (4 + 10 + 1); the macOS leak-checking allocator reports 32 bytes per allocation.Cause and fix
Backend/builtins/strings.callocates the returned string and frees both incoming string copies. The extern return path inBackend/src/function/expressions/externs.cppthen callsreceiveHostObjectIntoVale, which copies the returned bytes into the RC region. Nothing releases the original native buffer; the code even has adont we have to free here toocomment at that point.The fix releases that buffer after the copy. Optimized LLVM IR now contains
freeof the exact pointer returned byvale_abi___vale_addStr, afterstrncpyinto the new RC string.Verification
Measured before/after using the same frontend, stdlib, builtins, driver, and link command; only the backend is replaced:
The eight additional checks cover a C/Vale string-return round trip, i32/i64/float conversions, substring, ASCII conversion, and empty/UTF-8 concatenation. They check exact contents, also exercise int/bool/float/void extern returns, and return 0 both before and after. Normal runs also passed with
MallocScribble=1.All concatenation/control programs returned 0 with the expected checksum. The added regression also checks the resulting string's contents, and checks the native heap with census both off and on:
CMAKE_POLICY_VERSION_MINIMUMis needed for CMake 4; no CMake build type was set. No unrelated build-source changes are included. The regression fails on the original backend and passes on the patched backend. Its Linux branch uses Valgrind; Linux execution and the full compiler test suite have not been run here.Environment: macOS 26.6.2 (25G83), Apple ARM64; Apple clang 21.0.0; LLVM 16.0.6; CMake 4.0.1; Java 17.0.1; frontend built from the base revision with sbt 1.6.2 / Scala 2.12.19 (
git lfs pull --include="Frontend/lib/*", thensbt -batch 'set test in assembly := {}' clean assemblyinFrontend). The Coordinator was bootstrapped using Vale-Mac-0.2.0.27 and runs as x86_64 under Rosetta; it launches the rebuilt ARM64 backend and clang. All tested program binaries are ARM64. Existing compiler packaging/build instructions apply; no application or library-mode harness is needed.