Skip to content

Commit 1fa49dd

Browse files
authored
feat: compile to WebAssembly to eliminate native dependencies (#8)
* wip: compile to WebAssembly to eliminate native dependencies * chore: add jobs option to build script * ci: various config fixes * chore: attempt at fixing build issue on Windows * test: make tests Windows friendly * test: fix resource content check * build: check for emcmake * wip: initial porting of ELF injection * wip: initial porting of PE code * chore: check files exist in CLI * chore: disable more parts of LIEF * chore: change variable name for consistency * chore: remove TODO note * ci: add lint job * chore: only remove Mach-O signature if present * chore: update README instructions * docs: update usage example * chore: make main.js executable * ci: install dependencies on 'linux' executor * chore: reduce required Node.js version * ci: upload test results to CircleCI * chore: fix linting on Windows * test: refactor testing setup * chore: use dev suffix for version number * chore: more error handling for file operations * test: bump timeout on test Was timing out on Ubuntu executor on CI * ci: tweak CI executors * test: tweak test * ci: streamline CI config * test: don't use zx in tests * test: add more expectations around CLI output * fix: use correct uppercase method * test: comment out expect for now * fix: use correct resource type for PE * chore: improve build script on Windows * test: bump test timeout * chore: address review comments * ci: drop unused git config for Windows * test: another bump to timeout for CI
1 parent 284ef7d commit 1fa49dd

23 files changed

+4043
-467
lines changed

.circleci/config.yml

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ executors:
2121
debian:
2222
resource_class: medium
2323
docker:
24-
- image: docker.io/library/debian:11
24+
- image: docker.io/library/debian:bookworm
2525

2626
linux:
2727
resource_class: medium
@@ -36,35 +36,64 @@ executors:
3636
## COMMANDS ##
3737

3838
commands:
39+
npm_install:
40+
parameters:
41+
<<: *common_parameters
42+
steps:
43+
- when:
44+
condition:
45+
equal: [ alpine, << parameters.os >> ]
46+
steps:
47+
- run: apk update
48+
- run: apk add --no-cache nodejs npm
49+
50+
- when:
51+
condition:
52+
equal: [ debian, << parameters.os >> ]
53+
steps:
54+
- run: apt-get update
55+
- run: apt-get install --no-install-recommends -y ca-certificates nodejs npm
56+
57+
- when:
58+
condition:
59+
equal: [ linux, << parameters.os >> ]
60+
steps:
61+
- run: sudo apt-get update
62+
- run: sudo apt-get install --no-install-recommends -y ca-certificates nodejs npm
63+
64+
- run: npm install
65+
3966
prepare:
4067
parameters:
4168
<<: *common_parameters
4269
steps:
43-
# NOTE: minimal effort to allow moving most conditionals out of ci config
4470
- when:
4571
condition:
4672
equal: [ alpine, << parameters.os >> ]
4773
steps:
48-
- run: apk add -U make
74+
- run: apk update
75+
- run: apk add --no-cache build-base git ninja cmake python3 python3-dev py3-setuptools
4976

5077
- when:
5178
condition:
5279
equal: [ debian, << parameters.os >> ]
5380
steps:
54-
- run: apt-get update -y && apt-get install -y make
81+
- run: apt-get update
82+
- run: apt-get install --no-install-recommends -y build-essential git ninja-build cmake python3 python3-dev python3-setuptools
5583

5684
- when:
5785
condition:
5886
equal: [ linux, << parameters.os >> ]
5987
steps:
60-
- run: sudo apt-get update -y && sudo apt-get install -y make
88+
- run: sudo apt-get update
89+
- run: sudo apt-get install --no-install-recommends -y build-essential git ninja-build cmake
6190

6291
- when:
6392
condition:
64-
not:
65-
equal: [ "win/default", << parameters.os >> ]
93+
equal: [ macos, << parameters.os >> ]
6694
steps:
67-
- run: make install-deps EXECUTOR=<< parameters.os >>
95+
- run: brew install cmake
96+
- run: brew install ninja
6897

6998
- when:
7099
condition:
@@ -73,6 +102,28 @@ commands:
73102
- run: choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=User"'
74103
- run: choco install ninja
75104

105+
- run: git clone https://github.com/emscripten-core/emsdk.git
106+
107+
- when:
108+
condition:
109+
equal: [ "win/default", << parameters.os >> ]
110+
steps:
111+
- run:
112+
command: cd emsdk & emsdk install latest
113+
shell: cmd.exe
114+
115+
- when:
116+
condition:
117+
not:
118+
equal: [ "win/default", << parameters.os >> ]
119+
steps:
120+
- run: cd emsdk && ./emsdk install latest
121+
- run: cd emsdk && ./emsdk activate latest
122+
123+
lint:
124+
steps:
125+
- run: npm run lint
126+
76127
build:
77128
parameters:
78129
<<: *common_parameters
@@ -94,76 +145,88 @@ commands:
94145
# compilation terminated
95146
#
96147
# force overwrite job count here:
97-
command: make lief JOBS=3
148+
command: cd emsdk && source ./emsdk_env.sh && cd .. && npm run build -- --jobs=3
98149

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

107158
- persist_to_workspace:
108159
root: .
109160
paths:
110-
- dist/lief
161+
- build
162+
- dist
163+
- emsdk
111164

112165
test:
113166
steps:
114167
- attach_workspace:
115168
at: .
116169

117-
- run: make check
170+
- run: npm test -- --reporter mocha-junit-reporter
171+
172+
- store_test_results:
173+
path: .
118174

119175
## JOBS ##
120176

121177
jobs:
178+
lint:
179+
parameters:
180+
<<: *common_parameters
181+
executor: { name: << parameters.os >> }
182+
steps:
183+
- checkout
184+
- npm_install:
185+
os: << parameters.os >>
186+
- lint
187+
122188
build:
123189
parameters:
124190
<<: *common_parameters
125191
executor: { name: << parameters.os >> }
126192
steps:
127193
- checkout
194+
- npm_install:
195+
os: << parameters.os >>
128196
- prepare:
129197
os: << parameters.os >>
130198
- build:
131199
os: << parameters.os >>
132200
- store_artifacts:
133201
path: dist
134-
- store_artifacts:
135-
path: postject-api.h
136-
- store_artifacts:
137-
path: postject.py
138202

139203
test:
140204
parameters:
141205
<<: *common_parameters
142206
executor: { name: << parameters.os >> }
143207
steps:
144208
- checkout
145-
- prepare:
209+
- npm_install:
146210
os: << parameters.os >>
147211
- test
148212

149213
## WORKFLOWS ##
150214

151-
build_matrix: &build_matrix
152-
matrix:
153-
parameters:
154-
os: [ alpine, debian, linux, macos, "win/default" ]
155-
156-
test_matrix: &test_matrix
215+
matrix: &matrix
157216
matrix:
158217
parameters:
159-
os: [ alpine, debian, linux, macos ]
218+
os: [ debian, linux, macos, "win/default" ]
160219

161220
workflows:
162221
postject:
163222
jobs:
223+
- lint:
224+
matrix:
225+
parameters:
226+
os: [ linux, macos ]
164227
- build:
165-
<<: *build_matrix
228+
<<: *matrix
166229
- test:
167-
<<: *test_matrix
168-
requires: [ build-<<matrix.os>> ]
230+
<<: *matrix
231+
requires: [ build-<< matrix.os >> ]
169232

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,4 @@ Thumbs.db
5050

5151
vendor/lief/lief.*.so
5252
vendor/lief/build/
53-
examples/data.txt
54-
examples/test
55-
examples/*.o
53+
build/

CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
set(CMAKE_CXX_STANDARD 11)
3+
4+
project(postject)
5+
6+
# Disable unnecessary LIEF options
7+
option(LIEF_DOC "Build LIEF docs" OFF)
8+
option(LIEF_PYTHON_API "Build LIEF Python API" OFF)
9+
option(LIEF_EXAMPLES "Build LIEF examples" OFF)
10+
option(LIEF_TESTS "Build LIEF tests" OFF)
11+
option(LIEF_ART "Build LIEF with ART module" OFF)
12+
option(LIEF_DEX "Build LIEF with DEX module" OFF)
13+
option(LIEF_VDEX "Build LIEF with VDEX module" OFF)
14+
option(LIEF_OAT "Build LIEF with OAT module" OFF)
15+
16+
if(MSVC)
17+
set(LIEF_USE_CRT_RELEASE "MT" CACHE STRING "LIEF CRT option")
18+
endif()
19+
20+
add_subdirectory(vendor/lief)
21+
22+
add_executable(postject src/postject.cpp)
23+
set_target_properties(postject PROPERTIES LINK_FLAGS "-sMODULARIZE=1 --bind")
24+
25+
if(MSVC)
26+
set_property(TARGET postject PROPERTY LINK_FLAGS /NODEFAULTLIB:MSVCRT)
27+
endif()
28+
29+
target_link_libraries(postject PUBLIC LIEF::LIEF)

Makefile

Lines changed: 0 additions & 20 deletions
This file was deleted.

README.markdown

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,45 @@ Easily inject arbitrary read-only resources into executable formats
77

88
## Building
99

10-
### Windows
11-
12-
You'll need CMake and Ninja to build LIEF.
10+
### Prerequisites
1311

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

19-
### macOS / Linux
16+
### Build Command
2017

2118
```sh
22-
$ sudo make install-deps
23-
$ make lief
19+
$ npm run build
2420
```
2521

22+
The final output is placed in `dist/`, with `main.js` being the
23+
entrypoint.
24+
2625
## Usage
2726

2827
```sh
29-
$ ./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
28+
$ postject -h
29+
Usage: postject [options] <filename> <resource_name> <resource>
30+
31+
Inject arbitrary read-only resources into an executable for use at runtime
32+
33+
Arguments:
34+
filename The executable to inject into
35+
resource_name The resource name to use (section name on Mach-O and ELF, resource name for PE)
36+
resource The resource to inject
37+
38+
Options:
39+
--macho-segment-name <segment_name> Name for the Mach-O segment (default: "__POSTJECT")
40+
--output-api-header Output the API header to stdout
41+
--overwrite Overwrite the resource if it already exists
42+
-h, --help display help for command
3043
```
3144
3245
### Testing
3346
3447
```sh
35-
$ make check
48+
$ npm test
3649
```
3750
3851
## Design

build/deps.mk

Lines changed: 0 additions & 27 deletions
This file was deleted.

build/system.mk

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)