|
| 1 | +#include <stdint.h> |
| 2 | +#include <unistd.h> |
| 3 | + |
| 4 | +#define ALIGN (sizeof(size_t)) |
| 5 | +#define UCHAR_MAX 255 |
| 6 | +#define ONES ((size_t)-1 / UCHAR_MAX) |
| 7 | +#define HIGHS (ONES * (UCHAR_MAX / 2 + 1)) |
| 8 | +#define HASZERO(x) ((x) - ONES & ~(x) & HIGHS) |
| 9 | + |
| 10 | +#define JAVA_TOOL_OPTIONS_ENV_VAR_NAME "JAVA_TOOL_OPTIONS" |
| 11 | +#define JAVA_TOOL_OPTIONS_REQUIRE \ |
| 12 | + "-javaagent:" \ |
| 13 | + "/otel-auto-instrumentation-injector/instrumentation/jvm/javaagent.jar" |
| 14 | + |
| 15 | +extern char **__environ; |
| 16 | + |
| 17 | +size_t __strlen(const char *s) { |
| 18 | + const char *a = s; |
| 19 | + const size_t *w; |
| 20 | + for (; (uintptr_t)s % ALIGN; s++) |
| 21 | + if (!*s) |
| 22 | + return s - a; |
| 23 | + for (w = (const void *)s; !HASZERO(*w); w++) |
| 24 | + ; |
| 25 | + for (s = (const void *)w; *s; s++) |
| 26 | + ; |
| 27 | + return s - a; |
| 28 | +} |
| 29 | + |
| 30 | +char *__strchrnul(const char *s, int c) { |
| 31 | + size_t *w, k; |
| 32 | + |
| 33 | + c = (unsigned char)c; |
| 34 | + if (!c) |
| 35 | + return (char *)s + __strlen(s); |
| 36 | + |
| 37 | + for (; (uintptr_t)s % ALIGN; s++) |
| 38 | + if (!*s || *(unsigned char *)s == c) |
| 39 | + return (char *)s; |
| 40 | + k = ONES * c; |
| 41 | + for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w ^ k); w++) |
| 42 | + ; |
| 43 | + for (s = (void *)w; *s && *(unsigned char *)s != c; s++) |
| 44 | + ; |
| 45 | + return (char *)s; |
| 46 | +} |
| 47 | + |
| 48 | +char *__strcpy(char *restrict dest, const char *restrict src) { |
| 49 | + const unsigned char *s = src; |
| 50 | + unsigned char *d = dest; |
| 51 | + while ((*d++ = *s++)) |
| 52 | + ; |
| 53 | + return dest; |
| 54 | +} |
| 55 | + |
| 56 | +char *__strcat(char *restrict dest, const char *restrict src) { |
| 57 | + __strcpy(dest + __strlen(dest), src); |
| 58 | + return dest; |
| 59 | +} |
| 60 | + |
| 61 | +int __strcmp(const char *l, const char *r) { |
| 62 | + for (; *l == *r && *l; l++, r++) |
| 63 | + ; |
| 64 | + return *(unsigned char *)l - *(unsigned char *)r; |
| 65 | +} |
| 66 | + |
| 67 | +int __strncmp(const char *_l, const char *_r, size_t n) { |
| 68 | + const unsigned char *l = (void *)_l, *r = (void *)_r; |
| 69 | + if (!n--) |
| 70 | + return 0; |
| 71 | + for (; *l && *r && n && *l == *r; l++, r++, n--) |
| 72 | + ; |
| 73 | + return *l - *r; |
| 74 | +} |
| 75 | + |
| 76 | +char *__getenv(const char *name) { |
| 77 | + size_t l = __strchrnul(name, '=') - name; |
| 78 | + if (l && !name[l] && __environ) |
| 79 | + for (char **e = __environ; *e; e++) |
| 80 | + if (!__strncmp(name, *e, l) && l[*e] == '=') |
| 81 | + return *e + l + 1; |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +/* |
| 86 | + * Buffers of statically-allocated memory that we can use to safely return to |
| 87 | + * the program manipulated values of env vars without dynamic allocations. |
| 88 | + */ |
| 89 | +char cachedModifiedRuntimeOptionsValue[1012]; |
| 90 | + |
| 91 | +char *getenv(const char *name) { |
| 92 | + char *origValue = __getenv(name); |
| 93 | + int l = __strlen(name); |
| 94 | + |
| 95 | + char *javaToolOptionsVarName = JAVA_TOOL_OPTIONS_ENV_VAR_NAME; |
| 96 | + if (__strcmp(name, javaToolOptionsVarName) == 0) { |
| 97 | + if (__strlen(cachedModifiedRuntimeOptionsValue) == 0) { |
| 98 | + // No runtime environment variable has been requested before, |
| 99 | + // calculate the modified value and cache it. |
| 100 | + |
| 101 | + // Prepend our --require as the first item to the JAVA_TOOL_OPTIONS |
| 102 | + // string. |
| 103 | + char *javaToolOptionsRequire = JAVA_TOOL_OPTIONS_REQUIRE; |
| 104 | + __strcat(cachedModifiedRuntimeOptionsValue, javaToolOptionsRequire); |
| 105 | + } |
| 106 | + |
| 107 | + return cachedModifiedRuntimeOptionsValue; |
| 108 | + } |
| 109 | + |
| 110 | + return origValue; |
| 111 | +} |
0 commit comments