Path: blob/main/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_origin.h
35233 views
//===-- dfsan_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// This file is a part of DataFlowSanitizer.9//10// Origin id utils.11//===----------------------------------------------------------------------===//1213#ifndef DFSAN_ORIGIN_H14#define DFSAN_ORIGIN_H1516#include "dfsan_chained_origin_depot.h"17#include "dfsan_flags.h"18#include "sanitizer_common/sanitizer_stackdepot.h"1920namespace __dfsan {2122// Origin handling.23//24// Origin is a 32-bit identifier that is attached to any taint value in the25// program and describes how this memory came to be tainted.26//27// Chained origin id is like:28// zzzz xxxx xxxx xxxx29//30// Chained origin id describes an event of storing a taint value to31// memory. The xxx part is a value of ChainedOriginDepot, which is a mapping of32// (stack_id, prev_id) -> id, where33// * stack_id describes the event.34// StackDepot keeps a mapping between those and corresponding stack traces.35// * prev_id is another origin id that describes the earlier part of the36// taint value history. 0 prev_id indicates the start of a chain.37// Following a chain of prev_id provides the full recorded history of a taint38// value.39//40// This, effectively, defines a forest where nodes are points in value history41// marked with origin ids, and edges are events that are marked with stack_id.42//43// The "zzzz" bits of chained origin id are used to store the length of the44// origin chain.4546class Origin {47public:48static bool isValidId(u32 id) { return id != 0; }4950u32 raw_id() const { return raw_id_; }5152bool isChainedOrigin() const { return Origin::isValidId(raw_id_); }5354u32 getChainedId() const {55CHECK(Origin::isValidId(raw_id_));56return raw_id_ & kChainedIdMask;57}5859// Returns the next origin in the chain and the current stack trace.60//61// It scans a partition of StackDepot linearly, and is used only by origin62// tracking report.63Origin getNextChainedOrigin(StackTrace *stack) const {64CHECK(Origin::isValidId(raw_id_));65u32 prev_id;66u32 stack_id = GetChainedOriginDepot()->Get(getChainedId(), &prev_id);67if (stack)68*stack = StackDepotGet(stack_id);69return Origin(prev_id);70}7172static Origin CreateChainedOrigin(Origin prev, StackTrace *stack) {73int depth = prev.isChainedOrigin() ? prev.depth() : -1;74// depth is the length of the chain minus 1.75// origin_history_size of 0 means unlimited depth.76if (flags().origin_history_size > 0) {77++depth;78if (depth >= flags().origin_history_size || depth > kMaxDepth)79return prev;80}8182StackDepotHandle h = StackDepotPut_WithHandle(*stack);83if (!h.valid())84return prev;8586if (flags().origin_history_per_stack_limit > 0) {87int use_count = h.use_count();88if (use_count > flags().origin_history_per_stack_limit)89return prev;90}9192u32 chained_id;93bool inserted =94GetChainedOriginDepot()->Put(h.id(), prev.raw_id(), &chained_id);95CHECK((chained_id & kChainedIdMask) == chained_id);9697if (inserted && flags().origin_history_per_stack_limit > 0)98h.inc_use_count_unsafe();99100return Origin((depth << kDepthShift) | chained_id);101}102103static Origin FromRawId(u32 id) { return Origin(id); }104105private:106static const int kDepthBits = 4;107static const int kDepthShift = 32 - kDepthBits;108109static const u32 kChainedIdMask = ((u32)-1) >> kDepthBits;110111u32 raw_id_;112113explicit Origin(u32 raw_id) : raw_id_(raw_id) {}114115int depth() const {116CHECK(isChainedOrigin());117return (raw_id_ >> kDepthShift) & ((1 << kDepthBits) - 1);118}119120public:121static const int kMaxDepth = (1 << kDepthBits) - 1;122};123124} // namespace __dfsan125126#endif // DFSAN_ORIGIN_H127128129