From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001
From: Ian Moffett <ian@osmora.org>
Date: Thu, 7 Mar 2024 17:28:00 -0500
Subject: lib: Add mlibc

Signed-off-by: Ian Moffett <ian@osmora.org>
---
 lib/mlibc/tests/rtdl/scope3/libbar.c    |  8 +++++++
 lib/mlibc/tests/rtdl/scope3/libbaz.c    |  7 ++++++
 lib/mlibc/tests/rtdl/scope3/libfoo.c    |  5 +++++
 lib/mlibc/tests/rtdl/scope3/meson.build |  9 ++++++++
 lib/mlibc/tests/rtdl/scope3/test.c      | 39 +++++++++++++++++++++++++++++++++
 5 files changed, 68 insertions(+)
 create mode 100644 lib/mlibc/tests/rtdl/scope3/libbar.c
 create mode 100644 lib/mlibc/tests/rtdl/scope3/libbaz.c
 create mode 100644 lib/mlibc/tests/rtdl/scope3/libfoo.c
 create mode 100644 lib/mlibc/tests/rtdl/scope3/meson.build
 create mode 100644 lib/mlibc/tests/rtdl/scope3/test.c

(limited to 'lib/mlibc/tests/rtdl/scope3')

diff --git a/lib/mlibc/tests/rtdl/scope3/libbar.c b/lib/mlibc/tests/rtdl/scope3/libbar.c
new file mode 100644
index 0000000..dc377b6
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope3/libbar.c
@@ -0,0 +1,8 @@
+int g = 1;
+
+int call_foo();
+
+int call_bar() {
+	return call_foo();
+}
+
diff --git a/lib/mlibc/tests/rtdl/scope3/libbaz.c b/lib/mlibc/tests/rtdl/scope3/libbaz.c
new file mode 100644
index 0000000..32524cc
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope3/libbaz.c
@@ -0,0 +1,7 @@
+int g = 2;
+
+int call_foo();
+
+int call_baz() {
+	return call_foo();
+}
diff --git a/lib/mlibc/tests/rtdl/scope3/libfoo.c b/lib/mlibc/tests/rtdl/scope3/libfoo.c
new file mode 100644
index 0000000..bc86319
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope3/libfoo.c
@@ -0,0 +1,5 @@
+int g = 0;
+
+int call_foo() {
+	return g;
+}
diff --git a/lib/mlibc/tests/rtdl/scope3/meson.build b/lib/mlibc/tests/rtdl/scope3/meson.build
new file mode 100644
index 0000000..0c98583
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope3/meson.build
@@ -0,0 +1,9 @@
+libfoo = shared_library('foo', 'libfoo.c')
+libbar = shared_library('bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo)
+libbaz = shared_library('baz', 'libbaz.c', build_rpath: test_rpath, link_with: libfoo)
+test_depends = [libfoo, libbar, libbaz]
+
+libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
+libbar_native = shared_library('native-bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
+libbaz_native = shared_library('native-baz', 'libbaz.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
+test_native_depends = [libfoo_native, libbar_native, libbaz_native]
diff --git a/lib/mlibc/tests/rtdl/scope3/test.c b/lib/mlibc/tests/rtdl/scope3/test.c
new file mode 100644
index 0000000..30fc662
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope3/test.c
@@ -0,0 +1,39 @@
+#include <dlfcn.h>
+#include <stdio.h>
+#include <assert.h>
+
+#ifdef USE_HOST_LIBC
+#define LIBBAR "libnative-bar.so"
+#define LIBBAZ "libnative-baz.so"
+#else
+#define LIBBAR "libbar.so"
+#define LIBBAZ "libbaz.so"
+#endif
+
+int main() {
+	// In this test, we have bar -> foo and baz -> foo (where -> means 'depends on').
+	// All three objects contain a definition of a symbol g. Bar calls into foo to retrieve
+	// what foo thinks g is, but since bar appears earlier in the scope than foo, bar's copy
+	// of g wins.
+	//
+	// Next, we load baz, which is identical to bar. When baz calls into foo to retrieve g,
+	// foo still sees bar's definition of g, so bar's copy of g wins.
+	//
+	// Swapping the load order of bar and baz should therefore change the value of g which
+	// foo sees. This behaviour is why dlmopen exists. If we ever implement that, we should
+	// write a similar test and assert that the calls return different results.
+
+	void *libbar = dlopen(LIBBAR, RTLD_LAZY | RTLD_LOCAL);
+	int (*call_bar)(void) = dlsym(libbar, "call_bar");
+	printf("call_bar: %d\n", call_bar());
+	assert(call_bar() == 1);
+
+	void *libbaz = dlopen(LIBBAZ, RTLD_LAZY | RTLD_LOCAL);
+	int (*call_baz)(void) = dlsym(libbaz, "call_baz");
+	printf("call_baz: %d\n", call_baz());
+	assert(call_baz() == 1);
+
+
+	dlclose(libbar);
+	dlclose(libbaz);
+}
-- 
cgit v1.2.3