Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8a13147
Remove PAL_Random
jkoritzinsky Oct 16, 2024
f96014a
Move the "COM minipal" APIs into the regular repo minipal and update …
jkoritzinsky Oct 16, 2024
5ccbb53
Remove direct CoCreateGuid usage from CoreCLR and instead use the min…
jkoritzinsky Oct 16, 2024
cbb3c4a
Remove CoTaskMemAlloc/Free usage from xplat code (and instead use the…
jkoritzinsky Oct 16, 2024
995c858
palrt is empty, so remove it.
jkoritzinsky Oct 17, 2024
75c421d
Use minipal_u16_strlen in more places.
jkoritzinsky Oct 17, 2024
4ab9fdf
Use minipal's random APIs instead of Mono having their own
jkoritzinsky Oct 17, 2024
d44224d
fixup! Use minipal_u16_strlen in more places.
jkoritzinsky Oct 17, 2024
2b4779b
Remove 'random' usage in eventpipe and unify all usages of the minipa…
jkoritzinsky Oct 17, 2024
40077d5
PR feedback
jkoritzinsky Oct 17, 2024
9408aa6
Link minipal to bcrypt and flow it so superpmi picks it up
jkoritzinsky Oct 17, 2024
aae4c86
Make the co-task-mem-alloc PAL just call malloc
jkoritzinsky Oct 17, 2024
9ccdb60
We're in C
jkoritzinsky Oct 17, 2024
676c988
Add casts for Windows
jkoritzinsky Oct 17, 2024
1be8d8a
Fix assert
jkoritzinsky Oct 18, 2024
4f39154
Remove u16_strlen from the CoreCLR minipal
jkoritzinsky Oct 21, 2024
040549d
PR feedback and fix dbgutil.cpp
jkoritzinsky Oct 21, 2024
7ab733a
Fix CoreCLR eventpipe runtime.
jkoritzinsky Oct 21, 2024
367612d
Fix windows build
jkoritzinsky Oct 22, 2024
f31bcfd
Move guid definitions out of the minipal and back to the CoreCLR PAL …
jkoritzinsky Oct 23, 2024
329434f
Fix include path now that we're in the PAL and not palrt
jkoritzinsky Oct 24, 2024
337fcef
Add rt include directory for guid.cpp
jkoritzinsky Oct 25, 2024
2d94d0a
Export GUID_NULL from the dac's copy of the PAL for the dbi.
jkoritzinsky Oct 25, 2024
460b8e5
Revert "Remove u16_strlen from the CoreCLR minipal"
jkoritzinsky Oct 29, 2024
a907e73
Merge branch 'more-minipal' of github.com:jkoritzinsky/runtime into m…
jkoritzinsky Oct 29, 2024
1f5573e
Merge branch 'main' into more-minipal
jkoritzinsky Oct 29, 2024
2ef0a5a
Go back to a CoTaskMemAlloc PAL
jkoritzinsky Oct 29, 2024
5f4df82
Use a prefixed name everywhere instead of shiming the Win32 API name …
jkoritzinsky Oct 29, 2024
c84060e
Fix target name change after merge
jkoritzinsky Oct 29, 2024
05d141b
Don't prefix the method but instead put the header in a subfolder.
jkoritzinsky Oct 30, 2024
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
Prev Previous commit
Next Next commit
Move the "COM minipal" APIs into the regular repo minipal and update …
…the CoreCLR PAL to also use it. Also remove dangling references to DNCP and remove dead standalone-DNMD infra
  • Loading branch information
jkoritzinsky committed Oct 17, 2024
commit f96014a4378d26e8a5f4591af74074d3b4975190
8 changes: 3 additions & 5 deletions src/coreclr/minipal/Unix/dn-u16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ typedef char16_t WCHAR;

#include <dn-u16.h>
#include <string.h>
#include <minipal/strings.h>

size_t u16_strlen(const WCHAR* str)
{
size_t nChar = 0;
while (*str++)
nChar++;
return nChar;
return minipal_u16_strlen((CHAR16_T*)str);
}

int u16_strcmp(const WCHAR* str1, const WCHAR* str2)
Expand Down Expand Up @@ -192,4 +190,4 @@ uint64_t u16_strtoui64(const WCHAR* nptr, WCHAR** endptr, int base)
double u16_strtod(const WCHAR* nptr, WCHAR** endptr)
{
return PAL_wcstod(nptr, endptr);
}
}
3 changes: 1 addition & 2 deletions src/coreclr/palrt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ add_library_clr(palrt
${PALRT_SOURCES}
)

# Install the static PAL library for VS
install_clr(TARGETS palrt DESTINATIONS lib)
target_link_libraries(palrt PUBLIC minipal)
5 changes: 3 additions & 2 deletions src/coreclr/palrt/comem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
// ===========================================================================

#include "common.h"
#include <minipal/memory.h>

STDAPI_(LPVOID) CoTaskMemAlloc(SIZE_T cb)
{
return malloc(cb);
return minipal_co_task_mem_alloc(cb);
}

STDAPI_(void) CoTaskMemFree(LPVOID pv)
{
free(pv);
minipal_co_task_mem_free(pv);
}
33 changes: 5 additions & 28 deletions src/coreclr/palrt/guid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,20 @@
// PALRT guids
// ===========================================================================

#define INITGUID
#include <minipal/random.h>
#include <guiddef.h>
#include <minipal/guid.h>

// These are GUIDs and IIDs that would normally be provided by the system via uuid.lib,
// and that the PALRT exposes through headers.

DEFINE_GUID(GUID_NULL, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
DEFINE_GUID(IID_IUnknown, 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
DEFINE_GUID(IID_IClassFactory, 0x00000001, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);


// objidl.idl
DEFINE_GUID(IID_ISequentialStream, 0x0c733a30, 0x2a1c, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
DEFINE_GUID(IID_IStream, 0x0000000c, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

// Create a random guid based on the https://www.ietf.org/rfc/rfc4122.txt
STDAPI
CoCreateGuid(OUT GUID * pguid)
{
if (!minipal_get_cryptographically_secure_random_bytes(pguid, sizeof(GUID)))
minipal_guid_t guid;
if (!minipal_guid_v4_create(&guid))
{
return E_FAIL;
}

static const USHORT VersionMask = 0xF000;
static const USHORT RandomGuidVersion = 0x4000;

static const BYTE ClockSeqHiAndReservedMask = 0xC0;
static const BYTE ClockSeqHiAndReservedValue = 0x80;

// Modify bits indicating the type of the GUID

// time_hi_and_version
pguid->Data3 = (pguid->Data3 & ~VersionMask) | RandomGuidVersion;
// clock_seq_hi_and_reserved
pguid->Data4[0] = (pguid->Data4[0] & ~ClockSeqHiAndReservedMask) | ClockSeqHiAndReservedValue;
static_assert(sizeof(GUID) == sizeof(minipal_guid_t), "GUID and minipal_guid_t must be the same size");
memcpy(pguid, &guid, sizeof(GUID));

return S_OK;
}
3 changes: 3 additions & 0 deletions src/native/minipal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ include(configure.cmake)

set(SOURCES
cpufeatures.c
guid.c
memory.c
random.c
debugger.c
strings.c
time.c
unicodedata.c
utf8.c
Expand Down
77 changes: 77 additions & 0 deletions src/native/minipal/guid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "guid.h"
#include "random.h"
#include <string.h>
#ifdef HOST_WINDOWS
#include <Windows.h>
#endif

// Define some well-known GUIDs on non-Windows platforms.
#ifndef HOST_WINDOWS
// 00000000-0000-0000-0000-000000000000
minipal_guid_t const GUID_NULL = { 0x0, 0x0, 0x0, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };

// 00000000-0000-0000-C000-000000000046
minipal_guid_t const IID_IUnknown = { 0x0, 0x0, 0x0, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };

// 00000001-0000-0000-C000-000000000046
minipal_guid_t const IID_IClassFactory = { 0x1, 0x0, 0x0, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };

// 0c733a30-2a1c-11ce-ade5-00aa0044773d
minipal_guid_t const IID_ISequentialStream = { 0x0c733a30, 0x2a1c, 0x11ce, { 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d } };

// 0000000C-0000-0000-C000-000000000046
minipal_guid_t const IID_IStream = { 0xC, 0x0, 0x0, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
#endif

// See RFC-4122 section 4.4 on creation of random GUID.
// https://www.ietf.org/rfc/rfc4122.txt
//
// The version 4 UUID is meant for generating UUIDs from truly-random or
// pseudo-random numbers.
//
// The algorithm is as follows:
//
// o Set the two most significant bits (bits 6 and 7) of the
// clock_seq_hi_and_reserved to zero and one, respectively.
//
// o Set the four most significant bits (bits 12 through 15) of the
// time_hi_and_version field to the 4-bit version number from
// Section 4.1.3.
//
// o Set all the other bits to randomly (or pseudo-randomly) chosen
// values.
//
bool minipal_guid_v4_create(minipal_guid_t* guid)
{
#ifdef HOST_WINDOWS
// Windows has a built-in function for creating v4 GUIDs.
return SUCCEEDED(CoCreateGuid((GUID*)guid));
#else
if (minipal_get_cryptographically_secure_random_bytes((uint8_t*)guid, sizeof(*guid)) != 0)
return false;

{
// time_hi_and_version
const uint16_t mask = 0xf000; // b1111000000000000
const uint16_t value = 0x4000; // b0100000000000000
guid->data3 = (guid->data3 & ~mask) | value;
}

{
// clock_seq_hi_and_reserved
const uint8_t mask = 0xc0; // b11000000
const uint8_t value = 0x80; // b10000000
guid->data4[0] = (guid->data4[0] & ~mask) | value;
}

return true;
#endif
}

bool minipal_guid_equals(minipal_guid_t const* g1, minipal_guid_t const* g2)
{
return memcmp(g1, g2, sizeof(minipal_guid_t)) == 0;
}
43 changes: 43 additions & 0 deletions src/native/minipal/guid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#ifndef MINIPAL_GUID_H
#define MINIPAL_GUID_H

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

typedef struct minipal_guid__
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
typedef struct minipal_guid__
typedef struct

Copy link
Member Author

Choose a reason for hiding this comment

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

We get a slightly better experience in debuggers by giving a name to the struct instead of just naming the typedef (without the name on the struct, you just get "anonymous struct" which is a little annoying).

{
uint32_t data1;
uint16_t data2;
uint16_t data3;
uint8_t data4[8];
} minipal_guid_t;

bool minipal_guid_v4_create(minipal_guid_t* guid);

bool minipal_guid_equals(minipal_guid_t const* g1, minipal_guid_t const* g2);

#ifdef __cplusplus
}
#endif // __cplusplus

#ifdef __cplusplus
inline bool operator==(minipal_guid_t const& a, minipal_guid_t const& b)
{
return minipal_guid_equals(&a, &b);
}

inline bool operator!=(minipal_guid_t const& a, minipal_guid_t const& b)
{
return !(a == b);
}
#endif

#endif // MINIPAL_GUID_H
40 changes: 40 additions & 0 deletions src/native/minipal/memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "memory.h"

#ifdef HOST_WINDOWS
#include <Windows.h>

void* minipal_co_task_mem_alloc(size_t cb)
{
return CoTaskMemAlloc(cb);
}

void minipal_co_task_mem_free(void* pv)
{
CoTaskMemFree(pv);
}
#else
// CoTaskMemAlloc always aligns on an 8-byte boundary.
#define ALIGN 8

void* minipal_co_task_mem_alloc(size_t cb)
{
// Ensure malloc always allocates.
if (cb == 0)
cb = ALIGN;

// Align the allocation size.
size_t cb_safe = (cb + (ALIGN - 1)) & ~(ALIGN - 1);
if (cb_safe < cb) // Overflow
return NULL;

return aligned_alloc(ALIGN, cb_safe);
}

void minipal_co_task_mem_free(void* pv)
{
free(pv);
}
#endif
15 changes: 15 additions & 0 deletions src/native/minipal/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#ifndef MINIPAL_MEMORY_H
#define MINIPAL_MEMORY_H

#include <stdlib.h>

// Allocate memory on the platform equivalent of the CoTaskMem heap.
void* minipal_co_task_mem_alloc(size_t cb);

// Free memory allocated on the platform equivalent of the CoTaskMem heap.
void minipal_co_task_mem_free(void* pv);

#endif // MINIPAL_MEMORY_H
14 changes: 14 additions & 0 deletions src/native/minipal/strings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "strings.h"

size_t minipal_u16_strlen(const CHAR16_T* str)
{
size_t len = 0;
while (*str++)
{
len++;
}
return len;
}
8 changes: 8 additions & 0 deletions src/native/minipal/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ CHAR16_T minipal_toupper_invariant(CHAR16_T code);
*/
CHAR16_T minipal_tolower_invariant(CHAR16_T code);

/**
* @brief Get the length of a null-terminated UTF-16 string.
*
* @param str The null-terminated UTF-16 string.
* @return The length of the string.
*/
size_t minipal_u16_strlen(const CHAR16_T* str);

#ifdef __cplusplus
}
#endif // __cplusplus
Expand Down