Skip to content

Writable cached Int64 boxes let reflection change independent and future values #295

Closed

Description

@dimensionscape

Small boxed Int64 values share a cached object, but that object remains writable through Int64Data::__SetField. Updating one value through reflection changes independent values and later conversions of the same number to Dynamic.

Reproduced at d734565 on Windows x64, Haxe 4.3.7, MSVC 19.44.35213, release HXCPP_M64.

The native-target sequence is:

var first:Dynamic = haxe.Int64.make(0, 7);
var independent:Dynamic = haxe.Int64.make(0, 7);
Reflect.setField(first, "lo", 9);
var createdAfter:Dynamic = haxe.Int64.make(0, 7);

The native boxed Int64 exposes hi and lo through its field API. The write succeeds, but both independent and createdAfter now convert back to Int64 value 9 instead of 7. This happens within one thread and does not need GC timing.

Expected: a field write to one box must not change an independently constructed value or alter future boxing of a numeric constant. Rejecting an unsupported write to a shared constant is preferable to modifying unrelated values. The fix should make the mutability policy explicit.

Current native regression output:

independent boxed value unchanged: false
later boxing keeps original value: false

In src/Dynamic.cpp, Dynamic(cpp::Int64) uses fromSmallInt64 for values from -1 through 255. That returns the process-wide Int64Data object in sConstDynamicInt64s. Int64Data::__SetField assigns mValue for hi and lo without checking whether the object is a shared constant. Atomic publication of the cache does not prevent later mutation.

Prevent writes from modifying shared boxed constants. Evaluate rejecting mutation of constant boxes or avoiding shared boxes for values whose field API permits mutation. Preserve exact Int64 conversion and document/test the chosen behavior. Include both hi and lo, repeated boxing, independent values and concurrent readers. This is separate from #172, which fixed publication ordering when cached boxes are constructed.

I added conventional local coverage first under test/regression/BoxedInt64CacheMutation with Main.hx, build.hxml and stdout.txt. The test permits a rejected write and verifies only isolation of independent/future values. It restores the native cached value before reporting so the probe does not leave it changed for the remainder of the process. The native runtime fails both checks; Eval passes the equivalent isolation checks using that target's low field name.

No runtime implementation changes have been made. The fix belongs in libhxcpp and does not require Haxe compiler or stdlib changes.

Activity

  1. added a commit that references this issue on Oct 24, 2026
    a9dd0af
  2. dimensionscape commented on Oct 24, 2026

    @dimensionscape
    MemberAuthor

    Fixed in a9dd0af3d69da8e2d961c8f83bbf02d10269f4e2. Boxed Int64 values from -1 to 255 are constants shared by every boxing of that value, so writing hi or lo on one now throws instead of changing all of them. A box of any other value can still be changed. Test: BoxedInt64CacheMutation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions