Skip to content

Commit 389df24

Browse files
Fixes for 6.0.0-rc.3 (#449)
GC, cppia JIT and socket fixes found on the way to 6.0.0-rc.3, with their regression tests: cpp.Pointer fields and boxed pointers keep what they point into (Haxe 4's haxe.atomic.AtomicInt was freed under it), the heap stays at its target under churn of objects bigger than a line, x *= y on a Dynamic keeps y under the JIT, a lazily compiled cppia function's code is read with acquire, and Socket.select's timeout is capped at 1e8 seconds, which macOS accepts.
2 parents 655a130 + 155c9a4 commit 389df24

100 files changed

Lines changed: 1578 additions & 117 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/main.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ jobs:
265265
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
266266
run: timeout 1200 sh test/emscripten/exceptions/run.sh
267267

268+
- name: Rebuild with HXCPP_LINK_NO_EXCEPTIONS, without the compile cache
269+
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
270+
run: timeout 1200 sh test/emscripten/link_no_exceptions.sh
271+
268272
- name: Run the Embind test
269273
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
270274
run: timeout 600 sh test/emscripten/embind/run.sh
@@ -324,6 +328,10 @@ jobs:
324328
# Git Bash on Windows
325329
shell: bash
326330

331+
- run: bash test/release_stamp/check.sh
332+
name: What the release stamps into the package
333+
shell: bash
334+
327335

328336
# The test suites on every platform with the stable Haxe; nightly.yml runs
329337
# them with the Haxe nightly build once a day

‎.github/workflows/test.yml‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,34 @@ jobs:
215215
haxelib run hxcpp Build.xml
216216
test "$(tr -d '\r' < out/kept.txt)" = old
217217
test "$(tr -d '\r' < out/replaced.txt)" = new
218+
- name: build tool links again when a link flag changes
219+
continue-on-error: ${{ inputs.warn_only }}
220+
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
221+
working-directory: test/link_flags
222+
run: |
223+
haxelib run hxcpp Build.xml
224+
rm -f prog.map
225+
haxelib run hxcpp Build.xml -DWANT_MAP
226+
test -f prog.map
227+
- name: build tool fails again after a step after the link failed
228+
continue-on-error: ${{ inputs.warn_only }}
229+
if: ${{ !cancelled() && steps.setup.outcome == 'success' && runner.os == 'Windows' }}
230+
working-directory: test/link_flags
231+
run: |
232+
rm -rf obj prog.exe prog.exe.hash
233+
for build in first second; do
234+
if haxelib run hxcpp Build.xml -DmanifestFile=bad.manifest; then
235+
echo "::error::the $build build with a bad manifest should have failed"
236+
exit 1
237+
fi
238+
done
239+
- name: build tool lists the defines it reads
240+
continue-on-error: ${{ inputs.warn_only }}
241+
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
242+
working-directory: test/link_flags
243+
run: |
244+
haxelib run hxcpp defines > defines.txt
245+
grep -qx HXCPP_COMPILE_CACHE defines.txt
218246
- name: build tool rejects file names that differ only in case
219247
continue-on-error: ${{ inputs.warn_only }}
220248
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
@@ -467,7 +495,7 @@ jobs:
467495
working-directory: test/regression
468496
run: |
469497
failed=""
470-
for test in GcMutationRoots GcContainerMutation IncrementalGcSlices IncrementalGcContainers IncrementalGcArrayChunks IncrementalGcDequeMoves IncrementalGcPacing IncrementalGcStartSweep IncrementalGcStats IncrementalGcFinishBoundary IncrementalGcVerifier IncrementalWindowsHeaders GcWeakMapChain GenerationalFinalizerLifecycle GenerationalLargeBufferLifecycle; do
498+
for test in GcMutationRoots GcContainerMutation IncrementalGcSlices IncrementalGcContainers IncrementalGcArrayChunks IncrementalGcDequeMoves IncrementalGcPacing IncrementalGcStartSweep IncrementalGcStats IncrementalGcFinishBoundary IncrementalGcVerifier IncrementalWindowsHeaders GcWeakMapChain GcWeakMapWalkCollect GenerationalFinalizerLifecycle GenerationalLargeBufferLifecycle; do
471499
echo "::group::incremental gc: $test"
472500
# The lifecycle fixtures' hxml selects generational mode; compile
473501
# the same Main with incremental mode here instead.

‎Changes.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22
libhxcpp 6.0.0 (unreleased)
33
------------------------------------------------------------
44

5+
* Fixed a Haxe 4 haxe.atomic.AtomicInt kept in a field reading freed memory after a collection: Haxe 4's AtomicInt on C++ is a cpp.Pointer into a one-element array nothing else holds, and the collector ignored cpp.Pointer fields, so it freed the array (HaxeFoundation/hxcpp#1225; Haxe 5 changed AtomicInt instead, in HaxeFoundation/haxe#12236). A cpp.Pointer field, or a pointer boxed in a Dynamic, now keeps what it points into in the GC heap, as a pointer on the stack does, and pins it under HXCPP_GC_MOVING. Of 10,000 atomics in fields, 9,999 read freed memory after a collection and none do now
6+
* Fixed HXCPP_GC_INCREMENTAL failing to compile ("Incremental GC requires regenerated C++") with libhxcpp installed from a release, 6.0.0-rc.2 among them: the release workflow rewrote extraParams.hxml with the version and dropped its other line, the macro that turns on the generated write barriers the incremental collector needs. Only the version lines are rewritten now
7+
* Fixed a change of link flags or libraries alone leaving the program as it was: the build tool linked again only when an object or library file had changed, so -D HXCPP_LINK_STACK_SIZE, HXCPP_LINK_NO_MEMORY_GROWTH, HXCPP_LINK_NO_EMBIND or HXCPP_LINK_NODERAWFS on an Emscripten build, which change nothing that is compiled, or a flag or library added to a target in Build.xml, did nothing until the objects changed. The flags, libraries and library paths are now part of what a link is checked against, so each program links once more after updating
8+
* Fixed an Emscripten build failing to link (undefined symbol: __wasm_lpad_context) when -D HXCPP_LINK_NO_EXCEPTIONS was added to a project built before without it: the define also compiles without -fwasm-exceptions, but the build tool took every HXCPP_LINK_ define for a link option and compiled nothing again. It recompiles now, and once back without it
9+
* Fixed `haxelib run hxcpp defines` failing with "Could not read" from any folder but tools/hxcpp: it opened the build tool's sources by name, relative to the caller's folder. It lists the defines the build tool reads again
10+
* Fixed an ARM64 Windows build with HXCPP_MSVC_VER naming the Visual Studio to use: on an x64 machine the setup looked for vcvarsx64_arm64.bat, which Visual Studio calls vcvarsamd64_arm64.bat, and stopped, and on an ARM64 machine it found the ARM64 script and then ran the x64 one, compiling for x64. It runs the ARM64 script for the host
11+
* Fixed a build whose strip or manifest step failed after the link succeeding when run again, with the output unstripped or without its manifest: the link was recorded as done before those steps ran, so the next build had nothing to do. A failure after the link now forgets it, and the next build links again
12+
* Each thread object no longer holds a 64 KB buffer below API level 500: only Haxe 5's hx::thread::Scratch uses it, yet with Haxe 4 every Haxe thread kept one while it ran, and so did every native thread that asked for its sys.thread.Thread, for as long as Haxe's own thread list holds it, which for a native thread is until the process ends
13+
* Fixed Socket.select returning at once with nothing ready for a timeout past 2^31 seconds, Math.POSITIVE_INFINITY included: the timeout overflowed an int of seconds. It is capped at 10^8 seconds (over three years), past which macOS' select fails, and a NaN timeout waits for nothing. Windows' Poll, which takes the same timeout, is fixed with it
14+
* Fixed Sys.sleep returning at once, or after a much shorter time, for a duration that did not fit the native call: on Windows from 2^32 milliseconds, about 49.7 days, and past 2^31 seconds elsewhere. Sys.sleep(Math.POSITIVE_INFINITY) returned at once instead of sleeping for good. A long sleep goes in parts that fit
15+
* Fixed Lock.wait, Semaphore.acquire, Semaphore.tryAcquire with a timeout and a blocking Deque.pop ending the process with "GCFree Zone mismatch" when called inside a GC-free zone the caller had entered, as with cpp.vm.Gc.enterGCFreeZone: when they blocked they left that zone, so the caller's own exit failed. They keep the caller's zone, as Mutex and Condition already did
16+
* Fixed Mutex and Condition ending the process with "Bad Allocation while collecting" when a thread used them inside a GC-free zone it had entered, as with cpp.vm.Gc.enterGCFreeZone, while another thread collected. Their uncontended paths stop at a pending collection, and that stop paused the thread as if it were outside the zone; between collections it could instead wait for ever for a release the collector never sends to a thread in a zone. A thread in a zone is already safe, so cpp.vm.Gc.safePoint and those stops now do nothing there
17+
* Fixed a malformed cppia file that calls the super constructor of a class with no constructor, in it or in a script class above it, crashing the loader, which read the constructor through a null pointer. It fails to load with "Super constructor call on a class with no constructor". The fuzz job found it (libhxcpp #257)
18+
* Fixed a malformed cppia file whose constructor was a bare return or, in an interface, had no body loading and then crashing: a super call into it crashed the loader, and a new of its class crashed when it ran, through the constructor's missing function. It fails to load with "Constructor is not a function" (libhxcpp #257)
19+
* Fixed a malformed cppia file that makes a new instance of a script interface loading and then crashing when it ran, interpreted and under the JIT: an interface has no host base to make the object from. It fails to load with "Could not link new" and the interface's name, as for a type with nothing to construct (libhxcpp #257)
20+
* Fixed a malformed cppia file whose dynamic function was a bare return loading and then crashing, at the next collection after its class booted or when it was called, interpreted and under the JIT: its value was a closure of no function. It fails to load with "Dynamic function is not a function" (libhxcpp #257)
21+
* Fixed _hx_gc_freeze copying none of a string of 4,000 bytes or more with HXCPP_GC_NURSERY, so the frozen string read past its copy: the size of a large allocation in the nursery was taken as 0 when its header had a flag, as a long string's hash slot or UTF-16 text sets
22+
23+
* Fixed the collector freeing large buffers that the memory of a CFFI abstract made with create_abstract pointed to, and crashing: the collector sorts the large allocations into an index on the first lookups of a collection, and the marking threads, which mark an abstract's memory conservatively, could build it two at once. One thread builds it now. In a test holding 20,000 buffers through 8,000 abstracts, 10 to 14 collections in 16 lost buffers or the process crashed
24+
25+
* Fixed a weak map's toString calling toString on keys the collector had already freed, and on whatever had taken their memory, which could crash: a collection during the walk, which allocates as it goes, unlinked the entries whose keys had died, and the walk went on from its entry's next, which still led to them. A weak map now holds its keys and values while a walk is on them, and lets them go at the next collection after it
26+
* Fixed *= and -= on an element of an Array<Int> under the cppia JIT giving -2147483648 when the result passed the Int range: the JIT did them in Float arithmetic and converted back, where compiled code and the interpreter wrap, so a hash kept in an array, h[i] *= 31, went wrong once it overflowed
27+
* Fixed cppia negating an Int as a Float wherever the result was not used as an Int, interpreted and under the JIT: -x with x = 0 printed as "-0", -(-2147483648) gave 2147483648 instead of wrapping, and Type.typeof gave TFloat, as Dynamic, in a String or as a Float
28+
* Fixed interpreted cppia giving contains and remove on an Array<Any> as an Int where they became Dynamic or a String: "" + a.contains(x) was "1" and Std.isOfType(a.remove(x), Bool) false. The fix for the typed arrays had missed Array<Any>'s builtins
29+
* Fixed concat with a null array under the cppia JIT ending the process where it throws Null Object Reference (with HXCPP_CHECK_POINTER, or HXCPP_CATCH_SEGV as the Cppia host has), on typed arrays and Array<Any>: the native call had no exception bridge. The script catches it, as it does interpreted
30+
* Fixed loading a cppia module crashing with HXCPP_GC_MOVING when a collection that moved objects came while the module was making its types: updating the loading module's pointers read the types not yet made
31+
* Fixed the heap growing to twice its working target, and with more churn to hundreds of megabytes, when a program churned objects bigger than a line (128 bytes) with little of them live. A collection that started because no free block could take an allocation counted the free rows of blocks that bigger allocations had already taken and filled as rows in use, so the heap looked full and its target doubled at every other collection, and the groups given back in between were faulted in again. Churning 2,000,000 Bytes of 2,000 bytes with 64 kept reserves 20 MB instead of 41 MB, the same churn of 2,000-character strings after it 20 MB instead of 83 MB (896 MB with HXCPP_GC_MOVING), and churning 200,000 such Bytes takes 16 ms instead of 91 ms (median of 5 processes); a program that churned 1,000 to 2,000-byte Bytes, strings and arrays peaked at 26 MB to 48 MB instead of 187 MB to 896 MB (libhxcpp #256)
32+
533
libhxcpp 6.0.0-rc.2
634
------------------------------------------------------------
735

‎include/hx/GC.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ bool IsWeakRefValid(const HX_CHAR *inPtr);
202202
// that points to real, active objects.
203203
void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx);
204204

205+
// Keeps what a cpp.Pointer points into, as a pointer on the stack would
206+
void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx);
207+
205208

206209
// Create/Remove a root.
207210
// All statics are explicitly registered - this saves adding the whole data segment

‎include/hx/GcTypeInference.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ template<> inline void MarkMember<cpp::Variant>(cpp::Variant &outT,hx::MarkConte
3535
{
3636
outT.mark(__inCtx);
3737
}
38+
// A cpp.Pointer field into the GC heap keeps what it points into. Haxe 4's
39+
// haxe.atomic.AtomicInt is one into an array nothing else holds, which was
40+
// freed under it (HaxeFoundation/hxcpp#1225)
41+
template<typename T> inline void MarkMember(cpp::Pointer<T> &outT,hx::MarkContext *__inCtx)
42+
{
43+
if (outT.ptr)
44+
hx::MarkConservativePointer((void *)outT.ptr,__inCtx);
45+
}
3846
template<typename T> inline void MarkMember(hx::Native<T> &outT,hx::MarkContext *__inCtx)
3947
{
4048
if (outT.ptr)

‎src/Dynamic.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class Int64Data : public hx::Object
212212
class PointerData : public hx::Object
213213
{
214214
public:
215-
inline void *operator new( size_t inSize, hx::NewObjectType inAlloc=NewObjAlloc,const char *inName="cpp.Pointer")
215+
inline void *operator new( size_t inSize, hx::NewObjectType inAlloc=NewObjContainer,const char *inName="cpp.Pointer")
216216
{ return hx::Object::operator new(inSize,inAlloc,inName); }
217217

218218
PointerData(void *inValue) : mValue(inValue) {};
@@ -238,6 +238,13 @@ class PointerData : public hx::Object
238238
return mValue < r ? -1 : mValue==r ? 0 : 1;
239239
}
240240

241+
// A boxed pointer into the GC heap keeps what it points into, as a
242+
// cpp.Pointer field does: a script's haxe.atomic.AtomicInt is one
243+
void __Mark(hx::MarkContext *__inCtx) HXCPP_OVERRIDE
244+
{
245+
if (mValue)
246+
hx::MarkConservativePointer(mValue,__inCtx);
247+
}
241248

242249
void *mValue;
243250
};

‎src/hx/Hash.h‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,21 @@ struct Hash : public HashBase< typename ELEMENT::Key >
937937
}
938938

939939

940+
// A weak map holds its keys and values while a walk is on them. A
941+
// collection the walk allocated for, as toString does, unlinked the
942+
// entries whose keys died, and the walk went on from its entry's next,
943+
// which still led to them, to keys already freed. The map is marked again
944+
// where a collection might otherwise skip it: an old map in a minor
945+
// collection, or one an incremental cycle has already scanned
940946
struct AutoWalk
941947
{
942948
Hash *hash;
943-
AutoWalk(Hash *inHash) : hash(inHash) { hash->walks++; }
949+
AutoWalk(Hash *inHash) : hash(inHash)
950+
{
951+
hash->walks++;
952+
if (Element::WeakKeys)
953+
HX_OBJ_WB_PESSIMISTIC_GET(hash);
954+
}
944955
~AutoWalk() { hash->walks--; }
945956
};
946957

@@ -1104,14 +1115,16 @@ struct Hash : public HashBase< typename ELEMENT::Key >
11041115

11051116

11061117
// Mark ...
1118+
// inStrong marks the keys and values of a weak map too: see AutoWalk
11071119
struct HashMarker
11081120
{
11091121
hx::MarkContext *__inCtx;
1110-
HashMarker(hx::MarkContext *ctx) : __inCtx(ctx) { }
1122+
bool strong;
1123+
HashMarker(hx::MarkContext *ctx, bool inStrong) : __inCtx(ctx), strong(!Hash::Element::WeakKeys || inStrong) { }
11111124
void operator()(typename Hash::Element *inElem)
11121125
{
11131126
HX_MARK_ARRAY(inElem);
1114-
if (!Hash::Element::WeakKeys)
1127+
if (strong)
11151128
{
11161129
HX_MARK_MEMBER(inElem->key);
11171130
HX_MARK_MEMBER(inElem->value);
@@ -1127,14 +1140,14 @@ struct Hash : public HashBase< typename ELEMENT::Key >
11271140
if (size > 256 && hx::IncrementalRange(this,IncrementalScan,__inCtx)) return;
11281141
#endif
11291142

1130-
HashMarker marker(__inCtx);
1143+
HashMarker marker(__inCtx, walks!=0);
11311144
iterateBuckets(marker);
11321145
}
11331146

11341147
#ifdef HXCPP_GC_INCREMENTAL
11351148
static bool IncrementalScan(hx::Object *inOwner, size_t &ioBucket, size_t &ioItem, hx::MarkContext *__inCtx) {
11361149
Hash *hash = static_cast<Hash *>(inOwner);
1137-
HashMarker marker(__inCtx);
1150+
HashMarker marker(__inCtx, hash->walks!=0);
11381151
Element *element = reinterpret_cast<Element *>(ioItem);
11391152
unsigned int work = 0;
11401153
while (ioBucket < static_cast<size_t>(hash->bucketCount) && work < 256) {

‎src/hx/Thread.cpp‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ struct Deque : public Array_obj<Dynamic>
131131
__hxcpp_gc_safe_point();
132132
if (!inMutex.TryLock())
133133
{
134-
hx::EnterGCFreeZone();
134+
// Keeps a zone the caller is in, as Mutex does
135+
hx::AutoGCFreeZone zone;
135136
inMutex.Lock();
136-
hx::ExitGCFreeZone();
137137
}
138138
}
139139

@@ -165,9 +165,10 @@ struct Deque : public Array_obj<Dynamic>
165165
{
166166
mSemaphore.Reset();
167167
mMutex.Unlock();
168-
hx::EnterGCFreeZone();
169-
mSemaphore.Wait();
170-
hx::ExitGCFreeZone();
168+
{
169+
hx::AutoGCFreeZone zone;
170+
mSemaphore.Wait();
171+
}
171172
lockFor(mMutex);
172173
}
173174
}
@@ -204,10 +205,9 @@ struct Deque : public Array_obj<Dynamic>
204205
lockFor(mSemaphore.mMutex);
205206
if (inBlock && !hasItems())
206207
{
207-
hx::EnterGCFreeZone();
208+
hx::AutoGCFreeZone zone;
208209
while(!hasItems())
209210
mSemaphore.QWait();
210-
hx::ExitGCFreeZone();
211211
}
212212
Dynamic result = popHead();
213213
if (hasItems())
@@ -459,12 +459,12 @@ class hxLock : public hx::Object
459459
return false;
460460
}
461461

462-
hx::EnterGCFreeZone();
462+
// Keeps a zone the caller is in, as Mutex does
463+
hx::AutoGCFreeZone zone;
463464
if (inTimeout<0)
464465
mNotEmpty.Wait( );
465466
else
466467
mNotEmpty.WaitSeconds(wait);
467-
hx::ExitGCFreeZone();
468468
}
469469
}
470470
void Release()

‎src/hx/cppia/ArrayBuiltin.cpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,14 @@ struct ArraySetter : public ArrayBuiltinBase
269269
switch((int)FUNC::op)
270270
{
271271
case aoMult:
272-
case aoDiv:
273272
case aoSub:
273+
// Int elements stay in Int arithmetic, which wraps as compiled
274+
// code does: through a Float, a result past the Int range
275+
// converted to -2147483648
276+
rightHandType = elemType == etInt ? etInt : etFloat;
277+
break;
278+
279+
case aoDiv:
274280
case aoMod:
275281
rightHandType = etFloat;
276282
break;
@@ -1260,7 +1266,12 @@ struct ArrayBuiltin : public ArrayBuiltinBase
12601266

12611267
static hx::Object *SLJIT_CALL runConcat( Array_obj<ELEM> *inArray, Array_obj<ELEM> *inOther )
12621268
{
1269+
// A null array to append throws: the exception becomes the script's
1270+
// pending one, which the code after the call checks
1271+
TRY_NATIVE
12631272
return inArray->concat(inOther).mPtr;
1273+
CATCH_NATIVE
1274+
return 0;
12641275
}
12651276

12661277

@@ -1891,6 +1902,7 @@ struct ArrayBuiltin : public ArrayBuiltinBase
18911902
genNullReferenceExceptionCheck(compiler, thisVal);
18921903
args[0]->genCode(compiler, sJitArg1.as(jtPointer), etObject);
18931904
compiler->callNative( (void *)runConcat, thisVal, sJitArg1.as(jtPointer));
1905+
compiler->checkException();
18941906
compiler->convertReturnReg(etObject, inDest, destType);
18951907
break;
18961908
}

0 commit comments

Comments
 (0)