Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
libhxcpp 6.0.0 (unreleased)
------------------------------------------------------------

* Fixed a cppia class that implements both a script interface and a host interface, such as haxe.Constraints.IMap, calling the wrong method or crashing: the script interface's first method that the host does not know got the slot of the host's last interface method, so both were stored in one slot. Script interface methods are numbered after the host's now (libhxcpp #462)
* Fixed a thread spinning on Haxe 4's haxe.atomic.AtomicInt, as in while (flag.load() == 0) {}, stopping every collection, and so every other thread, for as long as it spun: the atomic operations were not safe points, so a collection waited for it, and it waited for a flag the collecting thread would set afterwards. They stop for a pending collection now, as Haxe 5's AtomicInt and Mutex.tryAcquire do, and so do the AtomicObject operations (libhxcpp #455)
* Fixed trace, Sys.print and Sys.println stopping every thread when stdout blocked, as it does for a pipe nobody reads, a slow log reader or a Windows console with text selected: they wrote outside a GC-free zone, so the next collection on any thread waited for the blocked write, and every other thread waited for the collection. On Windows, Process.stdin.close() did the same while another thread was blocked writing to the child, and waited itself until the child exited. They write in a zone now, and stdin.close() cancels the writes in progress before it closes, as Process.close() does (libhxcpp #454)
* Fixed a finalizer set with cpp.vm.Gc.setFinalizer that takes a Mutex ending the process with "Bad Allocation while collecting - from finalizer?", even with the lock free: finalizers run on the collecting thread inside the collection, and Mutex's uncontended path stopped there for the collection as if it were another thread. The collecting thread passes that stop now, so a finalizer can take a Mutex, as with stock hxcpp. A finalizer still must not allocate (libhxcpp #452)
* Fixed exceptions under the cppia JIT running on past the throw, or skipping the script's catch, when they came from a native helper of the jitted code: a Dynamic field or index updated with +=, -= and the others, Dynamic + Dynamic, ++ and -- on a Dynamic, a Dynamic or host ArrayAccess index, the typed array setters and increments, Array<Any> stores, string iterators and enum construction. Some of these caught the exception and the jitted code never looked, so it ran on and, if it allocated, could free the exception before the catch got it, crashing the next call; the rest let it unwind through the jitted frames, past the script's catch to the host's, or out of the process. Each now catches, the jitted code checks after each call, and the collector keeps a caught exception until it is handled. MSVC builds now compile with -bigobj, as debug builds already did (libhxcpp #465)
* Fixed three cppia array builtins writing to the wrong memory: under the JIT, a[i]++ or --a[i] whose result was used as a Float stored that result through whatever the third argument register held, corrupting the array or memory near it, and a typed push made room before running its argument, so an argument that grew the same array had the value stored past its buffer. Interpreted, a[i] = rhs and a[i] += rhs found the element before running rhs, so an rhs that grew the array stored into the old buffer, and a[a.length] = a.length stored the length after the store. The interpreter and the JIT now agree, and match the other targets (libhxcpp #463)
* Fixed HXCPP_GC_MOVING, and HXCPP_GC_DEBUG_ALWAYS_MOVE without generational, corrupting live objects after a collection that ran out of blocks to move into: the block it was emptying kept, in part, the start bits of the objects it had moved out, and a stale stack word holding one of their old addresses had a later collection take the moved header for an object of 254 rows and mark rows past the block's row table, over the objects in its first rows. The start bits go as the objects move, and a moved header is never taken for an object (libhxcpp #450)
* 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
* 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
* 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
Expand Down
17 changes: 17 additions & 0 deletions include/cpp/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,21 @@ T *StarOf(T &x) { return &x; }
}


// Haxe 4's haxe.atomic.AtomicInt is a cpp.Pointer<Int>, and its methods call
// these. A thread spinning on one never otherwise reached a safe point, so a
// collection on another thread waited for it for ever, as Haxe 5's
// hx::atomic::AtomicInt does not (libhxcpp #455). The runtime's own spinlocks
// call the volatile int * forms, which do not stop.
#define HX_ATOMIC_SAFE_POINT if (::hx::gPauseForCollect) __hxcpp_gc_safe_point();
inline int _hx_atomic_add(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_add((volatile int *)a.ptr, b); }
inline int _hx_atomic_sub(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_sub((volatile int *)a.ptr, b); }
inline int _hx_atomic_and(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_and((volatile int *)a.ptr, b); }
inline int _hx_atomic_or(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_or((volatile int *)a.ptr, b); }
inline int _hx_atomic_xor(::cpp::Pointer<int> a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_xor((int *)a.ptr, b); }
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); }
inline int _hx_atomic_exchange(::cpp::Pointer<int> a, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_exchange((volatile int *)a.ptr, replacement); }
inline int _hx_atomic_load(::cpp::Pointer<int> a) { HX_ATOMIC_SAFE_POINT return _hx_atomic_load((volatile int *)a.ptr); }
inline int _hx_atomic_store(::cpp::Pointer<int> a, int value) { HX_ATOMIC_SAFE_POINT return _hx_atomic_store((volatile int *)a.ptr, value); }
#undef HX_ATOMIC_SAFE_POINT

#endif
12 changes: 12 additions & 0 deletions include/hx/StdLibs.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,27 +554,39 @@ inline Dynamic __hxcpp_atomic_object_create(Dynamic value) {
}

inline Dynamic __hxcpp_atomic_object_store(Dynamic dynObj, Dynamic val) {
// A thread spinning on one must still reach a safe point (libhxcpp #455)
if (hx::gPauseForCollect)
__hxcpp_gc_safe_point();
AtomicObject *obj = dynamic_cast<AtomicObject *>(dynObj.mPtr);
if (!obj)
throw HX_INVALID_OBJECT;
return obj->store(val);
}

inline Dynamic __hxcpp_atomic_object_load(Dynamic dynObj) {
// A thread spinning on one must still reach a safe point (libhxcpp #455)
if (hx::gPauseForCollect)
__hxcpp_gc_safe_point();
AtomicObject *obj = dynamic_cast<AtomicObject *>(dynObj.mPtr);
if (!obj)
throw HX_INVALID_OBJECT;
return obj->load();
}

inline Dynamic __hxcpp_atomic_object_exchange(Dynamic dynObj, Dynamic newVal) {
// A thread spinning on one must still reach a safe point (libhxcpp #455)
if (hx::gPauseForCollect)
__hxcpp_gc_safe_point();
AtomicObject *obj = dynamic_cast<AtomicObject *>(dynObj.mPtr);
if (!obj)
throw HX_INVALID_OBJECT;
return obj->exchange(newVal);
}

inline Dynamic __hxcpp_atomic_object_compare_exchange(Dynamic dynObj, Dynamic expected, Dynamic replacement) {
// A thread spinning on one must still reach a safe point (libhxcpp #455)
if (hx::gPauseForCollect)
__hxcpp_gc_safe_point();
AtomicObject *obj = dynamic_cast<AtomicObject *>(dynObj.mPtr);
if (!obj)
throw HX_INVALID_OBJECT;
Expand Down
56 changes: 44 additions & 12 deletions src/hx/StdLibs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,23 @@ void WriteConsoleAllA(HANDLE h, const char *str, DWORD length) {
}
#endif

namespace
{
// Output to stdout can block for a long time: a pipe nobody reads, a slow
// reader, or a Windows console with text selected. Outside a GC-free zone, the
// next collection on any thread waits for it, and every other thread with it.
// The text is made before, since nothing may allocate in a zone, and a thread
// the collector does not know needs no zone.
struct OutputZone
{
bool entered;
OutputZone() : entered(HX_CTX_GET && hx::TryGCFreeZone()) { }
~OutputZone() { if (entered) hx::ExitGCFreeZone(); }
OutputZone(const OutputZone &) = delete;
OutputZone &operator=(const OutputZone &) = delete;
};
}

void __trace(Dynamic inObj, Dynamic info)
{
String text;
Expand All @@ -434,7 +451,6 @@ void __trace(Dynamic inObj, Dynamic info)
DWORD mode;
if (GetConsoleMode(handle, &mode))
{
fflush(stdout);
String s;
if (info == null()) {
s = HX_CSTRING("?? ") + text + HX_CSTRING("\n");
Expand All @@ -445,30 +461,38 @@ void __trace(Dynamic inObj, Dynamic info)
}
if (s.isUTF16Encoded())
{
WriteConsoleAllW(handle, s.__WCStr(), s.length);
const wchar_t *wide = s.__WCStr();
OutputZone zone;
fflush(stdout);
WriteConsoleAllW(handle, wide, s.length);
} else {
// ascii
WriteConsoleAllA(handle, s.__CStr(), s.length);
const char *narrow = s.__CStr();
OutputZone zone;
fflush(stdout);
WriteConsoleAllA(handle, narrow, s.length);
}
return;
}
#endif

hx::strbuf convertBuf;
const char *textOut = text.raw_ptr() ? text.out_str(&convertBuf) : "null";
if (info==null())
{
PRINTF("?? %s\n", text.raw_ptr() ? text.out_str(&convertBuf) : "null");
OutputZone zone;
PRINTF("?? %s\n", textOut);
fflush(stdout);
}
else
{
const char *filename = Dynamic((info)->__Field(HX_CSTRING("fileName"), HX_PROP_DYNAMIC))->toString().utf8_str(0,false);
int line = Dynamic((info)->__Field( HX_CSTRING("lineNumber") , HX_PROP_DYNAMIC))->__ToInt();

hx::strbuf convertBuf;
//PRINTF("%s:%d: %s\n", filename, line, text.raw_ptr() ? text.out_str(&convertBuf) : "null");
PRINTF("%s:%d: %s\n", filename, line, text.raw_ptr() ? text.out_str(&convertBuf) : "null");
OutputZone zone;
PRINTF("%s:%d: %s\n", filename, line, textOut);
fflush(stdout);
}
fflush(stdout);
}

void __hxcpp_exit(int inExitCode)
Expand Down Expand Up @@ -779,13 +803,17 @@ void __hxcpp_print_string(const String &inString)
HANDLE handle;
if (hxStdoutIsConsole(handle) && inV.isUTF16Encoded())
{
const wchar_t *wide = inV.__WCStr();
OutputZone zone;
fflush(stdout);
WriteConsoleAllW(handle, inV.__WCStr(), inV.length);
WriteConsoleAllW(handle, wide, inV.length);
return;
}
#endif
hx::strbuf convertBuf;
PRINTF("%s", inV.out_str(&convertBuf) );
const char *out = inV.out_str(&convertBuf);
OutputZone zone;
PRINTF("%s", out );
}

void __hxcpp_println_string(const String &inString)
Expand All @@ -796,15 +824,19 @@ void __hxcpp_println_string(const String &inString)
bool isConsole = hxStdoutIsConsole(handle);
if (isConsole && inV.isUTF16Encoded())
{
const wchar_t *wide = inV.__WCStr();
OutputZone zone;
fflush(stdout);
WriteConsoleAllW(handle, inV.__WCStr(), inV.length);
WriteConsoleAllW(handle, wide, inV.length);
fwrite("\n", 1, 1, stdout);
fflush(stdout);
return;
}
#endif
hx::strbuf convertBuf;
PRINTF("%s\n", inV.out_str(&convertBuf));
const char *out = inV.out_str(&convertBuf);
OutputZone zone;
PRINTF("%s\n", out);
// Flush every line, redirected or not. Boot asks for line buffering,
// but the Windows CRT treats _IOLBF as full buffering, so a redirected
// stdout there held a server's log lines back from whatever reads them,
Expand Down
Loading
Loading