2010-02-27 23:57:03 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2012-02-17 12:48:11 -08:00
|
|
|
#include <stdlib.h>
|
2008-06-08 14:39:49 -07:00
|
|
|
#include <stdio.h>
|
2008-02-27 13:01:30 -08:00
|
|
|
#include <sys/types.h>
|
2015-11-11 06:04:37 -08:00
|
|
|
#include <unistd.h>
|
2019-08-05 05:47:19 -07:00
|
|
|
#include <errno.h>
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2015-11-11 06:04:37 -08:00
|
|
|
#include <io.h>
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2012-02-17 12:48:11 -08:00
|
|
|
#include "evil_private.h"
|
2008-04-26 09:27:46 -07:00
|
|
|
|
2019-08-05 05:47:19 -07:00
|
|
|
/*
|
|
|
|
* Possible values
|
|
|
|
* PAGE_EXECUTE_READ (equivalent to PAGE_EXECUTE_WRITECOPY)
|
|
|
|
* PAGE_EXECUTE_READWRITE
|
|
|
|
* PAGE_READONLY (equivalent to PAGE_WRITECOPY)
|
|
|
|
* PAGE_READWRITE
|
|
|
|
*/
|
|
|
|
static DWORD
|
|
|
|
_evil_mmap_protection_get(int prot)
|
|
|
|
{
|
|
|
|
if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
|
|
|
|
return 0xffffffff;
|
|
|
|
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
{
|
|
|
|
if (prot & PROT_EXEC)
|
|
|
|
return PAGE_EXECUTE_READWRITE;
|
|
|
|
else
|
|
|
|
return PAGE_READWRITE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (prot & PROT_EXEC)
|
|
|
|
return PAGE_EXECUTE_READ;
|
|
|
|
else
|
|
|
|
return PAGE_READONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2008-06-08 14:39:49 -07:00
|
|
|
/***** API *****/
|
|
|
|
|
|
|
|
|
evil: Rename EAPI macro to EVIL_API in Evil library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
```__attribute__ ((visibility ("default")))``` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
```
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
```__atttribute__((visibility("default")))```
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: raster, vtorri, jptiz, lucas, woohyun
Reviewed By: vtorri, jptiz
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
|
|
|
EVIL_API void *
|
2012-10-05 13:09:47 -07:00
|
|
|
mmap(void *addr EVIL_UNUSED,
|
2008-02-27 13:01:30 -08:00
|
|
|
size_t len,
|
|
|
|
int prot,
|
|
|
|
int flags,
|
|
|
|
int fd,
|
|
|
|
off_t offset)
|
|
|
|
{
|
2014-07-13 01:59:40 -07:00
|
|
|
HANDLE fm;
|
2019-08-05 05:47:19 -07:00
|
|
|
DWORD protect;
|
2014-07-13 01:59:40 -07:00
|
|
|
DWORD acs = 0;
|
|
|
|
HANDLE handle;
|
|
|
|
void *data;
|
2019-09-12 07:01:33 -07:00
|
|
|
DWORD low;
|
|
|
|
DWORD high;
|
2008-06-08 14:39:49 -07:00
|
|
|
|
2019-08-05 05:47:19 -07:00
|
|
|
/* get protection */
|
|
|
|
protect = _evil_mmap_protection_get(prot);
|
|
|
|
if (protect == 0xffffffff)
|
2014-07-13 01:59:40 -07:00
|
|
|
return MAP_FAILED;
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2019-08-05 05:47:19 -07:00
|
|
|
/* check if the mapping is backed by a file or not */
|
|
|
|
if (fd == -1)
|
2008-02-27 13:01:30 -08:00
|
|
|
{
|
2019-08-05 05:47:19 -07:00
|
|
|
/* shared memory */
|
|
|
|
if (!(flags & MAP_ANON) || offset)
|
|
|
|
return MAP_FAILED;
|
2008-02-27 13:01:30 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-05 05:47:19 -07:00
|
|
|
if (flags & MAP_ANON)
|
|
|
|
return MAP_FAILED;
|
2014-07-13 01:59:40 -07:00
|
|
|
}
|
2008-06-08 14:39:49 -07:00
|
|
|
|
2019-08-05 05:47:19 -07:00
|
|
|
if (fd == -1)
|
|
|
|
handle = INVALID_HANDLE_VALUE;
|
|
|
|
else
|
2014-07-13 01:59:40 -07:00
|
|
|
{
|
2019-08-05 05:47:19 -07:00
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
if ((errno == EBADF) && (handle == INVALID_HANDLE_VALUE))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "[Evil] [mmap] _get_osfhandle failed\n");
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
2014-07-13 01:59:40 -07:00
|
|
|
}
|
2008-06-08 14:39:49 -07:00
|
|
|
|
2019-09-12 07:01:33 -07:00
|
|
|
#ifdef _WIN64
|
2020-12-09 12:11:42 -08:00
|
|
|
high = (DWORD)((len >> 32) & 0x00000000ffffffffULL);
|
|
|
|
low = (DWORD)(len & 0x00000000ffffffffULL);
|
2019-09-12 07:01:33 -07:00
|
|
|
#else
|
|
|
|
high = 0L;
|
|
|
|
low = len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fm = CreateFileMapping(handle, NULL, protect, high, low, NULL);
|
2014-07-13 01:59:40 -07:00
|
|
|
if (!fm)
|
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
fprintf(stderr, "[Evil] [mmap] CreateFileMapping failed: %s\n",
|
|
|
|
evil_last_error_get());
|
2014-07-13 01:59:40 -07:00
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2019-08-05 05:47:19 -07:00
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
acs = FILE_MAP_WRITE;
|
2014-07-13 01:59:40 -07:00
|
|
|
else
|
2019-08-05 05:47:19 -07:00
|
|
|
acs = FILE_MAP_READ;
|
|
|
|
if (prot & PROT_EXEC)
|
|
|
|
acs |= FILE_MAP_EXECUTE;
|
|
|
|
if (prot & MAP_PRIVATE)
|
|
|
|
acs |= FILE_MAP_COPY;
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2014-07-13 01:59:40 -07:00
|
|
|
data = MapViewOfFile(fm,
|
|
|
|
acs,
|
|
|
|
offset & 0xffff0000,
|
|
|
|
offset & 0x0000ffff,
|
|
|
|
len);
|
|
|
|
CloseHandle(fm);
|
2008-06-08 14:39:49 -07:00
|
|
|
|
2014-07-13 01:59:40 -07:00
|
|
|
if (!data)
|
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
fprintf(stderr, "[Evil] [mmap] MapViewOfFile failed: %s\n",
|
|
|
|
evil_last_error_get());
|
2014-07-13 01:59:40 -07:00
|
|
|
return MAP_FAILED;
|
2008-06-09 11:35:49 -07:00
|
|
|
}
|
2014-07-13 01:59:40 -07:00
|
|
|
|
|
|
|
return data;
|
2008-02-27 13:01:30 -08:00
|
|
|
}
|
|
|
|
|
evil: Rename EAPI macro to EVIL_API in Evil library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
```__attribute__ ((visibility ("default")))``` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
```
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
```__atttribute__((visibility("default")))```
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: raster, vtorri, jptiz, lucas, woohyun
Reviewed By: vtorri, jptiz
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
|
|
|
EVIL_API int
|
2008-02-27 13:01:30 -08:00
|
|
|
munmap(void *addr,
|
2012-10-05 13:09:47 -07:00
|
|
|
size_t len EVIL_UNUSED)
|
2008-02-27 13:01:30 -08:00
|
|
|
{
|
2014-07-13 01:59:40 -07:00
|
|
|
BOOL res;
|
2008-02-27 13:01:30 -08:00
|
|
|
|
2014-07-13 01:59:40 -07:00
|
|
|
res = UnmapViewOfFile(addr);
|
|
|
|
if (!res)
|
2017-12-10 22:06:02 -08:00
|
|
|
fprintf(stderr, "[Evil] [munmap] UnmapViewOfFile failed: %s\n",
|
|
|
|
evil_last_error_get());
|
2008-06-08 14:39:49 -07:00
|
|
|
|
2014-07-13 01:59:40 -07:00
|
|
|
return (res == 0) ? -1 : 0;
|
2008-02-27 13:01:30 -08:00
|
|
|
}
|
2019-08-05 05:47:19 -07:00
|
|
|
|
evil: Rename EAPI macro to EVIL_API in Evil library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
```__attribute__ ((visibility ("default")))``` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
```
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
```__atttribute__((visibility("default")))```
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: raster, vtorri, jptiz, lucas, woohyun
Reviewed By: vtorri, jptiz
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
|
|
|
EVIL_API int
|
2019-08-05 05:47:19 -07:00
|
|
|
mprotect(void *addr, size_t len, int prot)
|
|
|
|
{
|
|
|
|
DWORD old;
|
|
|
|
return VirtualProtect(addr, len, _evil_mmap_protection_get(prot), &old) ? 0 : -1;
|
|
|
|
}
|