diff --git a/tslang/include/TypeScript/LowerToLLVM/AnyLogic.h b/tslang/include/TypeScript/LowerToLLVM/AnyLogic.h index a75e16451..785c1a49c 100644 --- a/tslang/include/TypeScript/LowerToLLVM/AnyLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/AnyLogic.h @@ -88,7 +88,7 @@ class AnyLogic return memValue; } - mlir::Value UnboxAny(mlir::Value in, mlir::Type resLLVMType) + mlir::Value UnboxAny(mlir::Value in, mlir::Type resLLVMType, bool rawPointer = false) { // TODO: add type id to track data type // TODO: add data size check @@ -104,6 +104,37 @@ class AnyLogic // set actual value auto ptrValue = rewriter.create(loc, ptrTy, dataWithSizeType, inDataWithSizeTypedValue, ValueRange{zero, two}); + + // An Opaque read out of a box holding a smaller value (`0`: an s32) took the + // padding after it as the pointer's high bytes, which only a zeroing allocator left at 0. The + // value is as wide as the box says (ANY_SIZE): its bytes, zero-extended. The data field + // starts 8-aligned, so the 8 bytes read are inside the box either way. + if (rawPointer && isa(resLLVMType)) + { + auto i64Type = th.getI64Type(); + auto ptrSize = rewriter.create(loc, ptrTy, dataWithSizeType, inDataWithSizeTypedValue, + ValueRange{zero, zero}); + mlir::Value size = rewriter.create(loc, llvmIndexType, ptrSize); + if (llvmIndexType != i64Type) + { + size = rewriter.create(loc, i64Type, size); + } + + mlir::Value raw = rewriter.create(loc, i64Type, ptrValue); + + auto eight = rewriter.create(loc, i64Type, rewriter.getI64IntegerAttr(8)); + auto one64 = rewriter.create(loc, i64Type, rewriter.getI64IntegerAttr(1)); + auto isWide = rewriter.create(loc, LLVM::ICmpPredicate::uge, size, eight); + // for a narrower value: (1 << size * 8) - 1, its bytes' mask (a shift by 64 is poison, + // so the select keeps it out of the wide case) + auto narrowSize = rewriter.create(loc, isWide, rewriter.create(loc, i64Type, rewriter.getI64IntegerAttr(0)), size); + auto bits = rewriter.create(loc, narrowSize, eight); + auto mask = rewriter.create(loc, rewriter.create(loc, one64, bits), one64); + auto narrowed = rewriter.create(loc, raw, mask); + mlir::Value value = rewriter.create(loc, isWide, raw, narrowed); + return rewriter.create(loc, resLLVMType, value); + } + return rewriter.create(loc, resLLVMType, ptrValue); } diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 3ee622a6a..3f9a9c129 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -3535,7 +3535,7 @@ struct UnboxOpLowering : public TsLlvmPattern auto resType = op.getRes().getType(); AnyLogic al(op, rewriter, tch, loc, tsLlvmContext->compileOptions); - auto result = al.UnboxAny(in, tch.convertType(resType)); + auto result = al.UnboxAny(in, tch.convertType(resType), isa(resType)); rewriter.replaceOp(op, result); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index a010eae88..6f17f8849 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1814,6 +1814,12 @@ foreach(issues_mm gc rc none own) tslang_add_test(NAME test-jit-00-tslang-issue-29-thread-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issue_29_thread_test}") tslang_add_test(NAME test-compile-00-tslang-issue-29-thread-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issue_29_thread_test}") endforeach() +# an Opaque out of an `any` is as wide as the value in the box: `0` is null (the +# padding after a 4-byte value was read as the pointer's high bytes) +foreach(issues_mm gc rc none own) + tslang_add_test(NAME test-jit-00-any-to-opaque-narrow-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_opaque_narrow.ts") + tslang_add_test(NAME test-compile-00-any-to-opaque-narrow-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_opaque_narrow.ts") +endforeach() # ...and a closure, which needs its captured variables, can't be one add_test(NAME test-compile-cdecl-closure-error COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/cdecl_closure_error.ts") diff --git a/tslang/test/tester/tests/00any_to_opaque_narrow.ts b/tslang/test/tester/tests/00any_to_opaque_narrow.ts new file mode 100644 index 000000000..4dcfd6000 --- /dev/null +++ b/tslang/test/tester/tests/00any_to_opaque_narrow.ts @@ -0,0 +1,36 @@ +// `0` read 8 bytes out of a box holding a 4-byte s32: the padding after it became the +// pointer's high bytes, garbage wherever the allocator does not zero memory (-mm=rc, none, own), so +// the "null" pointer was not null. An Opaque out of an `any` is as wide as the value in the box. + +function opaqueOf(value: any): Opaque { + return value; +} + +function isNull(p: Opaque): boolean { + return p == 0; +} + +// leaves freed blocks of a box's size filled with ones, so a box made next is not zeroed by luck +// (where blocks are freed: rc, own) +function dirty() { + for (let i = 0; i < 4; i++) { + const junk: u64[] = []; + for (let j = 0; j < 4; j++) junk.push(0xFFFFFFFFFFFFFFFF); + } +} + +function main() { + for (let i = 0; i < 100; i++) { + dirty(); + const none: Opaque = 0; + assert(isNull(none), "an s32 0 in an any is a null Opaque"); + } + + assert(isNull(opaqueOf(0)), "through a parameter"); + assert(opaqueOf(0x1234) == 0x1234, "an s32 keeps its value"); + assert(opaqueOf(0xAB) == 0xAB, "a u8"); + assert(opaqueOf(0xABCD) == 0xABCD, "a u16"); + assert(opaqueOf(0x123456789A) == 0x123456789A, "a u64 is all 8 bytes"); + + print("done."); +}