// Local stock GCC 16.2 occurrence observer for VLS. // // The plugin publishes compiler-owned facts for resolved direct calls and // actual function definitions in the captured file set. It reads a private // manifest (fixed path or VLS_OBSERVER_INPUTS on the test leg), verifies every // physical span against the captured UTF-8 bytes, and writes one strict JSON // report (fixed path or VLS_OBSERVER_OUTPUT). No tree, line map or parser // pointer escapes this process. #include "gcc-plugin.h" #include "plugin-version.h" #include "cp/cp-tree.h" #include "cpplib.h" #include "diagnostic.h" #include "function.h" #include "input.h" #include #include #include #include #include #include #include #include #include int plugin_is_GPL_compatible; namespace { constexpr unsigned max_name = 64; constexpr unsigned max_type = 160; constexpr unsigned max_signature = 300; constexpr unsigned max_path = 4095; constexpr unsigned max_inputs = 1024; constexpr unsigned max_occurrences = 256; constexpr std::size_t max_file = 16U * 1024U * 1024U; constexpr std::size_t max_total = 64U * 1024U * 1024U; constexpr std::size_t max_manifest = 4U * 1024U * 1024U; constexpr unsigned max_visited = 200000; struct span { unsigned line{}; unsigned start{}; unsigned end{}; bool same(const span &other) const { return line == other.line && start == other.start && end == other.end; } }; struct file_entry { char path[max_path + 1]{}; unsigned char *data{}; std::size_t size{}; }; struct body_entry { unsigned uid{}; span where{}; char path[max_path + 1]{}; bool definition{}; bool conflict{}; }; struct use_entry { unsigned uid{}; span where{}; char path[max_path + 1]{}; char name[max_name + 1]{}; char type[max_type + 1]{}; char canonical[max_type + 1]{}; char signature[max_signature + 1]{}; bool conflict{}; }; char nonce[33]; char primary_path[max_path + 1]; file_entry files[max_inputs]; unsigned file_count; body_entry bodies[max_occurrences]; unsigned body_count; use_entry uses[max_occurrences]; unsigned use_count; std::size_t total_bytes; unsigned visited; bool at_capacity; bool withheld_origin; bool withheld_semantics; bool withheld_conflict; bool over_walk; bool admitted(const char *path) { if (!path) { return false; } for (unsigned i = 0; i != file_count; ++i) { if (!std::strcmp(files[i].path, path)) { return true; } } return false; } file_entry *file_for(const char *path) { if (!path) { return nullptr; } for (unsigned i = 0; i != file_count; ++i) { if (!std::strcmp(files[i].path, path)) { return &files[i]; } } return nullptr; } bool load_file(const char *path) { if (file_count == max_inputs) { return false; } const int fd = ::open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); if (fd < 0) { return false; } struct stat st{}; if (::fstat(fd, &st) != 0 || !S_ISREG(st.st_mode) || st.st_size < 0 || static_cast(st.st_size) > max_file || total_bytes + static_cast(st.st_size) > max_total) { ::close(fd); return false; } const auto size = static_cast(st.st_size); auto *data = static_cast(xmalloc(size ? size : 1)); if (!data) { ::close(fd); return false; } std::size_t done = 0; while (done != size) { const ssize_t n = ::read(fd, data + done, size - done); if (n <= 0) { free(data); ::close(fd); return false; } done += static_cast(n); } ::close(fd); file_entry &entry = files[file_count++]; std::strcpy(entry.path, path); entry.data = data; entry.size = size; total_bytes += size; return true; } bool parse_manifest() { const char *input = std::getenv("VLS_OBSERVER_INPUTS"); if (!input || !*input) { input = "/vls-observer/inputs"; } const int fd = ::open(input, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); if (fd < 0) { return false; } char *text = static_cast(xmalloc(max_manifest + 1)); if (!text) { ::close(fd); return false; } std::size_t size = 0; while (size != max_manifest) { const ssize_t n = ::read(fd, text + size, max_manifest - size); if (n == 0) { break; } if (n < 0) { free(text); ::close(fd); return false; } size += static_cast(n); } ::close(fd); if (size == max_manifest) { free(text); return false; } text[size] = '\0'; if (std::strlen(text) != size) { free(text); return false; } char *lines[max_inputs + 3]; unsigned count = 0; char *cursor = text; while (*cursor) { if (count == max_inputs + 3) { free(text); return false; } lines[count++] = cursor; char *end = std::strchr(cursor, '\n'); if (!end) { break; } *end = '\0'; cursor = end + 1; } if (count < 3 || std::strcmp(lines[0], "VLS-OCCURRENCES-1") != 0 || std::strlen(lines[1]) != 32) { free(text); return false; } for (unsigned i = 0; i != 32; ++i) { const char c = lines[1][i]; if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) { free(text); return false; } } std::strcpy(nonce, lines[1]); std::strcpy(primary_path, lines[2]); for (unsigned i = 2; i != count; ++i) { const std::size_t length = std::strlen(lines[i]); if (!length || length > max_path || lines[i][0] != '/') { free(text); return false; } if (!file_for(lines[i]) && !load_file(lines[i])) { free(text); return false; } } free(text); return true; } bool utf8_valid(const char *text) { const auto *s = reinterpret_cast(text); while (*s) { if (*s < 0x80) { ++s; } else if ((*s & 0xE0) == 0xC0 && (s[1] & 0xC0) == 0x80) { if (*s < 0xC2) { return false; } s += 2; } else if ((*s & 0xF0) == 0xE0 && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80) { s += 3; } else if ((*s & 0xF8) == 0xF0 && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80 && (s[3] & 0xC0) == 0x80) { s += 4; } else { return false; } } return true; } void json_string(std::FILE *out, const char *text) { std::fputc('"', out); for (const auto *s = reinterpret_cast(text); *s; ++s) { switch (*s) { case '"': std::fputs("\\\"", out); break; case '\\': std::fputs("\\\\", out); break; case '\n': std::fputs("\\n", out); break; case '\r': std::fputs("\\r", out); break; case '\t': std::fputs("\\t", out); break; default: if (*s < 0x20) { std::fprintf(out, "\\u%04x", unsigned(*s)); } else { std::fputc(*s, out); } } } std::fputc('"', out); } bool plain_name(tree decl) { if (!decl || TREE_CODE(decl) != FUNCTION_DECL || DECL_ARTIFICIAL(decl) || !DECL_NAME(decl)) { return false; } if (fndecl_built_in_p(decl) || !TREE_TYPE(decl) || TREE_CODE(TREE_TYPE(decl)) != FUNCTION_TYPE) { return false; } if (DECL_CONTEXT(decl) && TYPE_P(DECL_CONTEXT(decl))) { return false; } if (DECL_LANG_SPECIFIC(decl) && DECL_TEMPLATE_INFO(decl)) { return false; } tree id = DECL_NAME(decl); const unsigned length = IDENTIFIER_LENGTH(id); if (!length || length > max_name) { return false; } const char *name = IDENTIFIER_POINTER(id); for (unsigned i = 0; i != length; ++i) { const char c = name[i]; if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (i && c >= '0' && c <= '9'))) { return false; } } return true; } // One-based physical byte columns, end exclusive. GCC columns count bytes but // a first-line UTF-8 BOM is not part of the column space, so it is restored. bool token_span(location_t token, unsigned length, const char *expected, span &out, const char **path_out) { const location_t start = get_pure_location(get_start(token)); const location_t last = get_pure_location(get_finish(token)); if (RESERVED_LOCATION_P(start) || RESERVED_LOCATION_P(last) || get_pure_location(token) != start || from_macro_expansion_at(start) || from_macro_expansion_at(last)) { return false; } const line_map *raw_map = linemap_lookup(line_table, start); if (!raw_map || !MAP_ORDINARY_P(raw_map) || linemap_lookup(line_table, last) != raw_map) { return false; } const line_map_ordinary *map = linemap_check_ordinary(raw_map); if (map->sysp || !map->to_file) { return false; } file_entry *file = file_for(map->to_file); if (!file) { return false; } const unsigned line = SOURCE_LINE(map, start); const unsigned column = SOURCE_COLUMN(map, start); const unsigned final = SOURCE_COLUMN(map, last); if (!line || !column || SOURCE_LINE(map, last) != line || final == UINT_MAX || final < column || final - column + 1 != length) { return false; } std::size_t line_start = 0; for (unsigned current = 1; current != line; ++current) { const void *newline = std::memchr(file->data + line_start, '\n', file->size - line_start); if (!newline) { return false; } line_start = static_cast(static_cast(newline) - file->data) + 1; } const bool bom = line == 1 && file->size >= 3 && file->data[0] == 0xEF && file->data[1] == 0xBB && file->data[2] == 0xBF; const std::size_t shift = bom ? 3 : 0; const std::size_t begin = line_start + shift + column - 1; if (begin + length > file->size || std::memcmp(file->data + begin, expected, length) != 0) { return false; } out = {line, column + unsigned(shift), column + unsigned(shift) + length}; *path_out = file->path; return true; } bool range_bytes(location_t location, file_entry *&file, std::size_t &begin, std::size_t &end) { const location_t start = get_pure_location(get_start(location)); const location_t last = get_pure_location(get_finish(location)); if (RESERVED_LOCATION_P(start) || RESERVED_LOCATION_P(last) || from_macro_expansion_at(start) || from_macro_expansion_at(last)) { return false; } const line_map *raw_map = linemap_lookup(line_table, start); if (!raw_map || !MAP_ORDINARY_P(raw_map) || linemap_lookup(line_table, last) != raw_map) { return false; } const line_map_ordinary *map = linemap_check_ordinary(raw_map); if (map->sysp || !map->to_file) { return false; } file = file_for(map->to_file); if (!file) { return false; } const unsigned line = SOURCE_LINE(map, start); const unsigned column = SOURCE_COLUMN(map, start); const unsigned final = SOURCE_COLUMN(map, last); if (!line || !column || SOURCE_LINE(map, last) != line || final < column) { return false; } std::size_t line_start = 0; for (unsigned current = 1; current != line; ++current) { const void *newline = std::memchr(file->data + line_start, '\n', file->size - line_start); if (!newline) { return false; } line_start = static_cast(static_cast(newline) - file->data) + 1; } const bool bom = line == 1 && file->size >= 3 && file->data[0] == 0xEF && file->data[1] == 0xBB && file->data[2] == 0xBF; const std::size_t shift = bom ? 3 : 0; begin = line_start + shift + column - 1; end = line_start + shift + final; return begin < end && end <= file->size; } bool identifier_char(unsigned char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; } // Isolate the terminal identifier before the first '(' inside the call range. bool call_token(tree call, tree decl, span &out, const char **path_out) { const location_t location = EXPR_LOCATION(call); if (location == UNKNOWN_LOCATION) { return false; } file_entry *file = nullptr; std::size_t begin = 0; std::size_t end = 0; if (!range_bytes(location, file, begin, end)) { return false; } std::size_t open = begin; while (open != end && file->data[open] != '(') { ++open; } if (open == end) { return false; } std::size_t stop = open; while (stop != begin && (file->data[stop - 1] == ' ' || file->data[stop - 1] == '\t')) { --stop; } std::size_t start = stop; while (start != begin && identifier_char(file->data[start - 1])) { --start; } const unsigned length = IDENTIFIER_LENGTH(DECL_NAME(decl)); if (stop - start != length || std::memcmp(file->data + start, IDENTIFIER_POINTER(DECL_NAME(decl)), length) != 0) { return false; } // The conservative grammar admits one physical line for the callee prefix. if (std::memchr(file->data + begin, '\n', start - begin)) { return false; } unsigned line = 1; std::size_t line_start = 0; for (std::size_t i = 0; i != start; ++i) { if (file->data[i] == '\n') { ++line; line_start = i + 1; } } if (line_start > start || start - line_start > UINT_MAX - 1) { return false; } out = {line, unsigned(start - line_start + 1), unsigned(stop - line_start + 1)}; *path_out = file->path; return true; } body_entry *find_body(unsigned uid) { for (unsigned i = 0; i != body_count; ++i) { if (bodies[i].uid == uid) { return &bodies[i]; } } return nullptr; } void record_body(tree decl) { if (!plain_name(decl) || at_capacity) { return; } const location_t location = DECL_SOURCE_LOCATION(decl); if (RESERVED_LOCATION_P(location) || from_macro_expansion_at(get_pure_location(location))) { return; } span where; const char *path = nullptr; if (!token_span(location, IDENTIFIER_LENGTH(DECL_NAME(decl)), IDENTIFIER_POINTER(DECL_NAME(decl)), where, &path)) { withheld_origin = true; return; } const unsigned uid = DECL_UID(decl); if (body_entry *old = find_body(uid)) { if (!old->where.same(where)) { old->conflict = true; withheld_conflict = true; } old->definition = true; return; } if (body_count == max_occurrences) { at_capacity = true; return; } body_entry &entry = bodies[body_count++]; entry.uid = uid; entry.where = where; std::strcpy(entry.path, path); entry.definition = true; } void record_use(tree call, tree decl) { if (at_capacity) { return; } if (!plain_name(decl)) { withheld_semantics = true; return; } const tree type = TREE_TYPE(call); if (!type || type == error_mark_node) { withheld_semantics = true; return; } span where; const char *path = nullptr; if (!call_token(call, decl, where, &path)) { withheld_origin = true; return; } const char *type_text = type_as_string(type, TFF_SCOPE); const tree canonical_type = TYPE_CANONICAL(type) ? TYPE_CANONICAL(type) : type; const char *canonical_text = type_as_string(canonical_type, TFF_SCOPE); if (!type_text || !canonical_text || std::strlen(type_text) > max_type || std::strlen(canonical_text) > max_type || !utf8_valid(type_text) || !utf8_valid(canonical_text) || !utf8_valid(path)) { withheld_semantics = true; return; } const unsigned uid = DECL_UID(decl); for (unsigned i = 0; i != use_count; ++i) { if (!uses[i].where.same(where)) { continue; } if (uses[i].uid != uid || std::strcmp(uses[i].type, type_text) != 0 || std::strcmp(uses[i].canonical, canonical_text) != 0) { uses[i].conflict = true; withheld_conflict = true; } return; } if (use_count == max_occurrences) { at_capacity = true; return; } use_entry &entry = uses[use_count++]; entry.uid = uid; entry.where = where; std::strcpy(entry.path, path); std::memcpy(entry.name, IDENTIFIER_POINTER(DECL_NAME(decl)), IDENTIFIER_LENGTH(DECL_NAME(decl))); std::strcpy(entry.type, type_text); std::strcpy(entry.canonical, canonical_text); const char *signature = decl_as_string(decl, TFF_SCOPE | TFF_RETURN_TYPE); if (signature && std::strlen(signature) <= max_signature && utf8_valid(signature)) { std::strcpy(entry.signature, signature); } } tree scan(tree *slot, int *walk_subtrees, void *) { tree node = *slot; if (!node) { return NULL_TREE; } if (over_walk || ++visited > max_visited) { over_walk = true; *walk_subtrees = 0; return NULL_TREE; } if (TYPE_P(node) || DECL_P(node)) { *walk_subtrees = 0; return NULL_TREE; } if (TREE_CODE(node) != CALL_EXPR) { return NULL_TREE; } tree decl = cp_get_callee_fndecl_nofold(node); if (decl && plain_name(decl)) { record_use(node, decl); } else if (decl) { withheld_semantics = true; } return NULL_TREE; } void started(void *data, void *) { auto *decl = static_cast(data); if (!decl || TREE_CODE(decl) != FUNCTION_DECL || !plain_name(decl)) { return; } if (DECL_LANG_SPECIFIC(decl) && DECL_USE_TEMPLATE(decl)) { return; } record_body(decl); } void pre_genericize(void *data, void *) { auto *decl = static_cast(data); if (!decl || TREE_CODE(decl) != FUNCTION_DECL || !plain_name(decl) || !DECL_SAVED_TREE(decl)) { return; } if (DECL_LANG_SPECIFIC(decl) && DECL_USE_TEMPLATE(decl)) { return; } if (body_entry *body = find_body(DECL_UID(decl))) { body->definition = true; } const location_t location = DECL_SOURCE_LOCATION(decl); if (RESERVED_LOCATION_P(location)) { return; } const expanded_location expanded = expand_location(location); if (!admitted(expanded.file)) { return; } cp_walk_tree_without_duplicates(&DECL_SAVED_TREE(decl), scan, nullptr); } void finish(void *, void *) { const char *output = std::getenv("VLS_OBSERVER_OUTPUT"); if (!output || !*output) { output = "/vls-observer/report"; } const char *reason = ""; bool complete = true; if (line_table->seen_line_directive) { reason = "lineDirective"; complete = false; } else if (seen_error()) { reason = "compileErrors"; complete = false; } else if (at_capacity || over_walk) { reason = "capacity"; complete = false; } const int fd = ::open(output, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); if (fd < 0) { return; } std::FILE *out = ::fdopen(fd, "w"); if (!out) { ::close(fd); return; } std::fputs("{\"schema\":1,\"nonce\":", out); json_string(out, nonce); std::fprintf(out, ",\"complete\":%s,\"partial\":%s,\"reason\":", complete ? "true" : "false", (withheld_origin || withheld_semantics || withheld_conflict) ? "true" : "false"); json_string(out, reason); std::fputs(",\"occurrences\":[", out); bool first = true; if (complete) { for (unsigned i = 0; i != use_count; ++i) { const use_entry &use = uses[i]; if (use.conflict) { continue; } const body_entry *body = find_body(use.uid); const bool known = body && !body->conflict; if (!first) { std::fputc(',', out); } first = false; std::fputs("{\"name\":", out); json_string(out, use.name); std::fputs(",\"type\":", out); json_string(out, use.type); std::fputs(",\"canonicalType\":", out); json_string(out, use.canonical); if (use.signature[0]) { std::fputs(",\"signature\":", out); json_string(out, use.signature); } std::fputs(",\"use\":{\"path\":", out); json_string(out, use.path); std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u},\"definition\":", use.where.line, use.where.start, use.where.end); if (known) { std::fputs("{\"path\":", out); json_string(out, body->path); std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u}", body->where.line, body->where.start, body->where.end); } else { std::fputs("null", out); } std::fputc('}', out); } } std::fputs("]}\n", out); if (std::fflush(out) != 0 || std::ferror(out) || ::fsync(fd) != 0) { std::fclose(out); return; } std::fclose(out); } } // namespace int plugin_init(struct plugin_name_args *info, struct plugin_gcc_version *version) { if (!plugin_default_version_check(version, &gcc_version)) { return 1; } if (!parse_manifest()) { return 1; } register_callback(info->base_name, PLUGIN_START_PARSE_FUNCTION, started, nullptr); register_callback(info->base_name, PLUGIN_PRE_GENERICIZE, pre_genericize, nullptr); register_callback(info->base_name, PLUGIN_FINISH, finish, nullptr); return 0; }