Skip to content
Closed
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
19 changes: 9 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:

jobs:
windows64-build:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
if: false # DEBUG(mac-arm64-cppia-jit): only run the macOS arm64 cppia job
runs-on: windows-latest
env:
PLATFORM: windows64
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:


linux-build:
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
if: false # DEBUG(mac-arm64-cppia-jit): only run the macOS arm64 cppia job
strategy:
matrix:
arch: [x86_64, arm64]
Expand Down Expand Up @@ -552,15 +552,9 @@ jobs:
strategy:
fail-fast: false
matrix:
target: [macro, js, hl, cpp, jvm, php, python, lua, flash, neko]
# DEBUG(mac-arm64-cppia-jit): only run the failing cppia job
target: [cppia]
arch: [arm64]
include:
- target: hl
arch: arm64
runci-args: --skip-hl-jit
- target: hl
arch: x86_64
runner: macos-15-intel

runs-on: ${{ matrix.runner || 'macos-latest' }}
steps:
Expand Down Expand Up @@ -591,6 +585,11 @@ jobs:
- name: Print Haxe version
run: haxe -version

- name: Enable debugging for lldb
run: |
sudo DevToolsSecurity -enable || true
lldb --version

- name: Setup haxelib
run: |
set -ex
Expand Down
85 changes: 42 additions & 43 deletions src/generators/cpp/cppRetyper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,47 @@ let native_field_name_remap field =
| None ->
keyword_remap field.cf_name

(* Generate a no op tfunc for an abstract function. *)
(* This allows it to go through the rest of the generators with no special cases. *)
(* We can't implement abstract functions as pure virtual due to cppia needing to construct the class. *)
let abstract_tfunc basic field =
let args, ret =
match follow_with_coro field.cf_type with
| Coro (t, r) -> Common.expand_coro_type basic t r
| NotCoro TFun (t, r) -> t, r
| _ -> cpp_abort InternalError field.cf_pos
in

(* We need to fetch the default values for abstract functions from the @:Value meta *)
let get_default_value name =
try
match Meta.get Meta.Value field.cf_meta with
| _, [ (EObjectDecl decls, _) ], _ ->
Some
(decls
|> List.find (fun ((n, _, _), _) -> n = name)
|> snd
|> type_constant_value basic)
| _ -> None
with Not_found -> None
in

let map_arg (name, _, t) =
( (alloc_var VGenerated name t null_pos), (get_default_value name) ) in
let expr =
match follow ret with
| TAbstract ({ a_path = ([], "Void") }, _) ->
{ eexpr = TReturn None; etype = ret; epos = null_pos }
| _ ->
let zero_val = Some { eexpr = TConst (TInt Int32.zero); etype = ret; epos = null_pos } in
{ eexpr = TReturn zero_val; etype = ret; epos = null_pos } in

{
tf_args = args |> List.map map_arg;
tf_type = ret;
tf_expr = expr;
}

let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
let scriptable = Gctx.defined ctx.ctx_common Define.Scriptable in
let cpp_type_of = cpp_type_of ctx.ctx_common.basic in
Expand Down Expand Up @@ -1818,49 +1859,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
| Method (MethNormal | MethInline), Some { eexpr = TFunction func } ->
Some (create_function field func)
| Method MethNormal, _ when has_class_field_flag field CfAbstract ->
(* We need to fetch the default values for abstract functions from the @:Value meta *)
let abstract_tfunc =
let args, ret =
match follow_with_coro field.cf_type with
| Coro (t, r) -> Common.expand_coro_type ctx.ctx_common.basic t r
| NotCoro TFun (t, r) -> t, r
| _ -> cpp_abort InternalError field.cf_pos
in

let get_default_value name =
try
match Meta.get Meta.Value field.cf_meta with
| _, [ (EObjectDecl decls, _) ], _ ->
Some
(decls
|> List.find (fun ((n, _, _), _) -> n = name)
|> snd
|> type_constant_value ctx.ctx_common.basic)
| _ -> None
with Not_found -> None
in

(* Generate a no op tfunc for our abstract *)
(* This allows it to go through the rest of the generator with no special cases *)
(* We can't implement abstract functions as pure virtual due to cppia needing to construct the class *)
let map_arg (name, _, t) =
( (alloc_var VGenerated name t null_pos), (get_default_value name) ) in
let expr =
match follow ret with
| TAbstract ({ a_path = ([], "Void") }, _) ->
{ eexpr = TReturn None; etype = ret; epos = null_pos }
| _ ->
let zero_val = Some { eexpr = TConst (TInt Int32.zero); etype = ret; epos = null_pos } in
{ eexpr = TReturn zero_val; etype = ret; epos = null_pos } in

{
tf_args = args |> List.map map_arg;
tf_type = ret;
tf_expr = expr;
}
in

Some (create_function field abstract_tfunc)
Some (create_function field (abstract_tfunc ctx.ctx_common.basic field))
| _ ->
None
else
Expand Down
12 changes: 10 additions & 2 deletions src/generators/cpp/gen/cppCppia.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,14 @@ let generate_script_class common_ctx script class_def =
+ match TClass.get_cl_init class_def with Some _ -> 1 | _ -> 0)
^ "\n");

(* Methods without an expression (i.e. abstract ones) still need a real function, because hxcpp
reads every class method as a ScriptCallable and the JIT accesses it as one *)
let method_expression field =
match field.cf_expr with
| None when not (has_class_flag class_def CInterface) ->
Some (mk (TFunction (CppRetyper.abstract_tfunc common_ctx.Gctx.basic field)) field.cf_type field.cf_pos)
| e -> e
in
let generate_field isStatic field =
match (field.cf_kind, follow field.cf_type) with
| Var { v_read = AccInline; v_write = AccNever }, _ ->
Expand All @@ -1854,15 +1862,15 @@ let generate_script_class common_ctx script class_def =
| Method MethDynamic, TFun (args, ret) ->
script#func isStatic true field.cf_name ret args
(has_class_flag class_def CInterface)
field.cf_expr field.cf_pos
(method_expression field) field.cf_pos
| Method _, TFun (args, ret) when field.cf_name = "new" ->
script#func true false "new"
(TInst (class_def, []))
args false field.cf_expr field.cf_pos
| Method _, TFun (args, ret) ->
script#func isStatic false field.cf_name ret args
(has_class_flag class_def CInterface)
field.cf_expr field.cf_pos
(method_expression field) field.cf_pos
| Method _, _ ->
print_endline
("Unknown method type "
Expand Down
83 changes: 83 additions & 0 deletions tests/runci/targets/Cpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runci.targets;

import sys.FileSystem;
import runci.System.*;
import runci.System.CommandFailure;
import runci.Config.*;

class Cpp {
Expand Down Expand Up @@ -67,6 +68,86 @@ class Cpp {
Sys.putEnv("CPPIA_IS_JIT", null);
}

static final LLDB_ITERATIONS = 30;
static final GMALLOC_ITERATIONS = 3;
static final GMALLOC = "/usr/lib/libgmalloc.dylib";

/**
DEBUG(mac-arm64-cppia-jit): runs the cppia JIT under lldb repeatedly, to catch the
intermittent `hxSehException` crash on macOS arm64 that happens while the module
is being JIT-compiled. lldb stops on the EXC_BAD_ACCESS before hxcpp's SIGSEGV
handler converts it into an `hxSehException`, so we get a native backtrace.

The crash was caught once in `ScriptCallable::compile` with a garbage `body`
pointer (0x500000000), so this first runs a few times with Guard Malloc (which
faults on the first use-after-free/overflow), then repeatedly with MallocScribble
and malloc stack logging so stale memory is recognisable and its history printable.
**/
static function debugCppiaJitWithLldb(script:String, host:String) {
if (FileSystem.exists(GMALLOC)) {
runCppiaJitUnderLldb(script, host, "guard malloc", GMALLOC_ITERATIONS, [
'DYLD_INSERT_LIBRARIES=$GMALLOC'
]);
} else {
infoMsg('$GMALLOC not found, skipping guard malloc runs');
}
runCppiaJitUnderLldb(script, host, "malloc scribble", LLDB_ITERATIONS, [
"MallocScribble=1",
"MallocPreScribble=1",
"MallocStackLogging=1"
]);
}

static function runCppiaJitUnderLldb(script:String, host:String, label:String, iterations:Int, env:Array<String>) {
final lldbArgs = [
"--batch",
// lldb disables ASLR by default, and the crash didn't reproduce in 30 runs that way
"-O", "settings set target.disable-aslr false",
"-O", "settings set target.env-vars " + env.join(" "),
"-o", "run",
// these only run if the process stops because of a crash
"-k", "thread backtrace all",
"-k", "register read",
"-k", "image lookup --address $pc",
"-k", "disassemble --pc --count 24",
"-k", "frame variable --ptr-depth 1 *this",
"-k", "memory read --size 8 --format x --count 48 this",
"-k", "up",
"-k", "frame variable --ptr-depth 1 *this",
"-k", "up",
"-k", "frame variable name",
"-k", "frame select 0",
"-k", "command script import lldb.macosx.heap",
"-k", "malloc_info --stack-history this",
"-k", "quit 1",
"--", host, script, "-jit"
];
final cmd = "lldb " + [for (a in lldbArgs) "'" + a.split("'").join("'\\''") + "'"].join(" ") + " 2>&1";
final exitedRegex = ~/Process \d+ exited with status = (-?\d+)/;
Sys.putEnv("CPPIA_IS_JIT", "1");
for (i in 1...iterations + 1) {
infoMsg('lldb cppia jit run ($label) $i/$iterations: $cmd');
final t = haxe.Timer.stamp();
final p = new sys.io.Process("/bin/sh", ["-c", cmd]);
final output = p.stdout.readAll().toString();
final exitCode = p.exitCode();
p.close();
final dt = Math.round(haxe.Timer.stamp() - t);
final crashed = output.indexOf("stop reason =") != -1;
final exited = exitedRegex.match(output);
if (crashed || !exited || exitedRegex.matched(1) != "0") {
Sys.println(output);
failMsg('lldb cppia jit run ($label) $i failed in ${dt}s (lldb exit code $exitCode, crashed: $crashed)');
Sys.putEnv("CPPIA_IS_JIT", null);
throw new CommandFailure(exitCode == 0 ? 1 : exitCode);
}
if (i == 1)
Sys.println(output);
successMsg('lldb cppia jit run ($label) $i passed in ${dt}s');
}
Sys.putEnv("CPPIA_IS_JIT", null);
}

static public function run(args:Array<String>, testCompiled:Bool, testCppia:Bool) {
Cpp.testCppia = testCppia;

Expand Down Expand Up @@ -104,6 +185,8 @@ class Cpp {

runCommand("haxe", ["compile-cppia-host.hxml"].concat(args));
runCommand("haxe", ["compile-cppia.hxml"].concat(args));
if (systemName == "Mac" && System.arch == Arm64)
debugCppiaJitWithLldb("bin/unit.cppia", FileSystem.fullPath("bin/cppia/Host-debug"));
runCppia("bin/unit.cppia", FileSystem.fullPath("bin/cppia/Host-debug"));

changeDirectory(sysDir);
Expand Down
Loading