René's URL Explorer Experiment
Title: fix: typed array arguments leak and type confusion by edusperoni · Pull Request #2102 · NativeScript/android · GitHub
Open Graph Title: fix: typed array arguments leak and type confusion by edusperoni · Pull Request #2102 · NativeScript/android
X Title: fix: typed array arguments leak and type confusion by edusperoni · Pull Request #2102 · NativeScript/android
Description: Description
Memory-safety and robustness bugs in how JS arguments are converted for Java calls. Each fix has a spec that crashed or failed on the test app before this change.
1. Typed arrays passed to object parameters leaked, then aborted the process. Passing an ArrayBuffer, SharedArrayBuffer, typed array or DataView to an object parameter (ByteBuffer, Object, channel.read(new ArrayBuffer(n)), …) wraps its memory in a direct Java buffer. The converter also took a JNI global reference to that buffer and never deleted it. As a result:
the Java buffer could never be collected;
its link kept the JS typed array, and so the V8 backing store, alive forever;
once 51200 buffers had been passed, ART aborted with global reference table overflow (max=51200).
Core reaches this in file-system-access.android.ts (readBufferSync, writeBufferSync, writeBufferAsync).
The global ref was never needed for lifetime. The buffer is registered in the object table and linked to the JS object, which keeps the JS object, and with it the memory, alive for as long as Java holds the buffer. This is the same mechanism every other wrapper relies on. The conversion now:
drops the global ref;
deletes every local ref it creates (it used to leak ~5 per call);
looks up the ByteBuffer/ByteOrder method ids and the native byte order once, instead of on every call;
is shared by JsArgConverter and JsArgToArrayConverter instead of duplicated in both;
throws a JS error for a buffer over 2 GiB, which NewDirectByteBuffer cannot represent and ART aborts on.
2. Typed arrays passed to array parameters were type confusion. When the call's signature is already resolved, the same code handed the direct buffer to an array parameter. Two examples:
java.nio.ByteBuffer.wrap(u8), where wrap(byte[]) is the only one-argument overload;
java.lang.String.format("%s", f32), whose varargs parameter is Object[].
Without CheckJNI (release builds) Java then uses a DirectByteBuffer as an array. Debug builds abort:
JNI ERROR (app bug): attempt to pass an instance of java.nio.DirectByteBuffer as argument 1 to java.nio.ByteBuffer java.nio.ByteBuffer.wrap(byte[])
JNI ERROR (app bug): attempt to pass an instance of java.nio.ByteBufferAsFloatBuffer as argument 2 to java.lang.String java.lang.String.format(java.lang.String, java.lang.Object[])
The value is now copied into a new Java array of the parameter's type, which is the same copy semantics plain JS arrays already have:
Parameter
Copied as is (one SetArrayRegion)
byte[]
Int8Array, Uint8Array, Uint8ClampedArray, ArrayBuffer, SharedArrayBuffer, DataView
short[], char[]
Int16Array, Uint16Array (code units for char[])
int[]
Int32Array, Uint32Array
long[]
BigInt64Array, BigUint64Array
float[]
Float32Array
double[]
Float64Array
Any other typed array is converted element by element, exactly like a plain JS array holding the same numbers. For example:
Float64Array → int[] truncates through ToInt32;
Uint8Array → boolean[] maps non-zero values to true;
Int32Array → Object[] boxes each element.
These combinations throw Cannot convert to at index N:
a BigInt array for anything but long[];
an ArrayBuffer/DataView for anything but byte[];
any typed array for a nested array type such as int[][].
Overload resolution is unchanged. Whether a typed array should select a byte[] overload among several candidates is a separate decision. This PR only fixes the conversion once the signature is resolved.
3. Converting a large JS array to a primitive array overflowed the stack. ConvertJavaScriptArray built the Java array's contents in a stack array (VLA) sized by the JS array's length. A 4M-element array passed to int[] (16 MB) overflowed the main thread's stack, and worker threads have much smaller stacks. The contents are now built in a heap buffer.
4. A reused typed array could lose its Java buffer for good. This sequence broke the typed array for every later Java call:
JS drops a typed array, and its buffer becomes weakly held on the Java side.
Java hands the same buffer back to JS, which gets the original typed array.
Java lets go of the buffer and it is collected.
JS passes the typed array to Java again, and the call fails with Failed to get Java object by ID ... Attempt to use cleared object reference.
Now, when a typed array's link points at a collected buffer, the runtime asks Java to confirm that the buffer is gone (Runtime.releaseIfCollected). Only then does it drop the stale registrations on both sides (the Java object table entry and the native link state) and link a new buffer over the same memory. A link whose buffer Java still holds is never released, since that would leave the buffer pointing at memory nothing keeps alive.
5. Unconvertible JS array elements aborted the process. Converting a plain JS array to a Java array used ToChecked()/ToLocalChecked() on every element read. A BigInt or Symbol element, a throwing getter or a throwing valueOf therefore aborted with SIGTRAP. These now throw Cannot convert array to at index N: , for example ... [J at index 0: TypeError: Cannot convert a BigInt value to a number.
Arrays reject BigInt elements because scalar long parameters already reject BigInt. The scalar error message used to be empty because the message buffer was never filled; it now reads Cannot convert bigint to J at index N.
6. Over-long arrays aborted the process. A JS array or typed array longer than a Java array can be (more than 2^31 − 1 elements, for example a sparse [] with length = 2 ** 31) now fails with the usual conversion error instead of aborting.
Known hazards, not addressed here: the Java buffer points straight at the V8 backing store. If JS detaches that ArrayBuffer (transfer(), or a transfer through postMessage) or shrinks a resizable one while Java still holds the buffer, Java reads freed or stale memory. This is unchanged by this PR.
Does your commit message include the wording below to reference a specific issue in this repo?
No issue.
Related Pull Requests
None.
Does your pull request have unit tests?
Yes. testTypedArrayArgs.js has 18 specs, with a helper class com.tns.tests.TypedArrayArgs that has one single-overload method per array parameter type. The specs cover:
copy semantics for ByteBuffer.wrap(Uint8Array);
view offsets and lengths;
every same-layout pairing;
element-wise conversion matching plain arrays, including Object[] parameters;
the rejected combinations;
empty typed and plain arrays;
60,000 distinct typed arrays to an Object parameter, and as Object[] elements;
the Java buffer being collectable after JS drops the typed array;
a held buffer's memory staying valid after JS drops the typed array and GC runs;
a 4M-element JS array to int[];
relinking a reused typed array after Java collected its buffer;
BigInt, Symbol, throwing-getter and throwing-valueOf elements, plus BigInt to a scalar long;
an array longer than a Java array can be.
Verified on an API 36 arm64 emulator (debug build, CheckJNI on).
Scenario
Before
After
ByteBuffer.wrap(new Uint8Array(...))
CheckJNI abort (above)
heap ByteBuffer over a copy
String.format("%s", new Float32Array(2))
CheckJNI abort (above)
formats the boxed elements
60,000 × capacityOf(new Uint8Array(16))
global reference table overflow (max=51200) at ~51,200
passes in ~0.2 s
totalCapacity(Object[60000] of Float32Array)
global reference table overflow (max=51200)
passes
WeakReference to the buffer of a dropped Uint8Array, after GC
never cleared (40 GC rounds)
cleared after the first round
4M-element JS array → int[]
SIGSEGV, "stack pointer is in a non-existent map; likely due to stack overflow"
passes
Reused typed array after Java collected its buffer
Attempt to use cleared object reference (fails on the first run, every run)
relinked; Java sees a 3-byte buffer
echoLongs([1n, 2n]), echoInts([1, Symbol()]), an element getter that throws
SIGTRAP (V8 fatal from ToChecked)
JS error naming the cause
java.lang.Long.toHexString(5n)
throws with an empty message
Cannot convert bigint to J at index 0
Sparse array with length = 2 ** 31 → int[]
SIGABRT
Cannot convert array to [I at index 0
Full suite (./gradlew runtestsAndVerifyResults): 1545 specs, 0 failures. That is the 1527 existing specs plus the 18 new ones.
Summary by CodeRabbit
New Features
JavaScript typed arrays and array buffers can now be passed to Java methods as compatible primitive arrays or buffers.
Typed-array views and buffers support conversion to Java arrays, including element-by-element conversion when types differ. Buffer views preserve their offsets, and oversized inputs are rejected.
Tests
Added coverage for typed-array and buffer conversions, empty and large inputs, buffer references, and conversion errors.
Open Graph Description: Description
Memory-safety and robustness bugs in how JS arguments are converted for Java calls. Each fix has a spec that crashed or failed on the test app before this change.
1. Typed arrays passed...
X Description: Description
Memory-safety and robustness bugs in how JS arguments are converted for Java calls. Each fix has a spec that crashed or failed on the test app before this change.
1. Typed arrays passed...
Opengraph URL: https://github.com/NativeScript/android/pull/2102
X: @github
direct link
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:887fedcc-b2c0-7942-06dc-f55f0a0d9e83 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | CD68:2D2F2F:19A76B5:20D7ABC:6B01383C |
| html-safe-nonce | bb8053e586bb7064f6c7cf4bed0ebb524332210e70308718802004cfc096767d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDY4OjJEMkYyRjoxOUE3NkI1OjIwRDdBQkM6NkIwMTM4M0MiLCJ2aXNpdG9yX2lkIjoiMjg0MjI5NTcxOTM4MzY2MDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 697e8e34ea1c5de785a489008dfbca2b2d766ab36691f904cdfde886832e9611 |
| hovercard-subject-tag | pull_request:5253226145 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | ///pull_requests/show/files |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2102/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | Description
Memory-safety and robustness bugs in how JS arguments are converted for Java calls. Each fix has a spec that crashed or failed on the test app before this change.
1. Typed arrays passed... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Skip to content | https://github.com/NativeScript/android/pull/2102/files#start-of-content |
| https://github.com/ |
| Sign in | https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FNativeScript%2Fandroid%2Fpull%2F2102%2Ffiles |
| GitHubRepository hosting, code review, and collaboration | https://github.com/platform |
| CopilotAI coding agent for tasks, projects, and automated work | https://github.com/features/copilot |
| ActionsCI/CD and automation | https://github.com/features/actions |
| Code QualityQuality analysis, fixes, and enforcement | https://github.com/features/code-quality |
| CodespacesCloud development environments | https://github.com/features/codespaces |
| Code SecurityVulnerability detection and remediation | https://github.com/security/advanced-security/code-security |
| Secret ProtectionSecret scanning and leak prevention | https://github.com/security/advanced-security/secret-protection |
| All Features | https://github.com/features |
| Documentation | https://docs.github.com/en |
| Training | https://learn.github.com/skills |
| Certifications | https://learn.github.com/credentials |
| Build with Copilot | https://github.com/features/ai |
| Customize Copilot | https://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview |
| Integrations | https://github.com/marketplace |
| Trending repos | https://github.com/trending |
| GitHub Sponsors | https://github.com/open-source/sponsors |
| Developer articles | https://github.com/resources/articles |
| ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place. | https://github.blog/changelog/ |
| Enterprises | https://github.com/enterprise |
| Team | https://github.com/team |
| Startups | https://github.com/enterprise/startups |
| Nonprofits | https://github.com/solutions/industry/nonprofits |
| Healthcare | https://github.com/solutions/industry/healthcare |
| Financial services | https://github.com/solutions/industry/financial-services |
| Manufacturing | https://github.com/solutions/industry/manufacturing |
| Government | https://github.com/solutions/industry/government |
| See all industries | https://github.com/solutions/industry |
| Why GitHub | https://github.com/why-github |
| Customer Stories | https://github.com/customer-stories |
| Business insights | https://github.com/solutions/executive-insights |
| Trust Center | https://github.com/trust-center |
| GitHub Advanced SecurityEnterprise-grade code security | https://github.com/security/advanced-security |
| Copilot for businessEnterprise-grade AI features | https://github.com/features/copilot/copilot-business |
| Premium SupportEnterprise-grade 24/7 support | https://github.com/enterprise/premium-support |
| Documentation | https://docs.github.com/en |
| Resource center | https://github.com/resources |
| Blog | https://github.blog/ |
| Events & webinars | https://github.com/resources/events |
| Community Forum | https://github.com/orgs/community/discussions |
| Customer Stories | https://github.com/customer-stories |
| Integrations | https://github.com/marketplace |
| Customer support | https://help.github.com/ |
| Training | https://learn.github.com/skills |
| GitHub Status | https://www.githubstatus.com/ |
| About | https://github.com/about |
| Partners | https://github.com/partners |
| Changelog | https://github.blog/changelog/ |
| GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub. | https://githubuniverse.com/ |
| Pricing | https://github.com/pricing |
| Sign in | https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FNativeScript%2Fandroid%2Fpull%2F2102%2Ffiles |
| Sign up | https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=NativeScript%2Fandroid |
| Reload | https://github.com/NativeScript/android/pull/2102/files |
| Reload | https://github.com/NativeScript/android/pull/2102/files |
| Reload | https://github.com/NativeScript/android/pull/2102/files |
| NativeScript | https://github.com/NativeScript |
| android | https://github.com/NativeScript/android |
| Code | https://github.com/NativeScript/android |
| Issues106 (106) | https://github.com/NativeScript/android/issues |
| Pull requests24 (24) | https://github.com/NativeScript/android/pulls |
| Actions | https://github.com/NativeScript/android/actions |
| Projects | https://github.com/NativeScript/android/projects |
| Wiki | https://github.com/NativeScript/android/wiki |
| Security and quality | https://github.com/NativeScript/android/security |
| Insights | https://github.com/NativeScript/android/insights |
| Sign up for GitHub
| https://github.com/signup?return_to=%2FNativeScript%2Fandroid%2Fissues%2Fnew%2Fchoose |
| terms of service | https://docs.github.com/terms |
| privacy statement | https://docs.github.com/privacy |
| Sign in | https://github.com/login?return_to=%2FNativeScript%2Fandroid%2Fissues%2Fnew%2Fchoose |
| edusperoni | https://github.com/edusperoni |
| main | https://github.com/NativeScript/android/tree/main |
| fix/typed-array-arg-memory | https://github.com/NativeScript/android/tree/fix/typed-array-arg-memory |
|
Conversation
7
| https://github.com/NativeScript/android/pull/2102 |
|
Commits
2
| https://github.com/NativeScript/android/pull/2102/commits |
|
Checks
8
| https://github.com/NativeScript/android/pull/2102/checks |
|
Files changed
| https://github.com/NativeScript/android/pull/2102/files |
| Please reload this page | https://github.com/NativeScript/android/pull/2102/files |
|
fix: typed array arguments leak and type confusion
| https://github.com/NativeScript/android/pull/2102/files#top |
|
Show all changes
2 commits
| https://github.com/NativeScript/android/pull/2102/files |
|
c0e6be7
fix: typed array arguments leak and type confusion
edusperoni Nov 11, 2026
| https://github.com/NativeScript/android/pull/2102/commits/c0e6be7722b0a372cfc6a7ee79d2331008f4bab8 |
|
8524b93
fix: relink collected buffers; throw for unconvertible array elements
edusperoni Nov 11, 2026
| https://github.com/NativeScript/android/pull/2102/commits/8524b936fd06a40eb6f5e58292241329a834297b |
|
Clear filters
| https://github.com/NativeScript/android/pull/2102/files |
| Please reload this page | https://github.com/NativeScript/android/pull/2102/files |
| Please reload this page | https://github.com/NativeScript/android/pull/2102/files |
|
mainpage.js
| https://github.com/NativeScript/android/pull/2102/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5 |
|
testTypedArrayArgs.js
| https://github.com/NativeScript/android/pull/2102/files#diff-35e1421ebd379b3eebb4bfb0329ad0f287633a074ec59892efba7a81848a56a5 |
|
TypedArrayArgs.java
| https://github.com/NativeScript/android/pull/2102/files#diff-8a4e439f9dec3ec8abbf64418c43384f57b2e125ef9bf2595ee5b890841ffe9e |
|
JsArgConverter.cpp
| https://github.com/NativeScript/android/pull/2102/files#diff-9b41f905739990783e7f543f4c38aab34593873cf03762c959d6c3c7b6e991b7 |
|
JsArgConverter.h
| https://github.com/NativeScript/android/pull/2102/files#diff-bdd1e3debf725d5c60ef05e04e14346e55e103c6650314a49c83bf3d0c40cce6 |
|
JsArgToArrayConverter.cpp
| https://github.com/NativeScript/android/pull/2102/files#diff-2893b81a974ae0a84357d112dd872c8c7f3f3c74342ed15f25596e8f928e6ebf |
|
ObjectManager.cpp
| https://github.com/NativeScript/android/pull/2102/files#diff-0591ab8dc271af7e8ef11fabf0642e44b932f38003bd07a3696de149a02b2c9b |
|
ObjectManager.h
| https://github.com/NativeScript/android/pull/2102/files#diff-d491cf1566875df8740b3e9d732ac891286a776623aad17b249b36781ac7047d |
|
Runtime.java
| https://github.com/NativeScript/android/pull/2102/files#diff-cf9bb4e9ca1a476cacd6e270fc8bcd12c71f89d52d272915bc99df57b3581f59 |
| test-app/app/src/main/assets/app/mainpage.js | https://github.com/NativeScript/android/pull/2102/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5 |
|
View file
| https://github.com/NativeScript/android/blob/8524b936fd06a40eb6f5e58292241329a834297b/test-app/app/src/main/assets/app/mainpage.js |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/NativeScript/android/pull/2102/{{ revealButtonHref }} |
|
| https://github.com/NativeScript/android/pull/2102/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5 |
|
| https://github.com/NativeScript/android/pull/2102/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5 |
| test-app/app/src/main/assets/app/tests/testTypedArrayArgs.js | https://github.com/NativeScript/android/pull/2102/files#diff-35e1421ebd379b3eebb4bfb0329ad0f287633a074ec59892efba7a81848a56a5 |
|
View file
| https://github.com/NativeScript/android/blob/8524b936fd06a40eb6f5e58292241329a834297b/test-app/app/src/main/assets/app/tests/testTypedArrayArgs.js |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/NativeScript/android/pull/2102/{{ revealButtonHref }} |
| test-app/app/src/main/java/com/tns/tests/TypedArrayArgs.java | https://github.com/NativeScript/android/pull/2102/files#diff-8a4e439f9dec3ec8abbf64418c43384f57b2e125ef9bf2595ee5b890841ffe9e |
|
View file
| https://github.com/NativeScript/android/blob/8524b936fd06a40eb6f5e58292241329a834297b/test-app/app/src/main/java/com/tns/tests/TypedArrayArgs.java |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/NativeScript/android/pull/2102/{{ revealButtonHref }} |
| Please reload this page | https://github.com/NativeScript/android/pull/2102/files |
| Please reload this page | https://github.com/NativeScript/android/pull/2102/files |
|
| https://github.com |
| Terms | https://docs.github.com/site-policy/github-terms/github-terms-of-service |
| Privacy | https://docs.github.com/site-policy/privacy-policies/github-privacy-statement |
| Security | https://github.com/security |
| Status | https://www.githubstatus.com/ |
| Community | https://github.community/ |
| Docs | https://docs.github.com/ |
| Contact | https://support.github.com?tags=dotcom-footer |
Viewport: width=device-width
URLs of crawlers that visited me.