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

datasource: Allow setting smaller static strings #2832

Merged

Conversation

mauriciovasquezbernal
Copy link
Member

There are cases when an operator needs to change the value of a static string (coming from ebpf for instance). This commit ipt improves the logic to allow the operator calling the PutString() method with a string that is smaller than the buffer size.


I found this issue / limitation when trying to update the name field in wasm in #2823. That PR implements a workaround for it, but IMO it needs to be handled directly in the accessor.

@flyth any opinions on this one?

@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/allow-setting-small-statis-strings branch 2 times, most recently from d5c7b42 to 730edf2 Compare May 10, 2024 21:13
@flyth
Copy link
Member

flyth commented May 14, 2024

Hmm...

I think we need to add errors to the Put* functions to do these kind of things, otherwise it could lead to (from the developer) unexpected behavior (e.g. some Puts would work, some wouldn't (if the string is too long)). To be fair, it's already the case if you'd try to write a string with the exact length of the field vs. another length right now.

IMHO this isn't best practice in the first place (editing a statically sized string), but if we allow it, we should at least make sure it's reported if it didn't succeed.

Base automatically changed from mauricio/fixes-for-fields to main May 14, 2024 13:25
@mauriciovasquezbernal
Copy link
Member Author

I think we need to add errors to the Put* functions to do these kind of things, otherwise it could lead to (from the developer) unexpected behavior (e.g. some Puts would work, some wouldn't (if the string is too long)). To be fair, it's already the case if you'd try to write a string with the exact length of the field vs. another length right now.

I totally agree. What do you think about the "Get()" methods? Should we return errors instead of default value when something goes wrong?

IMHO this isn't best practice in the first place (editing a statically sized string), but if we allow it, we should at least make sure it's reported if it didn't succeed.

I don't see any reason why we should block this. I feel there are use cases where this is valuable.

@flyth
Copy link
Member

flyth commented May 14, 2024

I think we need to add errors to the Put* functions to do these kind of things, otherwise it could lead to (from the developer) unexpected behavior (e.g. some Puts would work, some wouldn't (if the string is too long)). To be fair, it's already the case if you'd try to write a string with the exact length of the field vs. another length right now.

I totally agree. What do you think about the "Get()" methods? Should we return errors instead of default value when something goes wrong?

Yeah, let's do it - better now than later. I was a bit worried about returning two values, because it might not be compatible with all languages that would compile to WASM - but I think it's better to have a clean implementation on the host and let the guest APIs worry about getting it right there.

IMHO this isn't best practice in the first place (editing a statically sized string), but if we allow it, we should at least make sure it's reported if it didn't succeed.

I don't see any reason why we should block this. I feel there are use cases where this is valuable.

I agree, we shouldn't block this.

@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/allow-setting-small-statis-strings branch from 730edf2 to 8c0db4e Compare May 14, 2024 23:28
@mauriciovasquezbernal
Copy link
Member Author

Updated the PR to return errors. The code is not as clean as before, but we need it.

@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/allow-setting-small-statis-strings branch from 8c0db4e to bb9e21d Compare May 14, 2024 23:35
Copy link
Member

@flyth flyth left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the errors - it's indeed not as clean looking as before, but - well - I guess cleaner working. 🤷

pkg/datasource/accessors.go Show resolved Hide resolved
pkg/datasource/formatters/json/json.go Show resolved Hide resolved
Comment on lines 438 to 439
if len(b) < 1 {
return
return ErrFieldTooShort
Copy link
Member

Choose a reason for hiding this comment

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

Do you think there are cases when it makes sense to read a Uint8 from something that's larger than 1 byte without specifying an additional offset? Otherwise maybe it's cleaner to just return ErrInvalidFieldLength and check for len(b) != 1. Also for the other types.

(Usually I'm not into restricting users from doing weird stuff, but here I don't know...)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added that check as well. At this point, I'm wondering if we should just check the type of the field instead... (but I'll hold that discussion for a bit).

@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/allow-setting-small-statis-strings branch from bb9e21d to 154c61c Compare May 15, 2024 13:44
Update all functions to also return a value, this
helps to have a better error handling.

Signed-off-by: Mauricio Vásquez <mauriciov@microsoft.com>
There are cases when an operator needs to change the value of a static
string (coming from ebpf for instance). This commit ipt improves the
logic to allow the operator calling the PutString() method with a
string that is smaller than the buffer size.

Signed-off-by: Mauricio Vásquez <mauriciov@microsoft.com>
@mauriciovasquezbernal mauriciovasquezbernal force-pushed the mauricio/allow-setting-small-statis-strings branch from 154c61c to 95117b3 Compare May 16, 2024 12:53
Copy link
Member

@flyth flyth left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for cleaning this up!

Comment on lines +27 to +30
type InvalidFieldLengthErr struct {
Expected int
Actual int
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

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 took me quite few iterations to reach this! Glad to see you liked it!

@mauriciovasquezbernal mauriciovasquezbernal merged commit b9cea9e into main May 16, 2024
59 of 60 checks passed
@mauriciovasquezbernal mauriciovasquezbernal deleted the mauricio/allow-setting-small-statis-strings branch May 16, 2024 18:37
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.

None yet

2 participants