1 //= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--=
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements ProgramState and ProgramStateManager.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Analysis/CFG.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
25 // Give the vtable for ConstraintManager somewhere to live.
26 // FIXME: Move this elsewhere.
27 ConstraintManager::~ConstraintManager() {}
29 namespace clang { namespace ento {
30 /// Increments the number of times this state is referenced.
32 void ProgramStateRetain(const ProgramState *state) {
33 ++const_cast<ProgramState*>(state)->refCount;
36 /// Decrement the number of times this state is referenced.
37 void ProgramStateRelease(const ProgramState *state) {
38 assert(state->refCount > 0);
39 ProgramState *s = const_cast<ProgramState*>(state);
40 if (--s->refCount == 0) {
41 ProgramStateManager &Mgr = s->getStateManager();
42 Mgr.StateSet.RemoveNode(s);
44 Mgr.freeStates.push_back(s);
49 ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
50 StoreRef st, GenericDataMap gdm)
56 stateMgr->getStoreManager().incrementReferenceCount(store);
59 ProgramState::ProgramState(const ProgramState &RHS)
60 : llvm::FoldingSetNode(),
61 stateMgr(RHS.stateMgr),
66 stateMgr->getStoreManager().incrementReferenceCount(store);
69 ProgramState::~ProgramState() {
71 stateMgr->getStoreManager().decrementReferenceCount(store);
74 ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
75 StoreManagerCreator CreateSMgr,
76 ConstraintManagerCreator CreateCMgr,
77 llvm::BumpPtrAllocator &alloc,
79 : Eng(&SubEng), EnvMgr(alloc), GDMFactory(alloc),
80 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
81 CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
82 StoreMgr.reset((*CreateSMgr)(*this));
83 ConstraintMgr.reset((*CreateCMgr)(*this, SubEng));
87 ProgramStateManager::~ProgramStateManager() {
88 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
90 I->second.second(I->second.first);
94 ProgramStateManager::removeDeadBindings(ProgramStateRef state,
95 const StackFrameContext *LCtx,
96 SymbolReaper& SymReaper) {
98 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
99 // The roots are any Block-level exprs and Decls that our liveness algorithm
100 // tells us are live. We then see what Decls they may reference, and keep
101 // those around. This code more than likely can be made faster, and the
102 // frequency of which this method is called should be experimented with
103 // for optimum performance.
104 ProgramState NewState = *state;
106 NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
108 // Clean up the store.
109 StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
111 NewState.setStore(newStore);
112 SymReaper.setReapedStore(newStore);
114 return getPersistentState(NewState);
117 ProgramStateRef ProgramStateManager::MarshalState(ProgramStateRef state,
118 const StackFrameContext *InitLoc) {
119 // make up an empty state for now.
120 ProgramState State(this,
121 EnvMgr.getInitialEnvironment(),
122 StoreMgr->getInitialStore(InitLoc),
123 GDMFactory.getEmptyMap());
125 return getPersistentState(State);
128 ProgramStateRef ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL,
129 const LocationContext *LC,
131 const StoreRef &newStore =
132 getStateManager().StoreMgr->BindCompoundLiteral(getStore(), CL, LC, V);
133 return makeWithStore(newStore);
136 ProgramStateRef ProgramState::bindDecl(const VarRegion* VR, SVal IVal) const {
137 const StoreRef &newStore =
138 getStateManager().StoreMgr->BindDecl(getStore(), VR, IVal);
139 return makeWithStore(newStore);
142 ProgramStateRef ProgramState::bindDeclWithNoInit(const VarRegion* VR) const {
143 const StoreRef &newStore =
144 getStateManager().StoreMgr->BindDeclWithNoInit(getStore(), VR);
145 return makeWithStore(newStore);
148 ProgramStateRef ProgramState::bindLoc(Loc LV, SVal V) const {
149 ProgramStateManager &Mgr = getStateManager();
150 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
152 const MemRegion *MR = LV.getAsRegion();
153 if (MR && Mgr.getOwningEngine())
154 return Mgr.getOwningEngine()->processRegionChange(newState, MR);
159 ProgramStateRef ProgramState::bindDefault(SVal loc, SVal V) const {
160 ProgramStateManager &Mgr = getStateManager();
161 const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion();
162 const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
163 ProgramStateRef new_state = makeWithStore(newStore);
164 return Mgr.getOwningEngine() ?
165 Mgr.getOwningEngine()->processRegionChange(new_state, R) :
170 ProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions,
171 const Expr *E, unsigned Count,
172 const LocationContext *LCtx,
173 StoreManager::InvalidatedSymbols *IS,
174 const CallEvent *Call) const {
176 StoreManager::InvalidatedSymbols invalidated;
177 return invalidateRegionsImpl(Regions, E, Count, LCtx,
180 return invalidateRegionsImpl(Regions, E, Count, LCtx, *IS, Call);
184 ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
185 const Expr *E, unsigned Count,
186 const LocationContext *LCtx,
187 StoreManager::InvalidatedSymbols &IS,
188 const CallEvent *Call) const {
189 ProgramStateManager &Mgr = getStateManager();
190 SubEngine* Eng = Mgr.getOwningEngine();
192 if (Eng && Eng->wantsRegionChangeUpdate(this)) {
193 StoreManager::InvalidatedRegions Invalidated;
194 const StoreRef &newStore
195 = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS,
197 ProgramStateRef newState = makeWithStore(newStore);
198 return Eng->processRegionChanges(newState, &IS, Regions, Invalidated, Call);
201 const StoreRef &newStore =
202 Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS,
204 return makeWithStore(newStore);
207 ProgramStateRef ProgramState::unbindLoc(Loc LV) const {
208 assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead.");
210 Store OldStore = getStore();
211 const StoreRef &newStore = getStateManager().StoreMgr->Remove(OldStore, LV);
213 if (newStore.getStore() == OldStore)
216 return makeWithStore(newStore);
220 ProgramState::enterStackFrame(const CallEvent &Call,
221 const StackFrameContext *CalleeCtx) const {
222 const StoreRef &NewStore =
223 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
224 return makeWithStore(NewStore);
227 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
228 // We only want to do fetches from regions that we can actually bind
229 // values. For example, SymbolicRegions of type 'id<...>' cannot
230 // have direct bindings (but their can be bindings on their subregions).
231 if (!R->isBoundable())
234 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
235 QualType T = TR->getValueType();
236 if (Loc::isLocType(T) || T->isIntegerType())
243 SVal ProgramState::getSVal(Loc location, QualType T) const {
244 SVal V = getRawSVal(cast<Loc>(location), T);
246 // If 'V' is a symbolic value that is *perfectly* constrained to
247 // be a constant value, use that value instead to lessen the burden
248 // on later analysis stages (so we have less symbolic values to reason
251 if (SymbolRef sym = V.getAsSymbol()) {
252 if (const llvm::APSInt *Int = getSymVal(sym)) {
253 // FIXME: Because we don't correctly model (yet) sign-extension
254 // and truncation of symbolic values, we need to convert
255 // the integer value to the correct signedness and bitwidth.
257 // This shows up in the following:
260 // unsigned x = foo();
264 // The symbolic value stored to 'x' is actually the conjured
265 // symbol for the call to foo(); the type of that symbol is 'char',
267 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
270 return loc::ConcreteInt(NewV);
272 return nonloc::ConcreteInt(NewV);
280 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
281 const LocationContext *LCtx,
282 SVal V, bool Invalidate) const{
284 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
289 ProgramState NewSt = *this;
291 return getStateManager().getPersistentState(NewSt);
295 ProgramState::bindExprAndLocation(const Stmt *S, const LocationContext *LCtx,
299 getStateManager().EnvMgr.bindExprAndLocation(Env,
300 EnvironmentEntry(S, LCtx),
306 ProgramState NewSt = *this;
308 return getStateManager().getPersistentState(NewSt);
311 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
312 DefinedOrUnknownSVal UpperBound,
314 QualType indexTy) const {
315 if (Idx.isUnknown() || UpperBound.isUnknown())
318 // Build an expression for 0 <= Idx < UpperBound.
319 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
320 // FIXME: This should probably be part of SValBuilder.
321 ProgramStateManager &SM = getStateManager();
322 SValBuilder &svalBuilder = SM.getSValBuilder();
323 ASTContext &Ctx = svalBuilder.getContext();
325 // Get the offset: the minimum value of the array index type.
326 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
327 // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
328 if (indexTy.isNull())
330 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
333 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
334 cast<NonLoc>(Idx), Min, indexTy);
335 if (newIdx.isUnknownOrUndef())
338 // Adjust the upper bound.
340 svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound),
343 if (newBound.isUnknownOrUndef())
346 // Build the actual comparison.
347 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT,
348 cast<NonLoc>(newIdx), cast<NonLoc>(newBound),
350 if (inBound.isUnknownOrUndef())
353 // Finally, let the constraint manager take care of it.
354 ConstraintManager &CM = SM.getConstraintManager();
355 return CM.assume(this, cast<DefinedSVal>(inBound), Assumption);
358 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
359 ProgramState State(this,
360 EnvMgr.getInitialEnvironment(),
361 StoreMgr->getInitialStore(InitLoc),
362 GDMFactory.getEmptyMap());
364 return getPersistentState(State);
367 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
368 ProgramStateRef FromState,
369 ProgramStateRef GDMState) {
370 ProgramState NewState(*FromState);
371 NewState.GDM = GDMState->GDM;
372 return getPersistentState(NewState);
375 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
377 llvm::FoldingSetNodeID ID;
381 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
384 ProgramState *newState = 0;
385 if (!freeStates.empty()) {
386 newState = freeStates.back();
387 freeStates.pop_back();
390 newState = (ProgramState*) Alloc.Allocate<ProgramState>();
392 new (newState) ProgramState(State);
393 StateSet.InsertNode(newState, InsertPos);
397 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
398 ProgramState NewSt(*this);
399 NewSt.setStore(store);
400 return getStateManager().getPersistentState(NewSt);
403 void ProgramState::setStore(const StoreRef &newStore) {
404 Store newStoreStore = newStore.getStore();
406 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
408 stateMgr->getStoreManager().decrementReferenceCount(store);
409 store = newStoreStore;
412 //===----------------------------------------------------------------------===//
413 // State pretty-printing.
414 //===----------------------------------------------------------------------===//
416 void ProgramState::print(raw_ostream &Out,
417 const char *NL, const char *Sep) const {
419 ProgramStateManager &Mgr = getStateManager();
420 Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
422 // Print out the environment.
423 Env.print(Out, NL, Sep);
425 // Print out the constraints.
426 Mgr.getConstraintManager().print(this, Out, NL, Sep);
428 // Print checker-specific data.
429 Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
432 void ProgramState::printDOT(raw_ostream &Out) const {
433 print(Out, "\\l", "\\|");
436 void ProgramState::dump() const {
440 void ProgramState::printTaint(raw_ostream &Out,
441 const char *NL, const char *Sep) const {
442 TaintMapImpl TM = get<TaintMap>();
445 Out <<"Tainted Symbols:" << NL;
447 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
448 Out << I->first << " : " << I->second << NL;
452 void ProgramState::dumpTaint() const {
453 printTaint(llvm::errs());
456 //===----------------------------------------------------------------------===//
458 //===----------------------------------------------------------------------===//
460 void *const* ProgramState::FindGDM(void *K) const {
461 return GDM.lookup(K);
465 ProgramStateManager::FindGDMContext(void *K,
466 void *(*CreateContext)(llvm::BumpPtrAllocator&),
467 void (*DeleteContext)(void*)) {
469 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
471 p.first = CreateContext(Alloc);
472 p.second = DeleteContext;
478 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
479 ProgramState::GenericDataMap M1 = St->getGDM();
480 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
485 ProgramState NewSt = *St;
487 return getPersistentState(NewSt);
490 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
491 ProgramState::GenericDataMap OldM = state->getGDM();
492 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
497 ProgramState NewState = *state;
499 return getPersistentState(NewState);
502 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
503 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
510 bool ScanReachableSymbols::scan(const SymExpr *sym) {
511 unsigned &isVisited = visited[sym];
516 if (!visitor.VisitSymbol(sym))
519 // TODO: should be rewritten using SymExpr::symbol_iterator.
520 switch (sym->getKind()) {
521 case SymExpr::RegionValueKind:
522 case SymExpr::ConjuredKind:
523 case SymExpr::DerivedKind:
524 case SymExpr::ExtentKind:
525 case SymExpr::MetadataKind:
527 case SymExpr::CastSymbolKind:
528 return scan(cast<SymbolCast>(sym)->getOperand());
529 case SymExpr::SymIntKind:
530 return scan(cast<SymIntExpr>(sym)->getLHS());
531 case SymExpr::IntSymKind:
532 return scan(cast<IntSymExpr>(sym)->getRHS());
533 case SymExpr::SymSymKind: {
534 const SymSymExpr *x = cast<SymSymExpr>(sym);
535 return scan(x->getLHS()) && scan(x->getRHS());
541 bool ScanReachableSymbols::scan(SVal val) {
542 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
543 return scan(X->getRegion());
545 if (nonloc::LazyCompoundVal *X = dyn_cast<nonloc::LazyCompoundVal>(&val))
546 return scan(X->getRegion());
548 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
549 return scan(X->getLoc());
551 if (SymbolRef Sym = val.getAsSymbol())
554 if (const SymExpr *Sym = val.getAsSymbolicExpression())
557 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
563 bool ScanReachableSymbols::scan(const MemRegion *R) {
564 if (isa<MemSpaceRegion>(R))
567 unsigned &isVisited = visited[R];
573 if (!visitor.VisitMemRegion(R))
576 // If this is a symbolic region, visit the symbol for the region.
577 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
578 if (!visitor.VisitSymbol(SR->getSymbol()))
581 // If this is a subregion, also visit the parent regions.
582 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
583 const MemRegion *Super = SR->getSuperRegion();
587 // When we reach the topmost region, scan all symbols in it.
588 if (isa<MemSpaceRegion>(Super)) {
589 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
590 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
595 // Regions captured by a block are also implicitly reachable.
596 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
597 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
598 E = BDR->referenced_vars_end();
599 for ( ; I != E; ++I) {
600 if (!scan(I.getCapturedRegion()))
608 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
609 ScanReachableSymbols S(this, visitor);
613 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
614 SymbolVisitor &visitor) const {
615 ScanReachableSymbols S(this, visitor);
616 for ( ; I != E; ++I) {
623 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
624 const MemRegion * const *E,
625 SymbolVisitor &visitor) const {
626 ScanReachableSymbols S(this, visitor);
627 for ( ; I != E; ++I) {
634 ProgramStateRef ProgramState::addTaint(const Stmt *S,
635 const LocationContext *LCtx,
636 TaintTagType Kind) const {
637 if (const Expr *E = dyn_cast_or_null<Expr>(S))
638 S = E->IgnoreParens();
640 SymbolRef Sym = getSVal(S, LCtx).getAsSymbol();
642 return addTaint(Sym, Kind);
644 const MemRegion *R = getSVal(S, LCtx).getAsRegion();
647 // Cannot add taint, so just return the state.
651 ProgramStateRef ProgramState::addTaint(const MemRegion *R,
652 TaintTagType Kind) const {
653 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
654 return addTaint(SR->getSymbol(), Kind);
658 ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
659 TaintTagType Kind) const {
660 // If this is a symbol cast, remove the cast before adding the taint. Taint
662 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
663 Sym = SC->getOperand();
665 ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
670 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
671 TaintTagType Kind) const {
672 if (const Expr *E = dyn_cast_or_null<Expr>(S))
673 S = E->IgnoreParens();
675 SVal val = getSVal(S, LCtx);
676 return isTainted(val, Kind);
679 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
680 if (const SymExpr *Sym = V.getAsSymExpr())
681 return isTainted(Sym, Kind);
682 if (const MemRegion *Reg = V.getAsRegion())
683 return isTainted(Reg, Kind);
687 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
691 // Element region (array element) is tainted if either the base or the offset
693 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
694 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
696 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
697 return isTainted(SR->getSymbol(), K);
699 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
700 return isTainted(ER->getSuperRegion(), K);
705 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
709 // Traverse all the symbols this symbol depends on to see if any are tainted.
710 bool Tainted = false;
711 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
713 assert(isa<SymbolData>(*SI));
714 const TaintTagType *Tag = get<TaintMap>(*SI);
715 Tainted = (Tag && *Tag == Kind);
717 // If this is a SymbolDerived with a tainted parent, it's also tainted.
718 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
719 Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
721 // If memory region is tainted, data is also tainted.
722 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
723 Tainted = Tainted || isTainted(SRV->getRegion(), Kind);
725 // If If this is a SymbolCast from a tainted value, it's also tainted.
726 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI))
727 Tainted = Tainted || isTainted(SC->getOperand(), Kind);
736 /// The GDM component containing the dynamic type info. This is a map from a
737 /// symbol to it's most likely type.
740 typedef llvm::ImmutableMap<const MemRegion *, DynamicTypeInfo> DynamicTypeMap;
741 template<> struct ProgramStateTrait<DynamicTypeMap>
742 : public ProgramStatePartialTrait<DynamicTypeMap> {
743 static void *GDMIndex() { static int index; return &index; }
747 DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const {
748 // Look up the dynamic type in the GDM.
749 const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg);
753 // Otherwise, fall back to what we know about the region.
754 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(Reg))
755 return DynamicTypeInfo(TR->getValueType());
757 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) {
758 SymbolRef Sym = SR->getSymbol();
759 return DynamicTypeInfo(Sym->getType(getStateManager().getContext()));
762 return DynamicTypeInfo();
765 ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg,
766 DynamicTypeInfo NewTy) const {
767 ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy);