Code Monkey home page Code Monkey logo

member_thunk's People

Contributors

sylveon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

member_thunk's Issues

Recognize x64 __vectorcall

Also, should stop relying on default calling convention (specifying any calling convention keyword on x64/ARM/ARM64 will make it use the normal convention for that architecture)

Support noexcept function pointers

Currently, while noexcept member functions are supported, noexcept function pointers aren't.

struct Foo
{
	void bar() noexcept {}
};

Foo foo;
auto thunk = member_thunk::make<void(*)(std::size_t)>(&foo, &Foo::bar); // ok
auto thunk = member_thunk::make<void(*)(std::size_t) noexcept>(&foo, &Foo::bar); // associated constraints are not satisfied

Obviously, this should only enable when the called member function is noexcept too.

x86 support

What calling conventions should we support?

Support batching creation

It might be undesirable to create a thunk and leave it world-writable. We should allow a way to directly allocate a page, create thunks in it, and the set that page as execute-only once done.

The api would look something like this, not unlike a stack allocator

member_thunk::thunk_page page; // allocates a page in read-write

const auto thunk1 = page.create_thunk(this, &Foo::bar);
const auto thunk2 = page.create_thunk(this, &Foo::buz);

page.finalize(); // page set to read-execute

Once thunk_page::finalize is called, trying to create any more thunks will throw an exception.

thunk_page would have ownership of the page therefore being a move-only type, and frees the page once out of scope.

It should automatically grow if required. This can be done by either using a "linked-list" form, where a thunk_page owns the next thunk_page and forwards all calls to that once it's full, or by using VirtualAlloc to reserve an entire 64k of memory, and then committing pages as required, but would also introduce a theorical cap to the number of thunks (but does not throw away virtual memory, as the allocation granularity is 64k anyways).

This would also allow to optimize calls to FlushInstructionCache and SetProcessValidTargets to only run within finalize rather than for each new thunk, as well as skipping the alignment machinery (page is already 4k aligned, and everything is already 16 or 32 bytes, so consecutive stack allocations are already aligned)

This can introduce potential use-after-free (or use-before-executable) issues, so should be used with care.

Align functions on a 16 bytes boundary

https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf

Assembly/Compiler Coding Rule 12. (M impact, H generality) All branch targets should be 16-byte aligned

Should help the prefetcher, and also required for #7 because SetProcessValidCallTargets wants 16-byte alignment. (Should we pad thunk objects to the nearest multiple of 16 with nops too?)

HeapAlloc can't provide custom alignment, but VirtualAlloc2 (which can) allocates in blocks of 4KB, so is hugely innefficient (although it allows us to put back write protection once the thunk is generated). Can continue using HeapAlloc but pad allocations, or implement own heap on top of VirtualAlloc2 pages.

ARM should be fine with that alignment too

Remove workaround for bug

There currently is a workaround for microsoft/STL#1183 here:

// TODO: uncomment when https://github.com/microsoft/STL/wiki/Changelog#expected-in-vs-2019-168-preview-3 ships
#pragma warning(suppress: 4244)
std::fill(reinterpret_cast<volatile std::uint8_t*>(this), reinterpret_cast<volatile std::uint8_t*>(this + 1), /*static_cast<std::uint8_t>*/(0xCC));

It should be removed when possible

Make sure first argument is trivial

Wouldn't want to suppress any special C++ behaviors related to constructors or destructors. (does it even have the same ABI with a pointer-sized struct? might need is_scalar instead)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.