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

feat: compile to WebAssembly to eliminate native dependencies #8

Merged
merged 39 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
18bb8fc
wip: compile to WebAssembly to eliminate native dependencies
dsanders11 Sep 9, 2022
6b1dfd9
chore: add jobs option to build script
dsanders11 Sep 10, 2022
58057aa
ci: various config fixes
dsanders11 Sep 9, 2022
8f14a58
chore: attempt at fixing build issue on Windows
dsanders11 Sep 10, 2022
2998439
test: make tests Windows friendly
dsanders11 Sep 10, 2022
ad71ebc
test: fix resource content check
dsanders11 Sep 10, 2022
6da9fe1
build: check for emcmake
dsanders11 Sep 10, 2022
0a572e9
wip: initial porting of ELF injection
dsanders11 Sep 10, 2022
3a3e28c
wip: initial porting of PE code
dsanders11 Sep 10, 2022
f8de557
chore: check files exist in CLI
dsanders11 Sep 10, 2022
5969053
chore: disable more parts of LIEF
dsanders11 Sep 10, 2022
45c68d4
chore: change variable name for consistency
dsanders11 Sep 10, 2022
105b4f6
chore: remove TODO note
dsanders11 Sep 10, 2022
338f7c3
ci: add lint job
dsanders11 Sep 10, 2022
5971840
chore: only remove Mach-O signature if present
dsanders11 Sep 10, 2022
331fbc9
chore: update README instructions
dsanders11 Sep 10, 2022
bd38f70
docs: update usage example
dsanders11 Sep 11, 2022
5e3a606
chore: make main.js executable
dsanders11 Sep 11, 2022
ea821fc
ci: install dependencies on 'linux' executor
dsanders11 Sep 22, 2022
8a7a0b2
chore: reduce required Node.js version
dsanders11 Sep 22, 2022
11db130
ci: upload test results to CircleCI
dsanders11 Sep 23, 2022
577d0d3
chore: fix linting on Windows
dsanders11 Sep 23, 2022
5628f8c
test: refactor testing setup
dsanders11 Sep 23, 2022
9e7def1
chore: use dev suffix for version number
dsanders11 Sep 23, 2022
c48c54c
chore: more error handling for file operations
dsanders11 Sep 23, 2022
166b3dd
test: bump timeout on test
dsanders11 Sep 23, 2022
a0cd439
ci: tweak CI executors
dsanders11 Sep 23, 2022
2d5a691
test: tweak test
dsanders11 Sep 23, 2022
c329a99
ci: streamline CI config
dsanders11 Sep 23, 2022
eb32deb
test: don't use zx in tests
dsanders11 Sep 23, 2022
adc8e60
test: add more expectations around CLI output
dsanders11 Sep 23, 2022
5b02cf0
fix: use correct uppercase method
dsanders11 Sep 23, 2022
3c09a82
test: comment out expect for now
dsanders11 Sep 23, 2022
8b7334a
fix: use correct resource type for PE
dsanders11 Sep 23, 2022
1da4681
chore: improve build script on Windows
dsanders11 Sep 23, 2022
9bddb76
test: bump test timeout
dsanders11 Sep 23, 2022
4ecba28
chore: address review comments
dsanders11 Sep 23, 2022
ae6e5c9
ci: drop unused git config for Windows
dsanders11 Sep 24, 2022
cf1fa19
test: another bump to timeout for CI
dsanders11 Sep 24, 2022
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
124 changes: 97 additions & 27 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ executors:
debian:
resource_class: medium
docker:
- image: docker.io/library/debian:11
- image: docker.io/library/debian:bookworm

linux:
resource_class: medium
Expand All @@ -36,35 +36,71 @@ executors:
## COMMANDS ##

commands:
npm_install:
parameters:
<<: *common_parameters
steps:
- when:
condition:
equal: [ alpine, << parameters.os >> ]
steps:
- run: apk update
- run: apk add --no-cache nodejs npm

- when:
condition:
equal: [ debian, << parameters.os >> ]
steps:
- run: apt-get update
- run: apt-get install --no-install-recommends -y ca-certificates nodejs npm

- when:
condition:
equal: [ linux, << parameters.os >> ]
steps:
- run: sudo apt-get update
- run: sudo apt-get install --no-install-recommends -y ca-certificates nodejs npm

- when:
condition:
equal: [ "win/default", << parameters.os >> ]
steps:
- run: git config --global core.autocrlf false
- run: git config --global core.eol lf
dsanders11 marked this conversation as resolved.
Show resolved Hide resolved

- run: npm install

prepare:
parameters:
<<: *common_parameters
steps:
# NOTE: minimal effort to allow moving most conditionals out of ci config
- when:
condition:
equal: [ alpine, << parameters.os >> ]
steps:
- run: apk add -U make
- run: apk update
- run: apk add --no-cache build-base git ninja cmake python3 python3-dev py3-setuptools

- when:
condition:
equal: [ debian, << parameters.os >> ]
steps:
- run: apt-get update -y && apt-get install -y make
- run: apt-get update
- run: apt-get install --no-install-recommends -y build-essential git ninja-build cmake python3 python3-dev python3-setuptools

- when:
condition:
equal: [ linux, << parameters.os >> ]
steps:
- run: sudo apt-get update -y && sudo apt-get install -y make
- run: sudo apt-get update
- run: sudo apt-get install --no-install-recommends -y build-essential git ninja-build cmake

- when:
condition:
not:
equal: [ "win/default", << parameters.os >> ]
equal: [ macos, << parameters.os >> ]
steps:
- run: make install-deps EXECUTOR=<< parameters.os >>
- run: brew install cmake
- run: brew install ninja

- when:
condition:
Expand All @@ -73,6 +109,28 @@ commands:
- run: choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=User"'
- run: choco install ninja

- run: git clone https://github.com/emscripten-core/emsdk.git

- when:
condition:
equal: [ "win/default", << parameters.os >> ]
steps:
- run:
command: cd emsdk & emsdk install latest
shell: cmd.exe

- when:
condition:
not:
equal: [ "win/default", << parameters.os >> ]
steps:
- run: cd emsdk && ./emsdk install latest
- run: cd emsdk && ./emsdk activate latest

lint:
steps:
- run: npm run lint

build:
parameters:
<<: *common_parameters
Expand All @@ -94,76 +152,88 @@ commands:
# compilation terminated
#
# force overwrite job count here:
command: make lief JOBS=3
command: cd emsdk && source ./emsdk_env.sh && cd .. && npm run build -- --jobs=3

- when:
condition:
equal: [ "win/default", << parameters.os >> ]
steps:
- run:
command: refreshenv & cd vendor\lief & python ./setup.py --ninja build_ext -b ..\..\dist\lief
command: refreshenv & cd emsdk & emsdk activate latest & cd .. & npm run build
shell: cmd.exe

- persist_to_workspace:
root: .
paths:
- dist/lief
- build
- dist
- emsdk

test:
steps:
- attach_workspace:
at: .

- run: make check
- run: npm test -- --reporter mocha-junit-reporter

- store_test_results:
path: .

## JOBS ##

jobs:
lint:
parameters:
<<: *common_parameters
executor: { name: << parameters.os >> }
steps:
- checkout
- npm_install:
os: << parameters.os >>
- lint

build:
parameters:
<<: *common_parameters
executor: { name: << parameters.os >> }
steps:
- checkout
- npm_install:
os: << parameters.os >>
- prepare:
os: << parameters.os >>
- build:
os: << parameters.os >>
- store_artifacts:
path: dist
- store_artifacts:
path: postject-api.h
- store_artifacts:
path: postject.py

test:
parameters:
<<: *common_parameters
executor: { name: << parameters.os >> }
steps:
- checkout
- prepare:
- npm_install:
os: << parameters.os >>
- test

## WORKFLOWS ##

build_matrix: &build_matrix
matrix:
parameters:
os: [ alpine, debian, linux, macos, "win/default" ]

test_matrix: &test_matrix
matrix: &matrix
matrix:
parameters:
os: [ alpine, debian, linux, macos ]
os: [ debian, linux, macos, "win/default" ]

workflows:
postject:
jobs:
- lint:
matrix:
parameters:
os: [ linux, macos ]
- build:
<<: *build_matrix
<<: *matrix
- test:
<<: *test_matrix
requires: [ build-<<matrix.os>> ]
<<: *matrix
requires: [ build-<< matrix.os >> ]

4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,4 @@ Thumbs.db

vendor/lief/lief.*.so
vendor/lief/build/
examples/data.txt
examples/test
examples/*.o
build/
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.9)
set(CMAKE_CXX_STANDARD 11)

project(postject)

# Disable unnecessary LIEF options
option(LIEF_DOC "Build LIEF docs" OFF)
option(LIEF_PYTHON_API "Build LIEF Python API" OFF)
option(LIEF_EXAMPLES "Build LIEF examples" OFF)
option(LIEF_TESTS "Build LIEF tests" OFF)
option(LIEF_ART "Build LIEF with ART module" OFF)
option(LIEF_DEX "Build LIEF with DEX module" OFF)
option(LIEF_VDEX "Build LIEF with VDEX module" OFF)
option(LIEF_OAT "Build LIEF with OAT module" OFF)

if(MSVC)
set(LIEF_USE_CRT_RELEASE "MT" CACHE STRING "LIEF CRT option")
endif()

add_subdirectory(vendor/lief)

add_executable(postject src/postject.cpp)
set_target_properties(postject PROPERTIES LINK_FLAGS "-sMODULARIZE=1 --bind")

if(MSVC)
set_property(TARGET postject PROPERTY LINK_FLAGS /NODEFAULTLIB:MSVCRT)
endif()

target_link_libraries(postject PUBLIC LIEF::LIEF)
20 changes: 0 additions & 20 deletions Makefile

This file was deleted.

37 changes: 25 additions & 12 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,45 @@ Easily inject arbitrary read-only resources into executable formats

## Building

### Windows

You'll need CMake and Ninja to build LIEF.
### Prerequisites

```sh
$ cd vendor\lief
$ python3 ./setup.py --ninja build_ext -b ..\..\dist\lief
```
* CMake
* Ninja
* [Emscripten (emsdk)](https://emscripten.org/docs/getting_started/downloads.html)

### macOS / Linux
### Build Command

```sh
$ sudo make install-deps
$ make lief
$ npm run build
```

The final output is placed in `dist/`, with `main.js` being the
entrypoint.

## Usage

```sh
$ ./postject.py --macho-segment-name __ELECTRON /Users/dsanders/electron/src/out/Testing/Electron.app/Contents/Frameworks/Electron\ Framework.framework/Electron\ Framework app_asar /Users/dsanders/test.asar
$ postject -h
Usage: postject [options] <filename> <resource_name> <resource>

Inject arbitrary read-only resources into an executable for use at runtime

Arguments:
filename The executable to inject into
resource_name The resource name to use (section name on Mach-O and ELF, resource name for PE)
resource The resource to inject

Options:
--macho-segment-name <segment_name> Name for the Mach-O segment (default: "__POSTJECT")
--output-api-header Output the API header to stdout
--overwrite Overwrite the resource if it already exists
-h, --help display help for command
```

### Testing

```sh
$ make check
$ npm test
```

## Design
Expand Down
27 changes: 0 additions & 27 deletions build/deps.mk

This file was deleted.

14 changes: 0 additions & 14 deletions build/system.mk

This file was deleted.

Loading