Skip to content

Commit 644564d

Browse files
committed
initial iserv-proxy
1 parent 928b0d8 commit 644564d

5 files changed

+1180
-1
lines changed

overlays/linux-cross.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ let
3232
PORT=$((5000 + $RANDOM % 5000))
3333
(>&2 echo "---> Starting ${interpreter.exeName} on port $PORT")
3434
${qemu}/bin/qemu-${qemuSuffix} ${interpreter.override
35-
(lib.optionalAttrs hostPlatform.isAndroid {
35+
({
36+
patches = lib.optional (builtins.compareVersions interpreter.version "9.0" > 0) ./patches/iserv-proxy-keep-cafs.patch
37+
++ lib.optional (builtins.compareVersions interpreter.version "9.0" > 0 && hostPlatform.isAndroid && hostPlatform.isAarch32) ./patches/iserv-proxy-interpreter-9.3-android32.patch
38+
++ lib.optional (builtins.compareVersions interpreter.version "9.0" > 0 && hostPlatform.isAndroid && hostPlatform.isAarch64) ./patches/iserv-proxy-interpreter-9.3-android.patch
39+
++ lib.optional (builtins.compareVersions interpreter.version "9.0" > 0 && hostPlatform.isMusl && hostPlatform.isAarch64) ./patches/iserv-proxy-interpreter-9.3-musl.patch
40+
;
41+
} // lib.optionalAttrs hostPlatform.isAndroid {
3642
setupBuildFlags = ["--ghc-option=-optl-static" ] ++ lib.optional hostPlatform.isAarch32 "--ghc-option=-optl-no-pie";
3743
enableDebugRTS = true;
3844
})}/bin/${interpreter.exeName} tmp $PORT $ISERV_ARGS &
Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
diff --git a/cbits/symbols.c b/cbits/symbols.c
2+
new file mode 100644
3+
index 0000000..d447895
4+
--- /dev/null
5+
+++ b/cbits/symbols.c
6+
@@ -0,0 +1,318 @@
7+
+#include <stddef.h>
8+
+#include <stdio.h>
9+
+#include <string.h>
10+
+#include <stdlib.h>
11+
+#include <signal.h>
12+
+#include <assert.h>
13+
+#include <math.h>
14+
+#include <errno.h>
15+
+#include <unistd.h>
16+
+#include <sys/stat.h>
17+
+#include <sys/wait.h>
18+
+#include <sys/epoll.h>
19+
+// #include <sys/eventfd.h>
20+
+// #include <fcntl.h> // this includes too many overloaded items.
21+
+#include <string.h>
22+
+#include <ctype.h>
23+
+#include <wchar.h>
24+
+#include <utime.h>
25+
+#include <poll.h>
26+
+#include <time.h>
27+
+#include <dirent.h>
28+
+#include <netdb.h>
29+
+#include <pthread.h>
30+
+#include <sys/mman.h>
31+
+#include <sys/auxv.h>
32+
+#include <sys/random.h>
33+
+#include <arpa/inet.h>
34+
+#include <sys/time.h>
35+
+#include <sys/stat.h>
36+
+#include <grp.h>
37+
+#include <pwd.h>
38+
+#include <sys/uio.h>
39+
+
40+
+// fnctl stubs, see above
41+
+extern void open(void);
42+
+extern void openat(void);
43+
+extern void creat(void);
44+
+extern void eventfd(void);
45+
+extern void eventfd_write(void);
46+
+
47+
+extern void futimes(void);
48+
+extern void lutimes(void);
49+
+extern void statx(void);
50+
+
51+
+extern void __stack_chk_fail(void);
52+
+extern void __vsprintf_chk(void);
53+
+extern void __open_2(void);
54+
+extern void __memcpy_chk(void);
55+
+extern void __memset_chk(void);
56+
+extern void __memmove_chk(void);
57+
+// GCC stuff
58+
+extern void __addtf3(void);
59+
+extern void __divtf3(void);
60+
+extern void __extenddftf2(void);
61+
+extern void __fixtfsi(void);
62+
+extern void __floatditf(void);
63+
+extern void __floatsitf(void);
64+
+extern void __getf2(void);
65+
+extern void __gttf2(void);
66+
+extern void __lttf2(void);
67+
+extern void __multf3(void);
68+
+extern void __subtf3(void);
69+
+extern void __trunctfdf2(void);
70+
+
71+
+#define MISSING_FUN(f) void (f)(void) { printf("Unknown call to `%s'\n", #f); exit(1); }
72+
+
73+
+MISSING_FUN(c_format_unix_time)
74+
+MISSING_FUN(c_format_unix_time_gmt)
75+
+MISSING_FUN(c_parse_unix_time)
76+
+MISSING_FUN(c_parse_unix_time_gmt)
77+
+
78+
+typedef void SymbolAddr;
79+
+typedef char SymbolName;
80+
+
81+
+typedef enum _SymStrength {
82+
+ STRENGTH_NORMAL,
83+
+ STRENGTH_WEAK,
84+
+ STRENGTH_STRONG,
85+
+} SymStrength;
86+
+
87+
+typedef enum _SymType {
88+
+ SYM_TYPE_CODE = 1 << 0, /* the symbol is a function and can be relocated via a jump island */
89+
+ SYM_TYPE_DATA = 1 << 1, /* the symbol is data */
90+
+ SYM_TYPE_INDIRECT_DATA = 1 << 2, /* see Note [_iob_func symbol] */
91+
+ SYM_TYPE_DUP_DISCARD = 1 << 3, /* the symbol is a symbol in a BFD import library
92+
+ however if a duplicate is found with a mismatching
93+
+ SymType then discard this one. */
94+
+} SymType;
95+
+
96+
+typedef struct _RtsSymbolVal {
97+
+ const SymbolName* lbl;
98+
+ SymbolAddr* addr;
99+
+ SymStrength strength;
100+
+ SymType type;
101+
+} RtsSymbolVal;
102+
+
103+
+#define SYM(x) { #x, (void*)(&x), STRENGTH_NORMAL, 1 }
104+
+typedef mode_t (*umask_func_ptr_t)(mode_t);
105+
+
106+
+RtsSymbolVal my_iserv_syms[] = {
107+
+ // arpa/inet.h
108+
+ SYM(htons),
109+
+ SYM(ntohs),
110+
+ SYM(htonl),
111+
+ SYM(ntohl),
112+
+ // sys/random.h
113+
+ SYM(getentropy),
114+
+ SYM(getrandom),
115+
+ // sys/auxv.h
116+
+ SYM(getauxval),
117+
+ // sys/mman.h
118+
+ SYM(madvise),SYM(mlock),SYM(mmap),SYM(mprotect),SYM(munmap),
119+
+ SYM(mremap),
120+
+ SYM(munlock),
121+
+ // select.h
122+
+ SYM(__FD_SET_chk),
123+
+ // sys/socket
124+
+ SYM(accept),SYM(bind),SYM(connect),SYM(getsockopt),SYM(listen),
125+
+ SYM(setsockopt),SYM(socket),SYM(getsockname),SYM(select),
126+
+ SYM(getpeername),SYM(__cmsg_nxthdr),SYM(recv),SYM(recvfrom),
127+
+ SYM(recvmsg),SYM(send),SYM(sendmsg),SYM(sendto),SYM(writev),
128+
+ SYM(accept4),
129+
+ // pthread.h
130+
+ SYM(pthread_equal),SYM(pthread_getspecific),SYM(pthread_key_create),
131+
+ SYM(pthread_key_delete),SYM(pthread_once),SYM(pthread_rwlock_destroy),
132+
+ SYM(pthread_rwlock_init),SYM(pthread_rwlock_rdlock),SYM(pthread_rwlock_unlock),
133+
+ SYM(pthread_rwlock_wrlock),SYM(pthread_self),SYM(pthread_setspecific),
134+
+ SYM(pthread_create),SYM(pthread_join),SYM(pthread_mutex_destroy),
135+
+ SYM(pthread_mutex_init),SYM(pthread_mutex_lock),SYM(pthread_mutex_trylock),
136+
+ SYM(pthread_mutex_unlock),SYM(pthread_mutexattr_destroy),
137+
+ SYM(pthread_mutexattr_init),SYM(pthread_mutexattr_settype),
138+
+ // chk.h
139+
+ SYM(__read_chk),SYM(__write_chk),
140+
+ // netdb.h
141+
+ SYM(freeaddrinfo),SYM(gai_strerror),SYM(getaddrinfo),SYM(getnameinfo),
142+
+ SYM(gethostbyname),
143+
+ // dirent.h
144+
+ SYM(readdir_r),SYM(readdir),
145+
+ SYM(opendir),SYM(closedir),
146+
+ // time.h
147+
+ SYM(clock),SYM(gmtime_r),
148+
+ // sys/time.h
149+
+ SYM(gettimeofday),SYM(clock_getres),SYM(clock_gettime),SYM(localtime_r),SYM(tzset),
150+
+ // unistd.h
151+
+ SYM(readlink),
152+
+ SYM(rename),
153+
+ SYM(rmdir),
154+
+ SYM(chown),
155+
+ SYM(realpath),
156+
+ SYM(fchdir),
157+
+ SYM(fdopendir),
158+
+ SYM(rewinddir),
159+
+ SYM(futimens),SYM(futimes),SYM(lutimes),
160+
+ SYM(mknod),
161+
+ SYM(lchown),
162+
+ SYM(symlink),
163+
+ SYM(endgrent),SYM(endpwent),
164+
+ SYM(pathconf),
165+
+ SYM(truncate),
166+
+ SYM(utimensat),
167+
+ SYM(statx),
168+
+ SYM(seekdir),
169+
+ SYM(telldir),
170+
+ SYM(clearenv),
171+
+ SYM(chdir),
172+
+ SYM(sleep),
173+
+ SYM(stdout),
174+
+ SYM(strftime),
175+
+ SYM(utimes),
176+
+ SYM(setenv),
177+
+ SYM(fpathconf),
178+
+ SYM(exit),
179+
+ SYM(environ),
180+
+ SYM(ftruncate),
181+
+ SYM(getenv),
182+
+ SYM(putenv),
183+
+ SYM(unsetenv),
184+
+ SYM(read),
185+
+ SYM(write),
186+
+ SYM(isatty),
187+
+ SYM(link),
188+
+ SYM(pipe),
189+
+ SYM(unlink),
190+
+ SYM(execv),SYM(execve),SYM(execvp),SYM(execvpe),
191+
+ SYM(syscall),SYM(sysconf),
192+
+ // errno.h
193+
+ SYM(__errno),
194+
+ // math.h
195+
+ SYM(sinhf), SYM(sinh), SYM(sinf), SYM(sin),
196+
+ SYM(coshf), SYM(cosh), SYM(cosf), SYM(cos),
197+
+ SYM(atanhf), SYM(atanh), SYM(atanf), SYM(atan),
198+
+ SYM(asinhf), SYM(asinh), SYM(asinf), SYM(asin),
199+
+ SYM(acoshf), SYM(acosh), SYM(acosf), SYM(acos),
200+
+ SYM(log1pf), SYM(log1p), SYM(logf), SYM(log),
201+
+ SYM(expm1f), SYM(expm1),
202+
+ SYM(expf), SYM(exp),
203+
+ SYM(ldexp),
204+
+ SYM(powf), SYM(pow),
205+
+ SYM(sqrtf), SYM(sqrt),
206+
+ SYM(tanhf), SYM(tanh), SYM(tanf), SYM(tan),
207+
+ // assert.h
208+
+ SYM(__assert2),
209+
+ // signal.h
210+
+ SYM(signal),SYM(sigaction),
211+
+ SYM(raise), SYM(sigaddset), SYM(sigemptyset), SYM(sigprocmask),
212+
+ // sys/eventfd.h
213+
+ SYM(eventfd), SYM(eventfd_write),
214+
+ // sys/stat.h
215+
+ SYM(fstat),
216+
+ SYM(lstat),
217+
+ SYM(stat),
218+
+ SYM(chmod),
219+
+ SYM(mkfifo),
220+
+ // SYM(umask),
221+
+ { "umask", (umask_func_ptr_t)(&umask), STRENGTH_NORMAL, 1 },
222+
+ // sys/wait.h
223+
+ SYM(waitpid),
224+
+ // sym/epoll.h
225+
+ SYM(epoll_create), SYM(epoll_ctl), SYM(epoll_wait),
226+
+ // poll.h
227+
+ SYM(poll),
228+
+ // fcntl.h
229+
+ SYM(open), SYM(creat), SYM(fcntl), SYM(ioctl),
230+
+ SYM(openat),SYM(__open_2),
231+
+ // string.h
232+
+ SYM(strerror),
233+
+ SYM(strcmp),
234+
+ SYM(memchr),SYM(strcpy),SYM(strchr),SYM(strncpy),SYM(strrchr),
235+
+ SYM(strcat),SYM(strncmp),SYM(strdup),
236+
+ SYM(strtoul),SYM(strspn),SYM(strtol),SYM(strstr),SYM(strcspn),
237+
+ SYM(__strncpy_chk2),SYM(__memcpy_chk),
238+
+ // ctype.h
239+
+ SYM(__ctype_get_mb_cur_max),
240+
+ // wchar.h
241+
+ SYM(mbrtowc), SYM(wcrtomb),
242+
+ // stdlib.h
243+
+ SYM(qsort),
244+
+ // unistd.h
245+
+ SYM(access), SYM(close), SYM(dup), SYM(dup2), SYM(fork), SYM(getpid),
246+
+ SYM(lseek),
247+
+ // utime.h
248+
+ SYM(utime),SYM(time),
249+
+ // ...
250+
+ SYM(fileno),
251+
+ SYM(__vsprintf_chk),
252+
+ SYM(__strlen_chk),
253+
+ SYM(__strchr_chk),
254+
+ SYM(__memset_chk),
255+
+ SYM(__memmove_chk),
256+
+ SYM(__stack_chk_fail),
257+
+ SYM(memmove),
258+
+ SYM(memcmp),
259+
+ SYM(memcpy),
260+
+ SYM(memset),
261+
+ SYM(stderr),
262+
+ SYM(realloc),
263+
+ SYM(calloc),
264+
+ SYM(malloc),
265+
+ SYM(free),
266+
+ SYM(fprintf),
267+
+ SYM(vfprintf),
268+
+ SYM(fopen), SYM(fclose),
269+
+ SYM(getegid),SYM(getgid),
270+
+ SYM(getpwent),
271+
+ SYM(getgrent),
272+
+ SYM(getgroups),
273+
+ SYM(getlogin),
274+
+ SYM(getuid),
275+
+ SYM(getgrgid_r),SYM(getgrnam_r),SYM(getpwnam_r),SYM(getpwuid_r),
276+
+ SYM(setegid),SYM(seteuid),SYM(setgid),SYM(setgrent),SYM(setgroups),
277+
+ SYM(setpwent),SYM(setuid),
278+
+ SYM(fread),
279+
+ SYM(abort),
280+
+ SYM(strlen),
281+
+ SYM(fwrite),
282+
+ SYM(feof),
283+
+ SYM(ferror),
284+
+ SYM(fflush),
285+
+ SYM(fgets),SYM(fputc),SYM(fputs),
286+
+ SYM(fseek),SYM(ftell),
287+
+ SYM(sscanf),
288+
+ SYM(shutdown),
289+
+ SYM(atoi),
290+
+ SYM(stdin),
291+
+ SYM(atexit),
292+
+ SYM(usleep),
293+
+ SYM(fchmod),
294+
+ SYM(fchown),
295+
+ SYM(fsync),
296+
+ SYM(getcwd),
297+
+ SYM(geteuid),
298+
+ SYM(localtime),
299+
+ SYM(lseek64),
300+
+ SYM(mkdir),
301+
+ SYM(mktime),
302+
+ SYM(fdopen),
303+
+ SYM(c_format_unix_time),
304+
+ SYM(c_format_unix_time_gmt),
305+
+ SYM(c_parse_unix_time),
306+
+ SYM(c_parse_unix_time_gmt),
307+
+ SYM(__addtf3),
308+
+ SYM(__divtf3),
309+
+ SYM(__extenddftf2),
310+
+ SYM(__fixtfsi),
311+
+ SYM(__floatditf),
312+
+ SYM(__floatsitf),
313+
+ SYM(__getf2),
314+
+ SYM(__gttf2),
315+
+ SYM(__lttf2),
316+
+ SYM(__multf3),
317+
+ SYM(__subtf3),
318+
+ SYM(__trunctfdf2),
319+
+ { 0, 0, STRENGTH_NORMAL, 1 } /* sentinel */
320+
+};
321+
+
322+
+RtsSymbolVal* iserv_syms() {
323+
+ return my_iserv_syms;
324+
+}
325+
\ No newline at end of file
326+
diff --git a/iserv-proxy.cabal b/iserv-proxy.cabal
327+
index a0eeaeb..a146108 100644
328+
--- a/iserv-proxy.cabal
329+
+++ b/iserv-proxy.cabal
330+
@@ -103,6 +103,7 @@ Executable iserv-proxy
331+
-- We need to pass -fkeep-cafs to ensure that the interpreter retains CAFs
332+
-- Iserv and GHC do something similar.
333+
ghc-options: -fkeep-cafs
334+
+ c-sources: cbits/symbols.c
335+
Build-Depends: base >= 4 && < 5,
336+
iserv-proxy
337+

0 commit comments

Comments
 (0)