diff --git a/src/lib/lazy-init.hpp b/src/lib/lazy-init.hpp index d5ee4b1bb..9aa7badca 100644 --- a/src/lib/lazy-init.hpp +++ b/src/lib/lazy-init.hpp @@ -117,7 +117,7 @@ namespace lib { char* anchorAddr = reinterpret_cast (unConst(anchor)); char* adjusted = anchorAddr + offset; void* rawTarget = reinterpret_cast (adjusted); - return static_cast (adjusted); + return static_cast (rawTarget); } @@ -129,7 +129,7 @@ namespace lib { * exploited as a trick to allow for automatic late initialisation * in a situation, were a functor needs to capture references. */ - const ptrdiff_t PAYLOAD_OFFSET = + const ptrdiff_t FUNCTOR_PAYLOAD_OFFSET = []{ size_t slot{42}; std::function probe = [slot]{ return RawAddr(&slot); }; @@ -272,6 +272,14 @@ namespace lib { } + template + void + installEmptyInitialiser() + { + pendingInit_.reset (new HeapStorage{emptyInitialiser()}); + } + + private: template DelegateType emptyInitialiser() @@ -283,40 +291,6 @@ namespace lib { return disabledFunctor; }); } - template - void - installEmptyInitialiser() - { - pendingInit_.reset (new HeapStorage{emptyInitialiser()}); - } - - - private: - template - DelegateType - buildInitialiserDelegate (std::function& targetFunctor, INI&& initialiser) - { - using TargetFun = std::function; - return DelegateType{ - [performInit = forward (initialiser) - ,targetOffset = captureRawAddrOffset (this, &targetFunctor)] - (RawAddr location) -> TargetFun& - {// apply known offset backwards to find current location of the host object - TargetFun* target = relocate (location, -PAYLOAD_OFFSET); - LazyInit* self = relocate (target, -targetOffset); - REQUIRE (self); - performInit (self); - self->pendingInit_.reset(); - return *target; - }}; - } - - template - DelegateType* - getPointerToDelegate(HeapStorage& buffer) - { - return reinterpret_cast*> (&buffer); - } template PendingInit @@ -329,6 +303,32 @@ namespace lib { targetFunctor = TrojanFun::generateTrap (getPointerToDelegate (*storageHandle)); return storageHandle; } + + template + DelegateType* + getPointerToDelegate(HeapStorage& buffer) + { + return reinterpret_cast*> (&buffer); + } + + template + DelegateType + buildInitialiserDelegate (std::function& targetFunctor, INI&& initialiser) + { + using TargetFun = std::function; + return DelegateType{ + [performInit = forward (initialiser) + ,targetOffset = captureRawAddrOffset (this, &targetFunctor)] + (RawAddr location) -> TargetFun& + {// apply known offset backwards to find current location of the host object + TargetFun* target = relocate (location, -FUNCTOR_PAYLOAD_OFFSET); + LazyInit* self = relocate (target, -targetOffset); + REQUIRE (self); + performInit (self); + self->pendingInit_.reset(); + return *target; + }}; + } }; diff --git a/tests/library/lazy-init-test.cpp b/tests/library/lazy-init-test.cpp index 8702deec8..222635386 100644 --- a/tests/library/lazy-init-test.cpp +++ b/tests/library/lazy-init-test.cpp @@ -73,22 +73,11 @@ namespace test{ void run (Arg) { - simpleUse(); - verify_trojanLambda(); verify_inlineStorage(); -// verify_numerics(); -// verify_adaptMapping(); -// verify_dynamicChange(); - } - - - - /** @test demonstrate a basic usage scenario - */ - void - simpleUse() - { + verify_TargetRelocation(); + verify_triggerMechanism(); + verify_lazyInitialisation(); } @@ -203,6 +192,112 @@ namespace test{ + /** @test verify navigating an object structure + * by applying known offsets consecutively + * from a starting point within an remote instance + * @remark in the real usage scenario, we know _only_ the offset + * and and attempt to find home without knowing the layout. + */ + void + verify_TargetRelocation() + { + struct Nested + { + int unrelated{rand()}; + int anchor{rand()}; + }; + struct Demo + { + Nested nested; + virtual ~Demo(){ }; + virtual RawAddr peek() + { + return &nested.anchor; + } + }; + + // find out generic offset... + const ptrdiff_t offNested = []{ + Nested probe; + return captureRawAddrOffset(&probe, &probe.anchor); + }(); + Demo here; + // find out actual offset in existing object + const ptrdiff_t offBase = captureRawAddrOffset(&here, &here.nested); + + CHECK (offBase > 0); + CHECK (offNested > 0); + + // create a copy far far away... + auto farAway = std::make_unique (here); + + // reconstruct base address from starting point + RawAddr startPoint = farAway->peek(); + Nested* farNested = relocate(startPoint, -offNested); + CHECK (here.nested.unrelated == farNested->unrelated); + + Demo* farSelf = relocate (farNested, -offBase); + CHECK (here.nested.anchor == farSelf->nested.anchor); + CHECK (isSameObject (*farSelf, *farAway)); + } + + + + /** @test demonstrate the trigger mechanism in isolation + */ + void + verify_triggerMechanism() + { + using Fun = std::function; + Fun theFun; + CHECK (not theFun); + + int report{0}; + auto delegate = [&report](RawAddr insideFun) -> Fun& + { + auto realFun = [&report](int num) + { + report += num; + return num + 23.55f; + }; + Fun& target = *relocate(insideFun, -FUNCTOR_PAYLOAD_OFFSET); + report = -42; // as proof that the init-delegate was invoked + target = realFun; + return target; + }; + CHECK (not theFun); + // install the init-»trap« + theFun = TrojanFun::generateTrap (&delegate); + CHECK (theFun); + CHECK (0 == report); + + // invoke function + int feed{1+rand()%100}; + float res = theFun (feed); + + // delegate *and* realFun were invoked + CHECK (feed == report + 42); + CHECK (res = feed -42 +23.55f); + + // again... + report = 0; + feed = -1-rand()%20; + res = theFun (feed); + + // this time the delegate was *not* invoked, + // only the installed realFun + CHECK (feed == report); + CHECK (res = feed + 23.55f); + } + + + + /** @test demonstrate a basic usage scenario + */ + void + verify_lazyInitialisation() + { + } }; diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 6f7705ada..9ce86568f 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -97283,8 +97283,8 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + @@ -97304,11 +97304,11 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + - - + + @@ -97393,7 +97393,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- + @@ -97412,6 +97412,44 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + +
+ + + + + + + + +

+ OpaqueHolder mißbraucht +

+ + +
+ + + + + + + + + + + + + + + + + + + +