Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/proxy-wasm/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ class ContextBase : public RootInterface,
std::string_view /* details */) override {
return unimplemented();
}

// Inject Data
WasmResult injectEncodedDataToFilterChain(std::string_view /* body_text */, bool /* end_stream */) override {
return unimplemented();
}

// Get all enpoint metrics of upstream cluster
WasmResult getUpstreamHosts(StringPairs * /* result */) override {
return unimplemented();
}

// Override upstream host and bypass lb policy
WasmResult setUpstreamOverrideHost(std::string_view /* address */) override {
return unimplemented();
}
Comment on lines +358 to +370
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增方法未实现可能导致运行时错误

🟠 Critical | 🐞 Bugs

📋 问题详情

新增的三个方法(injectEncodedDataToFilterChain/getUpstreamHosts/setUpstreamOverrideHost)均直接返回unimplemented(),未提供实际逻辑。这会导致调用这些接口时返回未实现错误,影响功能完整性。

💡 解决方案

应确保所有新增接口提供基础实现逻辑,至少返回WasmResult::Ok或具体业务逻辑。建议:

- return unimplemented();
+ return WasmResult::Ok; // 或具体实现

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌


void clearRouteCache() override { unimplemented(); }
void failStream(WasmStreamType stream_type) override { closeStream(stream_type); }

Expand Down
9 changes: 9 additions & 0 deletions include/proxy-wasm/context_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace proxy_wasm {

using Pairs = std::vector<std::pair<std::string_view, std::string_view>>;
using PairsWithStringValues = std::vector<std::pair<std::string_view, std::string>>;
using StringPairs = std::vector<std::pair<std::string, std::string>>;
using TimerToken = uint32_t;
using HttpCallToken = uint32_t;
using GrpcToken = uint32_t;
Expand Down Expand Up @@ -255,6 +256,14 @@ struct HttpInterface {

// Call just before the Context is deleted. See RootInterface.
virtual void onDelete() = 0;

// Inject encoded data to filter chain
virtual WasmResult injectEncodedDataToFilterChain(std::string_view /* body_text */, bool /* end_stream */) = 0;

// Get all enpoint metrics of upstream cluster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释拼写错误影响可读性

🟢 Minor | 🧹 Code Smells

📋 问题详情

getUpstreamHosts注释中'enpoint'应为'endpoint',拼写错误影响文档准确性。

💡 解决方案

修正拼写:

-enpoint
+endpoint
🔧 建议代码

‼️AI 生成代码 - 请在应用前检查逻辑、规范并测试

Suggested change
// Get all enpoint metrics of upstream cluster
// Get all endpoint metrics of upstream cluster

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

virtual WasmResult getUpstreamHosts(StringPairs * /* result */) = 0;
// Override upstream host and bypass lb policy
virtual WasmResult setUpstreamOverrideHost(std::string_view /* address */) = 0;
};

/**
Expand Down
7 changes: 6 additions & 1 deletion include/proxy-wasm/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Word send_local_response(Word response_code, Word response_code_details_ptr,
Word response_code_details_size, Word body_ptr, Word body_size,
Word additional_response_header_pairs_ptr,
Word additional_response_header_pairs_size, Word grpc_status);
Word inject_encoded_data_to_filter_chain(Word body_ptr, Word body_size, Word end_stream);
Word set_upstream_override_host(Word address_ptr, Word address_size);
Word get_upstream_hosts(Word ptr, Word size);
Word clear_route_cache();
Word get_shared_data(Word key_ptr, Word key_size, Word value_ptr_ptr, Word value_size_ptr,
Word cas_ptr);
Expand Down Expand Up @@ -198,7 +201,9 @@ Word wasi_unstable_path_filestat_get(Word fd, Word flags, Word path, Word path_l
_f(increment_metric) _f(record_metric) _f(get_metric) \
_f(set_effective_context) _f(done) \
_f(call_foreign_function) _f(redis_init) \
_f(redis_call)
_f(redis_call) _f(get_upstream_hosts) \
_f(inject_encoded_data_to_filter_chain) \
_f(set_upstream_override_host)

#define FOR_ALL_HOST_FUNCTIONS_ABI_SPECIFIC(_f) \
_f(get_configuration) _f(continue_request) _f(continue_response) _f(clear_route_cache) \
Expand Down
13 changes: 13 additions & 0 deletions include/proxy-wasm/wasm_api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ proxy_send_local_response(uint32_t response_code, const char *response_code_deta
WR(body_ptr), WS(body_size), WR(additional_response_header_pairs_ptr),
WS(additional_response_header_pairs_size), WS(grpc_status)));
}
inline WasmResult
proxy_inject_encoded_data_to_filter_chain(const char *body_ptr, size_t body_size, bool end_stream) {
return wordToWasmResult(exports::inject_encoded_data_to_filter_chain(
WR(body_ptr), WS(body_size), WS(end_stream)));
}
inline WasmResult
proxy_set_upstream_override_host(const char *address_ptr, size_t address_size) {
return wordToWasmResult(exports::set_upstream_override_host(WR(address_ptr), WS(address_size)));
}
inline WasmResult
proxy_get_upstream_hosts(const char **ptr, size_t *size) {
return wordToWasmResult(exports::get_upstream_hosts(WR(ptr), WR(size)));
}

inline WasmResult proxy_clear_route_cache() {
return wordToWasmResult(exports::clear_route_cache());
Expand Down
52 changes: 52 additions & 0 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,58 @@ Word send_local_response(Word response_code, Word response_code_details_ptr,
return WasmResult::Ok;
}

Word inject_encoded_data_to_filter_chain(Word body_ptr, Word body_size, Word end_stream) {
auto *context = contextOrEffectiveContext();
auto body = context->wasmVm()->getMemory(body_ptr, body_size);
if (!body) {
return WasmResult::InvalidMemoryAccess;
}
context->injectEncodedDataToFilterChain(body.value(), end_stream != 0U);
return WasmResult::Ok;
}

Word get_upstream_hosts(Word ptr_ptr, Word size_ptr) {
auto *context = contextOrEffectiveContext();
StringPairs pairs;
auto result = context->getUpstreamHosts(&pairs);
if (result != WasmResult::Ok) {
return result;
}
if (pairs.empty()) {
if (!context->wasm()->copyToPointerSize("", ptr_ptr, size_ptr)) {
return WasmResult::InvalidMemoryAccess;
}
return WasmResult::Ok;
}
Comment on lines +172 to +184
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_upstream_hosts空列表处理逻辑错误

🟡 Major | 🐞 Bugs

📋 问题详情

当pairs为空时,调用copyToPointerSize写入空字符串,但size_ptr应设为0而非写入内容,可能导致内存访问错误。

💡 解决方案

修改为空列表处理逻辑:

-copyToPointerSize("", ptr_ptr, size_ptr)
+setWord(size_ptr, 0)
🔧 建议代码

‼️AI 生成代码 - 请在应用前检查逻辑、规范并测试

Suggested change
Word get_upstream_hosts(Word ptr_ptr, Word size_ptr) {
auto *context = contextOrEffectiveContext();
StringPairs pairs;
auto result = context->getUpstreamHosts(&pairs);
if (result != WasmResult::Ok) {
return result;
}
if (pairs.empty()) {
if (!context->wasm()->copyToPointerSize("", ptr_ptr, size_ptr)) {
return WasmResult::InvalidMemoryAccess;
}
return WasmResult::Ok;
}
if (pairs.empty()) {
if (!context->wasmVm()->setWord(size_ptr, 0)) {
return WasmResult::InvalidMemoryAccess;
}
return WasmResult::Ok;
}

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

uint64_t size = PairsUtil::pairsSize(pairs);
uint64_t ptr = 0;
char *buffer = static_cast<char *>(context->wasm()->allocMemory(size, &ptr));
if (buffer == nullptr) {
return WasmResult::InvalidMemoryAccess;
Comment on lines +186 to +189
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内存分配未处理释放逻辑

🟡 Major | 🧹 Code Smells

📋 问题详情

allocMemory分配的内存未在异常路径释放,可能导致内存泄漏。需添加错误处理时的释放逻辑。

💡 解决方案

添加释放保护:

+  // 在所有return前确保释放
+  if (!PairsUtil::marshalPairs(pairs, buffer, size)) {
+    context->wasm()->freeMemory(buffer);
+    return WasmResult::InvalidMemoryAccess;
+  }

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

}
if (!PairsUtil::marshalPairs(pairs, buffer, size)) {
return WasmResult::InvalidMemoryAccess;
}
if (!context->wasmVm()->setWord(ptr_ptr, Word(ptr))) {
return WasmResult::InvalidMemoryAccess;
}
if (!context->wasmVm()->setWord(size_ptr, Word(size))) {
return WasmResult::InvalidMemoryAccess;
}
return WasmResult::Ok;
}

Word set_upstream_override_host(Word address_ptr, Word address_size) {
auto *context = contextOrEffectiveContext();
auto address = context->wasmVm()->getMemory(address_ptr, address_size);
if (!address) {
return WasmResult::InvalidMemoryAccess;
}
context->setUpstreamOverrideHost(address.value());
return WasmResult::Ok;
}


Word clear_route_cache() {
auto *context = contextOrEffectiveContext();
context->clearRouteCache();
Expand Down
3 changes: 3 additions & 0 deletions src/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ void WasmBase::registerCallbacks() {
_REGISTER_PROXY(get_log_level);
_REGISTER_PROXY(redis_init);
_REGISTER_PROXY(redis_call);
_REGISTER_PROXY(inject_encoded_data_to_filter_chain);
_REGISTER_PROXY(set_upstream_override_host);
_REGISTER_PROXY(get_upstream_hosts);
}
#undef _REGISTER_PROXY

Expand Down
Loading