UserspaceEmulator: Transfer the environment to the emulated process

This commit is contained in:
Andreas Kling 2020-07-27 15:57:12 +02:00
parent 1366557094
commit f097ed6ada
3 changed files with 22 additions and 8 deletions

View file

@ -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 }

View file

@ -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);

View file

@ -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;