Skip to content

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

mssanitizer-basic-usage

Verified

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

references/sample_racecheck/racecheck.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 racecheck.asc
 * \brief 算子数据竞争检测样例,演示 mssanitizer 对算子跨流水线 RAW 数据竞争的检测和定位
 */
#include "acl/acl.h"
#include "kernel_operator.h"

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

    __aicore__ inline void Process()
    {
        AscendC::LocalMemAllocator<AscendC::Hardware::UB> ubAlloc;
        AscendC::LocalTensor<float> localBuf = ubAlloc.Alloc<float, elemCnt>();

        // V 流水写入 Global Memory 后,MTE2 流水立即读取同一地址,
        // 缺少 SetFlag/WaitFlag 同步保护,构成 RAW 数据竞争
        uint32_t blockIdx = AscendC::GetBlockIdx();
        gmBuf.SetValue(blockIdx, (float)100.0f);       // V pipe: 写入
        AscendC::DataCopy(localBuf, gmBuf, elemCnt);   // MTE2 pipe: 读取(缺同步!)
    }

private:
    constexpr static uint32_t elemCnt = 256;
    AscendC::GlobalTensor<float> gmBuf;
};

__global__ __vector__ void raw_hazard_demo(GM_ADDR buf)
{
    AscendC::InitSocState();
    RawHazardDemo 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);

    // 启动 2 个 block,构造跨 block 数据竞争场景
    raw_hazard_demo<<<2, 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: 193kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 193kUpdated 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: 193kUpdated Claude CodeCodexVerifiedInstall