Path: blob/main/contrib/llvm-project/compiler-rt/lib/msan/msan_origin.h
35262 views
//===-- msan_origin.h ----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Origin id utils.9//===----------------------------------------------------------------------===//10#ifndef MSAN_ORIGIN_H11#define MSAN_ORIGIN_H1213#include "sanitizer_common/sanitizer_stackdepot.h"14#include "msan_chained_origin_depot.h"1516namespace __msan {1718// Origin handling.19//20// Origin is a 32-bit identifier that is attached to any uninitialized value in21// the program and describes, more or less exactly, how this memory came to be22// uninitialized.23//24// There are 3 kinds of origin ids:25// 1xxx xxxx xxxx xxxx heap origin id26// 0000 xxxx xxxx xxxx stack origin id27// 0zzz xxxx xxxx xxxx chained origin id28//29// Heap origin id describes a heap memory allocation and contains (in the xxx30// part) a value of StackDepot.31//32// Stack origin id describes a stack memory allocation and contains (in the xxx33// part) an index into StackOriginDescr and StackOriginPC. We don't store a34// stack trace for such origins for performance reasons.35//36// Chained origin id describes an event of storing an uninitialized value to37// memory. The xxx part is a value of ChainedOriginDepot, which is a mapping of38// (stack_id, prev_id) -> id, where39// * stack_id describes the event.40// StackDepot keeps a mapping between those and corresponding stack traces.41// * prev_id is another origin id that describes the earlier part of the42// uninitialized value history.43// Following a chain of prev_id provides the full recorded history of an44// uninitialized value.45//46// This, effectively, defines a tree (or 2 trees, see below) where nodes are47// points in value history marked with origin ids, and edges are events that are48// marked with stack_id.49//50// The "zzz" bits of chained origin id are used to store the length (or depth)51// of the origin chain.5253class Origin {54public:55static bool isValidId(u32 id) { return id != 0 && id != (u32)-1; }5657u32 raw_id() const { return raw_id_; }58bool isHeapOrigin() const {59// 0xxx xxxx xxxx xxxx60return raw_id_ >> kHeapShift == 0;61}62bool isStackOrigin() const {63// 1000 xxxx xxxx xxxx64return (raw_id_ >> kDepthShift) == (1 << kDepthBits);65}66bool isChainedOrigin() const {67// 1zzz xxxx xxxx xxxx, zzz != 00068return (raw_id_ >> kDepthShift) > (1 << kDepthBits);69}70u32 getChainedId() const {71CHECK(isChainedOrigin());72return raw_id_ & kChainedIdMask;73}74u32 getStackId() const {75CHECK(isStackOrigin());76return raw_id_ & kChainedIdMask;77}78u32 getHeapId() const {79CHECK(isHeapOrigin());80return raw_id_ & kHeapIdMask;81}8283// Returns the next origin in the chain and the current stack trace.84Origin getNextChainedOrigin(StackTrace *stack) const {85CHECK(isChainedOrigin());86u32 prev_id;87u32 stack_id = ChainedOriginDepotGet(getChainedId(), &prev_id);88if (stack) *stack = StackDepotGet(stack_id);89return Origin(prev_id);90}9192StackTrace getStackTraceForHeapOrigin() const {93return StackDepotGet(getHeapId());94}9596static Origin CreateStackOrigin(u32 id) {97CHECK((id & kStackIdMask) == id);98return Origin((1 << kHeapShift) | id);99}100101static Origin CreateHeapOrigin(StackTrace *stack) {102u32 stack_id = StackDepotPut(*stack);103CHECK(stack_id);104CHECK((stack_id & kHeapIdMask) == stack_id);105return Origin(stack_id);106}107108static Origin CreateChainedOrigin(Origin prev, StackTrace *stack) {109int depth = prev.isChainedOrigin() ? prev.depth() : 0;110// depth is the length of the chain minus 1.111// origin_history_size of 0 means unlimited depth.112if (flags()->origin_history_size > 0) {113if (depth + 1 >= flags()->origin_history_size) {114return prev;115} else {116++depth;117CHECK(depth < (1 << kDepthBits));118}119}120121StackDepotHandle h = StackDepotPut_WithHandle(*stack);122if (!h.valid()) return prev;123124if (flags()->origin_history_per_stack_limit > 0) {125int use_count = h.use_count();126if (use_count > flags()->origin_history_per_stack_limit) return prev;127}128129u32 chained_id;130bool inserted = ChainedOriginDepotPut(h.id(), prev.raw_id(), &chained_id);131CHECK((chained_id & kChainedIdMask) == chained_id);132133if (inserted && flags()->origin_history_per_stack_limit > 0)134h.inc_use_count_unsafe();135136return Origin((1 << kHeapShift) | (depth << kDepthShift) | chained_id);137}138139static Origin FromRawId(u32 id) {140return Origin(id);141}142143private:144static const int kDepthBits = 3;145static const int kDepthShift = 32 - kDepthBits - 1;146147static const int kHeapShift = 31;148static const u32 kChainedIdMask = ((u32)-1) >> (32 - kDepthShift);149static const u32 kStackIdMask = ((u32)-1) >> (32 - kDepthShift);150static const u32 kHeapIdMask = ((u32)-1) >> (32 - kHeapShift);151152u32 raw_id_;153154explicit Origin(u32 raw_id) : raw_id_(raw_id) {}155156int depth() const {157CHECK(isChainedOrigin());158return (raw_id_ >> kDepthShift) & ((1 << kDepthBits) - 1);159}160161public:162static const int kMaxDepth = (1 << kDepthBits) - 1;163};164165} // namespace __msan166167#endif // MSAN_ORIGIN_H168169170