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

Un-mangle libc string function names so they can benefit from LLVM optimizations. #448

Merged
merged 1 commit into from
Feb 17, 2025
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
20 changes: 9 additions & 11 deletions sdk/include/cdefs.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright Microsoft and CHERIoT Contributors.
// SPDX-License-Identifier: MIT

#ifndef __CDEFS_H__
#define __CDEFS_H__
#pragma once

/*
* Testing against Clang-specific extensions.
Expand Down Expand Up @@ -87,14 +86,6 @@ using _Bool = bool;
#else
# define __cheri_libcall __attribute__((cheri_libcall))
# define __cheri_callback __attribute__((cheri_ccallback))

/**
* Define the symbol for the libcall that the compiler will expand the `strlen`
* builtin to. This builtin is used internally in libc++ (and possibly in
* other places) to avoid the namespace pollution from including `string.h` but
* is either constant folded in the front end or expanded to a libcall.
*/
unsigned __builtin_strlen(const char *str) __asm__("_Z6strlenPKc");
#endif

#define offsetof(a, b) __builtin_offsetof(a, b)
Expand Down Expand Up @@ -132,4 +123,11 @@ unsigned __builtin_strlen(const char *str) __asm__("_Z6strlenPKc");
#define CHERIOT_VERSION_TRIPLE(major, minor, patch) \
((major * 10000) + (minor * 100) + (patch))

#endif // _CDEFS_H_
/**
* Helper for declaring standard library functions that should be exported
* as __cheri_libcall, but which should not have their names mangled. This
* is needed to enable LLVM optimizations to trigger on standard library
* functions.
*/
#define CHERIOT_DECLARE_STANDARD_LIBCALL(name, ret, ...) \
__cheri_libcall ret name(__VA_ARGS__) __asm__(#name);
67 changes: 46 additions & 21 deletions sdk/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,52 @@
#include <stddef.h>
#include <stdint.h>

int __cheri_libcall memcmp(const void *str1,
const void *str2,
size_t count) __asm__("memcmp");
void *__cheri_libcall memcpy(void *dest,
const void *src,
size_t n) __asm__("memcpy");
void *__cheri_libcall memset(void *, int, size_t) __asm__("memset");
void *__cheri_libcall memmove(void *dest,
const void *src,
size_t n) __asm__("memmove");
void *__cheri_libcall memchr(const void *, int, size_t);
void *__cheri_libcall memrchr(const void *, int, size_t);
size_t __cheri_libcall strlen(const char *str);
int __cheri_libcall strncmp(const char *s1, const char *s2, size_t n);
char *__cheri_libcall strncpy(char *dest, const char *src, size_t n);
int __cheri_libcall strcmp(const char *s1, const char *s2);
char *__cheri_libcall strnstr(const char *haystack,
const char *needle,
size_t haystackLength);
char *__cheri_libcall strchr(const char *s, int c);
size_t __cheri_libcall strlcpy(char *dest, const char *src, size_t n);
CHERIOT_DECLARE_STANDARD_LIBCALL(memcmp,
int,
const void *str1,
const void *str2,
size_t count)
CHERIOT_DECLARE_STANDARD_LIBCALL(memcmp,
int,
const void *str1,
const void *str2,
size_t count)
CHERIOT_DECLARE_STANDARD_LIBCALL(memcpy,
void *,
void *dest,
const void *src,
size_t n)
CHERIOT_DECLARE_STANDARD_LIBCALL(memset, void *, void *, int, size_t)
CHERIOT_DECLARE_STANDARD_LIBCALL(memmove,
void *,
void *dest,
const void *src,
size_t n)
CHERIOT_DECLARE_STANDARD_LIBCALL(memchr, void *, const void *, int, size_t)
CHERIOT_DECLARE_STANDARD_LIBCALL(memrchr, void *, const void *, int, size_t)
CHERIOT_DECLARE_STANDARD_LIBCALL(strlen, size_t, const char *str)
CHERIOT_DECLARE_STANDARD_LIBCALL(strncmp,
int,
const char *s1,
const char *s2,
size_t n)
CHERIOT_DECLARE_STANDARD_LIBCALL(strncpy,
char *,
char *dest,
const char *src,
size_t n)
CHERIOT_DECLARE_STANDARD_LIBCALL(strcmp, int, const char *s1, const char *s2)
CHERIOT_DECLARE_STANDARD_LIBCALL(strnstr,
char *,
const char *haystack,
const char *needle,
size_t haystackLength)
CHERIOT_DECLARE_STANDARD_LIBCALL(strchr, char *, const char *s, int c)
CHERIOT_DECLARE_STANDARD_LIBCALL(strlcpy,
size_t,
char *dest,
const char *src,
size_t n)

/**
* Explicit bzero is a memset variant that the compiler is not permitted to
Expand Down
4 changes: 2 additions & 2 deletions sdk/lib/atomic/atomic.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* inserts calls to them. Declare them using the asm label extension.
*/
#define DECLARE_ATOMIC_LIBCALL(name, ret, ...) \
[[cheri::interrupt_state(disabled)]] __cheri_libcall ret name( \
__VA_ARGS__) asm(#name);
[[cheri::interrupt_state(disabled)]] CHERIOT_DECLARE_STANDARD_LIBCALL( \
name, ret, __VA_ARGS__)

/**
* Macro that defines a library function to implement an atomic load for the
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/string/strcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <cdefs.h>
#include <stddef.h>
#include <string.h>

/*
* Compare strings.
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/string/strlen.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cdefs.h>
#include <stddef.h>
#include <string.h>

size_t __cheri_libcall strlen(const char *str)
{
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/string/strncpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cdefs.h>
#include <stddef.h>
#include <string.h>

char *__cheri_libcall strncpy(char *dest, const char *src, size_t n)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/allocator-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace

free(pStack);
free(pGlobal);
heap_quarantine_empty();
(void)heap_quarantine_empty();

state = 1;

Expand Down Expand Up @@ -271,7 +271,7 @@ namespace
* failure we see below isn't because we aren't allowing the revocation
* state machine to advance.
*/
heap_quarantine_empty();
(void)heap_quarantine_empty();

bool memoryExhausted = false;
for (auto &allocation : allocations)
Expand Down