HarmonyOS native回调ts时报错stack-buffer-overflow

native回调ts时报错stack-buffer-overflow,代码如下:

Index.ets:

import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';

@Entry
@Component
struct Index {
  @State message: string = 'Test Node-API callNative result: ';
  @State message2: string = 'Test Node-API nativeCallArkTS result: ';
  private msg :string |undefined= undefined;
  private cnt:number = 0

  build() {
    Row() {
      Column() {
        Text(this.message2)
          .fontSize(16)
          .fontWeight(FontWeight.Bold)
        Button('注册log').btnStyle().onClick(() => {
          testNapi.registerLog((type: number, level: number, domain: number, tag: string, msg: string)=>{
            this.message2+= tag
          })
        })

        Button('打印log').btnStyle().onClick(() => {
          hilog.info(0x0000, '当前cnt =%{public}d',this.cnt );
          this.cnt ++;
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
@Extend(Button) function btnStyle() {
  .width('100%')
  .margin({ top: 8 })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

Index.d.ts:

export const registerLog:(cb:(type: number, level: number, domain: number, tag: string, msg: string)=>void) => void;
  • 1.

C++:

#include <hilog/log.h>
#include <string>
#include "napi/native_api.h"

using namespace std;
#include "hilog/log.h"
#include <node_api.h>
#include <iostream>
#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0x3200 // 全局domain宏,标识业务领域
#define LOG_TAG "MY_TAG111"  // 全局tag宏,标识模块日志tag
napi_env g_env;
napi_callback_info g_info;
napi_ref g_callback_ref;


void MyHiLog(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *msg) {
  if (g_env == nullptr || g_callback_ref == nullptr) {
    std::cerr << "Environment or callback reference is null" << std::endl;
    return;
  }

  napi_handle_scope scope;
  napi_status status = napi_open_handle_scope(g_env, &scope);
  if (status != napi_ok) {
    std::cerr << "Failed to open handle scope" << std::endl;
    return;
  }

  napi_value global;
  status = napi_get_global(g_env, &global);
  if (status != napi_ok) {
    std::cerr << "Failed to get global object" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  napi_value callback;
  status = napi_get_reference_value(g_env, g_callback_ref, &callback);
  if (status != napi_ok) {
    std::cerr << "Failed to get callback reference" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  napi_value argv[5];
  status = napi_create_int32(g_env, type, &argv[0]);
  if (status != napi_ok) {
    std::cerr << "Failed to create int32 for type" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  status = napi_create_int32(g_env, level, &argv[1]);
  if (status != napi_ok) {
    std::cerr << "Failed to create int32 for level" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  status = napi_create_uint32(g_env, domain, &argv[2]);
  if (status != napi_ok) {
    std::cerr << "Failed to create uint32 for domain" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  status = napi_create_string_utf8(g_env, tag, NAPI_AUTO_LENGTH, &argv[3]);
  if (status != napi_ok) {
    std::cerr << "Failed to create string for tag" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  status = napi_create_string_utf8(g_env, msg, NAPI_AUTO_LENGTH, &argv[4]);
  if (status != napi_ok) {
    std::cerr << "Failed to create string for msg" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }
  napi_valuetype valuetype;
  status = napi_typeof(g_env, callback, &valuetype);
  if (status != napi_ok || valuetype != napi_function) {
    std::cerr << "Callback is not a function" << std::endl;
    napi_close_handle_scope(g_env, scope);
    return;
  }

  napi_value result;
  std::cerr << "Calling JavaScript function..." << std::endl;
  status = napi_call_function(g_env, global, callback, 5, argv, &result);
  if (status != napi_ok) {
    std::cerr << "Failed to call function" << std::endl;
  } else {
    std::cerr << "Function called successfully" << std::endl;
  }
  napi_close_handle_scope(g_env, scope);
}
static napi_value registerLog(napi_env env, napi_callback_info info) {
  size_t argc = 1;
  napi_value args[1];
  napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  if (status != napi_ok) {
    napi_throw_error(env, nullptr, "Failed to get callback info");
    return nullptr;
  }
  if (argc < 1) {
    napi_throw_error(env, nullptr, "Callback function expected");
    return nullptr;
  }
  status = napi_create_reference(env, args[0], 1, &g_callback_ref);
  if (status != napi_ok) {
    napi_throw_error(env, nullptr, "Failed to create reference");
    return nullptr;
  }
  g_env = env;
  // 注册回调接口
  OH_LOG_SetCallback(MyHiLog);
  return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
  napi_property_descriptor desc[] = {
    {"registerLog", nullptr, registerLog, nullptr, nullptr, nullptr, napi_default, nullptr}
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END

static napi_module demoModule = {
  .nm_version = 1,
  .nm_flags = 0,
  .nm_filename = nullptr,
  .nm_register_func = Init,
  .nm_modname = "entry",
  .nm_priv = ((void *)0),
  .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.

报错信息:

Reason:Signal:SIGSEGV(SEGV_ACCERR)@0x0000005bb4261ff0  current thread stack low address = 0x0000005bb4262000, probably caused by stack-buffer-overflow
  • 1.
HarmonyOS
2025-01-10 07:44:56
552浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

MyHiLog中要判断type为非0的时候直接return,LogType为0表示为当前进程的日志。

分享
微博
QQ
微信
回复
2025-01-10 10:46:19


相关问题
在C++时,如何阻塞TS主线程?
1491浏览 • 1回复 待解决
HarmonyOS 运行HmosWorld时报错
1057浏览 • 1回复 待解决
HarmonyOS 应用发布时报错
732浏览 • 1回复 待解决
HarmonyOS 事件
1011浏览 • 1回复 待解决
HarmonyOS 调用相册函数时报错
796浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
1208浏览 • 1回复 待解决
HarmonyOS native C++ 层传递buffer 到ArkTS 层
1093浏览 • 1回复 待解决
HarmonyOS 启动rn项目时报错
1020浏览 • 1回复 待解决
拉起UIAbility时报错16000050
3398浏览 • 1回复 待解决
HarmonyOS Watch没有
769浏览 • 1回复 待解决
HarmonyOS onNewWant未
612浏览 • 1回复 待解决
HarmonyOS Web组件
1169浏览 • 1回复 待解决
HarmonyOS asset运行add方法时报错
1243浏览 • 1回复 待解决
HarmonyOS 获取推送token时报错1000900010
1167浏览 • 1回复 待解决
HarmonyOS 使用flutter创建packages时报错
836浏览 • 1回复 待解决