summaryrefslogtreecommitdiffstats
path: root/js/src/ctypes/CTypes.h
blob: 665aed537e40902418c4942f63492d1441825f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*-
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef ctypes_CTypes_h
#define ctypes_CTypes_h

#include "mozilla/Sprintf.h"
#include "mozilla/Vector.h"

#include "ffi.h"
#include "prlink.h"

#include "ctypes/typedefs.h"
#include "gc/ZoneAllocator.h"
#include "js/AllocPolicy.h"
#include "js/GCHashTable.h"
#include "js/UniquePtr.h"
#include "js/Vector.h"
#include "vm/JSObject.h"
#include "vm/StringType.h"

namespace JS {
struct CTypesCallbacks;
}  // namespace JS

namespace js {
namespace ctypes {

/*******************************************************************************
** Utility classes
*******************************************************************************/

// CTypes builds a number of strings. StringBuilder allows repeated appending
// with a single error check at the end. Only the Vector methods required for
// building the string are exposed.

template <class CharT, size_t N>
class StringBuilder {
  Vector<CharT, N, SystemAllocPolicy> v;

  // Have any (OOM) errors been encountered while constructing this string?
  bool errored{false};

#ifdef DEBUG
  // Have we finished building this string?
  bool finished{false};

  // Did we check for errors?
  mutable bool checked{false};
#endif

 public:
  explicit operator bool() const {
#ifdef DEBUG
    checked = true;
#endif
    return !errored;
  }

  // Handle the result of modifying the string, by remembering the persistent
  // errored status.
  bool handle(bool result) {
    MOZ_ASSERT(!finished);
    if (!result) {
      errored = true;
    }
    return result;
  }

  bool resize(size_t n) { return handle(v.resize(n)); }

  CharT& operator[](size_t index) { return v[index]; }
  const CharT& operator[](size_t index) const { return v[index]; }
  size_t length() const { return v.length(); }

  template <typename U>
  [[nodiscard]] bool append(U&& u) {
    return handle(v.append(u));
  }

  template <typename U>
  [[nodiscard]] bool append(const U* begin, const U* end) {
    return handle(v.append(begin, end));
  }

  template <typename U>
  [[nodiscard]] bool append(const U* begin, size_t len) {
    return handle(v.append(begin, len));
  }

  CharT* begin() {
    MOZ_ASSERT(!finished);
    return v.begin();
  }

  // finish() produces the results of the string building, and is required as
  // the last thing before the string contents are used. The StringBuilder must
  // be checked for errors before calling this, however.
  Vector<CharT, N, SystemAllocPolicy>&& finish() {
    MOZ_ASSERT(!errored);
    MOZ_ASSERT(!finished);
    MOZ_ASSERT(checked);
#ifdef DEBUG
    finished = true;
#endif
    return std::move(v);
  }
};

// Note that these strings do not have any inline storage, because we use move
// constructors to pass the data around and inline storage would necessitate
// copying.
typedef StringBuilder<char16_t, 0> AutoString;
typedef StringBuilder<char, 0> AutoCString;

typedef Vector<char16_t, 0, SystemAllocPolicy> AutoStringChars;
typedef Vector<char, 0, SystemAllocPolicy> AutoCStringChars;

// Convenience functions to append, insert, and compare Strings.
template <class T, size_t N, size_t ArrayLength>
void AppendString(JSContext* cx, StringBuilder<T, N>& v,
                  const char (&array)[ArrayLength]) {
  // Don't include the trailing '\0'.
  size_t alen = ArrayLength - 1;
  size_t vlen = v.length();
  if (!v.resize(vlen + alen)) {
    return;
  }

  for (size_t i = 0; i < alen; ++i) {
    v[i + vlen] = array[i];
  }
}

template <class T, size_t N>
void AppendChars(StringBuilder<T, N>& v, const char c, size_t count) {
  size_t vlen = v.length();
  if (!v.resize(vlen + count)) {
    return;
  }

  for (size_t i = 0; i < count; ++i) {
    v[i + vlen] = c;
  }
}

template <class T, size_t N>
void AppendUInt(StringBuilder<T, N>& v, unsigned n) {
  char array[16];
  size_t alen = SprintfLiteral(array, "%u", n);
  size_t vlen = v.length();
  if (!v.resize(vlen + alen)) {
    return;
  }

  for (size_t i = 0; i < alen; ++i) {
    v[i + vlen] = array[i];
  }
}

template <class T, size_t N, size_t M, class AP>
void AppendString(JSContext* cx, StringBuilder<T, N>& v,
                  mozilla::Vector<T, M, AP>& w) {
  if (!v.append(w.begin(), w.length())) {
    return;
  }
}

template <size_t N>
void AppendString(JSContext* cx, StringBuilder<char16_t, N>& v, JSString* str) {
  MOZ_ASSERT(str);
  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return;
  }
  JS::AutoCheckCannotGC nogc;
  if (linear->hasLatin1Chars()) {
    if (!v.append(linear->latin1Chars(nogc), linear->length())) {
      return;
    }
  } else {
    if (!v.append(linear->twoByteChars(nogc), linear->length())) {
      return;
    }
  }
}

template <size_t N>
void AppendString(JSContext* cx, StringBuilder<char, N>& v, JSString* str) {
  MOZ_ASSERT(str);
  size_t vlen = v.length();
  size_t alen = str->length();
  if (!v.resize(vlen + alen)) {
    return;
  }

  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return;
  }

  JS::AutoCheckCannotGC nogc;
  if (linear->hasLatin1Chars()) {
    const Latin1Char* chars = linear->latin1Chars(nogc);
    for (size_t i = 0; i < alen; ++i) {
      v[i + vlen] = char(chars[i]);
    }
  } else {
    const char16_t* chars = linear->twoByteChars(nogc);
    for (size_t i = 0; i < alen; ++i) {
      v[i + vlen] = char(chars[i]);
    }
  }
}

template <class T, size_t N, size_t ArrayLength>
void PrependString(JSContext* cx, StringBuilder<T, N>& v,
                   const char (&array)[ArrayLength]) {
  // Don't include the trailing '\0'.
  size_t alen = ArrayLength - 1;
  size_t vlen = v.length();
  if (!v.resize(vlen + alen)) {
    return;
  }

  // Move vector data forward. This is safe since we've already resized.
  memmove(v.begin() + alen, v.begin(), vlen * sizeof(T));

  // Copy data to insert.
  for (size_t i = 0; i < alen; ++i) {
    v[i] = array[i];
  }
}

template <size_t N>
void PrependString(JSContext* cx, StringBuilder<char16_t, N>& v,
                   JSString* str) {
  MOZ_ASSERT(str);
  size_t vlen = v.length();
  size_t alen = str->length();
  if (!v.resize(vlen + alen)) {
    return;
  }

  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return;
  }

  // Move vector data forward. This is safe since we've already resized.
  memmove(v.begin() + alen, v.begin(), vlen * sizeof(char16_t));

  // Copy data to insert.
  CopyChars(v.begin(), *linear);
}

[[nodiscard]] bool ReportErrorIfUnpairedSurrogatePresent(JSContext* cx,
                                                         JSLinearString* str);

[[nodiscard]] JSObject* GetThisObject(JSContext* cx, const CallArgs& args,
                                      const char* msg);

/*******************************************************************************
** Function and struct API definitions
*******************************************************************************/

// for JS error reporting
enum ErrorNum {
#define MSG_DEF(name, count, exception, format) name,
#include "ctypes/ctypes.msg"
#undef MSG_DEF
  CTYPESERR_LIMIT
};

/**
 * ABI constants that specify the calling convention to use.
 * ctypes.default_abi corresponds to the cdecl convention, and in almost all
 * cases is the correct choice. ctypes.stdcall_abi is provided for calling
 * stdcall functions on Win32, and implies stdcall symbol name decoration;
 * ctypes.winapi_abi is just stdcall but without decoration.
 */
enum ABICode {
  ABI_DEFAULT,
  ABI_STDCALL,
  ABI_THISCALL,
  ABI_WINAPI,
  INVALID_ABI
};

enum TypeCode {
  TYPE_void_t,
#define DEFINE_TYPE(name, type, ffiType) TYPE_##name,
  CTYPES_FOR_EACH_TYPE(DEFINE_TYPE)
#undef DEFINE_TYPE
      TYPE_pointer,
  TYPE_function,
  TYPE_array,
  TYPE_struct
};

// Descriptor of one field in a StructType. The name of the field is stored
// as the key to the hash entry.
struct FieldInfo {
  HeapPtr<JSObject*> mType;  // CType of the field
  size_t mIndex;             // index of the field in the struct (first is 0)
  size_t mOffset;            // offset of the field in the struct, in bytes

  void trace(JSTracer* trc) { TraceEdge(trc, &mType, "fieldType"); }
};

struct UnbarrieredFieldInfo {
  JSObject* mType;  // CType of the field
  size_t mIndex;    // index of the field in the struct (first is 0)
  size_t mOffset;   // offset of the field in the struct, in bytes
};
static_assert(sizeof(UnbarrieredFieldInfo) == sizeof(FieldInfo),
              "UnbarrieredFieldInfo should be the same as FieldInfo but with "
              "unbarriered mType");

// Hash policy for FieldInfos.
struct FieldHashPolicy {
  using Key = JSLinearString*;
  using Lookup = Key;

  static HashNumber hash(const Lookup& l) { return js::HashStringChars(l); }

  static bool match(const Key& k, const Lookup& l) {
    return js::EqualStrings(k, l);
  }
};

using FieldInfoHash = GCHashMap<js::HeapPtr<JSLinearString*>, FieldInfo,
                                FieldHashPolicy, CellAllocPolicy>;

// Descriptor of ABI, return type, argument types, and variadicity for a
// FunctionType.
struct FunctionInfo {
  explicit FunctionInfo(JS::Zone* zone) : mArgTypes(zone), mFFITypes(zone) {}

  // Initialized in NewFunctionInfo when !mIsVariadic, but only later, in
  // FunctionType::Call, when mIsVariadic. Not always consistent with
  // mFFITypes, due to lazy initialization when mIsVariadic.
  ffi_cif mCIF;

  // Calling convention of the function. Convert to ffi_abi using GetABI
  // and ObjectValue. Stored as a JSObject* for ease of tracing.
  HeapPtr<JSObject*> mABI;

  // The CType of the value returned by the function.
  HeapPtr<JSObject*> mReturnType;

  // A fixed array of known parameter types, excluding any variadic
  // parameters (if mIsVariadic).
  GCVector<HeapPtr<JSObject*>, 0, CellAllocPolicy> mArgTypes;

  // A variable array of ffi_type*s corresponding to both known parameter
  // types and dynamic (variadic) parameter types. Longer than mArgTypes
  // only if mIsVariadic.
  Vector<ffi_type*, 0, CellAllocPolicy> mFFITypes;

  // Flag indicating whether the function behaves like a C function with
  // ... as the final formal parameter.
  bool mIsVariadic;
};

// Parameters necessary for invoking a JS function from a C closure.
struct ClosureInfo {
  JSContext* cx;
  HeapPtr<JSObject*> closureObj;  // CClosure object
  HeapPtr<JSObject*> typeObj;     // FunctionType describing the C function
  HeapPtr<JSObject*> thisObj;  // 'this' object to use for the JS function call
  HeapPtr<JSObject*> jsfnObj;  // JS function
  void* errResult;       // Result that will be returned if the closure throws
  ffi_closure* closure;  // The C closure itself

  // Anything conditionally freed in the destructor should be initialized to
  // nullptr here.
  explicit ClosureInfo(JSContext* context)
      : cx(context), errResult(nullptr), closure(nullptr) {}

  ~ClosureInfo() {
    if (closure) {
      ffi_closure_free(closure);
    }
    js_free(errResult);
  }
};

bool IsCTypesGlobal(HandleValue v);
bool IsCTypesGlobal(JSObject* obj);

const JS::CTypesCallbacks* GetCallbacks(JSObject* obj);

/*******************************************************************************
** JSClass reserved slot definitions
*******************************************************************************/

enum CTypesGlobalSlot {
  SLOT_CALLBACKS = 0,  // pointer to JS::CTypesCallbacks struct
  SLOT_ERRNO = 1,      // Value for latest |errno|
  SLOT_LASTERROR =
      2,  // Value for latest |GetLastError|, used only with Windows
  CTYPESGLOBAL_SLOTS
};

enum CABISlot {
  SLOT_ABICODE = 0,  // ABICode of the CABI object
  CABI_SLOTS
};

enum CTypeProtoSlot {
  SLOT_POINTERPROTO = 0,   // ctypes.PointerType.prototype object
  SLOT_ARRAYPROTO = 1,     // ctypes.ArrayType.prototype object
  SLOT_STRUCTPROTO = 2,    // ctypes.StructType.prototype object
  SLOT_FUNCTIONPROTO = 3,  // ctypes.FunctionType.prototype object
  SLOT_CDATAPROTO = 4,     // ctypes.CData.prototype object
  SLOT_POINTERDATAPROTO =
      5,  // common ancestor of all CData objects of PointerType
  SLOT_ARRAYDATAPROTO = 6,  // common ancestor of all CData objects of ArrayType
  SLOT_STRUCTDATAPROTO =
      7,  // common ancestor of all CData objects of StructType
  SLOT_FUNCTIONDATAPROTO =
      8,                // common ancestor of all CData objects of FunctionType
  SLOT_INT64PROTO = 9,  // ctypes.Int64.prototype object
  SLOT_UINT64PROTO = 10,   // ctypes.UInt64.prototype object
  SLOT_CTYPES = 11,        // ctypes object
  SLOT_OURDATAPROTO = 12,  // the data prototype corresponding to this object
  CTYPEPROTO_SLOTS
};

enum CTypeSlot {
  SLOT_PROTO = 0,     // 'prototype' property of the CType object
  SLOT_TYPECODE = 1,  // TypeCode of the CType object
  SLOT_FFITYPE = 2,   // ffi_type representing the type
  SLOT_NAME = 3,      // name of the type
  SLOT_SIZE = 4,      // size of the type, in bytes
  SLOT_ALIGN = 5,     // alignment of the type, in bytes
  SLOT_PTR = 6,       // cached PointerType object for type.ptr
  // Note that some of the slots below can overlap, since they're for
  // mutually exclusive types.
  SLOT_TARGET_T = 7,   // (PointerTypes only) 'targetType' property
  SLOT_ELEMENT_T = 7,  // (ArrayTypes only) 'elementType' property
  SLOT_LENGTH = 8,     // (ArrayTypes only) 'length' property
  SLOT_FIELDS = 7,     // (StructTypes only) 'fields' property
  SLOT_FIELDINFO = 8,  // (StructTypes only) FieldInfoHash table
  SLOT_FNINFO = 7,     // (FunctionTypes only) FunctionInfo struct
  SLOT_ARGS_T = 8,     // (FunctionTypes only) 'argTypes' property (cached)
  CTYPE_SLOTS
};

enum CDataSlot {
  SLOT_CTYPE = 0,     // CType object representing the underlying type
  SLOT_REFERENT = 1,  // JSObject this object must keep alive, if any
  SLOT_DATA = 2,      // pointer to a buffer containing the binary data
  SLOT_OWNS = 3,      // TrueValue() if this CData owns its own buffer
  SLOT_FUNNAME = 4,   // JSString representing the function name
  CDATA_SLOTS
};

enum CClosureSlot {
  SLOT_CLOSUREINFO = 0,  // ClosureInfo struct
  CCLOSURE_SLOTS
};

enum CDataFinalizerSlot {
  // PrivateValue storing CDataFinalizer::Private* pointer or UndefinedValue.
  SLOT_DATAFINALIZER_PRIVATE = 0,
  // The type of the value (a CType JSObject).
  // We hold it to permit ImplicitConvert and ToSource.
  SLOT_DATAFINALIZER_VALTYPE = 1,
  // The type of the function used at finalization (a CType JSObject).
  // We hold it to permit |ToSource|.
  SLOT_DATAFINALIZER_CODETYPE = 2,
  CDATAFINALIZER_SLOTS
};

enum TypeCtorSlot {
  SLOT_FN_CTORPROTO = 0  // ctypes.{Pointer,Array,Struct}Type.prototype
  // JSFunction objects always get exactly two slots.
};

enum Int64Slot {
  SLOT_INT64 = 0,  // pointer to a 64-bit buffer containing the integer
  INT64_SLOTS
};

enum Int64FunctionSlot {
  SLOT_FN_INT64PROTO = 0  // ctypes.{Int64,UInt64}.prototype object
  // JSFunction objects always get exactly two slots.
};

/*******************************************************************************
** Object API definitions
*******************************************************************************/

namespace CType {
JSObject* Create(JSContext* cx, HandleObject typeProto, HandleObject dataProto,
                 TypeCode type, JSString* name, HandleValue size,
                 HandleValue align, ffi_type* ffiType);

JSObject* DefineBuiltin(JSContext* cx, HandleObject ctypesObj,
                        const char* propName, JSObject* typeProto,
                        JSObject* dataProto, const char* name, TypeCode type,
                        HandleValue size, HandleValue align, ffi_type* ffiType);

bool IsCType(JSObject* obj);
bool IsCTypeProto(JSObject* obj);
TypeCode GetTypeCode(JSObject* typeObj);
bool TypesEqual(JSObject* t1, JSObject* t2);
size_t GetSize(JSObject* obj);
[[nodiscard]] bool GetSafeSize(JSObject* obj, size_t* result);
bool IsSizeDefined(JSObject* obj);
size_t GetAlignment(JSObject* obj);
ffi_type* GetFFIType(JSContext* cx, JSObject* obj);
JSString* GetName(JSContext* cx, HandleObject obj);
JSObject* GetProtoFromCtor(JSObject* obj, CTypeProtoSlot slot);
JSObject* GetProtoFromType(JSContext* cx, JSObject* obj, CTypeProtoSlot slot);
const JS::CTypesCallbacks* GetCallbacksFromType(JSObject* obj);
}  // namespace CType

namespace PointerType {
JSObject* CreateInternal(JSContext* cx, HandleObject baseType);

JSObject* GetBaseType(JSObject* obj);
}  // namespace PointerType

using UniquePtrFFIType = UniquePtr<ffi_type>;

namespace ArrayType {
JSObject* CreateInternal(JSContext* cx, HandleObject baseType, size_t length,
                         bool lengthDefined);

JSObject* GetBaseType(JSObject* obj);
size_t GetLength(JSObject* obj);
[[nodiscard]] bool GetSafeLength(JSObject* obj, size_t* result);
UniquePtrFFIType BuildFFIType(JSContext* cx, JSObject* obj);
}  // namespace ArrayType

namespace StructType {
[[nodiscard]] bool DefineInternal(JSContext* cx, JSObject* typeObj,
                                  JSObject* fieldsObj);

const FieldInfoHash* GetFieldInfo(JSObject* obj);
const FieldInfo* LookupField(JSContext* cx, JSObject* obj,
                             JSLinearString* name);
JSObject* BuildFieldsArray(JSContext* cx, JSObject* obj);
UniquePtrFFIType BuildFFIType(JSContext* cx, JSObject* obj);
}  // namespace StructType

namespace FunctionType {
JSObject* CreateInternal(JSContext* cx, HandleValue abi, HandleValue rtype,
                         const HandleValueArray& args);

JSObject* ConstructWithObject(JSContext* cx, JSObject* typeObj,
                              JSObject* refObj, PRFuncPtr fnptr,
                              JSObject* result);

FunctionInfo* GetFunctionInfo(JSObject* obj);
void BuildSymbolName(JSContext* cx, JSString* name, JSObject* typeObj,
                     AutoCString& result);
}  // namespace FunctionType

namespace CClosure {
JSObject* Create(JSContext* cx, HandleObject typeObj, HandleObject fnObj,
                 HandleObject thisObj, HandleValue errVal, PRFuncPtr* fnptr);
}  // namespace CClosure

namespace CData {
JSObject* Create(JSContext* cx, HandleObject typeObj, HandleObject refObj,
                 void* data, bool ownResult);

JSObject* GetCType(JSObject* dataObj);
void* GetData(JSObject* dataObj);
bool IsCData(JSObject* obj);
bool IsCDataMaybeUnwrap(MutableHandleObject obj);
bool IsCData(HandleValue v);
bool IsCDataProto(JSObject* obj);

// Attached by JSAPI as the function 'ctypes.cast'
[[nodiscard]] bool Cast(JSContext* cx, unsigned argc, Value* vp);
// Attached by JSAPI as the function 'ctypes.getRuntime'
[[nodiscard]] bool GetRuntime(JSContext* cx, unsigned argc, Value* vp);
}  // namespace CData

namespace Int64 {
bool IsInt64(JSObject* obj);
}  // namespace Int64

namespace UInt64 {
bool IsUInt64(JSObject* obj);
}  // namespace UInt64

}  // namespace ctypes
}  // namespace js

#endif /* ctypes_CTypes_h */