Skip to content

Instantly share code, notes, and snippets.

@yurynix
Last active July 7, 2022 15:20
Show Gist options
  • Save yurynix/b8d7597a1cd81f1a2e12fec131a81c90 to your computer and use it in GitHub Desktop.
Save yurynix/b8d7597a1cd81f1a2e12fec131a81c90 to your computer and use it in GitHub Desktop.
Fake cpu count for node

cpu-count.js:

console.log(require('os').cpus().length);

To compile:

gcc -shared -fPIC intercept.c -o intercept.so -ldl

To run:

LD_PRELOAD=./intercept.so node cpu-count.js 
processor : 0
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp flagm2 frint
CPU implementer : 0x00
CPU architecture: 8
CPU variant : 0x0
cpu 82016 2676 148601 70586032 4466 0 3988 0 0 0
cpu0 20312 667 36767 17647814 1168 0 1360 0 0 0
intr 34168354 0 11705282 209637 0 0 0 1 0 0 0 21812717 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 479 0 0 0 0 23346 25949 27838 24097 0 180561 158092 70 0 0 0 0 0 139 59 42 45
ctxt 64298657
btime 1657020932
processes 32061
procs_running 1
procs_blocked 0
softirq 6051752 6 1731826 51 496308 1523 0 1471 2558365 0 1262202
struct timeval {
unsigned long tv_sec; /* seconds */
unsigned long tv_usec; /* microseconds */
};
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
int gettimeofday(struct timeval * tv, struct timezone * tz) {
tv->tv_sec = 0;
tv->tv_usec = 0;
return 0;
}
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
typedef int (*orig_open_f_type)(const char *pathname, int flags);
orig_open_f_type orig_open64 = NULL;
int open64(const char *pathname, int flags) {
//printf("open64('%s', %d)\n",pathname, flags);
if (0 == strncmp(pathname, "/proc/cpuinfo", strlen(pathname))) {
printf("Redirecting cpuinfo!\n");
return orig_open64("/fake-cpuinfo", flags);
}
if (0 == strncmp(pathname, "/proc/stat", strlen(pathname))) {
printf("Redirecting /proc/stat\n");
return orig_open64("/fake-stat", flags);
}
int fd = orig_open64(pathname, flags);
return fd;
}
__attribute__((constructor)) void init() {
printf("Intercepting open() calls\n");
if (orig_open64 == NULL) {
orig_open64 = (orig_open_f_type)dlsym(RTLD_NEXT, "open64");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment