Example #1
0
        public WaterboxHost(WaterboxOptions opt)
        {
            _nextStart = opt.StartAddress;
            Initialize(_nextStart);
            using (this.EnterExit())
            {
                _emu      = new EmuLibc(this);
                _syscalls = new Syscalls(this);

                _imports = new PatchImportResolver(
                    NotImplementedSyscalls.Instance,
                    BizExvoker.GetExvoker(_emu, CallingConventionAdapters.Waterbox),
                    BizExvoker.GetExvoker(_syscalls, CallingConventionAdapters.Waterbox)
                    );

                if (true)
                {
                    var moduleName = opt.Filename;

                    var    path   = Path.Combine(opt.Path, moduleName);
                    var    gzpath = path + ".gz";
                    byte[] data;
                    if (File.Exists(gzpath))
                    {
                        using var fs = new FileStream(gzpath, FileMode.Open, FileAccess.Read);
                        data         = Util.DecompressGzipFile(fs);
                    }
                    else
                    {
                        data = File.ReadAllBytes(path);
                    }

                    _module = new ElfLoader(moduleName, data, _nextStart, opt.SkipCoreConsistencyCheck, opt.SkipMemoryConsistencyCheck);

                    ComputeNextStart(_module.Memory.Size);
                    AddMemoryBlock(_module.Memory, moduleName);
                    _savestateComponents.Add(_module);
                    _disposeList.Add(_module);
                }

                ConnectAllImports();

                // load all heaps
                _heap          = CreateHeapHelper(opt.SbrkHeapSizeKB, "brk-heap", true);
                _sealedheap    = CreateHeapHelper(opt.SealedHeapSizeKB, "sealed-heap", true);
                _invisibleheap = CreateHeapHelper(opt.InvisibleHeapSizeKB, "invisible-heap", false);
                _plainheap     = CreateHeapHelper(opt.PlainHeapSizeKB, "plain-heap", true);

                if (opt.MmapHeapSizeKB != 0)
                {
                    _mmapheap = new MapHeap(_nextStart, opt.MmapHeapSizeKB * 1024, "mmap-heap");
                    _mmapheap.Memory.Activate();
                    ComputeNextStart(opt.MmapHeapSizeKB * 1024);
                    AddMemoryBlock(_mmapheap.Memory, "mmap-heap");
                    _savestateComponents.Add(_mmapheap);
                    _disposeList.Add(_mmapheap);
                }

                System.Diagnostics.Debug.WriteLine($"About to enter unmanaged code for {opt.Filename}");
                if (!OSTailoredCode.IsUnixHost && !System.Diagnostics.Debugger.IsAttached && Win32Imports.IsDebuggerPresent())
                {
                    // this means that GDB or another unconventional debugger is attached.
                    // if that's the case, and it's observing this core, it probably wants a break
                    System.Diagnostics.Debugger.Break();
                }

                _module.RunNativeInit();
            }
        }