Start doing more validation of intrinsics: assert, new, delete, rand (#2766)

This is adding more validation of intrinsics. It addresses a bug in rand where CHECK-fails would occur for bad range inputs, instead of a runtime error. I'm also addressing what I think was an int32 range issue in the handling by running the generator with int64.
This commit is contained in:
Jon Ross-Perkins
2023-04-13 12:53:34 -07:00
committed by GitHub
parent d52836e854
commit d7978deb3a
23 changed files with 360 additions and 20 deletions
+17 -6
View File
@@ -7,6 +7,7 @@
#include <llvm/Support/raw_ostream.h>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <optional>
@@ -40,8 +41,6 @@ using llvm::isa;
namespace Carbon {
static std::mt19937 generator(12);
// Constructs an ActionStack suitable for the specified phase.
static auto MakeTodo(Phase phase, Nonnull<Heap*> heap) -> ActionStack {
switch (phase) {
@@ -1593,12 +1592,24 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
}
case IntrinsicExpression::Intrinsic::Rand: {
CARBON_CHECK(args.size() == 2);
const auto& low = cast<IntValue>(*args[0]).value();
const auto& high = cast<IntValue>(*args[1]).value();
CARBON_CHECK(high > low);
const int64_t low = cast<IntValue>(*args[0]).value();
const int64_t high = cast<IntValue>(*args[1]).value();
if (low >= high) {
return ProgramError(exp.source_loc())
<< "Rand inputs must be ordered for a non-empty range: "
<< low << " must be less than " << high;
}
// Use 64-bit to handle large ranges where `high - low` might exceed
// int32_t maximums.
static std::mt19937_64 generator(12);
const int64_t range = high - low;
// We avoid using std::uniform_int_distribution because it's not
// reproducible across builds/platforms.
int r = (generator() % (high - low)) + low;
int64_t r = (generator() % range) + low;
CARBON_CHECK(r >= std::numeric_limits<int32_t>::min() &&
r <= std::numeric_limits<int32_t>::max())
<< "Non-int32 result: " << r;
CARBON_CHECK(r >= low && r <= high) << "Out-of-range result: " << r;
return todo_.FinishAction(arena_->New<IntValue>(r));
}
case IntrinsicExpression::Intrinsic::ImplicitAs: {
+1 -1
View File
@@ -3666,7 +3666,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
case IntrinsicExpression::Intrinsic::Dealloc: {
if (args.size() != 1) {
return ProgramError(e->source_loc())
<< "__intrinsic_new takes 1 argument";
<< "__intrinsic_delete takes 1 argument";
}
const auto* arg_type = &args[0]->static_type();
CARBON_RETURN_IF_ERROR(
+15
View File
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
Assert(true, "HALLO WELT");
return 0;
}
+22
View File
@@ -0,0 +1,22 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: result: 0
package ExplorerTest api;
class ConvertTo(T:! type) {
var v: T;
impl as ImplicitAs(T) {
fn Convert[self: Self]() -> T { return self.v; }
}
}
fn Main() -> i32 {
Assert({.v = true} as ConvertTo(bool), {.v = "Pass"} as ConvertTo(String));
return 0;
}
-1
View File
@@ -10,7 +10,6 @@
package ExplorerTest api;
fn Main() -> i32 {
Assert(true, "HALLO WELT");
Assert(false, "HALLO WELT");
return 0;
}
+2 -5
View File
@@ -5,6 +5,7 @@
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/data/prelude.carbon:{{.*}}: "Fail"
package ExplorerTest api;
@@ -16,10 +17,6 @@ class ConvertTo(T:! type) {
}
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assert/fail_convert.carbon:[[@LINE+3]]: type error in __intrinsic_assert argument 0
// CHECK:STDERR: expected: bool
// CHECK:STDERR: actual: class ConvertTo(T = bool)
__intrinsic_assert({.v = true} as ConvertTo(bool), {.v = "Pass"} as ConvertTo(String));
__intrinsic_assert({.v = false} as ConvertTo(bool), {.v = "Fail"} as ConvertTo(String));
Assert({.v = false} as ConvertTo(bool), {.v = "Fail"} as ConvertTo(String));
return 0;
}
@@ -0,0 +1,22 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
class ConvertTo(T:! type) {
var v: T;
impl as ImplicitAs(T) {
fn Convert[self: Self]() -> T { return self.v; }
}
}
fn Main() -> i32 {
// CHECK:STDERR: SYNTAX ERROR: {{.*}}/explorer/testdata/assert/fail_intrinsic_bool_type.carbon:[[@LINE+1]]: syntax error, unexpected COMMA
__intrinsic_assert(, "unused");
return 0;
}
+22
View File
@@ -0,0 +1,22 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
class ConvertTo(T:! type) {
var v: T;
impl as ImplicitAs(T) {
fn Convert[self: Self]() -> T { return self.v; }
}
}
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assert/fail_intrinsic_no_args.carbon:[[@LINE+1]]: __intrinsic_assert takes 2 arguments
__intrinsic_assert();
return 0;
}
@@ -0,0 +1,24 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
class ConvertTo(T:! type) {
var v: T;
impl as ImplicitAs(T) {
fn Convert[self: Self]() -> T { return self.v; }
}
}
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assert/fail_intrinsic_no_convert.carbon:[[@LINE+3]]: type error in __intrinsic_assert argument 0
// CHECK:STDERR: expected: bool
// CHECK:STDERR: actual: class ConvertTo(T = bool)
__intrinsic_assert({.v = true} as ConvertTo(bool), {.v = "Pass"} as ConvertTo(String));
return 0;
}
+24
View File
@@ -0,0 +1,24 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
class ConvertTo(T:! type) {
var v: T;
impl as ImplicitAs(T) {
fn Convert[self: Self]() -> T { return self.v; }
}
}
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assert/fail_intrinsic_str_type.carbon:[[@LINE+3]]: type error in __intrinsic_assert argument 1
// CHECK:STDERR: expected: String
// CHECK:STDERR: actual: i32
__intrinsic_assert(true, 1);
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
var p: i32*;
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/testdata/pointer/fail_delete_uninit.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<p>>
heap.Delete(p);
return 0;
}
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_intrinsic_delete_no_args.carbon:[[@LINE+1]]: __intrinsic_delete takes 1 argument
__intrinsic_delete();
return 0;
}
@@ -0,0 +1,18 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
var x: i32;
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_intrinsic_delete_type.carbon:[[@LINE+3]]: type error in *
// CHECK:STDERR: expected a pointer type
// CHECK:STDERR: actual: i32
__intrinsic_delete(x);
return 0;
}
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_intrinsic_new_no_args.carbon:[[@LINE+1]]: __intrinsic_new takes 1 argument
__intrinsic_new();
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: result: 0
package ExplorerTest api;
class C {
fn DoPrint() { Print("test"); }
}
fn Main() -> i32 {
var p: auto = heap.New(C);
var y: auto = *p;
// The following line would fail with: "could not find `y: auto`"
// y.DoPrint();
heap.Delete(p);
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: test
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn F() { Print("test"); }
fn Main() -> i32 {
var p: auto = heap.New(F);
var y: auto = *p;
y();
heap.Delete(p);
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/data/prelude.carbon:{{.*}}: Rand inputs must be ordered for a non-empty range: 0 must be less than 0
package ExplorerTest api;
fn Main() -> i32 {
Rand(0, 0);
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/random/fail_intrinsic_no_args.carbon:[[@LINE+1]]: Rand takes 2 arguments, received 0
__intrinsic_rand();
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/random/fail_intrinsic_type0.carbon:[[@LINE+3]]: type error in Rand argument 0
// CHECK:STDERR: expected: i32
// CHECK:STDERR: actual: String
__intrinsic_rand("a", 1);
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
fn Main() -> i32 {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/random/fail_intrinsic_type1.carbon:[[@LINE+3]]: type error in Rand argument 1
// CHECK:STDERR: expected: i32
// CHECK:STDERR: actual: String
__intrinsic_rand(1, "a");
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/data/prelude.carbon:{{.*}}: Rand inputs must be ordered for a non-empty range: 1 must be less than -1
package ExplorerTest api;
fn Main() -> i32 {
Rand(1, -1);
return 0;
}
+19
View File
@@ -0,0 +1,19 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
// -2147483648 is unsupported as a literal, but we can do it with a decrement.
var low: i32 = -2147483647;
low -= 1;
var high: i32 = 2147483647;
Rand(low, high);
return 0;
}
+5 -7
View File
@@ -9,15 +9,13 @@
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn test(para: i32)->bool{
return true;
}
fn Main() -> i32 {
var i : i32 = Rand(0,100);
var j : i32 = Rand(0,100);
if(i == j){
var i: i32 = Rand(0, 100);
var j: i32 = Rand(0, 100);
if (i == j) {
Print("HALLO WELT");
}else{
} else {
Print("Nice!");
}
return 0;