Merged
fix(machine): Split runtime headers and use secure bundle functions #6422
fix(machine): Split runtime headers and use secure bundle functions #6422
Merged
W4termyAdd on Sep 17
Total: 7 files changed+196-127
@@ -19,6 +19,7 @@
19#include "machine/utils/machine_ws_intf.h"19#include "machine/utils/machine_ws_intf.h"
20#include "machine/device/tilefwk/core_func_data.h"20#include "machine/device/tilefwk/core_func_data.h"
21#include "core_status_manager.h"21#include "core_status_manager.h"
22+#include "wrap_task_utils.h"
22 23 
23namespace npu::tile_fwk::dynamic {24namespace npu::tile_fwk::dynamic {
24 25 
@@ -26,48 +27,6 @@ struct SchDeviceTaskContext;
26using SendTaskToAiCoreFunc = std::function<void(struct SchDeviceTaskContext* devCtx, CoreType type, int coreIdx,27using SendTaskToAiCoreFunc = std::function<void(struct SchDeviceTaskContext* devCtx, CoreType type, int coreIdx,
27 uint64_t newTask)>;28 uint64_t newTask)>;
28 29 
29-enum class MixResourceType { MIX_UNKNOWN = 0, MIX_1C1V = 1, MIX_1C2V = 2 };
30-enum class DieId { DIE_0 = 0, DIE_1 = 1, DIE_MIX = 2, DIE_UNKNOWN };
31- 
32-inline void WrapInfoQueueLock(WrapInfoQueue* rq)
33-{
34- while (!__sync_bool_compare_and_swap(&rq->lock, 0, 1)) {
35- }
36-}
37- 
38-inline void WrapInfoQueueUnLock(WrapInfoQueue* rq)
39-{
40- while (!__sync_bool_compare_and_swap(&rq->lock, 1, 0)) {
41- }
42-}
43- 
44-inline uint32_t GetTaskNumByMixResType(uint8_t mixType)
45-{
46- switch (mixType) {
47- case static_cast<uint8_t>(MixResourceType::MIX_1C1V):
48- return 2;
49- case static_cast<uint8_t>(MixResourceType::MIX_1C2V):
50- return 3;
51- default:
52- return 0;
53- }
54-}
55- 
56-inline bool IsWrapTaskReady(const WrapInfo* wrapInfo)
57-{
58- switch (wrapInfo->mixResourceType) {
59- case static_cast<uint8_t>(MixResourceType::MIX_1C1V):
60- return wrapInfo->tasklist[WRAP_IDX_AIC] != AICORE_TASK_INIT &&
61- wrapInfo->tasklist[WRAP_IDX_AIV0] != AICORE_TASK_INIT;
62- case static_cast<uint8_t>(MixResourceType::MIX_1C2V):
63- return wrapInfo->tasklist[WRAP_IDX_AIC] != AICORE_TASK_INIT &&
64- wrapInfo->tasklist[WRAP_IDX_AIV0] != AICORE_TASK_INIT &&
65- wrapInfo->tasklist[WRAP_IDX_AIV1] != AICORE_TASK_INIT;
66- default:
67- return false;
68- }
69-}
70- 
71#define RETURN_NULL_IF_NOT(val) \30#define RETURN_NULL_IF_NOT(val) \
72 do { \31 do { \
73 if (!(val)) \32 if (!(val)) \
@@ -0,0 +1,66 @@
1+/**
2+ * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+/*!
12+ * \file wrap_task_utils.h
13+ * \brief Wrap task queue and readiness helpers.
14+ */
15+ 
16+#pragma once
17+ 
18+#include <cstdint>
19+#include "aicore_constants.h"
20+#include "machine/utils/machine_ws_intf.h"
21+#include "machine/device/tilefwk/core_func_data.h"
22+ 
23+namespace npu::tile_fwk::dynamic {
24+enum class MixResourceType { MIX_UNKNOWN = 0, MIX_1C1V = 1, MIX_1C2V = 2 };
25+enum class DieId { DIE_0 = 0, DIE_1 = 1, DIE_MIX = 2, DIE_UNKNOWN };
26+ 
27+inline void WrapInfoQueueLock(WrapInfoQueue* rq)
28+{
29+ while (!__sync_bool_compare_and_swap(&rq->lock, 0, 1)) {
30+ }
31+}
32+ 
33+inline void WrapInfoQueueUnLock(WrapInfoQueue* rq)
34+{
35+ while (!__sync_bool_compare_and_swap(&rq->lock, 1, 0)) {
36+ }
37+}
38+ 
39+inline uint32_t GetTaskNumByMixResType(uint8_t mixType)
40+{
41+ switch (mixType) {
42+ case static_cast<uint8_t>(MixResourceType::MIX_1C1V):
43+ return 2;
44+ case static_cast<uint8_t>(MixResourceType::MIX_1C2V):
45+ return 3;
46+ default:
47+ return 0;
48+ }
49+}
50+ 
51+inline bool IsWrapTaskReady(const WrapInfo* wrapInfo)
52+{
53+ switch (wrapInfo->mixResourceType) {
54+ case static_cast<uint8_t>(MixResourceType::MIX_1C1V):
55+ return wrapInfo->tasklist[WRAP_IDX_AIC] != AICORE_TASK_INIT &&
56+ wrapInfo->tasklist[WRAP_IDX_AIV0] != AICORE_TASK_INIT;
57+ case static_cast<uint8_t>(MixResourceType::MIX_1C2V):
58+ return wrapInfo->tasklist[WRAP_IDX_AIC] != AICORE_TASK_INIT &&
59+ wrapInfo->tasklist[WRAP_IDX_AIV0] != AICORE_TASK_INIT &&
60+ wrapInfo->tasklist[WRAP_IDX_AIV1] != AICORE_TASK_INIT;
61+ default:
62+ return false;
63+ }
64+}
65+ 
66+} // namespace npu::tile_fwk::dynamic
@@ -98,7 +98,9 @@ std::string MakeAicpuOpName(const std::string& displayName, uint64_t bundleKey)
98 return displayName;98 return displayName;
99 }99 }
100 char keySuffix[18] = {};100 char keySuffix[18] = {};
101- (void)std::snprintf(keySuffix, sizeof(keySuffix), "_%016llx", static_cast<unsigned long long>(bundleKey));101+ const int ret = snprintf_s(keySuffix, sizeof(keySuffix), sizeof(keySuffix) - 1, "_%016llx",
102+ static_cast<unsigned long long>(bundleKey));
103+ MACHINE_ASSERT(ret >= 0) << "[kernel-bundle] failed to format AICPU op name suffix";
102 const size_t prefixSize = maxNameSize - std::strlen(keySuffix);104 const size_t prefixSize = maxNameSize - std::strlen(keySuffix);
103 return displayName.substr(0, prefixSize) + keySuffix;105 return displayName.substr(0, prefixSize) + keySuffix;
104}106}
@@ -15,12 +15,12 @@
15 15 
16#include "machine/runtime/bundle/kernel_bundle_loader.h"16#include "machine/runtime/bundle/kernel_bundle_loader.h"
17 17 
18-#include <cstring>
19#include <iomanip>18#include <iomanip>
20#include <sstream>19#include <sstream>
21 20 
22#include "machine/runtime/bundle/kernel_bundle_crc32.h"21#include "machine/runtime/bundle/kernel_bundle_crc32.h"
23#include "machine/runtime/bundle/kernel_bundle_format.h"22#include "machine/runtime/bundle/kernel_bundle_format.h"
23+#include "machine/runtime/runner/runtime_utils.h"
24#include "utils/file_utils.h"24#include "utils/file_utils.h"
25#include "tilefwk/error_code.h"25#include "tilefwk/error_code.h"
26#include "tilefwk/pypto_fwk_log.h"26#include "tilefwk/pypto_fwk_log.h"
@@ -39,7 +39,7 @@ std::vector<uint8_t> CopySegment(const uint8_t* src, size_t len)
39{39{
40 std::vector<uint8_t> v(len);40 std::vector<uint8_t> v(len);
41 if (len != 0) {41 if (len != 0) {
42- std::memcpy(v.data(), src, len);42+ MemcpyS(v.data(), v.size(), src, len);
43 }43 }
44 return v;44 return v;
45}45}
@@ -122,7 +122,7 @@ std::shared_ptr<LoadedBundle> KernelBundleLoader::LoadFromMemory(const uint8_t*
122 return nullptr;122 return nullptr;
123 }123 }
124 BundleHeader hdr{};124 BundleHeader hdr{};
125- std::memcpy(&hdr, p, sizeof(BundleHeader));125+ MemcpyS(&hdr, sizeof(hdr), p, sizeof(BundleHeader));
126 if (hdr.magic != kBundleMagic) {126 if (hdr.magic != kBundleMagic) {
127 MACHINE_LOGE(DevCommonErr::FILE_ERROR, "[kernel-bundle] bad magic %#lx", hdr.magic);127 MACHINE_LOGE(DevCommonErr::FILE_ERROR, "[kernel-bundle] bad magic %#lx", hdr.magic);
128 return nullptr;128 return nullptr;
@@ -151,7 +151,8 @@ std::shared_ptr<LoadedBundle> KernelBundleLoader::LoadFromMemory(const uint8_t*
151 uint64_t bundleKey = 0xCBF29CE484222325ULL; // FNV-1a offset basis151 uint64_t bundleKey = 0xCBF29CE484222325ULL; // FNV-1a offset basis
152 for (uint32_t i = 0; i < hdr.tlvCount; ++i) {152 for (uint32_t i = 0; i < hdr.tlvCount; ++i) {
153 TlvHeader t{};153 TlvHeader t{};
154- std::memcpy(&t, p + sizeof(BundleHeader) + static_cast<size_t>(i) * sizeof(TlvHeader), sizeof(TlvHeader));154+ MemcpyS(&t, sizeof(t), p + sizeof(BundleHeader) + static_cast<size_t>(i) * sizeof(TlvHeader),
155+ sizeof(TlvHeader));
155 if (t.valueOffset > n || t.valueLength > n - t.valueOffset) {156 if (t.valueOffset > n || t.valueLength > n - t.valueOffset) {
156 MACHINE_LOGE(DevCommonErr::FILE_ERROR, "[kernel-bundle] TLV[%u] value out of range", i);157 MACHINE_LOGE(DevCommonErr::FILE_ERROR, "[kernel-bundle] TLV[%u] value out of range", i);
157 return nullptr;158 return nullptr;
@@ -16,10 +16,10 @@
16#include "machine/runtime/bundle/pack/kernel_bundle_packer.h"16#include "machine/runtime/bundle/pack/kernel_bundle_packer.h"
17 17 
18#include <array>18#include <array>
19-#include <cstring>
20 19 
21#include "machine/runtime/bundle/kernel_bundle_crc32.h"20#include "machine/runtime/bundle/kernel_bundle_crc32.h"
22#include "machine/runtime/bundle/kernel_bundle_format.h"21#include "machine/runtime/bundle/kernel_bundle_format.h"
22+#include "machine/runtime/runner/runtime_utils.h"
23#include "utils/file_utils.h"23#include "utils/file_utils.h"
24#include "tilefwk/error_code.h"24#include "tilefwk/error_code.h"
25#include "tilefwk/pypto_fwk_log.h"25#include "tilefwk/pypto_fwk_log.h"
@@ -75,12 +75,13 @@ std::vector<uint8_t> KernelBundlePacker::Build() const
75 hdr.totalSize = totalSize;75 hdr.totalSize = totalSize;
76 hdr.headerCrc32 = Crc32(&hdr, sizeof(BundleHeader) - sizeof(uint32_t));76 hdr.headerCrc32 = Crc32(&hdr, sizeof(BundleHeader) - sizeof(uint32_t));
77 77 
78- std::memcpy(buf.data(), &hdr, sizeof(BundleHeader));78+ MemcpyS(buf.data(), buf.size(), &hdr, sizeof(BundleHeader));
79- std::memcpy(buf.data() + sizeof(BundleHeader), tlvs.data(), tlvCount * sizeof(TlvHeader));79+ MemcpyS(buf.data() + sizeof(BundleHeader), buf.size() - sizeof(BundleHeader), tlvs.data(),
80+ tlvCount * sizeof(TlvHeader));
80 for (uint32_t i = 0; i < tlvCount; ++i) {81 for (uint32_t i = 0; i < tlvCount; ++i) {
81 const auto& d = *segs[i].data;82 const auto& d = *segs[i].data;
82 if (!d.empty()) {83 if (!d.empty()) {
83- std::memcpy(buf.data() + tlvs[i].valueOffset, d.data(), d.size());84+ MemcpyS(buf.data() + tlvs[i].valueOffset, buf.size() - tlvs[i].valueOffset, d.data(), d.size());
84 }85 }
85 }86 }
86 return buf;87 return buf;
@@ -302,82 +302,14 @@ public:
302 inline int GetNoSuccOpIdx(size_t idx) const { return At(noSuccOpList_, idx); }302 inline int GetNoSuccOpIdx(size_t idx) const { return At(noSuccOpList_, idx); }
303 303 
304 inline size_t GetOperationSize() const { return operationList_.size(); }304 inline size_t GetOperationSize() const { return operationList_.size(); }
305- inline bool IsDeadEndHub(uint32_t opIndex) const305+ inline bool IsDeadEndHub(uint32_t opIndex) const;
306- {306+ inline bool IsTailTask(uint32_t opIndex) const;
307- if (deadEndHubBitmap_.size() == 0)307+ inline void ClearTailTask(uint32_t opIndex);
308- return false;308+ inline bool ClearDeadEndHub(uint32_t opIndex);
309- uint32_t wordIdx = opIndex / 64;309+ inline void PropagateDeadHubClear(uint32_t clearedOpIdx);
310- return (At(deadEndHubBitmap_, wordIdx) & (1ULL << (opIndex % 64))) != 0;310+ inline size_t GetBitmapByteSize() const;
311- }311+ inline void BackupBitmapTo(uint64_t* deadEndBuf, uint64_t* tailBuf, size_t byteSize) const;
312- inline bool IsTailTask(uint32_t opIndex) const312+ inline void RestoreBitmapFrom(const uint64_t* deadEndBuf, const uint64_t* tailBuf, size_t byteSize);
313- {
314- if (tailTaskBitmap_.size() == 0) {
315- return false;
316- }
317- uint32_t wordIdx = opIndex / 64;
318- return (At(tailTaskBitmap_, wordIdx) & (1ULL << (opIndex % 64))) != 0;
319- }
320- inline void ClearTailTask(uint32_t opIndex)
321- {
322- if (tailTaskBitmap_.size() == 0) {
323- return;
324- }
325- uint32_t wordIdx = opIndex / 64;
326- At(tailTaskBitmap_, wordIdx) &= ~(1ULL << (opIndex % 64));
327- }
328- inline bool ClearDeadEndHub(uint32_t opIndex)
329- {
330- if (deadEndHubBitmap_.size() == 0)
331- return false;
332- uint32_t wordIdx = opIndex / 64;
333- uint64_t bit = 1ULL << (opIndex % 64);
334- auto& word = At(deadEndHubBitmap_, wordIdx);
335- bool wasSet = (word & bit) != 0;
336- word &= ~bit;
337- return wasSet;
338- }
339- inline void PropagateDeadHubClear(uint32_t clearedOpIdx)
340- {
341- constexpr size_t kMaxStack = 128;
342- uint32_t stack[kMaxStack];
343- size_t stackSize = 0;
344- stack[stackSize++] = clearedOpIdx;
345- 
346- size_t opCount = GetOperationSize();
347- while (stackSize > 0) {
348- uint32_t target = stack[--stackSize];
349- for (size_t op = 0; op < opCount; op++) {
350- size_t succSize;
351- const int* succList = GetOperationDepGraphSuccAddr(static_cast<int>(op), succSize);
352- for (size_t j = 0; j < succSize; j++) {
353- if (static_cast<uint32_t>(succList[j]) != target)
354- continue;
355- ClearTailTask(static_cast<uint32_t>(op));
356- if (ClearDeadEndHub(static_cast<uint32_t>(op)) && stackSize < kMaxStack) {
357- stack[stackSize++] = static_cast<uint32_t>(op);
358- }
359- break;
360- }
361- }
362- }
363- }
364- inline size_t GetBitmapByteSize() const { return deadEndHubBitmap_.size() * sizeof(uint64_t); }
365- inline void BackupBitmapTo(uint64_t* deadEndBuf, uint64_t* tailBuf, size_t byteSize) const
366- {
367- if (byteSize == 0) {
368- return;
369- }
370- DevMemcpyS(deadEndBuf, byteSize, &At(deadEndHubBitmap_, 0), byteSize);
371- DevMemcpyS(tailBuf, byteSize, &At(tailTaskBitmap_, 0), byteSize);
372- }
373- inline void RestoreBitmapFrom(const uint64_t* deadEndBuf, const uint64_t* tailBuf, size_t byteSize)
374- {
375- if (byteSize == 0) {
376- return;
377- }
378- DevMemcpyS(&At(deadEndHubBitmap_, 0), byteSize, deadEndBuf, byteSize);
379- DevMemcpyS(&At(tailTaskBitmap_, 0), byteSize, tailBuf, byteSize);
380- }
381 inline uint32_t GetOperationStitchIndex(int operationIndex) const313 inline uint32_t GetOperationStitchIndex(int operationIndex) const
382 {314 {
383 return At(operationList_, operationIndex).stitchIndex;315 return At(operationList_, operationIndex).stitchIndex;
@@ -662,3 +594,5 @@ private:
662 bool fillContent);594 bool fillContent);
663};595};
664} // namespace npu::tile_fwk::dynamic596} // namespace npu::tile_fwk::dynamic
597+ 
598+#include "dev_encode_function_bitmap.h"
@@ -0,0 +1,106 @@
1+/**
2+ * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+/*!
12+ * \file dev_encode_function_bitmap.h
13+ * \brief Inline bitmap operations for DevAscendFunction.
14+ */
15+ 
16+#pragma once
17+ 
18+#include "dev_encode_function.h"
19+ 
20+namespace npu::tile_fwk::dynamic {
21+ 
22+inline bool DevAscendFunction::IsDeadEndHub(uint32_t opIndex) const
23+{
24+ if (deadEndHubBitmap_.size() == 0)
25+ return false;
26+ uint32_t wordIdx = opIndex / 64;
27+ return (At(deadEndHubBitmap_, wordIdx) & (1ULL << (opIndex % 64))) != 0;
28+}
29+ 
30+inline bool DevAscendFunction::IsTailTask(uint32_t opIndex) const
31+{
32+ if (tailTaskBitmap_.size() == 0) {
33+ return false;
34+ }
35+ uint32_t wordIdx = opIndex / 64;
36+ return (At(tailTaskBitmap_, wordIdx) & (1ULL << (opIndex % 64))) != 0;
37+}
38+ 
39+inline void DevAscendFunction::ClearTailTask(uint32_t opIndex)
40+{
41+ if (tailTaskBitmap_.size() == 0) {
42+ return;
43+ }
44+ uint32_t wordIdx = opIndex / 64;
45+ At(tailTaskBitmap_, wordIdx) &= ~(1ULL << (opIndex % 64));
46+}
47+ 
48+inline bool DevAscendFunction::ClearDeadEndHub(uint32_t opIndex)
49+{
50+ if (deadEndHubBitmap_.size() == 0)
51+ return false;
52+ uint32_t wordIdx = opIndex / 64;
53+ uint64_t bit = 1ULL << (opIndex % 64);
54+ auto& word = At(deadEndHubBitmap_, wordIdx);
55+ bool wasSet = (word & bit) != 0;
56+ word &= ~bit;
57+ return wasSet;
58+}
59+ 
60+inline void DevAscendFunction::PropagateDeadHubClear(uint32_t clearedOpIdx)
61+{
62+ constexpr size_t kMaxStack = 128;
63+ uint32_t stack[kMaxStack];
64+ size_t stackSize = 0;
65+ stack[stackSize++] = clearedOpIdx;
66+ 
67+ size_t opCount = GetOperationSize();
68+ while (stackSize > 0) {
69+ uint32_t target = stack[--stackSize];
70+ for (size_t op = 0; op < opCount; op++) {
71+ size_t succSize;
72+ const int* succList = GetOperationDepGraphSuccAddr(static_cast<int>(op), succSize);
73+ for (size_t j = 0; j < succSize; j++) {
74+ if (static_cast<uint32_t>(succList[j]) != target)
75+ continue;
76+ ClearTailTask(static_cast<uint32_t>(op));
77+ if (ClearDeadEndHub(static_cast<uint32_t>(op)) && stackSize < kMaxStack) {
78+ stack[stackSize++] = static_cast<uint32_t>(op);
79+ }
80+ break;
81+ }
82+ }
83+ }
84+}
85+ 
86+inline size_t DevAscendFunction::GetBitmapByteSize() const { return deadEndHubBitmap_.size() * sizeof(uint64_t); }
87+ 
88+inline void DevAscendFunction::BackupBitmapTo(uint64_t* deadEndBuf, uint64_t* tailBuf, size_t byteSize) const
89+{
90+ if (byteSize == 0) {
91+ return;
92+ }
93+ DevMemcpyS(deadEndBuf, byteSize, &At(deadEndHubBitmap_, 0), byteSize);
94+ DevMemcpyS(tailBuf, byteSize, &At(tailTaskBitmap_, 0), byteSize);
95+}
96+ 
97+inline void DevAscendFunction::RestoreBitmapFrom(const uint64_t* deadEndBuf, const uint64_t* tailBuf, size_t byteSize)
98+{
99+ if (byteSize == 0) {
100+ return;
101+ }
102+ DevMemcpyS(&At(deadEndHubBitmap_, 0), byteSize, deadEndBuf, byteSize);
103+ DevMemcpyS(&At(tailTaskBitmap_, 0), byteSize, tailBuf, byteSize);
104+}
105+ 
106+} // namespace npu::tile_fwk::dynamic