[SpecBot] Add specifications to core utility and AST classes #8460
Closed
Replies: 1 comment
-
|
This discussion was automatically closed because it expired on 2026-02-08T19:35:05.594Z. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
✨ Automatic Specification Mining
This discussion proposes formal specifications (class invariants, pre-conditions, post-conditions) for four core Z3 classes to improve code correctness and maintainability. The specifications capture essential properties that must be maintained throughout execution.
📋 Classes Annotated
regioninsrc/util/region.handsrc/util/region.cppref_vector_coreinsrc/util/ref_vector.hlim_svectorinsrc/util/lim_vector.hmacro_managerinsrc/ast/macros/macro_manager.handsrc/ast/macros/macro_manager.cpp🔍 Specifications Added
1.
region- Memory Region ManagerClass Invariants:
Scope consistency:
m_scopes.size() <= m_chunks.size()(Z3DEBUG version)SASSERT(m_scopes.size() <= m_chunks.size())Mark stack validity:
m_mark == nullptr || is_valid_mark_chain(m_mark)(Release version)Current page consistency:
m_curr_ptr >= m_curr_page && m_curr_ptr <= m_curr_end_ptr(Release version)SASSERT(m_curr_ptr >= m_curr_page && m_curr_ptr <= m_curr_end_ptr)Page alignment:
m_curr_page == nullptr || is_default_page(m_curr_page)(Release version, after operations)SASSERT(m_curr_page == nullptr || is_default_page(m_curr_page))Pre-conditions:
pop_scope():SASSERT(!m_scopes.empty())(Z3DEBUG) orSASSERT(m_mark != nullptr)(Release)pop_scope(unsigned num_scopes):num_scopes <= m_scopes.size()ornum_scopesvalid marks availableSASSERT(num_scopes <= m_scopes.size())Post-conditions:
allocate(size_t size):sizebytesSASSERT(result != nullptr)push_scope():m_scopes.size() == old(m_scopes.size()) + 1(Z3DEBUG)m_mark != nullptr && m_mark->m_prev_mark == old(m_mark)(Release)pop_scope():m_scopes.size() == old(m_scopes.size()) - 1(Z3DEBUG)2.
ref_vector_core(T, Ref)- Reference-Counted VectorClass Invariants:
Reference count consistency: All elements in
m_nodeshave incremented reference counts#ifdef DEBUGhelper checking each element has proper refcountSize consistency:
m_nodes.size()reflects actual number of referenced elementsPre-conditions:
shrink(unsigned sz):sz <= m_nodes.size()SASSERT(sz <= m_nodes.size())pop_back():!m_nodes.empty()SASSERT(!m_nodes.empty())operator[](unsigned idx):idx < m_nodes.size()SASSERT(idx < m_nodes.size())swap(ref_vector & other)(in ref_vector):&(this->m_manager) == &(other.m_manager)SASSERT(&(this->m_manager) == &(other.m_manager))Post-conditions:
push_back(T * n):size() == old(size()) + 1 && back() == nnis incrementedpop_back():size() == old(size()) - 1set(unsigned idx, T * n):operator[](idx) == nDestructor
~ref_vector_core():3.
lim_svector(T)- Scoped Vector with BacktrackingClass Invariants:
Scope limit consistency:
m_lim.size()represents number of active scopesMonotonic limits:
m_lim[i] <= m_lim[i+1]for all validiSASSERT(m_lim.empty() || m_lim[i] <= m_lim[i+1])(for all i)Limit bounds: Each
m_lim[i] <= this->size()SASSERT(m_lim.empty() || m_lim.back() <= this->size())Pre-conditions:
pop_scope(unsigned num_scopes):num_scopes > 0 && num_scopes <= m_lim.size()SASSERT(num_scopes > 0)(already present) andSASSERT(num_scopes <= m_lim.size())old_size(unsigned n):n <= m_lim.size()SASSERT(n <= m_lim.size())Post-conditions:
push_scope():num_scopes() == old(num_scopes()) + 1m_lim.back() == this->size()pop_scope(unsigned num_scopes):num_scopes() == old(num_scopes()) - num_scopesthis->size() == old(m_lim[m_lim.size() - num_scopes])4.
macro_manager- Macro Management with BacktrackingClass Invariants:
Parallel vector consistency:
m_decls.size() == m_macros.size() == m_macro_prs.size() == m_macro_deps.size()SASSERT(m_decls.size() == m_macros.size() && (!m.proofs_enabled() || m_decls.size() == m_macro_prs.size()) && m_decls.size() == m_macro_deps.size())Map-vector consistency:
m_decl2macrocontains exactly the elements inm_declsSASSERT(m_decl2macro.size() == m_decls.size())Map-vector element correspondence: For all
i < m_decls.size():m_decl2macro[m_decls[i]] == m_macros[i]Forbidden set consistency:
m_forbidden_setcontains exactly the elements inm_forbiddenSASSERT(m_forbidden_set.size() == m_forbidden.size())Scope stack consistency: For each scope
sinm_scopes:s.m_decls_lim <= m_decls.size() && s.m_forbidden_lim <= m_forbidden.size()Proof consistency: When
m.proofs_enabled():m_decl2macro_pr[f] != nullptrfor allfinm_declsSASSERT(!m.proofs_enabled() || m_decl2macro_pr.find(m_decls[i]) != nullptr)Pre-conditions:
pop_scope(unsigned num_scopes):num_scopes <= m_scopes.size()SASSERT(num_scopes <= m_scopes.size())insert(func_decl * f, ...):f != nullptr && q != nullptr!m_decls.contains(f)(for successful insertion)SASSERT(f != nullptr && q != nullptr)get_macro_quantifier(func_decl * f):f != nullptrSASSERT(f != nullptr)Post-conditions:
push_scope():m_scopes.size() == old(m_scopes.size()) + 1m_scopes.back().m_decls_lim == m_decls.size()m_scopes.back().m_forbidden_lim == m_forbidden.size()pop_scope(unsigned num_scopes):m_scopes.size() == old(m_scopes.size()) - num_scopesm_decls.size() == m_scopes.empty() ? 0 : m_scopes.back().m_decls_liminsert(func_decl * f, quantifier * q, ...)(when successful):m_decls.contains(f) && m_decl2macro[f] == qm_decls.size() == old(m_decls.size()) + 1reset():m_decls.empty() && m_macros.empty() && m_scopes.empty()🎯 Goals Achieved
Implementation Recommendations
These specifications should be implemented as follows:
Add
check_invariant()methods to classes where appropriate:region::check_invariant()- Validate page pointers and scope consistencyref_vector_core::check_invariant()- Optional debug-only refcount validationlim_svector::check_invariant()- Validate scope limit monotonicitymacro_manager::check_invariant()- Validate parallel vector synchronizationStrengthen existing assertions:
SASSERTchecks (e.g.,pop_back())Guard expensive checks:
#ifdef Z3DEBUGor#ifndef NDEBUGfor O(n) invariant validationAdd post-condition documentation:
Validation Performed
Human Review Required
While these specifications are derived from careful code analysis, human review is essential before implementation:
Behavioral Impact
📚 Methodology
Specifications synthesized using LLM-based invariant mining techniques inspired by:
Analysis Tools Used:
SASSERTusage🔄 Next Steps
🤖 Generated by SpecBot - Automatic Specification Mining Agent
Workflow Run ID: 21568875207
Date: 2026-02-01
Beta Was this translation helpful? Give feedback.
All reactions