Comments (6)
The implementation should be something like https://github.com/iovisor/bpftrace/blob/1326f040a0f88287ccbc8c18fe8956bca4cc225d/src/utils.cpp#L1017-L1050. I'll see if I can find any obvious differences. Meanwhile maybe @dsanders11 and @robertgzr could help?
from postject.
Also, cc @nodejs/single-executable if anyone else also has any clue
from postject.
The implementation should be something like https://github.com/iovisor/bpftrace/blob/1326f040a0f88287ccbc8c18fe8956bca4cc225d/src/utils.cpp#L1017-L1050. I'll see if I can find any obvious differences. Meanwhile maybe @dsanders11 and @robertgzr could help?
That implementation is a bit different - it's looking for SHT_NOTE
using the section header table (SHT), while Postject's implementation uses PT_NOTE
, which is a note segment. Sections are contained within segments, but they're a linker-time concept. The SHT is not used at run time and can be stripped from the executable. The the Wiki article on ELF:
The segments contain information that is needed for run time execution of the file, while sections contain important data for linking and relocation.
While SHT_NOTE
sections will exist inside of a PT_NOTE
segment, you can't rely on the SHT to find them at run time since that information may be stripped, so Postject walks the segments, rather than sections.
I don't see anything obvious, so I'll try to dig into this later and see what I can find. There might be some slight difference on ppc64le
that's not being accounted for in the current implementation which leads to using the wrong offset for the pointers values.
from postject.
Hmm, weird find - I'm able to reproduce this on Linux when I compile this on an x64 Ubuntu Linux:
test.cc
#include <iostream>
#include <string>
#include "postject-api.h"
int main() {
size_t size = 0;
if (postject_has_resource()) {
const void* ptr = postject_find_resource("foobar", &size, nullptr);
if (ptr == NULL) {
std::cerr << "ptr must not be NULL." << std::endl;
exit(1);
}
if (size == 0) {
std::cerr << "size must not be 0." << std::endl;
exit(1);
}
std::cout << std::string(static_cast<const char*>(ptr), size) << std::endl;
} else {
const void* ptr = postject_find_resource("foobar", &size, nullptr); // <- this call segfaults
if (ptr != nullptr) {
std::cerr << "ptr must be nullptr." << std::endl;
exit(1);
}
if (size > 0) {
std::cerr << "size must not be greater than 0." << std::endl;
exit(1);
}
std::cout << "Hello world" << std::endl;
}
return 0;
}
(postject-api.h
- from https://github.com/nodejs/postject/blob/35343439cac8c488f2596d7c4c1dddfec1fddcae/postject-api.h)
using clang but it works fine with gcc. 🤔
$ g++ test.cc
$ ./a.out
Hello world
$ clang++ test.cc
$ ./a.out
Segmentation fault (core dumped)
$ clang++ -g test.cc
$ gdb -q a.out
Reading symbols from a.out...
(gdb) run
Starting program: /home/parallels/Desktop/temp/project/trash/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00000000004015f0 in postject_find_resource (name=0x402004 "foobar", size=0x7fffffffdf00, options=0x0) at ./postject-api.h:141
141 if (note->n_namesz != 0 && note->n_descsz != 0 &&
(gdb) bt
#0 0x00000000004015f0 in postject_find_resource (name=0x402004 "foobar", size=0x7fffffffdf00, options=0x0) at ./postject-api.h:141
#1 0x0000000000401401 in main () at test.cc:21
(gdb) quit
A debugging session is active.
Inferior 1 [process 187727] will be killed.
Quit anyway? (y or n) y
System info:
$ uname -a
Linux parallels-Parallels-Virtual-Platform 5.13.0-40-generic #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang++ --version
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
from postject.
FWIW, I tried using dl_iterate_phdr
to implement the runtime API for Linux using https://github.com/percona/percona-server/blob/5486efdbebd4e9a6fd94af5410853137a73d551b/mysys/build_id.cc as the base and it doesn't segfault when I compile with clang++.
@dsanders11 I'll send a PR for this soon if you're not aware of anything obviously wrong with function which I haven't considered.
from postject.
Fix - #77
from postject.
Related Issues (20)
- Internally use `objcopy --add-section` on macOS and Linux HOT 10
- Unable to make build on macOS 13 due to deprecated functions HOT 3
- Print a message when the build is successful HOT 1
- Weired output message `The signature seems corrupted!` on Windows HOT 7
- SEA created following Node.js documentation doesn't work HOT 9
- Can't read and write to target executable HOT 8
- single executable applications with warning HOT 4
- Investigate why running the wasm blob in Postject is slow
- upx compatibility(Cannot pack after postject-ed) HOT 2
- Question about the sentinel fuse. HOT 5
- Error: buffer[colonIndex].charCodeAt is not a function HOT 2
- stripping postjected executable causes segmentation fault
- Can't read and write to target executable HOT 2
- Error: Multiple occurences of sentinel found in the binary HOT 5
- SEA issue at step running postject to inject the blob HOT 1
- Error: Could not find the sentinel NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 in the binary
- injecting multiple resources to the same binary.
- Signing a `darwin-x64` node executable on `darwin-arm64` leads to a segmentation fault when started [SOLVED] HOT 1
- Types/Typescript support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from postject.