forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Takeshi Yoneda <[email protected]>
- Loading branch information
Showing
18 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
exports_files(["abi.h"]) # Exported for tests. | ||
|
||
go_library( | ||
name = "envoy_dynamic_modules_go_sdk", | ||
srcs = glob(["*.go"]) + ["abi.h"], | ||
cgo = True, | ||
importpath = "github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build cgo | ||
|
||
package gosdk | ||
|
||
/* | ||
#define ENVOY_DYNAMIC_MODULE_GO_SDK 1 | ||
#include "abi.h" | ||
*/ | ||
import "C" | ||
import "unsafe" | ||
|
||
var abiVersion = append([]byte(string("7bf4504e9874e385f15c4a835da3c4dfe9480a3d7262d46b18740d7192866649")), 0) | ||
|
||
//export envoy_dynamic_module_on_program_init | ||
func envoy_dynamic_module_on_program_init() C.envoy_dynamic_module_type_abi_version { | ||
if OnProgramInit() { | ||
return C.envoy_dynamic_module_type_abi_version(uintptr(unsafe.Pointer(&abiVersion[0]))) | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#pragma once | ||
|
||
// NOLINT(namespace-envoy) | ||
|
||
// This is a pure C header file that defines the ABI of the core of dynamic modules used by Envoy. | ||
// | ||
// This must not contain any dependencies besides standard library since it is not only used by | ||
// Envoy itself but also by dynamic module SDKs written in non-C++ languages. | ||
// | ||
// Currently, compatibility is only guaranteed by an exact version match between the Envoy | ||
// codebase and the dynamic module SDKs. In the future, after the ABI is stabilized, we will revisit | ||
// this restriction and hopefully provide a wider compatibility guarantee. Until then, Envoy | ||
// checks the hash of the ABI header files to ensure that the dynamic modules are built against the | ||
// same version of the ABI. | ||
|
||
#ifdef __cplusplus | ||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
extern "C" { | ||
#else | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#endif | ||
|
||
#ifdef ENVOY_DYNAMIC_MODULE_GO_SDK | ||
// Use uintptr_t to represent a raw pointer in C for simplicity in Go bindings. | ||
typedef uintptr_t envoy_dynamic_module_raw_pointer; | ||
#else | ||
typedef void* envoy_dynamic_module_raw_pointer; // NOLINT(modernize-use-using) | ||
#endif | ||
|
||
// ----------------------------------------------------------------------------- | ||
// ---------------------------------- Types ------------------------------------ | ||
// ----------------------------------------------------------------------------- | ||
// | ||
// Types used in the ABI. The name of a type must be prefixed with "envoy_dynamic_module_type_". | ||
|
||
/** | ||
* envoy_dynamic_module_type_abi_version represents a null-terminated string that contains the ABI | ||
* version of the dynamic module. This is used to ensure that the dynamic module is built against | ||
* the compatible version of the ABI. | ||
*/ | ||
typedef envoy_dynamic_module_raw_pointer // NOLINT(modernize-use-using) | ||
envoy_dynamic_module_type_abi_version; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// ------------------------------- Event Hooks --------------------------------- | ||
// ----------------------------------------------------------------------------- | ||
// | ||
// Event hooks are functions that are called by Envoy in response to certain events. | ||
// The module must implement and export these functions in the dynamic module object file. | ||
// | ||
// Each event hook is defined as a function prototype. The symbol must be prefixed with | ||
// "envoy_dynamic_module_on_". | ||
|
||
/** | ||
* envoy_dynamic_module_on_program_init is called by the main thread exactly when the module is | ||
* loaded. The function returns the ABI version of the dynamic module. If null is returned, the | ||
* module will be unloaded immediately. | ||
* | ||
* For Envoy, the return value will be used to check the compatibility of the dynamic module. | ||
* | ||
* For dynamic modules, this is useful when they need to perform some process-wide | ||
* initialization or check if the module is compatible with the platform, such as CPU features. | ||
* Note that initialization routines of a dynamic module can also be performed without this function | ||
* through constructor functions in an object file. However, normal constructors cannot be used | ||
* to check compatibility and gracefully fail the initialization because there is no way to | ||
* report an error to Envoy. | ||
* | ||
* @return envoy_dynamic_module_type_abi_version is the ABI version of the dynamic module. Null | ||
* means the error and the module will be unloaded immediately. | ||
*/ | ||
envoy_dynamic_module_type_abi_version envoy_dynamic_module_on_program_init(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go | ||
|
||
go 1.22.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package gosdk | ||
|
||
var OnProgramInit func() bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("//test/extensions/dynamic_modules/test_data/go:test_data.bzl", "test_program") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
package(default_visibility = ["//test/extensions/dynamic_modules:__pkg__"]) | ||
|
||
test_program(name = "no_op") | ||
|
||
test_program(name = "no_program_init") | ||
|
||
test_program(name = "program_init_fail") | ||
|
||
test_program(name = "abi_version_mismatch") |
12 changes: 12 additions & 0 deletions
12
test/extensions/dynamic_modules/test_data/go/abi_version_mismatch.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import "C" | ||
import "unsafe" | ||
|
||
func main() {} // c-shared must define the empty main function. | ||
|
||
//export envoy_dynamic_module_on_program_init | ||
func envoy_dynamic_module_on_program_init() uintptr { | ||
version := append([]byte("invalid-version-hash"), 0) | ||
return uintptr(unsafe.Pointer(&version[0])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module main | ||
|
||
go 1.22.5 | ||
|
||
require github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go v0.0.0 | ||
|
||
replace ( | ||
github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go => ../../../../../source/extensions/dynamic_modules/sdk/go/ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
gosdk "github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go" | ||
) | ||
|
||
func main() {} // c-shared must define the empty main function. | ||
|
||
func init() { | ||
gosdk.OnProgramInit = func() bool { return true } | ||
} |
3 changes: 3 additions & 0 deletions
3
test/extensions/dynamic_modules/test_data/go/no_program_init.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
func main() {} // c-shared must define the empty main function. |
11 changes: 11 additions & 0 deletions
11
test/extensions/dynamic_modules/test_data/go/program_init_fail.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
gosdk "github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go" | ||
) | ||
|
||
func main() {} // c-shared must define the empty main function. | ||
|
||
func init() { | ||
gosdk.OnProgramInit = func() bool { return false } | ||
} |
16 changes: 16 additions & 0 deletions
16
test/extensions/dynamic_modules/test_data/go/test_data.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_binary") | ||
|
||
# This declares a go_binary target that is used to build a shared Go library. | ||
# name + ".go" is the source file that is compiled to create the shared library. | ||
def test_program(name): | ||
go_binary( | ||
name = name, | ||
srcs = [name + ".go"], | ||
out = "lib" + name + ".so", | ||
cgo = True, | ||
importpath = "github.com/envoyproxy/envoy/test/extensions/dynmic_modules/sdk/go", | ||
linkmode = "c-shared", | ||
deps = [ | ||
"//source/extensions/dynamic_modules/sdk/go:envoy_dynamic_modules_go_sdk", | ||
], | ||
) |