mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 22:02:39 +00:00
Stacks in Context can grow when eval is run, and if they are implemented as SmallVector, the growing can cause a reallocation. Meanwhile for stacks that hold larger structs/classes, we hold onto the pointer into the stack instead of copying out. This can cause a UAF when reallocation happens. StableStack never reallocates when growing, as it is represented by a vector of pointers to arrays, where each array is a factor larger than the previous one. This prevents UAF from occurring when we run eval while holding a pointer into a stack. This solves a similar problem that our ValuesStores previously solved in #5576. In that case, we also represent them as a vector of pointers to arrays. The difference being our ValueStores are meant to grow but not shrink, and they are built around the ID concept where IDs happen to be an index into the store.
30 lines
962 B
C++
30 lines
962 B
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/check/generic_region_stack.h"
|
|
|
|
#include "common/vlog.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto GenericRegionStack::Push(PendingGeneric generic) -> void {
|
|
CARBON_VLOG("GenericRegion Push: {0} {1}\n", generic.generic_id,
|
|
generic.region);
|
|
pending_generic_ids_.push_back(generic);
|
|
pending_eval_block_stack_.PushArray();
|
|
dependent_inst_stack_.PushArray();
|
|
constants_in_generic_stack_.Emplace();
|
|
}
|
|
|
|
auto GenericRegionStack::Pop() -> void {
|
|
auto pending = pending_generic_ids_.pop_back_val();
|
|
CARBON_VLOG("GenericRegion Pop: {0} {1}\n", pending.generic_id,
|
|
pending.region);
|
|
pending_eval_block_stack_.PopArray();
|
|
dependent_inst_stack_.PopArray();
|
|
constants_in_generic_stack_.Pop();
|
|
}
|
|
|
|
} // namespace Carbon::Check
|