diff mbox series

[RFC,v3,05/10] linux-user: Implement native-bypass option support

Message ID 20230625212707.1078951-6-fufuyqqqqqq@gmail.com
State New
Headers show
Series Native Library Calls | expand

Commit Message

Yeqi Fu June 25, 2023, 9:27 p.m. UTC
This commit implements the -native-bypass support in linux-user. The
native_calls_enabled() function can be true only when the
'-native-bypass' option is given.

Signed-off-by: Yeqi Fu <fufuyqqqqqq@gmail.com>
---
 linux-user/main.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

Comments

Alex Bennée July 3, 2023, 6:21 p.m. UTC | #1
Yeqi Fu <fufuyqqqqqq@gmail.com> writes:

> This commit implements the -native-bypass support in linux-user. The
> native_calls_enabled() function can be true only when the
> '-native-bypass' option is given.
>
> Signed-off-by: Yeqi Fu <fufuyqqqqqq@gmail.com>
> ---
>  linux-user/main.c | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
>
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 5e6b2e1714..98e31c77d5 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -60,6 +60,13 @@
>  #include "semihosting/semihost.h"
>  #endif
>  
> +#if defined(CONFIG_NATIVE_CALL)
> +#include "native/native-defs.h"
> +
> +static const char *native_lib;
> +bool native_bypass_enabled;

This bool feels redundant if we can check for a non-null native-lib. You
could certainly expose a function though:

bool native_bypass_enabled() {
     return native_lib ? true : false;
}

?

> +#endif
> +
>  #ifndef AT_FLAGS_PRESERVE_ARGV0
>  #define AT_FLAGS_PRESERVE_ARGV0_BIT 0
>  #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
> @@ -125,6 +132,7 @@ static void usage(int exitcode);
>  static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
>  const char *qemu_uname_release;
>  
> +

rm whitespace

>  #if !defined(TARGET_DEFAULT_STACK_SIZE)
>  /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
>     we allocate a bigger stack. Need a better solution, for example
> @@ -293,6 +301,18 @@ static void handle_arg_set_env(const char *arg)
>      free(r);
>  }
>  
> +#if defined(CONFIG_NATIVE_CALL)
> +static void handle_arg_native_bypass(const char *arg)
> +{
> +    if (access(arg, F_OK) != 0) {
> +        fprintf(stderr, "native library %s does not exist\n", arg);
> +        exit(EXIT_FAILURE);
> +    }
> +    native_lib = arg;
> +    native_bypass_enabled = true;
> +}
> +#endif
> +
>  static void handle_arg_unset_env(const char *arg)
>  {
>      char *r, *p, *token;
> @@ -522,6 +542,10 @@ static const struct qemu_argument arg_table[] = {
>       "",           "Generate a /tmp/perf-${pid}.map file for perf"},
>      {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
>       "",           "Generate a jit-${pid}.dump file for perf"},
> +#if defined(CONFIG_NATIVE_CALL)
> +    {"native-bypass", "QEMU_NATIVE_BYPASS", true, handle_arg_native_bypass,
> +     "",           "native bypass for library calls in user mode only."},
> +#endif
>      {NULL, NULL, false, NULL, NULL, NULL}
>  };
>  
> @@ -826,6 +850,18 @@ int main(int argc, char **argv, char **envp)
>          }
>      }
>  
> +#if defined(CONFIG_NATIVE_CALL)
> +    /* Set the library for native bypass  */
> +    if (native_bypass_enabled) {

Then this could be:

  if (native_lib && g_file_test(native_lib, G_FILE_TEST_EXITS)) {

Or maybe better:

 if (native_lib) {
    if (g_file_test(native_lib, G_FILE_TEST_EXITS)) {
       .. setup ..
    } else {
       fprintf(stderr, "can't open %s\n", native_lib);
       exit(EXIT_FAILURE);
    }
}

> +        GString *lib = g_string_new(native_lib);
> +        lib = g_string_prepend(lib, "LD_PRELOAD=");
> +        if (envlist_appendenv(envlist, g_string_free(lib, false), ":") != 0) {
> +            fprintf(stderr,
> +                    "failed to append the native library to environment.\n");
> +            exit(EXIT_FAILURE);
> +        }
> +    }
> +#endif
>      target_environ = envlist_to_environ(envlist, NULL);
>      envlist_free(envlist);
diff mbox series

Patch

diff --git a/linux-user/main.c b/linux-user/main.c
index 5e6b2e1714..98e31c77d5 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -60,6 +60,13 @@ 
 #include "semihosting/semihost.h"
 #endif
 
+#if defined(CONFIG_NATIVE_CALL)
+#include "native/native-defs.h"
+
+static const char *native_lib;
+bool native_bypass_enabled;
+#endif
+
 #ifndef AT_FLAGS_PRESERVE_ARGV0
 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0
 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
@@ -125,6 +132,7 @@  static void usage(int exitcode);
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
 
+
 #if !defined(TARGET_DEFAULT_STACK_SIZE)
 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
    we allocate a bigger stack. Need a better solution, for example
@@ -293,6 +301,18 @@  static void handle_arg_set_env(const char *arg)
     free(r);
 }
 
+#if defined(CONFIG_NATIVE_CALL)
+static void handle_arg_native_bypass(const char *arg)
+{
+    if (access(arg, F_OK) != 0) {
+        fprintf(stderr, "native library %s does not exist\n", arg);
+        exit(EXIT_FAILURE);
+    }
+    native_lib = arg;
+    native_bypass_enabled = true;
+}
+#endif
+
 static void handle_arg_unset_env(const char *arg)
 {
     char *r, *p, *token;
@@ -522,6 +542,10 @@  static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+#if defined(CONFIG_NATIVE_CALL)
+    {"native-bypass", "QEMU_NATIVE_BYPASS", true, handle_arg_native_bypass,
+     "",           "native bypass for library calls in user mode only."},
+#endif
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
@@ -826,6 +850,18 @@  int main(int argc, char **argv, char **envp)
         }
     }
 
+#if defined(CONFIG_NATIVE_CALL)
+    /* Set the library for native bypass  */
+    if (native_bypass_enabled) {
+        GString *lib = g_string_new(native_lib);
+        lib = g_string_prepend(lib, "LD_PRELOAD=");
+        if (envlist_appendenv(envlist, g_string_free(lib, false), ":") != 0) {
+            fprintf(stderr,
+                    "failed to append the native library to environment.\n");
+            exit(EXIT_FAILURE);
+        }
+    }
+#endif
     target_environ = envlist_to_environ(envlist, NULL);
     envlist_free(envlist);