Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ffi): add FFI interface to get operators along with fields while performing expressions validation #263

Merged
merged 22 commits into from
Nov 20, 2024

Conversation

Oyami-Srk
Copy link
Member

@Oyami-Srk Oyami-Srk commented Sep 24, 2024

This interface allows the caller to validate an expression string with schema and get its operators/fields without needing a Router instance.

KAG-5013

@Oyami-Srk Oyami-Srk changed the title [DO NOT MERGE] feat(expressions-compatibility) add FFI interface to get operators along with fields [WIP] feat(expressions-compatibility) add FFI interface to get operators along with fields Sep 25, 2024
@Oyami-Srk Oyami-Srk force-pushed the KAG-5013 branch 2 times, most recently from cf1954c to a009d30 Compare September 25, 2024 02:46
src/ffi.rs Outdated Show resolved Hide resolved
@Oyami-Srk Oyami-Srk changed the title [WIP] feat(expressions-compatibility) add FFI interface to get operators along with fields feat(expressions-compatibility) add FFI interface to get operators along with fields Sep 29, 2024
@Oyami-Srk Oyami-Srk marked this pull request as ready for review September 29, 2024 02:39
src/ffi.rs Outdated Show resolved Hide resolved
src/ffi.rs Outdated Show resolved Hide resolved
src/ffi.rs Outdated Show resolved Hide resolved
src/semantics.rs Outdated Show resolved Hide resolved
src/semantics.rs Outdated Show resolved Hide resolved
src/ffi.rs Outdated
#[cfg(feature = "expr_validation")]
#[no_mangle]
pub unsafe extern "C" fn expression_validate(
atc: *const u8,
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason to use u8 for strings here, and i8 elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

According to the document, ffi::c_char is defined as i8. However, the document also mentioned that On modern architectures this type will always be either i8 or u8.

Choosing u8 here is just to keep consistency with Rust internal bytes represent like String::as_bytes.

P.S. I saw all the FFI interfaces in this project have no consistent way to represent c_char. E.g. fields is u8 in router_get_fields and i8 in context_add_value.

Copy link
Member

Choose a reason for hiding this comment

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

@Oyami-Srk In general, we tries to use u8 for char whenever possible, except when dealing with char provided by LuaJIT.

This is because LuaJIT converts string to const char *, and this is a signed value. If we accept u8 LuaJIT will complain when attempting to convert a string.

src/ffi.rs Outdated Show resolved Hide resolved
@Oyami-Srk Oyami-Srk force-pushed the KAG-5013 branch 2 times, most recently from 2820904 to 2490bba Compare October 16, 2024 02:48
@Oyami-Srk
Copy link
Member Author

Rebased with the latest main branch. Nothing else has been changed except Cargo.toml, Cargo.lock and src/semantics.rs since 2 weeks ago.

Oyami-Srk and others added 10 commits October 17, 2024 10:36
…ong with fields

The FFI interface introducing by this commit has corresponding Go wrapping:
```
func ValidateExpression(atc string, s *Schema) (bool, []string, int64) {
	atcC := unsafe.Pointer(C.CString(atc))
	defer C.free(atcC)

	errLen := C.ulong(1024)
	errBuf := [1024]C.uchar{}

	expr := C.expression_validate((*C.uchar)(atcC), s.s, &errBuf[0], &errLen)
	defer C.expression_validate_free_result(expr)

	if expr == nil {
		fmt.Println("Error: ", string(errBuf[:errLen]))
		return false, nil, 0
	}

	validate := bool(expr.validate)
	operators := int64(expr.operators)
	flds := make([]string, expr.fields_total)
	flds_slice := unsafe.Slice(expr.fields, expr.fields_total)

	for i := range flds {
		flds[i] = C.GoString((*C.char)(unsafe.Pointer(flds_slice[i])))
	}

	return validate, flds, operators
}
```
…Rust

An example go binding for the FFI interface introduced by this commit:
```Go
func ValidateExpression(atc string, s *Schema) (bool, []string, uint64, error) {
	atcC := unsafe.Pointer(C.CString(atc))
	defer C.free(atcC)

	errLen := C.ulong(1024)
	errBuf := [1024]C.uchar{}

	fieldsLen := C.ulong(1024)
	fieldsBuf := [1024]C.uchar{}
	fieldsTotal := C.ulong(0)
	operatorsC := C.uint64_t(0)

	result := C.expression_validate((*C.uchar)(atcC), s.s, &fieldsBuf[0], &fieldsLen, &fieldsTotal, &operatorsC, &errBuf[0], &errLen)

	if bool(result) == false {
		return false, nil, 0, fmt.Errorf(string(errBuf[:errLen]))
	}

	operators := uint64(operatorsC)

	flds := make([]string, uintptr(fieldsTotal))
	p := 0
	for i := range flds {
		flds[i] = C.GoString((*C.char)(unsafe.Pointer(&fieldsBuf[p])))
		p += len(flds[i]) + 1
	}

	return true, flds, operators, nil
}
```
@Oyami-Srk
Copy link
Member Author

@javierguerragiraldez Could you please approve this if there's no other concern in this Rust implementation?

Cargo.toml Outdated Show resolved Hide resolved
src/ast.rs Outdated Show resolved Hide resolved
src/ast.rs Outdated Show resolved Hide resolved
src/ffi.rs Outdated Show resolved Hide resolved
src/ffi.rs Outdated Show resolved Hide resolved
src/ffi.rs Outdated
/// - ATC_ROUTER_EXPRESSION_VALIDATE_FAILED(1) if validation is failed.
/// - ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL(2) if the provided fields buffer is not enough.
///
/// If `fields_buf` is null and `fields_len` or `fields_total` is non-null, it will write
Copy link
Member

Choose a reason for hiding this comment

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

I think we should just be optimistic here and always ask the user to pass in a buffer, 99% of the time reallocate will not be necessary, and we can avoid having to parse the expression twice which could be expensive (especially with Regexes).

Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed the ability to acquire the buffer length. Callers must pass a valid buffer if the calling is for getting the fields in use.
But I keep the fields_buf null-able because this interface is expression_validate and callers may not always want fields. This will allow us to do more refactoring on the Kong side with this interface. Like getting rid of validating expressions by inserting and then deleting them.

…stead.

This commit contains two parts:
1. Moving everything guarded by feature flag `expr_validation` to `ffi.rs`.
2. Remove feature flag `expr_validation`.
3. Remove public interfaces `get_field` and `get_operator` for `Predicate`.

No internal logic has been changed.
remove the redundant error message when buffer is too small.
remove the ability to fetch fields count and total length without a valid `field_buf`. Must pass a valid `field_buf` if calling for getting fields.
@Oyami-Srk
Copy link
Member Author

Did a little improvement:

  1. Break ffi.rs into module.
  2. Move everything guarded by the feature flag expr_validation to ffi:expression.
  3. Remove feature flag expr_validation.
  4. Remove public interfaces get_field and get_operator for Predicate.
  5. Remove the redundant error message when the buffer is too small.
  6. Remove the ability to fetch field count and total length without a valid field_buf. (The field_buf is still null-able to allow doing validation only.)

@dndx Could you review it when you have a moment?
@javierguerragiraldez Could you please re-generate the C header and update cgo compile flag as the feature flag expr_validation is removed, and only need feature ffi.

@Oyami-Srk Oyami-Srk requested a review from dndx October 30, 2024 09:53
@javierguerragiraldez
Copy link
Contributor

@javierguerragiraldez Could you please re-generate the C header and update [cgo compile flag] as the feature flag expr_validation is removed, and only need feature ffi.

👍 done

src/ffi/expression.rs Outdated Show resolved Hide resolved
src/ffi/expression.rs Outdated Show resolved Hide resolved
src/ffi/expression.rs Outdated Show resolved Hide resolved
src/ffi/expression.rs Outdated Show resolved Hide resolved
src/ffi/expression.rs Outdated Show resolved Hide resolved
src/ffi/expression.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

LGTM, just a small thing.

src/ffi/expression.rs Show resolved Hide resolved
src/ffi/context.rs Outdated Show resolved Hide resolved
Co-authored-by: Datong Sun <[email protected]>
@dndx dndx changed the title feat(expressions-compatibility) add FFI interface to get operators along with fields feat(ffi) add FFI interface to get operators along with fields while performing expressions validation Nov 20, 2024
@dndx dndx changed the title feat(ffi) add FFI interface to get operators along with fields while performing expressions validation feat(ffi): add FFI interface to get operators along with fields while performing expressions validation Nov 20, 2024
@dndx dndx merged commit 6f12fae into main Nov 20, 2024
15 checks passed
@dndx dndx deleted the KAG-5013 branch November 20, 2024 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants