Skip to content

Ascend/msagent / skills/operator/mssanitizer-basic-usage

mssanitizer-basic-usage

Unverified

昇腾AscendC算子异常检测工具(msSanitizer)。检测内存越界、内存泄漏、数据竞争、未初始化读取、同步异常等缺陷。触发关键词:mssanitizer、sanitizer、检测工具、内存检测、竞争检测、初始化检测、同步检测

references/sample_memcheck/memcheck.asc

View on GitHub
/**
 * Copyright (c) 2026 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/* !
 * \file memcheck.asc
 * \brief 算子内存越界检测样例,演示 mssanitizer 对算子 UB 缓冲区越界写入的检测和定位
 */
#include "acl/acl.h"
#include "kernel_operator.h"

class BoundsWriteDemo {
public:
    __aicore__ inline BoundsWriteDemo() = default;
    __aicore__ inline void Init(GM_ADDR buf)
    {
        srcGlobal.SetGlobalBuffer((__gm__ float*)buf, elemCnt);
    }

    __aicore__ inline void Process()
    {
        AscendC::LocalMemAllocator<AscendC::Hardware::UB> ubAlloc;

        // UB 只分配了 allocCnt 个 float 的空间,
        // 但 DataCopy 搬运 elemCnt 个元素(elemCnt > allocCnt),越界写入
        AscendC::LocalTensor<float> localBuf = ubAlloc.Alloc<float, allocCnt>();
        AscendC::DataCopy(localBuf, srcGlobal, elemCnt);
    }

private:
    constexpr static uint32_t elemCnt = 256;   // DataCopy 搬运量
    constexpr static uint32_t allocCnt = 64;   // UB 实际分配量(不足,导致越界)
    AscendC::GlobalTensor<float> srcGlobal;
};

__global__ __vector__ void bounds_write_demo(GM_ADDR buf)
{
    AscendC::InitSocState();
    BoundsWriteDemo op;
    op.Init(buf);
    op.Process();
}

int32_t main(int32_t argc, char* argv[])
{
    constexpr uint32_t elemCnt = 256;
    size_t bufBytes = elemCnt * sizeof(float);

    aclInit(nullptr);

    int32_t deviceId = 0;
    aclrtSetDevice(deviceId);

    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    uint8_t* devBuf = nullptr;
    aclrtMalloc((void**)&devBuf, bufBytes, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemset(devBuf, bufBytes, 0, bufBytes);

    bounds_write_demo<<<1, nullptr, stream>>>(devBuf);
    aclrtSynchronizeStream(stream);

    aclrtFree(devBuf);
    aclrtDestroyStream(stream);

    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 192kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 192kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 192kUpdated Claude CodeCodexVerifiedInstall