记一次apisix编译报错:size of C type is unknown or too large at line 39, context: init_worker_by_lua*

博客作者遇到APIsix在启动时加载插件失败,错误涉及hmac.lua。经过分析,发现错误可能与OpenSSL版本和内存不足有关。检查CPU和内存使用情况后排除了资源不足的可能性。进一步研究发现,错误插件之间存在共性,都依赖hmac.lua。通过查看hmac.lua的源码和GitHub上的相关issue,确定问题可能由OpenSSL版本不兼容引起。最终,更新hmac.lua文件解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

apisix(2.13版本)在启动时会报插件加载失败的错误,且每次加载失败的插件都不太一样;
在这里插入图片描述

定位思路

检查环境

看报错信息,感觉像是C类型未知,或者C类型过大

但是检查代码本身感觉没问题,hmac.lua代码如下:


local str_util = require "resty.string"
local to_hex = str_util.to_hex
local ffi = require "ffi"
local ffi_new = ffi.new
local ffi_str = ffi.string
local ffi_gc = ffi.gc
local ffi_typeof = ffi.typeof
local C = ffi.C
local setmetatable = setmetatable
local error = error


local _M = { _VERSION = '0.04' }

local mt = { __index = _M }


ffi.cdef[[
typedef struct engine_st ENGINE;
typedef struct evp_pkey_ctx_st EVP_PKEY_CTX;
typedef struct evp_md_ctx_st EVP_MD_CTX;
typedef struct evp_md_st EVP_MD;
typedef struct hmac_ctx_st HMAC_CTX;

//OpenSSL 1.0
void HMAC_CTX_init(HMAC_CTX *ctx);
void HMAC_CTX_cleanup(HMAC_CTX *ctx);

//OpenSSL 1.1
HMAC_CTX *HMAC_CTX_new(void);
void HMAC_CTX_free(HMAC_CTX *ctx);

int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md, ENGINE *impl);
int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len);
int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);

const EVP_MD *EVP_md5(void);
const EVP_MD *EVP_sha1(void);
const EVP_MD *EVP_sha256(void);
const EVP_MD *EVP_sha512(void);
]]

2、怀疑是不是内存不足导致的

查看CPU内存使用情况:

在这里插入图片描述

查看磁盘使用情况:

在这里插入图片描述

如上所示,CPU内存磁盘的使用率都不高,资源肯定够用的

检查报错插件的共同点

1、测试多次发现,报错的插件虽然会变,但是只在如下几个插件之间变化:

  • hmac-auth
  • jwt-auth
  • rocketmq-logger
  • google-cloud-logging
  • aws-lambda

2、将以上插件暂时停用,发现apisix启动不再报错;

  • 停用以上插件,修改config-default.yaml文件,将以上插件注释掉;
  • 重启apisix,启动不再报错;

3、分析以上插件的共同点
查看以上插件的源码,发现最终都依赖了hmac.lua文件,还是要定位hmac.lua文件本身;

local hmac       = require("resty.hmac")

定位hmac.lua

定位到这里,知道是hmac.lua文件有问题,但是又不知道哪里出了问题。只能查看github,地址如下:
https://github.com/jkeys089/lua-resty-hmac

1、查看hmac.lua文件修改记录,发现在0.04版本之后,只有一次代码发生大的变动:https://github.com/jkeys089/lua-resty-hmac/pull/20/files

在这里插入图片描述

2、检查本次PR解决的问题,发现一个issue,解决的问题就是我报的错误:
https://github.com/jkeys089/lua-resty-hmac/issues/19

在这里插入图片描述

3、查看报错原因

this appears to be an issue with 1) when using openssl 1.0.x and 2) use with other libraries that explictly typedef EVP_MD_CTX to env_md_ctx_st and they are loaded before this library (from what i can see it’s the case for evp.lua from lua-resty-jwt and aes.lua from lua-resty-string)
一个可能是openssl版本问题;还有个可能是hmac.lua使用的EVP_MD_CTX结构体,在该库之前加载了;

解决方案

将hmac.lua最新版本的代码拷贝到/home/apisix-2.13.1/deps/share/lua/5.1/resty/目录下,替换掉老的hmac.lua,重新启动apisix,问题解决;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值