Skip to content

Commit

Permalink
Runtime: Ensure the validity of returned cdef pointers
Browse files Browse the repository at this point in the history
The exports tables are static, but when appending the aliases, then indeed the pointer could become invalid - however unlikely that may be here.
  • Loading branch information
rdw-software committed Feb 11, 2024
1 parent ff78ace commit f04bae6
Show file tree
Hide file tree
Showing 24 changed files with 62 additions and 35 deletions.
6 changes: 4 additions & 2 deletions Runtime/Bindings/crypto_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <cstddef>
#include <iostream>
#include <string>

size_t openssl_to_base64(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len) {
EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new();
Expand Down Expand Up @@ -108,8 +109,9 @@ EMBED_BINARY(crypto_exported_types, "Runtime/Bindings/crypto_exports.h")

namespace crypto_ffi {

const char* getTypeDefinitions() {
return crypto_exported_types;
std::string getTypeDefinitions() {
return std::string(crypto_exported_types, crypto_exported_types_size);
;
}

void* getExportsTable() {
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Bindings/crypto_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include <cstddef>
#include <cstdint>
#include <string>

#include "crypto_exports.h"

namespace crypto_ffi {

const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
const char* getVersionText();
long int getVersionNumber();
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Bindings/glfw_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ EMBED_BINARY(glfw_exported_types, "Runtime/Bindings/glfw_exports.h")

namespace glfw_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
size_t totalSize = glfw_aliased_types_size + glfw_exported_types_size + 1;

std::string cdefs;
Expand All @@ -237,7 +237,7 @@ namespace glfw_ffi {
cdefs.append("\n");
cdefs.append(glfw_exported_types, glfw_exported_types_size);

return cdefs.c_str();
return cdefs;
}

void* getExportsTable() {
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Bindings/glfw_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "interop_ffi.hpp"
#include "macros.hpp"

#include <string>

#define GLFW_VERSION_STRING TOSTRING(GLFW_VERSION_MAJOR) "." TOSTRING(GLFW_VERSION_MINOR) "." TOSTRING(GLFW_VERSION_REVISION)

void glfw_set_window_move_callback(GLFWwindow* window, std::queue<deferred_event_t>* queue);
Expand All @@ -29,6 +31,6 @@ void glfw_set_character_input_callback(GLFWwindow* window, std::queue<deferred_e
#include "glfw_exports.h"

namespace glfw_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
6 changes: 3 additions & 3 deletions Runtime/Bindings/iconv_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "macros.hpp"

#include <iostream>
#include <string.h>
#include <string>

#include <iconv.h>

Expand Down Expand Up @@ -51,8 +51,8 @@ EMBED_BINARY(iconv_exported_types, "Runtime/Bindings/iconv_exports.h")

namespace iconv_ffi {

const char* getTypeDefinitions() {
return iconv_exported_types;
std::string getTypeDefinitions() {
return std::string(iconv_exported_types);
}

void* getExportsTable() {
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Bindings/iconv_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <cstddef>
#include <cstdint>
#include <string.h>
#include <string>

#include "iconv_exports.h"

namespace iconv_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
4 changes: 2 additions & 2 deletions Runtime/Bindings/interop_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ EMBED_BINARY(interop_exported_types, "Runtime/Bindings/interop_exports.h")

namespace interop_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
size_t totalSize = interop_aliased_types_size + interop_exported_types_size + 1;

std::string cdefs;
Expand All @@ -57,7 +57,7 @@ namespace interop_ffi {
cdefs.append("\n");
cdefs.append(interop_exported_types, interop_exported_types_size);

return cdefs.c_str();
return cdefs;
}

void* getExportsTable() {
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Bindings/interop_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <queue>
#include <cstddef>
#include <string>

typedef WGPUTexture wgpu_texture_t;
typedef WGPUBuffer wgpu_buffer_t;
Expand All @@ -15,6 +16,6 @@ typedef std::queue<deferred_event_t>* deferred_event_queue_t;
#include "interop_exports.h"

namespace interop_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
5 changes: 3 additions & 2 deletions Runtime/Bindings/labsound_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <cstddef>
#include <cstring>
#include <string>
#include <vector>

using namespace lab;
Expand Down Expand Up @@ -397,7 +398,7 @@ EMBED_BINARY(labsound_exported_types, "Runtime/Bindings/labsound_exports.h")

namespace labsound_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
size_t totalSize = labsound_aliased_types_size + labsound_exported_types_size + 1;

std::string cdefs;
Expand All @@ -407,7 +408,7 @@ namespace labsound_ffi {
cdefs.append("\n");
cdefs.append(labsound_exported_types, labsound_exported_types_size);

return cdefs.c_str();
return cdefs;
}

void* getExportsTable() {
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Bindings/labsound_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstddef>
#include <cstdint>
#include <string>

// Opaque to LuaJIT
typedef lab::AudioDevice_RtAudio* labsound_audio_device_t;
Expand Down Expand Up @@ -32,7 +33,7 @@ static_assert(sizeof(labsound_audio_stream_config_t::desired_channels) == sizeof
static_assert(sizeof(labsound_audio_stream_config_t::desired_samplerate) == sizeof(lab::AudioStreamConfig::desired_samplerate));

namespace labsound_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Bindings/rml_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace rml_ffi {
return rml_ffi::assignedLuaState;
}

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
size_t totalSize = rml_aliased_types_size + rml_exported_types_size + 1;

std::string cdefs;
Expand All @@ -201,7 +201,7 @@ namespace rml_ffi {
cdefs.append("\n");
cdefs.append(rml_exported_types, rml_exported_types_size);

return cdefs.c_str();
return cdefs;
}

void* getExportsTable() {
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Bindings/rml_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <webgpu.h>

#include <string>

typedef WGPUDevice wgpu_device_t;
typedef rml_geometry_info_t* rml_compiled_geometry_t;
typedef GLFWwindow* glfw_window_t;
Expand All @@ -15,6 +17,6 @@ typedef GLFWwindow* glfw_window_t;
namespace rml_ffi {
void assignLuaState(lua_State* L);
lua_State* getAssignedLuaState();
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
4 changes: 3 additions & 1 deletion Runtime/Bindings/runtime_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ extern "C" {
#include "luajit_repl.h"
}

#include <string>

EMBED_BINARY(runtime_exported_types, "Runtime/Bindings/runtime_exports.h")

namespace runtime_ffi {
Expand All @@ -22,7 +24,7 @@ namespace runtime_ffi {
dotty(assignedLuaState);
}

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
return runtime_exported_types;
}

Expand Down
4 changes: 3 additions & 1 deletion Runtime/Bindings/runtime_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "lua.hpp"

#include <string>

#include "runtime_exports.h"

namespace runtime_ffi {
Expand All @@ -13,6 +15,6 @@ namespace runtime_ffi {
// REPL
void runtime_repl_start();

const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
3 changes: 2 additions & 1 deletion Runtime/Bindings/stbi_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <string>
#include <utility>

const char* stbi_version() {
Expand Down Expand Up @@ -204,7 +205,7 @@ EMBED_BINARY(stbi_exported_types, "Runtime/Bindings/stbi_exports.h")

namespace stbi_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
return stbi_exported_types;
}

Expand Down
3 changes: 2 additions & 1 deletion Runtime/Bindings/stbi_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

#include <cstdint>
#include <cstddef>
#include <string>

#include "stbi_exports.h"

namespace stbi_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
3 changes: 2 additions & 1 deletion Runtime/Bindings/stduuid_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <random>
#include <array>
#include <string>

// The platform-specific RNG APIs aren't enabled by default since they're non-standard
#define UUID_SYSTEM_GENERATOR
Expand Down Expand Up @@ -85,7 +86,7 @@ EMBED_BINARY(stduuid_exported_types, "Runtime/Bindings/stduuid_exports.h")

namespace stduuid_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
return stduuid_exported_types;
}

Expand Down
4 changes: 3 additions & 1 deletion Runtime/Bindings/stduuid_ffi.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <string>

#include "stduuid_exports.h"

bool uuid_create_v4(uuid_rfc_string_t* result);
Expand All @@ -8,6 +10,6 @@ bool uuid_create_v5(const char* namespace_uuid_str, const char* name, uuid_rfc_s
bool uuid_create_system(uuid_rfc_string_t* result);

namespace stduuid_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
5 changes: 3 additions & 2 deletions Runtime/Bindings/uws_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "WebServer.hpp"

#include <string>
#include <unordered_map>

const char* uws_version() {
Expand Down Expand Up @@ -197,7 +198,7 @@ EMBED_BINARY(uws_exported_types, "Runtime/Bindings/uws_exports.h")

namespace uws_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
size_t totalSize = uws_aliased_types_size + uws_exported_types_size + 1;

std::string cdefs;
Expand All @@ -207,7 +208,7 @@ namespace uws_ffi {
cdefs.append("\n");
cdefs.append(uws_exported_types, uws_exported_types_size);

return cdefs.c_str();
return cdefs;
}

void* getExportsTable() {
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Bindings/uws_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string>

namespace uws_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
uWS::Loop* assignEventLoop(void* existing_native_loop);
void unassignEventLoop(uWS::Loop* uwsEventLoop);
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Bindings/webgpu_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ EMBED_BINARY(webgpu_exported_types, "Runtime/Bindings/webgpu_exports.h")

namespace webgpu_ffi {

const char* getTypeDefinitions() {
std::string getTypeDefinitions() {
size_t totalSize = webgpu_aliased_types_size + webgpu_exported_types_size + 1;

std::string cdefs;
Expand All @@ -22,7 +22,7 @@ namespace webgpu_ffi {
cdefs.append("\n");
cdefs.append(webgpu_exported_types, webgpu_exported_types_size);

return cdefs.c_str();
return cdefs;
}

void* getExportsTable() {
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Bindings/webgpu_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <webgpu.h>
#include <wgpu.h>

#include <string>

#include "webgpu_exports.h"

namespace webgpu_ffi {
const char* getTypeDefinitions();
std::string getTypeDefinitions();
void* getExportsTable();
}
Loading

0 comments on commit f04bae6

Please sign in to comment.