Skip to content

Commit 66ff681

Browse files
Merge the fixes for CI on 42c1963
The fuzz comparison stops on a value shared at every level instead of timing out, and a cppia class's __init__ body is kept as the expression it is rather than cast to a function, which UBSan reported.
2 parents 42c1963 + ac79682 commit 66ff681

5 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎src/hx/cppia/Cppia.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ struct CppiaFunction : public CppiaOwned<CppiaFunction>
514514
std::string name;
515515
std::vector<ArgInfo> args;
516516
ScriptCallable *funExpr;
517+
// The body as it came. A class's __init__ is an expression of any kind,
518+
// which is not a ScriptCallable, and has no funExpr
519+
CppiaExpr *body;
517520

518521
CppiaFunction(CppiaModule *inCppia,bool inIsStatic,bool inIsDynamic);
519522

‎src/hx/cppia/CppiaClasses.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ void CppiaClassInfo::linkTypes()
14191419
}
14201420
else if (staticFunctions[i]->name == "__init__")
14211421
{
1422-
initExpr = staticFunctions[i]->funExpr;
1422+
initExpr = staticFunctions[i]->body;
14231423
staticFunctions.erase( staticFunctions.begin() + i);
14241424
}
14251425
else

‎src/hx/cppia/CppiaFunction.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ ScriptCallable *getClosureFunction(hx::Object *inObj)
11431143
// --- CppiaFunction ----
11441144

11451145
CppiaFunction::CppiaFunction(CppiaModule *inCppia,bool inIsStatic,bool inIsDynamic) :
1146-
cppia(*inCppia), isStatic(inIsStatic), isDynamic(inIsDynamic), funExpr(0)
1146+
cppia(*inCppia), isStatic(inIsStatic), isDynamic(inIsDynamic), funExpr(0), body(0)
11471147
{
11481148
linked = false;
11491149
vtableSlot = -1;
@@ -1171,10 +1171,13 @@ void CppiaFunction::load(CppiaStream &stream,bool inExpectBody)
11711171
// A body is a function, or for an abstract function a bare RETURN. A
11721172
// class's __init__ is an expression of any kind, which the class runs
11731173
// as it is. A malformed file can give any other expression
1174-
CppiaExpr *body = createCppiaExpr(stream);
1175-
if (name!="__init__" && !dynamic_cast<ScriptCallable *>(body) && std::string(body->getName())!="RetVal")
1174+
body = createCppiaExpr(stream);
1175+
if (name=="__init__")
1176+
funExpr = 0;
1177+
else if (!dynamic_cast<ScriptCallable *>(body) && std::string(body->getName())!="RetVal")
11761178
throw "Function body is not a function";
1177-
funExpr = (ScriptCallable *)body;
1179+
else
1180+
funExpr = (ScriptCallable *)body;
11781181
}
11791182
stream.module->creatingFunction = 0;
11801183
}

‎test/fuzz/Fuzz.hx‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ class Fuzz {
367367
var v2 = s1 == null ? null : attempt(() -> haxe.Unserializer.run(s1));
368368
var s2:String = v2 == null ? null : attempt(() -> serialize(v2));
369369
var v3 = s2 == null ? null : attempt(() -> haxe.Unserializer.run(s2));
370-
if (v3 != null && canon(v2) != canon(v3))
370+
if (v3 != null && differ(v2, v3))
371371
fail('serialized $s1 then $s2');
372372
}
373373

@@ -397,7 +397,7 @@ class Fuzz {
397397
if (v1 == null)
398398
return;
399399
var s2 = haxe.Json.stringify(v1);
400-
if (canon(v1) != canon(haxe.Json.parse(s2)))
400+
if (differ(v1, haxe.Json.parse(s2)))
401401
fail('printed $s1 then $s2');
402402
}
403403

@@ -547,12 +547,34 @@ class Fuzz {
547547
haxe.crypto.Md5.encode(s);
548548
}
549549

550+
/**
551+
Whether two values have different texts from canon(). A value that
552+
takes more than CANON_WORK steps is not compared: the serializer's
553+
references can share a value at every level, as "ay1:aoy0:r0gr0h"
554+
does, and the depth cut-off alone leaves 2^32 steps.
555+
**/
556+
static function differ(a:Dynamic, b:Dynamic):Bool {
557+
try {
558+
canonLeft = CANON_WORK;
559+
var ta = canon(a);
560+
canonLeft = CANON_WORK;
561+
return ta != canon(b);
562+
} catch (e:CanonTooBig) {
563+
return false;
564+
}
565+
}
566+
567+
static inline var CANON_WORK = 100000;
568+
static var canonLeft = 0;
569+
550570
/**
551571
A text for a value that does not depend on the order fields and map
552572
keys come out in. Shared and cyclic values from the serializer's
553573
references are cut off below a depth.
554574
**/
555575
static function canon(v:Dynamic, depth = 0):String {
576+
if (--canonLeft < 0)
577+
throw new CanonTooBig();
556578
if (depth > 32)
557579
return "...";
558580
function sorted(items:Array<String>) {
@@ -684,3 +706,8 @@ private class Reader {
684706
return v.getDouble(0);
685707
}
686708
}
709+
710+
/** Thrown by Fuzz.canon for a value too big to compare. **/
711+
private class CanonTooBig {
712+
public function new() {}
713+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ay1:aoy0:r0gr0hInfi,

0 commit comments

Comments
 (0)