Skip to content

Commit 824a00e

Browse files
grendellojonpryor
authored andcommitted
[java-interop] Fix stack overflow when running on Linux (#156)
Java.Interop native code is built with `-std=c99` which, on Linux, makes the **strdup**(3) and **realpath**(3) functions undeclared (they're not part of the C99 standard). This poses a problem since both of them return pointers and the assumed return value for an undeclared function is an `int` - when running on a 64-bit system the "integer" is cast to a pointer so that the high 32-bits of the resulting value are set to 1 thus creating an invalid pointer. This commit makes sure both functions are declared. The commit also removes call to `gettid()` which does not have a wrapper in glibc and should be called directly using `syscall(2)`.
1 parent 43c0273 commit 824a00e

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/java-interop/java-interop-gc-bridge-mono.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "java-interop-mono.h"
1010

1111
#ifdef __linux__
12+
#include <sys/syscall.h>
1213
#include <unistd.h>
1314
#endif /* !defined (__linux__) */
1415

@@ -1178,7 +1179,7 @@ get_thread_id (void)
11781179
return _mono_thread_get_managed_id (thread);
11791180
}
11801181
#if __linux__
1181-
int64_t tid = gettid ();
1182+
int64_t tid = (int64_t)((pid_t)syscall(SYS_gettid));
11821183
#else
11831184
int64_t tid = (int64_t) pthread_self ();
11841185
#endif

src/java-interop/java-interop.mdproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<_Files>@(Compile -&gt; '%(Identity)', ' ')</_Files>
117117
</PropertyGroup>
118118
<MakeDir Directories="obj" />
119-
<Exec Command="gcc -g -shared -std=c99 -o $(OutputPath)\lib$(OutputName).so $(_CppFlags) $(_LinkFlags) $(_Libs) $(_Includes) $(_Files)" />
119+
<Exec Command="gcc -g -shared -std=c99 -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L -o $(OutputPath)\lib$(OutputName).so $(_CppFlags) $(_LinkFlags) $(_Libs) $(_Includes) $(_Files)" />
120120
</Target>
121121
<Target Name="Clean">
122122
<RemoveDir Directories="obj" />

0 commit comments

Comments
 (0)