#include <algorithm>
#include <cstddef>
#include <iostream>
+#include <iterator>
#include <map>
#include <string>
#include <vector>
#include "base/loader/elf_object.hh"
#include "base/loader/object_file.hh"
#include "base/logging.hh"
+#include "base/random.hh"
#include "cpu/thread_context.hh"
#include "debug/Stack.hh"
#include "mem/page_table.hh"
template<class IntType> void
RiscvProcess::argsInit(int pageSize)
{
+ const int RandomBytes = 16;
+
updateBias();
objFile->loadSections(initVirtMem);
ElfObject* elfObject = dynamic_cast<ElfObject*>(objFile);
// Determine stack size and populate auxv
Addr stack_top = memState->getStackMin();
- stack_top -= elfObject->programHeaderSize();
+ stack_top -= RandomBytes;
for (const string& arg: argv)
stack_top -= arg.size() + 1;
for (const string& env: envp)
allocateMem(roundDown(stack_top, pageSize),
roundUp(memState->getStackSize(), pageSize));
- // Copy program headers to stack
- memState->setStackMin(memState->getStackMin() -
- elfObject->programHeaderSize());
- uint8_t* phdr = new uint8_t[elfObject->programHeaderSize()];
- initVirtMem.readBlob(elfObject->programHeaderTable(), phdr,
- elfObject->programHeaderSize());
- initVirtMem.writeBlob(memState->getStackMin(), phdr,
- elfObject->programHeaderSize());
- delete phdr;
+ // Copy random bytes (for AT_RANDOM) to stack
+ memState->setStackMin(memState->getStackMin() - RandomBytes);
+ uint8_t at_random[RandomBytes];
+ generate(begin(at_random), end(at_random),
+ [&]{ return random_mt.random(0, 0xFF); });
+ initVirtMem.writeBlob(memState->getStackMin(), at_random, RandomBytes);
// Copy argv to stack
vector<Addr> argPointers;