142 lines · c
1//===-- Holder Class for manipulating va_lists ------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_ARG_LIST_H10#define LLVM_LIBC_SRC___SUPPORT_ARG_LIST_H11 12#include "hdr/stdint_proxy.h"13#include "src/__support/common.h"14#include "src/__support/macros/config.h"15#include "src/string/memory_utils/inline_memcpy.h"16 17#include <stdarg.h>18#include <stddef.h>19 20namespace LIBC_NAMESPACE_DECL {21namespace internal {22 23template <typename V, typename A>24LIBC_INLINE constexpr V align_up(V val, A align) {25 return ((val + V(align) - 1) / V(align)) * V(align);26}27 28class ArgList {29 va_list vlist;30 31public:32 LIBC_INLINE ArgList(va_list vlist) { va_copy(this->vlist, vlist); }33 LIBC_INLINE ArgList(ArgList &other) { va_copy(this->vlist, other.vlist); }34 LIBC_INLINE ~ArgList() { va_end(this->vlist); }35 36 LIBC_INLINE ArgList &operator=(ArgList &rhs) {37 va_copy(vlist, rhs.vlist);38 return *this;39 }40 41 template <class T> LIBC_INLINE T next_var() { return va_arg(vlist, T); }42};43 44// Used for testing things that use an ArgList when it's impossible to know what45// the arguments should be ahead of time. An example of this would be fuzzing,46// since a function passed a random input could request unpredictable arguments.47class MockArgList {48 size_t arg_counter = 0;49 50public:51 LIBC_INLINE MockArgList() = default;52 LIBC_INLINE MockArgList(va_list) { ; }53 LIBC_INLINE MockArgList(MockArgList &other) {54 arg_counter = other.arg_counter;55 }56 LIBC_INLINE ~MockArgList() = default;57 58 LIBC_INLINE MockArgList &operator=(MockArgList &rhs) {59 arg_counter = rhs.arg_counter;60 return *this;61 }62 63 template <class T> LIBC_INLINE T next_var() {64 arg_counter++;65 return T(arg_counter);66 }67 68 size_t read_count() const { return arg_counter; }69};70 71// Used by the GPU implementation to parse how many bytes need to be read from72// the variadic argument buffer.73template <bool packed> class DummyArgList {74 size_t arg_counter = 0;75 76public:77 LIBC_INLINE DummyArgList() = default;78 LIBC_INLINE DummyArgList(va_list) { ; }79 LIBC_INLINE DummyArgList(DummyArgList &other) {80 arg_counter = other.arg_counter;81 }82 LIBC_INLINE ~DummyArgList() = default;83 84 LIBC_INLINE DummyArgList &operator=(DummyArgList &rhs) {85 arg_counter = rhs.arg_counter;86 return *this;87 }88 89 template <class T> LIBC_INLINE T next_var() {90 arg_counter = packed ? arg_counter + sizeof(T)91 : align_up(arg_counter, alignof(T)) + sizeof(T);92 return T(arg_counter);93 }94 95 size_t read_count() const { return arg_counter; }96};97 98// Used for the GPU implementation of `printf`. This models a variadic list as a99// simple array of pointers that are built manually by the implementation.100template <bool packed> class StructArgList {101 void *ptr;102 void *end;103 104public:105 LIBC_INLINE StructArgList(void *ptr, size_t size)106 : ptr(ptr), end(reinterpret_cast<unsigned char *>(ptr) + size) {}107 LIBC_INLINE StructArgList(const StructArgList &other) {108 ptr = other.ptr;109 end = other.end;110 }111 LIBC_INLINE StructArgList() = default;112 LIBC_INLINE ~StructArgList() = default;113 114 LIBC_INLINE StructArgList &operator=(const StructArgList &rhs) {115 ptr = rhs.ptr;116 return *this;117 }118 119 LIBC_INLINE void *get_ptr() const { return ptr; }120 121 template <class T> LIBC_INLINE T next_var() {122 if (!packed)123 ptr = reinterpret_cast<void *>(124 align_up(reinterpret_cast<uintptr_t>(ptr), alignof(T)));125 if (ptr >= end)126 return T(-1);127 128 // Memcpy because pointer alignment may be illegal given a packed struct.129 T val;130 inline_memcpy(&val, ptr, sizeof(T));131 132 ptr =133 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(ptr) + sizeof(T));134 return val;135 }136};137 138} // namespace internal139} // namespace LIBC_NAMESPACE_DECL140 141#endif // LLVM_LIBC_SRC___SUPPORT_ARG_LIST_H142