Skip to content

Commit 35cede7

Browse files
Fix seven runtime problems found in stock hxcpp's recent reports (#499)
Erase a moved object's old id entry before inserting its new address When the moving collector moves an object that has an id, MoveSpecial found its entry in sObjectIdMap, inserted the new address with operator[], and then erased the old entry through the iterator it had found. The insert can rehash the unordered_map, and a rehash invalidates every iterator, so the erase was undefined behaviour. Any id'd object can be moved, ObjectMap keys among them, since under HXCPP_GC_MOVING they all take their hash from __hxcpp_obj_id. The id is now read out, the old entry erased, and the new one inserted, as the finalizer and zombie maps just above already do. No test can fail before this: libstdc++, libc++ and MSVC all keep a node's iterator valid through a rehash, even in their debug modes. Issue679 moves 20,000 ObjectMap keys through compactions and still finds every one. HaxeFoundation/hxcpp#1468 Tests: Issue679 Check the length of an IPv6 address array before reading it host_to_string_ipv6, socket_connect_ipv6 and socket_bind_ipv6 read 16 bytes from &host[0] whatever the array's length. An empty array grew to one element and was read 15 bytes past it, a shorter one was read past its end, and a null one crashed. So host_to_string_ipv6 of four 0xff bytes gave "ffff:ffff::106:8010", and connect and bind went to whatever address followed the array in memory. host_reverse_ipv6 already checked for 16 bytes and copied them out first. The four functions share one helper now, which copies the address out of the array and refuses a null array or one of any length but 16. host_to_string_ipv6 gives null for such an array, as host_reverse_ipv6 does, and connect and bind throw "Invalid IPv6 address". sys.net.Host always passes the 16 bytes host_resolve_ipv6 made, so only direct cpp.NativeSocket callers could reach this. HaxeFoundation/hxcpp@a8a5724f, which swapped &a[0] for Pointer() in these functions, does not check the length either. Tests: SocketIpv6AddressLength Run a cppia var statement with no declarations without indexing it TVars reads its declaration count from the file, and checkCount accepts 0. runVoid then started from &vars[0] on the empty vector, which is undefined behaviour: a standard library that checks indexing, such as libstdc++ with _GLIBCXX_ASSERTIONS or MSVC's checked iterators, stops the program there. The Haxe compiler never writes an empty one, so only a malformed or hand-made file reaches it. BlockExpr guards the same pattern with a size check. runVoid walks the declarations by index now, as genCode already did. CppiaEmptyVars loads hand-written modules whose main is an empty var statement, alone and twice in a block, and runs them interpreted and under the JIT. No build here checks indexing, so it passed before too. HaxeFoundation/hxcpp@a8a5724f Tests: CppiaEmptyVars Stop for a collection in the atomic operations on a plain int * libhxcpp #455 made Haxe 4's haxe.atomic.AtomicInt operations safe points, through cpp::Pointer<int> overloads of the _hx_atomic_* functions. Haxe 5.0.0-preview.1's AtomicInt is a class with an Int field instead, and calls _hx_atomic_add(&{0}, ...) on that field, so it passes a plain int *. That binds to the runtime's volatile int * forms, which never stop, so a thread spinning on flag.load() still held up every collection, and every other thread with it. AtomicBool wraps AtomicInt and had the same gap. AtomicSpinSafePoint has failed this way on the preview.1 jobs, where failures are warnings. The stopping forms take an int * now, and the cpp::Pointer<int> ones call them. The runtime's own spinlocks pass a volatile int *, so they keep the forms that do not stop. _hx_atomic_xor was the one runtime form that took an int *; it takes a volatile int * like the others. AtomicFieldSpinSafePoint does what preview.1's AtomicInt does on any Haxe: a thread spins on each of the nine operations in turn, called on the address of an Int field, while the main thread collects. Before this, the first collection never finished and its watchdog ended the test after 60 s; it passes in under a second now, and with HXCPP_GC_MOVING. HaxeFoundation/hxcpp#1443 Tests: AtomicFieldSpinSafePoint, AtomicSpinSafePoint Say an Int is not an Int64 haxe.Int64.isInt64(100) was true on C++ and false on the other targets, and so was isInt64(1.0), because a whole Float boxes as an Int. Haxe 4.2 implements isInt64 with cpp::Int64Struct::is, which asks Int64Handler, and Haxe 4.3 and 5.0.0-preview.1 with Std.isOfType(val, cpp.Int64), which asks the cpp::Int64 class's IsInt64 in Dynamic.cpp. Both took a boxed Int as well as a boxed Int64. They took Ints because small Int64 values, -1 to 255, used to come out of the boxed Int cache. Since 6.0.0 they have a cache of their own, so every boxed Int64 is an Int64Data and reports vtInt64, and both checks now take only that. This changes behaviour for code that used isInt64 to accept any integer. Std.isOfType(int64, Int) stays true for a value that fits, as for a whole Float. Int64IsInt64 checks isInt64 on Ints, a whole Float, null, a String and Int64 values on both sides of the box cache, asks the handler and the class directly so each is covered whichever Haxe builds it, and asks again from a cppia script, interpreted and under the JIT. Before this, the Int, 0 and 1.0 cases were all true. HaxeFoundation/hxcpp#1461 Tests: Int64IsInt64 Report a thread that allocates in a GC-free zone during a collection A thread in a GC-free zone that allocates, or collects, while another thread collects reaches PauseForCollect and waits there for the collection to finish. The collector neither waits for a thread in a zone nor releases it at the end, so in a release build that thread waited for ever, with nothing to say why. Native code that misses an ExitGCFreeZone is the usual way there. Debug builds report any allocation or collection in a zone, before it gets that far. PauseForCollect now ends the program with a critical error that names the misuse when its thread is in a zone. No correct caller gets there: safe points return first in a zone, as do the Mutex and Condition fast paths that call them, and ExitGCFreeZone waits out a collection on the state lock instead. GcFreeZoneAllocationReported runs the misuse in a child process: one thread enters a zone and allocates once a collection is pending, while another keeps it pending by reaching no safe point for half a second. Before this the allocation never returned and the child said so after 5 s; now the child prints the error and ends, and a debug build's own message is accepted too. HaxeFoundation/hxcpp#1468 Tests: GcFreeZoneAllocationReported Say how many certificates did not parse instead of a wrong mbedTLS error mbedtls_x509_crt_parse_file and mbedtls_x509_crt_parse_path return a negative error code, or, when they parsed something, a positive count of the certificates they could not parse. cert_load_file and cert_load_path passed any non-zero result to ssl_error, and mbedtls_strerror negates a positive value and looks it up: one bad certificate threw "ERROR - Generic error", two "BIGNUM - An error occurred while reading from or writing to a file", and so on. A negative result still goes to ssl_error. A positive one throws "Could not parse 1 certificate in <file>", or "certificates" for more. For a directory the message says "certificates or files", because parse_path also counts each file it could not read a certificate from. Both still throw, as before; whether a directory or bundle that partly parsed should be kept instead is left for its own change. SslCertificateParseCount writes a good certificate, the same with one corrupt certificate after it, and one between two corrupt ones, loads each, and loads a directory with a certificate and a text file. Before this, the three messages were the mbedTLS ones. HaxeFoundation/hxcpp@45adede6 Tests: SslCertificateParseCount Link a cppia script's host static object fields directly below API 500 HaxeFoundation/hxcpp#1047, to keep typed callables working at API level 500, stopped cppia linking a host class's static object fields to a direct reference: a script closure stored through an hx::Object pointer into a typed function slot is not callable as one. The exclusion had no API level guard, so in Haxe 4 builds, and Haxe 5.0.0-preview.1 ones, which build against the 430 runtime, every script read or write of a host static object, a singleton, a static Array or a static function, was a __GetStatic or __SetStatic by name, interpreted and jitted. c653b13 restored direct access for object members the same way and left statics out. The exclusion applies at API level 500 only now, as for members, and below that an object static gets the same MemReference an Int or String static already had. Statics are roots, marked at the start of every collection, so the direct store needs no write barrier, as a compiled store into a static has none. A new host_statics workload in bench/cppia reads one host static Array and stores another on each pass. Against f0622380, with --base: interpreter 13.2M -> 23.4M iterations/s (14.3M -> 25.8M rerun) JIT 31.5M -> 386M iterations/s (35.6M -> 542M rerun) native_call, which this cannot reach, read 8% slower under the JIT in both runs (287M against 312M). Keeping only Script.main's store to CppiaBridge.workload on the named path brought it to 296M against 301M, within the spread: the JIT compiles lazily, so main's smaller code moved every function compiled after it. CppiaHostStaticObjects has a script store young objects, an Array and a closure into host statics that alone hold them, through minor and full collections with HXCPP_GC_GENERATIONAL, then read them back in the script and in the host, interpreted and under the JIT. It passed before too, through the named path. HaxeFoundation/hxcpp#1365 Tests: CppiaHostStaticObjects, CppiaObjectFields
1 parent 7a45d8c commit 35cede7

39 files changed

Lines changed: 626 additions & 39 deletions

File tree

‎Changes.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ libhxcpp 6.0.3 (unreleased)
66
* Fixed the collector marking and moving the variables a cppia closure captures in place in the closure: a closure packs its captures at 4-byte offsets, so on 64-bit a String or object captured after an Int, as text in var count = 1; var text = "x"; function() return count + text, sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. Each capture is copied out to be marked or visited now, and written back if the moving collector moved it. Interpreted and jitted closures were both affected
77
* Fixed the cppia interpreter reading an argument's slot in place in the script frame when it fills in the argument's default value, and writing a Float or Dynamic default the same way: a frame keeps 4-byte alignment, so on 64-bit an argument after an Int, as b in f(a:Int, ?b:String = "x"), sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. The slots are copied in and out now, as the interpreter's other frame accesses have been since 6.0.0. The JIT, which compiles defaults into the function, was not affected
88
* Fixed Semaphore.tryAcquire(-1) waiting for ever where any other negative timeout returns at once, so a timeout worked out in whole seconds as deadline - now could wait with no limit once the deadline had passed. It was kept for Haxe 5.0.0-preview.1's Lock.wait() (libhxcpp #461), but preview.1's Lock waits through the runtime's own lock, which still takes a negative timeout as no limit, as Haxe 4's does. Only Haxe 5 nightlies from March to November 2026, which libhxcpp does not support, passed -1 to the semaphore; later ones call acquire() for no timeout. Every negative timeout returns at once now, as with stock hxcpp on Linux and macOS (HaxeFoundation/haxe#13114)
9+
* Fixed a thread spinning on Haxe 5.0.0-preview.1's haxe.atomic.AtomicInt or AtomicBool, as in while (flag.load() == 0) {}, stopping every collection, and so every other thread, for as long as it spun: preview.1 passes the address of an Int field to the _hx_atomic_* functions, which took it as a volatile int *, the runtime's form that never stops. An int * stops for a pending collection now, as Haxe 4's cpp.Pointer has since libhxcpp #455, and the runtime's own spinlocks keep the volatile form (HaxeFoundation/hxcpp#1443)
10+
* Fixed undefined behaviour in HXCPP_GC_MOVING when a compaction moved an object that has an id, as an ObjectMap key does: the move inserted the new address into the id table, then erased the old entry through an iterator found before the insert, which a rehash during the insert leaves invalid. The old entry is erased first now (HaxeFoundation/hxcpp#1468)
11+
* Fixed cpp.NativeSocket's host_to_string_ipv6, socket_connect_ipv6 and socket_bind_ipv6 reading 16 bytes from an address array of any length: a shorter one was read past its end, so it formatted, connected to or bound whatever followed it in memory, and a null one crashed. An array that is not 16 bytes long now gives null from host_to_string_ipv6, as it already did from host_reverse_ipv6, and connect and bind throw "Invalid IPv6 address". sys.net.Host always passes 16 bytes (HaxeFoundation/hxcpp@a8a5724f)
12+
* haxe.Int64.isInt64 is false for an Int now, as on the other targets, in compiled code and in cppia: isInt64(100) was true, and so was isInt64(1.0), since a whole Float boxes as an Int. This changes what code that used isInt64 to accept any integer sees, and it should test Std.isOfType(value, Int) as well. Std.isOfType(value, cpp.Int64) changed with it, as Haxe 4.3 and later implement isInt64 with it, and cpp.Int64Struct.is, which Haxe 4.2 uses. Std.isOfType(value, Int) is still true for an Int64 that fits (HaxeFoundation/hxcpp#1461)
13+
* A thread that allocates or collects inside a GC-free zone while another thread collects, as native code that misses an exit from one can, now ends the program with "Critical Error: Allocating or collecting from a GC-free thread while another thread collects - was ExitGCFreeZone missed?". In a release build it waited for ever, with no message: the collector neither waits for a thread in a zone nor releases it. Debug builds already reported any allocation in a zone (HaxeFoundation/hxcpp#1468)
14+
* Fixed sys.ssl.Certificate.loadFile and loadPath reporting certificates that did not parse as an unrelated mbedTLS error: mbedTLS counts them, and the count was read as an error code, so one threw "ERROR - Generic error" and two a BIGNUM file error. They throw "Could not parse 2 certificates in <file>" now, and for a directory "certificates or files", as mbedTLS also counts a file that holds none (HaxeFoundation/hxcpp@45adede6)
15+
* Fixed cppia's interpreter running a var statement with no declarations, which a malformed file can hold, from the first element of its empty list: undefined behaviour, on which a standard library that checks indexing stops the program (HaxeFoundation/hxcpp@a8a5724f)
16+
* A cppia script reads and writes a host class's static object fields, such as a singleton, a static Array or a static function, directly again below API level 500, which includes Haxe 5.0.0-preview.1, instead of looking each up by name: a loop that reads one host static Array and stores another runs 1.8 times as fast interpreted and 12 times as fast under the JIT. Host statics of other types and object fields already were direct; API level 500 keeps the lookup, which a typed function needs (HaxeFoundation/hxcpp#1365)
917

1018
libhxcpp 6.0.2
1119
------------------------------------------------------------

‎bench/cppia/Script.hx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Script {
1111
case "closure_call": new ClosureCall();
1212
case "native_call": new NativeCall();
1313
case "native_callback": new NativeCallback();
14+
case "host_statics": new HostStatics();
1415
default: throw 'Unknown CPPIA workload ${CppiaBridge.name}';
1516
};
1617
}
@@ -147,3 +148,18 @@ class NativeCallback implements CppiaWorkload {
147148
});
148149
}
149150
}
151+
152+
class HostStatics implements CppiaWorkload {
153+
public function new() {}
154+
155+
public function run():Int {
156+
var sum = 0;
157+
for (i in 0...200000) {
158+
// A read and a write of host static objects each pass
159+
var values = CppiaBridge.values;
160+
sum += values[i & 15];
161+
CppiaBridge.last = values;
162+
}
163+
return sum;
164+
}
165+
}

‎bench/cppia/shared/CppiaBridge.hx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package;
55
class CppiaBridge {
66
public static var name:String;
77
public static var workload:CppiaWorkload;
8+
/** Read and written by the host_statics workload. **/
9+
public static var values:Array<Int> = [for (i in 0...16) i];
10+
public static var last:Array<Int>;
811

912
@:noInline
1013
public static function step(value:Int):Int {

‎bench/src/CppiaBenchmarks.hx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class CppiaBenchmarks extends Benchmark {
2323
result.push(new CppiaBenchmarks("closure_call", "calls", 200000, 10, jit));
2424
result.push(new CppiaBenchmarks("native_call", "calls", 200000, 50, jit));
2525
result.push(new CppiaBenchmarks("native_callback", "callbacks", 200000, 10, jit));
26+
result.push(new CppiaBenchmarks("host_statics", "iterations", 200000, 10, jit));
2627
}
2728
return result;
2829
}

‎include/cpp/Int64.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class Int64Handler
3232
}
3333
else if (op==dhoIs)
3434
{
35+
// Every Int64 boxes as one, small values included, so a boxed Int
36+
// is not one, as on the other targets
3537
StructHandlerDynamicParams *params = (StructHandlerDynamicParams *)outResult;
36-
hx::Object *obj = params->inData;
37-
int type = obj->__GetType();
38-
params->outProcessed = type==vtInt || type==vtInt64;
38+
params->outProcessed = params->inData->__GetType()==vtInt64;
3939
}
4040
else
4141
return DefaultStructHandler::handler(op,ioValue,inSize, outResult);

‎include/cpp/Pointer.h‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -574,21 +574,33 @@ T *StarOf(T &x) { return &x; }
574574
}
575575

576576

577-
// Haxe 4's haxe.atomic.AtomicInt is a cpp.Pointer<Int>, and its methods call
578-
// these. A thread spinning on one never otherwise reached a safe point, so a
579-
// collection on another thread waited for it for ever, as Haxe 5's
580-
// hx::atomic::AtomicInt does not (libhxcpp #455). The runtime's own spinlocks
581-
// call the volatile int * forms, which do not stop.
577+
// The forms of these that stop for a pending collection, for an int * and a
578+
// cpp::Pointer<int>. Haxe 4's haxe.atomic.AtomicInt is a cpp.Pointer<Int>, and
579+
// Haxe 5.0.0-preview.1's keeps an Int field and passes its address, a plain
580+
// int *. A thread spinning on either never otherwise reached a safe point, so
581+
// a collection on another thread waited for it for ever (libhxcpp #455).
582+
// Later Haxe 5's hx::atomic::AtomicInt stops by itself. The runtime's own
583+
// spinlocks call the volatile int * forms in hx/StdLibs.h, which do not stop.
582584
#define HX_ATOMIC_SAFE_POINT if (::hx::gPauseForCollect) __hxcpp_gc_safe_point();
583-
inline int _hx_atomic_add(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_add((volatile int *)a.ptr, b); }
584-
inline int _hx_atomic_sub(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_sub((volatile int *)a.ptr, b); }
585-
inline int _hx_atomic_and(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_and((volatile int *)a.ptr, b); }
586-
inline int _hx_atomic_or(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_or((volatile int *)a.ptr, b); }
587-
inline int _hx_atomic_xor(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_xor((int *)a.ptr, b); }
588-
inline int _hx_atomic_compare_exchange(::cpp::Pointer<int> a, int expected, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_compare_exchange((volatile int *)a.ptr, expected, replacement); }
589-
inline int _hx_atomic_exchange(::cpp::Pointer<int> a, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_exchange((volatile int *)a.ptr, replacement); }
590-
inline int _hx_atomic_load(::cpp::Pointer<int> a) { HX_ATOMIC_SAFE_POINT return _hx_atomic_load((volatile int *)a.ptr); }
591-
inline int _hx_atomic_store(::cpp::Pointer<int> a, int value) { HX_ATOMIC_SAFE_POINT return _hx_atomic_store((volatile int *)a.ptr, value); }
585+
inline int _hx_atomic_add(int *a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_add((volatile int *)a, b); }
586+
inline int _hx_atomic_sub(int *a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_sub((volatile int *)a, b); }
587+
inline int _hx_atomic_and(int *a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_and((volatile int *)a, b); }
588+
inline int _hx_atomic_or(int *a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_or((volatile int *)a, b); }
589+
inline int _hx_atomic_xor(int *a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_xor((volatile int *)a, b); }
590+
inline int _hx_atomic_compare_exchange(int *a, int expected, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_compare_exchange((volatile int *)a, expected, replacement); }
591+
inline int _hx_atomic_exchange(int *a, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_exchange((volatile int *)a, replacement); }
592+
inline int _hx_atomic_load(int *a) { HX_ATOMIC_SAFE_POINT return _hx_atomic_load((volatile int *)a); }
593+
inline int _hx_atomic_store(int *a, int value) { HX_ATOMIC_SAFE_POINT return _hx_atomic_store((volatile int *)a, value); }
592594
#undef HX_ATOMIC_SAFE_POINT
593595

596+
inline int _hx_atomic_add(::cpp::Pointer<int> a, int b) { return _hx_atomic_add(a.ptr, b); }
597+
inline int _hx_atomic_sub(::cpp::Pointer<int> a, int b) { return _hx_atomic_sub(a.ptr, b); }
598+
inline int _hx_atomic_and(::cpp::Pointer<int> a, int b) { return _hx_atomic_and(a.ptr, b); }
599+
inline int _hx_atomic_or(::cpp::Pointer<int> a, int b) { return _hx_atomic_or(a.ptr, b); }
600+
inline int _hx_atomic_xor(::cpp::Pointer<int> a, int b) { return _hx_atomic_xor(a.ptr, b); }
601+
inline int _hx_atomic_compare_exchange(::cpp::Pointer<int> a, int expected, int replacement) { return _hx_atomic_compare_exchange(a.ptr, expected, replacement); }
602+
inline int _hx_atomic_exchange(::cpp::Pointer<int> a, int replacement) { return _hx_atomic_exchange(a.ptr, replacement); }
603+
inline int _hx_atomic_load(::cpp::Pointer<int> a) { return _hx_atomic_load(a.ptr); }
604+
inline int _hx_atomic_store(::cpp::Pointer<int> a, int value) { return _hx_atomic_store(a.ptr, value); }
605+
594606
#endif

‎include/hx/StdLibs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ inline int _hx_atomic_or(volatile int *a, int b) {
427427
#endif
428428
}
429429

430-
inline int _hx_atomic_xor(int *a, int b) {
430+
inline int _hx_atomic_xor(volatile int *a, int b) {
431431
#if defined(HX_GCC_ATOMICS)
432432
return __atomic_fetch_xor(a, b, __ATOMIC_SEQ_CST);
433433
#elif defined(HX_MSVC_ATOMICS)

‎src/Dynamic.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,11 @@ static bool IsFloat(hx::Object *inPtr)
636636
{
637637
return inPtr && (TCanCast<IntData>(inPtr) || TCanCast<DoubleData>(inPtr) || TCanCast<Int64Data>(inPtr) );
638638
}
639+
// haxe.Int64.isInt64 asks this from Haxe 4.3 (Int64Handler's dhoIs before).
640+
// Every Int64 boxes as one, small values included, so a boxed Int is not one
639641
static bool IsInt64(hx::Object *inPtr)
640642
{
641-
return inPtr && (TCanCast<Int64Data>(inPtr) || TCanCast<IntData>(inPtr));
643+
return TCanCast<Int64Data>(inPtr);
642644
}
643645

644646
static bool IsPointer(hx::Object *inPtr)

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,11 +3272,20 @@ struct GetFieldByName : public CppiaDynamicExpr
32723272
return new LinkMissExpr(this);
32733273
}
32743274
const StaticInfo *info = staticClass->GetStaticStorage(name);
3275+
bool direct = info && info->type!=hx::fsUnknown;
3276+
3277+
// A host static can hold a typed function at API level 500, which a
3278+
// script closure must be converted to on the way in: stored through
3279+
// an hx::Object pointer, it is not callable as one
3280+
// (HaxeFoundation/hxcpp#1365). Below that an object static holds the
3281+
// object as it is, as an object field does, and statics are roots,
3282+
// so a store needs no barrier
3283+
#if (HXCPP_API_LEVEL>=500)
3284+
if (direct && info->type==fsObject)
3285+
direct = false;
3286+
#endif
32753287

3276-
// Do not use a MemReference for static access to objects.
3277-
// If the object in question is a hx::Callable_obj, its pointer will be downcasted to a hx::Object* and then updated to a non callable_obj pointer.
3278-
// This leads to memory exceptions later when then trying to invoke that callable.
3279-
if (info && info->type!=hx::fsUnknown && info->type != fsObject)
3288+
if (direct)
32803289
{
32813290
CppiaExpr* replace = createStaticAccess(this, info->type, info->address);
32823291
replace->link(inModule);
@@ -6632,12 +6641,12 @@ struct TVars : public CppiaVoidExpr
66326641
return this;
66336642
}
66346643

6644+
// By index: a file can declare no vars, and vars[0] of an empty vector is
6645+
// undefined behaviour, which checked library builds trap
66356646
void runVoid(CppiaCtx *ctx) HXCPP_OVERRIDE
66366647
{
6637-
CppiaExpr **v = &vars[0];
6638-
CppiaExpr **end = v + vars.size();
6639-
for(;v<end && !ctx->breakContReturn && !ctx->exception;v++)
6640-
(*v)->runVoid(ctx);
6648+
for(size_t v=0; v<vars.size() && !ctx->breakContReturn && !ctx->exception; v++)
6649+
vars[v]->runVoid(ctx);
66416650
}
66426651

66436652
#ifdef CPPIA_JIT

‎src/hx/gc/Immix.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4998,12 +4998,15 @@ class GlobalAllocator
49984998
hx::sMakeZombieSet.insert(inTo);
49994999
}
50005000

5001+
// Erased before the insert, as above: the insert can rehash the map,
5002+
// which leaves id no iterator to erase through
50015003
hx::ObjectIdMap::iterator id = hx::sObjectIdMap.find(inFrom);
50025004
if (id!=hx::sObjectIdMap.end())
50035005
{
5004-
hx::sIdObjectMap[id->second] = inTo;
5005-
hx::sObjectIdMap[inTo] = id->second;
5006+
int objectId = id->second;
50065007
hx::sObjectIdMap.erase(id);
5008+
hx::sObjectIdMap[inTo] = objectId;
5009+
hx::sIdObjectMap[objectId] = inTo;
50075010
}
50085011

50095012
// Maybe do something faster with weakmaps
@@ -8358,6 +8361,13 @@ class LocalAllocator : public hx::StackContext
83588361
if (sgIsCollecting)
83598362
CriticalGCError("Bad Allocation while collecting - from finalizer?");
83608363

8364+
// The collector neither waits for a thread in a GC-free zone nor
8365+
// releases it, so this would wait for ever. Safe points return before
8366+
// this in a zone, so only allocating or collecting in one gets here,
8367+
// which debug builds report whether a collection is pending or not
8368+
if (mGCFreeZone)
8369+
CriticalGCError("Allocating or collecting from a GC-free thread while another thread collects - was ExitGCFreeZone missed?");
8370+
83618371
mPausedForCollect = true;
83628372
mReadyForCollect.Set();
83638373
mCollectDone.Wait();

0 commit comments

Comments
 (0)