Ver código fonte

UserspaceEmulator: Transfer the environment to the emulated process

Andreas Kling 5 anos atrás
pai
commit
f097ed6ada

+ 13 - 4
DevTools/UserspaceEmulator/Emulator.cpp

@@ -62,17 +62,17 @@ Emulator& Emulator::the()
     return *s_the;
 }
 
-Emulator::Emulator(const Vector<String>& arguments, NonnullRefPtr<ELF::Loader> elf)
+Emulator::Emulator(const Vector<String>& arguments, const Vector<String>& environment, NonnullRefPtr<ELF::Loader> elf)
     : m_elf(move(elf))
     , m_cpu(*this)
 {
     m_malloc_tracer = make<MallocTracer>();
     ASSERT(!s_the);
     s_the = this;
-    setup_stack(arguments);
+    setup_stack(arguments, environment);
 }
 
-void Emulator::setup_stack(const Vector<String>& arguments)
+void Emulator::setup_stack(const Vector<String>& arguments, const Vector<String>& environment)
 {
     auto stack_region = make<SimpleRegion>(stack_location, stack_size);
     stack_region->set_stack(true);
@@ -86,7 +86,16 @@ void Emulator::setup_stack(const Vector<String>& arguments)
         argv_entries.append(m_cpu.esp().value());
     }
 
-    m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // char** envp = { nullptr }
+    Vector<u32> env_entries;
+
+    for (auto& variable : environment) {
+        m_cpu.push_string(variable.characters());
+        env_entries.append(m_cpu.esp().value());
+    }
+
+    m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // char** envp = { envv_entries..., nullptr }
+    for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
+        m_cpu.push32(shadow_wrap_as_initialized(env_entries[i]));
     u32 envp = m_cpu.esp().value();
 
     m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // char** argv = { argv_entries..., nullptr }

+ 2 - 2
DevTools/UserspaceEmulator/Emulator.h

@@ -43,7 +43,7 @@ class Emulator {
 public:
     static Emulator& the();
 
-    Emulator(const Vector<String>& arguments, NonnullRefPtr<ELF::Loader>);
+    Emulator(const Vector<String>& arguments, const Vector<String>& environment, NonnullRefPtr<ELF::Loader>);
 
     bool load_elf();
     void dump_backtrace();
@@ -68,7 +68,7 @@ private:
 
     OwnPtr<MallocTracer> m_malloc_tracer;
 
-    void setup_stack(const Vector<String>& arguments);
+    void setup_stack(const Vector<String>& arguments, const Vector<String>& environment);
 
     int virt$get_dir_entries(int fd, FlatPtr buffer, ssize_t);
     int virt$ioctl(int fd, unsigned, FlatPtr);

+ 7 - 2
DevTools/UserspaceEmulator/main.cpp

@@ -32,7 +32,7 @@
 #include <LibELF/Loader.h>
 #include <getopt.h>
 
-int main(int argc, char** argv)
+int main(int argc, char** argv, char** env)
 {
     if (argc == 1) {
         out() << "usage: UserspaceEmulator <command>";
@@ -55,7 +55,12 @@ int main(int argc, char** argv)
         arguments.append(argv[i]);
     }
 
-    UserspaceEmulator::Emulator emulator(arguments, move(elf));
+    Vector<String> environment;
+    for (int i = 0; env[i]; ++i) {
+        environment.append(env[i]);
+    }
+
+    UserspaceEmulator::Emulator emulator(arguments, environment, move(elf));
     if (!emulator.load_elf())
         return 1;