summaryrefslogtreecommitdiff
path: root/hooks.h
blob: c0da47e178bdd3e6c1b924c798acb81b082dff65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once

static void* (*_dlsym)(void*, const char*);
static void (*_glBlitFramebuffer)(GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLbitfield, GLenum);
static EGLBoolean (*_eglSwapBuffers)(EGLDisplay, EGLSurface);
static __eglMustCastToProperFunctionPointerType (*_eglGetProcAddress)(const char*);
static void (*_glXSwapBuffers)(Display*, GLXDrawable);
static __GLXextFuncPtr (*_glXGetProcAddress)(const GLubyte*);
static __GLXextFuncPtr (*_glXGetProcAddressARB)(const GLubyte*);
static snd_pcm_sframes_t (*_snd_pcm_writei)(snd_pcm_t*, const void*, snd_pcm_uframes_t);
static snd_pcm_sframes_t (*_snd_pcm_writen)(snd_pcm_t*, void**, snd_pcm_uframes_t);
static snd_pcm_sframes_t (*_snd_pcm_mmap_writei)(snd_pcm_t*, const void*, snd_pcm_uframes_t);
static snd_pcm_sframes_t (*_snd_pcm_mmap_writen)(snd_pcm_t*, void**, snd_pcm_uframes_t);
static int (*_clock_gettime)(clockid_t, struct timespec*);
static void* store_real_symbol_and_return_fake_symbol(const char*, void*);
static void hook_function(void**, const char*, const bool);

#define HOOK(x) hook_function((void**)&_##x, #x, false)

void
glBlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter)
{
   HOOK(glBlitFramebuffer);
   const GLint a[] = { srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1 };
   memcpy(LAST_FRAMEBUFFER_BLIT, a, sizeof(a));
   _glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
}

EGLBoolean
eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
{
   HOOK(eglSwapBuffers);
   swap_buffers();
   return _eglSwapBuffers(dpy, surface);
}

__eglMustCastToProperFunctionPointerType
eglGetProcAddress(const char *procname)
{
   HOOK(eglGetProcAddress);
   return (_eglGetProcAddress ? store_real_symbol_and_return_fake_symbol(procname, _eglGetProcAddress(procname)) : NULL);
}

void
glXSwapBuffers(Display *dpy, GLXDrawable drawable)
{
   HOOK(glXSwapBuffers);
   swap_buffers();
   _glXSwapBuffers(dpy, drawable);
}

__GLXextFuncPtr
glXGetProcAddressARB(const GLubyte *procname)
{
   HOOK(glXGetProcAddressARB);
   return (_glXGetProcAddressARB ? store_real_symbol_and_return_fake_symbol((const char*)procname, _glXGetProcAddressARB(procname)) : NULL);
}

__GLXextFuncPtr
glXGetProcAddress(const GLubyte *procname)
{
   HOOK(glXGetProcAddress);
   return (_glXGetProcAddress ? store_real_symbol_and_return_fake_symbol((const char*)procname, _glXGetProcAddress(procname)) : NULL);
}

snd_pcm_sframes_t
snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
{
   HOOK(snd_pcm_writei);
   alsa_writei(pcm, buffer, size, __func__);
   return _snd_pcm_writei(pcm, buffer, size);
}

snd_pcm_sframes_t
snd_pcm_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
{
   HOOK(snd_pcm_writen);
   // FIXME: Implement
   return _snd_pcm_writen(pcm, bufs, size);
}

snd_pcm_sframes_t
snd_pcm_mmap_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
{
   HOOK(snd_pcm_mmap_writei);
   alsa_writei(pcm, buffer, size, __func__);
   return _snd_pcm_mmap_writei(pcm, buffer, size);
}

snd_pcm_sframes_t
snd_pcm_mmap_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
{
   HOOK(snd_pcm_mmap_writen);
   // FIXME: Implement
   return _snd_pcm_mmap_writen(pcm, bufs, size);
}

int
clock_gettime(clockid_t clk_id, struct timespec *tp)
{
   HOOK(clock_gettime);

   if ((clk_id == CLOCK_MONOTONIC || clk_id == CLOCK_MONOTONIC_RAW)) {
      const uint64_t fake = get_fake_time_ns();
      tp->tv_sec = fake / (uint64_t)1e9;
      tp->tv_nsec = (fake % (uint64_t)1e9);
      return 0;
   }

   return _clock_gettime(clk_id, tp);
}

static void*
store_real_symbol_and_return_fake_symbol(const char *symbol, void *ret)
{
   if (!ret || !symbol)
      return ret;

   if (0) {}
#define SET_IF_NOT_HOOKED(x, y) do { if (!_##x) { _##x = y; WARNX("SET %s to %p", #x, y); } } while (0)
#define FAKE_SYMBOL(x) else if (!strcmp(symbol, #x)) { SET_IF_NOT_HOOKED(x, ret); return x; }
   FAKE_SYMBOL(glBlitFramebuffer)
   FAKE_SYMBOL(eglSwapBuffers)
   FAKE_SYMBOL(eglGetProcAddress)
   FAKE_SYMBOL(glXSwapBuffers)
   FAKE_SYMBOL(glXGetProcAddressARB)
   FAKE_SYMBOL(glXGetProcAddress)
   FAKE_SYMBOL(snd_pcm_writei)
   FAKE_SYMBOL(snd_pcm_writen)
   FAKE_SYMBOL(snd_pcm_mmap_writei)
   FAKE_SYMBOL(snd_pcm_mmap_writen)
   FAKE_SYMBOL(clock_gettime)
#undef FAKE_SYMBOL
#undef SET_IF_NOT_HOOKED

   return ret;
}

#define HOOK_DLSYM(x) hook_function((void**)&_##x, #x, true)

static void
hook_function(void **ptr, const char *name, const bool versioned)
{
   if (*ptr)
      return;

   if (versioned) {
      const char *versions[] = { "GLIBC_2.0""GLIBC_2.2.5" };
      for (size_t i = 0; !*ptr && i < ARRAY_SIZE(versions); ++i)
         *ptr = dlvsym(RTLD_NEXT, name, versions[i]);
   } else {
      HOOK_DLSYM(dlsym);
      *ptr = _dlsym(RTLD_NEXT, name);
   }

   if (!*ptr)
      ERRX(EXIT_FAILURE"HOOK FAIL %s", name);

   WARNX("HOOK %s", name);
}

void*
dlsym(void *handle, const char *symbol)
{
   HOOK_DLSYM(dlsym);

   if (!strcmp(symbol, "dlsym"))
      return dlsym;

   return store_real_symbol_and_return_fake_symbol(symbol, _dlsym(handle, symbol));
}