-
Notifications
You must be signed in to change notification settings - Fork 18
src: add GetBuildId helper function #339
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||||||||||||||||||||||||||||
| #include "nsolid_elf_utils.h" | ||||||||||||||||||||||||||||||||||
| #include "nsolid_util.h" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #include <fcntl.h> | ||||||||||||||||||||||||||||||||||
| #include <libelf.h> | ||||||||||||||||||||||||||||||||||
| #include <gelf.h> | ||||||||||||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||||||||||||
| #include <cstring> | ||||||||||||||||||||||||||||||||||
| #include <map> | ||||||||||||||||||||||||||||||||||
| #include "uv.h" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| namespace node { | ||||||||||||||||||||||||||||||||||
| namespace nsolid { | ||||||||||||||||||||||||||||||||||
| namespace elf_utils { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| int GetBuildId(const std::string& path, std::string* build_id) { | ||||||||||||||||||||||||||||||||||
| static std::map<std::string, std::string> build_id_cache_; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Elf* e; | ||||||||||||||||||||||||||||||||||
| Elf_Scn* scn = nullptr; | ||||||||||||||||||||||||||||||||||
| GElf_Shdr shdr; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (build_id_cache_.find(path) != build_id_cache_.end()) { | ||||||||||||||||||||||||||||||||||
| *build_id = build_id_cache_[path]; | ||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| int ret; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ret = 0; | ||||||||||||||||||||||||||||||||||
| if (elf_version(EV_CURRENT) == EV_NONE) { | ||||||||||||||||||||||||||||||||||
| return elf_errno(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| int fd = open(path.c_str(), O_RDONLY); | ||||||||||||||||||||||||||||||||||
| if (fd < 0) { | ||||||||||||||||||||||||||||||||||
| return -errno; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| e = elf_begin(fd, ELF_C_READ, nullptr); | ||||||||||||||||||||||||||||||||||
| if (!e) { | ||||||||||||||||||||||||||||||||||
| ret = elf_errno(); | ||||||||||||||||||||||||||||||||||
| goto error; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| size_t shstrndx; | ||||||||||||||||||||||||||||||||||
| if (elf_getshdrstrndx(e, &shstrndx) != 0) { | ||||||||||||||||||||||||||||||||||
| ret = elf_errno(); | ||||||||||||||||||||||||||||||||||
| goto end_error; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| *build_id = std::string(""); | ||||||||||||||||||||||||||||||||||
| while ((scn = elf_nextscn(e, scn)) != nullptr) { | ||||||||||||||||||||||||||||||||||
| if (gelf_getshdr(scn, &shdr) != &shdr) { | ||||||||||||||||||||||||||||||||||
| ret = elf_errno(); | ||||||||||||||||||||||||||||||||||
| goto end_error; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| char* name = elf_strptr(e, shstrndx, shdr.sh_name); | ||||||||||||||||||||||||||||||||||
| if (name && strcmp(name, ".note.gnu.build-id") == 0) { | ||||||||||||||||||||||||||||||||||
| Elf_Data* data = elf_getdata(scn, nullptr); | ||||||||||||||||||||||||||||||||||
| if (data && data->d_size >= 16) { | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add bounds checking for note data size. The current check - if (data && data->d_size >= 16) {
+ if (data && data->d_size >= 12) { // Minimum for note header
// ELF Note header: namesz(4), descsz(4), type(4) + name padding
// Compute offset to build-id properly
uint32_t* note = reinterpret_cast<uint32_t*>(data->d_buf);
uint32_t namesz = note[0];
uint32_t descsz = note[1];
+
+ // Verify we have enough data for the complete note
+ size_t name_end = 12 + ((namesz + 3) & ~3);
+ if (data->d_size < name_end + descsz) {
+ continue; // Skip malformed note
+ }
+
- // Name starts at offset 12
- // Descriptor (build-id) starts at next aligned offset
- size_t name_end = 12 + ((namesz + 3) & ~3);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| // ELF Note header: namesz(4), descsz(4), type(4) + name padding | ||||||||||||||||||||||||||||||||||
| // Compute offset to build-id properly | ||||||||||||||||||||||||||||||||||
| uint32_t* note = reinterpret_cast<uint32_t*>(data->d_buf); | ||||||||||||||||||||||||||||||||||
| uint32_t namesz = note[0]; | ||||||||||||||||||||||||||||||||||
| uint32_t descsz = note[1]; | ||||||||||||||||||||||||||||||||||
| // Name starts at offset 12 | ||||||||||||||||||||||||||||||||||
| // Descriptor (build-id) starts at next aligned offset | ||||||||||||||||||||||||||||||||||
| size_t name_end = 12 + ((namesz + 3) & ~3); | ||||||||||||||||||||||||||||||||||
| uint8_t* id = reinterpret_cast<uint8_t*>(data->d_buf) + name_end; | ||||||||||||||||||||||||||||||||||
| *build_id = utils::buffer_to_hex(id, descsz); | ||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (build_id->empty()) { | ||||||||||||||||||||||||||||||||||
| ret = UV_ENOENT; | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| build_id_cache_[path] = *build_id; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+79
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix thread safety issue with cache update. The cache update outside the mutex protection (from the earlier thread safety fix) will cause race conditions. If implementing the mutex fix suggested earlier, ensure the cache update is also protected: if (build_id->empty()) {
ret = UV_ENOENT;
} else {
+ std::lock_guard<std::mutex> lock(cache_mutex_);
build_id_cache_[path] = *build_id;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| end_error: | ||||||||||||||||||||||||||||||||||
| elf_end(e); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| error: | ||||||||||||||||||||||||||||||||||
| close(fd); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return ret; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| } // namespace elf_utils | ||||||||||||||||||||||||||||||||||
| } // namespace nsolid | ||||||||||||||||||||||||||||||||||
| } // namespace node | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef SRC_NSOLID_NSOLID_ELF_UTILS_H_ | ||
| #define SRC_NSOLID_NSOLID_ELF_UTILS_H_ | ||
|
|
||
| #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace node { | ||
| namespace nsolid { | ||
|
|
||
| namespace elf_utils { | ||
| int GetBuildId(const std::string& path, std::string* build_id); | ||
| } // namespace elf_utils | ||
| } // namespace nsolid | ||
| } // namespace node | ||
|
|
||
| #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #endif // SRC_NSOLID_NSOLID_ELF_UTILS_H_ |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||
| #include <node.h> | ||||||||
| #include <v8.h> | ||||||||
| #include <string> | ||||||||
| #if defined(__linux__) | ||||||||
| #include "../../../src/nsolid/nsolid_elf_utils.h" | ||||||||
| #endif | ||||||||
|
|
||||||||
| using v8::FunctionCallbackInfo; | ||||||||
| using v8::Isolate; | ||||||||
| using v8::String; | ||||||||
| using v8::Value; | ||||||||
|
|
||||||||
| static void GetBuildId(const FunctionCallbackInfo<Value>& args) { | ||||||||
| #if defined(__linux__) | ||||||||
| Isolate* isolate = args.GetIsolate(); | ||||||||
| assert(args[0]->IsString()); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing include for assert The code uses +#include <cassert>
#include <node.h>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
| v8::String::Utf8Value path_utf8(isolate, args[0]); | ||||||||
| std::string path(*path_utf8, path_utf8.length()); | ||||||||
| std::string build_id; | ||||||||
| int res = node::nsolid::elf_utils::GetBuildId(path, &build_id); | ||||||||
| if (res != 0) { | ||||||||
| return; | ||||||||
| } | ||||||||
|
|
||||||||
| args.GetReturnValue().Set( | ||||||||
| String::NewFromUtf8(isolate, build_id.c_str()).ToLocalChecked()); | ||||||||
| #endif | ||||||||
| } | ||||||||
|
santigimeno marked this conversation as resolved.
|
||||||||
|
|
||||||||
| NODE_MODULE_INIT(/* exports, module, context */) { | ||||||||
| NODE_SET_METHOD(exports, "getBuildId", GetBuildId); | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| 'targets': [ | ||
| { | ||
| 'target_name': 'binding', | ||
| 'sources': [ 'binding.cc' ], | ||
| 'includes': ['../common.gypi'], | ||
| 'defines': [ 'NODE_WANT_INTERNALS=1' ], | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 'use strict'; | ||
| const common = require('../../common'); | ||
| const assert = require('assert'); | ||
| const { execSync } = require('child_process'); | ||
| const process = require('process'); | ||
|
|
||
| // Only run on Linux | ||
| if (process.platform !== 'linux') { | ||
| console.log('Skipping: nsolid-elf-utils only supported on Linux'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const bindingPath = require.resolve(`./build/${common.buildType}/binding`); | ||
| const binding = require(bindingPath); | ||
|
santigimeno marked this conversation as resolved.
|
||
|
|
||
| const expected = | ||
| execSync(`readelf -n ${process.execPath} | awk '/Build ID/ { print $3 }'`, | ||
| { encoding: 'utf8' }).trim(); | ||
|
santigimeno marked this conversation as resolved.
|
||
|
|
||
| const buildId = binding.getBuildId(process.execPath); | ||
| assert.strictEqual(buildId, | ||
| expected, | ||
| `Mismatch: addon='${buildId}', readelf='${expected}'`); | ||
Uh oh!
There was an error while loading. Please reload this page.