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

support custom allocators (like uv_replace_allocator()) #224

Merged
merged 7 commits into from
Jun 28, 2024
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cmake_dependent_option(USE_MBEDTLS "Use mbedTLS" ON "TLSUV_TLSLIB STREQUAL mbedt

set(tlsuv_sources
src/tlsuv.c
src/bio.c
# src/bio.c
src/http.c
src/tcp_src.c
src/um_debug.c
Expand All @@ -43,6 +43,7 @@ set(tlsuv_sources
src/p11.h
src/util.h
src/connector.c
src/alloc.c
)

if(USE_OPENSSL)
Expand Down
16 changes: 16 additions & 0 deletions include/tlsuv/tlsuv.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ extern "C" {

const char* tlsuv_version();

/**
* \brief Override the use of the standard library’s malloc(3), calloc(3), realloc(3), free(3),
* memory allocation functions.
*
* calling with method will also use passed functions to call uv_replace_allocator(),
* and appropriate function(s) in selected TLS engine (if supported)
* @param malloc_f
* @param realloc_f
* @param calloc_f
* @param free_f
*/
void tlsuv_set_allocator(uv_malloc_func malloc_f,
uv_realloc_func realloc_f,
uv_calloc_func calloc_f,
uv_free_func free_f);

typedef struct tlsuv_stream_s tlsuv_stream_t;

typedef void(*tlsuv_log_func)(int level, const char *file, unsigned int line, const char *msg);
Expand Down
106 changes: 106 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) NetFoundry Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <uv.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct allocator_t {
uv_malloc_func malloc_f;
uv_realloc_func realloc_f;
uv_calloc_func calloc_f;
uv_free_func free_f;
};

static struct allocator_t ALLOC = {
.malloc_f = malloc,
.realloc_f = realloc,
.calloc_f = calloc,
.free_f = free,
};


#if USE_OPENSSL
#include <openssl/crypto.h>

static void * crypto_malloc(size_t num, const char *file, int line) {
return ALLOC.malloc_f(num);
}

static void * crypto_realloc(void *addr, size_t num, const char *file, int line) {
return ALLOC.realloc_f(addr, num);
}

static void crypto_free(void *addr, const char *file, int line) {
ALLOC.free_f(addr);
}
#endif

#if USE_MBEDTLS
#include <mbedtls/platform.h>
#endif

void tlsuv_set_allocator(uv_malloc_func malloc_f,
uv_realloc_func realloc_f,
uv_calloc_func calloc_f,
uv_free_func free_f) {

ALLOC.malloc_f = malloc_f;
ALLOC.realloc_f = realloc_f;
ALLOC.calloc_f = calloc_f;
ALLOC.free_f = free_f;

uv_replace_allocator(malloc_f, realloc_f, calloc_f, free_f);
#if USE_OPENSSL
CRYPTO_set_mem_functions(crypto_malloc, crypto_realloc, crypto_free);
#endif

#if USE_MBEDTLS && defined(MBEDTLS_PLATFORM_MEMORY)
#if !defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
mbedtls_platform_set_calloc_free(calloc_f, free_f);
#endif
#endif
}


void *tlsuv__malloc(size_t size) {
return ALLOC.malloc_f(size);
}

void *tlsuv__calloc(size_t n, size_t size) {
return ALLOC.calloc_f(n, size);
}

void *tlsuv__realloc(void *addr, size_t size) {
return ALLOC.realloc_f(addr, size);
}

void tlsuv__free(void *addr) {
return ALLOC.free_f(addr);
}

extern char* tlsuv__strndup(const char* s, size_t len) {
Copy link
Member

Choose a reason for hiding this comment

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

unnecessary extern here

assert(s != NULL);
char *r = tlsuv__malloc(len + 1);
memcpy(r, s, len);
r[len] = 0;
return r;
}

char* tlsuv__strdup(const char *s) {
assert(s != NULL);
return tlsuv__strndup(s, strlen(s));
}

27 changes: 10 additions & 17 deletions src/bio.h → src/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TLSUV_BIO_H
#define TLSUV_BIO_H
#ifndef TLSUV_ALLOC_H
#define TLSUV_ALLOC_H

#include "tlsuv/queue.h"
#include <stdint.h>
#include <stdlib.h>

typedef struct tlsuv_bio_s {
size_t available;
size_t headoffset;
unsigned int qlen;
STAILQ_HEAD(msgq, msg) message_q;
} tlsuv_BIO;
extern void *tlsuv__malloc(size_t size);
extern void *tlsuv__calloc(size_t n, size_t size);
extern void *tlsuv__realloc(void *addr, size_t size);
extern void tlsuv__free(void *addr);

// create new BIO
tlsuv_BIO *tlsuv_BIO_new(void);
void tlsuv_BIO_free(tlsuv_BIO *bio);
extern char* tlsuv__strdup(const char *s);

Check warning on line 25 in src/alloc.h

View workflow job for this annotation

GitHub Actions / build (windows, openssl)

'_strdup': inconsistent dll linkage
extern char* tlsuv__strndup(const char *s, size_t len);

int tlsuv_BIO_put(tlsuv_BIO *bio, const uint8_t *buf, size_t len);
int tlsuv_BIO_read(tlsuv_BIO *bio, uint8_t *buf, size_t len);
size_t tlsuv_BIO_available(tlsuv_BIO *bio);

#endif//TLSUV_BIO_H
#endif //TLSUV_ALLOC_H
22 changes: 4 additions & 18 deletions src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,11 @@

#include "um_debug.h"

/*
Copyright 2020 NetFoundry, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <stdlib.h>
#include <uv.h>

#include "alloc.h"

static const unsigned char base64[] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
Expand Down Expand Up @@ -80,7 +66,7 @@ int tlsuv_base64_encode(const uint8_t *in, size_t in_len, char **out, size_t *ou
return UV_ENOMEM;
}
if (*out == NULL) {
*out = malloc(b64len + 1);
*out = tlsuv__malloc(b64len + 1);
}

uint8_t *outp = (uint8_t *)*out;
Expand Down Expand Up @@ -123,7 +109,7 @@ size_t tlsuv_base64url_decode(const char *in, char **out, size_t *out_len) {
*out = NULL;
return 0;
}
unsigned char *buf = calloc(*out_len + 1, 1);
unsigned char *buf = tlsuv__calloc(*out_len + 1, 1);

register const unsigned char *bufin;
register unsigned char *bufout;
Expand Down
103 changes: 0 additions & 103 deletions src/bio.c

This file was deleted.

12 changes: 7 additions & 5 deletions src/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <zlib.h>
#include <stdlib.h>
#include <string.h>

#include "alloc.h"
#include "um_debug.h"

#include "compression.h"
Expand Down Expand Up @@ -45,11 +47,11 @@ struct tlsuv_http_inflater_s {
};

static void* comp_alloc(void *ctx, unsigned int c, unsigned int s) {
return calloc(c, s);
return tlsuv__calloc(c, s);
}

static void comp_free(void *ctx, void *p) {
free(p);
tlsuv__free(p);
}

#if __linux__ || defined(__FreeBSD__)
Expand Down Expand Up @@ -117,15 +119,15 @@ const char *um_available_encoding(void) {
http_inflater_t *um_get_inflater(const char *encoding, data_cb cb, void *ctx) {
um_available_encoding();

http_inflater_t *inf = calloc(1, sizeof(http_inflater_t));
http_inflater_t *inf = tlsuv__calloc(1, sizeof(http_inflater_t));
inf->s.zalloc = comp_alloc;
inf->s.zfree = comp_free;
if (strcmp(encoding, "gzip") == 0)
inflateInit2(&inf->s, 16 + MAX_WBITS);
else if (strcmp(encoding, "deflate") == 0)
inflateInit(&inf->s);
else {
free(inf);
tlsuv__free(inf);
return NULL;
}

Expand All @@ -137,7 +139,7 @@ http_inflater_t *um_get_inflater(const char *encoding, data_cb cb, void *ctx) {
void um_free_inflater(http_inflater_t *inflater) {
if (inflater) {
inflateEnd_f(&inflater->s);
free(inflater);
tlsuv__free(inflater);
}
}

Expand Down
Loading
Loading