JustasMasiulis / inline_syscall

Inline syscalls made easy for windows on clang
Apache License 2.0
638 stars 87 forks source link

mingw usage? #2

Closed phra closed 4 years ago

phra commented 4 years ago

hello,

i am trying to use the library with https://github.com/tpoechtrager/wclang in order to compile a PE from linux. i have tried with both clang-6 and clang-8 but i always get the following error:

$ ./x86_64-w64-mingw32-clang++ -Wall --pedantic hello.cpp -o hello.exe
In file included from hello.cpp:8:
In file included from ./inline_syscall/include/in_memory_init.hpp:20:
In file included from ./inline_syscall/include/inline_syscall.hpp:103:
./inline_syscall/include/inline_syscall.inl:61:28: warning: inline variables are a C++17 extension [-Wc++17-extensions]
                "_sysc")]] inline static JM_INLINE_SYSCALL_ENTRY_TYPE entry{ Hash };
                           ^
hello.cpp:18:24: error: implicit instantiation of undefined template 'jm::syscall_function<long long (*)()>'
    NTSTATUS status  = INLINE_SYSCALL(NtAllocateVirtualMemory)((HANDLE)-1, &allocation, 0, &size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
                       ^
./inline_syscall/include/inline_syscall.hpp:26:5: note: expanded from macro 'INLINE_SYSCALL'
    INLINE_SYSCALL_MANUAL(               \
    ^
./inline_syscall/include/inline_syscall.hpp:44:5: note: expanded from macro 'INLINE_SYSCALL_MANUAL'
    ::jm::syscall_function<decltype(function_pointer)> { syscall_id }
    ^
./inline_syscall/include/inline_syscall.hpp:55:11: note: template is declared here
    class syscall_function;
          ^
1 warning and 1 error generated.

The source code of hello.cpp is the following:

#include <winternl.h>
#include <ntstatus.h>
#include <windows.h>
#include <iostream>

// This header contains the initialization function.
// If you already initialized, inline_syscall.hpp contains all you need.
#include "inline_syscall/include/in_memory_init.hpp"

int main() {
    FARPROC NtAllocateVirtualMemory = GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtAllocateVirtualMemory");
    // Needs to be called once at startup before INLINE_SYSCALL is used.
    jm::init_syscalls_list();

    // Usage of the main macro INLINE_SYSCALL
    void* allocation = nullptr;
    SIZE_T size      = 0x1000;
    NTSTATUS status  = INLINE_SYSCALL(NtAllocateVirtualMemory)((HANDLE)-1, &allocation, 0, &size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
    return 0;
}

any idea on how to fix the template error?

JustasMasiulis commented 4 years ago

// This header contains the initialization function. // If you already initialized, inline_syscall.hpp contains all you need.

include "inline_syscall/include/in_memory_init.hpp"

// I'd recommend https://github.com/processhacker/phnt for these definitions NTSTATUS NtAllocateVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protect);

int main() { // Needs to be called once at startup before INLINE_SYSCALL is used. jm::init_syscalls_list();

// Usage of the main macro INLINE_SYSCALL
void* allocation = nullptr;
SIZE_T size      = 0x1000;
NTSTATUS status  = INLINE_SYSCALL(NtAllocateVirtualMemory)((HANDLE)-1, &allocation, 0, &size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
return 0;

}

phra commented 4 years ago

it works, thank you @JustasMasiulis !

i have another question: is it possible to check at runtime if the syscall is available on the host operating system? eg:

if (!GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtQuerySecurityPolicy")) {
    // NtQuerySecurityPolicy is not available, os < win10
}
JustasMasiulis commented 4 years ago
template<std::uint32_t Hash>
JM_INLINE_SYSCALL_FORCEINLINE bool syscall_present_impl() {
    ::jm::detail::exports_directory exports(static_cast<const char*>(::jm::detail::ntdll_base()));
    for(auto i = exports.size();; --i) {
        if(::jm::hash(exports.name(i)) == Hash)
            return true;
    }
    return false;
}
#define SYSCALL_PRESENT(syscall_name) (syscall_present_impl<::jm::hash(#syscall_name)>())
phra commented 4 years ago

thanks again!