| 1 |
const std = @import("std"); |
| 2 |
const fs = std.fs; |
| 3 |
const logger = @import("../logger.zig"); |
| 4 |
const linked_libraries = @import("linked_libraries/root.zig"); |
| 5 |
|
| 6 |
extern "c" fn getenv(name: [*:0]const u8) ?[*:0]const u8; |
| 7 |
|
| 8 |
/// A value in the integration environment: either a single string or a list of strings. |
| 9 |
pub const EnvironmentValue = union(enum) { |
| 10 |
string: []const u8, |
| 11 |
list: []const []const u8, |
| 12 |
|
| 13 |
pub fn jsonStringify(self: @This(), jw: anytype) !void { |
| 14 |
switch (self) { |
| 15 |
.string => |s| try jw.write(s), |
| 16 |
.list => |l| try jw.write(l), |
| 17 |
} |
| 18 |
} |
| 19 |
}; |
| 20 |
|
| 21 |
/// Self-reported environment information provided by integrations (e.g. compiler version, runtime details). |
| 22 |
const IntegrationEnvironmentEntries = std.json.ArrayHashMap(EnvironmentValue); |
| 23 |
const IntegrationEnvironmentMap = std.json.ArrayHashMap(IntegrationEnvironmentEntries); |
| 24 |
|
| 25 |
const LinkedLibrariesMap = std.json.ArrayHashMap(linked_libraries.LibraryEntry); |
| 26 |
|
| 27 |
const EnvironmentJson = struct { |
| 28 |
integration_environment: IntegrationEnvironmentMap = .{}, |
| 29 |
linked_libraries: LinkedLibrariesMap = .{}, |
| 30 |
}; |
| 31 |
|
| 32 |
pub const Environment = struct { |
| 33 |
allocator: std.mem.Allocator, |
| 34 |
data: EnvironmentJson = .{}, |
| 35 |
libs: linked_libraries.LinkedLibraries, |
| 36 |
|
| 37 |
const Self = @This(); |
| 38 |
|
| 39 |
pub fn init(alloc: std.mem.Allocator) Self { |
| 40 |
return .{ |
| 41 |
.allocator = alloc, |
| 42 |
.libs = linked_libraries.LinkedLibraries.init(alloc), |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
pub fn deinit(self: *Self) void { |
| 47 |
var int_it = self.data.integration_environment.map.iterator(); |
| 48 |
while (int_it.next()) |int_entry| { |
| 49 |
var entry_it = int_entry.value_ptr.map.iterator(); |
| 50 |
while (entry_it.next()) |kv| { |
| 51 |
self.allocator.free(kv.key_ptr.*); |
| 52 |
self.freeEnvironmentValue(kv.value_ptr.*); |
| 53 |
} |
| 54 |
int_entry.value_ptr.map.deinit(self.allocator); |
| 55 |
self.allocator.free(int_entry.key_ptr.*); |
| 56 |
} |
| 57 |
self.data.integration_environment.map.deinit(self.allocator); |
| 58 |
|
| 59 |
var ll_it = self.data.linked_libraries.map.iterator(); |
| 60 |
while (ll_it.next()) |ll_entry| { |
| 61 |
self.allocator.free(ll_entry.key_ptr.*); |
| 62 |
} |
| 63 |
self.data.linked_libraries.map.deinit(self.allocator); |
| 64 |
|
| 65 |
self.libs.deinit(); |
| 66 |
} |
| 67 |
|
| 68 |
pub fn setIntegrationEnvironment(self: *Self, integration_name: []const u8, key: []const u8, value: []const u8) !void { |
| 69 |
try self.setIntegrationEnvironmentValue(integration_name, key, .{ .string = try self.allocator.dupe(u8, value) }); |
| 70 |
} |
| 71 |
|
| 72 |
pub fn setIntegrationEnvironmentList(self: *Self, integration_name: []const u8, key: []const u8, values: []const []const u8) !void { |
| 73 |
const duped = try self.allocator.alloc([]const u8, values.len); |
| 74 |
var i: usize = 0; |
| 75 |
errdefer { |
| 76 |
for (duped[0..i]) |item| self.allocator.free(item); |
| 77 |
self.allocator.free(duped); |
| 78 |
} |
| 79 |
while (i < values.len) : (i += 1) { |
| 80 |
duped[i] = try self.allocator.dupe(u8, values[i]); |
| 81 |
} |
| 82 |
try self.setIntegrationEnvironmentValue(integration_name, key, .{ .list = duped }); |
| 83 |
} |
| 84 |
|
| 85 |
fn freeEnvironmentValue(self: *Self, val: EnvironmentValue) void { |
| 86 |
switch (val) { |
| 87 |
.string => |s| self.allocator.free(s), |
| 88 |
.list => |l| { |
| 89 |
for (l) |item| self.allocator.free(item); |
| 90 |
self.allocator.free(l); |
| 91 |
}, |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
fn setIntegrationEnvironmentValue(self: *Self, integration_name: []const u8, key: []const u8, value: EnvironmentValue) !void { |
| 96 |
const int_gop = try self.data.integration_environment.map.getOrPut(self.allocator, integration_name); |
| 97 |
if (!int_gop.found_existing) { |
| 98 |
int_gop.key_ptr.* = try self.allocator.dupe(u8, integration_name); |
| 99 |
int_gop.value_ptr.* = .{}; |
| 100 |
} |
| 101 |
|
| 102 |
const entry_gop = try int_gop.value_ptr.map.getOrPut(self.allocator, key); |
| 103 |
if (entry_gop.found_existing) { |
| 104 |
self.freeEnvironmentValue(entry_gop.value_ptr.*); |
| 105 |
} else { |
| 106 |
entry_gop.key_ptr.* = try self.allocator.dupe(u8, key); |
| 107 |
} |
| 108 |
entry_gop.value_ptr.* = value; |
| 109 |
} |
| 110 |
|
| 111 |
fn populateLinkedLibraries(self: *Self) !void { |
| 112 |
// Clear existing entries |
| 113 |
var ll_it = self.data.linked_libraries.map.iterator(); |
| 114 |
while (ll_it.next()) |ll_entry| { |
| 115 |
self.allocator.free(ll_entry.key_ptr.*); |
| 116 |
} |
| 117 |
self.data.linked_libraries.map.clearRetainingCapacity(); |
| 118 |
|
| 119 |
for (self.libs.libraries.items) |lib| { |
| 120 |
const key = lib.soname orelse lib.path; |
| 121 |
const gop = try self.data.linked_libraries.map.getOrPut(self.allocator, key); |
| 122 |
gop.key_ptr.* = try self.allocator.dupe(u8, key); |
| 123 |
gop.value_ptr.* = .{ |
| 124 |
.path = lib.path, |
| 125 |
.build_id = lib.build_id, |
| 126 |
}; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
pub fn writeEnvironment(self: *Self, pid: i32) u8 { |
| 131 |
self.libs.collect() catch { |
| 132 |
logger.err("instrument-hooks: failed to collect linked libraries\n", .{}); |
| 133 |
}; |
| 134 |
self.populateLinkedLibraries() catch { |
| 135 |
logger.err("instrument-hooks: failed to populate linked libraries\n", .{}); |
| 136 |
}; |
| 137 |
|
| 138 |
if (self.data.integration_environment.map.count() == 0 and self.data.linked_libraries.map.count() == 0) return 0; |
| 139 |
|
| 140 |
const profile_folder = getenv("CODSPEED_PROFILE_FOLDER") orelse { |
| 141 |
return 0; |
| 142 |
}; |
| 143 |
|
| 144 |
const folder_slice = std.mem.span(profile_folder); |
| 145 |
|
| 146 |
var path_buf: [512]u8 = undefined; |
| 147 |
const path = std.fmt.bufPrint(&path_buf, "{s}/environment-{d}.json", .{ folder_slice, pid }) catch { |
| 148 |
logger.err("instrument-hooks: profile folder path too long\n", .{}); |
| 149 |
return 1; |
| 150 |
}; |
| 151 |
|
| 152 |
// Serialize and write |
| 153 |
const json = std.json.stringifyAlloc(self.allocator, self.data, .{ .whitespace = .indent_2 }) catch { |
| 154 |
logger.err("instrument-hooks: failed to serialize environment JSON\n", .{}); |
| 155 |
return 1; |
| 156 |
}; |
| 157 |
defer self.allocator.free(json); |
| 158 |
|
| 159 |
const file = fs.createFileAbsolute(path, .{}) catch { |
| 160 |
logger.err("instrument-hooks: failed to write environment.json\n", .{}); |
| 161 |
return 1; |
| 162 |
}; |
| 163 |
defer file.close(); |
| 164 |
|
| 165 |
file.writeAll(json) catch { |
| 166 |
logger.err("instrument-hooks: failed to write environment.json\n", .{}); |
| 167 |
return 1; |
| 168 |
}; |
| 169 |
|
| 170 |
return 0; |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
// --- Tests --- |
| 175 |
|
| 176 |
test "set and retrieve section entries" { |
| 177 |
var env = Environment.init(std.testing.allocator); |
| 178 |
defer env.deinit(); |
| 179 |
|
| 180 |
try env.setIntegrationEnvironment("gcc", "version", "14.2.0"); |
| 181 |
try env.setIntegrationEnvironment("gcc", "build", "g++ (Ubuntu 14.2.0-4ubuntu2) 14.2.0"); |
| 182 |
try env.setIntegrationEnvironment("clang", "version", "18.1.0"); |
| 183 |
|
| 184 |
try std.testing.expectEqual(@as(usize, 2), env.data.integration_environment.map.count()); |
| 185 |
try std.testing.expectEqual(@as(usize, 2), env.data.integration_environment.map.get("gcc").?.map.count()); |
| 186 |
try std.testing.expectEqual(@as(usize, 1), env.data.integration_environment.map.get("clang").?.map.count()); |
| 187 |
} |
| 188 |
|
| 189 |
test "overwrite existing entry" { |
| 190 |
var env = Environment.init(std.testing.allocator); |
| 191 |
defer env.deinit(); |
| 192 |
|
| 193 |
try env.setIntegrationEnvironment("gcc", "version", "13.0.0"); |
| 194 |
try env.setIntegrationEnvironment("gcc", "version", "14.2.0"); |
| 195 |
|
| 196 |
try std.testing.expectEqual(@as(usize, 1), env.data.integration_environment.map.count()); |
| 197 |
try std.testing.expectEqualStrings("14.2.0", env.data.integration_environment.map.get("gcc").?.map.get("version").?.string); |
| 198 |
} |
| 199 |
|
| 200 |
test "json serialization" { |
| 201 |
var env = Environment.init(std.testing.allocator); |
| 202 |
defer env.deinit(); |
| 203 |
|
| 204 |
try env.setIntegrationEnvironment("gcc", "version", "14.2.0"); |
| 205 |
try env.setIntegrationEnvironment("gcc", "build", "g++ (Ubuntu 14.2.0)"); |
| 206 |
try env.setIntegrationEnvironment("clang", "version", "18.1.0"); |
| 207 |
|
| 208 |
const json = try std.json.stringifyAlloc(std.testing.allocator, env.data, .{ .whitespace = .indent_2 }); |
| 209 |
defer std.testing.allocator.free(json); |
| 210 |
|
| 211 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"version\": \"14.2.0\"") != null); |
| 212 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"build\": \"g++ (Ubuntu 14.2.0)\"") != null); |
| 213 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"version\": \"18.1.0\"") != null); |
| 214 |
} |
| 215 |
|
| 216 |
test "empty sections" { |
| 217 |
var env = Environment.init(std.testing.allocator); |
| 218 |
defer env.deinit(); |
| 219 |
|
| 220 |
const json = try std.json.stringifyAlloc(std.testing.allocator, env.data, .{ .whitespace = .indent_2 }); |
| 221 |
defer std.testing.allocator.free(json); |
| 222 |
|
| 223 |
try std.testing.expectEqualStrings( |
| 224 |
\\{ |
| 225 |
\\ "integration_environment": {}, |
| 226 |
\\ "linked_libraries": {} |
| 227 |
\\} |
| 228 |
, json); |
| 229 |
} |
| 230 |
|
| 231 |
test "json escaping" { |
| 232 |
var env = Environment.init(std.testing.allocator); |
| 233 |
defer env.deinit(); |
| 234 |
|
| 235 |
try env.setIntegrationEnvironment("test", "path", "C:\\Program Files\\gcc"); |
| 236 |
|
| 237 |
const json = try std.json.stringifyAlloc(std.testing.allocator, env.data, .{ .whitespace = .indent_2 }); |
| 238 |
defer std.testing.allocator.free(json); |
| 239 |
|
| 240 |
// Backslashes should be escaped in JSON |
| 241 |
try std.testing.expect(std.mem.indexOf(u8, json, "C:\\\\Program Files\\\\gcc") != null); |
| 242 |
} |
| 243 |
|
| 244 |
test "merge preserves existing and adds new" { |
| 245 |
var env = Environment.init(std.testing.allocator); |
| 246 |
defer env.deinit(); |
| 247 |
|
| 248 |
// Simulate existing data parsed from file |
| 249 |
try env.setIntegrationEnvironment("python", "version", "3.12.0"); |
| 250 |
|
| 251 |
// Add new section |
| 252 |
try env.setIntegrationEnvironment("cpp", "version", "14.2.0"); |
| 253 |
|
| 254 |
try std.testing.expectEqual(@as(usize, 2), env.data.integration_environment.map.count()); |
| 255 |
|
| 256 |
const json = try std.json.stringifyAlloc(std.testing.allocator, env.data, .{ .whitespace = .indent_2 }); |
| 257 |
defer std.testing.allocator.free(json); |
| 258 |
|
| 259 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"python\"") != null); |
| 260 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"cpp\"") != null); |
| 261 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"3.12.0\"") != null); |
| 262 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"14.2.0\"") != null); |
| 263 |
} |
| 264 |
|
| 265 |
test "new entries override existing on merge" { |
| 266 |
var env = Environment.init(std.testing.allocator); |
| 267 |
defer env.deinit(); |
| 268 |
|
| 269 |
try env.setIntegrationEnvironment("python", "version", "3.12.0"); |
| 270 |
try env.setIntegrationEnvironment("python", "version", "3.13.0"); |
| 271 |
|
| 272 |
try std.testing.expectEqual(@as(usize, 1), env.data.integration_environment.map.count()); |
| 273 |
try std.testing.expectEqualStrings("3.13.0", env.data.integration_environment.map.get("python").?.map.get("version").?.string); |
| 274 |
} |
| 275 |
|
| 276 |
test "list environment value" { |
| 277 |
var env = Environment.init(std.testing.allocator); |
| 278 |
defer env.deinit(); |
| 279 |
|
| 280 |
try env.setIntegrationEnvironmentList("python", "sys_path", &.{ "/usr/lib/python3.13", "/home/user/.venv/lib" }); |
| 281 |
try env.setIntegrationEnvironment("python", "version", "3.13.0"); |
| 282 |
|
| 283 |
const json = try std.json.stringifyAlloc(std.testing.allocator, env.data, .{ .whitespace = .indent_2 }); |
| 284 |
defer std.testing.allocator.free(json); |
| 285 |
|
| 286 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"version\": \"3.13.0\"") != null); |
| 287 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"sys_path\": [") != null); |
| 288 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"/usr/lib/python3.13\"") != null); |
| 289 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"/home/user/.venv/lib\"") != null); |
| 290 |
} |
| 291 |
|
| 292 |
test "overwrite string with list" { |
| 293 |
var env = Environment.init(std.testing.allocator); |
| 294 |
defer env.deinit(); |
| 295 |
|
| 296 |
try env.setIntegrationEnvironment("python", "paths", "old_value"); |
| 297 |
try env.setIntegrationEnvironmentList("python", "paths", &.{ "/a", "/b" }); |
| 298 |
|
| 299 |
try std.testing.expectEqual(@as(usize, 1), env.data.integration_environment.map.get("python").?.map.count()); |
| 300 |
const val = env.data.integration_environment.map.get("python").?.map.get("paths").?; |
| 301 |
try std.testing.expectEqual(@as(usize, 2), val.list.len); |
| 302 |
} |
| 303 |
|
| 304 |
test "linked libraries serialization" { |
| 305 |
const alloc = std.testing.allocator; |
| 306 |
var env = Environment.init(alloc); |
| 307 |
defer env.deinit(); |
| 308 |
|
| 309 |
try env.libs.libraries.append(.{ |
| 310 |
.path = try alloc.dupe(u8, "/usr/lib/libc.so.6"), |
| 311 |
.soname = try alloc.dupe(u8, "libc.so.6"), |
| 312 |
.build_id = try alloc.dupe(u8, "abc123"), |
| 313 |
}); |
| 314 |
|
| 315 |
try env.libs.libraries.append(.{ |
| 316 |
.path = try alloc.dupe(u8, "/usr/lib/libm.so.6"), |
| 317 |
.soname = null, |
| 318 |
.build_id = null, |
| 319 |
}); |
| 320 |
|
| 321 |
try env.populateLinkedLibraries(); |
| 322 |
|
| 323 |
const json = try std.json.stringifyAlloc(alloc, env.data, .{ .whitespace = .indent_2 }); |
| 324 |
defer alloc.free(json); |
| 325 |
|
| 326 |
// Library with soname is keyed by soname |
| 327 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"libc.so.6\"") != null); |
| 328 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"/usr/lib/libc.so.6\"") != null); |
| 329 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"abc123\"") != null); |
| 330 |
|
| 331 |
// Library without soname is keyed by path |
| 332 |
try std.testing.expect(std.mem.indexOf(u8, json, "\"/usr/lib/libm.so.6\"") != null); |
| 333 |
} |