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

fix(js): free native strings #238

Merged
merged 3 commits into from
Sep 5, 2023
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
29 changes: 18 additions & 11 deletions wrappers/javascript/anoncreds-nodejs/src/NodeJSAnoncreds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ import {
} from './ffi'
import { getNativeAnoncreds } from './library'

function handleReturnPointer<Return>(returnValue: Buffer): Return {
function handleReturnPointer<Return>(returnValue: Buffer, cleanupCallback?: (buffer: Buffer) => void): Return {
if (returnValue.address() === 0) {
throw AnoncredsError.customError({ message: 'Unexpected null pointer' })
}

return returnValue.deref() as Return
const ret = returnValue.deref() as Return
if (cleanupCallback) cleanupCallback(returnValue)

return ret
}

export class NodeJSAnoncreds implements Anoncreds {
Expand All @@ -60,7 +63,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_generate_nonce(ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public createSchema(options: {
Expand All @@ -86,7 +89,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_revocation_registry_definition_get_attribute(objectHandle, name, ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public credentialGetAttribute(options: { objectHandle: ObjectHandle; name: string }) {
Expand All @@ -96,7 +99,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_credential_get_attribute(objectHandle, name, ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public createCredentialDefinition(options: {
Expand Down Expand Up @@ -215,7 +218,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_encode_credential_attributes(attributeRawValues, ret)
this.handleError()

const result = handleReturnPointer<string>(ret)
const result = handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)

return result.split(',')
}
Expand Down Expand Up @@ -296,7 +299,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_create_link_secret(ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public createPresentation(options: {
Expand Down Expand Up @@ -639,7 +642,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_get_current_error(ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

private objectFromJson(method: (byteBuffer: Buffer, ret: Buffer) => unknown, options: { json: string }) {
Expand Down Expand Up @@ -721,9 +724,13 @@ export class NodeJSAnoncreds implements Anoncreds {
this.handleError()

const returnValue = handleReturnPointer<{ data: Buffer; len: number }>(ret)
const output = new Uint8Array(byteBufferToBuffer(returnValue))
const jsonAsArray = new Uint8Array(byteBufferToBuffer(returnValue))

const output = new TextDecoder().decode(jsonAsArray)

this.nativeAnoncreds.anoncreds_buffer_free(returnValue.data)
Copy link
Member

Choose a reason for hiding this comment

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

Is a buffer also copied and not a ref?

Copy link
Contributor Author

@genaris genaris Aug 31, 2023

Choose a reason for hiding this comment

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

I've just done an update there because when creating an Uint8Array from a Buffer (this case in line 727) simply a new view is created, meaning (if I understand correctly) that no data is copied.

So I think it is safer to do this buffer free right after doing the text decoding and outputting to a string (which effectively copies the data).

Copy link
Contributor

Choose a reason for hiding this comment

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

It would depend on how ffi-napi ref-napi handles this. It seems to me that they copy the data, but I haven't checked the code, yet.


return new TextDecoder().decode(output)
return output
}

public getTypeName(options: { objectHandle: ObjectHandle }) {
Expand All @@ -734,7 +741,7 @@ export class NodeJSAnoncreds implements Anoncreds {
this.nativeAnoncreds.anoncreds_object_get_type_name(objectHandle, ret)
this.handleError()

return handleReturnPointer<string>(ret)
return handleReturnPointer<string>(ret, this.nativeAnoncreds.anoncreds_string_free)
}

public objectFree(options: { objectHandle: ObjectHandle }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const nativeBindings = {
anoncreds_generate_nonce: [FFI_ERRORCODE, [FFI_STRING_PTR]],
anoncreds_get_current_error: [FFI_ERRORCODE, [FFI_STRING_PTR]],
anoncreds_object_free: [FFI_VOID, [FFI_OBJECT_HANDLE]],
anoncreds_string_free: [FFI_VOID, [FFI_STRING_PTR]],
anoncreds_object_get_json: [FFI_ERRORCODE, [FFI_OBJECT_HANDLE, ByteBufferStructPtr]],
anoncreds_object_get_type_name: [FFI_ERRORCODE, [FFI_OBJECT_HANDLE, FFI_STRING_PTR]],
anoncreds_presentation_request_from_json: [FFI_ERRORCODE, [ByteBufferStruct, FFI_STRING_PTR]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
? jsi::Value::null()
: jsi::String::createFromAscii(rt, *value);
object.setProperty(rt, "value", valueWithoutNullptr);

if (!isNullptr) anoncreds_string_free((char *)*value);
}

object.setProperty(rt, "errorCode", int(code));
Expand Down Expand Up @@ -98,6 +100,8 @@ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
? jsi::Value::null()
: jsi::String::createFromUtf8(rt, value->data, value->len);
object.setProperty(rt, "value", valueWithoutNullptr);

if (value != nullptr) anoncreds_buffer_free(*value);
}

object.setProperty(rt, "errorCode", int(code));
Expand Down
Loading