--- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -2501,6 +2501,12 @@ C++ ObjC++ Var(flag_visibility_ms_compat) Changes visibility to match Microsoft Visual Studio by default. +fvls-observe +C++ Var(flag_vls_observe) Undocumented + +fvls-step +C++ Var(flag_vls_step) Undocumented + fvtable-gc C++ ObjC++ WarnRemoved No longer supported. --- a/gcc/cp/Make-lang.in +++ b/gcc/cp/Make-lang.in @@ -110,7 +110,7 @@ cp/reflect.o cp/rtti.o \ cp/search.o cp/semantics.o \ cp/tree.o cp/typeck.o cp/typeck2.o \ - cp/vtable-class-hierarchy.o $(CXX_C_OBJS) + cp/vls-eval.o cp/vtable-class-hierarchy.o $(CXX_C_OBJS) ifeq ($(if $(wildcard ../stage_current),$(shell cat \ ../stage_current)),stageautofeedback) --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -42,6 +42,7 @@ #include "intl.h" #include "toplev.h" #include "contracts.h" +#include "vls-eval.h" static bool verify_constant (tree, bool, bool *, bool *); #define VERIFY_CONSTANT(X) \ @@ -4277,6 +4278,10 @@ return t; } + vls_eval::call observed; + if (flag_vls_observe && get_function_named_in_call (t) == orig_fun) + observed.enter (fun); + /* We can't defer instantiating the function any longer. */ if (!DECL_INITIAL (fun) && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun)) @@ -4350,7 +4355,11 @@ new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj, &empty_base, jump_target); if (*jump_target) - return NULL_TREE; + { + if (observed.active ()) + observed.outcome (throws (jump_target) ? "throw" : "jump"); + return NULL_TREE; + } /* If we're initializing an empty class, don't set constness, because cxx_fold_indirect_ref will return the wrong object to set constness of. */ @@ -4439,7 +4448,10 @@ { bool cacheable = !!entry; if (result && result != error_mark_node) - /* OK */; + { + if (observed.active ()) + observed.cached (); + } else if (!DECL_SAVED_TREE (fun)) { /* When at_eof >= 3, cgraph has started throwing away @@ -4522,12 +4534,18 @@ jump_target); ggc_free (activate); if (*jump_target) - return NULL_TREE; + { + if (observed.active ()) + observed.outcome (throws (jump_target) ? "throw" : "jump"); + return NULL_TREE; + } } ctx->global->metafns_called = false; tree jmp_target = NULL_TREE; + if (observed.active ()) + observed.body (); cxx_eval_constant_expression (&call_ctx, body, vc_discard, non_constant_p, overflow_p, &jmp_target); @@ -4550,6 +4568,9 @@ else { result = ctx->global->get_value (res); + if (observed.active () && result && !*non_constant_p + && !*overflow_p && !*jump_target) + observed.value (result); if (result == NULL_TREE && !*non_constant_p && !DECL_DESTRUCTOR_P (fun)) { @@ -4686,6 +4707,21 @@ clear_no_implicit_zero (result); pop_cx_call_context (); + if (observed.active ()) + { + if (*jump_target) + observed.outcome (throws (jump_target) ? "throw" : "jump"); + else if (*overflow_p) + observed.outcome ("overflow"); + else if (*non_constant_p || result == error_mark_node) + observed.outcome ("failed"); + else if (DECL_CONSTRUCTOR_P (fun)) + observed.outcome ("constructor"); + else if (result == void_node) + observed.outcome ("void"); + else + observed.outcome ("returned"); + } return result; } --- a/gcc/cp/lex.cc +++ b/gcc/cp/lex.cc @@ -32,6 +32,7 @@ #include "gcc-rich-location.h" #include "cp-name-hint.h" #include "langhooks.h" +#include "vls-eval.h" static int interface_strcmp (const char *); static void init_cp_traits (void); @@ -80,6 +81,8 @@ cxx_finish (void) { c_common_finish (); + if (flag_vls_observe) + vls_eval::finish (); } ovl_op_info_t ovl_op_info[2][OVL_OP_MAX] = @@ -350,6 +353,9 @@ EXPR_STMT, OMP_DEPOBJ }; + if ((flag_vls_observe || flag_vls_step) && !vls_eval::start ()) + return false; + memset (&statement_code_p, 0, sizeof (statement_code_p)); for (i = 0; i < ARRAY_SIZE (stmt_codes); i++) statement_code_p[stmt_codes[i]] = true; --- /dev/null +++ b/gcc/cp/vls-eval.cc @@ -0,0 +1,390 @@ +/* Bounded, opt-in evaluator observation for the private Linux cc1plus. */ +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "cp-tree.h" +#include "vls-eval.h" +#include +#include + +namespace vls_eval { +namespace { + +constexpr int control_fd = 3; +constexpr int event_fd = 4; +constexpr unsigned max_events = 4096; +constexpr size_t max_trace = 1024 * 1024; +constexpr size_t max_control = 4096; +constexpr size_t terminal_reserve = 256; +char nonce[33]; +unsigned sequence; +size_t trace_bytes; +unsigned top; +bool observing; +bool stepping; + +// Each event fits in one Linux PIPE_BUF. There are no unbounded fields. +class record +{ +public: + char bytes[4096]; + size_t size = 0; + bool fits = true; + + explicit record (const char *kind) + { + append ("{\"v\":1,\"nonce\":\"%s\",\"seq\":%u,\"kind\":\"%s\"", + nonce, sequence, kind); + } + + void append (const char *format, ...) ATTRIBUTE_PRINTF_2 + { + if (!fits) + return; + va_list ap; + va_start (ap, format); + int n = vsnprintf (bytes + size, sizeof bytes - size, format, ap); + va_end (ap); + if (n < 0 || (size_t) n >= sizeof bytes - size) + fits = false; + else + size += n; + } + + // v1 metadata accepts printable ASCII only. Unrepresentable names/paths + // are JSON null; quote and backslash are escaped. No source string is a + // format string, and all scanning is bounded before any formatting. + void text (const char *s) + { + size_t n = s ? strnlen (s, 513) : 0; + if (!s || n > 512) + return append ("null"); + for (size_t i = 0; i < n; ++i) + if ((unsigned char) s[i] < 32 || (unsigned char) s[i] > 126) + return append ("null"); + append ("\""); + for (size_t i = 0; i < n; ++i) + { + if (s[i] == '"' || s[i] == '\\') + append ("\\"); + append ("%c", s[i]); + } + append ("\""); + } + + void frame (unsigned id, unsigned parent) + { + append (",\"frame\":%u,\"parent\":", id); + if (parent) + append ("%u", parent); + else + append ("null"); + } +}; + +long long +millis () +{ + struct timespec t; + if (clock_gettime (CLOCK_MONOTONIC, &t) != 0) + _exit (126); + return (long long) t.tv_sec * 1000 + t.tv_nsec / 1000000; +} + +// Recompute the remaining interval across interrupts and partial frames. +bool +ready (int fd, short events, long long deadline) +{ + struct pollfd p = {fd, events, 0}; + for (;;) + { + long long left = deadline - millis (); + if (left <= 0) + return false; + int n = poll (&p, 1, (int) left); + if (n > 0) + return (p.revents & (events | POLLHUP | POLLERR)) != 0; + if (n == 0 || errno != EINTR) + return false; + } +} + +void +send_record (const record &r) +{ + size_t done = 0; + long long deadline = millis () + 5000; + while (done < r.size) + { + ssize_t n = write (event_fd, r.bytes + done, r.size - done); + if (n > 0) + done += n; + else if (n == 0 || (errno != EINTR && errno != EAGAIN) + || !ready (event_fd, POLLOUT, deadline)) + _exit (126); // The reader retains only its verified complete prefix. + } + trace_bytes += r.size; + ++sequence; +} + +void ATTRIBUTE_NORETURN +terminate_probe (const char *reason, unsigned stop = 0) +{ + record r ("terminal"); + r.append (",\"reason\":\"%s\"", reason); + if (stop) + r.append (",\"stop\":%u", stop); + r.append ("}\n"); + gcc_assert (r.fits && r.size <= terminal_reserve); + send_record (r); + _exit (strcmp (reason, "cancelled") == 0 ? 125 : 126); +} + +unsigned +publish (record &r) +{ + r.append ("}\n"); + if (!r.fits || sequence >= max_events - 1 + || trace_bytes + r.size > max_trace - terminal_reserve) + terminate_probe ("truncated"); + unsigned issued = sequence; + send_record (r); + return issued; +} + +bool +same_pipe (const struct stat &a, const struct stat &b) +{ + return a.st_dev == b.st_dev && a.st_ino == b.st_ino; +} + +bool +pipe_mode (int fd, int mode, struct stat &st) +{ + int flags = fcntl (fd, F_GETFL); + return fstat (fd, &st) == 0 && S_ISFIFO (st.st_mode) + && flags >= 0 && (flags & O_ACCMODE) == mode; +} + +bool +set_nonblocking (int fd) +{ + int flags = fcntl (fd, F_GETFL); + return flags >= 0 && fcntl (fd, F_SETFL, flags | O_NONBLOCK) == 0; +} + +bool +valid_pipes () +{ + struct stat control, events; + if (!pipe_mode (control_fd, O_RDONLY, control) + || !pipe_mode (event_fd, O_WRONLY, events) || same_pipe (control, events)) + return false; + for (int fd = 0; fd <= 2; ++fd) + { + struct stat io; + if (fstat (fd, &io) == 0 + && (same_pipe (io, events) || same_pipe (io, control))) + return false; + } + return set_nonblocking (control_fd) && set_nonblocking (event_fd); +} + +bool +valid_nonce (const char *s) +{ + if (!s || strnlen (s, sizeof nonce) != sizeof nonce - 1) + return false; + for (size_t i = 0; i < sizeof nonce - 1; ++i) + if (!(s[i] >= '0' && s[i] <= '9') && !(s[i] >= 'a' && s[i] <= 'f')) + return false; + return true; +} + +void +await_control (unsigned id, unsigned parent) +{ + record paused ("stopped"); + paused.frame (id, parent); + unsigned stop = publish (paused); + char prefix[64]; + size_t prefix_size = snprintf (prefix, sizeof prefix, "%s %u ", nonce, stop); + char line[max_control]; + size_t used = 0; + size_t consumed = 0; + long long deadline = millis () + 60000; + for (;;) + { + if (!ready (control_fd, POLLIN, deadline)) + terminate_probe ("controlTimeout", stop); + char c; + ssize_t n = read (control_fd, &c, 1); + if (n == 0) + terminate_probe ("controlEOF", stop); + if (n < 0) + { + if (errno == EINTR || errno == EAGAIN) + continue; + terminate_probe ("controlError", stop); + } + if (++consumed > 4 * max_control || used == sizeof line) + terminate_probe ("controlLimit", stop); + line[used++] = c; + if (c != '\n') + continue; + const size_t length = used; + used = 0; + if (length <= prefix_size || memcmp (line, prefix, prefix_size) != 0) + continue; + const char *action = nullptr; + const char *command = line + prefix_size; + size_t size = length - prefix_size; + if (size == 5 && memcmp (command, "step\n", 5) == 0) + action = "step"; + else if (size == 9 && memcmp (command, "continue\n", 9) == 0) + { + stepping = false; + action = "continue"; + } + else if (size == 7 && memcmp (command, "cancel\n", 7) == 0) + terminate_probe ("cancelled", stop); + else + continue; + record resumed ("resumed"); + resumed.frame (id, parent); + resumed.append (",\"stop\":%u,\"action\":\"%s\"", stop, action); + publish (resumed); + return; + } +} + +} // anonymous namespace + +bool +start () +{ + const char *s = getenv ("VLS_GCC_NONCE"); + if (!flag_vls_observe || !valid_nonce (s) || !valid_pipes () + || signal (SIGPIPE, SIG_IGN) == SIG_ERR) + { + error ("invalid private evaluator observer setup"); + return false; + } + memcpy (nonce, s, sizeof nonce); + observing = true; + stepping = flag_vls_step; + record r ("hello"); + r.append (",\"backend\":\"gcc-16.2.0/vls-eval-1\",\"pid\":%ld," + "\"capabilities\":[\"integralReturn\",\"cooperativeStep\"]," + "\"kinds\":[\"hello\",\"enter\",\"body\",\"value\",\"cached\"," + "\"stopped\",\"resumed\",\"leave\",\"terminal\",\"frontendEnd\"]", + (long) getpid ()); + publish (r); + return true; +} + +void +finish () +{ + if (observing) + { + record r ("frontendEnd"); + publish (r); + observing = false; + } +} + +void +call::enter (tree decl) +{ + if (!observing || DECL_ARTIFICIAL (decl) || !DECL_NAME (decl)) + return; + location_t loc = DECL_SOURCE_LOCATION (decl); + if (RESERVED_LOCATION_P (loc) || in_system_header_at (loc)) + return; + m_parent = top; + top = m_frame = sequence; // hello consumed zero; each enter is unique. + record r ("enter"); + r.frame (m_frame, m_parent); + r.append (",\"name\":"); + r.text (IDENTIFIER_POINTER (DECL_NAME (decl))); + // GCC logical declaration location; consumers must map it to a snapshot + // before using it as an edit or a physical source identity. + expanded_location x = expand_location (loc); + if (!from_macro_expansion_at (loc) && x.file && x.line > 0 && x.column > 0) + { + r.append (",\"sourceKind\":\"logicalDeclaration\",\"source\":"); + r.text (x.file); + r.append (",\"line\":%d,\"byteColumn\":%d", x.line, x.column); + } + else + r.append (",\"sourceKind\":\"unavailable\""); + publish (r); +} + +void +call::body () const +{ + record r ("body"); + r.frame (m_frame, m_parent); + publish (r); +} + +void +call::cached () const +{ + record r ("cached"); + r.frame (m_frame, m_parent); + publish (r); +} + +void +call::value (tree result) const +{ + record r ("value"); + r.frame (m_frame, m_parent); + tree type = TREE_TYPE (result); + if (TREE_CODE (result) != INTEGER_CST || !type || !INTEGRAL_TYPE_P (type) + || TREE_OVERFLOW (result) || TYPE_PRECISION (type) == 0 + || TYPE_PRECISION (type) > 64) + { + r.append (",\"value\":{\"kind\":\"unavailable\"," + "\"reason\":\"unsupportedScalar\"}"); + publish (r); + return; + } + + unsigned precision = TYPE_PRECISION (type); + unsigned HOST_WIDE_INT bits = TREE_INT_CST_LOW (result); + // The low limb is sign-extended for narrow signed types. Copy exactly the + // type width, including partial high nibbles and the full 64-bit case. + if (precision < HOST_BITS_PER_WIDE_INT) + bits &= (HOST_WIDE_INT_1U << precision) - 1; + unsigned digits = (precision + 3) / 4; + char hex[17]; + hex[digits] = 0; + for (unsigned i = digits; i; --i) + { + hex[i - 1] = "0123456789abcdef"[bits & 15]; + bits >>= 4; + } + r.append (",\"value\":{\"kind\":\"integer\",\"precision\":%u," + "\"signed\":%s,\"bitsHex\":\"%s\"}", precision, + TYPE_UNSIGNED (type) ? "false" : "true", hex); + publish (r); + if (stepping) + await_control (m_frame, m_parent); +} + +void +call::leave () +{ + gcc_assert (top == m_frame); + top = m_parent; + record r ("leave"); + r.frame (m_frame, m_parent); + r.append (",\"outcome\":\"%s\"", m_outcome); + publish (r); +} + +} // namespace vls_eval --- /dev/null +++ b/gcc/cp/vls-eval.h @@ -0,0 +1,32 @@ +/* Private live constexpr observer for GCC 16.2.0. */ +#ifndef GCC_CP_VLS_EVAL_H +#define GCC_CP_VLS_EVAL_H + +namespace vls_eval { + +bool start (); +void finish (); + +// A dynamic attempt in this invocation. Only numeric IDs and static outcome +// tags survive a hook. GCC owns all trees, including the result declaration. +class call +{ + unsigned m_frame = 0; + unsigned m_parent; + const char *m_outcome = "failed"; + void leave (); +public: + call () = default; + call (const call &) = delete; + call &operator= (const call &) = delete; + ~call () { if (active ()) leave (); } + bool active () const { return m_frame != 0; } + void enter (tree); + void body () const; + void cached () const; + void value (tree) const; + void outcome (const char *tag) { m_outcome = tag; } +}; + +} // namespace vls_eval +#endif