May 27, 2016
On 2016-05-27 15:12, Atila Neves wrote:

> I get an SSL warning for that link.

Hmm, here's the code inline:

module red;

import std.stdio;

import core.sys.posix.sys.mman : mprotect, PROT_NONE, PROT_READ, PROT_WRITE, PROT_EXEC;
import core.stdc.errno : errno;
import core.stdc.string : memmove, memcpy;

extern (C) int getpagesize();

void procWrite(void* pos, const ref ubyte[6] data)
{
    void* addr = pos;
    size_t page = getpagesize();
    addr -= (cast(size_t)addr) % page;

    if(0 != mprotect(addr, page, PROT_READ | PROT_WRITE | PROT_EXEC)){
        return; // error
    }
    memmove(pos, data.ptr, data.length);
    mprotect(pos, page, PROT_READ | PROT_EXEC);
}

ubyte[6] redirect(void* from, void* to)
{

    // compute ASM
    ubyte[6] cmd;

    // // sanity checks
    if(((from <= to) && (to <= from+5))
        || ((to <= from) && (from <= to+5)))
    {
        return cmd;
        //throw new Exception("illegal source-destination combination");
    }

    cmd[0] = 0xE9; // jmp
    cmd[5] = 0xC3; // retn

    size_t new_dest = cast(size_t) to;
    new_dest = new_dest - (cast(size_t)from + 5);
    int offset = cast(int)cast(ptrdiff_t)new_dest;

    cmd[1 .. 1 + offset.sizeof] = (cast(ubyte*)&offset)[0 .. offset.sizeof];

    // // save original
    ubyte[6] original = (cast(ubyte*)from)[0 .. cmd.length];

    // write asm
    procWrite(from, cmd);
    return original;
    // return null;
}

void restoreRedirection(void* addr, const ref ubyte[6] data)
{
    procWrite(addr, data);
}

void a()
{
    writeln("a");
}

void b()
{
    writeln("b");
}

void bar(Foo foo)
{
    writeln("bar");
}

class Foo
{
    void foo()
    {
        writeln("foo");
    }
}

void replaceMethod()
{
    auto f = new Foo;
    auto f2 = new Foo;
    auto vtbl = (cast(void**)f.__vptr)[0 .. 6].dup;
    vtbl[5] = &bar;
    f.__vptr = cast(immutable(void*)*) vtbl.ptr;
    f.foo();
    f2.foo();
}

void replaceFunction()
{
    a();
    auto r = redirect(&a, &b);
    a();
    restoreRedirection(&a, r);
    a();
}

void main()
{
    replaceMethod();
    replaceFunction();
}

-- 
/Jacob Carlborg
May 29, 2016
On Friday, 27 May 2016 at 18:49:12 UTC, Jacob Carlborg wrote:
> On 2016-05-27 15:12, Atila Neves wrote:
>
>> [...]
>
> Hmm, here's the code inline:
>
> module red;
>
> [...]

Yep, that's definitely crazier than I what I posted. Nifty!

Atila
May 29, 2016
On Thursday, 26 May 2016 at 09:40:26 UTC, Atila Neves wrote:
> On Wednesday, 25 May 2016 at 21:52:37 UTC, Alex Parrill wrote:
>> Have you looked at std.typecons.AutoImplement at all?
>
> I'd never seen it before, thanks!

 I recall adding on a wishlist somewhere that every week or something that a video or article is made about these things. Preferably a video. It could be them talking about design decisions of certain features in D, or more likely exposure to an entire module in D that covers the basics and when you'd use each function/feature/template and why. I'm not talking you need an hour long video or something, maybe like the introduction to the STL format, or even rapid firing and showing quick premade examples/use-cases of why it was used (for optimal or problem solving reasons). The video could be say 10 minutes long.

 There's so much in the library I would have to go through and try to remember and I know very little of it. Exposing myself to it feels like a chore sometimes, and often I don't.
May 29, 2016
On Sunday, 29 May 2016 at 22:20:05 UTC, Era Scarecrow wrote:
> On Thursday, 26 May 2016 at 09:40:26 UTC, Atila Neves wrote:
>> On Wednesday, 25 May 2016 at 21:52:37 UTC, Alex Parrill wrote:
>>> Have you looked at std.typecons.AutoImplement at all?
>>
>> I'd never seen it before, thanks!
>
>  I recall adding on a wishlist somewhere that every week or something that a video or article is made about these things. Preferably a video. It could be them talking about design decisions of certain features in D, or more likely exposure to an entire module in D that covers the basics and when you'd use each function/feature/template and why. I'm not talking you need an hour long video or something, maybe like the introduction to the STL format, or even rapid firing and showing quick premade examples/use-cases of why it was used (for optimal or problem solving reasons). The video could be say 10 minutes long.
>
>  There's so much in the library I would have to go through and try to remember and I know very little of it. Exposing myself to it feels like a chore sometimes, and often I don't.

Do you know about Adam's This Week in D?
http://arsdnet.net/this-week-in-d/2016-may-22.html

Vladimir also tries to maintain his Planet of D
http://planet.dsource.org/

But the main problem is just time (videos even take more time), maybe we should have a user-based newsletter or forum where everyone can submit articles.
May 30, 2016
On 2016-05-29 22:03, Atila Neves wrote:

> Yep, that's definitely crazier than I what I posted. Nifty!

Should add that the code for replacing functions is from flectioned [1].

[1] http://dsource.org/projects/flectioned

-- 
/Jacob Carlborg
1 2
Next ›   Last »