Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds FreeBSD OS, and potentially other BSD OSes #476

Merged
merged 8 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ For more information, check out our

## Installation and Use

Currently, we support Windows (7 or later), Linux, and macOS. An installed version of RetDec requires approximately 4 GB of free disk space.
Currently, we support Windows (7 or later), Linux, macOS, and (experimental) FreeBSD. An installed version of RetDec requires approximately 4 GB of free disk space.

### Windows

Expand Down Expand Up @@ -91,6 +91,21 @@ Currently, we support Windows (7 or later), Linux, and macOS. An installed versi

For more information, run `retdec-decompiler.py` with `--help`.

### FreeBSD

1. There are currently no pre-built "ports" packages for FreeBSD. You will have to build and install the decompiler by yourself. The process is described below.

2. After you have built the decompiler, you may need to install the following packages and execute the following command:
* `sudo pkg install python37 bison autotools`
* `sudo ln -s /usr/local/bin/python3.7 /usr/local/bin/python3`
s3rvac marked this conversation as resolved.
Show resolved Hide resolved

3. Now, you are all set to run the decompiler. To decompile a binary file named `test.exe`, run

```
$RETDEC_INSTALL_DIR/bin/retdec-decompiler.py test.exe
```
For more information, run `retdec-decompiler.py` with `--help`.

## Build and Installation

This section describes a local build and installation of RetDec. Instructions for Docker are given in the next section.
Expand Down Expand Up @@ -154,6 +169,21 @@ Packages should be preferably installed via [Homebrew](https://brew.sh).
* [autotools](https://en.wikipedia.org/wiki/GNU_Build_System) ([autoconf](https://www.gnu.org/software/autoconf/autoconf.html), [automake](https://www.gnu.org/software/automake/), and [libtool](https://www.gnu.org/software/libtool/))
* Optional: [Doxygen](http://www.stack.nl/~dimitri/doxygen/) and [Graphviz](http://www.graphviz.org/) for generating API documentation

#### FreeBSD

Packages should be installed via FreeBSDs pre-compiled package repository using the `pkg` command OR built from scratch using the `ports` database method.

* Full "pkg" tool instructions [handbook pkg method](https://www.freebsd.org/doc/handbook/pkgng-intro.html)
* `pkg install cmake python37 bison git autotools`
OR
* Full "ports" instructions [handbook ports method](https://www.freebsd.org/doc/handbook/ports-using.html)
* `portsnap fetch`
* `portsnap extract`
* For example "cmake" would be
* `whereis cmake`
* `cd /usr/ports/devel/cmake`
* `make install clean`

### Process

Note: Although RetDec now supports a system-wide installation ([#94](https://github.com/avast-tl/retdec/issues/94)), unless you use your distribution's package manager to install it, we recommend installing RetDec locally into a designated directory. The reason for this is that uninstallation will be easier as you will only need to remove a single directory. To perform a local installation, run `cmake` with the `-DCMAKE_INSTALL_PREFIX=<path>` parameter, where `<path>` is directory into which RetDec will be installed (e.g. `$HOME/projects/retdec-install` on Linux and macOS, and `C:\projects\retdec-install` on Windows).
Expand Down Expand Up @@ -186,6 +216,22 @@ Note: Although RetDec now supports a system-wide installation ([#94](https://git
* `cmake .. -DCMAKE_INSTALL_PREFIX=<path>`
* `make -jN` (`N` is the number of CPU cores to use for parallel build)
* `make install`
* FreeBSD:
* `sudo pkg install git cmake`
* `git clone https://github.com/avast-tl/retdec`
* `cd retdec`
* `mkdir build && cd build`
* ```sh
# FreeBSD (and other BSDs) do need cmake, python3, bison, git, autotools. Flex and perl are pre-installed in the OS but check versions.
# Later versions may be available for each of the packages.
# See what is installed:
sudo pkg info cmake python37 bison autotools
# Install/upgrade them
sudo pkg install cmake python37 bison autotools
```
* `cmake .. -DCMAKE_INSTALL_PREFIX=<path>`
* `make -jN` (`N` is the number of CPU cores to use for parallel build)
* `make install`

You have to pass the following parameters to `cmake`:
* `-DCMAKE_INSTALL_PREFIX=<path>` to set the installation path to `<path>`. Quote the path if you are using backslashes on Windows (e.g. `-DCMAKE_INSTALL_PREFIX="C:\retdec"`).
Expand Down
13 changes: 9 additions & 4 deletions include/retdec/utils/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
// Windows, macOS, and Linux.
#if defined(__WIN) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#define OS_WINDOWS
#elif defined(__APPLE__)
#define OS_MACOS
#else
#define OS_LINUX
#include <sys/param.h>
#if defined(__APPLE__)
#define OS_MACOS
#elif defined(BSD)
#define OS_BSD
s3rvac marked this conversation as resolved.
Show resolved Hide resolved
#else
#define OS_LINUX
#endif
#endif

// It is also useful to know whether the operating system is POSIX compliant.
#if defined(OS_MACOS) || defined(OS_LINUX)
#if defined(OS_MACOS) || defined(OS_LINUX) || defined(OS_BSD)
#define OS_POSIX
#endif

Expand Down
33 changes: 31 additions & 2 deletions src/utils/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#include "retdec/utils/memory.h"
#include "retdec/utils/os.h"
#include <cstddef>

#ifdef OS_WINDOWS
#include <windows.h>
#elif defined(OS_MACOS)
#elif defined(OS_MACOS) || defined(OS_BSD)
#include <sys/types.h>
#include <sys/sysctl.h>
#else
Expand All @@ -32,7 +33,7 @@ namespace {
*/
bool limitSystemMemoryOnPOSIX(std::size_t limit) {
struct rlimit rl = {
.rlim_cur = limit, // Soft limit.
.rlim_cur = static_cast<rlim_t>(limit), // Soft limit.
.rlim_max = RLIM_INFINITY // Hard limit (ceiling for rlim_cur).
};
auto rc = setrlimit(RLIMIT_AS, &rl);
Expand Down Expand Up @@ -119,6 +120,30 @@ bool limitSystemMemoryOnMacOS(std::size_t limit) {
return limitSystemMemoryOnPOSIX(limit);
}

#elif defined(OS_BSD)

/**
* @brief Implementation of @c getTotalSystemMemory() on *BSD.
*
* AKA FreeBSD, DragonFly, NetBSD, OpenBSD, TrueOS, PCBSD
*/
std::size_t getTotalSystemMemoryOnBSD() {
int what[] = { CTL_HW, HW_PHYSMEM };
std::size_t value = 0;
std::size_t length = sizeof(value);
auto rc = sysctl(what, 2, &value, &length, nullptr, 0);
return rc != -1 ? value : 0;
}

/**
* @brief Implementation of @c limitSystemMemory() on *BSD.
*
* AKA FreeBSD, DragonFly, NetBSD, OpenBSD, TrueOS, PCBSD
*/
bool limitSystemMemoryOnBSD(std::size_t limit) {
return limitSystemMemoryOnPOSIX(limit);
}

#else

/*
Expand Down Expand Up @@ -151,6 +176,8 @@ std::size_t getTotalSystemMemory() {
return getTotalSystemMemoryOnWindows();
#elif defined(OS_MACOS)
return getTotalSystemMemoryOnMacOS();
#elif defined(OS_BSD)
return getTotalSystemMemoryOnBSD();
#else
return getTotalSystemMemoryOnLinux();
#endif
Expand All @@ -175,6 +202,8 @@ bool limitSystemMemory(std::size_t limit) {
return limitSystemMemoryOnWindows(limit);
#elif defined(OS_MACOS)
return limitSystemMemoryOnMacOS(limit);
#elif defined(OS_BSD)
return limitSystemMemoryOnBSD(limit);
#else
return limitSystemMemoryOnLinux(limit);
#endif
Expand Down